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