SLIDE 1
Busy Developer's Guide to Building A Virtual Machine Ted Neward - - PowerPoint PPT Presentation
Busy Developer's Guide to Building A Virtual Machine Ted Neward - - PowerPoint PPT Presentation
Busy Developer's Guide to Building A Virtual Machine Ted Neward Neward & Associates http://www.tedneward.com | ted@tedneward.com Objectives Building a virtual machine understanding a VM can come from building one so let's build
SLIDE 2
SLIDE 3
Architecture
Major moving parts of most virtual machines
SLIDE 4
Architecture
Architecture (simplified)
– code memory: byte array(s) holding the program's code – IP register: instruction pointer – call stack: function call frames – FP register: pointer to the current stack frame – global memory (heap): memory for storage/use – processor: fetch-decode-execute mechanism – constant pool: collection of constants
usually anything that isn't "word"s to the machine
SLIDE 5
Bytecode Operations
A quick primer
SLIDE 6
BytecodeOps
All assembly-level languages share some characteristics
– instructions are formed of two parts
- operation code (opcode)
- operation parameters (operands)
– these will sometimes be supplemented by other things
- directives (commands to the tools)
- labels (symbolic names used)
SLIDE 7
BytecodeOps
Opcodes take operands (parameters)
– some take none (NULL, pop, etc) – some take one (a constant value, etc) – some take two (add, subtract, etc) – some may take a varying number
depending on semantics of the opcode
SLIDE 8
BytecodeOps
Operations fall into categories
– machine ops
moving data in/out of parts of the machine
– mathematical ops
add, subtract, multiply, divide
– comparison ops
greater-than, less-than, equal, not-equal, greater-than-or- equal, less-than-or-equal
SLIDE 9
BytecodeOps
Operations fall into categories
– branching ops
unconditional, branch-if-true
– call ops
direct, indirect
– storage ops
global store/load, local store/load
SLIDE 10
Stack-based Virtual Machines
Everybody on, everybody off the stack
SLIDE 11
Stack VMs
Stack-based virtual machines
– simulates a hardware processor w/no general-purpose registers – instructions must use an operand stack to hold temporary values – all operands come from the stack – all results go back onto the stack
SLIDE 12
Register-based Virtual Machines
How many registers do you need?
SLIDE 13
Register VMs
All storage/work is in registers
– general-purpose registers – floating-point registers – string/data registers – (usually) still a stack involved
which makes register machines a superset of stack machines
SLIDE 14
Register VMs
No-operand opcodes:
– NOP: do nothing – HALT: end execution
SLIDE 15
Register VMs
Basic value-manipulation opcodes:
– LOAD: load value into register
- constant value
- from memory
- from other register
– STORE: store register
into memory
SLIDE 16
Register VMs
Mathematical ops
– ADD, SUB, MUL, DIV, MOD
three operands: src1, src2, and dest
SLIDE 17
Implementation
Build it already!
SLIDE 18
Implementation
Steps (1/2)
– Basic architecture and scaffolding – Simple (no-operand) ops: NOP, HALT, DUMP – Simple stack ops: CONST, LDC, POP – Globals ops: GSTORE, GLOAD – Math ops: ADD, SUB, etc
SLIDE 19
Implementation
Steps (2/2)
– Comparison ops: EQ, NE, GTE, etc – Branching ops: JMP, JT, JF, etc – Call ops: CALL, CALLI, RET – Locals ops: LSTORE, LLOAD
SLIDE 20
Summary
Wrapping up
SLIDE 21
Implementation
Futures
– add other types beyond ints and functions – memories could be made smaller (blocks/chunks/etc) and demand-allocated – definitions for "structures" – new opcodes – optimize, optimize, optimize, ...
SLIDE 22
Resources
Where to go to get more
SLIDE 23
Resources
VM implementations to study
– Java (JVM), .NET (CLR), Android (ART) – Javascript (V8, Chakra), WebAssembly – Python, Ruby, Smalltalk (Squeak) – Erlang (BEAM) – SQLite – ScummVM
SLIDE 24
Resources
Books
– Language Implementation Patterns
Parr (Pragmatic Publishers)
– Virtual Machines
Smith, Nair (Morgan Kaufman)
SLIDE 25
Resources
Web
– C-- ("high-level assembly language")
https://www.cs.tufts.edu/~nr/c--/index.html
– BEAM VM Wisdoms (by Dmytro Lytovchenko) – SCUMMVM Technical Reference
https://wiki.scummvm.org/index.php?title=SCUMM/Technica l_Reference
SLIDE 26