refactor: abstract VM
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "fix-abstract-vm"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
gc-arena = { workspace = true }
|
||||
hashbrown = { workspace = true }
|
||||
num_enum = { workspace = true }
|
||||
smallvec = { workspace = true }
|
||||
string-interner = { workspace = true }
|
||||
likely_stable = { workspace = true }
|
||||
sptr = "0.3"
|
||||
|
||||
fix-builtins = { path = "../fix-builtins" }
|
||||
fix-codegen = { path = "../fix-codegen" }
|
||||
fix-common = { path = "../fix-common" }
|
||||
fix-error = { path = "../fix-error" }
|
||||
@@ -0,0 +1,502 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::fmt;
|
||||
use std::num::NonZeroU8;
|
||||
|
||||
use sptr::Strict;
|
||||
|
||||
const SIGN_MASK: u64 = 0x7FFF_FFFF_FFFF_FFFF;
|
||||
const QUIET_NAN: u64 = 0x7FF8_0000_0000_0000;
|
||||
const NEG_QUIET_NAN: u64 = 0xFFF8_0000_0000_0000;
|
||||
|
||||
pub(crate) trait ArrayExt<const LEN: usize> {
|
||||
type Elem;
|
||||
fn truncate_to<const M: usize>(self) -> [Self::Elem; M];
|
||||
}
|
||||
|
||||
impl<T: Default + Copy, const N: usize> ArrayExt<N> for [T; N] {
|
||||
type Elem = T;
|
||||
fn truncate_to<const M: usize>(self) -> [Self::Elem; M] {
|
||||
let copy_len = usize::min(N, M);
|
||||
let mut out = [T::default(); M];
|
||||
out[0..copy_len].copy_from_slice(&self[0..copy_len]);
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RawStore: Sized {
|
||||
fn to_val(self, value: &mut Value);
|
||||
fn from_val(value: &Value) -> Self;
|
||||
}
|
||||
|
||||
impl RawStore for [u8; 6] {
|
||||
#[inline]
|
||||
fn to_val(self, value: &mut Value) {
|
||||
value.set_data(self);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_val(value: &Value) -> Self {
|
||||
*value.data()
|
||||
}
|
||||
}
|
||||
|
||||
impl RawStore for bool {
|
||||
#[inline]
|
||||
fn to_val(self, value: &mut Value) {
|
||||
value.set_data([u8::from(self)].truncate_to());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_val(value: &Value) -> Self {
|
||||
value.data()[0] == 1
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! int_store {
|
||||
($ty:ty) => {
|
||||
impl RawStore for $ty {
|
||||
#[inline]
|
||||
fn to_val(self, value: &mut Value) {
|
||||
let bytes = self.to_ne_bytes();
|
||||
value.set_data(bytes.truncate_to());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_val(value: &Value) -> Self {
|
||||
<$ty>::from_ne_bytes(value.data().truncate_to())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int_store!(u8);
|
||||
int_store!(u16);
|
||||
int_store!(u32);
|
||||
|
||||
int_store!(i8);
|
||||
int_store!(i16);
|
||||
int_store!(i32);
|
||||
|
||||
fn store_ptr<P: Strict + Copy>(value: &mut Value, ptr: P) {
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
{
|
||||
assert!(
|
||||
ptr.addr() <= 0x0000_FFFF_FFFF_FFFF,
|
||||
"Pointer too large to store in NaN box"
|
||||
);
|
||||
|
||||
let val = (unsafe { value.whole_mut() } as *mut [u8; 8]).cast::<P>();
|
||||
|
||||
let ptr = Strict::map_addr(ptr, |addr| {
|
||||
addr | (usize::from(value.header().into_raw()) << 48)
|
||||
});
|
||||
|
||||
unsafe { val.write(ptr) };
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
{
|
||||
let _ = (value, ptr);
|
||||
unimplemented!("32-bit pointer storage not supported");
|
||||
}
|
||||
}
|
||||
|
||||
fn load_ptr<P: Strict>(value: &Value) -> P {
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
{
|
||||
let val = (unsafe { value.whole() } as *const [u8; 8]).cast::<P>();
|
||||
let ptr = unsafe { val.read() };
|
||||
Strict::map_addr(ptr, |addr| addr & 0x0000_FFFF_FFFF_FFFF)
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
{
|
||||
let _ = value;
|
||||
unimplemented!("32-bit pointer storage not supported");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RawStore for *const T {
|
||||
fn to_val(self, value: &mut Value) {
|
||||
store_ptr::<*const T>(value, self);
|
||||
}
|
||||
|
||||
fn from_val(value: &Value) -> Self {
|
||||
load_ptr::<*const T>(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> RawStore for *mut T {
|
||||
fn to_val(self, value: &mut Value) {
|
||||
store_ptr::<*mut T>(value, self);
|
||||
}
|
||||
|
||||
fn from_val(value: &Value) -> Self {
|
||||
load_ptr::<*mut T>(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
enum TagVal {
|
||||
_P1,
|
||||
_P2,
|
||||
_P3,
|
||||
_P4,
|
||||
_P5,
|
||||
_P6,
|
||||
_P7,
|
||||
|
||||
_N1,
|
||||
_N2,
|
||||
_N3,
|
||||
_N4,
|
||||
_N5,
|
||||
_N6,
|
||||
_N7,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub struct RawTag(TagVal);
|
||||
|
||||
impl RawTag {
|
||||
pub const P1: RawTag = RawTag(TagVal::_P1);
|
||||
pub const P2: RawTag = RawTag(TagVal::_P2);
|
||||
pub const P3: RawTag = RawTag(TagVal::_P3);
|
||||
pub const P4: RawTag = RawTag(TagVal::_P4);
|
||||
pub const P5: RawTag = RawTag(TagVal::_P5);
|
||||
pub const P6: RawTag = RawTag(TagVal::_P6);
|
||||
pub const P7: RawTag = RawTag(TagVal::_P7);
|
||||
|
||||
pub const N1: RawTag = RawTag(TagVal::_N1);
|
||||
pub const N2: RawTag = RawTag(TagVal::_N2);
|
||||
pub const N3: RawTag = RawTag(TagVal::_N3);
|
||||
pub const N4: RawTag = RawTag(TagVal::_N4);
|
||||
pub const N5: RawTag = RawTag(TagVal::_N5);
|
||||
pub const N6: RawTag = RawTag(TagVal::_N6);
|
||||
pub const N7: RawTag = RawTag(TagVal::_N7);
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn new(neg: bool, val: NonZeroU8) -> RawTag {
|
||||
unsafe { Self::new_unchecked(neg, val.get() & 0x07) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn new_checked(neg: bool, val: u8) -> Option<RawTag> {
|
||||
Some(RawTag(match (neg, val) {
|
||||
(false, 1) => TagVal::_P1,
|
||||
(false, 2) => TagVal::_P2,
|
||||
(false, 3) => TagVal::_P3,
|
||||
(false, 4) => TagVal::_P4,
|
||||
(false, 5) => TagVal::_P5,
|
||||
(false, 6) => TagVal::_P6,
|
||||
(false, 7) => TagVal::_P7,
|
||||
|
||||
(true, 1) => TagVal::_N1,
|
||||
(true, 2) => TagVal::_N2,
|
||||
(true, 3) => TagVal::_N3,
|
||||
(true, 4) => TagVal::_N4,
|
||||
(true, 5) => TagVal::_N5,
|
||||
(true, 6) => TagVal::_N6,
|
||||
(true, 7) => TagVal::_N7,
|
||||
|
||||
_ => return None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// `val` must be in the range `1..8`
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) const unsafe fn new_unchecked(neg: bool, val: u8) -> RawTag {
|
||||
RawTag(match (neg, val) {
|
||||
(false, 1) => TagVal::_P1,
|
||||
(false, 2) => TagVal::_P2,
|
||||
(false, 3) => TagVal::_P3,
|
||||
(false, 4) => TagVal::_P4,
|
||||
(false, 5) => TagVal::_P5,
|
||||
(false, 6) => TagVal::_P6,
|
||||
(false, 7) => TagVal::_P7,
|
||||
|
||||
(true, 1) => TagVal::_N1,
|
||||
(true, 2) => TagVal::_N2,
|
||||
(true, 3) => TagVal::_N3,
|
||||
(true, 4) => TagVal::_N4,
|
||||
(true, 5) => TagVal::_N5,
|
||||
(true, 6) => TagVal::_N6,
|
||||
(true, 7) => TagVal::_N7,
|
||||
|
||||
_ => unsafe { core::hint::unreachable_unchecked() },
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn is_neg(self) -> bool {
|
||||
matches!(self.0, |TagVal::_N1| TagVal::_N2
|
||||
| TagVal::_N3
|
||||
| TagVal::_N4
|
||||
| TagVal::_N5
|
||||
| TagVal::_N6
|
||||
| TagVal::_N7)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn val(self) -> NonZeroU8 {
|
||||
match self.0 {
|
||||
TagVal::_P1 | TagVal::_N1 => NonZeroU8::MIN,
|
||||
TagVal::_P2 | TagVal::_N2 => NonZeroU8::MIN.saturating_add(1),
|
||||
TagVal::_P3 | TagVal::_N3 => NonZeroU8::MIN.saturating_add(2),
|
||||
TagVal::_P4 | TagVal::_N4 => NonZeroU8::MIN.saturating_add(3),
|
||||
TagVal::_P5 | TagVal::_N5 => NonZeroU8::MIN.saturating_add(4),
|
||||
TagVal::_P6 | TagVal::_N6 => NonZeroU8::MIN.saturating_add(5),
|
||||
TagVal::_P7 | TagVal::_N7 => NonZeroU8::MIN.saturating_add(6),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn neg_val(self) -> (bool, u8) {
|
||||
match self.0 {
|
||||
TagVal::_P1 => (false, 1),
|
||||
TagVal::_P2 => (false, 2),
|
||||
TagVal::_P3 => (false, 3),
|
||||
TagVal::_P4 => (false, 4),
|
||||
TagVal::_P5 => (false, 5),
|
||||
TagVal::_P6 => (false, 6),
|
||||
TagVal::_P7 => (false, 7),
|
||||
TagVal::_N1 => (true, 1),
|
||||
TagVal::_N2 => (true, 2),
|
||||
TagVal::_N3 => (true, 3),
|
||||
TagVal::_N4 => (true, 4),
|
||||
TagVal::_N5 => (true, 5),
|
||||
TagVal::_N6 => (true, 6),
|
||||
TagVal::_N7 => (true, 7),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for RawTag {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("RawTag")
|
||||
.field("neg", &self.is_neg())
|
||||
.field("val", &self.val())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[repr(transparent)]
|
||||
struct Header(u16);
|
||||
|
||||
impl Header {
|
||||
#[inline]
|
||||
fn new(tag: RawTag) -> Header {
|
||||
let (neg, val) = tag.neg_val();
|
||||
Header(0x7FF8 | (u16::from(neg) << 15) | u16::from(val))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn tag(self) -> RawTag {
|
||||
unsafe { RawTag::new_unchecked(self.get_sign(), self.get_tag()) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn get_sign(self) -> bool {
|
||||
self.0 & 0x8000 != 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn get_tag(self) -> u8 {
|
||||
(self.0 & 0x0007) as u8
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn into_raw(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[repr(C, align(8))]
|
||||
pub struct Value {
|
||||
#[cfg(target_endian = "big")]
|
||||
header: Header,
|
||||
data: [u8; 6],
|
||||
#[cfg(target_endian = "little")]
|
||||
header: Header,
|
||||
}
|
||||
|
||||
impl Value {
|
||||
#[inline]
|
||||
pub(crate) fn new(tag: RawTag, data: [u8; 6]) -> Value {
|
||||
Value {
|
||||
header: Header::new(tag),
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn empty(tag: RawTag) -> Value {
|
||||
Value::new(tag, [0; 6])
|
||||
}
|
||||
|
||||
pub(crate) fn store<T: RawStore>(tag: RawTag, val: T) -> Value {
|
||||
let mut v = Value::new(tag, [0; 6]);
|
||||
T::to_val(val, &mut v);
|
||||
v
|
||||
}
|
||||
|
||||
pub(crate) fn load<T: RawStore>(self) -> T {
|
||||
T::from_val(&self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) const fn tag(&self) -> RawTag {
|
||||
self.header.tag()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn header(&self) -> &Header {
|
||||
&self.header
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn set_data(&mut self, val: [u8; 6]) {
|
||||
self.data = val;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn data(&self) -> &[u8; 6] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn data_mut(&mut self) -> &mut [u8; 6] {
|
||||
&mut self.data
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
unsafe fn whole(&self) -> &[u8; 8] {
|
||||
let ptr = (self as *const Value).cast::<[u8; 8]>();
|
||||
unsafe { &*ptr }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
unsafe fn whole_mut(&mut self) -> &mut [u8; 8] {
|
||||
let ptr = (self as *mut Value).cast::<[u8; 8]>();
|
||||
unsafe { &mut *ptr }
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) union RawBox {
|
||||
float: f64,
|
||||
value: Value,
|
||||
bits: u64,
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
ptr: *const (),
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
ptr: (u32, *const ()),
|
||||
}
|
||||
|
||||
impl RawBox {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn from_float(val: f64) -> RawBox {
|
||||
match (val.is_nan(), val.is_sign_positive()) {
|
||||
(true, true) => RawBox {
|
||||
float: f64::from_bits(QUIET_NAN),
|
||||
},
|
||||
(true, false) => RawBox {
|
||||
float: f64::from_bits(NEG_QUIET_NAN),
|
||||
},
|
||||
(false, _) => RawBox { float: val },
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn from_value(value: Value) -> RawBox {
|
||||
RawBox { value }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) const fn tag(&self) -> Option<RawTag> {
|
||||
if self.is_value() {
|
||||
Some(unsafe { self.value.tag() })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn is_float(&self) -> bool {
|
||||
(unsafe { !self.float.is_nan() } || unsafe { self.bits & SIGN_MASK == QUIET_NAN })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) const fn is_value(&self) -> bool {
|
||||
(unsafe { self.float.is_nan() } && unsafe { self.bits & SIGN_MASK != QUIET_NAN })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn float(&self) -> Option<&f64> {
|
||||
if self.is_float() {
|
||||
Some(unsafe { &self.float })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn value(&self) -> Option<&Value> {
|
||||
if self.is_value() {
|
||||
Some(unsafe { &self.value })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn into_float_unchecked(self) -> f64 {
|
||||
unsafe { self.float }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn to_bits(self) -> u64 {
|
||||
unsafe { self.bits }
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for RawBox {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.float() {
|
||||
Some(val) => f.debug_tuple("RawBox::Float").field(val).finish(),
|
||||
None => {
|
||||
let val = self.value().expect("RawBox is neither float nor value");
|
||||
|
||||
f.debug_struct("RawBox::Data")
|
||||
.field("tag", &val.tag())
|
||||
.field("data", val.data())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use fix_codegen::OperandType;
|
||||
use fix_common::StringId;
|
||||
use num_enum::TryFromPrimitive;
|
||||
use string_interner::Symbol as _;
|
||||
|
||||
use crate::{OperandData, VmRuntimeCtx};
|
||||
|
||||
pub struct BytecodeReader<'a> {
|
||||
bytecode: &'a [u8],
|
||||
pc: usize,
|
||||
inst_start_pc: usize,
|
||||
}
|
||||
|
||||
impl<'a> BytecodeReader<'a> {
|
||||
pub fn new(bytecode: &'a [u8], pc: usize) -> Self {
|
||||
Self {
|
||||
bytecode,
|
||||
pc,
|
||||
inst_start_pc: pc,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn from_after_op(bytecode: &'a [u8], inst_start_pc: usize) -> Self {
|
||||
Self {
|
||||
bytecode,
|
||||
pc: inst_start_pc + 1,
|
||||
inst_start_pc,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg_attr(debug_assertions, track_caller)]
|
||||
fn read_array<const N: usize>(&mut self) -> [u8; N] {
|
||||
let ret = self.bytecode[self.pc..self.pc + N]
|
||||
.try_into()
|
||||
.expect("read_array failed");
|
||||
self.pc += N;
|
||||
ret
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_op(&mut self) -> fix_codegen::Op {
|
||||
use fix_codegen::Op;
|
||||
self.inst_start_pc = self.pc;
|
||||
let byte = self.bytecode[self.pc];
|
||||
if !likely_stable::likely((0..Op::Illegal as u8).contains(&byte)) {
|
||||
panic!("unknown opcode: {byte:#04x}")
|
||||
}
|
||||
self.pc += 1;
|
||||
unsafe { std::mem::transmute::<u8, Op>(byte) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_u8(&mut self) -> u8 {
|
||||
let val = self.bytecode[self.pc];
|
||||
self.pc += 1;
|
||||
val
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_u16(&mut self) -> u16 {
|
||||
u16::from_le_bytes(self.read_array())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_u32(&mut self) -> u32 {
|
||||
u32::from_le_bytes(self.read_array())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_i32(&mut self) -> i32 {
|
||||
i32::from_le_bytes(self.read_array())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_i64(&mut self) -> i64 {
|
||||
i64::from_le_bytes(self.read_array())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_f64(&mut self) -> f64 {
|
||||
f64::from_le_bytes(self.read_array())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_string_id(&mut self) -> StringId {
|
||||
let raw = self.read_u32();
|
||||
#[allow(clippy::unwrap_used)]
|
||||
StringId(string_interner::symbol::SymbolU32::try_from_usize(raw as usize).unwrap())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn read_operand_data<C: VmRuntimeCtx>(&mut self, ctx: &C) -> OperandData {
|
||||
let tag = self.read_u8();
|
||||
let Ok(ty) = OperandType::try_from_primitive(tag)
|
||||
.map_err(|err| panic!("unknown operand tag: {:#04x}", err.number));
|
||||
match ty {
|
||||
OperandType::Const => {
|
||||
let id = self.read_u32();
|
||||
OperandData::Const(ctx.get_const(id))
|
||||
}
|
||||
OperandType::BigInt => {
|
||||
let val = self.read_i64();
|
||||
OperandData::BigInt(val)
|
||||
}
|
||||
OperandType::Local => {
|
||||
let layer = self.read_u8();
|
||||
let idx = self.read_u32();
|
||||
OperandData::Local { layer, idx }
|
||||
}
|
||||
OperandType::BuiltinConst => {
|
||||
let id = self.read_string_id();
|
||||
OperandData::BuiltinConst(id)
|
||||
}
|
||||
OperandType::Builtins => OperandData::Builtins,
|
||||
OperandType::ReplBinding => {
|
||||
let id = self.read_string_id();
|
||||
OperandData::ReplBinding(id)
|
||||
}
|
||||
OperandType::ScopedImportBinding => {
|
||||
let slot_id = self.read_u32();
|
||||
let name = self.read_string_id();
|
||||
OperandData::ScopedImportBinding { slot_id, name }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pc(&self) -> usize {
|
||||
self.pc
|
||||
}
|
||||
|
||||
pub fn set_pc(&mut self, pc: usize) {
|
||||
self.pc = pc;
|
||||
}
|
||||
|
||||
pub fn inst_start_pc(&self) -> usize {
|
||||
self.inst_start_pc
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
use fix_common::StringId;
|
||||
use gc_arena::{Gc, Mutation};
|
||||
|
||||
use crate::{
|
||||
AttrSet, Break, BytecodeReader, Closure, List, Machine, NixNum, NixString, NixType, Null,
|
||||
PrimOp, PrimOpApp, Step, StrictValue,
|
||||
};
|
||||
|
||||
pub trait Forced<'gc>: Sized {
|
||||
const WIDTH: usize;
|
||||
|
||||
/// Force and type-check the `WIDTH` slots starting at `base_depth` from
|
||||
/// TOS, deepest-first. If a slot holds a thunk, enter it and return
|
||||
/// `Break::Force`. If a slot holds a value of the wrong type, call
|
||||
/// `finish_type_err` and return `Break::Done`.
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step;
|
||||
|
||||
/// After `force_and_check` returned `Continue`, pop `WIDTH` slots
|
||||
/// (TOS first) and convert. Type assertions are infallible because
|
||||
/// `force_and_check` already validated every slot.
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self;
|
||||
}
|
||||
|
||||
impl<'gc> Forced<'gc> for StrictValue<'gc> {
|
||||
const WIDTH: usize = 1;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
m.force_slot_to_pc(base_depth, reader, mc, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
m.pop_forced()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_forced_inline {
|
||||
($($ty:ty => $nix_ty:expr),* $(,)?) => {
|
||||
$(
|
||||
impl<'gc> Forced<'gc> for $ty {
|
||||
const WIDTH: usize = 1;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
m.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = m.peek_forced(base_depth);
|
||||
if v.as_inline::<$ty>().is_none() {
|
||||
let _: Step = m.finish_type_err($nix_ty, v.ty());
|
||||
return Step::Break(Break::Done);
|
||||
}
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
m.pop_forced()
|
||||
.as_inline::<$ty>()
|
||||
.expect("type checked in force_and_check")
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_forced_gc {
|
||||
($($ty:ty => $nix_ty:expr),* $(,)?) => {
|
||||
$(
|
||||
impl<'gc> Forced<'gc> for Gc<'gc, $ty> {
|
||||
const WIDTH: usize = 1;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
m.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = m.peek_forced(base_depth);
|
||||
if v.as_gc::<$ty>().is_none() {
|
||||
let _: Step = m.finish_type_err($nix_ty, v.ty());
|
||||
return Step::Break(Break::Done);
|
||||
}
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
m.pop_forced()
|
||||
.as_gc::<$ty>()
|
||||
.expect("type checked in force_and_check")
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_forced_inline! {
|
||||
i32 => NixType::Int,
|
||||
bool => NixType::Bool,
|
||||
Null => NixType::Null,
|
||||
StringId => NixType::String,
|
||||
PrimOp => NixType::PrimOp,
|
||||
}
|
||||
|
||||
impl_forced_gc! {
|
||||
i64 => NixType::Int,
|
||||
NixString => NixType::String,
|
||||
AttrSet<'gc> => NixType::AttrSet,
|
||||
List<'gc> => NixType::List,
|
||||
Closure<'gc> => NixType::Closure,
|
||||
PrimOpApp<'gc> => NixType::PrimOpApp,
|
||||
}
|
||||
|
||||
impl<'gc> Forced<'gc> for NixNum {
|
||||
const WIDTH: usize = 1;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
m.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = m.peek_forced(base_depth);
|
||||
if v.as_num().is_none() {
|
||||
let _: Step = m.finish_type_err(NixType::Int, v.ty());
|
||||
return Step::Break(Break::Done);
|
||||
}
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
m.pop_forced()
|
||||
.as_num()
|
||||
.expect("type checked in force_and_check")
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Forced<'gc> for f64 {
|
||||
const WIDTH: usize = 1;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base_depth: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
m.force_slot_to_pc(base_depth, reader, mc, resume_pc)?;
|
||||
let v = m.peek_forced(base_depth);
|
||||
if v.as_float().is_none() {
|
||||
let _: Step = m.finish_type_err(NixType::Float, v.ty());
|
||||
return Step::Break(Break::Done);
|
||||
}
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
m.pop_forced()
|
||||
.as_float()
|
||||
.expect("type checked in force_and_check")
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, A: Forced<'gc>, B: Forced<'gc>> Forced<'gc> for (A, B) {
|
||||
const WIDTH: usize = A::WIDTH + B::WIDTH;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(m, reader, mc, base + B::WIDTH, resume_pc)?;
|
||||
B::force_and_check(m, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
let b = B::pop_converted(m);
|
||||
let a = A::pop_converted(m);
|
||||
(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, A: Forced<'gc>, B: Forced<'gc>, C: Forced<'gc>> Forced<'gc> for (A, B, C) {
|
||||
const WIDTH: usize = A::WIDTH + B::WIDTH + C::WIDTH;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(m, reader, mc, base + B::WIDTH + C::WIDTH, resume_pc)?;
|
||||
B::force_and_check(m, reader, mc, base + C::WIDTH, resume_pc)?;
|
||||
C::force_and_check(m, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
let c = C::pop_converted(m);
|
||||
let b = B::pop_converted(m);
|
||||
let a = A::pop_converted(m);
|
||||
(a, b, c)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, A: Forced<'gc>, B: Forced<'gc>, C: Forced<'gc>, D: Forced<'gc>> Forced<'gc>
|
||||
for (A, B, C, D)
|
||||
{
|
||||
const WIDTH: usize = A::WIDTH + B::WIDTH + C::WIDTH + D::WIDTH;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_and_check<M: Machine<'gc>>(
|
||||
m: &mut M,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
base: usize,
|
||||
resume_pc: usize,
|
||||
) -> Step {
|
||||
A::force_and_check(
|
||||
m,
|
||||
reader,
|
||||
mc,
|
||||
base + B::WIDTH + C::WIDTH + D::WIDTH,
|
||||
resume_pc,
|
||||
)?;
|
||||
B::force_and_check(m, reader, mc, base + C::WIDTH + D::WIDTH, resume_pc)?;
|
||||
C::force_and_check(m, reader, mc, base + D::WIDTH, resume_pc)?;
|
||||
D::force_and_check(m, reader, mc, base, resume_pc)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn pop_converted<M: Machine<'gc>>(m: &mut M) -> Self {
|
||||
let d = D::pop_converted(m);
|
||||
let c = C::pop_converted(m);
|
||||
let b = B::pop_converted(m);
|
||||
let a = A::pop_converted(m);
|
||||
(a, b, c, d)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
use fix_codegen::InstructionPtr;
|
||||
use fix_common::StringId;
|
||||
use fix_error::Source;
|
||||
use hashbrown::HashSet;
|
||||
|
||||
use crate::{
|
||||
AttrSet, Closure, ExtraScope, List, NixString, NixType, Null, Path, PrimOp, PrimOpApp,
|
||||
StaticValue, StrictValue, Thunk, ThunkState, Value,
|
||||
};
|
||||
|
||||
pub trait VmContext {
|
||||
fn split(&mut self) -> (&mut impl VmCode, &mut impl VmRuntimeCtx);
|
||||
}
|
||||
|
||||
pub trait VmRuntimeCtx {
|
||||
fn intern_string(&mut self, s: impl AsRef<str>) -> StringId;
|
||||
fn resolve_string(&self, id: StringId) -> &str;
|
||||
fn get_const(&self, id: u32) -> StaticValue;
|
||||
fn add_const(&mut self, val: StaticValue) -> u32;
|
||||
}
|
||||
|
||||
pub trait VmCode {
|
||||
fn bytecode(&self) -> &[u8];
|
||||
fn compile_with_scope(
|
||||
&mut self,
|
||||
source: Source,
|
||||
extra_scope: Option<ExtraScope>,
|
||||
ctx: &mut impl VmRuntimeCtx,
|
||||
) -> fix_error::Result<InstructionPtr>;
|
||||
}
|
||||
|
||||
pub trait VmRuntimeCtxExt: VmRuntimeCtx {
|
||||
fn get_string<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str>;
|
||||
fn get_string_or_path<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str>;
|
||||
fn get_string_id<'a, 'gc: 'a>(
|
||||
&'a mut self,
|
||||
val: StrictValue<'gc>,
|
||||
) -> std::result::Result<StringId, NixType>;
|
||||
fn convert_value(&self, val: Value) -> fix_common::Value;
|
||||
}
|
||||
|
||||
impl<T: VmRuntimeCtx> VmRuntimeCtxExt for T {
|
||||
fn get_string<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str> {
|
||||
if let Some(sid) = val.as_inline::<StringId>() {
|
||||
Some(self.resolve_string(sid))
|
||||
} else {
|
||||
val.as_gc::<NixString>().map(|ns| ns.as_ref().as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// Like `get_string`, but also accepts `Path` values (returning their
|
||||
/// underlying canonical-path string). Use this in places where Nix
|
||||
/// would coerce a path to a string (string interpolation, file IO
|
||||
/// builtins, etc.).
|
||||
fn get_string_or_path<'a, 'gc: 'a>(&'a self, val: StrictValue<'gc>) -> Option<&'a str> {
|
||||
if let Some(p) = val.as_inline::<Path>() {
|
||||
Some(self.resolve_string(p.0))
|
||||
} else {
|
||||
self.get_string(val)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_string_id<'a, 'gc: 'a>(
|
||||
&'a mut self,
|
||||
val: StrictValue<'gc>,
|
||||
) -> std::result::Result<StringId, NixType> {
|
||||
if let Some(sid) = val.as_inline::<StringId>() {
|
||||
Ok(sid)
|
||||
} else if let Some(s) = val.as_gc::<NixString>().map(|ns| ns.as_ref().as_str()) {
|
||||
Ok(self.intern_string(s))
|
||||
} else {
|
||||
Err(val.ty())
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_value(&self, val: Value) -> fix_common::Value {
|
||||
self.convert_value_with_seen(val, &mut HashSet::new())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait ConvertValueWithSeen: VmRuntimeCtx {
|
||||
fn convert_value_with_seen(&self, val: Value, seen: &mut HashSet<u64>) -> fix_common::Value;
|
||||
}
|
||||
|
||||
impl<T: VmRuntimeCtx> ConvertValueWithSeen for T {
|
||||
fn convert_value_with_seen(&self, val: Value, seen: &mut HashSet<u64>) -> fix_common::Value {
|
||||
use fix_common::Value;
|
||||
if let Some(i) = val.as_inline::<i32>() {
|
||||
Value::Int(i as i64)
|
||||
} else if let Some(gc_i) = val.as_gc::<i64>() {
|
||||
Value::Int(*gc_i)
|
||||
} else if let Some(f) = val.as_float() {
|
||||
Value::Float(f)
|
||||
} else if let Some(b) = val.as_inline::<bool>() {
|
||||
Value::Bool(b)
|
||||
} else if val.is::<Null>() {
|
||||
Value::Null
|
||||
} else if let Some(sid) = val.as_inline::<StringId>() {
|
||||
let s = self.resolve_string(sid).to_owned();
|
||||
Value::String(s)
|
||||
} else if let Some(ns) = val.as_gc::<NixString>() {
|
||||
Value::String(ns.as_str().to_owned())
|
||||
} else if let Some(p) = val.as_inline::<Path>() {
|
||||
Value::Path(self.resolve_string(p.0).to_owned())
|
||||
} else if let Some(attrs) = val.as_gc::<AttrSet>() {
|
||||
let bits = val.to_bits();
|
||||
if attrs.entries.is_empty() {
|
||||
return Value::AttrSet(Default::default());
|
||||
}
|
||||
if !seen.insert(bits) {
|
||||
return Value::Repeated;
|
||||
}
|
||||
let mut map = std::collections::BTreeMap::new();
|
||||
for &(key, val) in attrs.entries.iter() {
|
||||
let key = self.resolve_string(key).to_owned();
|
||||
let converted = self.convert_value_with_seen(val, seen);
|
||||
map.insert(fix_common::Symbol::from(key), converted);
|
||||
}
|
||||
Value::AttrSet(fix_common::AttrSet::new(map))
|
||||
} else if let Some(list) = val.as_gc::<List>() {
|
||||
let bits = val.to_bits();
|
||||
if list.inner.borrow().is_empty() {
|
||||
return Value::List(Default::default());
|
||||
}
|
||||
if !seen.insert(bits) {
|
||||
return Value::Repeated;
|
||||
}
|
||||
let items: Vec<_> = list
|
||||
.inner
|
||||
.borrow()
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|v| self.convert_value_with_seen(v, seen))
|
||||
.collect();
|
||||
Value::List(fix_common::List::new(items))
|
||||
} else if val.is::<Closure>() {
|
||||
Value::Func
|
||||
} else if let Some(thunk) = val.as_gc::<Thunk>() {
|
||||
if let ThunkState::Evaluated(v) = *thunk.borrow() {
|
||||
self.convert_value_with_seen(v.relax(), seen)
|
||||
} else {
|
||||
Value::Thunk
|
||||
}
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
mod boxing;
|
||||
mod bytecode_reader;
|
||||
mod forced;
|
||||
mod host;
|
||||
mod machine;
|
||||
mod path_util;
|
||||
mod resolve;
|
||||
mod state;
|
||||
mod value;
|
||||
|
||||
pub use bytecode_reader::*;
|
||||
pub use forced::*;
|
||||
pub use host::*;
|
||||
pub use machine::*;
|
||||
pub use path_util::*;
|
||||
pub use resolve::*;
|
||||
pub use state::*;
|
||||
pub use value::*;
|
||||
@@ -0,0 +1,174 @@
|
||||
use std::ops::ControlFlow;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use fix_common::StringId;
|
||||
use fix_error::Error;
|
||||
use gc_arena::Mutation;
|
||||
|
||||
use crate::{
|
||||
Break, BytecodeReader, CallFrame, ForceMode, Forced, GcEnv, NixType, PendingLoad, Step,
|
||||
StrictValue, Value, VmError,
|
||||
};
|
||||
|
||||
/// Abstract VM-side operations consumed by instruction handlers and primops.
|
||||
///
|
||||
/// Implementors maintain a value stack, a call stack, an environment chain,
|
||||
/// pending result/error state, and a set of GC-allocated globals. Methods
|
||||
/// fall into a few groups:
|
||||
///
|
||||
/// - Stack ops (`push` / `pop` / `peek` / `replace` / `pop_forced` / ...)
|
||||
/// - Forcing primitives (`force_slot` / `force_slot_to_pc`)
|
||||
/// - Calling (`call` / `return_from_primop`)
|
||||
/// - Call-frame management (`push_call_frame` / `pop_call_frame` / call-depth)
|
||||
/// - Environment access (`env` / `set_env` / `local`)
|
||||
/// - Result finalization (`finish_ok` / `finish_err` / ...)
|
||||
/// - Global lookup (`builtins` / `empty_list` / `empty_attrs` / ...)
|
||||
/// - Imports and scope slots (`import_cache_*` / `scope_slot*` / `set_pending_load`)
|
||||
pub trait Machine<'gc> {
|
||||
fn push(&mut self, val: Value<'gc>);
|
||||
fn pop(&mut self) -> Value<'gc>;
|
||||
fn peek(&self, depth: usize) -> Value<'gc>;
|
||||
fn peek_forced(&self, depth: usize) -> StrictValue<'gc>;
|
||||
fn pop_forced(&mut self) -> StrictValue<'gc>;
|
||||
fn replace(&mut self, depth: usize, val: Value<'gc>);
|
||||
fn stack_len(&self) -> usize;
|
||||
|
||||
fn force_slot_to_pc(
|
||||
&mut self,
|
||||
depth: usize,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> Step;
|
||||
|
||||
#[inline(always)]
|
||||
fn force_slot(
|
||||
&mut self,
|
||||
depth: usize,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Step {
|
||||
let pc = reader.inst_start_pc();
|
||||
self.force_slot_to_pc(depth, reader, mc, pc)
|
||||
}
|
||||
|
||||
fn call(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
arg: Value<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> Step;
|
||||
|
||||
#[inline(always)]
|
||||
fn return_from_primop(&mut self, val: Value<'gc>, reader: &mut BytecodeReader<'_>) -> Step {
|
||||
self.push(val);
|
||||
let Some(CallFrame {
|
||||
pc: ret_pc,
|
||||
thunk: _,
|
||||
env,
|
||||
}) = self.pop_call_frame()
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
reader.set_pc(ret_pc);
|
||||
self.dec_call_depth();
|
||||
self.set_env(env);
|
||||
Step::Continue(())
|
||||
}
|
||||
|
||||
fn push_call_frame(&mut self, frame: CallFrame<'gc>);
|
||||
fn pop_call_frame(&mut self) -> Option<CallFrame<'gc>>;
|
||||
fn call_depth(&self) -> usize;
|
||||
fn inc_call_depth(&mut self);
|
||||
fn dec_call_depth(&mut self);
|
||||
|
||||
fn env(&self) -> GcEnv<'gc>;
|
||||
fn set_env(&mut self, env: GcEnv<'gc>);
|
||||
|
||||
#[inline(always)]
|
||||
fn local(&self, layer: u8, idx: u32) -> Value<'gc> {
|
||||
let mut cur = self.env();
|
||||
for _ in 0..layer {
|
||||
let prev = cur.borrow().prev.expect("env chain too short");
|
||||
cur = prev;
|
||||
}
|
||||
cur.borrow().locals[idx as usize]
|
||||
}
|
||||
|
||||
fn finish_ok(&mut self, val: fix_common::Value) -> Step;
|
||||
fn finish_err(&mut self, err: Box<Error>) -> Step;
|
||||
fn finish_type_err(&mut self, expected: NixType, got: NixType) -> Step;
|
||||
|
||||
#[inline(always)]
|
||||
fn finish_vm_err(&mut self, err: VmError) -> Step {
|
||||
self.finish_err(err.into_error())
|
||||
}
|
||||
|
||||
fn builtins(&self) -> Value<'gc>;
|
||||
fn functor_sym(&self) -> StringId;
|
||||
fn empty_list(&self) -> Value<'gc>;
|
||||
fn empty_attrs(&self) -> Value<'gc>;
|
||||
fn force_mode(&self) -> ForceMode;
|
||||
|
||||
fn import_cache_get(&self, path: &Path) -> Option<Value<'gc>>;
|
||||
fn import_cache_insert(&mut self, path: PathBuf, val: Value<'gc>);
|
||||
fn scope_slot(&self, idx: u32) -> Value<'gc>;
|
||||
fn scope_slots_push(&mut self, val: Value<'gc>) -> u32;
|
||||
fn set_pending_load(&mut self, load: PendingLoad);
|
||||
}
|
||||
|
||||
/// Extension trait with convenience helpers built on top of [`Machine`].
|
||||
///
|
||||
/// Auto-implemented for every `Machine<'gc>` so callers just need to bring
|
||||
/// `MachineExt` (or `Machine`) into scope.
|
||||
pub trait MachineExt<'gc>: Machine<'gc> {
|
||||
/// Force the top `T::WIDTH` stack slots and return them as `T`.
|
||||
///
|
||||
/// If any slot holds a pending thunk, this method pushes a call frame
|
||||
/// whose resume PC is the **start of the current instruction**
|
||||
/// (`reader.inst_start_pc()`), enters the thunk, and returns
|
||||
/// `Break::Force`. When the thunk eventually returns, the VM will
|
||||
/// **re-execute the entire opcode handler from the beginning**.
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// * **Do not call this method more than once in a single handler.**
|
||||
/// If you need to force multiple values, use a tuple type such as
|
||||
/// `(StrictValue, StrictValue)` so they are forced and popped in one
|
||||
/// atomic operation.
|
||||
/// * The stack layout at the call site must be **identical** every time
|
||||
/// the handler is re-entered.
|
||||
/// * Propagate the return value with `?` so `Break::Force` correctly
|
||||
/// unwinds to the dispatch loop.
|
||||
#[inline(always)]
|
||||
fn force_and_retry<T: Forced<'gc>>(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
) -> ControlFlow<Break, T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let pc = reader.inst_start_pc();
|
||||
self.force_and_retry_pc(reader, mc, pc)
|
||||
}
|
||||
|
||||
/// Same as [`force_and_retry`](Self::force_and_retry) but allows
|
||||
/// specifying a custom resume PC.
|
||||
#[inline(always)]
|
||||
fn force_and_retry_pc<T: Forced<'gc>>(
|
||||
&mut self,
|
||||
reader: &mut BytecodeReader<'_>,
|
||||
mc: &Mutation<'gc>,
|
||||
resume_pc: usize,
|
||||
) -> ControlFlow<Break, T>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
T::force_and_check(self, reader, mc, 0, resume_pc)?;
|
||||
ControlFlow::Continue(T::pop_converted(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, M: Machine<'gc>> MachineExt<'gc> for M {}
|
||||
@@ -0,0 +1,18 @@
|
||||
use std::path::{Component, PathBuf};
|
||||
|
||||
pub fn canon_path_str(path: impl AsRef<std::path::Path>) -> String {
|
||||
let p = path.as_ref();
|
||||
let mut normalized = PathBuf::new();
|
||||
for component in p.components() {
|
||||
match component {
|
||||
Component::Prefix(p) => normalized.push(p.as_os_str()),
|
||||
Component::RootDir => normalized.push("/"),
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
normalized.pop();
|
||||
}
|
||||
Component::Normal(c) => normalized.push(c),
|
||||
}
|
||||
}
|
||||
normalized.to_string_lossy().into_owned()
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
use gc_arena::{Gc, Mutation};
|
||||
|
||||
use crate::{AttrSet, Machine, OperandData, Value};
|
||||
|
||||
/// Resolve a decoded operand into a runtime [`Value`].
|
||||
///
|
||||
/// The operand decoder ([`crate::BytecodeReader::read_operand_data`])
|
||||
/// produces a static enum; this function materializes it against the
|
||||
/// running [`Machine`] (env chain, builtins, scope slots, ...).
|
||||
#[inline]
|
||||
pub fn resolve_operand<'gc, M: Machine<'gc>>(
|
||||
op: &OperandData,
|
||||
mc: &Mutation<'gc>,
|
||||
m: &M,
|
||||
) -> Value<'gc> {
|
||||
use OperandData::*;
|
||||
match *op {
|
||||
Const(sv) => sv.into(),
|
||||
BigInt(val) => Value::new_gc(Gc::new(mc, val)),
|
||||
Local { layer, idx } => m.local(layer, idx),
|
||||
#[allow(clippy::unwrap_used)]
|
||||
BuiltinConst(id) => m.builtins().as_gc::<AttrSet>().unwrap().lookup(id).unwrap(),
|
||||
Builtins => m.builtins(),
|
||||
ReplBinding(_id) => todo!(),
|
||||
ScopedImportBinding { slot_id, name } => {
|
||||
let scope = m.scope_slot(slot_id);
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let attrs = scope.as_gc::<AttrSet>().expect("scope must be attrset");
|
||||
#[allow(clippy::unwrap_used)]
|
||||
attrs.lookup(name).expect("scoped binding not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
use std::ops::ControlFlow;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use fix_common::StringId;
|
||||
use fix_error::Error;
|
||||
use gc_arena::{Collect, Gc};
|
||||
use hashbrown::HashSet;
|
||||
|
||||
use crate::{GcEnv, StaticValue, Thunk};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub enum VmError {
|
||||
Catchable(String),
|
||||
Uncatchable(Box<Error>),
|
||||
}
|
||||
|
||||
impl From<Box<Error>> for VmError {
|
||||
fn from(e: Box<Error>) -> Self {
|
||||
VmError::Uncatchable(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl VmError {
|
||||
pub fn into_error(self) -> Box<Error> {
|
||||
match self {
|
||||
VmError::Catchable(_) => todo!("Check for tryEval catch frames"),
|
||||
VmError::Uncatchable(e) => e,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vm_err(msg: impl Into<String>) -> VmError {
|
||||
VmError::Uncatchable(Error::eval_error(msg.into()))
|
||||
}
|
||||
|
||||
#[derive(Collect, Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
#[collect(require_static)]
|
||||
pub enum ForceMode {
|
||||
#[default]
|
||||
AsIs,
|
||||
Shallow,
|
||||
Deep,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
pub enum Break {
|
||||
Force,
|
||||
Done,
|
||||
LoadFile,
|
||||
}
|
||||
|
||||
pub type Step = ControlFlow<Break>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct ErrorFrame {
|
||||
pub span_id: u32,
|
||||
pub message: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub struct CallFrame<'gc> {
|
||||
pub pc: usize,
|
||||
pub thunk: Option<Gc<'gc, Thunk<'gc>>>,
|
||||
pub env: GcEnv<'gc>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PendingLoad {
|
||||
pub path: PathBuf,
|
||||
pub scope: Option<PendingScope>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PendingScope {
|
||||
pub keys: HashSet<StringId>,
|
||||
pub slot_id: u32,
|
||||
}
|
||||
|
||||
/// Extra scope passed to a re-entrant compile from inside a running VM.
|
||||
///
|
||||
/// Currently only `ScopedImport` is produced (by the `scopedImport` builtin),
|
||||
/// but the variant is kept open so REPL bindings could later land here too.
|
||||
pub enum ExtraScope {
|
||||
ScopedImport {
|
||||
keys: HashSet<StringId>,
|
||||
slot_id: u32,
|
||||
},
|
||||
}
|
||||
|
||||
pub enum OperandData {
|
||||
Const(StaticValue),
|
||||
BigInt(i64),
|
||||
Local { layer: u8, idx: u32 },
|
||||
BuiltinConst(StringId),
|
||||
Builtins,
|
||||
ReplBinding(StringId),
|
||||
ScopedImportBinding { slot_id: u32, name: StringId },
|
||||
}
|
||||
@@ -0,0 +1,712 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
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;
|
||||
use smallvec::SmallVec;
|
||||
use string_interner::Symbol;
|
||||
use string_interner::symbol::SymbolU32;
|
||||
|
||||
use crate::boxing::{RawBox, RawStore, RawTag, Value as RawValue};
|
||||
|
||||
mod private {
|
||||
pub trait Cealed {}
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// TAG must be unique among all implementors.
|
||||
#[allow(private_interfaces)]
|
||||
pub unsafe trait Storable: private::Cealed {
|
||||
const TAG: RawTag;
|
||||
}
|
||||
#[allow(private_bounds)]
|
||||
pub trait InlineStorable: Storable + RawStore {}
|
||||
pub trait GcStorable: Storable {}
|
||||
|
||||
macro_rules! define_value_types {
|
||||
(
|
||||
inline { $($itype:ty => $itag:expr, $iname:literal;)* }
|
||||
gc { $($gtype:ty => $gtag:expr, $gname:literal;)* }
|
||||
) => {
|
||||
$(
|
||||
#[allow(private_interfaces)]
|
||||
unsafe impl Storable for $itype {
|
||||
const TAG: RawTag = $itag;
|
||||
}
|
||||
impl InlineStorable for $itype {}
|
||||
impl private::Cealed for $itype {}
|
||||
)*
|
||||
$(
|
||||
#[allow(private_interfaces)]
|
||||
unsafe impl Storable for $gtype {
|
||||
const TAG: RawTag = $gtag;
|
||||
}
|
||||
impl GcStorable for $gtype {}
|
||||
impl private::Cealed for $gtype {}
|
||||
)*
|
||||
|
||||
const _: () = assert!(size_of::<Value<'static>>() == 8);
|
||||
$(const _: () = assert!(size_of::<$itype>() <= 6);)*
|
||||
|
||||
const _: () = {
|
||||
let tags: &[(bool, u8)] = &[$(RawTag::neg_val($itag)),*, $(RawTag::neg_val($gtag)),*];
|
||||
let mut mask_false: u8 = 0;
|
||||
let mut mask_true: u8 = 0;
|
||||
let mut i = 0;
|
||||
while i < tags.len() {
|
||||
let (neg, val) = tags[i];
|
||||
let bit = 1 << val;
|
||||
if neg {
|
||||
assert!(mask_true & bit == 0, "duplicate true tag id");
|
||||
mask_true |= bit;
|
||||
} else {
|
||||
assert!(mask_false & bit == 0, "duplicate false tag id");
|
||||
mask_false |= bit;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
};
|
||||
|
||||
unsafe impl<'gc> Collect<'gc> for Value<'gc> {
|
||||
const NEEDS_TRACE: bool = true;
|
||||
fn trace<T: Trace<'gc>>(&self, cc: &mut T) {
|
||||
let Some(tag) = self.raw.tag() else { return };
|
||||
match tag {
|
||||
$(<$gtype as Storable>::TAG => unsafe {
|
||||
self.load_gc::<$gtype>().trace(cc)
|
||||
},)*
|
||||
$(<$itype as Storable>::TAG => (),)*
|
||||
_ => unreachable!("invalid value tag"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.tag() {
|
||||
None => write!(f, "Float({:?})", unsafe {
|
||||
self.raw.float().unwrap_unchecked()
|
||||
}),
|
||||
$(Some(<$itype as Storable>::TAG) => write!(f, "{}({:?})", $iname, unsafe {
|
||||
self.as_inline::<$itype>().unwrap_unchecked()
|
||||
}),)*
|
||||
$(Some(<$gtype as Storable>::TAG) =>
|
||||
write!(f, "{}(..)", $gname),)*
|
||||
_ => unreachable!("invalid value tag"),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
define_value_types! {
|
||||
inline {
|
||||
i32 => RawTag::P1, "SmallInt";
|
||||
bool => RawTag::P2, "Bool";
|
||||
Null => RawTag::P3, "Null";
|
||||
StringId => RawTag::P4, "SmallString";
|
||||
PrimOp => RawTag::P5, "PrimOp";
|
||||
Path => RawTag::N6, "Path";
|
||||
}
|
||||
gc {
|
||||
i64 => RawTag::P6, "BigInt";
|
||||
NixString => RawTag::P7, "String";
|
||||
AttrSet<'_> => RawTag::N1, "AttrSet";
|
||||
List<'_> => RawTag::N2, "List";
|
||||
Thunk<'_> => RawTag::N3, "Thunk";
|
||||
Closure<'_> => RawTag::N4, "Closure";
|
||||
PrimOpApp<'_> => RawTag::N5, "PrimOpApp";
|
||||
}
|
||||
}
|
||||
|
||||
/// # Nix runtime value representation
|
||||
///
|
||||
/// NaN-boxed value fitting in 8 bytes.
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct Value<'gc> {
|
||||
raw: RawBox,
|
||||
_marker: PhantomData<Gc<'gc, ()>>,
|
||||
}
|
||||
|
||||
impl Default for Value<'_> {
|
||||
#[inline(always)]
|
||||
fn default() -> Self {
|
||||
Self::new_inline(Null)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Value<'gc> {
|
||||
#[inline(always)]
|
||||
fn from_raw_value(rv: RawValue) -> Self {
|
||||
Self {
|
||||
raw: RawBox::from_value(rv),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Load a GC pointer from a value with a negative tag.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The value must actually store a `Gc<'gc, T>` with the matching type.
|
||||
#[inline(always)]
|
||||
unsafe fn load_gc<T: GcStorable>(self) -> Gc<'gc, T> {
|
||||
unsafe {
|
||||
let rv = self.raw.value().unwrap_unchecked();
|
||||
let ptr: *const T = <*const T as RawStore>::from_val(rv);
|
||||
Gc::from_ptr(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
const fn tag(self) -> Option<RawTag> {
|
||||
self.raw.tag()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Value<'gc> {
|
||||
#[inline]
|
||||
pub fn new_float(val: f64) -> Self {
|
||||
Self {
|
||||
raw: RawBox::from_float(val),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_inline<T: InlineStorable>(val: T) -> Self {
|
||||
Self::from_raw_value(RawValue::store(T::TAG, val))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_gc<T: GcStorable>(gc: Gc<'gc, T>) -> Self {
|
||||
let ptr = Gc::as_ptr(gc);
|
||||
Self::from_raw_value(RawValue::store(T::TAG, ptr))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn make_int(val: i64, mc: &Mutation<'gc>) -> Self {
|
||||
if val >= i32::MIN as i64 && val <= i32::MAX as i64 {
|
||||
Value::new_inline(val as i32)
|
||||
} else {
|
||||
Value::new_gc(Gc::new(mc, val))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Value<'gc> {
|
||||
#[inline]
|
||||
pub fn is_float(self) -> bool {
|
||||
self.raw.is_float()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is<T: Storable>(self) -> bool {
|
||||
self.tag() == Some(T::TAG)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Value<'gc> {
|
||||
#[inline]
|
||||
pub fn as_float(self) -> Option<f64> {
|
||||
self.raw.float().copied()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_inline<T: InlineStorable>(self) -> Option<T> {
|
||||
if self.is::<T>() {
|
||||
Some(unsafe {
|
||||
let rv = self.raw.value().unwrap_unchecked();
|
||||
T::from_val(rv)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_gc<T: GcStorable>(self) -> Option<Gc<'gc, T>> {
|
||||
if self.is::<T>() {
|
||||
Some(unsafe {
|
||||
let rv = self.raw.value().unwrap_unchecked();
|
||||
let ptr: *const T = <*const T as RawStore>::from_val(rv);
|
||||
Gc::from_ptr(ptr)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_bits(self) -> u64 {
|
||||
self.raw.to_bits()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_num(self) -> Option<NixNum> {
|
||||
if let Some(i) = self.as_inline::<i32>() {
|
||||
Some(NixNum::Int(i as i64))
|
||||
} else if let Some(gc_i) = self.as_gc::<i64>() {
|
||||
Some(NixNum::Int(*gc_i))
|
||||
} else {
|
||||
self.as_float().map(NixNum::Float)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn restrict(self) -> Result<StrictValue<'gc>, Gc<'gc, Thunk<'gc>>> {
|
||||
if let Some(thunk) = self.as_gc::<Thunk<'gc>>() {
|
||||
Err(thunk)
|
||||
} else {
|
||||
Ok(StrictValue(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ty(self) -> NixType {
|
||||
if self.is_float() {
|
||||
NixType::Float
|
||||
} else if self.is::<i32>() || self.is::<i64>() {
|
||||
NixType::Int
|
||||
} else if self.is::<bool>() {
|
||||
NixType::Bool
|
||||
} else if self.is::<Null>() {
|
||||
NixType::Null
|
||||
} else if self.is::<StringId>() {
|
||||
NixType::String
|
||||
} else if self.is::<PrimOp>() {
|
||||
NixType::PrimOp
|
||||
} else if self.is::<NixString>() {
|
||||
NixType::String
|
||||
} else if self.is::<Path>() {
|
||||
NixType::Path
|
||||
} else if self.is::<AttrSet>() {
|
||||
NixType::AttrSet
|
||||
} else if self.is::<List>() {
|
||||
NixType::List
|
||||
} else if self.is::<Thunk>() {
|
||||
NixType::Thunk
|
||||
} else if self.is::<Closure>() {
|
||||
NixType::Closure
|
||||
} else if self.is::<PrimOpApp>() {
|
||||
NixType::PrimOpApp
|
||||
} else {
|
||||
unreachable!("value has no recognized type tag")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_inline<T: InlineStorable>(self) -> Result<T, NixType> {
|
||||
self.as_inline::<T>().ok_or_else(|| self.ty())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_gc<T: GcStorable>(self) -> Result<Gc<'gc, T>, NixType> {
|
||||
self.as_gc::<T>().ok_or_else(|| self.ty())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_num(self) -> Result<NixNum, NixType> {
|
||||
self.as_num().ok_or_else(|| self.ty())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_bool(self) -> Result<bool, NixType> {
|
||||
self.as_inline::<bool>().ok_or_else(|| self.ty())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_float(self) -> Result<f64, NixType> {
|
||||
self.as_float().ok_or_else(|| self.ty())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default)]
|
||||
#[repr(transparent)]
|
||||
pub struct StaticValue(Value<'static>);
|
||||
|
||||
impl<'gc> From<StaticValue> for Value<'gc> {
|
||||
#[inline]
|
||||
fn from(value: StaticValue) -> Self {
|
||||
// SAFETY: StaticValue is guaranteed to not contain any `Gc`.
|
||||
unsafe { std::mem::transmute::<Value<'static>, Value<'gc>>(value.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticValue {
|
||||
#[inline]
|
||||
pub fn new_float(val: f64) -> Self {
|
||||
Self(Value::new_float(val))
|
||||
}
|
||||
#[inline]
|
||||
pub fn new_inline<T: InlineStorable>(val: T) -> Self {
|
||||
Self(Value::new_inline(val))
|
||||
}
|
||||
#[inline]
|
||||
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 {
|
||||
self.0.is_float()
|
||||
}
|
||||
#[inline]
|
||||
pub fn is<T: InlineStorable>(self) -> bool {
|
||||
self.0.is::<T>()
|
||||
}
|
||||
#[inline]
|
||||
pub fn as_float(self) -> Option<f64> {
|
||||
self.0.as_float()
|
||||
}
|
||||
#[inline]
|
||||
pub fn as_inline<T: InlineStorable>(self) -> Option<T> {
|
||||
self.0.as_inline::<T>()
|
||||
}
|
||||
#[inline]
|
||||
pub fn to_bits(self) -> u64 {
|
||||
self.0.raw.to_bits()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Null;
|
||||
impl RawStore for Null {
|
||||
fn to_val(self, value: &mut RawValue) {
|
||||
value.set_data([0; 6]);
|
||||
}
|
||||
fn from_val(_: &RawValue) -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl RawStore for StringId {
|
||||
fn to_val(self, value: &mut RawValue) {
|
||||
(self.0.to_usize() as u32).to_val(value);
|
||||
}
|
||||
fn from_val(value: &RawValue) -> Self {
|
||||
Self(
|
||||
SymbolU32::try_from_usize(u32::from_val(value) as usize)
|
||||
.expect("failed to read StringId from Value"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A canonicalized absolute path. Inline value carrying an interned
|
||||
/// `StringId` whose contents are the path's absolute, dot-resolved form.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Path(pub StringId);
|
||||
|
||||
impl RawStore for Path {
|
||||
fn to_val(self, value: &mut RawValue) {
|
||||
self.0.to_val(value);
|
||||
}
|
||||
fn from_val(value: &RawValue) -> Self {
|
||||
Self(StringId::from_val(value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Heap-allocated Nix string.
|
||||
///
|
||||
/// Stored on the GC heap via `Gc<'gc, NixString>`. The string data itself
|
||||
/// lives in a standard `Box<str>` owned by this struct; the GC only manages
|
||||
/// the outer allocation.
|
||||
#[derive(Collect)]
|
||||
#[collect(require_static)]
|
||||
pub struct NixString {
|
||||
data: Box<str>,
|
||||
// TODO: string context for derivation dependency tracking
|
||||
}
|
||||
|
||||
impl NixString {
|
||||
pub fn new(s: impl Into<Box<str>>) -> Self {
|
||||
Self { data: s.into() }
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for NixString {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self.data, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug, Default)]
|
||||
#[collect(no_drop)]
|
||||
pub struct AttrSet<'gc> {
|
||||
pub entries: SmallVec<[(StringId, Value<'gc>); 4]>,
|
||||
}
|
||||
|
||||
impl<'gc> AttrSet<'gc> {
|
||||
pub fn from_sorted_unchecked(entries: SmallVec<[(StringId, Value<'gc>); 4]>) -> Self {
|
||||
debug_assert!(entries.is_sorted_by_key(|(key, _)| *key));
|
||||
Self { entries }
|
||||
}
|
||||
|
||||
pub fn lookup(&self, key: StringId) -> Option<Value<'gc>> {
|
||||
self.entries
|
||||
.binary_search_by_key(&key, |(k, _)| *k)
|
||||
.ok()
|
||||
.map(|i| self.entries[i].1)
|
||||
}
|
||||
|
||||
pub fn has(&self, key: StringId) -> bool {
|
||||
self.entries.binary_search_by_key(&key, |(k, _)| *k).is_ok()
|
||||
}
|
||||
|
||||
pub fn merge(&self, other: &Self, mc: &Mutation<'gc>) -> Gc<'gc, Self> {
|
||||
use std::cmp::Ordering::*;
|
||||
|
||||
debug_assert!(self.entries.is_sorted_by_key(|(key, _)| *key));
|
||||
debug_assert!(other.entries.is_sorted_by_key(|(key, _)| *key));
|
||||
|
||||
let mut entries = SmallVec::new();
|
||||
let mut i = 0;
|
||||
let mut j = 0;
|
||||
while i < self.entries.len() && j < other.entries.len() {
|
||||
match self.entries[i].0.cmp(&other.entries[j].0) {
|
||||
Less => {
|
||||
entries.push(self.entries[i]);
|
||||
i += 1;
|
||||
}
|
||||
Greater => {
|
||||
entries.push(other.entries[j]);
|
||||
j += 1;
|
||||
}
|
||||
Equal => {
|
||||
entries.push(other.entries[j]);
|
||||
i += 1;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
entries.extend(other.entries[j..].iter().cloned());
|
||||
entries.extend(self.entries[i..].iter().cloned());
|
||||
|
||||
debug_assert!(entries.is_sorted_by_key(|(key, _)| *key));
|
||||
|
||||
Gc::new(mc, AttrSet { entries })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug, Default)]
|
||||
#[repr(transparent)]
|
||||
#[collect(no_drop)]
|
||||
pub struct List<'gc> {
|
||||
pub inner: RefLock<SmallVec<[Value<'gc>; 4]>>,
|
||||
}
|
||||
|
||||
impl<'gc> List<'gc> {
|
||||
pub fn new(mc: &Mutation<'gc>, data: SmallVec<[Value<'gc>; 4]>) -> Gc<'gc, Self> {
|
||||
Gc::new(
|
||||
mc,
|
||||
Self {
|
||||
inner: RefLock::new(data),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub 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() }
|
||||
}
|
||||
}
|
||||
|
||||
pub type Thunk<'gc> = RefLock<ThunkState<'gc>>;
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub enum ThunkState<'gc> {
|
||||
Pending { ip: usize, env: GcEnv<'gc> },
|
||||
Apply { func: Value<'gc>, arg: Value<'gc> },
|
||||
Blackhole,
|
||||
Evaluated(StrictValue<'gc>),
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub struct Env<'gc> {
|
||||
pub locals: SmallVec<[Value<'gc>; 4]>,
|
||||
pub prev: Option<GcEnv<'gc>>,
|
||||
}
|
||||
pub type GcEnv<'gc> = GcRefLock<'gc, Env<'gc>>;
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub struct WithEnv<'gc> {
|
||||
pub env: Value<'gc>,
|
||||
pub prev: Option<GcWithEnv<'gc>>,
|
||||
}
|
||||
pub type GcWithEnv<'gc> = Gc<'gc, WithEnv<'gc>>;
|
||||
|
||||
impl<'gc> Env<'gc> {
|
||||
pub fn empty() -> Self {
|
||||
Env {
|
||||
locals: SmallVec::new(),
|
||||
prev: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_arg(arg: Value<'gc>, n_locals: u32, prev: Gc<'gc, RefLock<Env<'gc>>>) -> Self {
|
||||
let mut locals = smallvec::smallvec![Value::default(); 1 + n_locals as usize];
|
||||
locals[0] = arg;
|
||||
Env {
|
||||
locals,
|
||||
prev: Some(prev),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub struct Closure<'gc> {
|
||||
pub ip: u32,
|
||||
pub n_locals: u32,
|
||||
pub env: Gc<'gc, RefLock<Env<'gc>>>,
|
||||
pub pattern: Option<Gc<'gc, PatternInfo>>,
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(require_static)]
|
||||
pub struct PatternInfo {
|
||||
pub required: SmallVec<[StringId; 4]>,
|
||||
pub optional: SmallVec<[StringId; 4]>,
|
||||
pub ellipsis: bool,
|
||||
pub param_spans: Box<[(StringId, u32)]>,
|
||||
}
|
||||
|
||||
#[repr(packed, Rust)]
|
||||
#[derive(Clone, Copy, Debug, Collect)]
|
||||
#[collect(require_static)]
|
||||
pub struct PrimOp {
|
||||
pub id: BuiltinId,
|
||||
pub arity: u8,
|
||||
pub dispatch_ip: u32,
|
||||
}
|
||||
|
||||
impl RawStore for PrimOp {
|
||||
fn to_val(self, value: &mut RawValue) {
|
||||
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, bytes @ ..] = *value.data();
|
||||
Self {
|
||||
id: BuiltinId::try_from_primitive(id).expect("invalid BuiltinId"),
|
||||
arity,
|
||||
dispatch_ip: u32::from_le_bytes(bytes),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Collect, Debug)]
|
||||
#[collect(no_drop)]
|
||||
pub struct PrimOpApp<'gc> {
|
||||
pub primop: PrimOp,
|
||||
pub arity: u8,
|
||||
pub args: [Value<'gc>; 3],
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Collect)]
|
||||
#[repr(transparent)]
|
||||
#[collect(no_drop)]
|
||||
pub struct StrictValue<'gc>(Value<'gc>);
|
||||
|
||||
impl<'gc> StrictValue<'gc> {
|
||||
#[inline]
|
||||
pub fn relax(self) -> Value<'gc> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Deref for StrictValue<'gc> {
|
||||
type Target = Value<'gc>;
|
||||
#[inline]
|
||||
fn deref(&self) -> &Value<'gc> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for StrictValue<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Collect)]
|
||||
#[collect(require_static)]
|
||||
pub enum NixType {
|
||||
Int,
|
||||
Float,
|
||||
Bool,
|
||||
Null,
|
||||
String,
|
||||
Path,
|
||||
AttrSet,
|
||||
List,
|
||||
Thunk,
|
||||
Closure,
|
||||
PrimOp,
|
||||
PrimOpApp,
|
||||
}
|
||||
|
||||
impl NixType {
|
||||
pub fn display(self) -> &'static str {
|
||||
use NixType::*;
|
||||
match self {
|
||||
Int => "an integer",
|
||||
Float => "a float",
|
||||
Bool => "a boolean",
|
||||
Null => "null",
|
||||
String => "a string",
|
||||
Path => "a path",
|
||||
AttrSet => "a set",
|
||||
List => "a list",
|
||||
Thunk => "a thunk",
|
||||
Closure => "a function",
|
||||
PrimOp => "a built-in function",
|
||||
PrimOpApp => "a partially applied built-in function",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NixType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.display())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum NixNum {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
}
|
||||
Reference in New Issue
Block a user