Struct fidget::vm::VmData

source ·
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>

source

pub fn new(context: &Context, node: Node) -> Result<Self, Error>

Builds a new tape for the given node

source

pub fn vars(&self) -> &HashMap<String, u32>

Returns this tape’s mapping of variable names to indexes

source

pub fn len(&self) -> usize

Returns the length of the internal VM tape

source

pub fn is_empty(&self) -> bool

Returns true if the internal VM tape is empty

source

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.

source

pub fn slot_count(&self) -> usize

Returns the number of slots used by the inner VM tape

source

pub fn var_count(&self) -> usize

Returns the number of variables used in this tape

source

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.

source

pub fn iter_asm(&self) -> impl Iterator<Item = RegOp> + '_

Produces an iterator that visits RegOp values in evaluation order

source

pub fn pretty_print(&self)

Pretty-prints the inner SSA tape

Trait Implementations§

source§

impl<const N: usize> Default for VmData<N>

source§

fn default() -> VmData<N>

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.