implement primop (filter)
This commit is contained in:
@@ -139,8 +139,4 @@ impl<'a> BytecodeReader<'a> {
|
||||
pub(crate) fn inst_start_pc(&self) -> usize {
|
||||
self.inst_start_pc
|
||||
}
|
||||
|
||||
pub(crate) fn bytecode(&self) -> &'a [u8] {
|
||||
self.bytecode
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ tail_fn!(op_make_closure, (reader, mc));
|
||||
tail_fn!(op_make_pattern_closure, (reader, mc));
|
||||
|
||||
tail_fn!(op_call, (ctx, reader, mc));
|
||||
tail_fn!(op_dispatch_primop, (ctx, reader, mc));
|
||||
tail_fn!(op_return, (ctx, reader, mc));
|
||||
|
||||
tail_fn!(op_make_attrs, (ctx, reader, mc));
|
||||
@@ -234,6 +235,7 @@ table! {
|
||||
MakePatternClosure => op_make_pattern_closure,
|
||||
|
||||
Call => op_call,
|
||||
DispatchPrimOp => op_dispatch_primop,
|
||||
Return => op_return,
|
||||
|
||||
MakeAttrs => op_make_attrs,
|
||||
|
||||
+29
-14
@@ -16,6 +16,7 @@ pub(crate) trait Forced<'gc>: Sized {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step;
|
||||
|
||||
/// After `force_and_check` returned `Continue`, pop `WIDTH` slots
|
||||
@@ -33,8 +34,9 @@ impl<'gc> Forced<'gc> for StrictValue<'gc> {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
vm.force_slot(base_depth, reader, mc)
|
||||
vm.force_slot_to_pc(base_depth, reader, mc, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -55,8 +57,9 @@ macro_rules! impl_forced_inline {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
vm.force_slot(base_depth, reader, mc)?;
|
||||
vm.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = vm.peek_forced(base_depth);
|
||||
if v.as_inline::<$ty>().is_none() {
|
||||
let _: Step = vm.finish_type_err($nix_ty, v.ty());
|
||||
@@ -88,8 +91,9 @@ macro_rules! impl_forced_gc {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
vm.force_slot(base_depth, reader, mc)?;
|
||||
vm.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = vm.peek_forced(base_depth);
|
||||
if v.as_gc::<$ty>().is_none() {
|
||||
let _: Step = vm.finish_type_err($nix_ty, v.ty());
|
||||
@@ -135,8 +139,9 @@ impl<'gc> Forced<'gc> for NixNum {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
vm.force_slot(base_depth, reader, mc)?;
|
||||
vm.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = vm.peek_forced(base_depth);
|
||||
if v.as_num().is_none() {
|
||||
let _: Step = vm.finish_type_err(NixType::Int, v.ty());
|
||||
@@ -162,8 +167,9 @@ impl<'gc> Forced<'gc> for f64 {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
vm.force_slot(base_depth, reader, mc)?;
|
||||
vm.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = vm.peek_forced(base_depth);
|
||||
if v.as_float().is_none() {
|
||||
let _: Step = vm.finish_type_err(NixType::Float, v.ty());
|
||||
@@ -189,9 +195,10 @@ impl<'gc, A: Forced<'gc>, B: Forced<'gc>> Forced<'gc> for (A, B) {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(vm, reader, mc, base + B::WIDTH)?;
|
||||
B::force_and_check(vm, reader, mc, base)
|
||||
A::force_and_check(vm, reader, mc, base + B::WIDTH, resume_pc)?;
|
||||
B::force_and_check(vm, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -211,10 +218,11 @@ impl<'gc, A: Forced<'gc>, B: Forced<'gc>, C: Forced<'gc>> Forced<'gc> for (A, B,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(vm, reader, mc, base + B::WIDTH + C::WIDTH)?;
|
||||
B::force_and_check(vm, reader, mc, base + C::WIDTH)?;
|
||||
C::force_and_check(vm, reader, mc, base)
|
||||
A::force_and_check(vm, reader, mc, base + B::WIDTH + C::WIDTH, resume_pc)?;
|
||||
B::force_and_check(vm, reader, mc, base + C::WIDTH, resume_pc)?;
|
||||
C::force_and_check(vm, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -237,11 +245,18 @@ impl<'gc, A: Forced<'gc>, B: Forced<'gc>, C: Forced<'gc>, D: Forced<'gc>> Forced
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(vm, reader, mc, base + B::WIDTH + C::WIDTH + D::WIDTH)?;
|
||||
B::force_and_check(vm, reader, mc, base + C::WIDTH + D::WIDTH)?;
|
||||
C::force_and_check(vm, reader, mc, base + D::WIDTH)?;
|
||||
D::force_and_check(vm, reader, mc, base)
|
||||
A::force_and_check(
|
||||
vm,
|
||||
reader,
|
||||
mc,
|
||||
base + B::WIDTH + C::WIDTH + D::WIDTH,
|
||||
resume_pc,
|
||||
)?;
|
||||
B::force_and_check(vm, reader, mc, base + C::WIDTH + D::WIDTH, resume_pc)?;
|
||||
C::force_and_check(vm, reader, mc, base + D::WIDTH, resume_pc)?;
|
||||
D::force_and_check(vm, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use gc_arena::{Gc, Mutation};
|
||||
use gc_arena::{Gc, Mutation, RefLock};
|
||||
|
||||
use crate::value::*;
|
||||
use crate::{BytecodeReader, NixNum, Step, VmError, VmRuntimeCtx, VmRuntimeCtxExt as _};
|
||||
@@ -173,9 +173,14 @@ impl<'gc> crate::Vm<'gc> {
|
||||
) -> Step {
|
||||
let (l, r) = self.try_force::<(Gc<List>, Gc<List>)>(reader, mc)?;
|
||||
let mut items = smallvec::SmallVec::new();
|
||||
items.extend_from_slice(&l);
|
||||
items.extend_from_slice(&r);
|
||||
self.push(Value::new_gc(Gc::new(mc, crate::List { inner: items })));
|
||||
items.extend_from_slice(&l.inner.borrow());
|
||||
items.extend_from_slice(&r.inner.borrow());
|
||||
self.push(Value::new_gc(Gc::new(
|
||||
mc,
|
||||
crate::List {
|
||||
inner: RefLock::new(items),
|
||||
},
|
||||
)));
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
@@ -224,10 +229,10 @@ impl<'gc> crate::Vm<'gc> {
|
||||
return Ok(a == b);
|
||||
}
|
||||
if let (Some(a), Some(b)) = (lhs.as_gc::<crate::List>(), rhs.as_gc::<crate::List>()) {
|
||||
if a.inner.len() != b.inner.len() {
|
||||
if a.inner.borrow().len() != b.inner.borrow().len() {
|
||||
return Ok(false);
|
||||
}
|
||||
for (x, y) in a.inner.iter().zip(b.inner.iter()) {
|
||||
for (x, y) in a.inner.borrow().iter().zip(b.inner.borrow().iter()) {
|
||||
let lx = x.restrict().expect("forced");
|
||||
let ly = y.restrict().expect("forced");
|
||||
if !self.values_equal(ctx, lx, ly)? {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
use fix_builtins::PrimOpPhase;
|
||||
use fix_error::Error;
|
||||
use gc_arena::Mutation;
|
||||
use num_enum::TryFromPrimitive as _;
|
||||
|
||||
use crate::value::*;
|
||||
use crate::{BytecodeReader, CallFrame, Step, Vm, VmRuntimeCtx};
|
||||
|
||||
impl<'gc> Vm<'gc> {
|
||||
pub(crate) fn op_dispatch_primop(
|
||||
&mut self,
|
||||
_ctx: &mut impl VmRuntimeCtx,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
use PrimOpPhase::*;
|
||||
let phase_disc = reader.read_u8();
|
||||
let Ok(phase) = PrimOpPhase::try_from_primitive(phase_disc) else {
|
||||
return self.finish_err(Error::eval_error("invalid primop phase"));
|
||||
};
|
||||
match phase {
|
||||
FilterForceList => self.primop_filter_force_list(reader, mc),
|
||||
FilterCallPred => self.primop_filter_call_pred(reader, mc),
|
||||
FilterCheck => self.primop_filter_check(reader, mc),
|
||||
_ => todo!("dispatch builtin phase"),
|
||||
}
|
||||
}
|
||||
|
||||
fn return_from_primop(&mut self, reader: &mut BytecodeReader<'_>) -> Step {
|
||||
let Some(CallFrame {
|
||||
pc: ret_pc,
|
||||
stack_depth: _,
|
||||
thunk: _,
|
||||
env,
|
||||
with_env,
|
||||
}) = self.call_stack.pop()
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
reader.set_pc(ret_pc);
|
||||
self.call_depth -= 1;
|
||||
self.env = env;
|
||||
self.with_env = with_env;
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
pub(crate) fn primop_filter_force_list(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
self.force_slot(0, reader, mc)?;
|
||||
let list = match self.peek_forced(0).expect_gc::<List>() {
|
||||
Ok(list) => list,
|
||||
Err(got) => return self.finish_type_err(NixType::List, got),
|
||||
};
|
||||
if list.inner.borrow().is_empty() {
|
||||
return self.return_from_primop(reader);
|
||||
}
|
||||
// prepare stack layout: [ pred list idx acc ]
|
||||
self.push(Value::new_inline(0));
|
||||
self.push(Value::new_gc(List::new_gc(mc)));
|
||||
reader.set_pc(PrimOpPhase::FilterCallPred.ip() as usize);
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
pub(crate) fn primop_filter_call_pred(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
self.force_slot(3, reader, mc)?;
|
||||
let pred = self.peek_forced(3);
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let idx = self.peek(1).as_inline::<i32>().unwrap();
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let elem = self.peek_forced(2).as_gc::<List>().unwrap().inner.borrow()[idx as usize];
|
||||
self.push(pred.relax());
|
||||
self.call(reader, mc, elem, PrimOpPhase::FilterCheck.ip() as usize)
|
||||
}
|
||||
|
||||
pub(crate) fn primop_filter_check(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
let ret = self.try_force::<bool>(reader, mc)?;
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let idx = self.peek(1).as_inline::<i32>().unwrap();
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let list = self.peek_forced(2).as_gc::<List>().unwrap();
|
||||
let list = list.inner.borrow();
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let acc = self.peek_forced(0).as_gc::<List>().unwrap();
|
||||
if ret {
|
||||
let mut acc = acc.unlock(mc).borrow_mut();
|
||||
acc.push(list[idx as usize]);
|
||||
}
|
||||
if idx as usize == list.len() - 1 {
|
||||
let acc = self.pop();
|
||||
let _ = self.pop(); // idx
|
||||
let _ = self.pop(); // list
|
||||
let _ = self.pop(); // pred
|
||||
self.push(acc);
|
||||
return self.return_from_primop(reader);
|
||||
}
|
||||
self.replace(1, Value::new_inline(idx + 1));
|
||||
reader.set_pc(PrimOpPhase::FilterCallPred.ip() as usize);
|
||||
Step::Continue(())
|
||||
}
|
||||
}
|
||||
@@ -8,18 +8,18 @@ use crate::{
|
||||
|
||||
impl<'gc> crate::Vm<'gc> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn op_call(
|
||||
pub(crate) fn call(
|
||||
&mut self,
|
||||
ctx: &mut impl VmRuntimeCtx,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
arg: Value<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
let func = self.try_force::<StrictValue>(reader, mc)?;
|
||||
if self.call_depth > 10000 {
|
||||
return self.finish_err(Error::eval_error("stack overflow; max-call-depth exceeded"));
|
||||
}
|
||||
self.call_depth += 1;
|
||||
let arg = reader.read_operand_data(ctx).resolve(mc, self);
|
||||
if let Some(closure) = func.as_gc::<Closure>() {
|
||||
let ip = closure.ip;
|
||||
let n_locals = closure.n_locals;
|
||||
@@ -29,7 +29,7 @@ impl<'gc> crate::Vm<'gc> {
|
||||
} else {
|
||||
let new_env = Gc::new(mc, RefLock::new(Env::with_arg(arg, n_locals, env)));
|
||||
self.call_stack.push(CallFrame {
|
||||
pc: reader.pc(),
|
||||
pc: resume_pc,
|
||||
stack_depth: 0,
|
||||
thunk: None,
|
||||
env: self.env,
|
||||
@@ -38,12 +38,64 @@ impl<'gc> crate::Vm<'gc> {
|
||||
reader.set_pc(ip as usize);
|
||||
self.env = new_env;
|
||||
}
|
||||
} else if let Some(primop) = func.as_inline::<PrimOp>() {
|
||||
if primop.arity == 1 {
|
||||
self.push(arg);
|
||||
self.call_stack.push(CallFrame {
|
||||
pc: resume_pc,
|
||||
stack_depth: 0,
|
||||
thunk: None,
|
||||
env: self.env,
|
||||
with_env: self.with_env,
|
||||
});
|
||||
reader.set_pc(primop.dispatch_ip as usize)
|
||||
} else {
|
||||
let app = PrimOpApp {
|
||||
primop,
|
||||
arity: primop.arity - 1,
|
||||
args: [arg, Value::default(), Value::default()],
|
||||
};
|
||||
self.push(Value::new_gc(Gc::new(mc, app)));
|
||||
}
|
||||
} else if let Some(app) = func.as_gc::<PrimOpApp>() {
|
||||
if app.arity == 1 {
|
||||
for i in 0..app.primop.arity - 1 {
|
||||
self.push(app.args[i as usize]);
|
||||
}
|
||||
self.push(arg);
|
||||
self.call_stack.push(CallFrame {
|
||||
pc: resume_pc,
|
||||
stack_depth: 0,
|
||||
thunk: None,
|
||||
env: self.env,
|
||||
with_env: self.with_env,
|
||||
});
|
||||
reader.set_pc(app.primop.dispatch_ip as usize)
|
||||
} else {
|
||||
let new_app = PrimOpApp {
|
||||
arity: app.arity - 1,
|
||||
..*app
|
||||
};
|
||||
self.push(Value::new_gc(Gc::new(mc, new_app)))
|
||||
}
|
||||
} else {
|
||||
todo!("call other types: {func:?}")
|
||||
}
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn op_call(
|
||||
&mut self,
|
||||
ctx: &impl VmRuntimeCtx,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
let arg = reader.read_operand_data(ctx).resolve(mc, self);
|
||||
let pc = reader.pc();
|
||||
self.call(reader, mc, arg, pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn op_return(
|
||||
&mut self,
|
||||
@@ -51,16 +103,7 @@ impl<'gc> crate::Vm<'gc> {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
self.handle_return(reader, ctx, mc)
|
||||
}
|
||||
|
||||
pub(crate) fn handle_return<C: VmRuntimeCtx>(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
ctx: &C,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
let ret_inst_pc = reader.pc() - 1;
|
||||
let val = self.try_force::<StrictValue>(reader, mc)?;
|
||||
let Some(CallFrame {
|
||||
pc: ret_pc,
|
||||
stack_depth,
|
||||
@@ -69,66 +112,15 @@ impl<'gc> crate::Vm<'gc> {
|
||||
with_env,
|
||||
}) = self.call_stack.pop()
|
||||
else {
|
||||
let val = self.pop();
|
||||
return self.finish_ok(ctx.convert_value(val));
|
||||
return self.finish_ok(ctx.convert_value(val.relax()));
|
||||
};
|
||||
reader.set_pc(ret_pc);
|
||||
if let Some(outer_thunk) = thunk {
|
||||
let val = self.pop();
|
||||
match val.restrict() {
|
||||
Ok(val) => {
|
||||
*outer_thunk.borrow_mut(mc) = ThunkState::Evaluated(val);
|
||||
if reader.bytecode().get(ret_pc).copied() == Some(fix_codegen::Op::Return as u8)
|
||||
{
|
||||
self.push(val.relax());
|
||||
}
|
||||
}
|
||||
Err(inner_thunk) => {
|
||||
let mut state = inner_thunk.borrow_mut(mc);
|
||||
match *state {
|
||||
ThunkState::Pending {
|
||||
ip: inner_ip,
|
||||
env: inner_env,
|
||||
with_env: inner_with_env,
|
||||
} => {
|
||||
self.call_stack.push(CallFrame {
|
||||
pc: ret_pc,
|
||||
stack_depth,
|
||||
thunk: Some(outer_thunk),
|
||||
env,
|
||||
with_env,
|
||||
});
|
||||
self.call_stack.push(CallFrame {
|
||||
pc: ret_inst_pc,
|
||||
stack_depth: 0,
|
||||
thunk: Some(inner_thunk),
|
||||
env: inner_env,
|
||||
with_env: inner_with_env,
|
||||
});
|
||||
*state = ThunkState::Blackhole;
|
||||
reader.set_pc(inner_ip);
|
||||
self.env = inner_env;
|
||||
self.with_env = inner_with_env;
|
||||
return Step::Continue(());
|
||||
}
|
||||
ThunkState::Evaluated(val) => {
|
||||
*outer_thunk.borrow_mut(mc) = ThunkState::Evaluated(val);
|
||||
if reader.bytecode().get(ret_pc).copied()
|
||||
== Some(fix_codegen::Op::Return as u8)
|
||||
{
|
||||
self.push(val.relax());
|
||||
}
|
||||
}
|
||||
ThunkState::Apply { func: _, arg: _ } => todo!("force Apply thunk"),
|
||||
ThunkState::Blackhole => {
|
||||
return self
|
||||
.finish_err(Error::eval_error("infinite recursion encountered"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*outer_thunk.borrow_mut(mc) = ThunkState::Evaluated(val);
|
||||
self.replace(stack_depth, val.relax());
|
||||
} else {
|
||||
self.call_depth -= 1;
|
||||
self.push(val.relax())
|
||||
}
|
||||
self.env = env;
|
||||
self.with_env = with_env;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use fix_common::StringId;
|
||||
use fix_error::Error;
|
||||
use gc_arena::Gc;
|
||||
use gc_arena::{Gc, RefLock};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use crate::value::NixType;
|
||||
@@ -298,7 +298,12 @@ impl<'gc> crate::Vm<'gc> {
|
||||
for _ in 0..count {
|
||||
items.push(reader.read_operand_data(ctx).resolve(mc, self));
|
||||
}
|
||||
let list = Gc::new(mc, List { inner: items });
|
||||
let list = Gc::new(
|
||||
mc,
|
||||
List {
|
||||
inner: RefLock::new(items),
|
||||
},
|
||||
);
|
||||
self.push(Value::new_gc(list));
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ impl<'gc> crate::Vm<'gc> {
|
||||
self.push(Value::new_inline(PrimOp {
|
||||
id,
|
||||
arity: fix_builtins::BUILTINS[id as usize].1,
|
||||
dispatch_ip: id.entry_phase().ip(),
|
||||
}));
|
||||
Step::Continue(())
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
pub(crate) mod arithmetic;
|
||||
pub(crate) mod builtins_misc;
|
||||
pub(crate) mod builtins;
|
||||
pub(crate) mod calls;
|
||||
pub(crate) mod closures;
|
||||
pub(crate) mod collections;
|
||||
pub(crate) mod control;
|
||||
pub(crate) mod literals;
|
||||
pub(crate) mod misc;
|
||||
pub(crate) mod variables;
|
||||
pub(crate) mod with_scope;
|
||||
|
||||
+42
-16
@@ -142,6 +142,7 @@ impl<T: VmRuntimeCtx> VmRuntimeCtxExt for T {
|
||||
} else if let Some(list) = val.as_gc::<List>() {
|
||||
let items: Vec<_> = list
|
||||
.inner
|
||||
.borrow()
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|v| self.convert_value(v))
|
||||
@@ -151,10 +152,12 @@ impl<T: VmRuntimeCtx> VmRuntimeCtxExt for T {
|
||||
Value::Func
|
||||
} else if val.is::<Thunk>() {
|
||||
Value::Thunk
|
||||
} else if val.as_inline::<PrimOp>().is_some() {
|
||||
Value::PrimOp("primop".into())
|
||||
} else if val.is::<PrimOpApp>() {
|
||||
Value::PrimOpApp("primop-app".into())
|
||||
} else if let Some(primop) = val.as_inline::<PrimOp>() {
|
||||
let name = fix_builtins::BUILTINS[primop.id as usize].0;
|
||||
Value::PrimOp(name.strip_prefix("__").unwrap_or(name))
|
||||
} else if let Some(app) = val.as_gc::<PrimOpApp>() {
|
||||
let name = fix_builtins::BUILTINS[app.primop.id as usize].0;
|
||||
Value::PrimOpApp(name.strip_prefix("__").unwrap_or(name))
|
||||
} else {
|
||||
Value::Null
|
||||
}
|
||||
@@ -231,7 +234,15 @@ fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmRuntimeCtx) -> Value<
|
||||
let id = BuiltinId::try_from_primitive(idx as u8).expect("infallible");
|
||||
let name = name.strip_prefix("__").unwrap_or(name);
|
||||
let name = ctx.intern_string(name);
|
||||
entries.push((name, Value::new_inline(PrimOp { id, arity })));
|
||||
let dispatch_ip = id.entry_phase().ip();
|
||||
entries.push((
|
||||
name,
|
||||
Value::new_inline(PrimOp {
|
||||
id,
|
||||
arity,
|
||||
dispatch_ip,
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
let consts = [
|
||||
@@ -248,15 +259,7 @@ fn init_builtins<'gc>(mc: &Mutation<'gc>, ctx: &mut impl VmRuntimeCtx) -> Value<
|
||||
"__storeDir",
|
||||
Value::new_inline(ctx.intern_string("/nix/store")),
|
||||
),
|
||||
(
|
||||
"__nixPath",
|
||||
Value::new_gc(Gc::new(
|
||||
mc,
|
||||
List {
|
||||
inner: SmallVec::new(),
|
||||
},
|
||||
)),
|
||||
),
|
||||
("__nixPath", Value::new_gc(Gc::new(mc, List::default()))),
|
||||
("null", Value::new_inline(Null)),
|
||||
("true", Value::new_inline(true)),
|
||||
("false", Value::new_inline(false)),
|
||||
@@ -382,16 +385,38 @@ impl<'gc> Vm<'gc> {
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> std::ops::ControlFlow<Break, T> {
|
||||
T::force_and_check(self, reader, mc, 0)?;
|
||||
self.try_force_to_pc(reader, mc, reader.inst_start_pc())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_force_to_pc<T: Forced<'gc>>(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> std::ops::ControlFlow<Break, T> {
|
||||
T::force_and_check(self, reader, mc, 0, resume_pc)?;
|
||||
std::ops::ControlFlow::Continue(T::pop_converted(self))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[allow(unused)]
|
||||
pub(crate) fn force_slot(
|
||||
&mut self,
|
||||
depth: usize,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
self.force_slot_to_pc(depth, reader, mc, reader.inst_start_pc())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn force_slot_to_pc(
|
||||
&mut self,
|
||||
depth: usize,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
let Some(thunk) = self.peek(depth).as_gc::<Thunk>() else {
|
||||
return Step::Continue(());
|
||||
@@ -404,7 +429,7 @@ impl<'gc> Vm<'gc> {
|
||||
self.call_stack.push(CallFrame {
|
||||
thunk: Some(thunk),
|
||||
stack_depth: depth,
|
||||
pc: reader.inst_start_pc(),
|
||||
pc: resume_pc,
|
||||
env: self.env,
|
||||
with_env: self.with_env,
|
||||
});
|
||||
@@ -554,6 +579,7 @@ impl<'gc> Vm<'gc> {
|
||||
MakePatternClosure => self.op_make_pattern_closure(&mut reader, mc),
|
||||
|
||||
Call => self.op_call(ctx, &mut reader, mc),
|
||||
DispatchPrimOp => self.op_dispatch_primop(ctx, &mut reader, mc),
|
||||
Return => self.op_return(ctx, &mut reader, mc),
|
||||
|
||||
MakeAttrs => self.op_make_attrs(ctx, &mut reader, mc),
|
||||
|
||||
+36
-10
@@ -1,5 +1,6 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
@@ -7,6 +8,7 @@ use std::ops::Deref;
|
||||
|
||||
use fix_builtins::BuiltinId;
|
||||
use fix_common::*;
|
||||
use gc_arena::barrier::Unlock;
|
||||
use gc_arena::collect::Trace;
|
||||
use gc_arena::{Collect, Gc, GcRefLock, Mutation, RefLock};
|
||||
use num_enum::TryFromPrimitive;
|
||||
@@ -345,8 +347,12 @@ impl StaticValue {
|
||||
Self(Value::new_inline(val))
|
||||
}
|
||||
#[inline]
|
||||
pub fn new_primop(id: BuiltinId, arity: u8) -> Self {
|
||||
Self(Value::new_inline(PrimOp { id, arity }))
|
||||
pub fn new_primop(id: BuiltinId, arity: u8, dispatch_ip: u32) -> Self {
|
||||
Self(Value::new_inline(PrimOp {
|
||||
id,
|
||||
arity,
|
||||
dispatch_ip,
|
||||
}))
|
||||
}
|
||||
#[inline]
|
||||
pub fn is_float(self) -> bool {
|
||||
@@ -487,14 +493,22 @@ impl<'gc> AttrSet<'gc> {
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug, Default)]
|
||||
#[repr(transparent)]
|
||||
#[collect(no_drop)]
|
||||
pub(crate) struct List<'gc> {
|
||||
pub(crate) inner: SmallVec<[Value<'gc>; 4]>,
|
||||
pub(crate) inner: RefLock<SmallVec<[Value<'gc>; 4]>>,
|
||||
}
|
||||
impl<'gc> Deref for List<'gc> {
|
||||
type Target = SmallVec<[Value<'gc>; 4]>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
|
||||
impl<'gc> List<'gc> {
|
||||
pub(crate) fn new_gc(mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||
Gc::new(mc, Self::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Unlock for List<'gc> {
|
||||
type Unlocked = RefCell<SmallVec<[Value<'gc>; 4]>>;
|
||||
unsafe fn unlock_unchecked(&self) -> &Self::Unlocked {
|
||||
unsafe { self.inner.unlock_unchecked() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -572,22 +586,33 @@ pub(crate) struct PatternInfo {
|
||||
pub(crate) param_spans: Box<[(StringId, u32)]>,
|
||||
}
|
||||
|
||||
#[repr(packed, Rust)]
|
||||
#[derive(Clone, Copy, Debug, Collect)]
|
||||
#[collect(require_static)]
|
||||
pub(crate) struct PrimOp {
|
||||
pub(crate) id: BuiltinId,
|
||||
pub(crate) arity: u8,
|
||||
pub(crate) dispatch_ip: u32,
|
||||
}
|
||||
|
||||
impl RawStore for PrimOp {
|
||||
fn to_val(self, value: &mut RawValue) {
|
||||
value.set_data([0, 0, 0, 0, self.id as u8, self.arity]);
|
||||
let bytes = self.dispatch_ip.to_le_bytes();
|
||||
value.set_data([
|
||||
self.id as u8,
|
||||
self.arity,
|
||||
bytes[0],
|
||||
bytes[1],
|
||||
bytes[2],
|
||||
bytes[3],
|
||||
]);
|
||||
}
|
||||
fn from_val(value: &RawValue) -> Self {
|
||||
let [.., id, arity] = *value.data();
|
||||
let [id, arity, bytes @ ..] = *value.data();
|
||||
Self {
|
||||
id: BuiltinId::try_from_primitive(id).expect("invalid BuiltinId"),
|
||||
arity,
|
||||
dispatch_ip: u32::from_le_bytes(bytes),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -596,7 +621,8 @@ impl RawStore for PrimOp {
|
||||
#[collect(no_drop)]
|
||||
pub(crate) struct PrimOpApp<'gc> {
|
||||
pub(crate) primop: PrimOp,
|
||||
pub(crate) args: SmallVec<[Value<'gc>; 2]>,
|
||||
pub(crate) arity: u8,
|
||||
pub(crate) args: [Value<'gc>; 3],
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Collect)]
|
||||
|
||||
Reference in New Issue
Block a user