1mod alu32;
2mod alu64;
3mod call;
4mod endian;
5mod helpers;
6mod jump;
7mod load;
8mod store;
9#[cfg(test)]
10mod test_utils;
11
12#[cfg(test)]
13pub use test_utils::{MockVm, make_test_instruction};
14use {
15 crate::{errors::ExecutionError, instruction::Instruction, opcode::Opcode},
16 alu32::{execute_alu32_imm, execute_alu32_reg, execute_neg32},
17 alu64::{execute_alu64_imm, execute_alu64_reg, execute_neg64},
18 load::{execute_lddw, execute_ldxb, execute_ldxdw, execute_ldxh, execute_ldxw},
19 store::{
20 execute_stb, execute_stdw, execute_sth, execute_stw, execute_stxb, execute_stxdw,
21 execute_stxh, execute_stxw,
22 },
23};
24pub use {
25 call::{execute_call_immediate, execute_call_register, execute_exit},
26 endian::execute_endian,
27 jump::{execute_jump, execute_jump_immediate, execute_jump_register},
28};
29
30pub type ExecutionResult<T> = Result<T, ExecutionError>;
31
32pub trait Vm {
33 fn get_register(&self, reg: usize) -> u64;
34 fn set_register(&mut self, reg: usize, value: u64);
35
36 fn get_pc(&self) -> usize;
37 fn set_pc(&mut self, pc: usize);
38 fn advance_pc(&mut self) {
39 self.set_pc(self.get_pc() + 1);
40 }
41
42 fn read_u8(&self, addr: u64) -> ExecutionResult<u8>;
43 fn read_u16(&self, addr: u64) -> ExecutionResult<u16>;
44 fn read_u32(&self, addr: u64) -> ExecutionResult<u32>;
45 fn read_u64(&self, addr: u64) -> ExecutionResult<u64>;
46
47 fn write_u8(&mut self, addr: u64, value: u8) -> ExecutionResult<()>;
48 fn write_u16(&mut self, addr: u64, value: u16) -> ExecutionResult<()>;
49 fn write_u32(&mut self, addr: u64, value: u32) -> ExecutionResult<()>;
50 fn write_u64(&mut self, addr: u64, value: u64) -> ExecutionResult<()>;
51
52 fn get_call_depth(&self) -> usize;
53 fn max_call_depth(&self) -> usize;
54 fn push_frame(
55 &mut self,
56 return_pc: usize,
57 saved_registers: [u64; 4],
58 saved_frame_pointer: u64,
59 ) -> ExecutionResult<()>;
60 fn pop_frame(&mut self) -> Option<(usize, [u64; 4], u64)>;
61
62 fn halt(&mut self, exit_code: u64);
63
64 fn get_stack_frame_size(&self) -> u64;
65
66 fn handle_syscall(&mut self, name: &str) -> ExecutionResult<u64>;
67
68 fn resolve_call_target(&self, target: u64) -> ExecutionResult<usize> {
69 Ok(target as usize)
70 }
71}
72
73pub fn execute_binary_immediate(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
74 match inst.opcode {
75 Opcode::Add64Imm
76 | Opcode::Sub64Imm
77 | Opcode::Mul64Imm
78 | Opcode::Div64Imm
79 | Opcode::Or64Imm
80 | Opcode::And64Imm
81 | Opcode::Lsh64Imm
82 | Opcode::Rsh64Imm
83 | Opcode::Mod64Imm
84 | Opcode::Xor64Imm
85 | Opcode::Mov64Imm
86 | Opcode::Arsh64Imm => execute_alu64_imm(vm, inst),
87 Opcode::Add32Imm
88 | Opcode::Sub32Imm
89 | Opcode::Mul32Imm
90 | Opcode::Div32Imm
91 | Opcode::Or32Imm
92 | Opcode::And32Imm
93 | Opcode::Lsh32Imm
94 | Opcode::Rsh32Imm
95 | Opcode::Mod32Imm
96 | Opcode::Xor32Imm
97 | Opcode::Mov32Imm
98 | Opcode::Arsh32Imm => execute_alu32_imm(vm, inst),
99 _ => Err(ExecutionError::InvalidInstruction),
100 }
101}
102
103pub fn execute_binary_register(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
104 match inst.opcode {
105 Opcode::Add64Reg
106 | Opcode::Sub64Reg
107 | Opcode::Mul64Reg
108 | Opcode::Div64Reg
109 | Opcode::Or64Reg
110 | Opcode::And64Reg
111 | Opcode::Lsh64Reg
112 | Opcode::Rsh64Reg
113 | Opcode::Mod64Reg
114 | Opcode::Xor64Reg
115 | Opcode::Mov64Reg
116 | Opcode::Arsh64Reg => execute_alu64_reg(vm, inst),
117 Opcode::Add32Reg
118 | Opcode::Sub32Reg
119 | Opcode::Mul32Reg
120 | Opcode::Div32Reg
121 | Opcode::Or32Reg
122 | Opcode::And32Reg
123 | Opcode::Lsh32Reg
124 | Opcode::Rsh32Reg
125 | Opcode::Mod32Reg
126 | Opcode::Xor32Reg
127 | Opcode::Mov32Reg
128 | Opcode::Arsh32Reg => execute_alu32_reg(vm, inst),
129 _ => Err(ExecutionError::InvalidInstruction),
130 }
131}
132
133pub fn execute_unary(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
134 match inst.opcode {
135 Opcode::Neg64 => execute_neg64(vm, inst),
136 Opcode::Neg32 => execute_neg32(vm, inst),
137 _ => Err(ExecutionError::InvalidInstruction),
138 }
139}
140
141pub fn execute_load_immediate(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
142 execute_lddw(vm, inst)
143}
144
145pub fn execute_load_memory(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
146 match inst.opcode {
147 Opcode::Ldxb => execute_ldxb(vm, inst),
148 Opcode::Ldxh => execute_ldxh(vm, inst),
149 Opcode::Ldxw => execute_ldxw(vm, inst),
150 Opcode::Ldxdw => execute_ldxdw(vm, inst),
151 _ => Err(ExecutionError::InvalidInstruction),
152 }
153}
154
155pub fn execute_store_immediate(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
156 match inst.opcode {
157 Opcode::Stb => execute_stb(vm, inst),
158 Opcode::Sth => execute_sth(vm, inst),
159 Opcode::Stw => execute_stw(vm, inst),
160 Opcode::Stdw => execute_stdw(vm, inst),
161 _ => Err(ExecutionError::InvalidInstruction),
162 }
163}
164
165pub fn execute_store_register(vm: &mut dyn Vm, inst: &Instruction) -> ExecutionResult<()> {
166 match inst.opcode {
167 Opcode::Stxb => execute_stxb(vm, inst),
168 Opcode::Stxh => execute_stxh(vm, inst),
169 Opcode::Stxw => execute_stxw(vm, inst),
170 Opcode::Stxdw => execute_stxdw(vm, inst),
171 _ => Err(ExecutionError::InvalidInstruction),
172 }
173}