use std::ops::{Add, Div, Mul, Sub};
use crate::{
bytecode::{Opcode, bytes_to_i64, bytes_to_u64},
const_table::ConstTable,
stack::Stack,
};
pub struct Vm<'c> {
stack: Stack,
code: &'c [u8],
pc: usize,
consts: &'c ConstTable,
}
#[inline(always)]
fn op_addi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_int(i).wrapping_add(stack.get_local_int(k));
stack.set_local_int(r, val);
}
#[inline(always)]
fn op_addf(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_float(i).add(stack.get_local_float(k));
stack.set_local_float(r, val);
}
#[inline(always)]
fn op_subi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_int(i).wrapping_sub(stack.get_local_int(k));
stack.set_local_int(r, val);
}
#[inline(always)]
fn op_subf(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_float(i).sub(stack.get_local_float(k));
stack.set_local_float(r, val);
}
#[inline(always)]
fn op_muli(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_int(i).mul(stack.get_local_int(k));
stack.set_local_int(r, val);
}
#[inline(always)]
fn op_mulf(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_float(i).mul(stack.get_local_float(k));
stack.set_local_float(r, val);
}
#[inline(always)]
fn op_divi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_int(i).div(stack.get_local_int(k));
stack.set_local_int(r, val);
}
#[inline(always)]
fn op_divf(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_float(i).div(stack.get_local_float(k));
stack.set_local_float(r, val);
}
#[inline(always)]
fn op_modi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_int(i) % (stack.get_local_int(k));
stack.set_local_int(r, val);
}
#[inline(always)]
fn op_modf(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = instr[2] as usize;
let k = instr[3] as usize;
let val = stack.get_local_float(i) % (stack.get_local_float(k));
stack.set_local_float(r, val);
}
#[inline(always)]
fn op_halt(stack: &mut Stack, _: &mut usize, _: &[u8; 8]) {
#[cfg(debug_assertions)]
{
eprintln!("HALTING.");
eprintln!("{}", stack);
}
std::process::exit(0);
}
#[inline(always)]
fn op_loadi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = bytes_to_i64(&instr[2..8]);
stack.set_local_int(r, i.into());
}
#[inline(always)]
fn op_loadci(stack: &mut Stack, _pc: &mut usize, consts: &ConstTable, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = bytes_to_u64(&instr[2..8]);
let value = consts.ints.get(i as usize).expect("Constant not found");
stack.set_local_int(r, *value);
}
#[inline(always)]
fn op_loadcf(stack: &mut Stack, _pc: &mut usize, consts: &ConstTable, instr: &[u8; 8]) {
let r = instr[1] as usize;
let i = bytes_to_u64(&instr[2..8]);
let value = consts.floats.get(i as usize).expect("Constant not found");
stack.set_local_float(r, *value);
}
#[inline(always)]
fn op_pushi(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let val = bytes_to_i64(&instr[1..8]);
stack.push_local_int(val);
}
#[inline(always)]
fn op_inci(stack: &mut Stack, _pc: &mut usize, instr: &[u8; 8]) {
let idx = instr[1] as usize;
stack.set_local_int(idx, stack.get_local_int(idx) + 1);
}
#[inline(always)]
fn op_jmp(_stack: &mut Stack, pc: &mut usize, instr: &[u8; 8]) {
let offset = bytes_to_i64(&instr[1..8]);
let offset_isize: isize = (i64::from(offset) * 8)
.try_into()
.expect("Jump offset does not fit in isize");
*pc = pc
.checked_add_signed(offset_isize)
.expect("Jump out of bounds") as usize;
}
#[inline(always)]
fn op_jmp_eqi(stack: &mut Stack, pc: &mut usize, instr: &[u8; 8]) {
let (i, k) = (instr[1] as usize, instr[2] as usize);
let lhs = stack.get_local_int(i);
let rhs = stack.get_local_int(k);
if lhs == rhs {
let offset = bytes_to_i64(&instr[3..8]);
let offset_isize: isize = (offset * 8)
.try_into()
.expect("Jump offset does not fit in isize");
*pc = pc
.checked_add_signed(offset_isize)
.expect("Jump out of bounds") as usize;
} else {
*pc += 8;
}
}
#[inline(always)]
fn op_alloc(stack: &mut Stack, pc: &mut usize, instr: &[u8; 8]) {
let num_locals = bytes_to_u64(&instr[1..8]) as usize;
stack.push_frame_with_locals(0, 0, num_locals);
}
#[inline(always)]
fn op_jmp_nei(stack: &mut Stack, pc: &mut usize, instr: &[u8; 8]) {
let (i, k) = (instr[1] as usize, instr[2] as usize);
let lhs = stack.get_local_int(i);
let rhs = stack.get_local_int(k);
if lhs != rhs {
let offset = bytes_to_i64(&instr[3..8]);
let offset_isize: isize = (offset * 8)
.try_into()
.expect("Jump offset does not fit in isize");
*pc = pc
.checked_add_signed(offset_isize)
.expect("Jump out of bounds") as usize;
} else {
*pc += 8;
}
}
impl<'c> Vm<'c> {
pub fn new(code: &'c [u8], consts: &'c ConstTable) -> Self {
Self {
stack: Stack::new(),
code,
pc: 0,
consts,
}
}
pub fn run(&mut self) {
loop {
let op = self.code[self.pc];
let opcode: Opcode = unsafe { std::mem::transmute(op) };
let instr: &[u8; 8] = unsafe {
self.code
.get_unchecked(self.pc..self.pc + 8)
.try_into()
.unwrap_unchecked()
};
match opcode {
Opcode::Halt => {
op_halt(&mut self.stack, &mut self.pc, instr);
}
Opcode::Loadi => {
op_loadi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::LoadCi => {
op_loadci(&mut self.stack, &mut self.pc, &self.consts, instr);
self.pc += 8;
}
Opcode::LoadCf => {
op_loadcf(&mut self.stack, &mut self.pc, &self.consts, instr);
self.pc += 8;
}
Opcode::Pushi => {
op_pushi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Addi => {
op_addi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Subi => {
op_subi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Muli => {
op_muli(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Divi => {
op_divi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Modi => {
op_modi(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Inci => {
op_inci(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Addf => {
op_addf(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Subf => {
op_subf(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Mulf => {
op_mulf(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Divf => {
op_divf(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Modf => {
op_modf(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
Opcode::Call => { }
Opcode::Jmp => {
op_jmp(&mut self.stack, &mut self.pc, instr);
}
Opcode::JmpEq => {
op_jmp_eqi(&mut self.stack, &mut self.pc, instr);
}
Opcode::JmpNe => {
op_jmp_nei(&mut self.stack, &mut self.pc, instr);
}
Opcode::Alloc => {
op_alloc(&mut self.stack, &mut self.pc, instr);
self.pc += 8;
}
}
}
}
}
#[cfg(test)]
mod tests {
use ux::{i48, i56, u48};
use super::*;
use crate::bytecode::{self};
#[test]
fn test_op_jmp_eq() {
let mut stack = Stack::new();
stack.push_frame_with_locals(0, 0, 3);
stack.set_local_uint(0, u48::new(1).into());
stack.set_local_uint(1, u48::new(1).into());
let mut pc = 0;
let instr = bytecode::PrettyOp::JmpEq {
i: 0,
k: 1,
offset: 2.into(),
}
.into_slice();
op_jmp_eqi(&mut stack, &mut pc, &instr);
assert_eq!(pc, 2 * 8);
let mut stack = Stack::new();
stack.push_frame_with_locals(0, 0, 3);
stack.set_local_uint(0, u48::new(1).into());
stack.set_local_uint(1, u48::new(2).into());
let mut pc = 0;
let instr = bytecode::PrettyOp::JmpEq {
i: 0,
k: 1,
offset: 2.into(),
}
.into_slice();
op_jmp_eqi(&mut stack, &mut pc, &instr);
assert_eq!(pc, 8);
}
#[test]
fn test_op_jmp_positive_and_negative() {
use crate::bytecode::PrettyOp;
let mut stack = Stack::new();
stack.push_frame_with_locals(0, 0, 1);
let mut pc;
for offset in 1..5 {
pc = 8; let instr = PrettyOp::Jmp {
offset: i56::new(offset),
}
.into_slice();
op_jmp(&mut stack, &mut pc, &instr);
assert_eq!(
pc,
8 + (offset as usize) * 8,
"Failed positive jump for offset {}",
offset
);
}
for offset in 1..5 {
pc = 40; let instr = PrettyOp::Jmp {
offset: i56::new(-(offset as i64)),
}
.into_slice();
op_jmp(&mut stack, &mut pc, &instr);
assert_eq!(
pc,
40 - (offset as usize) * 8,
"Failed negative jump for offset -{}",
offset
);
}
}
#[test]
fn test_op_loadi() {
for n in [0, i48::MAX.into(), 1234] {
let mut stack = Stack::new();
stack.push_frame_with_locals(0, 0, 1);
let mut pc = 0;
let instr = bytecode::PrettyOp::Loadi {
i: 0,
val: i48::new(n),
}
.into_slice();
op_loadi(&mut stack, &mut pc, &instr);
assert_eq!(stack.get_local_int(0), n);
}
}
}