busy developer s guide to building a virtual machine
play

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


  1. Busy Developer's Guide to Building A Virtual Machine Ted Neward Neward & Associates http://www.tedneward.com | ted@tedneward.com

  2. Objectives Building a virtual machine – understanding a VM can come from building one – so let's build one! and understand that there's limits to what we can do in a single session

  3. Architecture Major moving parts of most virtual machines

  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

  5. Bytecode Operations A quick primer

  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)

  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

  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

  9. BytecodeOps Operations fall into categories – branching ops unconditional, branch-if-true – call ops direct, indirect – storage ops global store/load, local store/load

  10. Stack-based Virtual Machines Everybody on, everybody off the stack

  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

  12. Register-based Virtual Machines How many registers do you need?

  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

  14. Register VMs No-operand opcodes: – NOP: do nothing – HALT: end execution

  15. Register VMs Basic value-manipulation opcodes: – LOAD: load value into register • constant value • from memory • from other register – STORE: store register into memory

  16. Register VMs Mathematical ops – ADD, SUB, MUL, DIV, MOD three operands: src1, src2, and dest

  17. Implementation Build it already!

  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

  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

  20. Summary Wrapping up

  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, ...

  22. Resources Where to go to get more

  23. Resources VM implementations to study – Java (JVM), .NET (CLR), Android (ART) – Javascript (V8, Chakra), WebAssembly – Python, Ruby, Smalltalk (Squeak) – Erlang (BEAM) – SQLite – ScummVM

  24. Resources Books – Language Implementation Patterns Parr (Pragmatic Publishers) – Virtual Machines Smith, Nair (Morgan Kaufman)

  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

  26. Credentials Who is this guy? – Principal -- Neward & Associates – Microsoft MVP; Java Expert – Author Professional F# 2.0 (w/Erickson, et al; Wrox, 2010) Effective Enterprise Java (Addison-Wesley, 2004) SSCLI Essentials (w/Stutz, et al; OReilly, 2003) Server-Based Java Programming (Manning, 2000) – Blog: http://blogs.tedneward.com – Twitter: @tedneward – For more, see http://www.newardassociates.com

Recommend


More recommend