this is a virtual machine that is implemented fallowing "So you Want to build a Language VM".
The virtual machine will be based on a register-based virtual machine
Our virtual CPUs will have the ability to take 32 bits of data at a time, execute it, and then go get another group of 32 bits. At a very general level, that is all hardware processors do:
- Read next instruction
- Execute instruction
- Repeat
A grouping of 32-bits is an Instructions.
- The first 8 are the Opcode
- The remaining bits will be the Operands Using this design our opcodes can have up to 3 Operands (arguments)
A opcode is a command that is represented by a byte. In this virtual machine it will be possible to have up to 255 Opcodes
If we were to load a number, we out only have enough space to store 2 bytes of data (16 bits) because 1 byte will specify the Opcode and 1 byte will specify the register to target
We want our VM to bee
- Reasonably performant compared to modern implementations. We'll use Python as our comparison.
- Fault tolerant
- Serves as command and control platform for running applications
- Clustering of VM's running on different physical servers
- Get used to writing a VM and learn some domain specific topics
- Research Other VM's and compare them to the simple VM
- Research Webassembly's VM and see if we can create it
- Tree-walking
- normally the first interpreter's people code
- transform source code into tree like structure and walk though all the operations
- simple
- flexible
- Slow compared to other types
- Stack-based
- Most common interpreters (JVM, Python)
- store operations on a stack, pop and push value to operate on it
- easier to code than register-based VM
- Decent performance
- Does not map to real hardware; CPUs use registers
- Most tasks require more instructions
- Register-based
- Least common interpreters (BEAM VM, Lua VM, Dalvik VM)
- Register-based VMs are much closer to how actual hardware works
- More performant
- More complex to code
- Have to worry about register allocation and de-allocation when writing the compiler