openvm_instructions/
exe.rs

1use std::collections::BTreeMap;
2
3use openvm_stark_backend::p3_field::Field;
4use serde::{Deserialize, Serialize};
5
6use crate::program::Program;
7
8/// Memory image is a map from (address space, address) to word.
9pub type MemoryImage<F> = BTreeMap<(u32, u32), F>;
10/// Stores the starting address, end address, and name of a set of function.
11pub type FnBounds = BTreeMap<u32, FnBound>;
12
13/// Executable program for OpenVM.
14#[derive(Clone, Debug, Default, Serialize, Deserialize)]
15#[serde(bound(
16    serialize = "F: Serialize",
17    deserialize = "F: std::cmp::Ord + Deserialize<'de>"
18))]
19pub struct VmExe<F> {
20    /// Program to execute.
21    pub program: Program<F>,
22    /// Start address of pc.
23    pub pc_start: u32,
24    /// Initial memory image.
25    pub init_memory: MemoryImage<F>,
26    /// Starting + ending bounds for each function.
27    pub fn_bounds: FnBounds,
28}
29
30impl<F> VmExe<F> {
31    pub fn new(program: Program<F>) -> Self {
32        Self {
33            program,
34            pc_start: 0,
35            init_memory: BTreeMap::new(),
36            fn_bounds: Default::default(),
37        }
38    }
39    pub fn with_pc_start(mut self, pc_start: u32) -> Self {
40        self.pc_start = pc_start;
41        self
42    }
43    pub fn with_init_memory(mut self, init_memory: MemoryImage<F>) -> Self {
44        self.init_memory = init_memory;
45        self
46    }
47}
48
49impl<F: Field> From<Program<F>> for VmExe<F> {
50    fn from(program: Program<F>) -> Self {
51        Self::new(program)
52    }
53}
54
55#[derive(Debug, Default, Clone, Serialize, Deserialize)]
56pub struct FnBound {
57    pub start: u32,
58    pub end: u32,
59    pub name: String,
60}