Crate tiny_vm

source ·
Expand description

TinyVm example.

This example introduces a simple 16 bit virtual machine the TinyVm. The VM consists of three registers defined in TinyReg, a separate data and instruction memory and a small set of instructions TinyInsn, sufficient to implement a guest program to compute the Fibonacci sequence.

The TinyVm implements a simple just-in-time (JIT) compiler to demonstrate the juicebox_asm crate. Additionally, it implements a reference interpreter.

fn main() {
  let mut prog = Vec::new();
  prog.push(TinyInsn::LoadImm(TinyReg::A, 100));
  prog.push(TinyInsn::Add(TinyReg::B, TinyReg::A));
  prog.push(TinyInsn::Addi(TinyReg::C, 100));
  prog.push(TinyInsn::Halt);

  let mut vm = TinyVm::new(prog);
  vm.interp();

  assert_eq!(100, vm.read_reg(TinyReg::A));
  assert_eq!(100, vm.read_reg(TinyReg::B));
  assert_eq!(100, vm.read_reg(TinyReg::C));
  assert_eq!(4, vm.icnt);
  assert_eq!(4, vm.pc);

  vm.pc = 0;
  vm.jit();

  assert_eq!(100, vm.read_reg(TinyReg::A));
  assert_eq!(200, vm.read_reg(TinyReg::B));
  assert_eq!(200, vm.read_reg(TinyReg::C));
  assert_eq!(8, vm.icnt);
  assert_eq!(4, vm.pc);
}

Structs

  • A minial fixup utility to implement jump labels when constructing guest programs.
  • A guest physical address.
  • The TinyVm virtual machine state.

Enums

Functions