1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::vm::errors::trace_errors::TraceError;
use crate::{types::relocatable::Relocatable, vm::errors::memory_errors::MemoryError};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq)]
pub struct TraceEntry {
pub pc: Relocatable,
pub ap: Relocatable,
pub fp: Relocatable,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RelocatedTraceEntry {
pub ap: usize,
pub fp: usize,
pub pc: usize,
}
pub fn relocate_trace_register(
value: &Relocatable,
relocation_table: &Vec<usize>,
) -> Result<usize, TraceError> {
let segment_index: usize = value.segment_index.try_into().map_err(|_| {
TraceError::MemoryError(MemoryError::AddressInTemporarySegment(value.segment_index))
})?;
if relocation_table.len() <= segment_index {
return Err(TraceError::NoRelocationFound);
}
Ok(relocation_table[segment_index] + value.offset)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relocate_relocatable_value() {
let value = Relocatable {
segment_index: 2,
offset: 7,
};
let relocation_table = vec![1, 2, 5];
assert_eq!(
relocate_trace_register(&value, &relocation_table).unwrap(),
12
);
}
#[test]
fn relocate_relocatable_value_no_relocation() {
let value = Relocatable {
segment_index: 2,
offset: 7,
};
let relocation_table = vec![1, 2];
let error = relocate_trace_register(&value, &relocation_table);
assert_eq!(error, Err(TraceError::NoRelocationFound));
assert_eq!(
error.unwrap_err().to_string(),
"No relocation found for this segment"
);
}
#[test]
fn relocate_temp_segment_address() {
let value = Relocatable {
segment_index: -2,
offset: 7,
};
let error = relocate_trace_register(&value, &Vec::new());
assert_eq!(
error,
Err(TraceError::MemoryError(
MemoryError::AddressInTemporarySegment(value.segment_index)
))
);
}
}