feat: gc-arena (WIP, does not compile)
This commit is contained in:
40
Cargo.lock
generated
40
Cargo.lock
generated
@@ -178,6 +178,28 @@ version = "0.1.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
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]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.14.3"
|
version = "0.14.3"
|
||||||
@@ -319,6 +341,7 @@ dependencies = [
|
|||||||
"bumpalo",
|
"bumpalo",
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"ecow",
|
"ecow",
|
||||||
|
"gc-arena",
|
||||||
"hashbrown 0.15.3",
|
"hashbrown 0.15.3",
|
||||||
"inkwell",
|
"inkwell",
|
||||||
"itertools",
|
"itertools",
|
||||||
@@ -488,6 +511,12 @@ version = "1.13.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sptr"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "static_assertions"
|
name = "static_assertions"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -505,6 +534,17 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"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]]
|
[[package]]
|
||||||
name = "text-size"
|
name = "text-size"
|
||||||
version = "1.1.1"
|
version = "1.1.1"
|
||||||
|
|||||||
@@ -30,5 +30,6 @@ regex = "1.11"
|
|||||||
hashbrown = "0.15"
|
hashbrown = "0.15"
|
||||||
inkwell = { version = "0.6.0", features = ["llvm18-1"] }
|
inkwell = { version = "0.6.0", features = ["llvm18-1"] }
|
||||||
bumpalo = { version = "3.17", features = ["allocator-api2"] }
|
bumpalo = { version = "3.17", features = ["allocator-api2"] }
|
||||||
|
gc-arena = "0.5.3"
|
||||||
|
|
||||||
rustyline = { version = "15.0", optional = true }
|
rustyline = { version = "15.0", optional = true }
|
||||||
|
|||||||
@@ -1,59 +1,72 @@
|
|||||||
use std::rc::Rc;
|
use gc_arena::{Gc, Mutation};
|
||||||
|
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
use crate::env::VmEnv;
|
use crate::env::VmEnv;
|
||||||
use crate::ty::common::Const;
|
use crate::ty::common::Const;
|
||||||
use crate::ty::internal::{AttrSet, PrimOp, Value};
|
use crate::ty::internal::{AttrSet, CoW, PrimOp, Value};
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
pub fn env<'jit, 'vm>(vm: &'vm VM<'jit>) -> VmEnv<'jit, 'vm> {
|
pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
||||||
let primops = [
|
let primops: [PrimOp<'gc>; 7] = [
|
||||||
PrimOp::new("add", 2, |_, args| {
|
PrimOp::new("add", 2, |args, _, _| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
first.add(second);
|
first.add(second);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("sub", 2, |_, args| {
|
PrimOp::new("sub", 2, |args, _, _| {
|
||||||
let [mut first, mut second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, mut second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
second.neg();
|
second.neg();
|
||||||
first.add(second);
|
first.add(second);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("mul", 2, |_, args| {
|
PrimOp::new("mul", 2, |args, _, _| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
first.mul(second);
|
first.mul(second);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("div", 2, |_, args| {
|
PrimOp::new("div", 2, |args, _, _| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
first.div(second)?;
|
first.div(second)?;
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("lessThan", 2, |_, args| {
|
PrimOp::new("lessThan", 2, |args, _, _| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
first.lt(second);
|
first.lt(second);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("seq", 2, |vm, args| {
|
PrimOp::new("seq", 2, |args, vm, mc| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let Ok([mut first, second]): Result<[_; 2], _> = args.try_into() else {
|
||||||
first.force(vm).unwrap();
|
unreachable!()
|
||||||
|
};
|
||||||
|
first.force(vm, mc).unwrap();
|
||||||
second.ok()
|
second.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("deepSeq", 2, |vm, args| {
|
PrimOp::new("deepSeq", 2, |args, vm, mc| {
|
||||||
let [mut first, second]: [Value; 2] = args.try_into().unwrap();
|
let [mut first, second] = *args.into_boxed_slice() else {
|
||||||
first.force_deep(vm).unwrap();
|
unreachable!()
|
||||||
|
};
|
||||||
|
first.force_deep(vm, mc).unwrap();
|
||||||
second.ok()
|
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("true"), Value::Const(Const::Bool(true)));
|
||||||
env_map.insert(vm.new_sym("false"), Value::Const(Const::Bool(false)));
|
env_map.insert(vm.new_sym("false"), Value::Const(Const::Bool(false)));
|
||||||
|
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
for primop in primops {
|
for primop in primops {
|
||||||
let primop = Rc::new(primop);
|
let primop = Gc::new(mc, primop);
|
||||||
env_map.insert(
|
env_map.insert(
|
||||||
vm.new_sym(format!("__{}", primop.name)),
|
vm.new_sym(format!("__{}", primop.name)),
|
||||||
Value::PrimOp(primop.clone()),
|
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));
|
map.insert(vm.new_sym(primop.name), Value::PrimOp(primop));
|
||||||
}
|
}
|
||||||
let sym = vm.new_sym("builtins");
|
let sym = vm.new_sym("builtins");
|
||||||
let attrs = Rc::new_cyclic(|weak| {
|
let attrs = CoW::new_cyclic(|this| {
|
||||||
map.insert(sym, Value::Builtins(weak.clone()));
|
map.insert(sym, Value::AttrSet(this));
|
||||||
AttrSet::from_inner(map)
|
AttrSet::from_inner(map)
|
||||||
});
|
}, mc);
|
||||||
let builtins = Value::AttrSet(attrs);
|
let builtins = Value::AttrSet(attrs);
|
||||||
|
|
||||||
env_map.insert(sym, builtins);
|
env_map.insert(sym, builtins);
|
||||||
VmEnv::new(vm.bump.alloc(env_map), &vm.bump)
|
VmEnv::new(Gc::new(mc, env_map.into()), mc)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
|
use gc_arena::Collect;
|
||||||
|
|
||||||
use crate::ty::common::Const;
|
use crate::ty::common::Const;
|
||||||
use crate::ty::internal::Param;
|
use crate::ty::internal::Param;
|
||||||
@@ -9,7 +9,8 @@ type Slice<T> = Box<[T]>;
|
|||||||
|
|
||||||
pub type OpCodes = Slice<OpCode>;
|
pub type OpCodes = Slice<OpCode>;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub enum OpCode {
|
pub enum OpCode {
|
||||||
/// load a constant onto stack
|
/// load a constant onto stack
|
||||||
Const { idx: usize },
|
Const { idx: usize },
|
||||||
@@ -103,7 +104,8 @@ pub enum UnOp {
|
|||||||
Not,
|
Not,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub struct Func {
|
pub struct Func {
|
||||||
pub param: Param,
|
pub param: Param,
|
||||||
pub opcodes: OpCodes,
|
pub opcodes: OpCodes,
|
||||||
|
|||||||
128
src/env.rs
128
src/env.rs
@@ -1,121 +1,103 @@
|
|||||||
use std::fmt::Debug;
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
use bumpalo::Bump;
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
use hashbrown::{DefaultHashBuilder, HashMap};
|
|
||||||
|
|
||||||
use crate::ty::internal::Value;
|
use crate::ty::internal::Value;
|
||||||
|
use crate::ty::common::Map;
|
||||||
|
|
||||||
type Map<'bump, K, V> = HashMap<K, V, DefaultHashBuilder, &'bump Bump>;
|
#[derive(Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
#[derive(Clone)]
|
pub struct Env<'gc, K: Hash + Eq + Collect, V: Collect> {
|
||||||
pub struct Env<'bump, K: Hash + Eq + Clone, V: Clone> {
|
let_: Gc<'gc, LetEnv<'gc, K, V>>,
|
||||||
let_: &'bump LetEnv<'bump, K, V>,
|
with: Gc<'gc, With<'gc, K, V>>,
|
||||||
with: &'bump With<'bump, K, V>,
|
last: Option<Gc<'gc, Env<'gc, K, V>>>,
|
||||||
last: Option<&'bump Env<'bump, K, V>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Collect)]
|
||||||
pub struct LetEnv<'bump, K: Hash + Eq + Clone, V: Clone> {
|
#[collect(no_drop)]
|
||||||
map: LetNode<'bump, K, V>,
|
pub struct LetEnv<'gc, K: Hash + Eq + Collect, V: Collect> {
|
||||||
last: Option<&'bump LetEnv<'bump, K, V>>,
|
map: LetNode<'gc, K, V>,
|
||||||
|
last: Option<Gc<'gc, LetEnv<'gc, K, V>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Debug + Hash + Eq + Clone, V: Debug + Clone> Debug for Env<'_, K, V> {
|
pub type VmEnv<'gc> = Env<'gc, usize, Value<'gc>>;
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
f.debug_struct("Env")
|
#[derive(Default, Clone, Collect)]
|
||||||
.field("let_", &self.let_)
|
#[collect(no_drop)]
|
||||||
.field("with", &self.with)
|
pub struct With<'gc, K: Hash + Eq + Collect, V: Collect> {
|
||||||
.field("last", &self.last)
|
map: Option<Gc<'gc, Map<K, V>>>,
|
||||||
.finish()
|
last: Option<Gc<'gc, With<'gc, K, V>>>,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Debug + Hash + Eq + Clone, V: Debug + Clone> Debug for LetEnv<'_, K, V> {
|
#[derive(Collect)]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
#[collect(no_drop)]
|
||||||
f.debug_struct("LetEnv")
|
enum LetNode<'gc, K: Hash + Eq + Collect, V: Collect> {
|
||||||
.field("map", &self.map)
|
Let(Gc<'gc, Map<K, V>>),
|
||||||
.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>),
|
|
||||||
SingleArg(K, V),
|
SingleArg(K, V),
|
||||||
MultiArg(&'bump Map<'bump, K, V>),
|
MultiArg(Gc<'gc, Map<K, V>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
Arg,
|
Arg,
|
||||||
Let,
|
Let,
|
||||||
With,
|
With,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'bump, K: Hash + Eq + Clone, V: Clone> Env<'bump, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> Env<'gc, K, V> {
|
||||||
pub fn new(map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> Self {
|
pub fn new(map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Self {
|
Gc::new(mc, Self {
|
||||||
let_: &*bump.alloc(LetEnv::new(map)),
|
let_: LetEnv::new(map, mc),
|
||||||
with: &*bump.alloc(With {
|
with: Gc::new(mc, With {
|
||||||
map: None,
|
map: None,
|
||||||
last: None,
|
last: 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) {
|
if let Some(val) = self.let_.lookup(symbol) {
|
||||||
return Some(val);
|
return Some(val);
|
||||||
}
|
}
|
||||||
self.with.lookup(symbol)
|
self.with.lookup(symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_arg(&'bump self, ident: K, val: V, bump: &'bump Bump) -> &'bump Self {
|
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Env {
|
Gc::new(mc, Env {
|
||||||
let_: self.let_.enter_arg(ident, val, bump),
|
let_: self.let_.enter_arg(ident, val, mc),
|
||||||
with: self.with,
|
with: self.with,
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_let(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self {
|
pub fn enter_let(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Env {
|
Gc::new(mc, Self {
|
||||||
let_: self.let_.enter_let(map, bump),
|
let_: self.let_.enter_let(map, mc),
|
||||||
with: self.with,
|
with: self.with,
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_with(&'bump self, map: &'bump Map<'bump, K, V>, bump: &'bump Bump) -> &'bump Self {
|
pub fn enter_with(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Env {
|
Gc::new(mc, Env {
|
||||||
let_: self.let_,
|
let_: self.let_,
|
||||||
with: self.with.enter(map, bump),
|
with: self.with.enter(map, mc),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn leave(&self) -> &Self {
|
pub fn leave(&self) -> Gc<'gc, Self> {
|
||||||
self.last.unwrap()
|
self.last.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'bump, K: Hash + Eq + Clone, V: Clone> LetEnv<'bump, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> LetEnv<'gc, K, V> {
|
||||||
pub fn new(map: &'bump HashMap<K, V, DefaultHashBuilder, &Bump>) -> Self {
|
pub fn new(map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Self {
|
Gc::new(mc,Self {
|
||||||
map: LetNode::Let(map),
|
map: LetNode::Let(map),
|
||||||
last: None,
|
last: None,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, symbol: &K) -> Option<&V> {
|
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))
|
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 {
|
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Self {
|
Gc::new(mc, Self {
|
||||||
map: LetNode::SingleArg(ident, val),
|
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 {
|
pub fn enter_let(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Self {
|
Gc::new(mc, Self {
|
||||||
map: LetNode::Let(map),
|
map: LetNode::Let(map),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'bump, K: Hash + Eq + Clone, V: Clone> With<'bump, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> With<'gc, K, V> {
|
||||||
pub fn lookup(&'bump self, symbol: &K) -> Option<&'bump V> {
|
pub fn lookup(&'gc self, symbol: &K) -> Option<&'gc V> {
|
||||||
if let Some(val) = self.map.as_ref()?.get(symbol) {
|
if let Some(val) = self.map.as_ref()?.get(symbol) {
|
||||||
return Some(val);
|
return Some(val);
|
||||||
}
|
}
|
||||||
self.last.as_ref().and_then(|env| env.lookup(symbol))
|
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 {
|
pub fn enter(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
&*bump.alloc(Self {
|
Gc::new(mc, Self {
|
||||||
map: Some(map),
|
map: Some(map),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use gc_arena::{Gc, Mutation};
|
||||||
use inkwell::AddressSpace;
|
use inkwell::AddressSpace;
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
use inkwell::execution_engine::ExecutionEngine;
|
use inkwell::execution_engine::ExecutionEngine;
|
||||||
@@ -56,7 +57,7 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
"capture_env",
|
"capture_env",
|
||||||
context
|
context
|
||||||
.void_type()
|
.void_type()
|
||||||
.fn_type(&[value_type.into(), ptr_type.into()], false),
|
.fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into()], false),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
let neg = module.add_function(
|
let neg = module.add_function(
|
||||||
@@ -202,10 +203,10 @@ extern "C" fn helper_debug(value: JITValue) {
|
|||||||
dbg!(value.tag);
|
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 thunk = unsafe { (thunk.data.ptr as *const Thunk).as_ref().unwrap() };
|
||||||
let env = unsafe { env.as_ref() }.unwrap();
|
let env = unsafe { Gc::from_ptr(env) };
|
||||||
thunk.capture(env);
|
thunk.capture(env, unsafe { mc.as_ref() }.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue {
|
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::*;
|
use ValueTag::*;
|
||||||
match func.tag {
|
match func.tag {
|
||||||
Function => {
|
Function => {
|
||||||
let mut func: Value = func.into();
|
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();
|
.unwrap();
|
||||||
func.into()
|
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 env = unsafe { env.as_ref() }.unwrap();
|
||||||
let val: JITValue = env.lookup(&sym).unwrap().into();
|
let val: JITValue = env.lookup(&sym).unwrap().into();
|
||||||
val
|
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);
|
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()
|
val.into()
|
||||||
}
|
}
|
||||||
|
|||||||
112
src/jit/mod.rs
112
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::OptimizationLevel;
|
||||||
use inkwell::builder::Builder;
|
use inkwell::builder::Builder;
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
@@ -55,42 +56,36 @@ pub union JITValueData {
|
|||||||
ptr: *const (),
|
ptr: *const (),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit: 'vm, 'vm> From<JITValue> for Value<'jit, 'vm> {
|
impl<'gc> From<JITValue> for Value<'gc> {
|
||||||
fn from(value: JITValue) -> Self {
|
fn from(value: JITValue) -> Self {
|
||||||
use ValueTag::*;
|
use ValueTag::*;
|
||||||
match value.tag {
|
|
||||||
List | AttrSet | String | Function | Thunk | Path => unsafe {
|
|
||||||
Rc::increment_strong_count(value.data.ptr);
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
match value.tag {
|
match value.tag {
|
||||||
Int => Value::Const(Const::Int(unsafe { value.data.int })),
|
Int => Value::Const(Const::Int(unsafe { value.data.int })),
|
||||||
Null => Value::Const(Const::Null),
|
Null => Value::Const(Const::Null),
|
||||||
Function => Value::Func(unsafe { Rc::from_raw(value.data.ptr as *const _) }),
|
Function => Value::Func(unsafe { Gc::from_ptr(value.data.ptr as *const _) }),
|
||||||
Thunk => Value::Thunk(unsafe { Rc::from_raw(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),
|
_ => todo!("not implemented for {:?}", value.tag),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&Value<'_, '_>> for JITValue {
|
impl From<&Value<'_>> for JITValue {
|
||||||
fn from(value: &Value<'_, '_>) -> Self {
|
fn from(value: &Value<'_>) -> Self {
|
||||||
match value {
|
match *value {
|
||||||
Value::Const(Const::Int(int)) => JITValue {
|
Value::Const(Const::Int(int)) => JITValue {
|
||||||
tag: ValueTag::Int,
|
tag: ValueTag::Int,
|
||||||
data: JITValueData { int: *int },
|
data: JITValueData { int },
|
||||||
},
|
},
|
||||||
Value::Func(func) => JITValue {
|
Value::Func(func) => JITValue {
|
||||||
tag: ValueTag::Function,
|
tag: ValueTag::Function,
|
||||||
data: JITValueData {
|
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,
|
tag: ValueTag::Thunk,
|
||||||
data: JITValueData {
|
data: JITValueData {
|
||||||
ptr: Rc::as_ptr(thunk) as *const _,
|
ptr: Gc::as_ptr(thunk.thunk) as *const _,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -98,8 +93,8 @@ impl From<&Value<'_, '_>> for JITValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Value<'_, '_>> for JITValue {
|
impl From<Value<'_>> for JITValue {
|
||||||
fn from(value: Value<'_, '_>) -> Self {
|
fn from(value: Value) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Value::Const(Const::Int(int)) => JITValue {
|
Value::Const(Const::Int(int)) => JITValue {
|
||||||
tag: ValueTag::Int,
|
tag: ValueTag::Int,
|
||||||
@@ -108,13 +103,13 @@ impl From<Value<'_, '_>> for JITValue {
|
|||||||
Value::Func(func) => JITValue {
|
Value::Func(func) => JITValue {
|
||||||
tag: ValueTag::Function,
|
tag: ValueTag::Function,
|
||||||
data: JITValueData {
|
data: JITValueData {
|
||||||
ptr: Rc::into_raw(func) as *const _,
|
ptr: Gc::as_ptr(func) as *const _,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Value::Thunk(thunk) => JITValue {
|
Value::Thunk(thunk) => JITValue {
|
||||||
tag: ValueTag::Thunk,
|
tag: ValueTag::Thunk,
|
||||||
data: JITValueData {
|
data: JITValueData {
|
||||||
ptr: Rc::into_raw(thunk) as *const _,
|
ptr: Gc::as_ptr(thunk.thunk) as *const _,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -122,20 +117,43 @@ impl From<Value<'_, '_>> for JITValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type JITFunc<'jit, 'vm> =
|
#[derive(Collect)]
|
||||||
unsafe extern "C" fn(*const VM<'jit>, *const VmEnv<'jit, 'vm>) -> JITValue;
|
#[collect(require_static)]
|
||||||
|
pub struct JITFunc<'gc>(JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>);
|
||||||
|
|
||||||
pub struct JITContext<'ctx> {
|
impl<'gc> From<JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>> for JITFunc<'gc> {
|
||||||
context: &'ctx Context,
|
fn from(value: JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>) -> Self {
|
||||||
module: Module<'ctx>,
|
Self(value)
|
||||||
builder: Builder<'ctx>,
|
}
|
||||||
execution_engine: ExecutionEngine<'ctx>,
|
|
||||||
|
|
||||||
helpers: Helpers<'ctx>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
impl<'gc> Deref for JITFunc<'gc> {
|
||||||
pub fn new(context: &'ctx Context) -> Self {
|
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
|
// force linker to link JIT engine
|
||||||
unsafe {
|
unsafe {
|
||||||
inkwell::llvm_sys::execution_engine::LLVMLinkInMCJIT();
|
inkwell::llvm_sys::execution_engine::LLVMLinkInMCJIT();
|
||||||
@@ -156,7 +174,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_ptr<T>(&self, ptr: *const T) -> PointerValue<'ctx> {
|
pub fn new_ptr<T>(&self, ptr: *const T) -> PointerValue<'gc> {
|
||||||
self.builder
|
self.builder
|
||||||
.build_int_to_ptr(
|
.build_int_to_ptr(
|
||||||
self.helpers.int_type.const_int(ptr as _, false),
|
self.helpers.int_type.const_int(ptr as _, false),
|
||||||
@@ -166,11 +184,11 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_function<'bump>(
|
pub fn compile_function(
|
||||||
&self,
|
&self,
|
||||||
func: &Func,
|
func: &'gc Func,
|
||||||
vm: &'vm VM<'ctx>,
|
vm: &'gc VM<'gc>,
|
||||||
) -> Result<JitFunction<'ctx, JITFunc<'ctx, 'vm>>> {
|
) -> Result<JITFunc<'gc>> {
|
||||||
let mut stack = Stack::<_, STACK_SIZE>::new();
|
let mut stack = Stack::<_, STACK_SIZE>::new();
|
||||||
let mut iter = func.opcodes.iter().copied();
|
let mut iter = func.opcodes.iter().copied();
|
||||||
let func_ = self
|
let func_ = self
|
||||||
@@ -190,8 +208,8 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
if func_.verify(true) {
|
if func_.verify(true) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let name = func_.get_name().to_str().unwrap();
|
let name = func_.get_name().to_str().unwrap();
|
||||||
let addr = self.execution_engine.get_function(name).unwrap();
|
let func = self.execution_engine.get_function(name).unwrap();
|
||||||
Ok(addr)
|
Ok(func.into())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
todo!()
|
todo!()
|
||||||
@@ -201,10 +219,10 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
fn build_expr<const CAP: usize>(
|
fn build_expr<const CAP: usize>(
|
||||||
&self,
|
&self,
|
||||||
iter: &mut impl Iterator<Item = OpCode>,
|
iter: &mut impl Iterator<Item = OpCode>,
|
||||||
vm: &'vm VM<'_>,
|
vm: &'gc VM<'gc>,
|
||||||
env: PointerValue<'ctx>,
|
env: PointerValue<'gc>,
|
||||||
stack: &mut Stack<BasicValueEnum<'ctx>, CAP>,
|
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
||||||
func: FunctionValue<'ctx>,
|
func: FunctionValue<'gc>,
|
||||||
mut length: usize,
|
mut length: usize,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
while length > 1 {
|
while length > 1 {
|
||||||
@@ -270,9 +288,9 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
fn single_op<const CAP: usize>(
|
fn single_op<const CAP: usize>(
|
||||||
&self,
|
&self,
|
||||||
opcode: OpCode,
|
opcode: OpCode,
|
||||||
vm: &'vm VM<'_>,
|
vm: &'gc VM<'_>,
|
||||||
env: PointerValue<'ctx>,
|
env: PointerValue<'gc>,
|
||||||
stack: &mut Stack<BasicValueEnum<'ctx>, CAP>,
|
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
match opcode {
|
match opcode {
|
||||||
OpCode::Const { idx } => {
|
OpCode::Const { idx } => {
|
||||||
@@ -287,7 +305,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
|
|||||||
}
|
}
|
||||||
OpCode::LoadThunk { idx } => stack.push(
|
OpCode::LoadThunk { idx } => stack.push(
|
||||||
self.helpers
|
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 => {
|
OpCode::CaptureEnv => {
|
||||||
let thunk = *stack.tos();
|
let thunk = *stack.tos();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
|
use gc_arena::Arena;
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
@@ -31,7 +32,6 @@ fn test_expr(expr: &str, expected: Value) {
|
|||||||
prog.symbols.into(),
|
prog.symbols.into(),
|
||||||
prog.symmap.into(),
|
prog.symmap.into(),
|
||||||
prog.consts,
|
prog.consts,
|
||||||
Bump::new(),
|
|
||||||
jit,
|
jit,
|
||||||
);
|
);
|
||||||
let env = env(&vm);
|
let env = env(&vm);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#![cfg_attr(test, feature(test))]
|
#![cfg_attr(test, feature(test))]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
#![feature(iter_collect_into)]
|
#![feature(iter_collect_into)]
|
||||||
|
#![feature(arbitrary_self_types)]
|
||||||
|
|
||||||
mod builtins;
|
mod builtins;
|
||||||
mod bytecode;
|
mod bytecode;
|
||||||
|
|||||||
10
src/stack.rs
10
src/stack.rs
@@ -1,6 +1,8 @@
|
|||||||
use std::mem::{MaybeUninit, replace, transmute};
|
use std::mem::{MaybeUninit, replace, transmute};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
use gc_arena::Collect;
|
||||||
|
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
|
|
||||||
macro_rules! into {
|
macro_rules! into {
|
||||||
@@ -18,6 +20,14 @@ pub struct Stack<T, const CAP: usize> {
|
|||||||
top: usize,
|
top: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl<T: Collect, const CAP: usize> Collect for Stack<T, CAP> {
|
||||||
|
fn trace(&self, cc: &gc_arena::Collection) {
|
||||||
|
for v in self.iter() {
|
||||||
|
v.trace(cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T, const CAP: usize> Default for Stack<T, CAP> {
|
impl<T, const CAP: usize> Default for Stack<T, CAP> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
|
|||||||
@@ -1,10 +1,44 @@
|
|||||||
use std::fmt::{Display, Formatter, Result as FmtResult};
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use derive_more::{Constructor, IsVariant, Unwrap};
|
use derive_more::{Constructor, IsVariant, Unwrap};
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
|
use hashbrown::HashMap;
|
||||||
|
use gc_arena::Collect;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Constructor, Hash)]
|
pub struct Map<K: Collect, V: Collect>(HashMap<K, V>);
|
||||||
|
|
||||||
|
impl<K: Collect, V: Collect> Deref for Map<K, V> {
|
||||||
|
type Target = HashMap<K, V>;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: Collect, V: Collect> DerefMut for Map<K, V> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl<K: Collect, V: Collect> Collect for Map<K, V> {
|
||||||
|
fn trace(&self, cc: &gc_arena::Collection) {
|
||||||
|
for (k, v) in self.iter() {
|
||||||
|
k.trace(cc);
|
||||||
|
v.trace(cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<K: Collect, V: Collect> From<HashMap<K, V>> for Map<K, V> {
|
||||||
|
fn from(value: HashMap<K, V>) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Constructor, Hash, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub struct Catchable {
|
pub struct Catchable {
|
||||||
msg: String,
|
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 {
|
pub enum Const {
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
|
|
||||||
use crate::env::VmEnv;
|
use crate::env::VmEnv;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
@@ -13,82 +13,87 @@ use super::super::public as p;
|
|||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
#[derive(Debug, Default, Constructor, Clone, PartialEq)]
|
#[derive(Constructor, Clone, PartialEq)]
|
||||||
pub struct AttrSet<'jit: 'vm, 'vm> {
|
pub struct AttrSet<'gc> {
|
||||||
data: HashMap<usize, Value<'jit, 'vm>>,
|
data: HashMap<usize, Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit, 'vm> From<HashMap<usize, Value<'jit, 'vm>>> for AttrSet<'jit, 'vm> {
|
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for AttrSet<'gc> {
|
||||||
fn from(data: HashMap<usize, Value<'jit, 'vm>>) -> Self {
|
fn trace(&self, cc: &gc_arena::Collection) {
|
||||||
|
for (_, v) in self.data.iter() {
|
||||||
|
v.trace(cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
||||||
|
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||||
Self { data }
|
Self { data }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit: 'vm, 'vm> AttrSet<'jit, 'vm> {
|
impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||||
pub fn with_capacity(cap: usize) -> Self {
|
pub fn with_capacity(cap: usize) -> Self {
|
||||||
AttrSet {
|
AttrSet {
|
||||||
data: HashMap::with_capacity(cap),
|
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);
|
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() {
|
if self.data.get(&sym).is_some() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
self.data.insert(sym, val);
|
self.data.insert(sym, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select(&self, sym: usize) -> Option<Value<'jit, 'vm>> {
|
pub fn select(&self, sym: usize) -> Option<Value<'gc>> {
|
||||||
self.data.get(&sym).map(|val| match val {
|
self.data.get(&sym).cloned()
|
||||||
Value::Builtins(x) => Value::AttrSet(x.upgrade().unwrap()),
|
|
||||||
val => val.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_attr(&self, sym: usize) -> bool {
|
pub fn has_attr(&self, sym: usize) -> bool {
|
||||||
self.data.get(&sym).is_some()
|
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)| {
|
self.data.iter().for_each(|(_, v)| {
|
||||||
if let Value::Thunk(ref thunk) = v.clone() {
|
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() {
|
for (k, v) in other.data.iter() {
|
||||||
self.push_attr_force(*k, v.clone())
|
self.push_attr_force(*k, v.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_inner(&self) -> &HashMap<usize, Value<'jit, 'vm>> {
|
pub fn as_inner(&self) -> &HashMap<usize, Value<'gc>> {
|
||||||
&self.data
|
&self.data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<usize, Value<'jit, 'vm>>> {
|
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<usize, Value<'gc>>> {
|
||||||
unsafe { std::mem::transmute(self) }
|
unsafe { std::mem::transmute(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_inner(data: HashMap<usize, Value<'jit, 'vm>>) -> Self {
|
pub fn from_inner(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||||
Self { data }
|
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();
|
let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
|
||||||
for (_, v) in map.iter_mut() {
|
for (_, v) in map.iter_mut() {
|
||||||
v.force_deep(vm)?;
|
v.force_deep(vm, mc)?;
|
||||||
}
|
}
|
||||||
self.data = map.into_iter().collect();
|
self.data = map.into_iter().collect();
|
||||||
Ok(())
|
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()
|
self.data.iter().len() == other.data.iter().len()
|
||||||
&& std::iter::zip(
|
&& std::iter::zip(
|
||||||
self.data.iter().sorted_by_key(|(k, _)| **k),
|
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))
|
.all(|((_, v1), (_, v2))| v1.eq_impl(v2))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'jit, 'vm>>) -> p::Value {
|
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||||
p::Value::AttrSet(p::AttrSet::new(
|
p::Value::AttrSet(p::AttrSet::new(
|
||||||
self.data
|
self.data
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
use std::cell::{Cell, OnceCell};
|
use std::cell::Cell;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
|
use gc_arena::lock::GcRefLock;
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use inkwell::execution_engine::JitFunction;
|
use inkwell::execution_engine::JitFunction;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
|
|
||||||
use crate::bytecode::Func as BFunc;
|
use crate::bytecode::Func as BFunc;
|
||||||
use crate::env::VmEnv;
|
use crate::env::VmEnv;
|
||||||
@@ -13,7 +15,8 @@ use crate::jit::JITFunc;
|
|||||||
use crate::ty::internal::{Thunk, Value};
|
use crate::ty::internal::{Thunk, Value};
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub enum Param {
|
pub enum Param {
|
||||||
Ident(usize),
|
Ident(usize),
|
||||||
Formals {
|
Formals {
|
||||||
@@ -43,21 +46,29 @@ impl From<ir::Param> for Param {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Constructor)]
|
#[derive(Clone, Constructor)]
|
||||||
pub struct Func<'jit: 'vm, 'vm> {
|
pub struct Func<'gc> {
|
||||||
pub func: &'vm BFunc,
|
pub func: &'gc BFunc,
|
||||||
pub env: &'vm VmEnv<'jit, 'vm>,
|
pub env: Gc<'gc, VmEnv<'gc>>,
|
||||||
pub compiled: OnceCell<JitFunction<'jit, JITFunc<'jit, 'vm>>>,
|
pub compiled: GcRefLock<'gc, Option<JITFunc<'gc>>>,
|
||||||
pub count: Cell<usize>,
|
pub count: Cell<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
|
unsafe impl<'gc> Collect for Func<'gc> {
|
||||||
pub fn call(&self, vm: &'vm VM<'jit>, arg: Value<'jit, 'vm>) -> Result<Value<'jit, 'vm>> {
|
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<Value<'gc>> {
|
||||||
use Param::*;
|
use Param::*;
|
||||||
|
|
||||||
let mut env = self.env;
|
let mut env = self.env;
|
||||||
env = match self.func.param.clone() {
|
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 {
|
||||||
formals,
|
formals,
|
||||||
ellipsis,
|
ellipsis,
|
||||||
@@ -65,7 +76,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
|
|||||||
} => {
|
} => {
|
||||||
let arg = arg.unwrap_attr_set();
|
let arg = arg.unwrap_attr_set();
|
||||||
let mut new =
|
let mut new =
|
||||||
HashMap::with_capacity_in(formals.len() + alias.iter().len(), &vm.bump);
|
HashMap::with_capacity(formals.len() + alias.iter().len());
|
||||||
if !ellipsis
|
if !ellipsis
|
||||||
&& arg
|
&& arg
|
||||||
.as_inner()
|
.as_inner()
|
||||||
@@ -80,7 +91,7 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
|
|||||||
let arg = arg
|
let arg = arg
|
||||||
.select(formal)
|
.select(formal)
|
||||||
.or_else(|| {
|
.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();
|
.unwrap();
|
||||||
new.insert(formal, arg);
|
new.insert(formal, arg);
|
||||||
@@ -88,23 +99,22 @@ impl<'vm, 'jit: 'vm> Func<'jit, 'vm> {
|
|||||||
if let Some(alias) = alias {
|
if let Some(alias) = alias {
|
||||||
new.insert(alias, Value::AttrSet(arg));
|
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();
|
let count = self.count.get();
|
||||||
self.count.replace(count + 1);
|
self.count.replace(count + 1);
|
||||||
if count >= 1 {
|
if count >= 1 {
|
||||||
let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func));
|
let compiled = self.compiled.borrow_mut(mc).get_or_insert_with(|| vm.compile_func(self.func));
|
||||||
let env = env as *const _;
|
let ret = unsafe { compiled.call(vm as *const VM, env.as_ref() as *const VmEnv) };
|
||||||
let ret = unsafe { compiled.call(vm as *const VM, env) };
|
|
||||||
return Ok(ret.into());
|
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 {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use hashbrown::HashSet;
|
|||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use rpds::Vector;
|
use rpds::Vector;
|
||||||
|
use gc_arena::{Collect, Mutation};
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::ty::public as p;
|
use crate::ty::public as p;
|
||||||
@@ -9,38 +10,46 @@ use crate::vm::VM;
|
|||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
#[derive(Constructor, Clone, PartialEq)]
|
||||||
pub struct List<'jit: 'vm, 'vm> {
|
pub struct List<'gc> {
|
||||||
data: Vector<Value<'jit, 'vm>>,
|
data: Vector<Value<'gc>>
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
pub fn empty() -> Self {
|
||||||
List {
|
List {
|
||||||
data: Vector::new(),
|
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);
|
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() {
|
for elem in other.data.iter() {
|
||||||
self.data.push_back_mut(elem.clone());
|
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();
|
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
||||||
for v in vec.iter_mut() {
|
for v in vec.iter_mut() {
|
||||||
v.force_deep(vm)?;
|
v.force_deep(vm, mc)?;
|
||||||
}
|
}
|
||||||
self.data = vec.into_iter().collect();
|
self.data = vec.into_iter().collect();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'jit, 'vm>>) -> p::Value {
|
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||||
p::Value::List(p::List::new(
|
p::Value::List(p::List::new(
|
||||||
self.data
|
self.data
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
use hashbrown::HashSet;
|
use std::cell::Cell;
|
||||||
use std::cell::OnceCell;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::rc::{Rc, Weak};
|
use std::mem::MaybeUninit;
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
use derive_more::{IsVariant, Unwrap};
|
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::common::*;
|
||||||
use super::public as p;
|
use super::public as p;
|
||||||
@@ -25,38 +28,125 @@ pub use func::*;
|
|||||||
pub use list::List;
|
pub use list::List;
|
||||||
pub use primop::*;
|
pub use primop::*;
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
#[derive(Collect)]
|
||||||
pub enum Value<'jit: 'vm, 'vm> {
|
#[collect(unsafe_drop)]
|
||||||
Const(Const),
|
pub struct CoW<'gc, T: Clone + Collect> {
|
||||||
Thunk(Rc<Thunk<'jit, 'vm>>),
|
inner: Gc<'gc, CoWInner<T>>,
|
||||||
AttrSet(Rc<AttrSet<'jit, 'vm>>),
|
|
||||||
List(Rc<List<'jit, 'vm>>),
|
|
||||||
Catchable(Catchable),
|
|
||||||
PrimOp(Rc<PrimOp<'jit, 'vm>>),
|
|
||||||
PartialPrimOp(Rc<PartialPrimOp<'jit, 'vm>>),
|
|
||||||
Func(Rc<Func<'jit, 'vm>>),
|
|
||||||
Builtins(Weak<AttrSet<'jit, 'vm>>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hash for Value<'_, '_> {
|
struct CoWInner<T: Clone + Collect> {
|
||||||
|
ref_count: Cell<usize>,
|
||||||
|
data: MaybeUninit<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl<T: Clone + Collect> Collect for CoWInner<T> {
|
||||||
|
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<T> 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<T: Clone + Collect> CoWInner<T> {
|
||||||
|
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<H: std::hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
std::mem::discriminant(self).hash(state);
|
std::mem::discriminant(self).hash(state);
|
||||||
match self {
|
match self {
|
||||||
Const(x) => x.hash(state),
|
Const(x) => x.hash(state),
|
||||||
Thunk(x) => (x.as_ref() as *const self::Thunk).hash(state),
|
Thunk(x) => (x as *const self::Thunk).hash(state),
|
||||||
AttrSet(x) => (x.as_ref() as *const self::AttrSet).hash(state),
|
AttrSet(x) => x.as_ptr().hash(state),
|
||||||
List(x) => (x.as_ref() as *const self::List).hash(state),
|
List(x) => x.as_ptr().hash(state),
|
||||||
Catchable(x) => x.hash(state),
|
Catchable(x) => x.hash(state),
|
||||||
PrimOp(x) => (x.as_ref() as *const self::PrimOp).hash(state),
|
PrimOp(x) => (x.as_ref() as *const self::PrimOp).hash(state),
|
||||||
PartialPrimOp(x) => (x.as_ref() as *const self::PartialPrimOp).hash(state),
|
PartialPrimOp(x) => (x.as_ref() as *const self::PartialPrimOp).hash(state),
|
||||||
Func(x) => (x.as_ref() as *const self::Func).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 {
|
fn eq_impl(&self, other: &Self) -> bool {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match (self, other) {
|
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 {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Const(a), Const(b)) => a.eq(b),
|
(Const(a), Const(b)) => a.eq(b),
|
||||||
(AttrSet(a), AttrSet(b)) => {
|
(AttrSet(a), AttrSet(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||||
(a.as_ref() as *const self::AttrSet).eq(&(b.as_ref() as *const _))
|
(List(a), List(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||||
}
|
|
||||||
(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),
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for Value<'_, '_> {}
|
impl Eq for Value<'_> {}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
#[derive(IsVariant, Unwrap, Clone)]
|
||||||
pub enum ValueAsRef<'v, 'vm: 'v, 'jit: 'vm> {
|
pub enum ValueAsRef<'v, 'gc> {
|
||||||
Const(&'v Const),
|
Const(&'v Const),
|
||||||
Thunk(&'v Thunk<'jit, 'vm>),
|
Thunk(&'v Thunk<'gc>),
|
||||||
AttrSet(&'v AttrSet<'jit, 'vm>),
|
AttrSet(&'v AttrSet<'gc>),
|
||||||
List(&'v List<'jit, 'vm>),
|
List(&'v List<'gc>),
|
||||||
Catchable(&'v Catchable),
|
Catchable(&'v Catchable),
|
||||||
PrimOp(&'v PrimOp<'jit, 'vm>),
|
PrimOp(&'v PrimOp<'gc>),
|
||||||
PartialPrimOp(&'v PartialPrimOp<'jit, 'vm>),
|
PartialPrimOp(&'v PartialPrimOp<'gc>),
|
||||||
Func(&'v Func<'jit, 'vm>),
|
Func(&'v Func<'gc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, PartialEq)]
|
#[derive(IsVariant, Unwrap)]
|
||||||
pub enum ValueAsMut<'v, 'vm: 'v, 'jit: 'vm> {
|
pub enum ValueAsMut<'v, 'gc> {
|
||||||
Const(&'v mut Const),
|
Const(&'v mut Const),
|
||||||
Thunk(&'v Thunk<'jit, 'vm>),
|
Thunk(&'v Thunk<'gc>),
|
||||||
AttrSet(&'v mut AttrSet<'jit, 'vm>),
|
AttrSet(&'v mut AttrSet<'gc>),
|
||||||
List(&'v mut List<'jit, 'vm>),
|
List(&'v mut List<'gc>),
|
||||||
Catchable(&'v mut Catchable),
|
Catchable(&'v mut Catchable),
|
||||||
PrimOp(&'v mut PrimOp<'jit, 'vm>),
|
PrimOp(&'v PrimOp<'gc>),
|
||||||
PartialPrimOp(&'v mut PartialPrimOp<'jit, 'vm>),
|
PartialPrimOp(&'v mut PartialPrimOp<'gc>),
|
||||||
Func(&'v Func<'jit, 'vm>),
|
Func(&'v Func<'gc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'v, 'vm: 'v, 'jit: 'vm> Value<'jit, 'vm> {
|
impl<'gc, 'v> Value<'gc> {
|
||||||
pub fn as_ref(&'v self) -> ValueAsRef<'v, 'vm, 'jit> {
|
pub fn as_ref(&'v self) -> ValueAsRef<'v, 'gc> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
use ValueAsRef as R;
|
use ValueAsRef as R;
|
||||||
match self {
|
match self {
|
||||||
@@ -122,29 +209,27 @@ impl<'v, 'vm: 'v, 'jit: 'vm> Value<'jit, 'vm> {
|
|||||||
PrimOp(x) => R::PrimOp(x),
|
PrimOp(x) => R::PrimOp(x),
|
||||||
PartialPrimOp(x) => R::PartialPrimOp(x),
|
PartialPrimOp(x) => R::PartialPrimOp(x),
|
||||||
Func(x) => R::Func(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 Value::*;
|
||||||
use ValueAsMut as M;
|
use ValueAsMut as M;
|
||||||
match self {
|
match self {
|
||||||
Const(x) => M::Const(x),
|
Const(x) => M::Const(x),
|
||||||
Thunk(x) => M::Thunk(x),
|
Thunk(x) => M::Thunk(x),
|
||||||
AttrSet(x) => M::AttrSet(Rc::make_mut(x)),
|
AttrSet(x) => M::AttrSet(x.make_mut(mc)),
|
||||||
List(x) => M::List(Rc::make_mut(x)),
|
List(x) => M::List(x.make_mut(mc)),
|
||||||
Catchable(x) => M::Catchable(x),
|
Catchable(x) => M::Catchable(x),
|
||||||
PrimOp(x) => M::PrimOp(Rc::make_mut(x)),
|
PrimOp(x) => M::PrimOp(x),
|
||||||
PartialPrimOp(x) => M::PartialPrimOp(Rc::make_mut(x)),
|
PartialPrimOp(x) => M::PartialPrimOp(x.make_mut(mc)),
|
||||||
Func(x) => M::Func(x),
|
Func(x) => M::Func(x),
|
||||||
Builtins(_) => unreachable!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use Value::Const as VmConst;
|
use Value::Const as VmConst;
|
||||||
impl<'jit, 'vm> Value<'jit, 'vm> {
|
impl<'gc> Value<'gc> {
|
||||||
pub fn ok(self) -> Result<Self> {
|
pub fn ok(self) -> Result<Self> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
@@ -164,7 +249,6 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
|||||||
PrimOp(_) => "lambda",
|
PrimOp(_) => "lambda",
|
||||||
PartialPrimOp(_) => "lambda",
|
PartialPrimOp(_) => "lambda",
|
||||||
Func(_) => "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::*;
|
use Value::*;
|
||||||
if matches!(arg, Value::Catchable(_)) {
|
if matches!(arg, Value::Catchable(_)) {
|
||||||
*self = arg;
|
*self = arg;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
*self = match self {
|
*self = match self {
|
||||||
PrimOp(func) => func.call(vm, arg),
|
PrimOp(func) => func.call(arg, vm, mc),
|
||||||
PartialPrimOp(func) => func.call(vm, arg),
|
PartialPrimOp(func) => func.call(arg, vm, mc),
|
||||||
Value::Func(func) => func.call(vm, arg),
|
Value::Func(func) => func.call(arg, vm, mc),
|
||||||
Catchable(_) => return Ok(()),
|
Catchable(_) => return Ok(()),
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}?;
|
}?;
|
||||||
@@ -316,9 +400,9 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
|||||||
self
|
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 {
|
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(_) = self {
|
||||||
} else if let Value::Catchable(_) = elem {
|
} else if let Value::Catchable(_) = elem {
|
||||||
*self = elem;
|
*self = elem;
|
||||||
@@ -328,23 +412,23 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat(&mut self, other: Self) {
|
pub fn concat(&mut self, other: Self, mc: &Mutation<'gc>) {
|
||||||
if let x @ Value::Catchable(_) = other {
|
if let x @ Value::Catchable(_) = other {
|
||||||
*self = x;
|
*self = x;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::List(a), Value::List(b)) => {
|
(Value::List(a), Value::List(b)) => {
|
||||||
Rc::make_mut(a).concat(b.as_ref());
|
a.make_mut(mc).concat(b.as_ref());
|
||||||
}
|
}
|
||||||
(Value::Catchable(_), _) => (),
|
(Value::Catchable(_), _) => (),
|
||||||
_ => todo!(),
|
_ => 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 {
|
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(_) = self {
|
||||||
} else if let Value::Catchable(_) = val {
|
} else if let Value::Catchable(_) = val {
|
||||||
*self = val
|
*self = val
|
||||||
@@ -354,21 +438,21 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, other: Self) {
|
pub fn update(&mut self, other: Self, mc: &Mutation<'gc>) {
|
||||||
if let x @ Value::Catchable(_) = other {
|
if let x @ Value::Catchable(_) = other {
|
||||||
*self = x;
|
*self = x;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Value::AttrSet(a), Value::AttrSet(b)) => {
|
(Value::AttrSet(a), Value::AttrSet(b)) => {
|
||||||
Rc::make_mut(a).update(b.as_ref());
|
a.make_mut(mc).update(b.as_ref());
|
||||||
}
|
}
|
||||||
(Value::Catchable(_), _) => (),
|
(Value::Catchable(_), _) => (),
|
||||||
_ => todo!(),
|
_ => 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 {
|
let val = match self {
|
||||||
Value::AttrSet(attrs) => attrs
|
Value::AttrSet(attrs) => attrs
|
||||||
.select(sym)
|
.select(sym)
|
||||||
@@ -418,106 +502,127 @@ impl<'jit, 'vm> Value<'jit, 'vm> {
|
|||||||
self
|
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 {
|
if let Value::Thunk(thunk) = self {
|
||||||
let value = thunk.force(vm)?.clone();
|
let value = thunk.force(vm, mc)?.clone();
|
||||||
*self = value
|
*self = value
|
||||||
}
|
}
|
||||||
Ok(self)
|
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 {
|
match self {
|
||||||
Value::Thunk(thunk) => {
|
Value::Thunk(thunk) => {
|
||||||
let mut value = thunk.force(vm)?.clone();
|
let mut value = thunk.force(vm, mc)?.clone();
|
||||||
let _ = value.force_deep(vm)?;
|
let _ = value.force_deep(vm, mc)?;
|
||||||
*self = value;
|
*self = value;
|
||||||
}
|
}
|
||||||
Value::List(list) => Rc::make_mut(list).force_deep(vm)?,
|
Value::List(list) => list.make_mut(mc).force_deep(vm, mc)?,
|
||||||
Value::AttrSet(attrs) => Rc::make_mut(attrs).force_deep(vm)?,
|
Value::AttrSet(attrs) => attrs.make_mut(mc).force_deep(vm, mc)?,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_public(&self, vm: &'vm VM, seen: &mut HashSet<Value<'jit, 'vm>>) -> p::Value {
|
pub fn to_public(
|
||||||
|
&self,
|
||||||
|
vm: &VM<'gc>,
|
||||||
|
seen: &mut HashSet<Value<'gc>>,
|
||||||
|
) -> p::Value {
|
||||||
use self::Value::*;
|
use self::Value::*;
|
||||||
use p::Value;
|
use p::Value;
|
||||||
if seen.contains(self) {
|
if seen.contains(self) {
|
||||||
return Value::Repeated;
|
return Value::Repeated;
|
||||||
}
|
}
|
||||||
seen.insert(self.clone());
|
|
||||||
match self {
|
match self {
|
||||||
AttrSet(attrs) => attrs.to_public(vm, seen),
|
AttrSet(attrs) => {
|
||||||
List(list) => list.to_public(vm, seen),
|
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()),
|
Catchable(catchable) => Value::Catchable(catchable.clone()),
|
||||||
Const(cnst) => Value::Const(cnst.clone()),
|
Const(cnst) => Value::Const(cnst.clone()),
|
||||||
Thunk(_) => Value::Thunk,
|
Thunk(_) => Value::Thunk,
|
||||||
PrimOp(primop) => Value::PrimOp(primop.name),
|
PrimOp(primop) => Value::PrimOp(primop.name),
|
||||||
PartialPrimOp(primop) => Value::PartialPrimOp(primop.name),
|
PartialPrimOp(primop) => Value::PartialPrimOp(primop.name),
|
||||||
Func(_) => Value::Func,
|
Func(_) => Value::Func,
|
||||||
Builtins(x) => x.upgrade().unwrap().to_public(vm, seen),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[repr(transparent)]
|
||||||
pub struct Thunk<'jit, 'vm> {
|
#[derive(Clone, Collect)]
|
||||||
pub thunk: RefCell<_Thunk<'jit, 'vm>>,
|
#[collect(no_drop)]
|
||||||
|
pub struct Thunk<'gc> {
|
||||||
|
pub thunk: GcRefLock<'gc, _Thunk<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, IsVariant, Unwrap, Clone)]
|
#[derive(IsVariant, Unwrap)]
|
||||||
pub enum _Thunk<'jit, 'vm> {
|
pub enum _Thunk<'gc> {
|
||||||
Code(&'vm OpCodes, OnceCell<&'vm VmEnv<'jit, 'vm>>),
|
Code(
|
||||||
SuspendedFrom(*const Thunk<'jit, 'vm>),
|
&'gc OpCodes,
|
||||||
Value(Value<'jit, 'vm>),
|
GcRefLock<'gc, Option<Gc<'gc, VmEnv<'gc>>>>,
|
||||||
|
),
|
||||||
|
Suspended,
|
||||||
|
Value(Value<'gc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit, 'vm> Thunk<'jit, 'vm> {
|
unsafe impl<'gc> Collect for _Thunk<'gc> {
|
||||||
pub fn new(opcodes: &'vm OpCodes) -> Self {
|
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 {
|
||||||
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() {
|
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<Value<'gc>> {
|
||||||
use _Thunk::*;
|
use _Thunk::*;
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
Value(_) => {
|
Value(_) => {
|
||||||
return Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
return Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
||||||
Value(value) => value,
|
Value(value) => value.clone(),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
SuspendedFrom(from) => {
|
Suspended => {
|
||||||
return Err(Error::EvalError(format!(
|
return Err(Error::EvalError("infinite recursion encountered".into()));
|
||||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
|
||||||
self as *const Thunk
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
Code(..) => (),
|
Code(..) => (),
|
||||||
}
|
}
|
||||||
let (opcodes, env) = std::mem::replace(
|
let (opcodes, env) =
|
||||||
&mut *self.thunk.borrow_mut(),
|
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended).unwrap_code();
|
||||||
_Thunk::SuspendedFrom(self as *const Thunk),
|
let env = env.as_ref().borrow().unwrap();
|
||||||
)
|
|
||||||
.unwrap_code();
|
|
||||||
let env = env.get().unwrap();
|
|
||||||
let value = vm.eval(opcodes.iter().copied(), env)?;
|
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 _) } {
|
Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
||||||
Value(value) => value,
|
Value(value) => value.clone(),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&'vm self) -> Option<Value<'jit, 'vm>> {
|
pub fn value(&self) -> Option<Value<'gc>> {
|
||||||
match &*self.thunk.borrow() {
|
match &*self.thunk.borrow() {
|
||||||
_Thunk::Value(value) => Some(value.clone()),
|
_Thunk::Value(value) => Some(value.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
@@ -525,7 +630,7 @@ impl<'jit, 'vm> Thunk<'jit, 'vm> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Thunk<'_, '_> {
|
impl PartialEq for Thunk<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,74 +1,84 @@
|
|||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
|
use gc_arena::Mutation;
|
||||||
|
use gc_arena::Collect;
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
|
use super::CoW;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Constructor)]
|
#[derive(Debug, Clone, Constructor, Collect)]
|
||||||
pub struct PrimOp<'jit: 'vm, 'vm> {
|
#[collect(require_static)]
|
||||||
|
pub struct PrimOp<'gc> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
func: fn(&'vm VM<'jit>, Vec<Value<'jit, 'vm>>) -> Result<Value<'jit, 'vm>>,
|
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for PrimOp<'_, '_> {
|
impl PartialEq for PrimOp<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit, 'vm> PrimOp<'jit, 'vm> {
|
impl<'gc> PrimOp<'gc> {
|
||||||
pub fn call(&self, vm: &'vm VM<'jit>, arg: Value<'jit, 'vm>) -> Result<Value<'jit, 'vm>> {
|
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||||
let mut args = Vec::with_capacity(self.arity);
|
let mut args = Vec::with_capacity(self.arity);
|
||||||
args.push(arg);
|
args.push(arg);
|
||||||
if self.arity > 1 {
|
if self.arity > 1 {
|
||||||
Value::PartialPrimOp(
|
Value::PartialPrimOp(
|
||||||
PartialPrimOp {
|
CoW::new(PartialPrimOp {
|
||||||
name: self.name,
|
name: self.name,
|
||||||
arity: self.arity - 1,
|
arity: self.arity - 1,
|
||||||
args,
|
args,
|
||||||
func: self.func,
|
func: self.func,
|
||||||
}
|
}, mc)
|
||||||
.into(),
|
|
||||||
)
|
)
|
||||||
.ok()
|
.ok()
|
||||||
} else {
|
} else {
|
||||||
(self.func)(vm, args)
|
(self.func)(args, vm, mc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PartialPrimOp<'jit: 'vm, 'vm> {
|
pub struct PartialPrimOp<'gc> {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
args: Vec<Value<'jit, 'vm>>,
|
args: Vec<Value<'gc>>,
|
||||||
func: fn(&'vm VM<'jit>, Vec<Value<'jit, 'vm>>) -> Result<Value<'jit, 'vm>>,
|
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit: 'vm, 'vm> PartialPrimOp<'jit, 'vm> {
|
impl<'gc> PartialPrimOp<'gc> {
|
||||||
pub fn call(
|
pub fn call(
|
||||||
self: &mut Rc<Self>,
|
self: &mut CoW<'gc, Self>,
|
||||||
vm: &'vm VM<'jit>,
|
arg: Value<'gc>,
|
||||||
arg: Value<'jit, 'vm>,
|
vm: &VM,
|
||||||
) -> Result<Value<'jit, 'vm>> {
|
mc: &Mutation<'gc>
|
||||||
let self_mut = Rc::make_mut(self);
|
) -> Result<Value<'gc>> {
|
||||||
|
let func = self.func;
|
||||||
|
let self_mut = self.make_mut(mc);
|
||||||
self_mut.args.push(arg);
|
self_mut.args.push(arg);
|
||||||
self_mut.arity -= 1;
|
self_mut.arity -= 1;
|
||||||
if self_mut.arity > 0 {
|
if self_mut.arity > 0 {
|
||||||
Value::PartialPrimOp(self.clone()).ok()
|
Value::PartialPrimOp(self.clone()).ok()
|
||||||
} else {
|
} else {
|
||||||
let args = std::mem::take(&mut self_mut.args);
|
func(std::mem::take(&mut self_mut.args), vm, mc)
|
||||||
(self.func)(vm, args)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
528
src/vm/mod.rs
528
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 hashbrown::{HashMap, HashSet};
|
||||||
use inkwell::execution_engine::JitFunction;
|
use inkwell::context::Context;
|
||||||
use std::cell::{Cell, OnceCell, RefCell};
|
|
||||||
|
|
||||||
use crate::builtins::env;
|
use crate::builtins::env;
|
||||||
use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp};
|
use crate::bytecode::{BinOp, Func as F, OpCode, OpCodes, Program, UnOp};
|
||||||
@@ -20,37 +22,301 @@ use ecow::EcoString;
|
|||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
|
const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
|
||||||
|
type GcArena = Arena<Rootable!['gc => GcRoot<'gc>]>;
|
||||||
|
|
||||||
pub fn run(prog: Program, jit: JITContext<'_>) -> Result<p::Value> {
|
#[derive(Collect)]
|
||||||
let vm = VM {
|
#[collect(require_static)]
|
||||||
thunks: prog.thunks,
|
struct ContextWrapper(Context);
|
||||||
funcs: prog.funcs,
|
|
||||||
symbols: RefCell::new(prog.symbols),
|
|
||||||
symmap: RefCell::new(prog.symmap),
|
pub fn run(mut prog: Program) -> Result<p::Value> {
|
||||||
consts: prog.consts,
|
fn new<'gc>(prog: &mut Program, mc: &'gc Mutation<'gc>) -> GcRoot<'gc> {
|
||||||
bump: Bump::new(),
|
let jit = Gc::new(mc, ContextWrapper(Context::create()));
|
||||||
jit,
|
let thunks = std::mem::take(&mut prog.thunks);
|
||||||
};
|
let funcs = std::mem::take(&mut prog.funcs);
|
||||||
let env = env(&vm);
|
let symbols = std::mem::take(&mut prog.symbols);
|
||||||
let mut seen = HashSet::new();
|
let symmap = std::mem::take(&mut prog.symmap);
|
||||||
let value = vm
|
let consts = std::mem::take(&mut prog.consts);
|
||||||
.eval(prog.top_level.into_iter(), vm.bump.alloc(env))?
|
let vm = VM {
|
||||||
.to_public(&vm, &mut seen);
|
thunks,
|
||||||
Ok(value)
|
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<Rootable![GcRoot<'_>]> = 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)]
|
fn eval<'gc, T, F: for<'a> FnOnce(Value<'a>) -> Result<T>>(
|
||||||
pub struct VM<'jit> {
|
opcodes: impl Iterator<Item = OpCode>,
|
||||||
|
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
|
||||||
|
f: F
|
||||||
|
) -> Result<T> {
|
||||||
|
let mut iter = opcodes.into_iter();
|
||||||
|
let dynroot: gc_arena::DynamicRoot<Rootable!['a => 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<Value<'gc>, CAP>,
|
||||||
|
env: &mut Gc<'gc, VmEnv<'gc>>,
|
||||||
|
mc: &'gc Mutation<'gc>,
|
||||||
|
) -> Result<usize> {
|
||||||
|
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<JITFunc<'gc>>> = 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<Value<'gc>, 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]>,
|
thunks: Box<[OpCodes]>,
|
||||||
funcs: Box<[F]>,
|
funcs: Box<[F]>,
|
||||||
symbols: RefCell<Vec<EcoString>>,
|
symbols: RefCell<Vec<EcoString>>,
|
||||||
symmap: RefCell<HashMap<EcoString, usize>>,
|
symmap: RefCell<HashMap<EcoString, usize>>,
|
||||||
consts: Box<[Const]>,
|
consts: Box<[Const]>,
|
||||||
pub bump: Bump,
|
jit: JITContext<'gc>,
|
||||||
jit: JITContext<'jit>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'vm, 'jit: 'vm> VM<'jit> {
|
impl<'gc> VM<'gc> {
|
||||||
pub fn get_thunk(&self, idx: usize) -> &OpCodes {
|
pub fn get_thunk(&self, idx: usize) -> &OpCodes {
|
||||||
&self.thunks[idx]
|
&self.thunks[idx]
|
||||||
}
|
}
|
||||||
@@ -80,219 +346,7 @@ impl<'vm, 'jit: 'vm> VM<'jit> {
|
|||||||
self.consts[idx].clone()
|
self.consts[idx].clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eval(
|
pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> {
|
||||||
&'vm self,
|
|
||||||
opcodes: impl Iterator<Item = OpCode>,
|
|
||||||
mut env: &'vm VmEnv<'jit, 'vm>,
|
|
||||||
) -> Result<Value<'jit, 'vm>> {
|
|
||||||
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<Value<'jit, 'vm>, CAP>,
|
|
||||||
env: &mut &'vm VmEnv<'jit, 'vm>,
|
|
||||||
) -> Result<usize> {
|
|
||||||
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>> {
|
|
||||||
self.jit.compile_function(func, self).unwrap()
|
self.jit.compile_function(func, self).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user