bytecode evaluation
This commit is contained in:
parent
fb29955f7d
commit
8642dcdd6a
5 changed files with 528 additions and 31 deletions
272
src/jvm.rs
272
src/jvm.rs
|
@ -1,5 +1,6 @@
|
|||
use core::fmt::{Display, Formatter};
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::error::Error as ErrorTrait;
|
||||
|
||||
use crate::accessmasks::{ ClassAccessFlagMask, ClassAccessFlag, MethodAccessFlagMask, MethodAccessFlag};
|
||||
|
@ -9,12 +10,15 @@ use crate::classfile::{ JavaClassFile, MethodInfo, MethodDescriptor, AbstractTyp
|
|||
use crate::classstore;
|
||||
use crate::classstore::ClassStore;
|
||||
use crate::constantpool::{ ConstantPoolInfo, ConstantClassInfo, ConstantUtf8Info, ConstantMethodRefInfo, ConstantNameAndTypeInfo};
|
||||
use crate::stackframe::{ StackFrame, Value };
|
||||
use crate::heap_area::HeapArea;
|
||||
use crate::stackframe;
|
||||
use crate::stackframe::{ StackFrame, Value, OperandStack };
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
ClassStoreError(classstore::Error),
|
||||
ClassFileError(classfile::Error),
|
||||
StackFrameError(stackframe::Error, String),
|
||||
BadNameError(String),
|
||||
RunTimeError(String),
|
||||
OpcodeError(String),
|
||||
|
@ -45,8 +49,9 @@ impl Display for Error {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct JVM {
|
||||
class_store: ClassStore,
|
||||
stack_frames: Vec<StackFrame>,
|
||||
pub class_store: ClassStore,
|
||||
pub stack_frames: Vec<StackFrame>,
|
||||
pub heap_area: HeapArea,
|
||||
}
|
||||
|
||||
impl JVM {
|
||||
|
@ -54,6 +59,7 @@ impl JVM {
|
|||
return JVM {
|
||||
class_store: ClassStore::new(),
|
||||
stack_frames: Vec::new(),
|
||||
heap_area: HeapArea::new(usize::MAX),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,12 +136,30 @@ impl JVM {
|
|||
while self.stack_frames.len() != 0 {
|
||||
let jvm_op = self.bytecode_loop()?;
|
||||
match jvm_op {
|
||||
JVMCallbackOperation::PopFrame() => self.stack_frames.truncate(self.stack_frames.len() - 1),
|
||||
JVMCallbackOperation::PopFrame() => {
|
||||
self.stack_frames.truncate(self.stack_frames.len() - 1)
|
||||
},
|
||||
|
||||
JVMCallbackOperation::ReturnFrame(value) => {
|
||||
// Pop returning frame
|
||||
self.stack_frames.truncate(self.stack_frames.len() - 1);
|
||||
|
||||
let frame = {
|
||||
let frame_index = self.stack_frames.len() - 1;
|
||||
&mut self.stack_frames[frame_index]
|
||||
};
|
||||
let class = self.class_store.class_file_from_idx(frame.class_index).unwrap();
|
||||
let method = & class.methods[frame.method_index as usize];
|
||||
wrap_stackframe_error(class, method, self.stack_frames.last_mut().unwrap().operand_stack.push(value))?;
|
||||
}
|
||||
|
||||
JVMCallbackOperation::PushFrame(frame) => self.stack_frames.push(frame),
|
||||
|
||||
JVMCallbackOperation::LoadClass(name) => {
|
||||
self.class_store.load_class(&name)?;
|
||||
()
|
||||
},
|
||||
|
||||
JVMCallbackOperation::InitClass(name) => {
|
||||
self.init_class(*self.class_store.class_idx_from_name(&name).unwrap());
|
||||
}
|
||||
|
@ -237,19 +261,70 @@ impl JVM {
|
|||
)));
|
||||
}
|
||||
|
||||
let arguments = Vec::new();
|
||||
// TODO: Pass arguments
|
||||
let mut arguments = VecDeque::new();
|
||||
fill_arguments(class, method, &mut arguments, &callee_method_info.descriptor.argument_types, &mut frame.operand_stack)?;
|
||||
|
||||
let new_frame = StackFrame::new(
|
||||
callee_class_file,
|
||||
callee_class_index,
|
||||
callee_method_index as u16,
|
||||
&arguments.into_boxed_slice(),
|
||||
&arguments.make_contiguous(),
|
||||
);
|
||||
|
||||
return Ok(JVMCallbackOperation::PushFrame(new_frame));
|
||||
},
|
||||
|
||||
Instruction::LoadByteImmediate(byte) => {
|
||||
// sign extend into int
|
||||
let i8_int = i8::from_be_bytes([byte]);
|
||||
|
||||
let frame_result = frame.operand_stack.push(Value::Int(i8_int as i32));
|
||||
match frame_result {
|
||||
Ok(_) => (),
|
||||
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
|
||||
}
|
||||
},
|
||||
|
||||
Instruction::LoadLocalInt0() => {
|
||||
load_local_int(class, method, frame, 0)?;
|
||||
}
|
||||
Instruction::LoadLocalInt1() => {
|
||||
load_local_int(class, method, frame, 1)?;
|
||||
}
|
||||
Instruction::LoadLocalInt2() => {
|
||||
load_local_int(class, method, frame, 2)?;
|
||||
}
|
||||
Instruction::LoadLocalInt3() => {
|
||||
load_local_int(class, method, frame, 3)?;
|
||||
}
|
||||
|
||||
Instruction::MultiplyInt() => {
|
||||
let factor_1 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
|
||||
let factor_2 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
|
||||
wrap_stackframe_error(class, method, frame.operand_stack.push(Value::Int(factor_1 * factor_2)))?;
|
||||
}
|
||||
|
||||
Instruction::PushConstInt5() => {
|
||||
let frame_result = frame.operand_stack.push(Value::Int(5));
|
||||
match frame_result {
|
||||
Ok(_) => (),
|
||||
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
|
||||
}
|
||||
}
|
||||
|
||||
Instruction::ReturnInt() => {
|
||||
let expected_type = AbstractTypeDescription {
|
||||
array_level: 0,
|
||||
kind: AbstractTypeKind::Int(),
|
||||
};
|
||||
if method.descriptor.return_type != expected_type {
|
||||
return Err(Error::OpcodeError(format!("Found opcode '{:?}' on method returning '{:?}'", instruction, method.descriptor.return_type)))
|
||||
}
|
||||
|
||||
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
|
||||
|
||||
return Ok(JVMCallbackOperation::ReturnFrame(Value::Int(int)));
|
||||
}
|
||||
Instruction::ReturnVoid() => {
|
||||
let expected_type = AbstractTypeDescription {
|
||||
array_level: 0,
|
||||
|
@ -263,6 +338,27 @@ impl JVM {
|
|||
return Ok(JVMCallbackOperation::PopFrame());
|
||||
},
|
||||
|
||||
Instruction::StoreLocalInt0() => {
|
||||
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
|
||||
|
||||
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
|
||||
},
|
||||
Instruction::StoreLocalInt1() => {
|
||||
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(1))?;
|
||||
|
||||
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
|
||||
},
|
||||
Instruction::StoreLocalInt2() => {
|
||||
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(2))?;
|
||||
|
||||
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
|
||||
},
|
||||
Instruction::StoreLocalInt3() => {
|
||||
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(3))?;
|
||||
|
||||
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
|
||||
},
|
||||
|
||||
_ => {
|
||||
return Err(Error::RunTimeError(format!("Opcode not implemented yet: {:?}", instruction)))
|
||||
},
|
||||
|
@ -270,6 +366,7 @@ impl JVM {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Review this, maybe crash when there is no return?
|
||||
Ok(JVMCallbackOperation::PopFrame())
|
||||
}
|
||||
|
||||
|
@ -277,7 +374,168 @@ impl JVM {
|
|||
|
||||
enum JVMCallbackOperation {
|
||||
PopFrame(),
|
||||
ReturnFrame(Value),
|
||||
PushFrame(StackFrame),
|
||||
LoadClass(String),
|
||||
InitClass(String),
|
||||
}
|
||||
|
||||
fn load_local_int(class: &JavaClassFile, method: &MethodInfo, frame: &mut StackFrame, index: usize) -> Result<(), Error> {
|
||||
let frame_result = frame.load_local_int(index as u16);
|
||||
let local_int = match frame_result {
|
||||
Ok(i) => {
|
||||
i
|
||||
},
|
||||
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
|
||||
};
|
||||
|
||||
let frame_result = frame.operand_stack.push(Value::Int(local_int));
|
||||
match frame_result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut VecDeque<Value>, argument_types: &Box<[AbstractTypeDescription]>, stack: &mut OperandStack) -> Result<(), Error> {
|
||||
for argument_type in argument_types {
|
||||
if argument_type.array_level != 0 {
|
||||
// TODO: Type checking
|
||||
arguments.push_front(
|
||||
Value::Reference(wrap_stackframe_error(class, method, stack.pop_reference(0))?),
|
||||
)
|
||||
} else {
|
||||
match argument_type.kind {
|
||||
AbstractTypeKind::Void() => return Err(Error::RunTimeError("Functions cannot take arguments of type void".to_string())),
|
||||
// TODO: Add better description
|
||||
AbstractTypeKind::Byte() => {
|
||||
arguments.push_front(
|
||||
Value::Byte(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_byte(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Char() => {
|
||||
arguments.push_front(
|
||||
Value::Char(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_char(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Double() => {
|
||||
arguments.push_front(
|
||||
Value::Double1(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_double1(0)
|
||||
)?
|
||||
)
|
||||
);
|
||||
arguments.push_front(
|
||||
Value::Double0(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_double0(0)
|
||||
)?
|
||||
)
|
||||
);
|
||||
},
|
||||
AbstractTypeKind::Float() => {
|
||||
arguments.push_front(
|
||||
Value::Float(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_float(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Int() => {
|
||||
arguments.push_front(
|
||||
Value::Int(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_int(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Long() => {
|
||||
arguments.push_front(
|
||||
Value::Long1(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_long1(0)
|
||||
)?
|
||||
)
|
||||
);
|
||||
arguments.push_front(
|
||||
Value::Long0(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_long0(0)
|
||||
)?
|
||||
)
|
||||
);
|
||||
},
|
||||
AbstractTypeKind::Classname(ref name) => {
|
||||
// TODO: Type checking
|
||||
arguments.push_front(
|
||||
Value::Reference(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_reference(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Short() => {
|
||||
arguments.push_front(
|
||||
Value::Short(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_short(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
AbstractTypeKind::Boolean() => {
|
||||
arguments.push_front(
|
||||
Value::Boolean(
|
||||
wrap_stackframe_error(
|
||||
class,
|
||||
method,
|
||||
stack.pop_boolean(0)
|
||||
)?
|
||||
)
|
||||
)
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn wrap_stackframe_error<T>(class: &JavaClassFile, method: &MethodInfo, frame_result: Result<T, stackframe::Error>) -> Result<T, Error> {
|
||||
match frame_result {
|
||||
Ok(t) => Ok(t),
|
||||
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue