cairo_lang_casm::builder

Struct CasmBuilder

Source
pub struct CasmBuilder { /* private fields */ }
Expand description

Builder to more easily write casm code.

Allows CASM building without specifically thinking about ap changes and the sizes of opcodes. Wrong usages of it would panic instead of returning a result, as this builder assumes we are in a post validation of parameters stage.

Implementations§

Source§

impl CasmBuilder

Source

pub fn build<const BRANCH_COUNT: usize>( self, branch_names: [&str; BRANCH_COUNT], ) -> CasmBuildResult<BRANCH_COUNT>

Finalizes the builder, with the requested labels as the returning branches. “Fallthrough” is a special case for the fallthrough case.

Source

pub fn curr_ap_change(&self) -> usize

Returns the current ap change of the builder. Useful for manual ap change handling.

Source

pub fn add_var(&mut self, value: CellExpression) -> Var

Adds a variable pointing to value.

Source

pub fn alloc_var(&mut self, local_var: bool) -> Var

Allocates a new variable in memory, either local (FP-based) or temp (AP-based).

Source

pub fn duplicate_var(&mut self, var: Var) -> Var

Returns an additional variable pointing to the same value.

Source

pub fn add_hint<const INPUTS_COUNT: usize, const OUTPUTS_COUNT: usize, THint: Into<Hint>, F: FnOnce([ResOperand; INPUTS_COUNT], [CellRef; OUTPUTS_COUNT]) -> THint>( &mut self, f: F, inputs: [Var; INPUTS_COUNT], outputs: [Var; OUTPUTS_COUNT], )

Adds a hint, generated from inputs which are cell refs or immediates and outputs which must be cell refs.

Source

pub fn assert_vars_eq(&mut self, dst: Var, res: Var)

Adds an assertion that dst = res. dst must be a cell reference.

Source

pub fn buffer_write_and_inc(&mut self, buffer: Var, value: Var)

Writes and increments a buffer. Useful for RangeCheck and similar buffers. buffer must be a cell reference, or a cell reference with a small added constant. value must be a cell reference.

Source

pub fn maybe_add_tempvar(&mut self, var: Var) -> Var

Writes var as a new tempvar and returns it as a variable, unless its value is already deref (which includes trivial calculations as well) so it instead returns a variable pointing to that location.

Source

pub fn get_ref_and_inc(&mut self, buffer: Var) -> Var

Increments a buffer and allocates and returns variable pointing to its previous value.

Source

pub fn add_ap(&mut self, size: usize)

Increases AP by size.

Source

pub fn increase_ap_change(&mut self, amount: usize)

Increases the AP change by size, without adding an instruction.

Source

pub fn bin_op(&mut self, op: CellOperator, lhs: Var, rhs: Var) -> Var

Returns a variable that is the op of lhs and rhs. lhs must be a cell reference and rhs must be deref or immediate.

Source

pub fn double_deref(&mut self, var: Var, offset: i16) -> Var

Returns a variable that is [[var] + offset]. var must be a cell reference, or a cell ref plus a small constant.

Source

pub fn jump(&mut self, label: String)

Add a statement to jump to label.

Source

pub fn jump_nz(&mut self, condition: Var, label: String)

Add a statement to jump to label if condition != 0. condition must be a cell reference.

Source

pub fn label(&mut self, name: String)

Adds a label here named name.

Source

pub fn future_label(&mut self, name: String, offset: usize)

Adds a label name in distance offset from the current point. Useful for calling code outside of the builder’s context.

Source

pub fn rescope<const VAR_COUNT: usize>(&mut self, vars: [(Var, Var); VAR_COUNT])

Rescoping the values, while ignoring all vars not stated in vars and giving the vars on the left side the values of the vars on the right side.

Source

pub fn call(&mut self, label: String)

Adds a call command to ‘label’. All AP based variables are passed to the called function state and dropped from the calling function state.

Source

pub fn ret(&mut self)

A return statement in the code.

Source

pub fn steps(&self) -> usize

The number of steps at the last added statement.

Source

pub fn reset_steps(&mut self)

Resets the steps counter.

Source

pub fn fail(&mut self)

Create an assert that would always fail.

Source

pub fn mark_unreachable(&mut self)

Marks the current state as unreachable. Useful for removing bookkeeping after unsatisfiable conditions.

Source

pub fn get_value(&self, var: Var, adjust_ap: bool) -> CellExpression

Returns vars value, with fixed ap if adjust_ap is true.

Trait Implementations§

Source§

impl Default for CasmBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

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

Source§

fn upcast(&self) -> &T

Source§

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

Source§

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