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
use std::{collections::HashMap, fmt};
use super::{Instruction, PseudoOpcode, UOffset};
pub type Instructions = Vec<Instruction>;
pub type LabelMap = HashMap<String, usize>;
#[derive(Default, PartialEq)]
pub struct Program {
pub instructions: Instructions,
pub origin: Option<UOffset>,
}
impl Program {
pub fn get(&self, index: usize) -> Option<Instruction> {
self.instructions.get(index).cloned()
}
pub fn set(&mut self, index: usize, value: Instruction) {
if index >= self.instructions.len() {
self.instructions.resize_with(index + 1, Default::default);
}
self.instructions[index] = value;
}
}
impl fmt::Debug for Program {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
writeln!(formatter, "{{")?;
writeln!(formatter, "origin: {:?},", self.origin)?;
let lines = self
.instructions
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>();
write!(formatter, "lines: {:#?},", lines)?;
writeln!(formatter, "}}")
}
}
impl fmt::Display for Program {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut lines = Vec::new();
if let Some(offset) = self.origin {
lines.push(format!("{:<8}{}", PseudoOpcode::Org, offset));
}
for instruction in self.instructions.iter() {
lines.push(instruction.to_string());
}
write!(formatter, "{}", lines.join("\n"))
}
}