use std::collections::HashMap;
+use syscalls::*;
+
pub struct Interpreter<'a> {
module: &'a IRModule,
data_stack: Vec<u64>,
}
}
+ fn process_syscall_result(&mut self, result: Result<usize, Errno>) {
+ match result {
+ Ok(result) => {
+ self.data_stack.push(result as u64);
+ self.data_stack.push(0);
+ }
+ Err(err) => {
+ self.data_stack.push(0);
+ self.data_stack.push(err.into_raw() as u64);
+ }
+ }
+ }
+
pub fn run(&mut self) {
let mut looking_for_endif = false;
loop {
}
},
IR::EndIf => {},
+ IR::Sys0 => {
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num) });
+ },
+ IR::Sys1 => {
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1) });
+ },
+ IR::Sys2 => {
+ let a2 = self.data_stack.pop().unwrap();
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1, a2) });
+ },
+ IR::Sys3 => {
+ let a3 = self.data_stack.pop().unwrap();
+ let a2 = self.data_stack.pop().unwrap();
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3) });
+ },
+ IR::Sys4 => {
+ let a4 = self.data_stack.pop().unwrap();
+ let a3 = self.data_stack.pop().unwrap();
+ let a2 = self.data_stack.pop().unwrap();
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4) });
+ },
+ IR::Sys5 => {
+ let a5 = self.data_stack.pop().unwrap();
+ let a4 = self.data_stack.pop().unwrap();
+ let a3 = self.data_stack.pop().unwrap();
+ let a2 = self.data_stack.pop().unwrap();
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4, a5) });
+ },
+ IR::Sys6 => {
+ let a6 = self.data_stack.pop().unwrap();
+ let a5 = self.data_stack.pop().unwrap();
+ let a4 = self.data_stack.pop().unwrap();
+ let a3 = self.data_stack.pop().unwrap();
+ let a2 = self.data_stack.pop().unwrap();
+ let a1 = self.data_stack.pop().unwrap();
+ let call_num = Sysno::from(self.data_stack.pop().unwrap() as i32);
+ self.process_syscall_result(unsafe { syscall!(call_num, a1, a2, a3, a4, a5, a6) });
+ },
_ => {
println!("Instruction not implemented.");
- }
+ },
}
}
self.instruction_pointer += 1;