feat: get rid of gc and cyclic thunk
This commit is contained in:
@@ -2,7 +2,6 @@ use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
use derive_more::Constructor;
|
||||
use gc_arena::Collect;
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use itertools::Itertools;
|
||||
|
||||
@@ -17,12 +16,6 @@ pub struct AttrSet<'gc> {
|
||||
data: HashMap<usize, Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for AttrSet<'gc> {
|
||||
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, cc: &mut T) {
|
||||
self.data.trace(cc);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
||||
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||
Self { data }
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::cell::Cell;
|
||||
|
||||
use gc_arena::lock::{GcRefLock, RefLock};
|
||||
use gc_arena::{Collect, Gc, Mutation};
|
||||
use std::cell::{Cell, OnceCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::bytecode::Func as BFunc;
|
||||
use crate::env::VmEnv;
|
||||
@@ -11,8 +9,7 @@ use crate::jit::JITFunc;
|
||||
use crate::ty::internal::Value;
|
||||
use crate::vm::VM;
|
||||
|
||||
#[derive(Debug, Clone, Collect)]
|
||||
#[collect(no_drop)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
Ident(usize),
|
||||
Formals {
|
||||
@@ -44,42 +41,25 @@ impl From<ir::Param> for Param {
|
||||
|
||||
pub struct Func<'gc> {
|
||||
pub func: &'gc BFunc,
|
||||
pub env: Gc<'gc, VmEnv<'gc>>,
|
||||
pub compiled: GcRefLock<'gc, Option<JITFunc<'gc>>>,
|
||||
pub env: Rc<VmEnv<'gc>>,
|
||||
pub compiled: OnceCell<JITFunc<'gc>>,
|
||||
pub count: Cell<usize>,
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for Func<'gc> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
self.env.trace(cc);
|
||||
self.compiled.trace(cc);
|
||||
self.count.trace(cc);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Func<'gc> {
|
||||
pub fn new(func: &'gc BFunc, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) -> Self {
|
||||
pub fn new(func: &'gc BFunc, env: Rc<VmEnv<'gc>>) -> Self {
|
||||
Self {
|
||||
func,
|
||||
env,
|
||||
compiled: Gc::new(mc, RefLock::new(None)),
|
||||
compiled: OnceCell::new(),
|
||||
count: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_compile(
|
||||
&self,
|
||||
arg: Value<'gc>,
|
||||
vm: &'gc VM<'gc>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Result<Value<'gc>> {
|
||||
let env = self.env.enter_arg(arg, mc);
|
||||
let compiled = self
|
||||
.compiled
|
||||
.borrow_mut(mc)
|
||||
.get_or_insert_with(|| vm.compile_func(self.func))
|
||||
.clone();
|
||||
let ret = unsafe { compiled(env.as_ref() as *const VmEnv, mc as *const _) };
|
||||
pub fn call_compile(&self, arg: Value<'gc>, vm: &'gc VM<'gc>) -> Result<Value<'gc>> {
|
||||
let env = self.env.clone().enter_arg(arg);
|
||||
let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func));
|
||||
let ret = unsafe { compiled(env.as_ref() as *const VmEnv) };
|
||||
Ok(ret.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use hashbrown::HashSet;
|
||||
use gc_arena::{Collect, Gc, Mutation};
|
||||
use std::rc::Weak;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
|
||||
use crate::env::VmEnv;
|
||||
use crate::ty::public as p;
|
||||
use crate::vm::VM;
|
||||
use crate::env::VmEnv;
|
||||
|
||||
use super::Value;
|
||||
|
||||
@@ -12,17 +13,15 @@ pub struct List<'gc> {
|
||||
data: Vec<Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for List<'gc> {
|
||||
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, cc: &mut T) {
|
||||
self.data.trace(cc);
|
||||
impl<'gc> Default for List<'gc> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> List<'gc> {
|
||||
pub fn new() -> Self {
|
||||
List {
|
||||
data: Vec::new(),
|
||||
}
|
||||
List { data: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn with_capacity(cap: usize) -> Self {
|
||||
@@ -41,10 +40,10 @@ impl<'gc> List<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn capture(&mut self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||
pub fn capture(&mut self, env: &Weak<VmEnv<'gc>>) {
|
||||
self.data.iter().for_each(|v| {
|
||||
if let Value::Thunk(ref thunk) = v.clone() {
|
||||
thunk.capture_env(env, mc);
|
||||
thunk.capture_env_weak(env.clone());
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use std::cell::RefCell;
|
||||
use std::hash::Hash;
|
||||
use std::rc::Rc;
|
||||
use std::rc::{Rc, Weak};
|
||||
|
||||
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 replace_with::replace_with_or_abort;
|
||||
|
||||
@@ -27,9 +25,7 @@ pub use func::*;
|
||||
pub use list::List;
|
||||
pub use primop::*;
|
||||
|
||||
#[derive(IsVariant, Unwrap, Clone, Collect)]
|
||||
#[collect(no_drop)]
|
||||
// #[derive(IsVariant, Unwrap, Clone)]
|
||||
#[derive(IsVariant, Unwrap, Clone)]
|
||||
pub enum Value<'gc> {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
@@ -155,16 +151,15 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(&mut self, arg: Self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
|
||||
pub fn call(&mut self, arg: Self, vm: &VM) -> Result<()> {
|
||||
use Value::*;
|
||||
if matches!(arg, Value::Catchable(_)) {
|
||||
*self = arg;
|
||||
return Ok(());
|
||||
}
|
||||
*self = match self {
|
||||
PrimOp(func) => func.call(arg, vm, mc),
|
||||
PartialPrimOp(func) => func.call(arg, vm, mc),
|
||||
// Value::Func(func) => func.call(arg, vm, mc),
|
||||
PrimOp(func) => func.call(arg, vm),
|
||||
PartialPrimOp(func) => func.call(arg, vm),
|
||||
Catchable(_) => return Ok(()),
|
||||
_ => todo!(),
|
||||
}?;
|
||||
@@ -238,20 +233,18 @@ impl<'gc> Value<'gc> {
|
||||
|
||||
pub fn add(&mut self, other: Self) {
|
||||
use Value::*;
|
||||
replace_with_or_abort(self, |a| {
|
||||
match (a, other) {
|
||||
(Int(a), Int(b)) => Int(a + b),
|
||||
(Int(a), Float(b)) => Float(a as f64 + b),
|
||||
(Float(a), Int(b)) => Float(a + b as f64),
|
||||
(Float(a), Float(b)) => Float(a + b),
|
||||
(String(mut a), String(b)) => {
|
||||
Rc::make_mut(&mut a).push_str(&b);
|
||||
String(a)
|
||||
}
|
||||
(a @ Value::Catchable(_), _) => a,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
replace_with_or_abort(self, |a| match (a, other) {
|
||||
(Int(a), Int(b)) => Int(a + b),
|
||||
(Int(a), Float(b)) => Float(a as f64 + b),
|
||||
(Float(a), Int(b)) => Float(a + b as f64),
|
||||
(Float(a), Float(b)) => Float(a + b),
|
||||
(String(mut a), String(b)) => {
|
||||
Rc::make_mut(&mut a).push_str(&b);
|
||||
String(a)
|
||||
}
|
||||
(a @ Value::Catchable(_), _) => a,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -430,54 +423,73 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Collect)]
|
||||
#[collect(no_drop)]
|
||||
#[derive(Clone)]
|
||||
pub struct Thunk<'gc> {
|
||||
pub thunk: GcRefLock<'gc, _Thunk<'gc>>,
|
||||
pub thunk: Rc<RefCell<_Thunk<'gc>>>,
|
||||
}
|
||||
|
||||
#[derive(IsVariant, Unwrap)]
|
||||
pub enum _Thunk<'gc> {
|
||||
Code(&'gc OpCodes, Option<Gc<'gc, VmEnv<'gc>>>),
|
||||
Code(&'gc OpCodes, Option<Env<'gc>>),
|
||||
Suspended,
|
||||
Value(Value<'gc>),
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for _Thunk<'gc> {
|
||||
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, cc: &mut T) {
|
||||
use _Thunk::*;
|
||||
match self {
|
||||
Code(_, env) => env.trace(cc),
|
||||
Value(val) => val.trace(cc),
|
||||
_ => (),
|
||||
#[derive(Unwrap)]
|
||||
pub enum Env<'gc> {
|
||||
Strong(Rc<VmEnv<'gc>>),
|
||||
Weak(Weak<VmEnv<'gc>>),
|
||||
}
|
||||
|
||||
impl Env<'_> {
|
||||
fn upgrade(self) -> Self {
|
||||
if let Self::Weak(weak) = self {
|
||||
Env::Strong(weak.upgrade().unwrap())
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Thunk<'gc> {
|
||||
pub fn new(opcodes: &'gc OpCodes, mc: &Mutation<'gc>) -> Self {
|
||||
pub fn new(opcodes: &'gc OpCodes) -> Self {
|
||||
Thunk {
|
||||
thunk: Gc::new(mc, RefLock::new(_Thunk::Code(opcodes, None))),
|
||||
thunk: RefCell::new(_Thunk::Code(opcodes, None)).into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn capture_env(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||
if let _Thunk::Code(_, envcell) = &mut *self.thunk.borrow_mut(mc) {
|
||||
let _ = envcell.insert(env);
|
||||
pub fn capture_env(&self, env: Rc<VmEnv<'gc>>) {
|
||||
if let _Thunk::Code(_, envcell) = &mut *self.thunk.borrow_mut() {
|
||||
let _ = envcell.insert(Env::Strong(env));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn suspend(&self, mc: &Mutation<'gc>) -> Result<(&'gc OpCodes, Gc<'gc, VmEnv<'gc>>)> {
|
||||
let _Thunk::Code(opcodes, env) =
|
||||
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended)
|
||||
else {
|
||||
return Err(Error::EvalError("infinite recursion encountered".into()));
|
||||
};
|
||||
Ok((opcodes, env.unwrap()))
|
||||
pub fn capture_env_weak(&self, env: Weak<VmEnv<'gc>>) {
|
||||
if let _Thunk::Code(_, envcell) = &mut *self.thunk.borrow_mut() {
|
||||
let _ = envcell.insert(Env::Weak(env));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_value(&self, value: Value<'gc>, mc: &Mutation<'gc>) {
|
||||
*self.thunk.borrow_mut(mc) = _Thunk::Value(value);
|
||||
pub fn upgrade(&self) {
|
||||
replace_with_or_abort(&mut *self.thunk.borrow_mut(), |this| {
|
||||
if let _Thunk::Code(opcodes, envcell) = this {
|
||||
_Thunk::Code(opcodes, envcell.map(Env::upgrade))
|
||||
} else {
|
||||
this
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn suspend(&self) -> Result<(&'gc OpCodes, Rc<VmEnv<'gc>>)> {
|
||||
use _Thunk::*;
|
||||
match std::mem::replace(&mut *self.thunk.borrow_mut(), _Thunk::Suspended) {
|
||||
Code(opcodes, env) => Ok((opcodes, env.unwrap().upgrade().unwrap_strong())),
|
||||
_ => Err(Error::EvalError("infinite recursion encountered".into())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_value(&self, value: Value<'gc>) {
|
||||
*self.thunk.borrow_mut() = _Thunk::Value(value);
|
||||
}
|
||||
|
||||
pub fn get_value(&self) -> Option<&Value<'gc>> {
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use derive_more::Constructor;
|
||||
use gc_arena::Collect;
|
||||
use gc_arena::Mutation;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::vm::VM;
|
||||
|
||||
use super::Value;
|
||||
|
||||
#[derive(Debug, Clone, Constructor, Collect)]
|
||||
#[collect(require_static)]
|
||||
#[derive(Debug, Clone, Constructor)]
|
||||
pub struct PrimOp {
|
||||
pub name: &'static str,
|
||||
arity: usize,
|
||||
func: for<'gc> fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||
func: for<'gc> fn(Vec<Value<'gc>>, &VM) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PrimOp {
|
||||
@@ -24,21 +21,19 @@ impl PartialEq for PrimOp {
|
||||
}
|
||||
|
||||
impl PrimOp {
|
||||
pub fn call<'gc>(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||
pub fn call<'gc>(&self, arg: Value<'gc>, vm: &VM) -> Result<Value<'gc>> {
|
||||
let mut args = Vec::with_capacity(self.arity);
|
||||
args.push(arg);
|
||||
if self.arity > 1 {
|
||||
Value::PartialPrimOp(Rc::new(
|
||||
PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - 1,
|
||||
args,
|
||||
func: self.func,
|
||||
},
|
||||
))
|
||||
Value::PartialPrimOp(Rc::new(PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - 1,
|
||||
args,
|
||||
func: self.func,
|
||||
}))
|
||||
.ok()
|
||||
} else {
|
||||
(self.func)(args, vm, mc)
|
||||
(self.func)(args, vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,15 +43,7 @@ pub struct PartialPrimOp<'gc> {
|
||||
pub name: &'static str,
|
||||
arity: usize,
|
||||
args: Vec<Value<'gc>>,
|
||||
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for PartialPrimOp<'gc> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
for v in self.args.iter() {
|
||||
v.trace(cc);
|
||||
}
|
||||
}
|
||||
func: fn(Vec<Value<'gc>>, &VM) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PartialPrimOp<'_> {
|
||||
@@ -66,19 +53,14 @@ impl PartialEq for PartialPrimOp<'_> {
|
||||
}
|
||||
|
||||
impl<'gc> PartialPrimOp<'gc> {
|
||||
pub fn call(
|
||||
self: &mut Rc<Self>,
|
||||
arg: Value<'gc>,
|
||||
vm: &VM,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Result<Value<'gc>> {
|
||||
pub fn call(self: &mut Rc<Self>, arg: Value<'gc>, vm: &VM) -> Result<Value<'gc>> {
|
||||
let func = self.func;
|
||||
let Some(ret) = ({
|
||||
let self_mut = Rc::make_mut(self);
|
||||
self_mut.args.push(arg);
|
||||
self_mut.arity -= 1;
|
||||
if self_mut.arity == 0 {
|
||||
Some(func(std::mem::take(&mut self_mut.args), vm, mc))
|
||||
Some(func(std::mem::take(&mut self_mut.args), vm))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user