bytecode evaluation
This commit is contained in:
parent
fb29955f7d
commit
8642dcdd6a
5 changed files with 528 additions and 31 deletions
|
@ -1,5 +1,5 @@
|
|||
|
||||
use crate::classfile::{ JavaClassFile, AttributeData };
|
||||
use crate::heap_area::ObjectReference;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum Value {
|
||||
|
@ -7,9 +7,9 @@ pub enum Value {
|
|||
Byte(u8),
|
||||
Char(u16),
|
||||
Short(u16),
|
||||
Int(u32),
|
||||
Float(u32),
|
||||
Reference(u32),
|
||||
Int(i32),
|
||||
Float(f32),
|
||||
Reference(ObjectReference),
|
||||
ReturnAddress(u32),
|
||||
Double0(u32),
|
||||
Double1(u32),
|
||||
|
@ -24,6 +24,12 @@ pub struct OperandStack {
|
|||
depth: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
PushError(String),
|
||||
LocalError(String),
|
||||
}
|
||||
|
||||
impl OperandStack {
|
||||
fn new(size: u16) -> Self {
|
||||
return OperandStack {
|
||||
|
@ -31,6 +37,165 @@ impl OperandStack {
|
|||
depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, value: Value) -> Result<(), Error> {
|
||||
if self.depth as usize == self.stack.len() {
|
||||
return Err(Error::PushError(format!("Trying to push onto full operand stack, capacity: {}, value: {:?}", self.depth, value)))
|
||||
}
|
||||
|
||||
self.stack[self.depth as usize] = value;
|
||||
self.depth += 1;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pop_boolean(&mut self, index: usize) -> Result<bool, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Boolean(b) => Ok(b),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Boolean but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_byte(&mut self, index: usize) -> Result<u8, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
match value {
|
||||
Value::Byte(b) => Ok(b),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Byte but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_char(&mut self, index: usize) -> Result<u16, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Char(c) => Ok(c),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Char but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_short(&mut self, index: usize) -> Result<u16, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Short(s) => Ok(s),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Short but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_int(&mut self, index: usize) -> Result<i32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Int(i) => Ok(i),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Int but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_float(&mut self, index: usize) -> Result<f32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Float(f) => Ok(f),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Float but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_reference(&mut self, index: usize) -> Result<ObjectReference, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Reference(o) => Ok(o),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Reference but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_returnaddress(&mut self, index: usize) -> Result<u32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::ReturnAddress(a) => Ok(a),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected ReturnAddress but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_double0(&mut self, index: usize) -> Result<u32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Double0(a) => Ok(a),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Double0 but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_double1(&mut self, index: usize) -> Result<u32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Double1(a) => Ok(a),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Double1 but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_long0(&mut self, index: usize) -> Result<u32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Long0(a) => Ok(a),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Long0 but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_long1(&mut self, index: usize) -> Result<u32, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let value = self.stack[absolute_index];
|
||||
self.depth -= 1;
|
||||
match value {
|
||||
Value::Long1(a) => Ok(a),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function operand stack, expected Long1 but found '{:?}'", index, value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_double(&mut self, index: usize) -> Result<f64, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let higher_bytes = self.stack[absolute_index];
|
||||
let lower_bytes = self.stack[absolute_index + 1];
|
||||
self.depth -= 2;
|
||||
match (higher_bytes, lower_bytes) {
|
||||
(Value::Double0(hi), Value::Double1(lo)) => {
|
||||
let v: u64 = ((hi as u64) << 32) | lo as u64;
|
||||
Ok(
|
||||
f64::from_bits(v)
|
||||
)
|
||||
},
|
||||
_ => Err(Error::LocalError(format!("Mismatched types at index {} of the function operand stack, expected (Double0, Double1) but found '{:?}'", index, (higher_bytes, lower_bytes))))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_long(&mut self, index: usize) -> Result<u64, Error> {
|
||||
let absolute_index = self.depth as usize - 1 - index;
|
||||
let higher_bytes = self.stack[absolute_index];
|
||||
let lower_bytes = self.stack[absolute_index + 1];
|
||||
self.depth -= 2;
|
||||
match (higher_bytes, lower_bytes) {
|
||||
(Value::Long0(hi), Value::Long1(lo)) => {
|
||||
Ok(((hi as u64) << 32) | lo as u64)
|
||||
},
|
||||
_ => Err(Error::LocalError(format!("Mismatched types at index {} of the function operand stack, expected (Long0, Long1) but found '{:?}'", index, (higher_bytes, lower_bytes))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -65,4 +230,25 @@ impl StackFrame {
|
|||
instruction_pointer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_local_int(&self, index: u16) -> Result<i32, Error> {
|
||||
let local = self.locals[index as usize];
|
||||
match local {
|
||||
Value::Int(i) => Ok(i),
|
||||
_ => Err(Error::LocalError(format!("Mismatched type at index {} of the function locals, expected Int but found '{:?}'", index, local)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store_local(&mut self, index: u16, value: Value) -> Result<(), Error> {
|
||||
let field = self.locals.get_mut(index as usize);
|
||||
match field {
|
||||
Some(f) => {
|
||||
*f = value;
|
||||
Ok(())
|
||||
},
|
||||
None => {
|
||||
Err(Error::LocalError(format!("Tried to get local at index {} when max_locals is {}", index, self.locals.len())))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue