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

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