pub struct VmData<const N: usize = { u8::MAX as usize }> { /* private fields */ }Expand description
A flattened math expression, ready for evaluation or further compilation.
Under the hood, VmData stores two different representations:
- A tape in single static assignment form
(
SsaTape), which is suitable for use during tape simplification - A tape in register-allocated form (
RegTape), which can be efficiently evaluated or lowered into machine assembly
§Example
Consider the expression x + y. The SSA tape will look something like
this:
$0 = INPUT 0 // X
$1 = INPUT 1 // Y
$2 = ADD $0 $1 // (X + Y)
This will be lowered into a tape using real (or VM) registers:
r0 = INPUT 0 // X
r1 = INPUT 1 // Y
r0 = ADD r0 r1 // (X + Y)
Note that in this form, registers are reused (e.g. r0 stores both X and
X + Y).
We can peek at the internals and see this register-allocated tape:
use fidget::{compiler::RegOp, eval::MathShape, vm::{VmShape, VmData}};
let (sum, ctx) = fidget::rhai::eval("x + y")?;
let data = VmData::<255>::new(&ctx, sum)?;
assert_eq!(data.len(), 3); // X, Y, and (X + Y)
let mut iter = data.iter_asm();
assert_eq!(iter.next().unwrap(), RegOp::Input(0, 0));
assert_eq!(iter.next().unwrap(), RegOp::Input(1, 1));
assert_eq!(iter.next().unwrap(), RegOp::AddRegReg(0, 0, 1));Implementations§
source§impl<const N: usize> VmData<N>
impl<const N: usize> VmData<N>
sourcepub fn new(context: &Context, node: Node) -> Result<Self, Error>
pub fn new(context: &Context, node: Node) -> Result<Self, Error>
Builds a new tape for the given node
sourcepub fn vars(&self) -> &HashMap<String, u32>
pub fn vars(&self) -> &HashMap<String, u32>
Returns this tape’s mapping of variable names to indexes
sourcepub fn choice_count(&self) -> usize
pub fn choice_count(&self) -> usize
Returns the number of choice (min/max) nodes in the tape.
This is required because some evaluators pre-allocate spaces for the choice array.
sourcepub fn slot_count(&self) -> usize
pub fn slot_count(&self) -> usize
Returns the number of slots used by the inner VM tape
sourcepub fn simplify(
&self,
choices: &[Choice],
workspace: &mut VmWorkspace<N>,
tape: VmData<N>
) -> Result<Self, Error>
pub fn simplify( &self, choices: &[Choice], workspace: &mut VmWorkspace<N>, tape: VmData<N> ) -> Result<Self, Error>
Simplifies both inner tapes, using the provided choice array
To minimize allocations, this function takes a VmWorkspace and
spare VmData; it will reuse those allocations.
sourcepub fn iter_asm(&self) -> impl Iterator<Item = RegOp> + '_
pub fn iter_asm(&self) -> impl Iterator<Item = RegOp> + '_
Produces an iterator that visits RegOp values in evaluation order
sourcepub fn pretty_print(&self)
pub fn pretty_print(&self)
Pretty-prints the inner SSA tape
Trait Implementations§
Auto Trait Implementations§
impl<const N: usize> Freeze for VmData<N>
impl<const N: usize> RefUnwindSafe for VmData<N>
impl<const N: usize> Send for VmData<N>
impl<const N: usize> Sync for VmData<N>
impl<const N: usize> Unpin for VmData<N>
impl<const N: usize> UnwindSafe for VmData<N>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.