Code cleanup
This commit is contained in:
parent
c1eca63f5a
commit
30fe5036d4
7 changed files with 691 additions and 158 deletions
61
src/stackframe.rs
Normal file
61
src/stackframe.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue