From e9a3578c46da7e4e2062b0fdf92d00935cdb6d51 Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Tue, 19 Nov 2024 14:40:18 +0200 Subject: [PATCH] Refactoring: conversions ops --- .../ops_conversion_processor.rs | 141 ++++-------------- 1 file changed, 29 insertions(+), 112 deletions(-) diff --git a/vm/src/execution_engine/ops_conversion_processor.rs b/vm/src/execution_engine/ops_conversion_processor.rs index 15b20f4e..de1f1457 100644 --- a/vm/src/execution_engine/ops_conversion_processor.rs +++ b/vm/src/execution_engine/ops_conversion_processor.rs @@ -1,122 +1,26 @@ use crate::execution_engine::opcode::*; +use crate::stack::sack_value::StackValue; use crate::stack::stack_frame::StackFrame; +use std::fmt::Display; use tracing::trace; pub(crate) fn process(code: u8, stack_frames: &mut Vec) -> crate::error::Result<()> { let stack_frame = stack_frames.last_mut().unwrap(); match code { - I2L => { - let value: i32 = stack_frame.pop(); - let result = value as i64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2L -> {result}L"); - } - I2F => { - let value: i32 = stack_frame.pop(); - let result = value as f32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2F -> {result}F"); - } - I2D => { - let value: i32 = stack_frame.pop(); - let result = value as f64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2D -> {result}D"); - } - L2I => { - let value: i64 = stack_frame.pop(); - let result = value as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("L2I -> {result}I"); - } - L2F => { - let value: i64 = stack_frame.pop(); - let result = value as f32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("L2F -> {result}F"); - } - L2D => { - let value: i64 = stack_frame.pop(); - let result = value as f64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("L2D -> {result}D"); - } - F2I => { - let value: f32 = stack_frame.pop(); - let result = value as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("F2I -> {result}I"); - } - F2L => { - let value: f32 = stack_frame.pop(); - let result = value as i64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("F2L -> {result}L"); - } - F2D => { - let value: f32 = stack_frame.pop(); - let result = value as f64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("F2D -> {result}D"); - } - D2I => { - let value: f64 = stack_frame.pop(); - let result = value as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("D2I -> {result}I"); - } - D2L => { - let value: f64 = stack_frame.pop(); - let result = value as i64; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("D2L -> {result}L"); - } - I2B => { - let value: i32 = stack_frame.pop(); - let result = value as i8 as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2B -> {result}B"); - } - I2C => { - let value: i32 = stack_frame.pop(); - let result = value as u16 as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2C -> {result}C"); - } - I2S => { - let value: i32 = stack_frame.pop(); - let result = value as i16 as i32; - stack_frame.push(result); - - stack_frame.incr_pc(); - trace!("I2S -> {result}S"); - } + I2L => convert::(stack_frame, |from| from.into(), "I2L"), + I2F => convert::(stack_frame, |from| from as f32, "I2F"), + I2D => convert::(stack_frame, |from| from as f64, "I2D"), + L2I => convert::(stack_frame, |from| from as i32, "L2I"), + L2F => convert::(stack_frame, |from| from as f32, "L2F"), + L2D => convert::(stack_frame, |from| from as f64, "L2D"), + F2I => convert::(stack_frame, |from| from as i32, "F2I"), + F2L => convert::(stack_frame, |from| from as i64, "F2L"), + F2D => convert::(stack_frame, |from| from as f64, "F2D"), + D2I => convert::(stack_frame, |from| from as i32, "D2I"), + D2L => convert::(stack_frame, |from| from as i64, "D2L"), + I2B => convert::(stack_frame, |from| from as i8 as i32, "I2B"), + I2C => convert::(stack_frame, |from| from as u16 as i32, "I2C"), + I2S => convert::(stack_frame, |from| from as i16 as i32, "I2S"), _ => { return Err(crate::error::Error::new_execution(&format!( "Unknown conversion opcode: {}", @@ -127,3 +31,16 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec) -> crate::er Ok(()) } + +fn convert( + stack_frame: &mut StackFrame, + convertor: impl Fn(From) -> To, + name: &str, +) { + let from: From = stack_frame.pop(); + let to = convertor(from); + stack_frame.push(to); + + stack_frame.incr_pc(); + trace!("{name} -> {from}->{to}"); +}