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
impl CasmBuilder
sourcepub fn build<const BRANCH_COUNT: usize>(
self,
branch_names: [&str; BRANCH_COUNT],
) -> CasmBuildResult<BRANCH_COUNT>
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.
sourcepub fn curr_ap_change(&self) -> usize
pub fn curr_ap_change(&self) -> usize
Returns the current ap change of the builder. Useful for manual ap change handling.
sourcepub fn add_var(&mut self, value: CellExpression) -> Var
pub fn add_var(&mut self, value: CellExpression) -> Var
Adds a variable pointing to value
.
sourcepub fn alloc_var(&mut self, local_var: bool) -> Var
pub fn alloc_var(&mut self, local_var: bool) -> Var
Allocates a new variable in memory, either local (FP-based) or temp (AP-based).
sourcepub fn duplicate_var(&mut self, var: Var) -> Var
pub fn duplicate_var(&mut self, var: Var) -> Var
Returns an additional variable pointing to the same value.
sourcepub 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],
)
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.
sourcepub fn assert_vars_eq(&mut self, dst: Var, res: Var)
pub fn assert_vars_eq(&mut self, dst: Var, res: Var)
Adds an assertion that dst = res
.
dst
must be a cell reference.
sourcepub fn buffer_write_and_inc(&mut self, buffer: Var, value: Var)
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.
sourcepub fn maybe_add_tempvar(&mut self, var: Var) -> Var
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.
sourcepub fn get_ref_and_inc(&mut self, buffer: Var) -> Var
pub fn get_ref_and_inc(&mut self, buffer: Var) -> Var
Increments a buffer and allocates and returns variable pointing to its previous value.
sourcepub fn increase_ap_change(&mut self, amount: usize)
pub fn increase_ap_change(&mut self, amount: usize)
Increases the AP change by size
, without adding an instruction.
sourcepub fn bin_op(&mut self, op: CellOperator, lhs: Var, rhs: Var) -> Var
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.
sourcepub fn double_deref(&mut self, var: Var, offset: i16) -> Var
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.
sourcepub fn jump_nz(&mut self, condition: Var, label: String)
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.
sourcepub fn rescope<const VAR_COUNT: usize>(&mut self, vars: [(Var, Var); VAR_COUNT])
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.
sourcepub fn call(&mut self, label: String)
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.
sourcepub fn reset_steps(&mut self)
pub fn reset_steps(&mut self)
Resets the steps counter.
sourcepub fn mark_unreachable(&mut self)
pub fn mark_unreachable(&mut self)
Marks the current state as unreachable. Useful for removing bookkeeping after unsatisfiable conditions.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CasmBuilder
impl RefUnwindSafe for CasmBuilder
impl Send for CasmBuilder
impl Sync for CasmBuilder
impl Unpin for CasmBuilder
impl UnwindSafe for CasmBuilder
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> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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