//! Welcome to the land of The Great Dispatch Loop //! //! Have fun use hbbytecode::RoundingMode; use { super::{ bmc::BlockCopier, mem::Memory, value::{Value, ValueVariant}, Vm, VmRunError, VmRunOk, }, crate::{mem::Address, value::CheckedDivRem}, core::{cmp::Ordering, ops}, hbbytecode::{ OpsN, OpsO, OpsP, OpsRB, OpsRD, OpsRH, OpsRR, OpsRRAH, OpsRRB, OpsRRD, OpsRRH, OpsRRO, OpsRROH, OpsRRP, OpsRRPH, OpsRRR, OpsRRRR, OpsRRW, OpsRW, }, }; macro_rules! handler { ($self:expr, |$ty:ident ($($ident:pat),* $(,)?)| $expr:expr) => {{ let $ty($($ident),*) = $self.decode::<$ty>(); #[allow(clippy::no_effect)] let e = $expr; $self.bump_pc::<$ty, true>(); e }}; } impl Vm where Mem: Memory, { /// Execute program /// /// Program can return [`VmRunError`] if a trap handling failed #[cfg_attr(feature = "nightly", repr(align(4096)))] pub fn run(&mut self) -> Result { use hbbytecode::opcode::*; loop { // Big match // // Contribution guide: // - Zero register shall never be overwitten. It's value has to always be 0. // - Prefer `Self::read_reg` and `Self::write_reg` functions // - Extract parameters using `param!` macro // - Prioritise speed over code size // - Memory is cheap, CPUs not that much // - Do not heap allocate at any cost // - Yes, user-provided trap handler may allocate, // but that is not our »fault«. // - Unsafe is kinda must, but be sure you have validated everything // - Your contributions have to pass sanitizers and Miri // - Strictly follow the spec // - The spec does not specify how you perform actions, in what order, // just that the observable effects have to be performed in order and // correctly. // - Yes, we assume you run 64 bit CPU. Else ?conradluget a better CPU // sorry 8 bit fans, HBVM won't run on your Speccy :( unsafe { match self.memory.prog_read::(self.pc as _) { UN => { self.bump_pc::(); return Err(VmRunError::Unreachable); } TX => { self.bump_pc::(); return Ok(VmRunOk::End); } NOP => handler!(self, |OpsN()| ()), ADD8 => self.binary_op(u8::wrapping_add), ADD16 => self.binary_op(u16::wrapping_add), ADD32 => self.binary_op(u32::wrapping_add), ADD64 => self.binary_op(u64::wrapping_add), SUB8 => self.binary_op(u8::wrapping_sub), SUB16 => self.binary_op(u16::wrapping_sub), SUB32 => self.binary_op(u32::wrapping_sub), SUB64 => self.binary_op(u64::wrapping_sub), MUL8 => self.binary_op(u8::wrapping_mul), MUL16 => self.binary_op(u16::wrapping_mul), MUL32 => self.binary_op(u32::wrapping_mul), MUL64 => self.binary_op(u64::wrapping_mul), AND => self.binary_op::(ops::BitAnd::bitand), OR => self.binary_op::(ops::BitOr::bitor), XOR => self.binary_op::(ops::BitXor::bitxor), SLU8 => self.binary_op(|l, r| u8::wrapping_shl(l, r as u32)), SLU16 => self.binary_op(|l, r| u16::wrapping_shl(l, r as u32)), SLU32 => self.binary_op(u32::wrapping_shl), SLU64 => self.binary_op(|l, r| u64::wrapping_shl(l, r as u32)), SRU8 => self.binary_op(|l, r| u8::wrapping_shr(l, r as u32)), SRU16 => self.binary_op(|l, r| u16::wrapping_shr(l, r as u32)), SRU32 => self.binary_op(u32::wrapping_shr), SRS8 => self.binary_op(|l: i8, r| i8::wrapping_shl(l, r as u32)), SRS16 => self.binary_op(|l: i16, r| i16::wrapping_shl(l, r as u32)), SRS32 => self.binary_op(|l: i32, r| i32::wrapping_shl(l, r as u32)), SRS64 => self.binary_op(|l: i64, r| i64::wrapping_shl(l, r as u32)), CMP => handler!(self, |OpsRRR(tg, a0, a1)| { // Compare a0 <=> a1 // < → 0 // > → 1 // = → 2 self.write_reg( tg, self.read_reg(a0) .cast::() .cmp(&self.read_reg(a1).cast::()) as i64 + 1, ); }), CMPU => handler!(self, |OpsRRR(tg, a0, a1)| { // Unsigned comparsion self.write_reg( tg, self.read_reg(a0) .cast::() .cmp(&self.read_reg(a1).cast::()) as i64 + 1, ); }), DIRU8 => self.dir::(), DIRU16 => self.dir::(), DIRU32 => self.dir::(), DIRU64 => self.dir::(), DIRS8 => self.dir::(), DIRS16 => self.dir::(), DIRS32 => self.dir::(), DIRS64 => self.dir::(), NEG => handler!(self, |OpsRR(tg, a0)| { // Bit negation self.write_reg(tg, !self.read_reg(a0).cast::()) }), NOT => handler!(self, |OpsRR(tg, a0)| { // Logical negation self.write_reg(tg, u64::from(self.read_reg(a0).cast::() == 0)); }), SXT8 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), SXT16 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), SXT32 => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0).cast::() as i64) }), ADDI8 => self.binary_op_imm(u8::wrapping_add), ADDI16 => self.binary_op_imm(u16::wrapping_add), ADDI32 => self.binary_op_imm(u32::wrapping_add), ADDI64 => self.binary_op_imm(u64::wrapping_add), MULI8 => self.binary_op_imm(u8::wrapping_sub), MULI16 => self.binary_op_imm(u16::wrapping_sub), MULI32 => self.binary_op_imm(u32::wrapping_sub), MULI64 => self.binary_op_imm(u64::wrapping_sub), ANDI => self.binary_op_imm::(ops::BitAnd::bitand), ORI => self.binary_op_imm::(ops::BitOr::bitor), XORI => self.binary_op_imm::(ops::BitXor::bitxor), SLUI8 => self.binary_op_ims::(ops::Shl::shl), SLUI16 => self.binary_op_ims::(ops::Shl::shl), SLUI32 => self.binary_op_ims::(ops::Shl::shl), SLUI64 => self.binary_op_ims::(ops::Shl::shl), SRUI8 => self.binary_op_ims::(ops::Shr::shr), SRUI16 => self.binary_op_ims::(ops::Shr::shr), SRUI32 => self.binary_op_ims::(ops::Shr::shr), SRUI64 => self.binary_op_ims::(ops::Shr::shr), SRSI8 => self.binary_op_ims::(ops::Shr::shr), SRSI16 => self.binary_op_ims::(ops::Shr::shr), SRSI32 => self.binary_op_ims::(ops::Shr::shr), SRSI64 => self.binary_op_ims::(ops::Shr::shr), CMPI => handler!(self, |OpsRRD(tg, a0, imm)| { self.write_reg( tg, self.read_reg(a0) .cast::() .cmp(&Value::from(imm).cast::()) as i64, ); }), CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.write_reg(tg, self.read_reg(a0).cast::().cmp(&imm) as i64); }), CP => handler!(self, |OpsRR(tg, a0)| { self.write_reg(tg, self.read_reg(a0)); }), SWA => handler!(self, |OpsRR(r0, r1)| { // Swap registers match (r0, r1) { (0, 0) => (), (dst, 0) | (0, dst) => self.write_reg(dst, 0_u64), (r0, r1) => { core::ptr::swap( self.registers.get_unchecked_mut(usize::from(r0)), self.registers.get_unchecked_mut(usize::from(r1)), ); } } }), LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)), LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)), LI32 => handler!(self, |OpsRW(tg, imm)| self.write_reg(tg, imm)), LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)), LRA => handler!(self, |OpsRRO(tg, reg, imm)| { self.write_reg( tg, (self.pc + self.read_reg(reg).cast::() + imm + 3_u16).get(), ); }), LD => handler!(self, |OpsRRAH(dst, base, off, count)| { // Load. If loading more than register size, continue on adjecent registers self.load(dst, base, off, count)?; }), ST => handler!(self, |OpsRRAH(dst, base, off, count)| { // Store. Same rules apply as to LD self.store(dst, base, off, count)?; }), LDR => handler!(self, |OpsRROH(dst, base, off, count)| { self.load( dst, base, u64::from(off).wrapping_add((self.pc + 3_u64).get()), count, )?; }), STR => handler!(self, |OpsRROH(dst, base, off, count)| { self.store( dst, base, u64::from(off).wrapping_add((self.pc + 3_u64).get()), count, )?; }), BMC => { // Block memory copy match if let Some(copier) = &mut self.copier { // There is some copier, poll. copier.poll(&mut self.memory) } else { // There is none, make one! let OpsRRH(src, dst, count) = self.decode(); self.copier = Some(BlockCopier::new( Address::new(self.read_reg(src).cast()), Address::new(self.read_reg(dst).cast()), count as _, )); self.copier .as_mut() .unwrap_unchecked() // SAFETY: We just assigned there .poll(&mut self.memory) } { // We are done, shift program counter core::task::Poll::Ready(Ok(())) => { self.copier = None; self.bump_pc::(); } // Error, shift program counter (for consistency) // and yield error core::task::Poll::Ready(Err(e)) => { return Err(e.into()); } // Not done yet, proceed to next cycle core::task::Poll::Pending => (), } } BRC => handler!(self, |OpsRRB(src, dst, count)| { // Block register copy if src.checked_add(count).is_none() || dst.checked_add(count).is_none() { return Err(VmRunError::RegOutOfBounds); } core::ptr::copy( self.registers.get_unchecked(usize::from(src)), self.registers.get_unchecked_mut(usize::from(dst)), usize::from(count), ); }), JMP => handler!(self, |OpsO(off)| self.pc = self.pc.wrapping_add(off)), JAL => handler!(self, |OpsRRW(save, reg, offset)| { // Jump and link. Save PC after this instruction to // specified register and jump to reg + relative offset. self.write_reg(save, self.pc.get()); self.pc = self .pc .wrapping_add(self.read_reg(reg).cast::()) .wrapping_add(offset); }), JALA => handler!(self, |OpsRRW(save, reg, offset)| { // Jump and link. Save PC after this instruction to // specified register and jump to reg self.write_reg(save, self.pc.get()); self.pc = Address::new( self.read_reg(reg).cast::().wrapping_add(offset.into()), ); }), // Conditional jumps, jump only to immediates JEQ => self.cond_jmp::(Ordering::Equal), JNE => handler!(self, |OpsRRP(a0, a1, ja)| { if self.read_reg(a0).cast::() != self.read_reg(a1).cast::() { self.pc = Address::new( ((self.pc.get() as i64).wrapping_add(ja as i64)) as u64, ) } }), JLT => self.cond_jmp::(Ordering::Less), JGT => self.cond_jmp::(Ordering::Greater), JLTU => self.cond_jmp::(Ordering::Less), JGTU => self.cond_jmp::(Ordering::Greater), ECA => { // So we don't get timer interrupt after ECALL if TIMER_QUOTIENT != 0 { self.timer = self.timer.wrapping_add(1); } self.bump_pc::(); return Ok(VmRunOk::Ecall); } EBP => { self.bump_pc::(); return Ok(VmRunOk::Breakpoint); } FADD32 => self.binary_op::(ops::Add::add), FADD64 => self.binary_op::(ops::Add::add), FSUB32 => self.binary_op::(ops::Sub::sub), FSUB64 => self.binary_op::(ops::Sub::sub), FMUL32 => self.binary_op::(ops::Mul::mul), FMUL64 => self.binary_op::(ops::Mul::mul), FDIV32 => self.binary_op::(ops::Div::div), FDIV64 => self.binary_op::(ops::Div::div), FMA32 => self.fma::(), FMA64 => self.fma::(), FINV32 => handler!(self, |OpsRR(tg, reg)| { self.write_reg(tg, 1. / self.read_reg(reg).cast::()) }), FINV64 => handler!(self, |OpsRR(tg, reg)| { self.write_reg(tg, 1. / self.read_reg(reg).cast::()) }), FCMPLT32 => self.fcmp::(Ordering::Less), FCMPLT64 => self.fcmp::(Ordering::Less), FCMPGT32 => self.fcmp::(Ordering::Greater), FCMPGT64 => self.fcmp::(Ordering::Greater), ITF32 => handler!(self, |OpsRR(tg, reg)| { self.write_reg(tg, self.read_reg(reg).cast::() as f32); }), ITF64 => handler!(self, |OpsRR(tg, reg)| { self.write_reg(tg, self.read_reg(reg).cast::() as f64); }), FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| { self.write_reg( tg, crate::float::f32toint( self.read_reg(reg).cast::(), RoundingMode::try_from(mode) .map_err(|()| VmRunError::InvalidOperand)?, ), ); }), FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| { self.write_reg( tg, crate::float::f64toint( self.read_reg(reg).cast::(), RoundingMode::try_from(mode) .map_err(|()| VmRunError::InvalidOperand)?, ), ); }), FC32T64 => handler!(self, |OpsRR(tg, reg)| { self.write_reg(tg, self.read_reg(reg).cast::() as f64); }), FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| { self.write_reg( tg, crate::float::conv64to32( self.read_reg(reg).cast(), RoundingMode::try_from(mode) .map_err(|()| VmRunError::InvalidOperand)?, ), ) }), LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| { self.write_reg( tg, (self.pc + self.read_reg(reg).cast::() + imm + 3_u16).get(), ); }), LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| { self.load(dst, base, u64::from(off).wrapping_add(self.pc.get()), count)?; }), STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| { self.store(dst, base, u64::from(off).wrapping_add(self.pc.get()), count)?; }), JMP16 => handler!(self, |OpsP(off)| self.pc = self.pc.wrapping_add(off)), op => return Err(VmRunError::InvalidOpcode(op)), } } if TIMER_QUOTIENT != 0 { self.timer = self.timer.wrapping_add(1); if self.timer % TIMER_QUOTIENT == 0 { return Ok(VmRunOk::Timer); } } } } /// Bump instruction pointer #[inline(always)] fn bump_pc(&mut self) { self.pc = self .pc .wrapping_add(core::mem::size_of::() + PAST_OP as usize); } /// Decode instruction operands #[inline(always)] unsafe fn decode(&mut self) -> T { self.memory.prog_read::(self.pc + 1_u64) } /// Load #[inline(always)] unsafe fn load( &mut self, dst: u8, base: u8, offset: u64, count: u16, ) -> Result<(), VmRunError> { let n: u8 = match dst { 0 => 1, _ => 0, }; self.memory.load( self.ldst_addr_uber(dst, base, offset, count, n)?, self.registers .as_mut_ptr() .add(usize::from(dst) + usize::from(n)) .cast(), usize::from(count).wrapping_sub(n.into()), )?; Ok(()) } /// Store #[inline(always)] unsafe fn store( &mut self, dst: u8, base: u8, offset: u64, count: u16, ) -> Result<(), VmRunError> { self.memory.store( self.ldst_addr_uber(dst, base, offset, count, 0)?, self.registers.as_ptr().add(usize::from(dst)).cast(), count.into(), )?; Ok(()) } /// Perform binary operating over two registers #[inline(always)] unsafe fn binary_op(&mut self, op: impl Fn(T, T) -> T) { let OpsRRR(tg, a0, a1) = self.decode(); self.write_reg( tg, op(self.read_reg(a0).cast::(), self.read_reg(a1).cast::()), ); self.bump_pc::(); } /// Perform binary operation over register and immediate #[inline(always)] unsafe fn binary_op_imm(&mut self, op: impl Fn(T, T) -> T) { let OpsRR(tg, reg) = self.decode(); let imm: T = self.decode(); self.write_reg(tg, op(self.read_reg(reg).cast::(), imm)); self.bump_pc::(); self.bump_pc::(); } /// Perform binary operation over register and shift immediate #[inline(always)] unsafe fn binary_op_ims(&mut self, op: impl Fn(T, u32) -> T) { let OpsRRW(tg, reg, imm) = self.decode(); self.write_reg(tg, op(self.read_reg(reg).cast::(), imm)); self.bump_pc::(); } /// Fused division-remainder #[inline(always)] unsafe fn dir(&mut self) { handler!(self, |OpsRRRR(td, tr, a0, a1)| { let a0 = self.read_reg(a0).cast::(); let a1 = self.read_reg(a1).cast::(); if let Some(div) = a0.checked_div(a1) { self.write_reg(td, div); } else { self.write_reg(td, -1_i64); } if let Some(rem) = a0.checked_rem(a1) { self.write_reg(tr, rem); } else { self.write_reg(tr, a0); } }); } /// Fused multiply-add #[inline(always)] unsafe fn fma(&mut self) where T: ValueVariant + core::ops::Mul + core::ops::Add, { handler!(self, |OpsRRRR(tg, a0, a1, a2)| { let a0 = self.read_reg(a0).cast::(); let a1 = self.read_reg(a1).cast::(); let a2 = self.read_reg(a2).cast::(); self.write_reg(tg, a0 * a1 + a2) }); } /// Float comparsion #[inline(always)] unsafe fn fcmp(&mut self, nan: Ordering) { handler!(self, |OpsRRR(tg, a0, a1)| { let a0 = self.read_reg(a0).cast::(); let a1 = self.read_reg(a1).cast::(); self.write_reg(tg, (a0.partial_cmp(&a1).unwrap_or(nan) as i8 + 1) as u8) }); } /// Jump at `PC + #3` if ordering on `#0 <=> #1` is equal to expected #[inline(always)] unsafe fn cond_jmp(&mut self, expected: Ordering) { let OpsRRP(a0, a1, ja) = self.decode(); if self .read_reg(a0) .cast::() .cmp(&self.read_reg(a1).cast::()) == expected { self.pc = Address::new(((self.pc.get() as i64).wrapping_add(ja as i64)) as u64); } self.bump_pc::(); } /// Read register #[inline(always)] fn read_reg(&self, n: u8) -> Value { unsafe { *self.registers.get_unchecked(n as usize) } } /// Write a register. /// Writing to register 0 is no-op. #[inline(always)] fn write_reg(&mut self, n: u8, value: impl Into) { if n != 0 { unsafe { *self.registers.get_unchecked_mut(n as usize) = value.into() }; } } /// Load / Store Address check-computation überfunction #[inline(always)] unsafe fn ldst_addr_uber( &self, dst: u8, base: u8, offset: u64, size: u16, adder: u8, ) -> Result { let reg = dst.checked_add(adder).ok_or(VmRunError::RegOutOfBounds)?; if usize::from(reg) * 8 + usize::from(size) > 2048 { Err(VmRunError::RegOutOfBounds) } else { self.read_reg(base) .cast::() .checked_add(offset) .and_then(|x| x.checked_add(adder.into())) .ok_or(VmRunError::AddrOutOfBounds) .map(Address::new) } } }