Struct midenc_codegen_masm::Emulator
source · pub struct Emulator { /* private fields */ }Expand description
Emulator provides us with a means to execute our MASM IR directly without having to emit “real” MASM and run it via the Miden VM. In other words, it’s a convenient way to run tests to verify the expected behavior of a program without all of the baggage of the Miden VM.
Emulator is necessarily a more limited execution environment:
- It only handles instructions which are defined in the Op enum
- Anything related to proving, calling contracts, etc. is not supported
- The default environment is empty, i.e. there are no Miden VM standard library functions available. Users must emit Miden IR for all functions they wish to call, or alternatively, provide native stubs.
Implementations§
source§impl Emulator
impl Emulator
pub const DEFAULT_HEAP_SIZE: u32 = 16_384u32
pub const DEFAULT_HEAP_START: u32 = 8_192u32
pub const DEFAULT_LOCALS_START: u32 = 12_288u32
sourcepub fn new(memory_size: u32, hp: u32, lp: u32, print_stack: bool) -> Self
pub fn new(memory_size: u32, hp: u32, lp: u32, print_stack: bool) -> Self
Construct a new, empty emulator with:
- A linear memory heap of
memory_sizewords - The start of the usable heap set to
hp(an address in words) - The start of the reserved heap used for locals set to
lp(an address in words)
sourcepub fn set_max_cycles(&mut self, max: usize)
pub fn set_max_cycles(&mut self, max: usize)
Place a cap on the number of cycles the emulator will execute before failing with an error
sourcepub fn watchpoints(&self) -> impl Iterator<Item = Watchpoint> + '_
pub fn watchpoints(&self) -> impl Iterator<Item = Watchpoint> + '_
Returns all watchpoints that are currently managed by this [BreakpointManager]
sourcepub fn breakpoints(&self) -> impl Iterator<Item = Breakpoint>
pub fn breakpoints(&self) -> impl Iterator<Item = Breakpoint>
Returns all breakpoints that are currently managed by this [BreakpointManager]
sourcepub fn set_breakpoint(&mut self, bp: Breakpoint)
pub fn set_breakpoint(&mut self, bp: Breakpoint)
Sets a breakpoint for the emulator
sourcepub fn clear_breakpoint(&mut self, bp: Breakpoint)
pub fn clear_breakpoint(&mut self, bp: Breakpoint)
Removes the given breakpoint from the emulator
sourcepub fn clear_breakpoints(&mut self)
pub fn clear_breakpoints(&mut self)
Removes the all breakpoints from the emulator
sourcepub fn set_watchpoint(
&mut self,
addr: u32,
size: u32,
mode: WatchMode,
) -> WatchpointId
pub fn set_watchpoint( &mut self, addr: u32, size: u32, mode: WatchMode, ) -> WatchpointId
Sets a watchpoint in the emulator
sourcepub fn clear_watchpoint(&mut self, id: WatchpointId)
pub fn clear_watchpoint(&mut self, id: WatchpointId)
Sets a watchpoint in the emulator
sourcepub fn watchpoint_mode(&mut self, id: WatchpointId, mode: WatchMode)
pub fn watchpoint_mode(&mut self, id: WatchpointId, mode: WatchMode)
Set the watch mode for a Watchpoint using the identifier returned by [watch]
sourcepub fn clear_watchpoints(&mut self)
pub fn clear_watchpoints(&mut self)
Clears all watchpoints
sourcepub fn clear_break_and_watchpoints(&mut self)
pub fn clear_break_and_watchpoints(&mut self)
Clear all breakpoints and watchpoints
sourcepub fn info(&self) -> Option<DebugInfo<'_>>
pub fn info(&self) -> Option<DebugInfo<'_>>
Get’s debug information about the current emulator state
sourcepub fn stacktrace(&self) -> Vec<CallFrame>
pub fn stacktrace(&self) -> Vec<CallFrame>
Get a stacktrace for the code running in the emulator
sourcepub fn current_ip(&self) -> Option<Instruction>
pub fn current_ip(&self) -> Option<Instruction>
Get the instruction pointer that will be next executed by the emulator
sourcepub fn current_function(&self) -> Option<FunctionIdent>
pub fn current_function(&self) -> Option<FunctionIdent>
Get the name of the function that is currently executing
sourcepub fn stack(&mut self) -> &OperandStack<Felt>
pub fn stack(&mut self) -> &OperandStack<Felt>
Get access to the current state of the operand stack
sourcepub fn stack_mut(&mut self) -> &mut OperandStack<Felt>
pub fn stack_mut(&mut self) -> &mut OperandStack<Felt>
Get mutable access to the current state of the operand stack
sourcepub fn load_program(
&mut self,
program: Arc<Program>,
) -> Result<(), EmulationError>
pub fn load_program( &mut self, program: Arc<Program>, ) -> Result<(), EmulationError>
Load program into this emulator
This resets the emulator state, as only one program may be loaded at a time.
sourcepub fn load_module(&mut self, module: Arc<Module>) -> Result<(), EmulationError>
pub fn load_module(&mut self, module: Arc<Module>) -> Result<(), EmulationError>
Load module into this emulator
An error is returned if a module with the same name is already loaded.
sourcepub fn reload_module(
&mut self,
module: Arc<Module>,
) -> Result<(), EmulationError>
pub fn reload_module( &mut self, module: Arc<Module>, ) -> Result<(), EmulationError>
Reloads a loaded module, name.
This function will panic if the named module is not currently loaded.
sourcepub fn unload_module(&mut self, name: Ident)
pub fn unload_module(&mut self, name: Ident)
Unloads a loaded module, name.
This function will panic if the named module is not currently loaded.
sourcepub fn load_nif(
&mut self,
id: FunctionIdent,
function: Box<dyn FnMut(&mut Emulator, &[Felt]) -> Result<(), EmulationError>>,
) -> Result<(), EmulationError>
pub fn load_nif( &mut self, id: FunctionIdent, function: Box<dyn FnMut(&mut Emulator, &[Felt]) -> Result<(), EmulationError>>, ) -> Result<(), EmulationError>
Load function into this emulator, with the given identifier
Because we don’t know the set of [FuncId] that have already been allocated, we leave the choice up to the caller. We assert that functions do not get defined twice to catch conflicts, just in case.
sourcepub fn write_bytes_to_memory(&mut self, value: &[u8]) -> u32
pub fn write_bytes_to_memory(&mut self, value: &[u8]) -> u32
Allocate space for value on the emulator heap, and copy it’s contents there.
NOTE: The smallest unit of addressable memory is 4 bytes (32 bits). If you provide a value that is smaller than this, or is not a multiple of 4, the data will be padded with zeroes to ensure that it is.
sourcepub fn malloc(&mut self, size: usize) -> u32
pub fn malloc(&mut self, size: usize) -> u32
Allocate enough words to hold size bytes of memory
Returns the pointer as a byte-addressable address
sourcepub fn store(&mut self, addr: usize, value: Felt)
pub fn store(&mut self, addr: usize, value: Felt)
Write value to the word at addr, and element index
sourcepub fn start(&mut self) -> Result<OperandStack<Felt>, EmulationError>
pub fn start(&mut self) -> Result<OperandStack<Felt>, EmulationError>
Start executing the current program by invokeing the top-level initialization block (the
entrypoint).
This function will run the program to completion, and return the state of the operand stack on exit.
NOTE: If no entrypoint has been loaded, an error is returned.
The emulator is automatically reset when it exits successfully.
sourcepub fn init(&mut self) -> Result<EmulatorEvent, EmulationError>
pub fn init(&mut self) -> Result<EmulatorEvent, EmulationError>
Start emulation by entering the top-level initialization block (the entrypoint).
This should be called instead of start when stepping through a program rather than
executing it to completion in one call.
NOTE: If no entrypoint has been loaded, an error is returned.
It is up to the caller to reset the emulator when the program exits, unlike start.
sourcepub fn stop(&mut self)
pub fn stop(&mut self)
Stop running the currently executing function, and reset the cycle counter, operand stack, and linear memory.
This function preserves loaded code, breakpoints, and other configuration items.
If an attempt is made to run the emulator in the stopped state, a panic will occur
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the emulator state to its initial state at creation.
In addition to resetting the cycle counter, operand stack, and linear memory, this function also unloads all code, and clears all breakpoints. Only the configuration used to initialize the emulator is preserved.
To use the emulator after calling this function, you must load a program or module again.
sourcepub fn invoke(
&mut self,
callee: FunctionIdent,
args: &[Felt],
) -> Result<OperandStack<Felt>, EmulationError>
pub fn invoke( &mut self, callee: FunctionIdent, args: &[Felt], ) -> Result<OperandStack<Felt>, EmulationError>
Run the emulator by invoking callee with args placed on the
operand stack in FIFO order.
If a fatal error occurs during emulation, Err is returned,
e.g. if callee has not been loaded.
When callee returns, it’s result will be returned wrapped in Ok.
For functions with no return value, this will be Ok(None), or all
others it will be Ok(Some(value)).
sourcepub fn enter(
&mut self,
callee: FunctionIdent,
args: &[Felt],
) -> Result<EmulatorEvent, EmulationError>
pub fn enter( &mut self, callee: FunctionIdent, args: &[Felt], ) -> Result<EmulatorEvent, EmulationError>
Run the emulator by invoking callee with args placed on the
operand stack in FIFO order.
If a fatal error occurs during emulation, Err is returned,
e.g. if callee has not been loaded.
When callee returns, it’s result will be returned wrapped in Ok.
For functions with no return value, this will be Ok(None), or all
others it will be Ok(Some(value)).
sourcepub fn resume(&mut self) -> Result<EmulatorEvent, EmulationError>
pub fn resume(&mut self) -> Result<EmulatorEvent, EmulationError>
Resume execution when the emulator suspended due to a breakpoint
source§impl Emulator
impl Emulator
sourcepub fn step(&mut self) -> Result<EmulatorEvent, EmulationError>
pub fn step(&mut self) -> Result<EmulatorEvent, EmulationError>
Step the emulator forward one cycle, returning the type of event produced during that cycle, or an error.
sourcepub fn step_over(&mut self) -> Result<EmulatorEvent, EmulationError>
pub fn step_over(&mut self) -> Result<EmulatorEvent, EmulationError>
Step the emulator forward one step, but stepping past any nested blocks or function calls, returning the type of event produced during that cycle, or an error.
sourcepub fn step_out(&mut self) -> Result<EmulatorEvent, EmulationError>
pub fn step_out(&mut self) -> Result<EmulatorEvent, EmulationError>
Step the emulator forward until control returns from the current function.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Emulator
impl !RefUnwindSafe for Emulator
impl !Send for Emulator
impl !Sync for Emulator
impl Unpin for Emulator
impl !UnwindSafe for Emulator
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> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moresource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moresource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more