feat: gc (does compile, but WIP)
This commit is contained in:
24
Cargo.lock
generated
24
Cargo.lock
generated
@@ -45,15 +45,6 @@ version = "2.9.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bumpalo"
|
|
||||||
version = "3.17.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
|
|
||||||
dependencies = [
|
|
||||||
"allocator-api2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.21"
|
version = "1.2.21"
|
||||||
@@ -181,18 +172,16 @@ checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "gc-arena"
|
name = "gc-arena"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://github.com/kyren/gc-arena?rev=d651e3b4363d525a2d502c2305bc73e291835c84#d651e3b4363d525a2d502c2305bc73e291835c84"
|
||||||
checksum = "3cd70cf88a32937834aae9614ff2569b5d9467fa0c42c5d7762fd94a8de88266"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"gc-arena-derive",
|
"gc-arena-derive",
|
||||||
"sptr",
|
"hashbrown 0.15.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gc-arena-derive"
|
name = "gc-arena-derive"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://github.com/kyren/gc-arena?rev=d651e3b4363d525a2d502c2305bc73e291835c84#d651e3b4363d525a2d502c2305bc73e291835c84"
|
||||||
checksum = "c612a69f5557a11046b77a7408d2836fe77077f842171cd211c5ef504bd3cddd"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -338,7 +327,6 @@ dependencies = [
|
|||||||
name = "nixjit"
|
name = "nixjit"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bumpalo",
|
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"ecow",
|
"ecow",
|
||||||
"gc-arena",
|
"gc-arena",
|
||||||
@@ -511,12 +499,6 @@ 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"
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ ecow = "0.2"
|
|||||||
regex = "1.11"
|
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"] }
|
gc-arena = { git = "https://github.com/kyren/gc-arena", rev = "d651e3b4363d525a2d502c2305bc73e291835c84", features= ["hashbrown"] }
|
||||||
gc-arena = "0.5.3"
|
|
||||||
|
|
||||||
rustyline = { version = "15.0", optional = true }
|
rustyline = { version = "15.0", optional = true }
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use inkwell::context::Context;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use nixjit::compile::compile;
|
use nixjit::compile::compile;
|
||||||
use nixjit::error::Error;
|
use nixjit::error::Error;
|
||||||
use nixjit::error::Result;
|
use nixjit::error::Result;
|
||||||
use nixjit::ir::downgrade;
|
use nixjit::ir::downgrade;
|
||||||
use nixjit::jit::JITContext;
|
|
||||||
use nixjit::vm::run;
|
use nixjit::vm::run;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
@@ -27,9 +25,7 @@ fn main() -> Result<()> {
|
|||||||
let expr = root.tree().expr().unwrap();
|
let expr = root.tree().expr().unwrap();
|
||||||
let downgraded = downgrade(expr)?;
|
let downgraded = downgrade(expr)?;
|
||||||
let prog = compile(downgraded);
|
let prog = compile(downgraded);
|
||||||
let ctx = Context::create();
|
println!("{}", run(prog)?);
|
||||||
let jit = JITContext::new(&ctx);
|
|
||||||
println!("{}", run(prog, jit)?);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,25 +2,24 @@ 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::internal::{AttrSet, CoW, PrimOp, Value};
|
use crate::ty::internal::{AttrSet, CoW, PrimOp, Value};
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
pub fn env<'gc>(vm: &VM, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
||||||
let primops: [PrimOp<'gc>; 7] = [
|
let primops = [
|
||||||
PrimOp::new("add", 2, |args, _, _| {
|
PrimOp::new("add", 2, |args, _, mc| {
|
||||||
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
first.add(second);
|
first.add(second, mc);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("sub", 2, |args, _, _| {
|
PrimOp::new("sub", 2, |args, _, mc| {
|
||||||
let Ok([mut first, mut second]): Result<[Value; 2], _> = args.try_into() else {
|
let Ok([mut first, mut second]): Result<[Value; 2], _> = args.try_into() else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
second.neg();
|
second.neg();
|
||||||
first.add(second);
|
first.add(second, mc);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("mul", 2, |args, _, _| {
|
PrimOp::new("mul", 2, |args, _, _| {
|
||||||
@@ -44,7 +43,7 @@ pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
|||||||
first.lt(second);
|
first.lt(second);
|
||||||
first.ok()
|
first.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("seq", 2, |args, vm, mc| {
|
/* PrimOp::new("seq", 2, |args, vm, mc| {
|
||||||
let Ok([mut first, second]): Result<[_; 2], _> = args.try_into() else {
|
let Ok([mut first, second]): Result<[_; 2], _> = args.try_into() else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
@@ -52,17 +51,17 @@ pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
|||||||
second.ok()
|
second.ok()
|
||||||
}),
|
}),
|
||||||
PrimOp::new("deepSeq", 2, |args, vm, mc| {
|
PrimOp::new("deepSeq", 2, |args, vm, mc| {
|
||||||
let [mut first, second] = *args.into_boxed_slice() else {
|
let Ok([mut first, second]): Result<[_; 2], _> = args.try_into() else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
first.force_deep(vm, mc).unwrap();
|
first.force_deep(vm, mc).unwrap();
|
||||||
second.ok()
|
second.ok()
|
||||||
}),
|
}), */
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut env_map = HashMap::new();
|
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::Bool(true));
|
||||||
env_map.insert(vm.new_sym("false"), Value::Const(Const::Bool(false)));
|
env_map.insert(vm.new_sym("false"), Value::Bool(false));
|
||||||
|
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
for primop in primops {
|
for primop in primops {
|
||||||
@@ -74,10 +73,13 @@ pub fn env<'gc>(vm: &VM<'gc>, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
|
|||||||
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 = CoW::new_cyclic(|this| {
|
let attrs = CoW::new_cyclic(
|
||||||
|
|this| {
|
||||||
map.insert(sym, Value::AttrSet(this));
|
map.insert(sym, Value::AttrSet(this));
|
||||||
AttrSet::from_inner(map)
|
AttrSet::from_inner(map)
|
||||||
}, mc);
|
},
|
||||||
|
mc,
|
||||||
|
);
|
||||||
let builtins = Value::AttrSet(attrs);
|
let builtins = Value::AttrSet(attrs);
|
||||||
|
|
||||||
env_map.insert(sym, builtins);
|
env_map.insert(sym, builtins);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use hashbrown::HashMap;
|
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
use gc_arena::Collect;
|
use gc_arena::Collect;
|
||||||
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
use crate::ty::common::Const;
|
use crate::ty::common::Const;
|
||||||
use crate::ty::internal::Param;
|
use crate::ty::internal::Param;
|
||||||
@@ -18,10 +18,14 @@ pub enum OpCode {
|
|||||||
LookUp { sym: usize },
|
LookUp { sym: usize },
|
||||||
/// load a thunk lazily onto stack
|
/// load a thunk lazily onto stack
|
||||||
LoadThunk { idx: usize },
|
LoadThunk { idx: usize },
|
||||||
|
/// load a thunk value onto stack
|
||||||
|
LoadValue { idx: usize },
|
||||||
/// let TOS capture current environment
|
/// let TOS capture current environment
|
||||||
CaptureEnv,
|
CaptureEnv,
|
||||||
/// force TOS to value
|
/// force TOS to value
|
||||||
ForceValue,
|
ForceValue,
|
||||||
|
/// TODO:
|
||||||
|
InsertValue,
|
||||||
|
|
||||||
/// [ .. func arg ] consume 2 elements, call `func` with arg
|
/// [ .. func arg ] consume 2 elements, call `func` with arg
|
||||||
Call,
|
Call,
|
||||||
@@ -73,18 +77,19 @@ pub enum OpCode {
|
|||||||
SelectDynamicOrDefault,
|
SelectDynamicOrDefault,
|
||||||
/// enter the let environment of the attribute set at TOS
|
/// enter the let environment of the attribute set at TOS
|
||||||
EnterLetEnv,
|
EnterLetEnv,
|
||||||
/// exit current let envrironment
|
|
||||||
LeaveLetEnv,
|
|
||||||
/// enter the with environment of the attribute set at TOS
|
/// enter the with environment of the attribute set at TOS
|
||||||
EnterWithEnv,
|
EnterWithEnv,
|
||||||
/// exit current with envrironment
|
/// exit current envrironment
|
||||||
LeaveWithEnv,
|
LeaveEnv,
|
||||||
|
/// TODO:
|
||||||
|
PopEnv,
|
||||||
|
|
||||||
/// illegal operation, used as termporary placeholder
|
/// illegal operation, used as termporary placeholder
|
||||||
Illegal,
|
Illegal,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub enum BinOp {
|
pub enum BinOp {
|
||||||
Add,
|
Add,
|
||||||
Sub,
|
Sub,
|
||||||
@@ -98,7 +103,8 @@ pub enum BinOp {
|
|||||||
Upd,
|
Upd,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub enum UnOp {
|
pub enum UnOp {
|
||||||
Neg,
|
Neg,
|
||||||
Not,
|
Not,
|
||||||
|
|||||||
112
src/compile.rs
112
src/compile.rs
@@ -35,7 +35,7 @@ impl Compiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn compile(mut self, ir: ir::Ir) -> OpCodes {
|
fn compile(mut self, ir: ir::Ir) -> OpCodes {
|
||||||
ir.compile(&mut self);
|
ir.compile_force(&mut self);
|
||||||
self.opcodes()
|
self.opcodes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +60,12 @@ impl Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Compile {
|
pub trait Compile: Sized {
|
||||||
fn compile(self, comp: &mut Compiler);
|
fn compile(self, comp: &mut Compiler);
|
||||||
|
fn compile_force(self, comp: &mut Compiler) {
|
||||||
|
self.compile(comp);
|
||||||
|
comp.push(OpCode::ForceValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CompileWithLength {
|
pub trait CompileWithLength {
|
||||||
@@ -81,6 +85,9 @@ impl Compile for ir::Const {
|
|||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
comp.push(OpCode::Const { idx: self.idx });
|
comp.push(OpCode::Const { idx: self.idx });
|
||||||
}
|
}
|
||||||
|
fn compile_force(self, comp: &mut Compiler) {
|
||||||
|
self.compile(comp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compile for ir::Var {
|
impl Compile for ir::Var {
|
||||||
@@ -93,6 +100,9 @@ impl Compile for ir::Thunk {
|
|||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
comp.push(OpCode::LoadThunk { idx: self.idx });
|
comp.push(OpCode::LoadThunk { idx: self.idx });
|
||||||
}
|
}
|
||||||
|
fn compile_force(self, comp: &mut Compiler) {
|
||||||
|
comp.push(OpCode::LoadValue { idx: self.idx });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compile for ir::Attrs {
|
impl Compile for ir::Attrs {
|
||||||
@@ -110,7 +120,7 @@ impl Compile for ir::Attrs {
|
|||||||
}
|
}
|
||||||
for dynamic in self.dyns {
|
for dynamic in self.dyns {
|
||||||
let thunk = dynamic.1.is_thunk();
|
let thunk = dynamic.1.is_thunk();
|
||||||
dynamic.0.compile(comp);
|
dynamic.0.compile_force(comp);
|
||||||
dynamic.1.compile(comp);
|
dynamic.1.compile(comp);
|
||||||
if thunk && !self.rec {
|
if thunk && !self.rec {
|
||||||
comp.push(OpCode::CaptureEnv);
|
comp.push(OpCode::CaptureEnv);
|
||||||
@@ -138,11 +148,11 @@ impl Compile for ir::UnOp {
|
|||||||
use ir::UnOpKind::*;
|
use ir::UnOpKind::*;
|
||||||
match self.kind {
|
match self.kind {
|
||||||
Neg => {
|
Neg => {
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Neg });
|
comp.push(OpCode::UnOp { op: UnOp::Neg });
|
||||||
}
|
}
|
||||||
Not => {
|
Not => {
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Not });
|
comp.push(OpCode::UnOp { op: UnOp::Not });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,94 +164,94 @@ impl Compile for ir::BinOp {
|
|||||||
use ir::BinOpKind::*;
|
use ir::BinOpKind::*;
|
||||||
match self.kind {
|
match self.kind {
|
||||||
Add => {
|
Add => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Add });
|
comp.push(OpCode::BinOp { op: BinOp::Add });
|
||||||
}
|
}
|
||||||
Mul => {
|
Mul => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Mul });
|
comp.push(OpCode::BinOp { op: BinOp::Mul });
|
||||||
}
|
}
|
||||||
Div => {
|
Div => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Div });
|
comp.push(OpCode::BinOp { op: BinOp::Div });
|
||||||
}
|
}
|
||||||
And => {
|
And => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::And });
|
comp.push(OpCode::BinOp { op: BinOp::And });
|
||||||
}
|
}
|
||||||
Or => {
|
Or => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Or });
|
comp.push(OpCode::BinOp { op: BinOp::Or });
|
||||||
}
|
}
|
||||||
Eq => {
|
Eq => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Eq });
|
comp.push(OpCode::BinOp { op: BinOp::Eq });
|
||||||
}
|
}
|
||||||
Lt => {
|
Lt => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
||||||
}
|
}
|
||||||
Con => {
|
Con => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Con });
|
comp.push(OpCode::BinOp { op: BinOp::Con });
|
||||||
}
|
}
|
||||||
Upd => {
|
Upd => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Upd });
|
comp.push(OpCode::BinOp { op: BinOp::Upd });
|
||||||
}
|
}
|
||||||
|
|
||||||
Sub => {
|
Sub => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Sub });
|
comp.push(OpCode::BinOp { op: BinOp::Sub });
|
||||||
}
|
}
|
||||||
Impl => {
|
Impl => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Not });
|
comp.push(OpCode::UnOp { op: UnOp::Not });
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Or });
|
comp.push(OpCode::BinOp { op: BinOp::Or });
|
||||||
}
|
}
|
||||||
Neq => {
|
Neq => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Eq });
|
comp.push(OpCode::BinOp { op: BinOp::Eq });
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Not });
|
comp.push(OpCode::UnOp { op: UnOp::Not });
|
||||||
}
|
}
|
||||||
Gt => {
|
Gt => {
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
||||||
}
|
}
|
||||||
Leq => {
|
Leq => {
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Not });
|
comp.push(OpCode::UnOp { op: UnOp::Not });
|
||||||
}
|
}
|
||||||
Geq => {
|
Geq => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
comp.push(OpCode::BinOp { op: BinOp::Lt });
|
||||||
comp.push(OpCode::UnOp { op: UnOp::Not });
|
comp.push(OpCode::UnOp { op: UnOp::Not });
|
||||||
}
|
}
|
||||||
|
|
||||||
PipeL => {
|
PipeL => {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
comp.push(OpCode::Call);
|
comp.push(OpCode::Call);
|
||||||
}
|
}
|
||||||
PipeR => {
|
PipeR => {
|
||||||
self.rhs.compile(comp);
|
self.rhs.compile_force(comp);
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile_force(comp);
|
||||||
comp.push(OpCode::Call);
|
comp.push(OpCode::Call);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,13 +262,14 @@ impl Compile for ir::HasAttr {
|
|||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
self.lhs.compile(comp);
|
self.lhs.compile(comp);
|
||||||
for attr in self.rhs {
|
for attr in self.rhs {
|
||||||
|
comp.push(OpCode::ForceValue);
|
||||||
match attr {
|
match attr {
|
||||||
ir::Attr::Str(sym) => {
|
ir::Attr::Str(sym) => {
|
||||||
comp.push(OpCode::AttrSet { cap: 0 });
|
comp.push(OpCode::AttrSet { cap: 0 });
|
||||||
comp.push(OpCode::SelectOrDefault { sym })
|
comp.push(OpCode::SelectOrDefault { sym })
|
||||||
}
|
}
|
||||||
ir::Attr::Dynamic(dynamic) => {
|
ir::Attr::Dynamic(dynamic) => {
|
||||||
dynamic.compile(comp);
|
dynamic.compile_force(comp);
|
||||||
comp.push(OpCode::AttrSet { cap: 0 });
|
comp.push(OpCode::AttrSet { cap: 0 });
|
||||||
comp.push(OpCode::SelectDynamicOrDefault);
|
comp.push(OpCode::SelectDynamicOrDefault);
|
||||||
}
|
}
|
||||||
@@ -283,6 +294,7 @@ impl Compile for ir::Select {
|
|||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
self.expr.compile(comp);
|
self.expr.compile(comp);
|
||||||
for attr in self.attrpath {
|
for attr in self.attrpath {
|
||||||
|
comp.push(OpCode::ForceValue);
|
||||||
match attr {
|
match attr {
|
||||||
ir::Attr::Str(sym) => {
|
ir::Attr::Str(sym) => {
|
||||||
comp.push(OpCode::AttrSet { cap: 0 });
|
comp.push(OpCode::AttrSet { cap: 0 });
|
||||||
@@ -305,11 +317,7 @@ impl Compile for ir::Select {
|
|||||||
let last = comp.pop().unwrap();
|
let last = comp.pop().unwrap();
|
||||||
let _ = comp.pop();
|
let _ = comp.pop();
|
||||||
default.compile(comp);
|
default.compile(comp);
|
||||||
match last {
|
comp.push(last);
|
||||||
OpCode::SelectOrDefault { sym } => comp.push(OpCode::SelectOrDefault { sym }),
|
|
||||||
OpCode::SelectDynamicOrDefault => comp.push(OpCode::SelectDynamicOrDefault),
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let last = comp.pop().unwrap();
|
let last = comp.pop().unwrap();
|
||||||
@@ -322,14 +330,18 @@ impl Compile for ir::Select {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn compile_force(self, comp: &mut Compiler) {
|
||||||
|
self.compile(comp);
|
||||||
|
comp.push(OpCode::ForceValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Compile for ir::ConcatStrings {
|
impl Compile for ir::ConcatStrings {
|
||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
let mut iter = self.parts.into_iter();
|
let mut iter = self.parts.into_iter();
|
||||||
iter.next().unwrap().compile(comp);
|
iter.next().unwrap().compile_force(comp);
|
||||||
for item in iter {
|
for item in iter {
|
||||||
item.compile(comp);
|
item.compile_force(comp);
|
||||||
comp.push(OpCode::ConcatString);
|
comp.push(OpCode::ConcatString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -366,7 +378,7 @@ impl Compile for ir::Let {
|
|||||||
self.attrs.compile(comp);
|
self.attrs.compile(comp);
|
||||||
comp.push(OpCode::EnterLetEnv);
|
comp.push(OpCode::EnterLetEnv);
|
||||||
self.expr.compile(comp);
|
self.expr.compile(comp);
|
||||||
comp.push(OpCode::LeaveLetEnv);
|
comp.push(OpCode::LeaveEnv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,7 +387,7 @@ impl Compile for ir::With {
|
|||||||
self.namespace.compile(comp);
|
self.namespace.compile(comp);
|
||||||
comp.push(OpCode::EnterWithEnv);
|
comp.push(OpCode::EnterWithEnv);
|
||||||
self.expr.compile(comp);
|
self.expr.compile(comp);
|
||||||
comp.push(OpCode::LeaveWithEnv);
|
comp.push(OpCode::LeaveEnv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,7 +407,7 @@ impl Compile for ir::LoadFunc {
|
|||||||
|
|
||||||
impl Compile for ir::Call {
|
impl Compile for ir::Call {
|
||||||
fn compile(self, comp: &mut Compiler) {
|
fn compile(self, comp: &mut Compiler) {
|
||||||
self.func.compile(comp);
|
self.func.compile_force(comp);
|
||||||
self.args.into_iter().for_each(|arg| {
|
self.args.into_iter().for_each(|arg| {
|
||||||
arg.compile(comp);
|
arg.compile(comp);
|
||||||
comp.push(OpCode::Call);
|
comp.push(OpCode::Call);
|
||||||
|
|||||||
117
src/env.rs
117
src/env.rs
@@ -1,13 +1,13 @@
|
|||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
use gc_arena::{Collect, Gc, Mutation};
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
use crate::ty::internal::Value;
|
use crate::ty::internal::Value;
|
||||||
use crate::ty::common::Map;
|
|
||||||
|
|
||||||
#[derive(Collect)]
|
#[derive(Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub struct Env<'gc, K: Hash + Eq + Collect, V: Collect> {
|
pub struct Env<'gc, K: Hash + Eq + Collect<'gc>, V: Collect<'gc>> {
|
||||||
let_: Gc<'gc, LetEnv<'gc, K, V>>,
|
let_: Gc<'gc, LetEnv<'gc, K, V>>,
|
||||||
with: Gc<'gc, With<'gc, K, V>>,
|
with: Gc<'gc, With<'gc, K, V>>,
|
||||||
last: Option<Gc<'gc, Env<'gc, K, V>>>,
|
last: Option<Gc<'gc, Env<'gc, K, V>>>,
|
||||||
@@ -15,7 +15,7 @@ pub struct Env<'gc, K: Hash + Eq + Collect, V: Collect> {
|
|||||||
|
|
||||||
#[derive(Collect)]
|
#[derive(Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub struct LetEnv<'gc, K: Hash + Eq + Collect, V: Collect> {
|
pub struct LetEnv<'gc, K: Hash + Eq + Collect<'gc>, V: Collect<'gc>> {
|
||||||
map: LetNode<'gc, K, V>,
|
map: LetNode<'gc, K, V>,
|
||||||
last: Option<Gc<'gc, LetEnv<'gc, K, V>>>,
|
last: Option<Gc<'gc, LetEnv<'gc, K, V>>>,
|
||||||
}
|
}
|
||||||
@@ -24,17 +24,17 @@ pub type VmEnv<'gc> = Env<'gc, usize, Value<'gc>>;
|
|||||||
|
|
||||||
#[derive(Default, Clone, Collect)]
|
#[derive(Default, Clone, Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub struct With<'gc, K: Hash + Eq + Collect, V: Collect> {
|
pub struct With<'gc, K: Hash + Eq + Collect<'gc>, V: Collect<'gc>> {
|
||||||
map: Option<Gc<'gc, Map<K, V>>>,
|
map: Option<Gc<'gc, HashMap<K, V>>>,
|
||||||
last: Option<Gc<'gc, With<'gc, K, V>>>,
|
last: Option<Gc<'gc, With<'gc, K, V>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Collect)]
|
#[derive(Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
enum LetNode<'gc, K: Hash + Eq + Collect, V: Collect> {
|
enum LetNode<'gc, K: Hash + Eq + Collect<'gc>, V: Collect<'gc>> {
|
||||||
Let(Gc<'gc, Map<K, V>>),
|
Let(Gc<'gc, HashMap<K, V>>),
|
||||||
SingleArg(K, V),
|
SingleArg(K, V),
|
||||||
MultiArg(Gc<'gc, Map<K, V>>),
|
MultiArg(Gc<'gc, HashMap<K, V>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
@@ -44,19 +44,25 @@ pub enum Type {
|
|||||||
With,
|
With,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> Env<'gc, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect<'gc>, V: Clone + Collect<'gc>> Env<'gc, K, V> {
|
||||||
pub fn new(map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn new(map: Gc<'gc, HashMap<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Gc::new(mc, Self {
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Self {
|
||||||
let_: LetEnv::new(map, mc),
|
let_: LetEnv::new(map, mc),
|
||||||
with: Gc::new(mc, With {
|
with: Gc::new(
|
||||||
|
mc,
|
||||||
|
With {
|
||||||
map: None,
|
map: None,
|
||||||
last: None,
|
last: None,
|
||||||
}),
|
},
|
||||||
|
),
|
||||||
last: None,
|
last: None,
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&'gc self, symbol: &K) -> Option<&'gc V> {
|
pub fn lookup(&self, symbol: &K) -> Option<&V> {
|
||||||
if let Some(val) = self.let_.lookup(symbol) {
|
if let Some(val) = self.let_.lookup(symbol) {
|
||||||
return Some(val);
|
return Some(val);
|
||||||
}
|
}
|
||||||
@@ -64,27 +70,44 @@ impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> Env<'gc, K, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Gc::new(mc, Env {
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Env {
|
||||||
let_: self.let_.enter_arg(ident, val, mc),
|
let_: self.let_.enter_arg(ident, val, mc),
|
||||||
with: self.with,
|
with: self.with,
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_let(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter_let(
|
||||||
Gc::new(mc, Self {
|
self: Gc<'gc, Self>,
|
||||||
|
map: Gc<'gc, HashMap<K, V>>,
|
||||||
|
mc: &Mutation<'gc>,
|
||||||
|
) -> Gc<'gc, Self> {
|
||||||
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Self {
|
||||||
let_: self.let_.enter_let(map, mc),
|
let_: self.let_.enter_let(map, mc),
|
||||||
with: self.with,
|
with: self.with,
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_with(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter_with(
|
||||||
Gc::new(mc, Env {
|
self: Gc<'gc, Self>,
|
||||||
|
map: Gc<'gc, HashMap<K, V>>,
|
||||||
|
mc: &Mutation<'gc>,
|
||||||
|
) -> Gc<'gc, Self> {
|
||||||
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Env {
|
||||||
let_: self.let_,
|
let_: self.let_,
|
||||||
with: self.with.enter(map, mc),
|
with: self.with.enter(map, mc),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn leave(&self) -> Gc<'gc, Self> {
|
pub fn leave(&self) -> Gc<'gc, Self> {
|
||||||
@@ -92,12 +115,15 @@ impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> Env<'gc, K, V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> LetEnv<'gc, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect<'gc>, V: Clone + Collect<'gc>> LetEnv<'gc, K, V> {
|
||||||
pub fn new(map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn new(map: Gc<'gc, HashMap<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Gc::new(mc,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> {
|
||||||
@@ -118,32 +144,49 @@ impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> LetEnv<'gc, K, V>
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter_arg(self: Gc<'gc, Self>, ident: K, val: V, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||||
Gc::new(mc, 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(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter_let(
|
||||||
Gc::new(mc, Self {
|
self: Gc<'gc, Self>,
|
||||||
|
map: Gc<'gc, HashMap<K, V>>,
|
||||||
|
mc: &Mutation<'gc>,
|
||||||
|
) -> Gc<'gc, Self> {
|
||||||
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Self {
|
||||||
map: LetNode::Let(map),
|
map: LetNode::Let(map),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, K: Hash + Eq + Clone + Collect, V: Clone + Collect> With<'gc, K, V> {
|
impl<'gc, K: Hash + Eq + Clone + Collect<'gc>, V: Clone + Collect<'gc>> With<'gc, K, V> {
|
||||||
pub fn lookup(&'gc self, symbol: &K) -> Option<&'gc V> {
|
pub fn lookup(&self, symbol: &K) -> Option<&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(self: Gc<'gc, Self>, map: Gc<'gc, Map<K, V>>, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
pub fn enter(
|
||||||
Gc::new(mc, Self {
|
self: Gc<'gc, Self>,
|
||||||
|
map: Gc<'gc, HashMap<K, V>>,
|
||||||
|
mc: &Mutation<'gc>,
|
||||||
|
) -> Gc<'gc, Self> {
|
||||||
|
Gc::new(
|
||||||
|
mc,
|
||||||
|
Self {
|
||||||
map: Some(map),
|
map: Some(map),
|
||||||
last: Some(self),
|
last: Some(self),
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use inkwell::module::Module;
|
|||||||
use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
|
use inkwell::types::{FloatType, FunctionType, IntType, PointerType, StructType};
|
||||||
use inkwell::values::{BasicValueEnum, FunctionValue};
|
use inkwell::values::{BasicValueEnum, FunctionValue};
|
||||||
|
|
||||||
|
use crate::bytecode::OpCodes;
|
||||||
use crate::env::VmEnv;
|
use crate::env::VmEnv;
|
||||||
use crate::jit::JITValueData;
|
use crate::jit::JITValueData;
|
||||||
use crate::ty::internal::{Thunk, Value};
|
use crate::ty::internal::{Thunk, Value};
|
||||||
@@ -22,6 +23,8 @@ pub struct Helpers<'ctx> {
|
|||||||
pub value_type: StructType<'ctx>,
|
pub value_type: StructType<'ctx>,
|
||||||
pub func_type: FunctionType<'ctx>,
|
pub func_type: FunctionType<'ctx>,
|
||||||
|
|
||||||
|
pub new_thunk: FunctionValue<'ctx>,
|
||||||
|
|
||||||
pub debug: FunctionValue<'ctx>,
|
pub debug: FunctionValue<'ctx>,
|
||||||
pub capture_env: FunctionValue<'ctx>,
|
pub capture_env: FunctionValue<'ctx>,
|
||||||
pub neg: FunctionValue<'ctx>,
|
pub neg: FunctionValue<'ctx>,
|
||||||
@@ -48,6 +51,12 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
let ptr_type = context.ptr_type(AddressSpace::default());
|
let ptr_type = context.ptr_type(AddressSpace::default());
|
||||||
let value_type = context.struct_type(&[int_type.into(), int_type.into()], false);
|
let value_type = context.struct_type(&[int_type.into(), int_type.into()], false);
|
||||||
let func_type = value_type.fn_type(&[ptr_type.into(), ptr_type.into()], false);
|
let func_type = value_type.fn_type(&[ptr_type.into(), ptr_type.into()], false);
|
||||||
|
|
||||||
|
let new_thunk = module.add_function(
|
||||||
|
"new_thunk",
|
||||||
|
value_type.fn_type(&[ptr_type.into(), ptr_type.into()], false),
|
||||||
|
None,
|
||||||
|
);
|
||||||
let debug = module.add_function(
|
let debug = module.add_function(
|
||||||
"debug",
|
"debug",
|
||||||
context.void_type().fn_type(&[value_type.into()], false),
|
context.void_type().fn_type(&[value_type.into()], false),
|
||||||
@@ -55,9 +64,10 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
);
|
);
|
||||||
let capture_env = module.add_function(
|
let capture_env = module.add_function(
|
||||||
"capture_env",
|
"capture_env",
|
||||||
context
|
context.void_type().fn_type(
|
||||||
.void_type()
|
&[value_type.into(), ptr_type.into(), ptr_type.into()],
|
||||||
.fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into()], false),
|
false,
|
||||||
|
),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
let neg = module.add_function(
|
let neg = module.add_function(
|
||||||
@@ -109,6 +119,7 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
execution_engine.add_global_mapping(&new_thunk, helper_new_thunk as _);
|
||||||
execution_engine.add_global_mapping(&debug, helper_debug as _);
|
execution_engine.add_global_mapping(&debug, helper_debug as _);
|
||||||
execution_engine.add_global_mapping(&capture_env, helper_capture_env as _);
|
execution_engine.add_global_mapping(&capture_env, helper_capture_env as _);
|
||||||
execution_engine.add_global_mapping(&neg, helper_neg as _);
|
execution_engine.add_global_mapping(&neg, helper_neg as _);
|
||||||
@@ -130,6 +141,8 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
value_type,
|
value_type,
|
||||||
func_type,
|
func_type,
|
||||||
|
|
||||||
|
new_thunk,
|
||||||
|
|
||||||
debug,
|
debug,
|
||||||
capture_env,
|
capture_env,
|
||||||
neg,
|
neg,
|
||||||
@@ -188,25 +201,20 @@ impl<'ctx> Helpers<'ctx> {
|
|||||||
])
|
])
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_thunk(&self, thunk: *const Thunk) -> BasicValueEnum<'ctx> {
|
|
||||||
self.value_type
|
|
||||||
.const_named_struct(&[
|
|
||||||
self.int_type.const_int(ValueTag::Thunk as _, false).into(),
|
|
||||||
self.ptr_int_type.const_int(thunk as _, false).into(),
|
|
||||||
])
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn helper_debug(value: JITValue) {
|
extern "C" fn helper_debug(value: JITValue) {
|
||||||
dbg!(value.tag);
|
dbg!(value.tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn helper_capture_env<'gc>(thunk: JITValue, env: *const VmEnv<'gc>, mc: *const Mutation<'gc>) {
|
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 { Gc::from_ptr(env) };
|
let env = unsafe { Gc::from_ptr(env) };
|
||||||
thunk.capture(env, unsafe { mc.as_ref() }.unwrap());
|
thunk.capture_env(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 {
|
||||||
@@ -309,12 +317,21 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn helper_call(func: JITValue, arg: JITValue, vm: *const VM, mc: *const Mutation) -> 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(arg.into(), unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap())
|
func.call(
|
||||||
|
arg.into(),
|
||||||
|
unsafe { vm.as_ref() }.unwrap(),
|
||||||
|
unsafe { mc.as_ref() }.unwrap(),
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
func.into()
|
func.into()
|
||||||
}
|
}
|
||||||
@@ -329,7 +346,20 @@ extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn helper_force(thunk: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue {
|
extern "C" fn helper_force(thunk: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue {
|
||||||
let mut val = Value::from(thunk);
|
todo!()
|
||||||
val.force(unsafe { vm.as_ref() }.unwrap(), unsafe { mc.as_ref() }.unwrap()).unwrap();
|
/* let mut val = Value::from(thunk);
|
||||||
val.into()
|
val.force(
|
||||||
|
unsafe { vm.as_ref() }.unwrap(),
|
||||||
|
unsafe { mc.as_ref() }.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
val.into() */
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" fn helper_new_thunk(opcodes: *const OpCodes, mc: *const Mutation) -> JITValue {
|
||||||
|
Value::Thunk(Thunk::new(
|
||||||
|
unsafe { opcodes.as_ref() }.unwrap(),
|
||||||
|
unsafe { mc.as_ref() }.unwrap(),
|
||||||
|
))
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use gc_arena::{Collect, Gc};
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
use inkwell::OptimizationLevel;
|
use inkwell::OptimizationLevel;
|
||||||
use inkwell::builder::Builder;
|
use inkwell::builder::Builder;
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
@@ -60,10 +60,12 @@ impl<'gc> From<JITValue> for Value<'gc> {
|
|||||||
fn from(value: JITValue) -> Self {
|
fn from(value: JITValue) -> Self {
|
||||||
use ValueTag::*;
|
use ValueTag::*;
|
||||||
match value.tag {
|
match value.tag {
|
||||||
Int => Value::Const(Const::Int(unsafe { value.data.int })),
|
Int => Value::Int(unsafe { value.data.int }),
|
||||||
Null => Value::Const(Const::Null),
|
Null => Value::Null,
|
||||||
Function => Value::Func(unsafe { Gc::from_ptr(value.data.ptr as *const _) }),
|
Function => Value::Func(unsafe { Gc::from_ptr(value.data.ptr as *const _) }),
|
||||||
Thunk => Value::Thunk(self::Thunk { thunk: unsafe { Gc::from_ptr(value.data.ptr as *const _) } }),
|
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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +74,7 @@ impl<'gc> From<JITValue> for Value<'gc> {
|
|||||||
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::Int(int) => JITValue {
|
||||||
tag: ValueTag::Int,
|
tag: ValueTag::Int,
|
||||||
data: JITValueData { int },
|
data: JITValueData { int },
|
||||||
},
|
},
|
||||||
@@ -96,7 +98,7 @@ 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::Int(int) => JITValue {
|
||||||
tag: ValueTag::Int,
|
tag: ValueTag::Int,
|
||||||
data: JITValueData { int },
|
data: JITValueData { int },
|
||||||
},
|
},
|
||||||
@@ -117,18 +119,33 @@ impl From<Value<'_>> for JITValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Collect)]
|
pub struct JITFunc<'gc>(
|
||||||
#[collect(require_static)]
|
JitFunction<'gc, unsafe extern "C" fn(*const VmEnv<'gc>, *const Mutation<'gc>) -> JITValue>,
|
||||||
pub struct JITFunc<'gc>(JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>);
|
);
|
||||||
|
|
||||||
impl<'gc> From<JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>> for JITFunc<'gc> {
|
unsafe impl<'gc> Collect<'gc> for JITFunc<'gc> {
|
||||||
fn from(value: JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>) -> Self {
|
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, _: &mut T) {}
|
||||||
|
const NEEDS_TRACE: bool = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'gc>
|
||||||
|
From<
|
||||||
|
JitFunction<'gc, unsafe extern "C" fn(*const VmEnv<'gc>, *const Mutation<'gc>) -> JITValue>,
|
||||||
|
> for JITFunc<'gc>
|
||||||
|
{
|
||||||
|
fn from(
|
||||||
|
value: JitFunction<
|
||||||
|
'gc,
|
||||||
|
unsafe extern "C" fn(*const VmEnv<'gc>, *const Mutation<'gc>) -> JITValue,
|
||||||
|
>,
|
||||||
|
) -> Self {
|
||||||
Self(value)
|
Self(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc> Deref for JITFunc<'gc> {
|
impl<'gc> Deref for JITFunc<'gc> {
|
||||||
type Target = JitFunction<'gc, unsafe extern "C" fn(*const VM<'gc>, *const VmEnv<'gc>) -> JITValue>;
|
type Target =
|
||||||
|
JitFunction<'gc, unsafe extern "C" fn(*const VmEnv<'gc>, *const Mutation<'gc>) -> JITValue>;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
@@ -143,15 +160,10 @@ pub struct JITContext<'gc> {
|
|||||||
helpers: Helpers<'gc>,
|
helpers: Helpers<'gc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<'gc> Collect for JITContext<'gc> {
|
unsafe impl<'gc> Collect<'gc> for JITContext<'gc> {
|
||||||
fn trace(&self, _cc: &gc_arena::Collection) {}
|
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, _: &mut T) {}
|
||||||
fn needs_trace() -> bool
|
const NEEDS_TRACE: bool = false;
|
||||||
where
|
|
||||||
Self: Sized, {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'gc> JITContext<'gc> {
|
impl<'gc> JITContext<'gc> {
|
||||||
pub fn new(context: &'gc Context) -> Self {
|
pub fn new(context: &'gc Context) -> Self {
|
||||||
// force linker to link JIT engine
|
// force linker to link JIT engine
|
||||||
@@ -184,20 +196,25 @@ impl<'gc> JITContext<'gc> {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_function(
|
pub fn compile_function(&self, func: &'gc Func, vm: &'gc VM<'gc>) -> Result<JITFunc<'gc>> {
|
||||||
&self,
|
|
||||||
func: &'gc Func,
|
|
||||||
vm: &'gc VM<'gc>,
|
|
||||||
) -> 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
|
||||||
.module
|
.module
|
||||||
.add_function("nixjit_function", self.helpers.func_type, None);
|
.add_function("nixjit_function", self.helpers.func_type, None);
|
||||||
let env = func_.get_nth_param(1).unwrap().into_pointer_value();
|
let env = func_.get_nth_param(0).unwrap().into_pointer_value();
|
||||||
|
let mc = func_.get_nth_param(1).unwrap().into_pointer_value();
|
||||||
let entry = self.context.append_basic_block(func_, "entry");
|
let entry = self.context.append_basic_block(func_, "entry");
|
||||||
self.builder.position_at_end(entry);
|
self.builder.position_at_end(entry);
|
||||||
self.build_expr(&mut iter, vm, env, &mut stack, func_, func.opcodes.len())?;
|
self.build_expr(
|
||||||
|
&mut iter,
|
||||||
|
vm,
|
||||||
|
env,
|
||||||
|
mc,
|
||||||
|
&mut stack,
|
||||||
|
func_,
|
||||||
|
func.opcodes.len(),
|
||||||
|
)?;
|
||||||
|
|
||||||
assert_eq!(stack.len(), 1);
|
assert_eq!(stack.len(), 1);
|
||||||
let value = stack.pop();
|
let value = stack.pop();
|
||||||
@@ -221,13 +238,14 @@ impl<'gc> JITContext<'gc> {
|
|||||||
iter: &mut impl Iterator<Item = OpCode>,
|
iter: &mut impl Iterator<Item = OpCode>,
|
||||||
vm: &'gc VM<'gc>,
|
vm: &'gc VM<'gc>,
|
||||||
env: PointerValue<'gc>,
|
env: PointerValue<'gc>,
|
||||||
|
mc: PointerValue<'gc>,
|
||||||
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
||||||
func: FunctionValue<'gc>,
|
func: FunctionValue<'gc>,
|
||||||
mut length: usize,
|
mut length: usize,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
while length > 1 {
|
while length > 1 {
|
||||||
let opcode = iter.next().unwrap();
|
let opcode = iter.next().unwrap();
|
||||||
let br = self.single_op(opcode, vm, env, stack)?;
|
let br = self.single_op(opcode, vm, env, mc, stack)?;
|
||||||
length -= 1;
|
length -= 1;
|
||||||
if br > 0 {
|
if br > 0 {
|
||||||
let consq = self.context.append_basic_block(func, "consq");
|
let consq = self.context.append_basic_block(func, "consq");
|
||||||
@@ -259,13 +277,13 @@ impl<'gc> JITContext<'gc> {
|
|||||||
|
|
||||||
length -= br;
|
length -= br;
|
||||||
self.builder.position_at_end(consq);
|
self.builder.position_at_end(consq);
|
||||||
let br = self.build_expr(iter, vm, env, stack, func, br)?;
|
let br = self.build_expr(iter, vm, env, mc, stack, func, br)?;
|
||||||
self.builder.build_store(result, stack.pop())?;
|
self.builder.build_store(result, stack.pop())?;
|
||||||
self.builder.build_unconditional_branch(cont)?;
|
self.builder.build_unconditional_branch(cont)?;
|
||||||
|
|
||||||
length -= br;
|
length -= br;
|
||||||
self.builder.position_at_end(alter);
|
self.builder.position_at_end(alter);
|
||||||
self.build_expr(iter, vm, env, stack, func, br)?;
|
self.build_expr(iter, vm, env, mc, stack, func, br)?;
|
||||||
self.builder.build_store(result, stack.pop())?;
|
self.builder.build_store(result, stack.pop())?;
|
||||||
self.builder.build_unconditional_branch(cont)?;
|
self.builder.build_unconditional_branch(cont)?;
|
||||||
|
|
||||||
@@ -278,7 +296,7 @@ impl<'gc> JITContext<'gc> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if length > 0 {
|
if length > 0 {
|
||||||
self.single_op(iter.next().unwrap(), vm, env, stack)
|
self.single_op(iter.next().unwrap(), vm, env, mc, stack)
|
||||||
} else {
|
} else {
|
||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
@@ -290,6 +308,7 @@ impl<'gc> JITContext<'gc> {
|
|||||||
opcode: OpCode,
|
opcode: OpCode,
|
||||||
vm: &'gc VM<'_>,
|
vm: &'gc VM<'_>,
|
||||||
env: PointerValue<'gc>,
|
env: PointerValue<'gc>,
|
||||||
|
mc: PointerValue<'gc>,
|
||||||
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
stack: &mut Stack<BasicValueEnum<'gc>, CAP>,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
match opcode {
|
match opcode {
|
||||||
@@ -304,8 +323,18 @@ impl<'gc> JITContext<'gc> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::LoadThunk { idx } => stack.push(
|
OpCode::LoadThunk { idx } => stack.push(
|
||||||
self.helpers
|
self.builder
|
||||||
.new_thunk(Thunk::new(vm.get_thunk(idx))),
|
.build_direct_call(
|
||||||
|
self.helpers.new_thunk,
|
||||||
|
&[
|
||||||
|
self.new_ptr(vm.get_thunk(idx) as *const _).into(),
|
||||||
|
mc.into(),
|
||||||
|
],
|
||||||
|
"call_capture_env",
|
||||||
|
)?
|
||||||
|
.try_as_basic_value()
|
||||||
|
.unwrap_left()
|
||||||
|
.into(),
|
||||||
)?,
|
)?,
|
||||||
OpCode::CaptureEnv => {
|
OpCode::CaptureEnv => {
|
||||||
let thunk = *stack.tos();
|
let thunk = *stack.tos();
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use bumpalo::Bump;
|
|
||||||
use gc_arena::Arena;
|
use gc_arena::Arena;
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
|
||||||
@@ -17,29 +16,14 @@ use crate::ir::downgrade;
|
|||||||
use crate::jit::JITContext;
|
use crate::jit::JITContext;
|
||||||
use crate::ty::common::Const;
|
use crate::ty::common::Const;
|
||||||
use crate::ty::public::*;
|
use crate::ty::public::*;
|
||||||
use crate::vm::VM;
|
use crate::vm::run;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn test_expr(expr: &str, expected: Value) {
|
fn test_expr(expr: &str, expected: Value) {
|
||||||
let downgraded = downgrade(rnix::Root::parse(expr).tree().expr().unwrap()).unwrap();
|
let downgraded = downgrade(rnix::Root::parse(expr).tree().expr().unwrap()).unwrap();
|
||||||
let prog = compile(downgraded);
|
let prog = compile(downgraded);
|
||||||
dbg!(&prog);
|
dbg!(&prog);
|
||||||
let ctx = Context::create();
|
assert_eq!(run(prog).unwrap(), expected);
|
||||||
let jit = JITContext::new(&ctx);
|
|
||||||
let vm = VM::new(
|
|
||||||
prog.thunks,
|
|
||||||
prog.funcs,
|
|
||||||
prog.symbols.into(),
|
|
||||||
prog.symmap.into(),
|
|
||||||
prog.consts,
|
|
||||||
jit,
|
|
||||||
);
|
|
||||||
let env = env(&vm);
|
|
||||||
let value = vm
|
|
||||||
.eval(prog.top_level.into_iter(), vm.bump.alloc(env))
|
|
||||||
.unwrap()
|
|
||||||
.to_public(&vm, &mut HashSet::new());
|
|
||||||
assert_eq!(value, expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! map {
|
macro_rules! map {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#![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)]
|
#![feature(arbitrary_self_types)]
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ pub struct Stack<T, const CAP: usize> {
|
|||||||
top: usize,
|
top: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T: Collect, const CAP: usize> Collect for Stack<T, CAP> {
|
unsafe impl<'gc, T: Collect<'gc>, const CAP: usize> Collect<'gc> for Stack<T, CAP> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||||
for v in self.iter() {
|
for v in self.iter() {
|
||||||
v.trace(cc);
|
v.trace(cc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,22 @@
|
|||||||
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;
|
use gc_arena::Collect;
|
||||||
|
|
||||||
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)]
|
#[derive(Clone, Debug, PartialEq, Constructor, Hash, Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub struct Catchable {
|
pub struct Catchable {
|
||||||
msg: String,
|
msg: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<String> for Catchable {
|
||||||
|
fn from(value: String) -> Self {
|
||||||
|
Catchable { msg: value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for Catchable {
|
impl Display for Catchable {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||||
write!(f, "<error: {}>", self.msg)
|
write!(f, "<error: {}>", self.msg)
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
|
use std::ops::Deref;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use hashbrown::{HashMap, HashSet};
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use itertools::Itertools;
|
|
||||||
use gc_arena::{Collect, Gc, Mutation};
|
use gc_arena::{Collect, Gc, Mutation};
|
||||||
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::env::VmEnv;
|
use crate::env::VmEnv;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
@@ -13,25 +14,25 @@ use super::super::public as p;
|
|||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
#[derive(Constructor, Clone, PartialEq)]
|
#[derive(Constructor, Clone, PartialEq, Collect)]
|
||||||
|
#[collect(no_drop)]
|
||||||
pub struct AttrSet<'gc> {
|
pub struct AttrSet<'gc> {
|
||||||
data: HashMap<usize, Value<'gc>>,
|
data: HashMap<usize, Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for AttrSet<'gc> {
|
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
|
||||||
for (_, v) in self.data.iter() {
|
|
||||||
v.trace(cc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
||||||
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||||
Self { data }
|
Self { data }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'gc> Deref for AttrSet<'gc> {
|
||||||
|
type Target = HashMap<usize, Value<'gc>>;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||||
pub fn with_capacity(cap: usize) -> Self {
|
pub fn with_capacity(cap: usize) -> Self {
|
||||||
AttrSet {
|
AttrSet {
|
||||||
@@ -61,7 +62,7 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
|||||||
pub fn capture(&mut self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
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, mc);
|
thunk.capture_env(env, mc);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -85,12 +86,13 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> 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();
|
todo!()
|
||||||
|
/* 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, mc)?;
|
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<'gc>) -> bool {
|
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use gc_arena::lock::{GcRefLock, RefLock};
|
||||||
use gc_arena::lock::GcRefLock;
|
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
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 +11,7 @@ use crate::error::Result;
|
|||||||
use crate::ir;
|
use crate::ir;
|
||||||
use crate::jit::JITFunc;
|
use crate::jit::JITFunc;
|
||||||
use crate::ty::internal::{Thunk, Value};
|
use crate::ty::internal::{Thunk, Value};
|
||||||
use crate::vm::VM;
|
use crate::vm::{GcRoot, VM, eval};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Collect)]
|
#[derive(Debug, Clone, Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
@@ -46,7 +44,7 @@ impl From<ir::Param> for Param {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Constructor)]
|
#[derive(Clone)]
|
||||||
pub struct Func<'gc> {
|
pub struct Func<'gc> {
|
||||||
pub func: &'gc BFunc,
|
pub func: &'gc BFunc,
|
||||||
pub env: Gc<'gc, VmEnv<'gc>>,
|
pub env: Gc<'gc, VmEnv<'gc>>,
|
||||||
@@ -54,17 +52,33 @@ pub struct Func<'gc> {
|
|||||||
pub count: Cell<usize>,
|
pub count: Cell<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<'gc> Collect for Func<'gc> {
|
unsafe impl<'gc> Collect<'gc> for Func<'gc> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||||
self.env.trace(cc);
|
self.env.trace(cc);
|
||||||
self.compiled.trace(cc);
|
self.compiled.trace(cc);
|
||||||
self.count.trace(cc);
|
self.count.trace(cc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
impl<'gc> Func<'gc> {
|
||||||
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
pub fn new(func: &'gc BFunc, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) -> Self {
|
||||||
use Param::*;
|
Self {
|
||||||
|
func,
|
||||||
|
env,
|
||||||
|
compiled: Gc::new(mc, RefLock::new(None)),
|
||||||
|
count: Cell::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call(
|
||||||
|
&self,
|
||||||
|
arg: Value<'gc>,
|
||||||
|
vm: &'gc VM<'gc>,
|
||||||
|
mc: &Mutation<'gc>,
|
||||||
|
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
|
||||||
|
) -> Result<Value<'gc>> {
|
||||||
|
todo!()
|
||||||
|
/* use Param::*;
|
||||||
|
|
||||||
let mut env = self.env;
|
let mut env = self.env;
|
||||||
env = match self.func.param.clone() {
|
env = match self.func.param.clone() {
|
||||||
@@ -75,8 +89,7 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
|||||||
alias,
|
alias,
|
||||||
} => {
|
} => {
|
||||||
let arg = arg.unwrap_attr_set();
|
let arg = arg.unwrap_attr_set();
|
||||||
let mut new =
|
let mut new = HashMap::with_capacity(formals.len() + alias.iter().len());
|
||||||
HashMap::with_capacity(formals.len() + alias.iter().len());
|
|
||||||
if !ellipsis
|
if !ellipsis
|
||||||
&& arg
|
&& arg
|
||||||
.as_inner()
|
.as_inner()
|
||||||
@@ -91,7 +104,8 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
|||||||
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), mc).into()))
|
default
|
||||||
|
.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
new.insert(formal, arg);
|
new.insert(formal, arg);
|
||||||
@@ -106,15 +120,18 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
|||||||
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.borrow_mut(mc).get_or_insert_with(|| vm.compile_func(self.func));
|
let compiled = &mut *self.compiled.borrow_mut(mc);
|
||||||
let ret = unsafe { compiled.call(vm as *const VM, env.as_ref() as *const VmEnv) };
|
let compiled = compiled.get_or_insert_with(|| vm.compile_func(self.func));
|
||||||
|
let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
|
||||||
return Ok(ret.into());
|
return Ok(ret.into());
|
||||||
}
|
}
|
||||||
vm.eval(self.func.opcodes.iter().copied(), env)
|
eval(self.func.opcodes.iter().copied(), arena, |val, _| {
|
||||||
|
Ok(val)
|
||||||
|
}) */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Func<'_, '_, '_> {
|
impl PartialEq for Func<'_> {
|
||||||
fn eq(&self, _: &Self) -> bool {
|
fn eq(&self, _: &Self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
use hashbrown::HashSet;
|
use hashbrown::HashSet;
|
||||||
|
|
||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
|
use gc_arena::{Arena, Collect, Rootable};
|
||||||
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;
|
||||||
use crate::vm::VM;
|
use crate::vm::{GcRoot, VM};
|
||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
|
|
||||||
#[derive(Constructor, Clone, PartialEq)]
|
#[derive(Constructor, Clone, PartialEq)]
|
||||||
pub struct List<'gc> {
|
pub struct List<'gc> {
|
||||||
data: Vector<Value<'gc>>
|
data: Vector<Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Collect for List<'_> {
|
unsafe impl<'gc> Collect<'gc> for List<'gc> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||||
for v in self.data.iter() {
|
for v in self.data.iter() {
|
||||||
v.trace(cc);
|
v.trace(cc);
|
||||||
}
|
}
|
||||||
@@ -40,15 +40,6 @@ impl<'gc> List<'gc> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
|
|
||||||
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
|
||||||
for v in vec.iter_mut() {
|
|
||||||
v.force_deep(vm, mc)?;
|
|
||||||
}
|
|
||||||
self.data = vec.into_iter().collect();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> 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
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
use std::marker::PhantomData;
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
@@ -30,22 +31,24 @@ pub use primop::*;
|
|||||||
|
|
||||||
#[derive(Collect)]
|
#[derive(Collect)]
|
||||||
#[collect(unsafe_drop)]
|
#[collect(unsafe_drop)]
|
||||||
pub struct CoW<'gc, T: Clone + Collect> {
|
pub struct CoW<'gc, T: Clone + Collect<'gc>> {
|
||||||
inner: Gc<'gc, CoWInner<T>>,
|
inner: Gc<'gc, CoWInner<'gc, T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CoWInner<T: Clone + Collect> {
|
struct CoWInner<'gc, T: Clone + Collect<'gc>> {
|
||||||
ref_count: Cell<usize>,
|
ref_count: Cell<usize>,
|
||||||
data: MaybeUninit<T>,
|
data: MaybeUninit<T>,
|
||||||
|
_marker: PhantomData<Cell<&'gc ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T: Clone + Collect> Collect for CoWInner<T> {
|
unsafe impl<'gc, T: Clone + Collect<'gc>> Collect<'gc> for CoWInner<'gc, T> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||||
unsafe { self.data.assume_init_ref() }.trace(cc);
|
unsafe { self.data.assume_init_ref() }.trace(cc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
#[allow(mutable_transmutes)]
|
||||||
|
impl<'gc, T: Clone + Collect<'gc>> CoW<'gc, T> {
|
||||||
pub fn new(data: T, mc: &Mutation<'gc>) -> Self {
|
pub fn new(data: T, mc: &Mutation<'gc>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: Gc::new(mc, CoWInner::new(data)),
|
inner: Gc::new(mc, CoWInner::new(data)),
|
||||||
@@ -58,6 +61,7 @@ impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
|||||||
CoWInner {
|
CoWInner {
|
||||||
ref_count: Cell::new(1),
|
ref_count: Cell::new(1),
|
||||||
data: MaybeUninit::uninit(),
|
data: MaybeUninit::uninit(),
|
||||||
|
_marker: PhantomData,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
let data = datafn(CoW { inner });
|
let data = datafn(CoW { inner });
|
||||||
@@ -81,37 +85,38 @@ impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, T: Clone + Collect> AsRef<T> for CoW<'gc, T> {
|
impl<'gc, T: Clone + Collect<'gc>> AsRef<T> for CoW<'gc, T> {
|
||||||
fn as_ref(&self) -> &T {
|
fn as_ref(&self) -> &T {
|
||||||
unsafe { self.inner.data.assume_init_ref() }
|
unsafe { self.inner.data.assume_init_ref() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, T: Clone + Collect> Deref for CoW<'gc, T> {
|
impl<'gc, T: Clone + Collect<'gc>> Deref for CoW<'gc, T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.as_ref()
|
self.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, T: Clone + Collect> Clone for CoW<'gc, T> {
|
impl<'gc, T: Clone + Collect<'gc>> Clone for CoW<'gc, T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
self.inner.ref_count.set(self.inner.ref_count.get() + 1);
|
self.inner.ref_count.set(self.inner.ref_count.get() + 1);
|
||||||
Self { inner: self.inner }
|
Self { inner: self.inner }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'gc, T: Clone + Collect> Drop for CoW<'gc, T> {
|
impl<'gc, T: Clone + Collect<'gc>> Drop for CoW<'gc, T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.inner.ref_count.set(self.inner.ref_count.get() - 1);
|
self.inner.ref_count.set(self.inner.ref_count.get() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Collect> CoWInner<T> {
|
impl<'gc, T: Clone + Collect<'gc>> CoWInner<'gc, T> {
|
||||||
fn new(data: T) -> Self {
|
fn new(data: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ref_count: Cell::new(1),
|
ref_count: Cell::new(1),
|
||||||
data: MaybeUninit::new(data),
|
data: MaybeUninit::new(data),
|
||||||
|
_marker: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,12 +124,16 @@ impl<T: Clone + Collect> CoWInner<T> {
|
|||||||
#[derive(IsVariant, Unwrap, Clone, Collect)]
|
#[derive(IsVariant, Unwrap, Clone, Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub enum Value<'gc> {
|
pub enum Value<'gc> {
|
||||||
Const(Const),
|
Int(i64),
|
||||||
|
Float(f64),
|
||||||
|
Bool(bool),
|
||||||
|
String(CoW<'gc, String>),
|
||||||
|
Null,
|
||||||
Thunk(Thunk<'gc>),
|
Thunk(Thunk<'gc>),
|
||||||
AttrSet(CoW<'gc, AttrSet<'gc>>),
|
AttrSet(CoW<'gc, AttrSet<'gc>>),
|
||||||
List(CoW<'gc, List<'gc>>),
|
List(CoW<'gc, List<'gc>>),
|
||||||
Catchable(Catchable),
|
Catchable(Gc<'gc, String>),
|
||||||
PrimOp(Gc<'gc, PrimOp<'gc>>),
|
PrimOp(Gc<'gc, PrimOp>),
|
||||||
PartialPrimOp(CoW<'gc, PartialPrimOp<'gc>>),
|
PartialPrimOp(CoW<'gc, PartialPrimOp<'gc>>),
|
||||||
Func(Gc<'gc, Func<'gc>>),
|
Func(Gc<'gc, Func<'gc>>),
|
||||||
}
|
}
|
||||||
@@ -134,7 +143,11 @@ impl Hash for Value<'_> {
|
|||||||
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),
|
Int(x) => x.hash(state),
|
||||||
|
Float(x) => x.to_bits().hash(state),
|
||||||
|
Bool(x) => x.hash(state),
|
||||||
|
Null => (),
|
||||||
|
String(x) => x.as_ptr().hash(state),
|
||||||
Thunk(x) => (x as *const self::Thunk).hash(state),
|
Thunk(x) => (x as *const self::Thunk).hash(state),
|
||||||
AttrSet(x) => x.as_ptr().hash(state),
|
AttrSet(x) => x.as_ptr().hash(state),
|
||||||
List(x) => x.as_ptr().hash(state),
|
List(x) => x.as_ptr().hash(state),
|
||||||
@@ -150,7 +163,13 @@ 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) {
|
||||||
(Const(a), Const(b)) => a.eq(b),
|
(Bool(a), Bool(b)) => a == b,
|
||||||
|
(Int(a), Int(b)) => a == b,
|
||||||
|
(Float(a), Float(b)) => a == b,
|
||||||
|
(Int(a), Float(b)) => *a as f64 == *b,
|
||||||
|
(Float(a), Int(b)) => *b as f64 == *a,
|
||||||
|
(String(a), String(b)) => a.as_str().eq(b.as_str()),
|
||||||
|
(Null, Null) => true,
|
||||||
(AttrSet(a), AttrSet(b)) => a.eq_impl(b),
|
(AttrSet(a), AttrSet(b)) => a.eq_impl(b),
|
||||||
(List(a), List(b)) => a.eq(b),
|
(List(a), List(b)) => a.eq(b),
|
||||||
_ => false,
|
_ => false,
|
||||||
@@ -162,7 +181,6 @@ 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),
|
|
||||||
(AttrSet(a), AttrSet(b)) => a.as_ptr().eq(&b.as_ptr()),
|
(AttrSet(a), AttrSet(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||||
(List(a), List(b)) => a.as_ptr().eq(&b.as_ptr()),
|
(List(a), List(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||||
_ => false,
|
_ => false,
|
||||||
@@ -174,24 +192,32 @@ impl Eq for Value<'_> {}
|
|||||||
|
|
||||||
#[derive(IsVariant, Unwrap, Clone)]
|
#[derive(IsVariant, Unwrap, Clone)]
|
||||||
pub enum ValueAsRef<'v, 'gc> {
|
pub enum ValueAsRef<'v, 'gc> {
|
||||||
Const(&'v Const),
|
Int(i64),
|
||||||
|
Float(f64),
|
||||||
|
Bool(bool),
|
||||||
|
String(&'v str),
|
||||||
|
Null,
|
||||||
Thunk(&'v Thunk<'gc>),
|
Thunk(&'v Thunk<'gc>),
|
||||||
AttrSet(&'v AttrSet<'gc>),
|
AttrSet(&'v AttrSet<'gc>),
|
||||||
List(&'v List<'gc>),
|
List(&'v List<'gc>),
|
||||||
Catchable(&'v Catchable),
|
Catchable(&'v str),
|
||||||
PrimOp(&'v PrimOp<'gc>),
|
PrimOp(&'v PrimOp),
|
||||||
PartialPrimOp(&'v PartialPrimOp<'gc>),
|
PartialPrimOp(&'v PartialPrimOp<'gc>),
|
||||||
Func(&'v Func<'gc>),
|
Func(&'v Func<'gc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(IsVariant, Unwrap)]
|
#[derive(IsVariant, Unwrap)]
|
||||||
pub enum ValueAsMut<'v, 'gc> {
|
pub enum ValueAsMut<'v, 'gc> {
|
||||||
Const(&'v mut Const),
|
Int(i64),
|
||||||
|
Float(f64),
|
||||||
|
Bool(bool),
|
||||||
|
String(&'v str),
|
||||||
|
Null,
|
||||||
Thunk(&'v Thunk<'gc>),
|
Thunk(&'v Thunk<'gc>),
|
||||||
AttrSet(&'v mut AttrSet<'gc>),
|
AttrSet(&'v mut AttrSet<'gc>),
|
||||||
List(&'v mut List<'gc>),
|
List(&'v mut List<'gc>),
|
||||||
Catchable(&'v mut Catchable),
|
Catchable(&'v str),
|
||||||
PrimOp(&'v PrimOp<'gc>),
|
PrimOp(&'v PrimOp),
|
||||||
PartialPrimOp(&'v mut PartialPrimOp<'gc>),
|
PartialPrimOp(&'v mut PartialPrimOp<'gc>),
|
||||||
Func(&'v Func<'gc>),
|
Func(&'v Func<'gc>),
|
||||||
}
|
}
|
||||||
@@ -201,7 +227,11 @@ impl<'gc, 'v> Value<'gc> {
|
|||||||
use Value::*;
|
use Value::*;
|
||||||
use ValueAsRef as R;
|
use ValueAsRef as R;
|
||||||
match self {
|
match self {
|
||||||
Const(x) => R::Const(x),
|
Int(x) => R::Int(*x),
|
||||||
|
Float(x) => R::Float(*x),
|
||||||
|
Bool(x) => R::Bool(*x),
|
||||||
|
String(x) => R::String(x),
|
||||||
|
Null => R::Null,
|
||||||
Thunk(x) => R::Thunk(x),
|
Thunk(x) => R::Thunk(x),
|
||||||
AttrSet(x) => R::AttrSet(x),
|
AttrSet(x) => R::AttrSet(x),
|
||||||
List(x) => R::List(x),
|
List(x) => R::List(x),
|
||||||
@@ -216,7 +246,11 @@ impl<'gc, 'v> Value<'gc> {
|
|||||||
use Value::*;
|
use Value::*;
|
||||||
use ValueAsMut as M;
|
use ValueAsMut as M;
|
||||||
match self {
|
match self {
|
||||||
Const(x) => M::Const(x),
|
Int(x) => M::Int(*x),
|
||||||
|
Float(x) => M::Float(*x),
|
||||||
|
Bool(x) => M::Bool(*x),
|
||||||
|
String(x) => M::String(x),
|
||||||
|
Null => M::Null,
|
||||||
Thunk(x) => M::Thunk(x),
|
Thunk(x) => M::Thunk(x),
|
||||||
AttrSet(x) => M::AttrSet(x.make_mut(mc)),
|
AttrSet(x) => M::AttrSet(x.make_mut(mc)),
|
||||||
List(x) => M::List(x.make_mut(mc)),
|
List(x) => M::List(x.make_mut(mc)),
|
||||||
@@ -227,8 +261,6 @@ impl<'gc, 'v> Value<'gc> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use Value::Const as VmConst;
|
|
||||||
impl<'gc> Value<'gc> {
|
impl<'gc> Value<'gc> {
|
||||||
pub fn ok(self) -> Result<Self> {
|
pub fn ok(self) -> Result<Self> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
@@ -237,11 +269,11 @@ impl<'gc> Value<'gc> {
|
|||||||
pub fn typename(&self) -> &'static str {
|
pub fn typename(&self) -> &'static str {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match self {
|
match self {
|
||||||
Const(self::Const::Int(_)) => "int",
|
Int(_) => "int",
|
||||||
Const(self::Const::Float(_)) => "float",
|
Float(_) => "float",
|
||||||
Const(self::Const::Bool(_)) => "bool",
|
Bool(_) => "bool",
|
||||||
Const(self::Const::String(_)) => "string",
|
String(_) => "string",
|
||||||
Const(self::Const::Null) => "null",
|
Null => "null",
|
||||||
Thunk(_) => "thunk",
|
Thunk(_) => "thunk",
|
||||||
AttrSet(_) => "set",
|
AttrSet(_) => "set",
|
||||||
List(_) => "list",
|
List(_) => "list",
|
||||||
@@ -269,7 +301,7 @@ impl<'gc> Value<'gc> {
|
|||||||
*self = match self {
|
*self = match self {
|
||||||
PrimOp(func) => func.call(arg, vm, mc),
|
PrimOp(func) => func.call(arg, vm, mc),
|
||||||
PartialPrimOp(func) => func.call(arg, vm, mc),
|
PartialPrimOp(func) => func.call(arg, vm, mc),
|
||||||
Value::Func(func) => func.call(arg, vm, mc),
|
// Value::Func(func) => func.call(arg, vm, mc),
|
||||||
Catchable(_) => return Ok(()),
|
Catchable(_) => return Ok(()),
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}?;
|
}?;
|
||||||
@@ -277,18 +309,18 @@ impl<'gc> Value<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn not(&mut self) {
|
pub fn not(&mut self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match &*self {
|
*self = match &*self {
|
||||||
VmConst(Bool(bool)) => VmConst(Bool(!bool)),
|
Bool(bool) => Bool(!bool),
|
||||||
Value::Catchable(_) => return,
|
Value::Catchable(_) => return,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn and(&mut self, other: Self) {
|
pub fn and(&mut self, other: Self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a && b)),
|
(Bool(a), Bool(b)) => Bool(*a && b),
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -296,9 +328,9 @@ impl<'gc> Value<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn or(&mut self, other: Self) {
|
pub fn or(&mut self, other: Self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a || b)),
|
(Bool(a), Bool(b)) => Bool(*a || b),
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -306,52 +338,52 @@ impl<'gc> Value<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn eq(&mut self, other: Self) {
|
pub fn eq(&mut self, other: Self) {
|
||||||
use Const::Bool;
|
use Value::Bool;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
(s, other) => VmConst(Bool(s.eq_impl(&other))),
|
(s, other) => Bool(s.eq_impl(&other)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lt(&mut self, other: Self) {
|
pub fn lt(&mut self, other: Self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = VmConst(Bool(match (&*self, other) {
|
*self = Bool(match (&*self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => *a < b,
|
(Int(a), Int(b)) => *a < b,
|
||||||
(VmConst(Int(a)), VmConst(Float(b))) => (*a as f64) < b,
|
(Int(a), Float(b)) => (*a as f64) < b,
|
||||||
(VmConst(Float(a)), VmConst(Int(b))) => *a < b as f64,
|
(Float(a), Int(b)) => *a < b as f64,
|
||||||
(VmConst(Float(a)), VmConst(Float(b))) => *a < b,
|
(Float(a), Float(b)) => *a < b,
|
||||||
(VmConst(String(a)), VmConst(String(b))) => a < &b,
|
(String(a), String(b)) => a.as_str() < b.as_str(),
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => {
|
(_, x @ Value::Catchable(_)) => {
|
||||||
*self = x;
|
*self = x;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn neg(&mut self) {
|
pub fn neg(&mut self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match &*self {
|
*self = match &*self {
|
||||||
VmConst(Int(int)) => VmConst(Int(-int)),
|
Int(int) => Int(-int),
|
||||||
VmConst(Float(float)) => VmConst(Float(-float)),
|
Float(float) => Float(-float),
|
||||||
Value::Catchable(_) => return,
|
Value::Catchable(_) => return,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, other: Self) {
|
pub fn add(&mut self, other: Self, mc: &Mutation<'gc>) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
(Int(a), Int(b)) => Int(a + b),
|
||||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 + b)),
|
(Int(a), Float(b)) => Float(*a as f64 + b),
|
||||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a + b as f64)),
|
(Float(a), Int(b)) => Float(a + b as f64),
|
||||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a + b)),
|
(Float(a), Float(b)) => Float(a + b),
|
||||||
(VmConst(String(a)), VmConst(String(b))) => {
|
(String(a), String(b)) => {
|
||||||
let mut string = a.clone();
|
let mut a = a.clone();
|
||||||
string.push_str(b.as_str());
|
a.make_mut(mc).push_str(b.as_str());
|
||||||
VmConst(String(string))
|
String(a)
|
||||||
}
|
}
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
@@ -360,12 +392,12 @@ impl<'gc> Value<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn mul(&mut self, other: Self) {
|
pub fn mul(&mut self, other: Self) {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
(Int(a), Int(b)) => Int(a * b),
|
||||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 * b)),
|
(Int(a), Float(b)) => Float(*a as f64 * b),
|
||||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a * b as f64)),
|
(Float(a), Int(b)) => Float(a * b as f64),
|
||||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a * b)),
|
(Float(a), Float(b)) => Float(a * b),
|
||||||
(Value::Catchable(_), _) => return,
|
(Value::Catchable(_), _) => return,
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -373,16 +405,16 @@ impl<'gc> Value<'gc> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn div(&mut self, other: Self) -> Result<()> {
|
pub fn div(&mut self, other: Self) -> Result<()> {
|
||||||
use Const::*;
|
use Value::*;
|
||||||
*self = match (&*self, other) {
|
*self = match (&*self, other) {
|
||||||
(_, VmConst(Int(0))) => return Err(Error::EvalError("division by zero".to_string())),
|
(_, Int(0)) => return Err(Error::EvalError("division by zero".to_string())),
|
||||||
(_, VmConst(Float(0.))) => {
|
(_, Float(0.)) => {
|
||||||
return Err(Error::EvalError("division by zero".to_string()));
|
return Err(Error::EvalError("division by zero".to_string()));
|
||||||
}
|
}
|
||||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
(Int(a), Int(b)) => Int(a / b),
|
||||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 / b)),
|
(Int(a), Float(b)) => Float(*a as f64 / b),
|
||||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
(Float(a), Int(b)) => Float(a / b as f64),
|
||||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
(Float(a), Float(b)) => Float(a / b),
|
||||||
(Value::Catchable(_), _) => return Ok(()),
|
(Value::Catchable(_), _) => return Ok(()),
|
||||||
(_, x @ Value::Catchable(_)) => x,
|
(_, x @ Value::Catchable(_)) => x,
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -390,9 +422,12 @@ impl<'gc> Value<'gc> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn concat_string(&mut self, mut other: Self) -> &mut Self {
|
pub fn concat_string(&mut self, mut other: Self, mc: &Mutation<'gc>) -> &mut Self {
|
||||||
match (self.coerce_to_string(), other.coerce_to_string()) {
|
match (self.coerce_to_string(), other.coerce_to_string()) {
|
||||||
(VmConst(Const::String(a)), VmConst(Const::String(b))) => a.push_str(b.as_str()),
|
(Value::String(a), Value::String(b)) => {
|
||||||
|
let mut a = a.clone();
|
||||||
|
a.make_mut(mc).push_str(b.as_str())
|
||||||
|
},
|
||||||
(_, Value::Catchable(_)) => *self = other,
|
(_, Value::Catchable(_)) => *self = other,
|
||||||
(Value::Catchable(_), _) => (),
|
(Value::Catchable(_), _) => (),
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
@@ -484,17 +519,17 @@ impl<'gc> Value<'gc> {
|
|||||||
|
|
||||||
pub fn has_attr(&mut self, sym: usize) -> &mut Self {
|
pub fn has_attr(&mut self, sym: usize) -> &mut Self {
|
||||||
if let Value::AttrSet(attrs) = self {
|
if let Value::AttrSet(attrs) = self {
|
||||||
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
|
let val = Value::Bool(attrs.has_attr(sym));
|
||||||
*self = val;
|
*self = val;
|
||||||
} else if let Value::Catchable(_) = self {
|
} else if let Value::Catchable(_) = self {
|
||||||
} else {
|
} else {
|
||||||
*self = VmConst(Const::Bool(false));
|
*self = Value::Bool(false);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn coerce_to_string(&mut self) -> &mut Self {
|
pub fn coerce_to_string(&mut self) -> &mut Self {
|
||||||
if let VmConst(Const::String(_)) = self {
|
if let Value::String(_) = self {
|
||||||
} else if let Value::Catchable(_) = self {
|
} else if let Value::Catchable(_) = self {
|
||||||
} else {
|
} else {
|
||||||
todo!()
|
todo!()
|
||||||
@@ -502,33 +537,7 @@ impl<'gc> Value<'gc> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> {
|
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||||
if let Value::Thunk(thunk) = self {
|
|
||||||
let value = thunk.force(vm, mc)?.clone();
|
|
||||||
*self = value
|
|
||||||
}
|
|
||||||
Ok(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn force_deep(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> {
|
|
||||||
match self {
|
|
||||||
Value::Thunk(thunk) => {
|
|
||||||
let mut value = thunk.force(vm, mc)?.clone();
|
|
||||||
let _ = value.force_deep(vm, mc)?;
|
|
||||||
*self = value;
|
|
||||||
}
|
|
||||||
Value::List(list) => list.make_mut(mc).force_deep(vm, mc)?,
|
|
||||||
Value::AttrSet(attrs) => attrs.make_mut(mc).force_deep(vm, mc)?,
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
Ok(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_public(
|
|
||||||
&self,
|
|
||||||
vm: &VM<'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) {
|
||||||
@@ -543,8 +552,12 @@ impl<'gc> Value<'gc> {
|
|||||||
seen.insert(self.clone());
|
seen.insert(self.clone());
|
||||||
list.to_public(vm, seen)
|
list.to_public(vm, seen)
|
||||||
}
|
}
|
||||||
Catchable(catchable) => Value::Catchable(catchable.clone()),
|
Catchable(catchable) => Value::Catchable(catchable.as_ref().clone().into()),
|
||||||
Const(cnst) => Value::Const(cnst.clone()),
|
Int(x) => Value::Const(Const::Int(*x)),
|
||||||
|
Float(x) => Value::Const(Const::Float(*x)),
|
||||||
|
Bool(x) => Value::Const(Const::Bool(*x)),
|
||||||
|
String(x) => Value::Const(Const::String(x.as_ref().into())),
|
||||||
|
Null => Value::Const(Const::Null),
|
||||||
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),
|
||||||
@@ -562,16 +575,13 @@ pub struct Thunk<'gc> {
|
|||||||
|
|
||||||
#[derive(IsVariant, Unwrap)]
|
#[derive(IsVariant, Unwrap)]
|
||||||
pub enum _Thunk<'gc> {
|
pub enum _Thunk<'gc> {
|
||||||
Code(
|
Code(&'gc OpCodes, Option<Gc<'gc, VmEnv<'gc>>>),
|
||||||
&'gc OpCodes,
|
|
||||||
GcRefLock<'gc, Option<Gc<'gc, VmEnv<'gc>>>>,
|
|
||||||
),
|
|
||||||
Suspended,
|
Suspended,
|
||||||
Value(Value<'gc>),
|
Value(Value<'gc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<'gc> Collect for _Thunk<'gc> {
|
unsafe impl<'gc> Collect<'gc> for _Thunk<'gc> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, cc: &mut T) {
|
||||||
use _Thunk::*;
|
use _Thunk::*;
|
||||||
match self {
|
match self {
|
||||||
Code(_, env) => env.trace(cc),
|
Code(_, env) => env.trace(cc),
|
||||||
@@ -584,48 +594,34 @@ unsafe impl<'gc> Collect for _Thunk<'gc> {
|
|||||||
impl<'gc> Thunk<'gc> {
|
impl<'gc> Thunk<'gc> {
|
||||||
pub fn new(opcodes: &'gc OpCodes, mc: &Mutation<'gc>) -> Self {
|
pub fn new(opcodes: &'gc OpCodes, mc: &Mutation<'gc>) -> Self {
|
||||||
Thunk {
|
Thunk {
|
||||||
thunk: Gc::new(
|
thunk: Gc::new(mc, RefLock::new(_Thunk::Code(opcodes, None))),
|
||||||
mc,
|
|
||||||
RefLock::new(_Thunk::Code(opcodes, Gc::new(mc, RefLock::new(None)))),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn capture(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
pub fn capture_env(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||||
if let _Thunk::Code(_, envcell) = &*self.thunk.borrow() {
|
if let _Thunk::Code(_, envcell) = &mut *self.thunk.borrow_mut(mc) {
|
||||||
envcell.borrow_mut(mc).insert(env);
|
let _ = envcell.insert(env);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force(&self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
pub fn suspend(&self, mc: &Mutation<'gc>) -> (&'gc OpCodes, Gc<'gc, VmEnv<'gc>>) {
|
||||||
use _Thunk::*;
|
let _Thunk::Code(opcodes, env) =
|
||||||
match &*self.thunk.borrow() {
|
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended)
|
||||||
Value(_) => {
|
else {
|
||||||
return Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
unreachable!()
|
||||||
Value(value) => value.clone(),
|
};
|
||||||
_ => unreachable!(),
|
(opcodes, env.unwrap())
|
||||||
});
|
|
||||||
}
|
|
||||||
Suspended => {
|
|
||||||
return Err(Error::EvalError("infinite recursion encountered".into()));
|
|
||||||
}
|
|
||||||
Code(..) => (),
|
|
||||||
}
|
|
||||||
let (opcodes, env) =
|
|
||||||
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended).unwrap_code();
|
|
||||||
let env = env.as_ref().borrow().unwrap();
|
|
||||||
let value = vm.eval(opcodes.iter().copied(), env)?;
|
|
||||||
let _ = std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Value(value));
|
|
||||||
Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
|
||||||
Value(value) => value.clone(),
|
|
||||||
_ => unreachable!(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> Option<Value<'gc>> {
|
pub fn insert_value(&self, value: Value<'gc>, mc: &Mutation<'gc>) {
|
||||||
match &*self.thunk.borrow() {
|
*self.thunk.borrow_mut(mc) = _Thunk::Value(value);
|
||||||
_Thunk::Value(value) => Some(value.clone()),
|
}
|
||||||
_ => None,
|
|
||||||
|
pub fn get_value(&self) -> Option<Value<'gc>> {
|
||||||
|
if let _Thunk::Value(val) = &*self.thunk.borrow() {
|
||||||
|
Some(val.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,41 @@
|
|||||||
use derive_more::Constructor;
|
use derive_more::Constructor;
|
||||||
use gc_arena::Mutation;
|
|
||||||
use gc_arena::Collect;
|
use gc_arena::Collect;
|
||||||
|
use gc_arena::Mutation;
|
||||||
|
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::vm::VM;
|
use crate::vm::VM;
|
||||||
|
|
||||||
use super::Value;
|
|
||||||
use super::CoW;
|
use super::CoW;
|
||||||
|
use super::Value;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Constructor, Collect)]
|
#[derive(Debug, Clone, Constructor, Collect)]
|
||||||
#[collect(require_static)]
|
#[collect(require_static)]
|
||||||
pub struct PrimOp<'gc> {
|
pub struct PrimOp {
|
||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
func: for<'gc> 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<'gc> PrimOp<'gc> {
|
impl PrimOp {
|
||||||
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
pub fn call<'gc>(&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(CoW::new(
|
||||||
CoW::new(PartialPrimOp {
|
PartialPrimOp {
|
||||||
name: self.name,
|
name: self.name,
|
||||||
arity: self.arity - 1,
|
arity: self.arity - 1,
|
||||||
args,
|
args,
|
||||||
func: self.func,
|
func: self.func,
|
||||||
}, mc)
|
},
|
||||||
)
|
mc,
|
||||||
|
))
|
||||||
.ok()
|
.ok()
|
||||||
} else {
|
} else {
|
||||||
(self.func)(args, vm, mc)
|
(self.func)(args, vm, mc)
|
||||||
@@ -50,8 +51,8 @@ pub struct PartialPrimOp<'gc> {
|
|||||||
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for PartialPrimOp<'gc> {
|
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect<'gc> for PartialPrimOp<'gc> {
|
||||||
fn trace(&self, cc: &gc_arena::Collection) {
|
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||||
for v in self.args.iter() {
|
for v in self.args.iter() {
|
||||||
v.trace(cc);
|
v.trace(cc);
|
||||||
}
|
}
|
||||||
@@ -69,7 +70,7 @@ impl<'gc> PartialPrimOp<'gc> {
|
|||||||
self: &mut CoW<'gc, Self>,
|
self: &mut CoW<'gc, Self>,
|
||||||
arg: Value<'gc>,
|
arg: Value<'gc>,
|
||||||
vm: &VM,
|
vm: &VM,
|
||||||
mc: &Mutation<'gc>
|
mc: &Mutation<'gc>,
|
||||||
) -> Result<Value<'gc>> {
|
) -> Result<Value<'gc>> {
|
||||||
let func = self.func;
|
let func = self.func;
|
||||||
let self_mut = self.make_mut(mc);
|
let self_mut = self.make_mut(mc);
|
||||||
|
|||||||
256
src/vm/mod.rs
256
src/vm/mod.rs
@@ -1,9 +1,9 @@
|
|||||||
use std::cell::{Cell, RefCell};
|
use std::cell::RefCell;
|
||||||
|
|
||||||
use gc_arena::lock::{GcRefLock, RefLock};
|
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable};
|
||||||
use gc_arena::{Arena, Collect, DynamicRootSet, Gc, Mutation, Rootable};
|
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
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};
|
||||||
@@ -28,9 +28,8 @@ type GcArena = Arena<Rootable!['gc => GcRoot<'gc>]>;
|
|||||||
#[collect(require_static)]
|
#[collect(require_static)]
|
||||||
struct ContextWrapper(Context);
|
struct ContextWrapper(Context);
|
||||||
|
|
||||||
|
|
||||||
pub fn run(mut prog: Program) -> Result<p::Value> {
|
pub fn run(mut prog: Program) -> Result<p::Value> {
|
||||||
fn new<'gc>(prog: &mut Program, mc: &'gc Mutation<'gc>) -> GcRoot<'gc> {
|
let mut arena: Arena<Rootable![GcRoot<'_>]> = Arena::new(|mc| {
|
||||||
let jit = Gc::new(mc, ContextWrapper(Context::create()));
|
let jit = Gc::new(mc, ContextWrapper(Context::create()));
|
||||||
let thunks = std::mem::take(&mut prog.thunks);
|
let thunks = std::mem::take(&mut prog.thunks);
|
||||||
let funcs = std::mem::take(&mut prog.funcs);
|
let funcs = std::mem::take(&mut prog.funcs);
|
||||||
@@ -49,46 +48,113 @@ pub fn run(mut prog: Program) -> Result<p::Value> {
|
|||||||
GcRoot {
|
GcRoot {
|
||||||
vm,
|
vm,
|
||||||
jit,
|
jit,
|
||||||
ctxs: DynamicRootSet::new(mc)
|
stack: Stack::new(),
|
||||||
|
envs: vec![env(&vm, mc)],
|
||||||
}
|
}
|
||||||
}
|
|
||||||
let arena: Arena<Rootable![GcRoot<'_>]> = Arena::new(|mc| {
|
|
||||||
new(&mut prog, mc)
|
|
||||||
});
|
});
|
||||||
eval(prog.top_level.into_iter(), &arena, |val, vm| {
|
eval(prog.top_level, &mut arena, |val, root, _| {
|
||||||
Ok(val.to_public(vm, &mut HashSet::new()))
|
Ok(val.to_public(&root.vm, &mut HashSet::new()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>) -> Result<T>>(
|
||||||
fn eval<'gc, T, F: for<'a> FnOnce(Value<'a>, &VM<'a>) -> Result<T>>(
|
opcodes: Box<[OpCode]>,
|
||||||
opcodes: impl Iterator<Item = OpCode>,
|
arena: &mut Arena<impl for<'gc> Rootable<'gc, Root = GcRoot<'gc>>>,
|
||||||
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
|
f: F,
|
||||||
f: F
|
|
||||||
) -> Result<T> {
|
) -> Result<T> {
|
||||||
let mut iter = opcodes.into_iter();
|
let mut opcodes = opcodes.into_vec();
|
||||||
let dynroot: gc_arena::DynamicRoot<Rootable!['a => GcRefLock<'a, EvalContext<'a>>]> = arena.mutate(|mc, root| {
|
opcodes.reverse();
|
||||||
root.ctxs.stash(mc, Gc::new(mc, RefLock::new( EvalContext::new(env(&root.vm, mc)))))
|
while let Some(opcode) = opcodes.pop() {
|
||||||
});
|
arena.mutate_root(|mc, root| {
|
||||||
while let Some(opcode) = iter.next() {
|
let consq = single_op(
|
||||||
arena.mutate(|mc, root| {
|
&root.vm,
|
||||||
let mut ctx_mut = root.ctxs.fetch(&dynroot).borrow_mut(mc);
|
opcode,
|
||||||
let ctx = &mut *ctx_mut;
|
&mut root.stack,
|
||||||
let jmp = single_op(&root.vm, opcode, &mut ctx.stack, &mut ctx.env, mc)?;
|
root.envs.last_mut().unwrap(),
|
||||||
for _ in 0..jmp {
|
mc,
|
||||||
iter.next().unwrap();
|
)?;
|
||||||
|
match consq {
|
||||||
|
Consq::NoOp => (),
|
||||||
|
Consq::Jmp(step) => opcodes.resize_with(opcodes.len() - step, || unreachable!()),
|
||||||
|
Consq::Force => {
|
||||||
|
let thunk = root.stack.tos().as_ref().unwrap_thunk();
|
||||||
|
let (code, env) = thunk.suspend(mc);
|
||||||
|
let mut code = code.to_vec();
|
||||||
|
code.reverse();
|
||||||
|
opcodes.push(OpCode::InsertValue);
|
||||||
|
opcodes.push(OpCode::PopEnv);
|
||||||
|
opcodes.extend(code);
|
||||||
|
root.envs.push(env);
|
||||||
|
}
|
||||||
|
Consq::PopEnv => _ = root.envs.pop().unwrap(),
|
||||||
|
Consq::Call => {
|
||||||
|
use Param::*;
|
||||||
|
let arg = root.stack.pop();
|
||||||
|
let func = root.stack.pop().unwrap_func();
|
||||||
|
let mut env = func.env;
|
||||||
|
env = match func.func.param.clone() {
|
||||||
|
Ident(ident) => env.enter_arg(ident, arg, mc),
|
||||||
|
Formals {
|
||||||
|
formals,
|
||||||
|
ellipsis,
|
||||||
|
alias,
|
||||||
|
} => {
|
||||||
|
let arg = arg.unwrap_attr_set();
|
||||||
|
let mut new =
|
||||||
|
HashMap::with_capacity(formals.len() + alias.iter().len());
|
||||||
|
if !ellipsis
|
||||||
|
&& arg
|
||||||
|
.iter()
|
||||||
|
.map(|(k, _)| k)
|
||||||
|
.sorted()
|
||||||
|
.ne(formals.iter().map(|(k, _)| k).sorted())
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
for (formal, default) in formals {
|
||||||
|
// TODO: rec env
|
||||||
|
let arg = arg
|
||||||
|
.select(formal)
|
||||||
|
.or_else(|| {
|
||||||
|
default.map(|idx| {
|
||||||
|
Value::Thunk(
|
||||||
|
Thunk::new(root.vm.get_thunk(idx), mc).into(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
new.insert(formal, arg);
|
||||||
|
}
|
||||||
|
if let Some(alias) = alias {
|
||||||
|
new.insert(alias, Value::AttrSet(arg));
|
||||||
|
}
|
||||||
|
env.enter_let(Gc::new(mc, new.into()), mc)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
root.envs.push(env);
|
||||||
|
let mut code = func.func.opcodes.to_vec();
|
||||||
|
code.reverse();
|
||||||
|
opcodes.push(OpCode::PopEnv);
|
||||||
|
opcodes.extend(code);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
arena.mutate(|mc, root| {
|
arena.mutate_root(|mc, root| {
|
||||||
let mut ctx = root.ctxs.fetch(&dynroot).borrow_mut(mc);
|
assert_eq!(root.stack.len(), 1);
|
||||||
assert_eq!(ctx.stack.len(), 1);
|
let ret = root.stack.pop();
|
||||||
let mut ret = ctx.stack.pop();
|
f(ret, root, mc)
|
||||||
ret.force(&root.vm, mc);
|
|
||||||
f(ret, &root.vm)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Consq {
|
||||||
|
Jmp(usize),
|
||||||
|
Call,
|
||||||
|
Force,
|
||||||
|
PopEnv,
|
||||||
|
NoOp,
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn single_op<'gc, const CAP: usize>(
|
fn single_op<'gc, const CAP: usize>(
|
||||||
vm: &'gc VM<'gc>,
|
vm: &'gc VM<'gc>,
|
||||||
@@ -96,39 +162,62 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
stack: &mut Stack<Value<'gc>, CAP>,
|
stack: &mut Stack<Value<'gc>, CAP>,
|
||||||
env: &mut Gc<'gc, VmEnv<'gc>>,
|
env: &mut Gc<'gc, VmEnv<'gc>>,
|
||||||
mc: &'gc Mutation<'gc>,
|
mc: &'gc Mutation<'gc>,
|
||||||
) -> Result<usize> {
|
) -> Result<Consq> {
|
||||||
match opcode {
|
match opcode {
|
||||||
OpCode::Illegal => panic!("illegal opcode"),
|
OpCode::Illegal => panic!("illegal opcode"),
|
||||||
OpCode::Const { idx } => stack.push(Value::Const(vm.get_const(idx)))?,
|
OpCode::Const { idx } => {
|
||||||
|
stack.push(match vm.get_const(idx) {
|
||||||
|
Const::Int(x) => Value::Int(x),
|
||||||
|
Const::Float(x) => Value::Float(x),
|
||||||
|
Const::Bool(x) => Value::Bool(x),
|
||||||
|
Const::String(x) => Value::String(CoW::new(x.into(), mc)),
|
||||||
|
Const::Null => Value::Null
|
||||||
|
})?
|
||||||
|
},
|
||||||
OpCode::LoadThunk { idx } => stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?,
|
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::LoadValue { idx } => {
|
||||||
OpCode::ForceValue => {
|
stack.push(Value::Thunk(Thunk::new(vm.get_thunk(idx), mc)))?;
|
||||||
stack.tos_mut().force(vm, mc)?;
|
stack.tos().as_ref().unwrap_thunk().capture_env(*env, mc);
|
||||||
|
return Ok(Consq::Force);
|
||||||
}
|
}
|
||||||
OpCode::Jmp { step } => return Ok(step),
|
OpCode::CaptureEnv => stack.tos().as_ref().unwrap_thunk().capture_env(*env, mc),
|
||||||
|
OpCode::ForceValue => {
|
||||||
|
if !stack.tos().is_thunk() {
|
||||||
|
return Ok(Consq::NoOp);
|
||||||
|
}
|
||||||
|
let Some(val) = stack.tos().as_ref().unwrap_thunk().get_value() else {
|
||||||
|
return Ok(Consq::Force);
|
||||||
|
};
|
||||||
|
stack.pop();
|
||||||
|
stack.push(val)?;
|
||||||
|
}
|
||||||
|
OpCode::InsertValue => {
|
||||||
|
let val = stack.pop();
|
||||||
|
stack.pop().unwrap_thunk().insert_value(val.clone(), mc);
|
||||||
|
let _ = stack.push(val);
|
||||||
|
}
|
||||||
|
OpCode::Jmp { step } => return Ok(Consq::Jmp(step)),
|
||||||
OpCode::JmpIfFalse { step } => {
|
OpCode::JmpIfFalse { step } => {
|
||||||
if let Value::Const(Const::Bool(false)) = stack.pop() {
|
if let Value::Bool(false) = stack.pop() {
|
||||||
return Ok(step);
|
return Ok(Consq::Jmp(step));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::Call => {
|
OpCode::Call => {
|
||||||
let arg = stack.pop();
|
let arg = stack.pop();
|
||||||
let func = stack.tos_mut();
|
let func = stack.tos_mut();
|
||||||
func.force(vm, mc)?;
|
if func.is_func() {
|
||||||
|
stack.push(arg)?;
|
||||||
|
return Ok(Consq::Call);
|
||||||
|
}
|
||||||
func.call(arg, vm, mc)?;
|
func.call(arg, vm, mc)?;
|
||||||
}
|
}
|
||||||
OpCode::Func { idx } => {
|
OpCode::Func { idx } => {
|
||||||
let func = vm.get_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, mc))))?;
|
||||||
stack.push(Value::Func(Gc::new(
|
|
||||||
mc,
|
|
||||||
Func::new(func, *env, compiled, Cell::new(0)),
|
|
||||||
)))?;
|
|
||||||
}
|
}
|
||||||
OpCode::UnOp { op } => {
|
OpCode::UnOp { op } => {
|
||||||
use UnOp::*;
|
use UnOp::*;
|
||||||
let value = stack.tos_mut();
|
let value = stack.tos_mut();
|
||||||
value.force(vm, mc)?;
|
|
||||||
match op {
|
match op {
|
||||||
Neg => value.neg(),
|
Neg => value.neg(),
|
||||||
Not => value.not(),
|
Not => value.not(),
|
||||||
@@ -138,13 +227,11 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
use BinOp::*;
|
use BinOp::*;
|
||||||
let mut rhs = stack.pop();
|
let mut rhs = stack.pop();
|
||||||
let lhs = stack.tos_mut();
|
let lhs = stack.tos_mut();
|
||||||
lhs.force(vm, mc)?;
|
|
||||||
rhs.force(vm, mc)?;
|
|
||||||
match op {
|
match op {
|
||||||
Add => lhs.add(rhs),
|
Add => lhs.add(rhs, mc),
|
||||||
Sub => {
|
Sub => {
|
||||||
rhs.neg();
|
rhs.neg();
|
||||||
lhs.add(rhs);
|
lhs.add(rhs, mc);
|
||||||
}
|
}
|
||||||
Mul => lhs.mul(rhs),
|
Mul => lhs.mul(rhs),
|
||||||
Div => lhs.div(rhs)?,
|
Div => lhs.div(rhs)?,
|
||||||
@@ -157,9 +244,8 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::ConcatString => {
|
OpCode::ConcatString => {
|
||||||
let mut rhs = stack.pop();
|
let rhs = stack.pop();
|
||||||
rhs.force(vm, mc)?;
|
stack.tos_mut().concat_string(rhs, mc);
|
||||||
stack.tos_mut().concat_string(rhs);
|
|
||||||
}
|
}
|
||||||
OpCode::Path => {
|
OpCode::Path => {
|
||||||
todo!()
|
todo!()
|
||||||
@@ -197,47 +283,38 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
}
|
}
|
||||||
OpCode::PushDynamicAttr => {
|
OpCode::PushDynamicAttr => {
|
||||||
let val = stack.pop();
|
let val = stack.pop();
|
||||||
let mut sym = stack.pop();
|
let sym = stack.pop();
|
||||||
sym.force(vm, mc)?.coerce_to_string();
|
let sym = vm.new_sym(sym.unwrap_string().as_ref());
|
||||||
let sym = vm.new_sym(sym.unwrap_const().unwrap_string());
|
|
||||||
stack.tos_mut().push_attr(sym, val, mc);
|
stack.tos_mut().push_attr(sym, val, mc);
|
||||||
}
|
}
|
||||||
OpCode::Select { sym } => {
|
OpCode::Select { sym } => {
|
||||||
stack.tos_mut().force(vm, mc)?.select(sym, vm)?;
|
stack.tos_mut().select(sym, vm)?;
|
||||||
}
|
}
|
||||||
OpCode::SelectOrDefault { sym } => {
|
OpCode::SelectOrDefault { sym } => {
|
||||||
let default = stack.pop();
|
let default = stack.pop();
|
||||||
stack
|
stack.tos_mut().select_with_default(sym, default)?;
|
||||||
.tos_mut()
|
|
||||||
.force(vm, mc)?
|
|
||||||
.select_with_default(sym, default)?;
|
|
||||||
}
|
}
|
||||||
OpCode::SelectDynamic => {
|
OpCode::SelectDynamic => {
|
||||||
let mut val = stack.pop();
|
let mut val = stack.pop();
|
||||||
val.force(vm, mc)?;
|
|
||||||
val.coerce_to_string();
|
val.coerce_to_string();
|
||||||
let sym = vm.new_sym(val.unwrap_const().unwrap_string());
|
let sym = vm.new_sym(val.unwrap_string().as_ref());
|
||||||
stack.tos_mut().force(vm, mc)?.select(sym, vm)?;
|
stack.tos_mut().select(sym, vm)?;
|
||||||
}
|
}
|
||||||
OpCode::SelectDynamicOrDefault => {
|
OpCode::SelectDynamicOrDefault => {
|
||||||
let default = stack.pop();
|
let default = stack.pop();
|
||||||
let mut val = stack.pop();
|
let mut val = stack.pop();
|
||||||
val.force(vm, mc)?;
|
|
||||||
val.coerce_to_string();
|
val.coerce_to_string();
|
||||||
let sym = vm.new_sym(val.unwrap_const().unwrap_string());
|
let sym = vm.new_sym(val.unwrap_string().as_ref());
|
||||||
stack
|
stack.tos_mut().select_with_default(sym, default)?;
|
||||||
.tos_mut()
|
|
||||||
.force(vm, mc)?
|
|
||||||
.select_with_default(sym, default)?;
|
|
||||||
}
|
}
|
||||||
OpCode::HasAttr { sym } => {
|
OpCode::HasAttr { sym } => {
|
||||||
stack.tos_mut().force(vm, mc)?.has_attr(sym);
|
stack.tos_mut().has_attr(sym);
|
||||||
}
|
}
|
||||||
OpCode::HasDynamicAttr => {
|
OpCode::HasDynamicAttr => {
|
||||||
let mut val = stack.pop();
|
let mut val = stack.pop();
|
||||||
val.coerce_to_string();
|
val.coerce_to_string();
|
||||||
let sym = vm.new_sym(val.unwrap_const().unwrap_string());
|
let sym = vm.new_sym(val.unwrap_string().as_ref());
|
||||||
stack.tos_mut().force(vm, mc)?.has_attr(sym);
|
stack.tos_mut().has_attr(sym);
|
||||||
}
|
}
|
||||||
OpCode::LookUp { sym } => {
|
OpCode::LookUp { sym } => {
|
||||||
stack.push(
|
stack.push(
|
||||||
@@ -251,13 +328,12 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
stack
|
stack
|
||||||
.pop()
|
.pop()
|
||||||
.unwrap_attr_set()
|
.unwrap_attr_set()
|
||||||
.as_inner()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(&k, v)| (k, v.clone()))
|
.map(|(&k, v)| (k, v.clone()))
|
||||||
.collect_into(&mut new);
|
.collect_into(&mut new);
|
||||||
*env = env.enter_let(Gc::new(mc, new.into()), mc);
|
*env = env.enter_let(Gc::new(mc, new.into()), mc);
|
||||||
}
|
}
|
||||||
OpCode::LeaveLetEnv => *env = env.leave(),
|
OpCode::LeaveEnv => *env = env.leave(),
|
||||||
OpCode::EnterWithEnv => {
|
OpCode::EnterWithEnv => {
|
||||||
let mut new = HashMap::new();
|
let mut new = HashMap::new();
|
||||||
stack
|
stack
|
||||||
@@ -267,40 +343,26 @@ fn single_op<'gc, const CAP: usize>(
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|(&k, v)| (k, v.clone()))
|
.map(|(&k, v)| (k, v.clone()))
|
||||||
.collect_into(&mut new);
|
.collect_into(&mut new);
|
||||||
*env = env.enter_with(Gc::new(mc, new.into()), mc);
|
*env = env.enter_with(Gc::new(mc, new), mc);
|
||||||
}
|
}
|
||||||
OpCode::LeaveWithEnv => *env = env.leave(),
|
OpCode::PopEnv => return Ok(Consq::PopEnv),
|
||||||
OpCode::Assert => {
|
OpCode::Assert => {
|
||||||
if !stack.pop().unwrap_const().unwrap_bool() {
|
if !stack.pop().unwrap_bool() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(0)
|
Ok(Consq::NoOp)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Collect)]
|
#[derive(Collect)]
|
||||||
#[collect(no_drop)]
|
#[collect(no_drop)]
|
||||||
pub struct GcRoot<'gc> {
|
pub struct GcRoot<'gc, const CAP: usize = STACK_SIZE> {
|
||||||
vm: Gc<'gc, VM<'gc>>,
|
vm: Gc<'gc, VM<'gc>>,
|
||||||
jit: Gc<'gc, ContextWrapper>,
|
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>,
|
stack: Stack<Value<'gc>, CAP>,
|
||||||
env: Gc<'gc, VmEnv<'gc>>
|
envs: Vec<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)]
|
#[derive(Constructor, Collect)]
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ extern crate test;
|
|||||||
|
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
use inkwell::context::Context;
|
|
||||||
use test::{Bencher, black_box};
|
use test::{Bencher, black_box};
|
||||||
|
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
@@ -14,7 +13,6 @@ use crate::compile::compile;
|
|||||||
use crate::ir::downgrade;
|
use crate::ir::downgrade;
|
||||||
use crate::ty::common::Const;
|
use crate::ty::common::Const;
|
||||||
use crate::ty::public::*;
|
use crate::ty::public::*;
|
||||||
use crate::vm::JITContext;
|
|
||||||
|
|
||||||
use super::run;
|
use super::run;
|
||||||
|
|
||||||
@@ -23,9 +21,7 @@ fn test_expr(expr: &str, expected: Value) {
|
|||||||
let downgraded = downgrade(rnix::Root::parse(expr).tree().expr().unwrap()).unwrap();
|
let downgraded = downgrade(rnix::Root::parse(expr).tree().expr().unwrap()).unwrap();
|
||||||
let prog = compile(downgraded);
|
let prog = compile(downgraded);
|
||||||
dbg!(&prog);
|
dbg!(&prog);
|
||||||
let ctx = Context::create();
|
assert_eq!(run(prog).unwrap(), expected);
|
||||||
let jit = JITContext::new(&ctx);
|
|
||||||
assert_eq!(run(prog, jit).unwrap(), expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! map {
|
macro_rules! map {
|
||||||
|
|||||||
Reference in New Issue
Block a user