Skip to content

Commit

Permalink
Refactoring: conversions ops
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Nov 19, 2024
1 parent b021e49 commit e9a3578
Showing 1 changed file with 29 additions and 112 deletions.
141 changes: 29 additions & 112 deletions vm/src/execution_engine/ops_conversion_processor.rs
Original file line number Diff line number Diff line change
@@ -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<StackFrame>) -> 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::<i32, i64>(stack_frame, |from| from.into(), "I2L"),
I2F => convert::<i32, f32>(stack_frame, |from| from as f32, "I2F"),
I2D => convert::<i32, f64>(stack_frame, |from| from as f64, "I2D"),
L2I => convert::<i64, i32>(stack_frame, |from| from as i32, "L2I"),
L2F => convert::<i64, f32>(stack_frame, |from| from as f32, "L2F"),
L2D => convert::<i64, f64>(stack_frame, |from| from as f64, "L2D"),
F2I => convert::<f32, i32>(stack_frame, |from| from as i32, "F2I"),
F2L => convert::<f32, i64>(stack_frame, |from| from as i64, "F2L"),
F2D => convert::<f32, f64>(stack_frame, |from| from as f64, "F2D"),
D2I => convert::<f64, i32>(stack_frame, |from| from as i32, "D2I"),
D2L => convert::<f64, i64>(stack_frame, |from| from as i64, "D2L"),
I2B => convert::<i32, i32>(stack_frame, |from| from as i8 as i32, "I2B"),
I2C => convert::<i32, i32>(stack_frame, |from| from as u16 as i32, "I2C"),
I2S => convert::<i32, i32>(stack_frame, |from| from as i16 as i32, "I2S"),
_ => {
return Err(crate::error::Error::new_execution(&format!(
"Unknown conversion opcode: {}",
Expand All @@ -127,3 +31,16 @@ pub(crate) fn process(code: u8, stack_frames: &mut Vec<StackFrame>) -> crate::er

Ok(())
}

fn convert<From: StackValue + Copy + Display, To: StackValue + Copy + Display>(
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}");
}

0 comments on commit e9a3578

Please sign in to comment.