Now with a hashmap for ops lookup

This commit is contained in:
2019-04-08 16:03:49 +02:00
parent 21b6fc4e64
commit 6ed58151ee
7 changed files with 149 additions and 119 deletions

View File

@@ -1,22 +1,22 @@
use std::io::{Read, Write};
use std::sync::{Mutex, Arc};
use std::sync::{Arc, Mutex};
use Result;
use block::Block;
use ops::{Operation, Args, num_to_op};
use ijvmreader::IJVMReader;
use binread::{BinRead, BinReadable};
use block::Block;
use frame::Frame;
use stack::Stack;
use ijvmreader::IJVMReader;
use ops::{num_to_op, Args, Operation};
use pool::Pool;
use stack::Stack;
use Result;
#[cfg(feature = "bonus:network")]
use netstack::NetStack;
#[cfg(feature = "bonus:heap")]
use heap::Heap;
#[cfg(feature = "bonus:network")]
use netstack::NetStack;
pub const MAGIC_HEADER:u32 = 0x1DEA_DFAD;
const ANTI_BS_SIZE:usize = 0xFF;
pub const MAGIC_HEADER: u32 = 0x1DEA_DFAD;
const ANTI_BS_SIZE: usize = 0xFF;
pub struct Machine {
pub wide: bool,
@@ -32,11 +32,10 @@ pub struct Machine {
pub stream_in: Box<Read + Send + Sync>,
pub stream_out: Arc<Mutex<Write + Send + Sync>>,
}
impl Machine {
pub fn new(pool: Pool, block: Block) -> Machine{
pub fn new(pool: Pool, block: Block) -> Machine {
Machine {
wide: false,
halted: false,
@@ -61,11 +60,11 @@ impl Machine {
}
let constants = match reader.read_block() {
Ok(a) => a,
Err(_) => return Err("Failed to read constants block")
Err(_) => return Err("Failed to read constants block"),
};
let text = match reader.read_block() {
Ok(block) => block,
Err(_) => return Err("Failed to read text block")
Err(_) => return Err("Failed to read text block"),
};
let pool = Pool::new(constants)?;
@@ -95,12 +94,12 @@ impl Machine {
} else {
func(self)
}
},
Ok(Operation::Invalid(a)) => {
println!("UNDEFINED OP: 0x{:X}", a);
}
Ok(Operation::Invalid) => {
println!("Reached invalid operation");
Err("Invalid op")
},
Err(str) => Err(str)
}
Err(str) => Err(str),
}
}
@@ -149,11 +148,11 @@ impl Machine {
}
let (name, args) = match num_to_op(opcode) {
Operation::Invalid(_) => return Err("Invalid operation"),
Operation::Invalid => return Err("Invalid operation"),
Operation::Op(name, _, args) => (name, args),
};
for arg in &args {
for arg in args {
let v = match arg {
Args::Byte => i32::from(self.block.read_i8()?),
Args::Short => i32::from(self.block.read_i16()?),
@@ -163,7 +162,7 @@ impl Machine {
} else {
i32::from(self.block.read_u8()?)
}
},
}
Args::Label => i32::from(self.block.read_i16()?),
Args::Constant => i32::from(self.block.read_u16()?),
};
@@ -174,14 +173,14 @@ impl Machine {
Ok((name.to_string(), params))
}
// pub fn get_stack_pointer(&self) -> usize {
// return self.frame.last().unwrap().stack.len();
// }
// pub fn get_stack_pointer(&self) -> usize {
// return self.frame.last().unwrap().stack.len();
// }
pub fn read_index(&mut self) -> Result<usize> {
if self.wide {
self.wide = false;
return Ok(self.block.read_u16()? as usize)
return Ok(self.block.read_u16()? as usize);
}
Ok(self.block.read_u8()? as usize)
}
@@ -193,4 +192,4 @@ impl Machine {
pub fn set_output(&mut self, outstream: Arc<Mutex<Write + Send + Sync>>) {
self.stream_out = outstream;
}
}
}