Struct watchmaker_vm::VirtualMachine
source · [−]pub struct VirtualMachine { /* private fields */ }
Expand description
A running instance of a virtual machine.
All internal registers are initialized to zero at start up.
The type exposes methods to either submit new data (iinput
, dinput
) or to extract results
(ioutput
, doutput
).
Operand addresses are evaluated modulo the size of the register array they refer to, to keep them within the allocated memory for that array.
This type is not thread safe. However, multiple instances do not share state and so can be used concurrently (for example, one virtual machine instance per thread) without interfering with each other.
Implementations
sourceimpl VirtualMachine
impl VirtualMachine
sourcepub fn new(architecture: &Architecture, instructions: Vec<Instruction>) -> Self
pub fn new(architecture: &Architecture, instructions: Vec<Instruction>) -> Self
Create a new instance of a virtual machine. This is the main method exposed by the crate.
Arguments
architecture
- Configuration of memory sizes and other parameters.instructions
- Instructions to execute.
sourcepub fn run(&mut self, budget: usize)
pub fn run(&mut self, budget: usize)
Call to run the virtual machine until one of the following criteria are met.
Stopping Criteria
SYN
instruction executed. Caller should use results in output memory, update input memory if required and callrun
again.HLT
instruction executed. Execution has finished. Caller should use results in output memory and not callrun
again.- The maximum number of instructions has been executed. Execution has finished.
Caller should use results in output memory and not call
run
again.
End of code behavior
Code execution will jump back to the first instruction on reaching the end of the provided instructions (ie wraps around). To prevent this behaviour, add a HLT instruction as the last instruction.
Arguments
budget
- The maximum number of instructions to implement.
sourcepub fn iinput(&mut self) -> &mut Vec<i64>
pub fn iinput(&mut self) -> &mut Vec<i64>
A modifiable bank of integers that act as input to the virtual machine.
sourcepub fn dinput(&mut self) -> &mut Vec<f64>
pub fn dinput(&mut self) -> &mut Vec<f64>
A bank of floating point values that act as input to the virtual machine.
sourcepub fn ioutput(&self) -> &Vec<i64>
pub fn ioutput(&self) -> &Vec<i64>
A snapshot of the bank of integers that act as output from the virtual machine.
sourcepub fn doutput(&self) -> &Vec<f64>
pub fn doutput(&self) -> &Vec<f64>
A snapshot of the bank of floating point values that act as output from the virtual machine.
sourcepub fn next_instruction(&self) -> &Instruction
pub fn next_instruction(&self) -> &Instruction
The next instruction to execute.
sourcepub fn next_instruction_position(&self) -> usize
pub fn next_instruction_position(&self) -> usize
The next instruction to execute.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for VirtualMachine
impl Send for VirtualMachine
impl Sync for VirtualMachine
impl Unpin for VirtualMachine
impl UnwindSafe for VirtualMachine
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more