jvm/src/stackframe.rs

62 lines
1.4 KiB
Rust
Raw Normal View History

2024-08-30 15:33:54 +02:00
use crate::classfile::{ JavaClassFile, AttributeData };
#[derive(Copy, Clone, Debug)]
pub enum LocalVariable {
Boolean(bool),
Byte(u8),
Char(u16),
Short(u16),
Int(u32),
Float(u32),
Reference(u32),
ReturnAddress(u32),
Double0(u32),
Double1(u32),
Long0(u32),
Long1(u32),
Empty(),
}
#[derive(Debug)]
pub struct OperandStack {
stack: Box<[LocalVariable]>,
depth: u16,
}
impl OperandStack {
fn new(size: u16) -> Self {
return OperandStack {
stack: vec![LocalVariable::Empty(); size.into()].into_boxed_slice(),
depth: 0,
}
}
}
#[derive(Debug)]
pub struct StackFrame {
locals: Box<[LocalVariable]>,
operand_stack: OperandStack,
class_id: usize,
method_index: u16,
instruction_pointer: u32,
}
impl StackFrame {
pub fn new(classfile: &JavaClassFile, class_id: usize, method_index: u16) -> Self {
let method_info = &classfile.methods[method_index as usize];
let code_data = match &method_info.attributes[method_info.code_attribute_index].data {
AttributeData::Code(data) => data,
_ => unreachable!(),
};
StackFrame {
locals: vec![LocalVariable::Empty(); code_data.max_locals.into()].into_boxed_slice(),
operand_stack: OperandStack::new(code_data.max_stack),
class_id,
method_index,
instruction_pointer: 0,
}
}
}