Skip to main content

cairo_vm/vm/trace/
mod.rs

1pub mod trace_entry {
2    use serde::{Deserialize, Serialize};
3
4    use crate::{
5        types::relocatable::Relocatable,
6        vm::errors::{memory_errors::MemoryError, trace_errors::TraceError},
7    };
8
9    ///A trace entry for every instruction that was executed.
10    ///Holds the register values before the instruction was executed.
11    ///Register values for ap & fp are represented as their offsets, as their indexes will always be 1
12    #[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
13    pub struct TraceEntry {
14        pub pc: Relocatable,
15        pub ap: usize,
16        pub fp: usize,
17    }
18
19    /// A trace entry for every instruction that was executed.
20    /// Holds the register values before the instruction was executed, after going through the relocation process
21    #[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
22    pub struct RelocatedTraceEntry {
23        pub pc: usize,
24        pub ap: usize,
25        pub fp: usize,
26    }
27
28    pub fn relocate_trace_register(
29        value: Relocatable,
30        relocation_table: &[usize],
31    ) -> Result<usize, TraceError> {
32        let segment_index: usize = value.segment_index.try_into().map_err(|_| {
33            TraceError::MemoryError(MemoryError::AddressInTemporarySegment(value.segment_index))
34        })?;
35
36        if relocation_table.len() <= segment_index {
37            return Err(TraceError::NoRelocationFound);
38        }
39        Ok(relocation_table[segment_index] + value.offset)
40    }
41}