Busy Developer's Guide to Building A Virtual Machine Ted Neward - - PowerPoint PPT Presentation

busy developer s guide to building a virtual machine
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Busy Developer's Guide to Building A Virtual Machine

Ted Neward Neward & Associates http://www.tedneward.com | ted@tedneward.com

slide-2
SLIDE 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

slide-3
SLIDE 3

Architecture

Major moving parts of most virtual machines

slide-4
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
SLIDE 5

Bytecode Operations

A quick primer

slide-6
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
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
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
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
SLIDE 10

Stack-based Virtual Machines

Everybody on, everybody off the stack

slide-11
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
SLIDE 12

Register-based Virtual Machines

How many registers do you need?

slide-13
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
SLIDE 14

Register VMs

No-operand opcodes:

– NOP: do nothing – HALT: end execution

slide-15
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
SLIDE 16

Register VMs

Mathematical ops

– ADD, SUB, MUL, DIV, MOD

three operands: src1, src2, and dest

slide-17
SLIDE 17

Implementation

Build it already!

slide-18
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
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
SLIDE 20

Summary

Wrapping up

slide-21
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
SLIDE 22

Resources

Where to go to get more

slide-23
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
SLIDE 24

Resources

Books

– Language Implementation Patterns

Parr (Pragmatic Publishers)

– Virtual Machines

Smith, Nair (Morgan Kaufman)

slide-25
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
SLIDE 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