diff --git a/Cargo.lock b/Cargo.lock index 4335382..26ea76d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,28 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "gc-arena" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cd70cf88a32937834aae9614ff2569b5d9467fa0c42c5d7762fd94a8de88266" +dependencies = [ + "gc-arena-derive", + "sptr", +] + +[[package]] +name = "gc-arena-derive" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c612a69f5557a11046b77a7408d2836fe77077f842171cd211c5ef504bd3cddd" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -319,6 +341,7 @@ dependencies = [ "bumpalo", "derive_more", "ecow", + "gc-arena", "hashbrown 0.15.3", "inkwell", "itertools", @@ -488,6 +511,12 @@ version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "sptr" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a" + [[package]] name = "static_assertions" version = "1.1.0" @@ -505,6 +534,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "text-size" version = "1.1.1" diff --git a/Cargo.toml b/Cargo.toml index 1738066..81ed3f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,5 +30,6 @@ regex = "1.11" hashbrown = "0.15" inkwell = { version = "0.6.0", features = ["llvm18-1"] } bumpalo = { version = "3.17", features = ["allocator-api2"] } +gc-arena = "0.5.3" rustyline = { version = "15.0", optional = true } diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index da6b34a..5ea9137 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -1,59 +1,72 @@ -use std::rc::Rc; - +use gc_arena::{Gc, Mutation}; use hashbrown::HashMap; use crate::env::VmEnv; use crate::ty::common::Const; -use crate::ty::internal::{AttrSet, PrimOp, Value}; +use crate::ty::internal::{AttrSet, CoW, PrimOp, Value}; use crate::vm::VM; -pub fn env<'jit, 'vm>(vm: &'vm VM<'jit>) -> VmEnv<'jit, 'vm> { - let primops = [ - PrimOp::new("add", 2, |_, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); +pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> { + let primops: [PrimOp<'gc>; 7] = [ + PrimOp::new("add", 2, |args, _, _| { + let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else { + unreachable!() + }; first.add(second); first.ok() }), - PrimOp::new("sub", 2, |_, args| { - let [mut first, mut second]: [Value; 2] = args.try_into().unwrap(); + PrimOp::new("sub", 2, |args, _, _| { + let Ok([mut first, mut second]): Result<[Value; 2], _> = args.try_into() else { + unreachable!() + }; second.neg(); first.add(second); first.ok() }), - PrimOp::new("mul", 2, |_, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); + PrimOp::new("mul", 2, |args, _, _| { + let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else { + unreachable!() + }; first.mul(second); first.ok() }), - PrimOp::new("div", 2, |_, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); + PrimOp::new("div", 2, |args, _, _| { + let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else { + unreachable!() + }; first.div(second)?; first.ok() }), - PrimOp::new("lessThan", 2, |_, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); + PrimOp::new("lessThan", 2, |args, _, _| { + let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else { + unreachable!() + }; first.lt(second); first.ok() }), - PrimOp::new("seq", 2, |vm, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); - first.force(vm).unwrap(); + PrimOp::new("seq", 2, |args, vm, mc| { + let Ok([mut first, second]): Result<[_; 2], _> = args.try_into() else { + unreachable!() + }; + first.force(vm, mc).unwrap(); second.ok() }), - PrimOp::new("deepSeq", 2, |vm, args| { - let [mut first, second]: [Value; 2] = args.try_into().unwrap(); - first.force_deep(vm).unwrap(); + PrimOp::new("deepSeq", 2, |args, vm, mc| { + let [mut first, second] = *args.into_boxed_slice() else { + unreachable!() + }; + first.force_deep(vm, mc).unwrap(); second.ok() }), ]; - let mut env_map = HashMap::new_in(&vm.bump); + let mut env_map = HashMap::new(); 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); + let primop = Gc::new(mc, primop); env_map.insert( vm.new_sym(format!("__{}", primop.name)), Value::PrimOp(primop.clone()), @@ -61,12 +74,12 @@ pub fn env<'jit, 'vm>(vm: &'vm VM<'jit>) -> VmEnv<'jit, 'vm> { map.insert(vm.new_sym(primop.name), Value::PrimOp(primop)); } let sym = vm.new_sym("builtins"); - let attrs = Rc::new_cyclic(|weak| { - map.insert(sym, Value::Builtins(weak.clone())); + let attrs = CoW::new_cyclic(|this| { + map.insert(sym, Value::AttrSet(this)); AttrSet::from_inner(map) - }); + }, mc); let builtins = Value::AttrSet(attrs); env_map.insert(sym, builtins); - VmEnv::new(vm.bump.alloc(env_map), &vm.bump) + VmEnv::new(Gc::new(mc, env_map.into()), mc) } diff --git a/src/bytecode.rs b/src/bytecode.rs index 5325601..5524bf5 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -1,6 +1,6 @@ use hashbrown::HashMap; - use ecow::EcoString; +use gc_arena::Collect; use crate::ty::common::Const; use crate::ty::internal::Param; @@ -9,7 +9,8 @@ type Slice = Box<[T]>; pub type OpCodes = Slice; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Collect)] +#[collect(no_drop)] pub enum OpCode { /// load a constant onto stack Const { idx: usize }, @@ -103,7 +104,8 @@ pub enum UnOp { Not, } -#[derive(Debug)] +#[derive(Debug, Collect)] +#[collect(no_drop)] pub struct Func { pub param: Param, pub opcodes: OpCodes, diff --git a/src/env.rs b/src/env.rs index 2cc7ac1..6253801 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,121 +1,103 @@ -use std::fmt::Debug; use std::hash::Hash; -use bumpalo::Bump; -use hashbrown::{DefaultHashBuilder, HashMap}; +use gc_arena::{Collect, Gc, Mutation}; use crate::ty::internal::Value; +use crate::ty::common::Map; -type Map<'bump, K, V> = HashMap; - -#[derive(Clone)] -pub struct Env<'bump, K: Hash + Eq + Clone, V: Clone> { - let_: &'bump LetEnv<'bump, K, V>, - with: &'bump With<'bump, K, V>, - last: Option<&'bump Env<'bump, K, V>>, +#[derive(Collect)] +#[collect(no_drop)] +pub struct Env<'gc, K: Hash + Eq + Collect, V: Collect> { + let_: Gc<'gc, LetEnv<'gc, K, V>>, + with: Gc<'gc, With<'gc, K, V>>, + last: Option>>, } -#[derive(Clone)] -pub struct LetEnv<'bump, K: Hash + Eq + Clone, V: Clone> { - map: LetNode<'bump, K, V>, - last: Option<&'bump LetEnv<'bump, K, V>>, +#[derive(Collect)] +#[collect(no_drop)] +pub struct LetEnv<'gc, K: Hash + Eq + Collect, V: Collect> { + map: LetNode<'gc, K, V>, + last: Option>>, } -impl Debug for Env<'_, K, V> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Env") - .field("let_", &self.let_) - .field("with", &self.with) - .field("last", &self.last) - .finish() - } +pub type VmEnv<'gc> = Env<'gc, usize, Value<'gc>>; + +#[derive(Default, Clone, Collect)] +#[collect(no_drop)] +pub struct With<'gc, K: Hash + Eq + Collect, V: Collect> { + map: Option>>, + last: Option>>, } -impl Debug for LetEnv<'_, K, V> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("LetEnv") - .field("map", &self.map) - .field("last", &self.last) - .finish() - } -} - -pub type VmEnv<'jit, 'vm> = Env<'vm, usize, Value<'jit, 'vm>>; - -#[derive(Debug, Default, Clone)] -pub struct With<'bump, K: Hash + Eq, V> { - map: Option<&'bump Map<'bump, K, V>>, - last: Option<&'bump With<'bump, K, V>>, -} - -#[derive(Debug, Clone)] -enum LetNode<'bump, K: Hash + Eq + Clone, V: Clone> { - Let(&'bump Map<'bump, K, V>), +#[derive(Collect)] +#[collect(no_drop)] +enum LetNode<'gc, K: Hash + Eq + Collect, V: Collect> { + Let(Gc<'gc, Map>), SingleArg(K, V), - MultiArg(&'bump Map<'bump, K, V>), + MultiArg(Gc<'gc, Map>), } -#[derive(Debug, Clone, Copy)] +#[derive(Clone, Copy)] pub enum Type { Arg, Let, With, } -impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> { - pub fn new(map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> Self { - Self { - let_: &*bump.alloc(LetEnv::new(map)), - with: &*bump.alloc(With { +impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> Env<'gc, K, V> { + pub fn new(map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Self { + let_: LetEnv::new(map, mc), + with: Gc::new(mc, With { map: None, last: None, }), last: None, - } + }) } - pub fn lookup(&self, symbol: &K) -> Option<&V> { + pub fn lookup(&'gc self, symbol: &K) -> Option<&'gc V> { if let Some(val) = self.let_.lookup(symbol) { return Some(val); } self.with.lookup(symbol) } - pub fn enter_arg(&'bump self, ident: K, val: V, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Env { - let_: self.let_.enter_arg(ident, val, bump), + pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Env { + let_: self.let_.enter_arg(ident, val, mc), with: self.with, last: Some(self), }) } - pub fn enter_let(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Env { - let_: self.let_.enter_let(map, bump), + pub fn enter_let(self: Gc<'gc, Self>, map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Self { + let_: self.let_.enter_let(map, mc), with: self.with, last: Some(self), }) } - pub fn enter_with(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Env { + pub fn enter_with(self: Gc<'gc, Self>, map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Env { let_: self.let_, - with: self.with.enter(map, bump), + with: self.with.enter(map, mc), last: Some(self), }) } - pub fn leave(&self) -> &Self { + pub fn leave(&self) -> Gc<'gc, Self> { self.last.unwrap() } } -impl<'bump, K: Hash + Eq + Clone, V: Clone> LetEnv<'bump, K, V> { - pub fn new(map: &'bump HashMap) -> Self { - Self { +impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> LetEnv<'gc, K, V> { + pub fn new(map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc,Self { map: LetNode::Let(map), last: None, - } + }) } pub fn lookup(&self, symbol: &K) -> Option<&V> { @@ -135,31 +117,31 @@ impl<'bump, K: Hash + Eq + Clone, V: Clone> LetEnv<'bump, K, V> { self.last.as_ref().and_then(|env| env.lookup(symbol)) } - pub fn enter_arg(&'bump self, ident: K, val: V, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Self { + pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Self { map: LetNode::SingleArg(ident, val), last: Some(self), }) } - pub fn enter_let(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Self { + pub fn enter_let(self: Gc<'gc, Self>, map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Self { map: LetNode::Let(map), last: Some(self), }) } } -impl<'bump, K: Hash + Eq + Clone, V: Clone> With<'bump, K, V> { - pub fn lookup(&'bump self, symbol: &K) -> Option<&'bump V> { +impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> With<'gc, K, V> { + pub fn lookup(&'gc self, symbol: &K) -> Option<&'gc V> { if let Some(val) = self.map.as_ref()?.get(symbol) { return Some(val); } self.last.as_ref().and_then(|env| env.lookup(symbol)) } - pub fn enter(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self { - &*bump.alloc(Self { + pub fn enter(self: Gc<'gc, Self>, map: Gc<'gc, Map>, mc: &Mutation<'gc>) -> Gc<'gc, Self> { + Gc::new(mc, Self { map: Some(map), last: Some(self), }) diff --git a/src/jit/helpers.rs b/src/jit/helpers.rs index ad843e7..e593964 100644 --- a/src/jit/helpers.rs +++ b/src/jit/helpers.rs @@ -1,3 +1,4 @@ +use gc_arena::{Gc, Mutation}; use inkwell::AddressSpace; use inkwell::context::Context; use inkwell::execution_engine::ExecutionEngine; @@ -56,7 +57,7 @@ impl<'ctx> Helpers<'ctx> { "capture_env", context .void_type() - .fn_type(&[value_type.into(), ptr_type.into()], false), + .fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into()], false), None, ); let neg = module.add_function( @@ -202,10 +203,10 @@ extern "C" fn helper_debug(value: JITValue) { dbg!(value.tag); } -extern "C" fn helper_capture_env(thunk: JITValue, env: *const VmEnv) { +extern "C" fn helper_capture_env<'gc>(thunk: JITValue, env: *const VmEnv<'gc>, mc: *const Mutation<'gc>) { let thunk = unsafe { (thunk.data.ptr as *const Thunk).as_ref().unwrap() }; - let env = unsafe { env.as_ref() }.unwrap(); - thunk.capture(env); + let env = unsafe { Gc::from_ptr(env) }; + thunk.capture(env, unsafe { mc.as_ref() }.unwrap()); } extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue { @@ -308,12 +309,12 @@ 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(func: JITValue, arg: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue { use ValueTag::*; match func.tag { Function => { let mut func: Value = func.into(); - func.call(unsafe { vm.as_ref() }.unwrap(), arg.into()) + func.call(arg.into(), unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap()) .unwrap(); func.into() } @@ -321,14 +322,14 @@ extern "C" fn helper_call<'jit>(func: JITValue, arg: JITValue, vm: *const VM<'ji } } -extern "C" fn helper_lookup<'jit, 'vm>(sym: usize, env: *const VmEnv<'jit, 'vm>) -> JITValue { +extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue { let env = unsafe { env.as_ref() }.unwrap(); let val: JITValue = env.lookup(&sym).unwrap().into(); val } -extern "C" fn helper_force<'jit>(thunk: JITValue, vm: *const VM<'jit>) -> JITValue { +extern "C" fn helper_force(thunk: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue { let mut val = Value::from(thunk); - val.force(unsafe { vm.as_ref() }.unwrap()).unwrap(); + val.force(unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap()).unwrap(); val.into() } diff --git a/src/jit/mod.rs b/src/jit/mod.rs index b33c322..2365628 100644 --- a/src/jit/mod.rs +++ b/src/jit/mod.rs @@ -1,5 +1,6 @@ -use std::rc::Rc; +use std::ops::Deref; +use gc_arena::{Collect, Gc}; use inkwell::OptimizationLevel; use inkwell::builder::Builder; use inkwell::context::Context; @@ -55,42 +56,36 @@ pub union JITValueData { ptr: *const (), } -impl<'jit: 'vm, 'vm> From for Value<'jit, 'vm> { +impl<'gc> From for Value<'gc> { fn from(value: JITValue) -> Self { use ValueTag::*; - match value.tag { - List | AttrSet | String | Function | Thunk | Path => unsafe { - Rc::increment_strong_count(value.data.ptr); - }, - _ => (), - } match value.tag { Int => Value::Const(Const::Int(unsafe { value.data.int })), Null => Value::Const(Const::Null), - Function => Value::Func(unsafe { Rc::from_raw(value.data.ptr as *const _) }), - Thunk => Value::Thunk(unsafe { Rc::from_raw(value.data.ptr as *const _) }), + Function => Value::Func(unsafe { Gc::from_ptr(value.data.ptr as *const _) }), + Thunk => Value::Thunk(self::Thunk { thunk: unsafe { Gc::from_ptr(value.data.ptr as *const _) } }), _ => todo!("not implemented for {:?}", value.tag), } } } -impl From<&Value<'_, '_>> for JITValue { - fn from(value: &Value<'_, '_>) -> Self { - match value { +impl From<&Value<'_>> for JITValue { + fn from(value: &Value<'_>) -> Self { + match *value { Value::Const(Const::Int(int)) => JITValue { tag: ValueTag::Int, - data: JITValueData { int: *int }, + data: JITValueData { int }, }, Value::Func(func) => JITValue { tag: ValueTag::Function, data: JITValueData { - ptr: Rc::as_ptr(func) as *const _, + ptr: Gc::as_ptr(func) as *const _, }, }, - Value::Thunk(thunk) => JITValue { + Value::Thunk(ref thunk) => JITValue { tag: ValueTag::Thunk, data: JITValueData { - ptr: Rc::as_ptr(thunk) as *const _, + ptr: Gc::as_ptr(thunk.thunk) as *const _, }, }, _ => todo!(), @@ -98,8 +93,8 @@ impl From<&Value<'_, '_>> for JITValue { } } -impl From> for JITValue { - fn from(value: Value<'_, '_>) -> Self { +impl From> for JITValue { + fn from(value: Value) -> Self { match value { Value::Const(Const::Int(int)) => JITValue { tag: ValueTag::Int, @@ -108,13 +103,13 @@ impl From> for JITValue { Value::Func(func) => JITValue { tag: ValueTag::Function, data: JITValueData { - ptr: Rc::into_raw(func) as *const _, + ptr: Gc::as_ptr(func) as *const _, }, }, Value::Thunk(thunk) => JITValue { tag: ValueTag::Thunk, data: JITValueData { - ptr: Rc::into_raw(thunk) as *const _, + ptr: Gc::as_ptr(thunk.thunk) as *const _, }, }, _ => todo!(), @@ -122,20 +117,43 @@ impl From> for JITValue { } } -pub type JITFunc<'jit, 'vm> = - unsafe extern "C" fn(*const VM<'jit>, *const VmEnv<'jit, 'vm>) -> JITValue; +#[derive(Collect)] +#[collect(require_static)] +pub struct JITFunc<'gc>(JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>); -pub struct JITContext<'ctx> { - context: &'ctx Context, - module: Module<'ctx>, - builder: Builder<'ctx>, - execution_engine: ExecutionEngine<'ctx>, - - helpers: Helpers<'ctx>, +impl<'gc> From, *const VmEnv<'gc>) -> JITValue>> for JITFunc<'gc> { + fn from(value: JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>) -> Self { + Self(value) + } } -impl<'vm, 'ctx: 'vm> JITContext<'ctx> { - pub fn new(context: &'ctx Context) -> Self { +impl<'gc> Deref for JITFunc<'gc> { + type Target = JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +pub struct JITContext<'gc> { + context: &'gc Context, + module: Module<'gc>, + builder: Builder<'gc>, + execution_engine: ExecutionEngine<'gc>, + + helpers: Helpers<'gc>, +} + +unsafe impl<'gc> Collect for JITContext<'gc> { + fn trace(&self, _cc: &gc_arena::Collection) {} + fn needs_trace() -> bool + where + Self: Sized, { + false + } +} + +impl<'gc> JITContext<'gc> { + pub fn new(context: &'gc Context) -> Self { // force linker to link JIT engine unsafe { inkwell::llvm_sys::execution_engine::LLVMLinkInMCJIT(); @@ -156,7 +174,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { } } - pub fn new_ptr(&self, ptr: *const T) -> PointerValue<'ctx> { + pub fn new_ptr(&self, ptr: *const T) -> PointerValue<'gc> { self.builder .build_int_to_ptr( self.helpers.int_type.const_int(ptr as _, false), @@ -166,11 +184,11 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { .unwrap() } - pub fn compile_function<'bump>( + pub fn compile_function( &self, - func: &Func, - vm: &'vm VM<'ctx>, - ) -> Result>> { + func: &'gc Func, + vm: &'gc VM<'gc>, + ) -> Result> { let mut stack = Stack::<_, STACK_SIZE>::new(); let mut iter = func.opcodes.iter().copied(); let func_ = self @@ -190,8 +208,8 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { if func_.verify(true) { unsafe { let name = func_.get_name().to_str().unwrap(); - let addr = self.execution_engine.get_function(name).unwrap(); - Ok(addr) + let func = self.execution_engine.get_function(name).unwrap(); + Ok(func.into()) } } else { todo!() @@ -201,10 +219,10 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { fn build_expr( &self, iter: &mut impl Iterator, - vm: &'vm VM<'_>, - env: PointerValue<'ctx>, - stack: &mut Stack, CAP>, - func: FunctionValue<'ctx>, + vm: &'gc VM<'gc>, + env: PointerValue<'gc>, + stack: &mut Stack, CAP>, + func: FunctionValue<'gc>, mut length: usize, ) -> Result { while length > 1 { @@ -270,9 +288,9 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { fn single_op( &self, opcode: OpCode, - vm: &'vm VM<'_>, - env: PointerValue<'ctx>, - stack: &mut Stack, CAP>, + vm: &'gc VM<'_>, + env: PointerValue<'gc>, + stack: &mut Stack, CAP>, ) -> Result { match opcode { OpCode::Const { idx } => { @@ -287,7 +305,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> { } OpCode::LoadThunk { idx } => stack.push( self.helpers - .new_thunk(Rc::into_raw(Rc::new(Thunk::new(vm.get_thunk(idx))))), + .new_thunk(Thunk::new(vm.get_thunk(idx))), )?, OpCode::CaptureEnv => { let thunk = *stack.tos(); diff --git a/src/jit/test.rs b/src/jit/test.rs index fd92ca4..1f6c2b0 100644 --- a/src/jit/test.rs +++ b/src/jit/test.rs @@ -3,6 +3,7 @@ extern crate test; use bumpalo::Bump; +use gc_arena::Arena; use hashbrown::{HashMap, HashSet}; use inkwell::context::Context; @@ -31,7 +32,6 @@ fn test_expr(expr: &str, expected: Value) { prog.symbols.into(), prog.symmap.into(), prog.consts, - Bump::new(), jit, ); let env = env(&vm); diff --git a/src/lib.rs b/src/lib.rs index 90ee9cc..a5e96b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![cfg_attr(test, feature(test))] #![allow(dead_code)] + #![feature(iter_collect_into)] +#![feature(arbitrary_self_types)] mod builtins; mod bytecode; diff --git a/src/stack.rs b/src/stack.rs index e4ee773..e8489b2 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -1,6 +1,8 @@ use std::mem::{MaybeUninit, replace, transmute}; use std::ops::Deref; +use gc_arena::Collect; + use crate::error::*; macro_rules! into { @@ -18,6 +20,14 @@ pub struct Stack { top: usize, } +unsafe impl Collect for Stack { + fn trace(&self, cc: &gc_arena::Collection) { + for v in self.iter() { + v.trace(cc); + } + } +} + impl Default for Stack { fn default() -> Self { Self::new() diff --git a/src/ty/common.rs b/src/ty/common.rs index 6bfbbdf..4abe234 100644 --- a/src/ty/common.rs +++ b/src/ty/common.rs @@ -1,10 +1,44 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; use std::hash::Hash; +use std::ops::{Deref, DerefMut}; use derive_more::{Constructor, IsVariant, Unwrap}; use ecow::EcoString; +use hashbrown::HashMap; +use gc_arena::Collect; -#[derive(Clone, Debug, PartialEq, Constructor, Hash)] +pub struct Map(HashMap); + +impl Deref for Map { + type Target = HashMap; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Map { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +unsafe impl Collect for Map { + fn trace(&self, cc: &gc_arena::Collection) { + for (k, v) in self.iter() { + k.trace(cc); + v.trace(cc); + } + } +} + +impl From> for Map { + fn from(value: HashMap) -> Self { + Self(value) + } +} + +#[derive(Clone, Debug, PartialEq, Constructor, Hash, Collect)] +#[collect(no_drop)] pub struct Catchable { msg: String, } @@ -15,7 +49,8 @@ impl Display for Catchable { } } -#[derive(Debug, Clone, IsVariant, Unwrap)] +#[derive(Debug, Clone, IsVariant, Unwrap, Collect)] +#[collect(require_static)] pub enum Const { Bool(bool), Int(i64), diff --git a/src/ty/internal/attrset.rs b/src/ty/internal/attrset.rs index 8da2873..079b084 100644 --- a/src/ty/internal/attrset.rs +++ b/src/ty/internal/attrset.rs @@ -1,9 +1,9 @@ use std::rc::Rc; use hashbrown::{HashMap, HashSet}; - use derive_more::Constructor; use itertools::Itertools; +use gc_arena::{Collect, Gc, Mutation}; use crate::env::VmEnv; use crate::error::Result; @@ -13,82 +13,87 @@ use super::super::public as p; use super::Value; #[repr(transparent)] -#[derive(Debug, Default, Constructor, Clone, PartialEq)] -pub struct AttrSet<'jit: 'vm, 'vm> { - data: HashMap>, +#[derive(Constructor, Clone, PartialEq)] +pub struct AttrSet<'gc> { + data: HashMap>, } -impl<'jit, 'vm> From>> for AttrSet<'jit, 'vm> { - fn from(data: HashMap>) -> Self { +unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for AttrSet<'gc> { + fn trace(&self, cc: &gc_arena::Collection) { + for (_, v) in self.data.iter() { + v.trace(cc); + } + } +} + +impl<'gc> From>> for AttrSet<'gc> { + fn from(data: HashMap>) -> Self { Self { data } } } -impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> { +impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> { pub fn with_capacity(cap: usize) -> Self { AttrSet { data: HashMap::with_capacity(cap), } } - pub fn push_attr_force(&mut self, sym: usize, val: Value<'jit, 'vm>) { + pub fn push_attr_force(&mut self, sym: usize, val: Value<'gc>) { self.data.insert(sym, val); } - pub fn push_attr(&mut self, sym: usize, val: Value<'jit, 'vm>) { + pub fn push_attr(&mut self, sym: usize, val: Value<'gc>) { if self.data.get(&sym).is_some() { todo!() } self.data.insert(sym, val); } - pub fn select(&self, sym: usize) -> Option> { - self.data.get(&sym).map(|val| match val { - Value::Builtins(x) => Value::AttrSet(x.upgrade().unwrap()), - val => val.clone(), - }) + pub fn select(&self, sym: usize) -> Option> { + self.data.get(&sym).cloned() } pub fn has_attr(&self, sym: usize) -> bool { self.data.get(&sym).is_some() } - pub fn capture(&mut self, env: &'vm VmEnv<'jit, 'vm>) { + pub fn capture(&mut self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) { self.data.iter().for_each(|(_, v)| { if let Value::Thunk(ref thunk) = v.clone() { - thunk.capture(env); + thunk.capture(env, mc); } }) } - pub fn update(&mut self, other: &AttrSet<'jit, 'vm>) { + pub fn update(&mut self, other: &AttrSet<'gc>) { for (k, v) in other.data.iter() { self.push_attr_force(*k, v.clone()) } } - pub fn as_inner(&self) -> &HashMap> { + pub fn as_inner(&self) -> &HashMap> { &self.data } - pub fn into_inner(self: Rc) -> Rc>> { + pub fn into_inner(self: Rc) -> Rc>> { unsafe { std::mem::transmute(self) } } - pub fn from_inner(data: HashMap>) -> Self { + pub fn from_inner(data: HashMap>) -> Self { Self { data } } - pub fn force_deep(&mut self, vm: &'vm VM<'jit>) -> Result<()> { + pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> { let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect(); for (_, v) in map.iter_mut() { - v.force_deep(vm)?; + v.force_deep(vm, mc)?; } self.data = map.into_iter().collect(); Ok(()) } - pub fn eq_impl(&self, other: &AttrSet<'jit, 'vm>) -> bool { + pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool { self.data.iter().len() == other.data.iter().len() && std::iter::zip( self.data.iter().sorted_by_key(|(k, _)| **k), @@ -97,7 +102,7 @@ impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> { .all(|((_, v1), (_, v2))| v1.eq_impl(v2)) } - pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet>) -> p::Value { + pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet>) -> p::Value { p::Value::AttrSet(p::AttrSet::new( self.data .iter() diff --git a/src/ty/internal/func.rs b/src/ty/internal/func.rs index a7e73f7..d1e5562 100644 --- a/src/ty/internal/func.rs +++ b/src/ty/internal/func.rs @@ -1,9 +1,11 @@ -use std::cell::{Cell, OnceCell}; +use std::cell::Cell; use derive_more::Constructor; +use gc_arena::lock::GcRefLock; use hashbrown::HashMap; use inkwell::execution_engine::JitFunction; use itertools::Itertools; +use gc_arena::{Collect, Gc, Mutation}; use crate::bytecode::Func as BFunc; use crate::env::VmEnv; @@ -13,7 +15,8 @@ use crate::jit::JITFunc; use crate::ty::internal::{Thunk, Value}; use crate::vm::VM; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Collect)] +#[collect(no_drop)] pub enum Param { Ident(usize), Formals { @@ -43,21 +46,29 @@ impl From for Param { } } -#[derive(Debug, Clone, Constructor)] -pub struct Func<'jit: 'vm, 'vm> { - pub func: &'vm BFunc, - pub env: &'vm VmEnv<'jit, 'vm>, - pub compiled: OnceCell>>, +#[derive(Clone, Constructor)] +pub struct Func<'gc> { + pub func: &'gc BFunc, + pub env: Gc<'gc, VmEnv<'gc>>, + pub compiled: GcRefLock<'gc, Option>>, pub count: Cell, } -impl<'vm, 'jit: 'vm> Func<'jit, 'vm> { - pub fn call(&self, vm: &'vm VM<'jit>, arg: Value<'jit, 'vm>) -> Result> { +unsafe impl<'gc> Collect for Func<'gc> { + fn trace(&self, cc: &gc_arena::Collection) { + self.env.trace(cc); + self.compiled.trace(cc); + self.count.trace(cc); + } +} + +impl<'jit: 'vm, 'vm, 'gc> Func<'gc> { + pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result> { use Param::*; let mut env = self.env; env = match self.func.param.clone() { - Ident(ident) => env.enter_arg(ident, arg, &vm.bump), + Ident(ident) => env.enter_arg(ident, arg, mc), Formals { formals, ellipsis, @@ -65,7 +76,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> { } => { let arg = arg.unwrap_attr_set(); let mut new = - HashMap::with_capacity_in(formals.len() + alias.iter().len(), &vm.bump); + HashMap::with_capacity(formals.len() + alias.iter().len()); if !ellipsis && arg .as_inner() @@ -80,7 +91,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> { let arg = arg .select(formal) .or_else(|| { - default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)).into())) + default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into())) }) .unwrap(); new.insert(formal, arg); @@ -88,23 +99,22 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> { if let Some(alias) = alias { new.insert(alias, Value::AttrSet(arg)); } - env.enter_let(vm.bump.alloc(new), &vm.bump) + env.enter_let(Gc::new(mc, new.into()), mc) } }; let count = self.count.get(); self.count.replace(count + 1); if count >= 1 { - let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func)); - let env = env as *const _; - let ret = unsafe { compiled.call(vm as *const VM, env) }; + let compiled = self.compiled.borrow_mut(mc).get_or_insert_with(|| vm.compile_func(self.func)); + let ret = unsafe { compiled.call(vm as *const VM, env.as_ref() as *const VmEnv) }; return Ok(ret.into()); } - vm.eval(self.func.opcodes.iter().copied(), vm.bump.alloc(env)) + vm.eval(self.func.opcodes.iter().copied(), env) } } -impl PartialEq for Func<'_, '_> { +impl PartialEq for Func<'_, '_, '_> { fn eq(&self, _: &Self) -> bool { false } diff --git a/src/ty/internal/list.rs b/src/ty/internal/list.rs index 61ccd88..e660661 100644 --- a/src/ty/internal/list.rs +++ b/src/ty/internal/list.rs @@ -2,6 +2,7 @@ use hashbrown::HashSet; use derive_more::Constructor; use rpds::Vector; +use gc_arena::{Collect, Mutation}; use crate::error::Result; use crate::ty::public as p; @@ -9,38 +10,46 @@ use crate::vm::VM; use super::Value; -#[derive(Debug, Constructor, Clone, PartialEq)] -pub struct List<'jit: 'vm, 'vm> { - data: Vector>, +#[derive(Constructor, Clone, PartialEq)] +pub struct List<'gc> { + data: Vector> } -impl<'jit: 'vm, 'vm> List<'jit, 'vm> { +unsafe impl Collect for List<'_> { + fn trace(&self, cc: &gc_arena::Collection) { + for v in self.data.iter() { + v.trace(cc); + } + } +} + +impl<'gc> List<'gc> { pub fn empty() -> Self { List { data: Vector::new(), } } - pub fn push(&mut self, elem: Value<'jit, 'vm>) { + pub fn push(&mut self, elem: Value<'gc>) { self.data.push_back_mut(elem); } - pub fn concat(&mut self, other: &List<'jit, 'vm>) { + pub fn concat(&mut self, other: &List<'gc>) { for elem in other.data.iter() { self.data.push_back_mut(elem.clone()); } } - pub fn force_deep(&mut self, vm: &'vm VM<'jit>) -> Result<()> { + pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> { let mut vec: Vec<_> = self.data.iter().cloned().collect(); for v in vec.iter_mut() { - v.force_deep(vm)?; + v.force_deep(vm, mc)?; } self.data = vec.into_iter().collect(); Ok(()) } - pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet>) -> p::Value { + pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet>) -> p::Value { p::Value::List(p::List::new( self.data .iter() diff --git a/src/ty/internal/mod.rs b/src/ty/internal/mod.rs index 1b9484d..fde105d 100644 --- a/src/ty/internal/mod.rs +++ b/src/ty/internal/mod.rs @@ -1,10 +1,13 @@ -use hashbrown::HashSet; -use std::cell::OnceCell; -use std::cell::RefCell; +use std::cell::Cell; use std::hash::Hash; -use std::rc::{Rc, Weak}; +use std::mem::MaybeUninit; +use std::ops::Deref; use derive_more::{IsVariant, Unwrap}; +use gc_arena::Mutation; +use gc_arena::lock::RefLock; +use gc_arena::{Collect, Gc, lock::GcRefLock}; +use hashbrown::HashSet; use super::common::*; use super::public as p; @@ -25,38 +28,125 @@ pub use func::*; pub use list::List; pub use primop::*; -#[derive(Debug, IsVariant, Unwrap, Clone)] -pub enum Value<'jit: 'vm, 'vm> { - Const(Const), - Thunk(Rc>), - AttrSet(Rc>), - List(Rc>), - Catchable(Catchable), - PrimOp(Rc>), - PartialPrimOp(Rc>), - Func(Rc>), - Builtins(Weak>), +#[derive(Collect)] +#[collect(unsafe_drop)] +pub struct CoW<'gc, T: Clone + Collect> { + inner: Gc<'gc, CoWInner>, } -impl Hash for Value<'_, '_> { +struct CoWInner { + ref_count: Cell, + data: MaybeUninit, +} + +unsafe impl Collect for CoWInner { + fn trace(&self, cc: &gc_arena::Collection) { + unsafe { self.data.assume_init_ref() }.trace(cc); + } +} + +impl<'gc, T: Clone + Collect> CoW<'gc, T> { + pub fn new(data: T, mc: &Mutation<'gc>) -> Self { + Self { + inner: Gc::new(mc, CoWInner::new(data)), + } + } + + pub fn new_cyclic(datafn: impl FnOnce(Self) -> T, mc: &Mutation<'gc>) -> Self { + let inner = Gc::new( + mc, + CoWInner { + ref_count: Cell::new(1), + data: MaybeUninit::uninit(), + }, + ); + let data = datafn(CoW { inner }); + unsafe { std::mem::transmute::<_, &mut MaybeUninit<_>>(&inner.data) }.write(data); + Self { inner } + } + + pub fn make_mut(&mut self, mc: &Mutation<'gc>) -> &mut T { + if self.inner.ref_count.get() == 1 { + unsafe { std::mem::transmute(self.inner.data.assume_init_ref()) } + } else { + unsafe { + *self = CoW::new(self.inner.data.assume_init_ref().clone(), mc); + std::mem::transmute(self.inner.data.assume_init_ref()) + } + } + } + + pub fn as_ptr(&self) -> *const T { + self.as_ref() as *const T + } +} + +impl<'gc, T: Clone + Collect> AsRef for CoW<'gc, T> { + fn as_ref(&self) -> &T { + unsafe { self.inner.data.assume_init_ref() } + } +} + +impl<'gc, T: Clone + Collect> Deref for CoW<'gc, T> { + type Target = T; + fn deref(&self) -> &Self::Target { + self.as_ref() + } +} + +impl<'gc, T: Clone + Collect> Clone for CoW<'gc, T> { + fn clone(&self) -> Self { + self.inner.ref_count.set(self.inner.ref_count.get() + 1); + Self { inner: self.inner } + } +} + +impl<'gc, T: Clone + Collect> Drop for CoW<'gc, T> { + fn drop(&mut self) { + self.inner.ref_count.set(self.inner.ref_count.get() - 1); + } +} + +impl CoWInner { + fn new(data: T) -> Self { + Self { + ref_count: Cell::new(1), + data: MaybeUninit::new(data), + } + } +} + +#[derive(IsVariant, Unwrap, Clone, Collect)] +#[collect(no_drop)] +pub enum Value<'gc> { + Const(Const), + Thunk(Thunk<'gc>), + AttrSet(CoW<'gc, AttrSet<'gc>>), + List(CoW<'gc, List<'gc>>), + Catchable(Catchable), + PrimOp(Gc<'gc, PrimOp<'gc>>), + PartialPrimOp(CoW<'gc, PartialPrimOp<'gc>>), + Func(Gc<'gc, Func<'gc>>), +} + +impl Hash for Value<'_> { fn hash(&self, state: &mut H) { use Value::*; std::mem::discriminant(self).hash(state); match self { Const(x) => x.hash(state), - Thunk(x) => (x.as_ref() as *const self::Thunk).hash(state), - AttrSet(x) => (x.as_ref() as *const self::AttrSet).hash(state), - List(x) => (x.as_ref() as *const self::List).hash(state), + Thunk(x) => (x as *const self::Thunk).hash(state), + AttrSet(x) => x.as_ptr().hash(state), + List(x) => x.as_ptr().hash(state), Catchable(x) => x.hash(state), PrimOp(x) => (x.as_ref() as *const self::PrimOp).hash(state), PartialPrimOp(x) => (x.as_ref() as *const self::PartialPrimOp).hash(state), Func(x) => (x.as_ref() as *const self::Func).hash(state), - Builtins(x) => (x.as_ptr()).hash(state), } } } -impl<'jit: 'vm, 'vm> Value<'jit, 'vm> { +impl<'gc> Value<'gc> { fn eq_impl(&self, other: &Self) -> bool { use Value::*; match (self, other) { @@ -68,49 +158,46 @@ impl<'jit: 'vm, 'vm> Value<'jit, 'vm> { } } -impl<'jit: 'vm, 'vm> PartialEq for Value<'jit, 'vm> { +impl<'jit: 'vm, 'vm, 'gc> PartialEq for Value<'gc> { fn eq(&self, other: &Self) -> bool { use Value::*; match (self, other) { (Const(a), Const(b)) => a.eq(b), - (AttrSet(a), AttrSet(b)) => { - (a.as_ref() as *const self::AttrSet).eq(&(b.as_ref() as *const _)) - } - (List(a), List(b)) => (a.as_ref() as *const self::List).eq(&(b.as_ref() as *const _)), - (Builtins(a), Builtins(b)) => a.ptr_eq(b), + (AttrSet(a), AttrSet(b)) => a.as_ptr().eq(&b.as_ptr()), + (List(a), List(b)) => a.as_ptr().eq(&b.as_ptr()), _ => false, } } } -impl Eq for Value<'_, '_> {} +impl Eq for Value<'_> {} -#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)] -pub enum ValueAsRef<'v, 'vm: 'v, 'jit: 'vm> { +#[derive(IsVariant, Unwrap, Clone)] +pub enum ValueAsRef<'v, 'gc> { Const(&'v Const), - Thunk(&'v Thunk<'jit, 'vm>), - AttrSet(&'v AttrSet<'jit, 'vm>), - List(&'v List<'jit, 'vm>), + Thunk(&'v Thunk<'gc>), + AttrSet(&'v AttrSet<'gc>), + List(&'v List<'gc>), Catchable(&'v Catchable), - PrimOp(&'v PrimOp<'jit, 'vm>), - PartialPrimOp(&'v PartialPrimOp<'jit, 'vm>), - Func(&'v Func<'jit, 'vm>), + PrimOp(&'v PrimOp<'gc>), + PartialPrimOp(&'v PartialPrimOp<'gc>), + Func(&'v Func<'gc>), } -#[derive(Debug, IsVariant, Unwrap, PartialEq)] -pub enum ValueAsMut<'v, 'vm: 'v, 'jit: 'vm> { +#[derive(IsVariant, Unwrap)] +pub enum ValueAsMut<'v, 'gc> { Const(&'v mut Const), - Thunk(&'v Thunk<'jit, 'vm>), - AttrSet(&'v mut AttrSet<'jit, 'vm>), - List(&'v mut List<'jit, 'vm>), + Thunk(&'v Thunk<'gc>), + AttrSet(&'v mut AttrSet<'gc>), + List(&'v mut List<'gc>), Catchable(&'v mut Catchable), - PrimOp(&'v mut PrimOp<'jit, 'vm>), - PartialPrimOp(&'v mut PartialPrimOp<'jit, 'vm>), - Func(&'v Func<'jit, 'vm>), + PrimOp(&'v PrimOp<'gc>), + PartialPrimOp(&'v mut PartialPrimOp<'gc>), + Func(&'v Func<'gc>), } -impl<'v, 'vm: 'v, 'jit: 'vm> Value<'jit, 'vm> { - pub fn as_ref(&'v self) -> ValueAsRef<'v, 'vm, 'jit> { +impl<'gc, 'v> Value<'gc> { + pub fn as_ref(&'v self) -> ValueAsRef<'v, 'gc> { use Value::*; use ValueAsRef as R; match self { @@ -122,29 +209,27 @@ impl<'v, 'vm: 'v, 'jit: 'vm> Value<'jit, 'vm> { PrimOp(x) => R::PrimOp(x), PartialPrimOp(x) => R::PartialPrimOp(x), Func(x) => R::Func(x), - Builtins(_) => unreachable!(), } } - pub fn as_mut(&'v mut self) -> ValueAsMut<'v, 'vm, 'jit> { + pub fn as_mut(&'v mut self, mc: &Mutation<'gc>) -> ValueAsMut<'v, 'gc> { use Value::*; use ValueAsMut as M; match self { Const(x) => M::Const(x), Thunk(x) => M::Thunk(x), - AttrSet(x) => M::AttrSet(Rc::make_mut(x)), - List(x) => M::List(Rc::make_mut(x)), + AttrSet(x) => M::AttrSet(x.make_mut(mc)), + List(x) => M::List(x.make_mut(mc)), Catchable(x) => M::Catchable(x), - PrimOp(x) => M::PrimOp(Rc::make_mut(x)), - PartialPrimOp(x) => M::PartialPrimOp(Rc::make_mut(x)), + PrimOp(x) => M::PrimOp(x), + PartialPrimOp(x) => M::PartialPrimOp(x.make_mut(mc)), Func(x) => M::Func(x), - Builtins(_) => unreachable!(), } } } use Value::Const as VmConst; -impl<'jit, 'vm> Value<'jit, 'vm> { +impl<'gc> Value<'gc> { pub fn ok(self) -> Result { Ok(self) } @@ -164,7 +249,6 @@ impl<'jit, 'vm> Value<'jit, 'vm> { PrimOp(_) => "lambda", PartialPrimOp(_) => "lambda", Func(_) => "lambda", - Builtins(_) => "set", } } @@ -176,16 +260,16 @@ impl<'jit, 'vm> Value<'jit, 'vm> { } } - pub fn call(&mut self, vm: &'vm VM<'jit>, arg: Self) -> Result<()> { + pub fn call(&mut self, arg: Self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> { use Value::*; if matches!(arg, Value::Catchable(_)) { *self = arg; return Ok(()); } *self = match self { - PrimOp(func) => func.call(vm, arg), - PartialPrimOp(func) => func.call(vm, arg), - Value::Func(func) => func.call(vm, arg), + PrimOp(func) => func.call(arg, vm, mc), + PartialPrimOp(func) => func.call(arg, vm, mc), + Value::Func(func) => func.call(arg, vm, mc), Catchable(_) => return Ok(()), _ => todo!(), }?; @@ -316,9 +400,9 @@ impl<'jit, 'vm> Value<'jit, 'vm> { self } - pub fn push(&mut self, elem: Self) -> &mut Self { + pub fn push(&mut self, elem: Self, mc: &Mutation<'gc>) -> &mut Self { if let Value::List(list) = self { - Rc::make_mut(list).push(elem); + list.make_mut(mc).push(elem); } else if let Value::Catchable(_) = self { } else if let Value::Catchable(_) = elem { *self = elem; @@ -328,23 +412,23 @@ impl<'jit, 'vm> Value<'jit, 'vm> { self } - pub fn concat(&mut self, other: Self) { + pub fn concat(&mut self, other: Self, mc: &Mutation<'gc>) { if let x @ Value::Catchable(_) = other { *self = x; return; } match (self, other) { (Value::List(a), Value::List(b)) => { - Rc::make_mut(a).concat(b.as_ref()); + a.make_mut(mc).concat(b.as_ref()); } (Value::Catchable(_), _) => (), _ => todo!(), } } - pub fn push_attr(&mut self, sym: usize, val: Self) -> &mut Self { + pub fn push_attr(&mut self, sym: usize, val: Self, mc: &Mutation<'gc>) -> &mut Self { if let Value::AttrSet(attrs) = self { - Rc::make_mut(attrs).push_attr(sym, val) + attrs.make_mut(mc).push_attr(sym, val); } else if let Value::Catchable(_) = self { } else if let Value::Catchable(_) = val { *self = val @@ -354,21 +438,21 @@ impl<'jit, 'vm> Value<'jit, 'vm> { self } - pub fn update(&mut self, other: Self) { + pub fn update(&mut self, other: Self, mc: &Mutation<'gc>) { if let x @ Value::Catchable(_) = other { *self = x; return; } match (self, other) { (Value::AttrSet(a), Value::AttrSet(b)) => { - Rc::make_mut(a).update(b.as_ref()); + a.make_mut(mc).update(b.as_ref()); } (Value::Catchable(_), _) => (), _ => todo!(), } } - pub fn select(&mut self, sym: usize, vm: &'vm VM<'jit>) -> Result<&mut Self> { + pub fn select(&mut self, sym: usize, vm: &VM<'gc>) -> Result<&mut Self> { let val = match self { Value::AttrSet(attrs) => attrs .select(sym) @@ -418,106 +502,127 @@ impl<'jit, 'vm> Value<'jit, 'vm> { self } - pub fn force(&mut self, vm: &'vm VM<'jit>) -> Result<&mut Self> { + pub fn force(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> { if let Value::Thunk(thunk) = self { - let value = thunk.force(vm)?.clone(); + let value = thunk.force(vm, mc)?.clone(); *self = value } Ok(self) } - pub fn force_deep(&mut self, vm: &'vm VM<'jit>) -> Result<&mut Self> { + pub fn force_deep(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> { match self { Value::Thunk(thunk) => { - let mut value = thunk.force(vm)?.clone(); - let _ = value.force_deep(vm)?; + let mut value = thunk.force(vm, mc)?.clone(); + let _ = value.force_deep(vm, mc)?; *self = value; } - Value::List(list) => Rc::make_mut(list).force_deep(vm)?, - Value::AttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?, + Value::List(list) => list.make_mut(mc).force_deep(vm, mc)?, + Value::AttrSet(attrs) => attrs.make_mut(mc).force_deep(vm, mc)?, _ => (), } Ok(self) } - pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet>) -> p::Value { + pub fn to_public( + &self, + vm: &VM<'gc>, + seen: &mut HashSet>, + ) -> p::Value { use self::Value::*; use p::Value; if seen.contains(self) { return Value::Repeated; } - seen.insert(self.clone()); match self { - AttrSet(attrs) => attrs.to_public(vm, seen), - List(list) => list.to_public(vm, seen), + AttrSet(attrs) => { + seen.insert(self.clone()); + attrs.to_public(vm, seen) + } + List(list) => { + seen.insert(self.clone()); + list.to_public(vm, seen) + } Catchable(catchable) => Value::Catchable(catchable.clone()), Const(cnst) => Value::Const(cnst.clone()), Thunk(_) => Value::Thunk, PrimOp(primop) => Value::PrimOp(primop.name), PartialPrimOp(primop) => Value::PartialPrimOp(primop.name), Func(_) => Value::Func, - Builtins(x) => x.upgrade().unwrap().to_public(vm, seen), } } } -#[derive(Debug, Clone)] -pub struct Thunk<'jit, 'vm> { - pub thunk: RefCell<_Thunk<'jit, 'vm>>, +#[repr(transparent)] +#[derive(Clone, Collect)] +#[collect(no_drop)] +pub struct Thunk<'gc> { + pub thunk: GcRefLock<'gc, _Thunk<'gc>>, } -#[derive(Debug, IsVariant, Unwrap, Clone)] -pub enum _Thunk<'jit, 'vm> { - Code(&'vm OpCodes, OnceCell<&'vm VmEnv<'jit, 'vm>>), - SuspendedFrom(*const Thunk<'jit, 'vm>), - Value(Value<'jit, 'vm>), +#[derive(IsVariant, Unwrap)] +pub enum _Thunk<'gc> { + Code( + &'gc OpCodes, + GcRefLock<'gc, Option>>>, + ), + Suspended, + Value(Value<'gc>), } -impl<'jit, 'vm> Thunk<'jit, 'vm> { - pub fn new(opcodes: &'vm OpCodes) -> Self { +unsafe impl<'gc> Collect for _Thunk<'gc> { + fn trace(&self, cc: &gc_arena::Collection) { + use _Thunk::*; + match self { + Code(_, env) => env.trace(cc), + Value(val) => val.trace(cc), + _ => (), + } + } +} + +impl<'gc> Thunk<'gc> { + pub fn new(opcodes: &'gc OpCodes, mc: &Mutation<'gc>) -> Self { Thunk { - thunk: RefCell::new(_Thunk::Code(opcodes, OnceCell::new())), + thunk: Gc::new( + mc, + RefLock::new(_Thunk::Code(opcodes, Gc::new(mc, RefLock::new(None)))), + ), } } - pub fn capture(&self, env: &'vm VmEnv<'jit, 'vm>) { + pub fn capture(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) { if let _Thunk::Code(_, envcell) = &*self.thunk.borrow() { - envcell.get_or_init(|| env); + envcell.borrow_mut(mc).insert(env); } } - pub fn force(&self, vm: &'vm VM<'jit>) -> Result<&Value<'jit, 'vm>> { + pub fn force(&self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result> { use _Thunk::*; match &*self.thunk.borrow() { Value(_) => { return Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } { - Value(value) => value, + Value(value) => value.clone(), _ => unreachable!(), }); } - SuspendedFrom(from) => { - return Err(Error::EvalError(format!( - "thunk {:p} already suspended from {from:p} (infinite recursion encountered)", - self as *const Thunk - ))); + Suspended => { + return Err(Error::EvalError("infinite recursion encountered".into())); } Code(..) => (), } - let (opcodes, env) = std::mem::replace( - &mut *self.thunk.borrow_mut(), - _Thunk::SuspendedFrom(self as *const Thunk), - ) - .unwrap_code(); - let env = env.get().unwrap(); + let (opcodes, env) = + std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended).unwrap_code(); + let env = env.as_ref().borrow().unwrap(); let value = vm.eval(opcodes.iter().copied(), env)?; - let _ = std::mem::replace(&mut *self.thunk.borrow_mut(), _Thunk::Value(value)); + let _ = std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Value(value)); Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } { - Value(value) => value, + Value(value) => value.clone(), _ => unreachable!(), }) } - pub fn value(&'vm self) -> Option> { + pub fn value(&self) -> Option> { match &*self.thunk.borrow() { _Thunk::Value(value) => Some(value.clone()), _ => None, @@ -525,7 +630,7 @@ impl<'jit, 'vm> Thunk<'jit, 'vm> { } } -impl PartialEq for Thunk<'_, '_> { +impl PartialEq for Thunk<'_> { fn eq(&self, _: &Self) -> bool { false } diff --git a/src/ty/internal/primop.rs b/src/ty/internal/primop.rs index f9cab25..f20a475 100644 --- a/src/ty/internal/primop.rs +++ b/src/ty/internal/primop.rs @@ -1,74 +1,84 @@ -use std::rc::Rc; - use derive_more::Constructor; +use gc_arena::Mutation; +use gc_arena::Collect; use crate::error::Result; use crate::vm::VM; use super::Value; +use super::CoW; -#[derive(Debug, Clone, Constructor)] -pub struct PrimOp<'jit: 'vm, 'vm> { +#[derive(Debug, Clone, Constructor, Collect)] +#[collect(require_static)] +pub struct PrimOp<'gc> { pub name: &'static str, arity: usize, - func: fn(&'vm VM<'jit>, Vec>) -> Result>, + func: fn(Vec>, &VM, &Mutation<'gc>) -> Result>, } -impl PartialEq for PrimOp<'_, '_> { +impl PartialEq for PrimOp<'_> { fn eq(&self, _: &Self) -> bool { false } } -impl<'jit, 'vm> PrimOp<'jit, 'vm> { - pub fn call(&self, vm: &'vm VM<'jit>, arg: Value<'jit, 'vm>) -> Result> { +impl<'gc> PrimOp<'gc> { + pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result> { let mut args = Vec::with_capacity(self.arity); args.push(arg); if self.arity > 1 { Value::PartialPrimOp( - PartialPrimOp { + CoW::new(PartialPrimOp { name: self.name, arity: self.arity - 1, args, func: self.func, - } - .into(), + }, mc) ) .ok() } else { - (self.func)(vm, args) + (self.func)(args, vm, mc) } } } -#[derive(Debug, Clone)] -pub struct PartialPrimOp<'jit: 'vm, 'vm> { +#[derive(Clone)] +pub struct PartialPrimOp<'gc> { pub name: &'static str, arity: usize, - args: Vec>, - func: fn(&'vm VM<'jit>, Vec>) -> Result>, + args: Vec>, + func: fn(Vec>, &VM, &Mutation<'gc>) -> Result>, } -impl PartialEq for PartialPrimOp<'_, '_> { +unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for PartialPrimOp<'gc> { + fn trace(&self, cc: &gc_arena::Collection) { + for v in self.args.iter() { + v.trace(cc); + } + } +} + +impl PartialEq for PartialPrimOp<'_> { fn eq(&self, _: &Self) -> bool { false } } -impl<'jit: 'vm, 'vm> PartialPrimOp<'jit, 'vm> { +impl<'gc> PartialPrimOp<'gc> { pub fn call( - self: &mut Rc, - vm: &'vm VM<'jit>, - arg: Value<'jit, 'vm>, - ) -> Result> { - let self_mut = Rc::make_mut(self); + self: &mut CoW<'gc, Self>, + arg: Value<'gc>, + vm: &VM, + mc: &Mutation<'gc> + ) -> Result> { + let func = self.func; + let self_mut = self.make_mut(mc); self_mut.args.push(arg); self_mut.arity -= 1; if self_mut.arity > 0 { Value::PartialPrimOp(self.clone()).ok() } else { - let args = std::mem::take(&mut self_mut.args); - (self.func)(vm, args) + func(std::mem::take(&mut self_mut.args), vm, mc) } } } diff --git a/src/vm/mod.rs b/src/vm/mod.rs index af7d535..ca25005 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -1,7 +1,9 @@ -use bumpalo::Bump; +use std::cell::{Cell, RefCell}; + +use gc_arena::lock::{GcRefLock, RefLock}; +use gc_arena::{Arena, Collect, DynamicRootSet, Gc, Mutation, Rootable}; use hashbrown::{HashMap, HashSet}; -use inkwell::execution_engine::JitFunction; -use std::cell::{Cell, OnceCell, RefCell}; +use inkwell::context::Context; use crate::builtins::env; use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp}; @@ -20,37 +22,301 @@ use ecow::EcoString; mod test; const STACK_SIZE: usize = 8 * 1024 / size_of::(); +type GcArena = Arena GcRoot<'gc>]>; -pub fn run(prog: Program, jit: JITContext<'_>) -> Result { - let vm = VM { - thunks: prog.thunks, - funcs: prog.funcs, - 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(), vm.bump.alloc(env))? - .to_public(&vm, &mut seen); - Ok(value) +#[derive(Collect)] +#[collect(require_static)] +struct ContextWrapper(Context); + + +pub fn run(mut prog: Program) -> Result { + fn new<'gc>(prog: &mut Program, mc: &'gc Mutation<'gc>) -> GcRoot<'gc> { + let jit = Gc::new(mc, ContextWrapper(Context::create())); + let thunks = std::mem::take(&mut prog.thunks); + let funcs = std::mem::take(&mut prog.funcs); + let symbols = std::mem::take(&mut prog.symbols); + let symmap = std::mem::take(&mut prog.symmap); + let consts = std::mem::take(&mut prog.consts); + let vm = VM { + thunks, + funcs, + consts, + symbols: RefCell::new(symbols), + symmap: RefCell::new(symmap), + jit: JITContext::new(&jit.as_ref().0), + }; + let vm = Gc::new(mc, vm); + GcRoot { + vm, + jit, + ctxs: DynamicRootSet::new(mc) + } + } + let arena: Arena]> = Arena::new(|mc| { + new(&mut prog, mc) + }); + eval(prog.top_level.into_iter(), &arena, |val| { + Ok(arena.mutate(|_, root: &GcRoot| { + val.to_public(&root.vm, &mut HashSet::new()) + })) + }) } -#[derive(Constructor)] -pub struct VM<'jit> { +fn eval<'gc, T, F: for<'a> FnOnce(Value<'a>) -> Result>( + opcodes: impl Iterator, + arena: &Arena Rootable<'a, Root = GcRoot<'a>>>, + f: F +) -> Result { + let mut iter = opcodes.into_iter(); + let dynroot: gc_arena::DynamicRoot GcRefLock<'a, EvalContext<'a>>]> = arena.mutate(|mc, root| { + root.ctxs.stash(mc, Gc::new(mc, RefLock::new( EvalContext::new(env(&root.vm, mc))))) + }); + while let Some(opcode) = iter.next() { + arena.mutate(|mc, root| { + let mut ctx_mut = root.ctxs.fetch(&dynroot).borrow_mut(mc); + let ctx = &mut *ctx_mut; + let jmp = single_op(&root.vm, opcode, &mut ctx.stack, &mut ctx.env, mc)?; + for _ in 0..jmp { + iter.next().unwrap(); + } + Result::Ok(()) + })?; + } + arena.mutate(|mc, root| { + let mut ctx = root.ctxs.fetch(&dynroot).borrow_mut(mc); + assert_eq!(ctx.stack.len(), 1); + let mut ret = ctx.stack.pop(); + ret.force(&root.vm, mc); + f(ret) + }) +} + +#[inline(always)] +fn single_op<'gc, const CAP: usize>( + vm: &'gc VM<'gc>, + opcode: OpCode, + stack: &mut Stack, CAP>, + env: &mut Gc<'gc, VmEnv<'gc>>, + mc: &'gc Mutation<'gc>, +) -> Result { + match opcode { + OpCode::Illegal => panic!("illegal opcode"), + OpCode::Const { idx } => stack.push(Value::Const(vm.get_const(idx)))?, + OpCode::LoadThunk { idx } => stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?, + OpCode::CaptureEnv => stack.tos().as_ref().unwrap_thunk().capture(*env, mc), + OpCode::ForceValue => { + stack.tos_mut().force(vm, mc)?; + } + OpCode::Jmp { step } => return Ok(step), + OpCode::JmpIfFalse { step } => { + if let Value::Const(Const::Bool(false)) = stack.pop() { + return Ok(step); + } + } + OpCode::Call => { + let arg = stack.pop(); + let func = stack.tos_mut(); + func.force(vm, mc)?; + func.call(arg, vm, mc)?; + } + OpCode::Func { idx } => { + let func = vm.get_func(idx); + let compiled: GcRefLock<'gc, Option>> = Gc::new(mc, RefLock::new(None)); + stack.push(Value::Func(Gc::new( + mc, + Func::new(func, *env, compiled, Cell::new(0)), + )))?; + } + OpCode::UnOp { op } => { + use UnOp::*; + let value = stack.tos_mut(); + value.force(vm, mc)?; + match op { + Neg => value.neg(), + Not => value.not(), + } + } + OpCode::BinOp { op } => { + use BinOp::*; + let mut rhs = stack.pop(); + let lhs = stack.tos_mut(); + lhs.force(vm, mc)?; + rhs.force(vm, mc)?; + match op { + Add => lhs.add(rhs), + Sub => { + rhs.neg(); + lhs.add(rhs); + } + Mul => lhs.mul(rhs), + Div => lhs.div(rhs)?, + And => lhs.and(rhs), + Or => lhs.or(rhs), + Eq => Value::eq(lhs, rhs), + Lt => lhs.lt(rhs), + Con => lhs.concat(rhs, mc), + Upd => lhs.update(rhs, mc), + } + } + OpCode::ConcatString => { + let mut rhs = stack.pop(); + rhs.force(vm, mc)?; + stack.tos_mut().concat_string(rhs); + } + OpCode::Path => { + todo!() + } + OpCode::List => { + stack.push(Value::List(CoW::new(List::empty(), mc)))?; + } + OpCode::PushElem => { + let elem = stack.pop(); + stack.tos_mut().push(elem, mc); + } + OpCode::AttrSet { cap } => { + stack.push(Value::AttrSet(CoW::new(AttrSet::with_capacity(cap), mc)))?; + } + OpCode::FinalizeRec => { + let mut new = HashMap::new(); + stack + .tos() + .as_ref() + .unwrap_attr_set() + .as_inner() + .iter() + .map(|(&k, v)| (k, v.clone())) + .collect_into(&mut new); + *env = env.enter_let(Gc::new(mc, new.into()), mc); + stack + .tos_mut() + .as_mut(mc) + .unwrap_attr_set() + .capture(*env, mc); + } + OpCode::PushStaticAttr { name } => { + let val = stack.pop(); + stack.tos_mut().push_attr(name, val, mc); + } + OpCode::PushDynamicAttr => { + let val = stack.pop(); + let mut sym = stack.pop(); + sym.force(vm, mc)?.coerce_to_string(); + let sym = vm.new_sym(sym.unwrap_const().unwrap_string()); + stack.tos_mut().push_attr(sym, val, mc); + } + OpCode::Select { sym } => { + stack.tos_mut().force(vm, mc)?.select(sym, vm)?; + } + OpCode::SelectOrDefault { sym } => { + let default = stack.pop(); + stack + .tos_mut() + .force(vm, mc)? + .select_with_default(sym, default)?; + } + OpCode::SelectDynamic => { + let mut val = stack.pop(); + val.force(vm, mc)?; + val.coerce_to_string(); + let sym = vm.new_sym(val.unwrap_const().unwrap_string()); + stack.tos_mut().force(vm, mc)?.select(sym, vm)?; + } + OpCode::SelectDynamicOrDefault => { + let default = stack.pop(); + let mut val = stack.pop(); + val.force(vm, mc)?; + val.coerce_to_string(); + let sym = vm.new_sym(val.unwrap_const().unwrap_string()); + stack + .tos_mut() + .force(vm, mc)? + .select_with_default(sym, default)?; + } + OpCode::HasAttr { sym } => { + stack.tos_mut().force(vm, mc)?.has_attr(sym); + } + OpCode::HasDynamicAttr => { + let mut val = stack.pop(); + val.coerce_to_string(); + let sym = vm.new_sym(val.unwrap_const().unwrap_string()); + stack.tos_mut().force(vm, mc)?.has_attr(sym); + } + OpCode::LookUp { sym } => { + stack.push( + env.lookup(&sym) + .ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym))))? + .clone(), + )?; + } + OpCode::EnterLetEnv => { + let mut new = HashMap::new(); + stack + .pop() + .unwrap_attr_set() + .as_inner() + .iter() + .map(|(&k, v)| (k, v.clone())) + .collect_into(&mut new); + *env = env.enter_let(Gc::new(mc, new.into()), mc); + } + OpCode::LeaveLetEnv => *env = env.leave(), + OpCode::EnterWithEnv => { + let mut new = HashMap::new(); + stack + .pop() + .unwrap_attr_set() + .as_inner() + .iter() + .map(|(&k, v)| (k, v.clone())) + .collect_into(&mut new); + *env = env.enter_with(Gc::new(mc, new.into()), mc); + } + OpCode::LeaveWithEnv => *env = env.leave(), + OpCode::Assert => { + if !stack.pop().unwrap_const().unwrap_bool() { + todo!() + } + } + } + Ok(0) +} + +#[derive(Collect)] +#[collect(no_drop)] +pub struct GcRoot<'gc> { + vm: Gc<'gc, VM<'gc>>, + jit: Gc<'gc, ContextWrapper>, + ctxs: DynamicRootSet<'gc> +} + +#[derive(Collect)] +#[collect(no_drop)] +pub struct EvalContext<'gc, const CAP: usize = STACK_SIZE> { + stack: Stack, CAP>, + env: Gc<'gc, VmEnv<'gc>> +} + +impl<'gc, const CAP: usize> EvalContext<'gc, CAP> { + pub fn new(env: Gc<'gc, VmEnv<'gc>>) -> Self { + Self { + stack: Stack::new(), + env + } + } +} + +#[derive(Constructor, Collect)] +#[collect(no_drop)] +pub struct VM<'gc> { thunks: Box<[OpCodes]>, funcs: Box<[F]>, symbols: RefCell>, symmap: RefCell>, consts: Box<[Const]>, - pub bump: Bump, - jit: JITContext<'jit>, + jit: JITContext<'gc>, } -impl<'vm, 'jit: 'vm> VM<'jit> { +impl<'gc> VM<'gc> { pub fn get_thunk(&self, idx: usize) -> &OpCodes { &self.thunks[idx] } @@ -80,219 +346,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> { self.consts[idx].clone() } - pub fn eval( - &'vm self, - opcodes: impl Iterator, - mut env: &'vm VmEnv<'jit, 'vm>, - ) -> Result> { - let mut stack = Stack::<_, STACK_SIZE>::new(); - let mut iter = opcodes.into_iter(); - while let Some(opcode) = iter.next() { - let jmp = self.single_op(opcode, &mut stack, &mut env)?; - for _ in 0..jmp { - iter.next().unwrap(); - } - } - assert_eq!(stack.len(), 1); - let mut ret = stack.pop(); - ret.force(self)?; - Ok(ret) - } - - #[inline(always)] - fn single_op<'s, const CAP: usize>( - &'vm self, - opcode: OpCode, - stack: &'s mut Stack, CAP>, - env: &mut &'vm VmEnv<'jit, 'vm>, - ) -> Result { - match opcode { - OpCode::Illegal => panic!("illegal opcode"), - OpCode::Const { idx } => stack.push(Value::Const(self.get_const(idx)))?, - OpCode::LoadThunk { idx } => { - stack.push(Value::Thunk(Thunk::new(self.get_thunk(idx)).into()))? - } - OpCode::CaptureEnv => stack.tos().as_ref().unwrap_thunk().capture(*env), - OpCode::ForceValue => { - stack.tos_mut().force(self)?; - } - OpCode::Jmp { step } => return Ok(step), - OpCode::JmpIfFalse { step } => { - if let Value::Const(Const::Bool(false)) = stack.pop() { - return Ok(step); - } - } - OpCode::Call => { - let arg = stack.pop(); - let func = stack.tos_mut(); - func.force(self)?; - func.call(self, arg)?; - } - OpCode::Func { idx } => { - let func = self.get_func(idx); - stack.push(Value::Func( - Func::new(func, env, OnceCell::new(), Cell::new(0)).into(), - ))?; - } - OpCode::UnOp { op } => { - use UnOp::*; - let value = stack.tos_mut(); - value.force(self)?; - match op { - Neg => value.neg(), - Not => value.not(), - } - } - OpCode::BinOp { op } => { - use BinOp::*; - let mut rhs = stack.pop(); - let lhs = stack.tos_mut(); - lhs.force(self)?; - rhs.force(self)?; - match op { - Add => lhs.add(rhs), - Sub => { - rhs.neg(); - lhs.add(rhs); - } - Mul => lhs.mul(rhs), - Div => lhs.div(rhs)?, - And => lhs.and(rhs), - Or => lhs.or(rhs), - Eq => Value::eq(lhs, rhs), - Lt => lhs.lt(rhs), - Con => lhs.concat(rhs), - Upd => lhs.update(rhs), - } - } - OpCode::ConcatString => { - let mut rhs = stack.pop(); - rhs.force(self)?; - stack.tos_mut().concat_string(rhs); - } - OpCode::Path => { - todo!() - } - OpCode::List => { - stack.push(Value::List(List::empty().into()))?; - } - OpCode::PushElem => { - let elem = stack.pop(); - stack.tos_mut().push(elem); - } - OpCode::AttrSet { cap } => { - stack.push(Value::AttrSet(AttrSet::with_capacity(cap).into()))?; - } - 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, - ); - stack.tos_mut().as_mut().unwrap_attr_set().capture(env); - } - OpCode::PushStaticAttr { name } => { - let val = stack.pop(); - stack.tos_mut().push_attr(name, val); - } - OpCode::PushDynamicAttr => { - let val = stack.pop(); - let mut sym = stack.pop(); - sym.force(self)?.coerce_to_string(); - let sym = self.new_sym(sym.unwrap_const().unwrap_string()); - stack.tos_mut().push_attr(sym, val); - } - OpCode::Select { sym } => { - stack.tos_mut().force(self)?.select(sym, self)?; - } - OpCode::SelectOrDefault { sym } => { - let default = stack.pop(); - stack - .tos_mut() - .force(self)? - .select_with_default(sym, default)?; - } - OpCode::SelectDynamic => { - let mut val = stack.pop(); - val.force(self)?; - val.coerce_to_string(); - let sym = self.new_sym(val.unwrap_const().unwrap_string()); - stack.tos_mut().force(self)?.select(sym, self)?; - } - OpCode::SelectDynamicOrDefault => { - let default = stack.pop(); - let mut val = stack.pop(); - val.force(self)?; - val.coerce_to_string(); - let sym = self.new_sym(val.unwrap_const().unwrap_string()); - stack - .tos_mut() - .force(self)? - .select_with_default(sym, default)?; - } - OpCode::HasAttr { sym } => { - stack.tos_mut().force(self)?.has_attr(sym); - } - OpCode::HasDynamicAttr => { - let mut val = stack.pop(); - val.coerce_to_string(); - let sym = self.new_sym(val.unwrap_const().unwrap_string()); - stack.tos_mut().force(self)?.has_attr(sym); - } - OpCode::LookUp { sym } => { - stack.push( - env.lookup(&sym) - .ok_or_else(|| { - Error::EvalError(format!("{} not found", self.get_sym(sym))) - })? - .clone(), - )?; - } - 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!() - } - } - } - Ok(0) - } - - pub fn compile_func(&'vm self, func: &'vm F) -> JitFunction<'jit, JITFunc<'jit, 'vm>> { + pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> { self.jit.compile_function(func, self).unwrap() } }