Skip to content

Commit

Permalink
Refactoring: stack
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Nov 9, 2024
1 parent a3242e2 commit 75e40a6
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 293 deletions.
32 changes: 16 additions & 16 deletions vm/src/execution_engine/ops_comparison_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
let stack_frame = stack_frames.last_mut().unwrap();
match code {
LCMP => {
let b = stack_frame.pop_i64();
let a = stack_frame.pop_i64();
let b: i64 = stack_frame.pop();
let a: i64 = stack_frame.pop();

if a > b {
stack_frame.push(1);
Expand All @@ -21,8 +21,8 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
trace!("LCMP -> {a} ? {b}");
}
FCMPL => {
let b = stack_frame.pop_f32();
let a = stack_frame.pop_f32();
let b: f32 = stack_frame.pop();
let a: f32 = stack_frame.pop();

let result = if a.is_nan() || b.is_nan() {
-1
Expand All @@ -40,8 +40,8 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
trace!("FCMPL -> {a} ? {b}");
}
FCMPG => {
let b = stack_frame.pop_f32();
let a = stack_frame.pop_f32();
let b: f32 = stack_frame.pop();
let a: f32 = stack_frame.pop();

let result = if a.is_nan() || b.is_nan() {
1
Expand All @@ -59,8 +59,8 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
trace!("FCMPG -> {a} ? {b}");
}
DCMPL => {
let b = f64::from_bits(stack_frame.pop_i64() as u64);
let a = f64::from_bits(stack_frame.pop_i64() as u64);
let b: f64 = stack_frame.pop();
let a: f64 = stack_frame.pop();

let result = if a.is_nan() || b.is_nan() {
-1
Expand All @@ -78,8 +78,8 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
trace!("DCMPL -> {a} ? {b}");
}
DCMPG => {
let b = f64::from_bits(stack_frame.pop_i64() as u64);
let a = f64::from_bits(stack_frame.pop_i64() as u64);
let b: f64 = stack_frame.pop();
let a: f64 = stack_frame.pop();

let result = if a.is_nan() || b.is_nan() {
1
Expand All @@ -97,37 +97,37 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er
trace!("DCMPG -> {a} ? {b}");
}
IFEQ => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value == 0 { offset } else { 3 });
trace!("IFEQ -> value={value}, offset={offset}");
}
IFNE => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value != 0 { offset } else { 3 });
trace!("IFNE -> value={value}, offset={offset}");
}
IFLT => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value < 0 { offset } else { 3 });
trace!("IFLT -> value={value}, offset={offset}");
}
IFGE => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value >= 0 { offset } else { 3 });
trace!("IFGE -> value={value}, offset={offset}");
}
IFGT => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value > 0 { offset } else { 3 });
trace!("IFGT -> value={value}, offset={offset}");
}
IFLE => {
let value = stack_frame.pop();
let value: i32 = stack_frame.pop();
let offset = stack_frame.get_two_bytes_ahead();
stack_frame.advance_pc(if value <= 0 { offset } else { 3 });
trace!("IFLE -> value={value}, offset={offset}");
Expand Down
32 changes: 16 additions & 16 deletions vm/src/execution_engine/ops_constant_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,77 @@ pub(crate) fn process(
let stack_frame = stack_frames.last_mut().unwrap();
match code {
ACONST_NULL => {
stack_frame.push(0);
stack_frame.push(0i32);
stack_frame.incr_pc();
trace!("ACONST_NULL");
}
ICONST_M1 => {
stack_frame.push(-1);
stack_frame.push(-1i32);
stack_frame.incr_pc();
trace!("ICONST_M1");
}
ICONST_0 => {
stack_frame.push(0);
stack_frame.push(0i32);
stack_frame.incr_pc();
trace!("ICONST_0");
}
ICONST_1 => {
stack_frame.push(1);
stack_frame.push(1i32);
stack_frame.incr_pc();
trace!("ICONST_1");
}
ICONST_2 => {
stack_frame.push(2);
stack_frame.push(2i32);
stack_frame.incr_pc();
trace!("ICONST_2");
}
ICONST_3 => {
stack_frame.push(3);
stack_frame.push(3i32);
stack_frame.incr_pc();
trace!("ICONST_3");
}
ICONST_4 => {
stack_frame.push(4);
stack_frame.push(4i32);
stack_frame.incr_pc();
trace!("ICONST_4");
}
ICONST_5 => {
stack_frame.push(5);
stack_frame.push(5i32);
stack_frame.incr_pc();
trace!("ICONST_5");
}
LCONST_0 => {
stack_frame.push_i64(0i64);
stack_frame.push(0i64);
stack_frame.incr_pc();
trace!("LCONST_0");
}
LCONST_1 => {
stack_frame.push_i64(1i64);
stack_frame.push(1i64);
stack_frame.incr_pc();
trace!("LCONST_1");
}
FCONST_0 => {
stack_frame.push_f32(0.0);
stack_frame.push(0.0f32);
stack_frame.incr_pc();
trace!("FCONST_0");
}
FCONST_1 => {
stack_frame.push_f32(1.0);
stack_frame.push(1.0f32);
stack_frame.incr_pc();
trace!("FCONST_1");
}
FCONST_2 => {
stack_frame.push_f32(2.0);
stack_frame.push(2.0f32);
stack_frame.incr_pc();
trace!("FCONST_2");
}
DCONST_0 => {
stack_frame.push_f64(0.0);
stack_frame.push(0.0f64);
stack_frame.incr_pc();
trace!("DCONST_0");
}
DCONST_1 => {
stack_frame.push_f64(1.0);
stack_frame.push(1.0f64);
stack_frame.incr_pc();
trace!("DCONST_1");
}
Expand Down Expand Up @@ -132,7 +132,7 @@ pub(crate) fn process(
method_area.resolve_ldc2_w(&current_class_name, cpoolindex)
})?;

stack_frame.push_i64(value);
stack_frame.push(value);

stack_frame.incr_pc();
trace!(
Expand Down
33 changes: 13 additions & 20 deletions vm/src/execution_engine/ops_control_processor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::error::Error;
use crate::execution_engine::opcode::*;
use crate::helper::i32toi64;
use crate::stack::stack_frame::StackFrame;
use tracing::trace;

Expand All @@ -16,7 +15,7 @@ pub(crate) fn process(
trace!("GOTO -> offset={offset}");
}
LOOKUPSWITCH => {
let key = stack_frame.pop();
let key: i32 = stack_frame.pop();
let instruction_pc = stack_frame.pc() as i16;
stack_frame.adjust_pc_to_4();

Expand Down Expand Up @@ -44,7 +43,7 @@ pub(crate) fn process(
trace!("LOOKUPSWITCH -> default_offset={default_offset}, npairs={npairs}");
}
TABLESWITCH => {
let index = stack_frame.pop();
let index: i32 = stack_frame.pop();
let instruction_pc = stack_frame.pc() as i16;
stack_frame.adjust_pc_to_4();

Expand All @@ -65,7 +64,7 @@ pub(crate) fn process(
trace!("TABLESWITCH -> default_offset={default_offset}, low={low}, high={high}");
}
IRETURN => {
let ret = stack_frame.pop();
let ret: i32 = stack_frame.pop();
stack_frames.pop();
let _frame = stack_frames
.last_mut()
Expand All @@ -74,22 +73,18 @@ pub(crate) fn process(
trace!("IRETURN -> ret={ret}");
}
LRETURN => {
let ret_high = stack_frame.pop();
let ret_low = stack_frame.pop();
let ret: i64 = stack_frame.pop();

stack_frames.pop();
let frame = stack_frames
.last_mut()
.ok_or(Error::new_execution("Error getting stack last value"))?;

frame.push(ret_low);
frame.push(ret_high);

let ret = i32toi64(ret_high, ret_low);
frame.push(ret);
trace!("LRETURN -> ret={ret}");
}
FRETURN => {
let ret = stack_frame.pop();
let ret: f32 = stack_frame.pop();
stack_frames.pop();
stack_frames
.last_mut()
Expand All @@ -98,21 +93,19 @@ pub(crate) fn process(
trace!("FRETURN -> ret={ret}");
}
DRETURN => {
let ret_high = stack_frame.pop();
let ret_low = stack_frame.pop();
let ret: f64 = stack_frame.pop();

stack_frames.pop();
let frame = stack_frames
.last_mut()
.ok_or(Error::new_execution("Error getting stack last value"))?;

frame.push(ret_low);
frame.push(ret_high);
frame.push(ret);

let ret = i32toi64(ret_high, ret_low);
trace!("DRETURN -> ret={ret}");
}
ARETURN => {
let objref = stack_frame.pop();
let objref: i32 = stack_frame.pop();

stack_frames.pop();
stack_frames
Expand All @@ -122,13 +115,13 @@ pub(crate) fn process(
trace!("ARETURN -> objref={objref}");
}
RETURN => {
trace!("RETURN -> stack_frame.locals={:?}", stack_frame.locals);
trace!("RETURN -> stack_frame.locals={:?}", stack_frame.locals());
let last_value = Some(
stack_frames
.last()
.ok_or(Error::new_execution("Error getting stack last value"))?
.locals
.clone(),
.locals()
.to_vec(),
);

stack_frames.pop();
Expand Down
Loading

0 comments on commit 75e40a6

Please sign in to comment.