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 future_label(&mut self, name: String, offset: usize)
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.
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.
Sourcepub fn get_value(&self, var: Var, adjust_ap: bool) -> CellExpression
pub fn get_value(&self, var: Var, adjust_ap: bool) -> CellExpression
Returns var
s value, with fixed ap if adjust_ap
is true.
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