Академический Документы
Профессиональный Документы
Культура Документы
Terence Parr
University of San Francisco
May 13, 2014
Preliminaries
What is a VM?
A simulated computer that runs a simple instruction
set (bytecodes).
A4 44
D 0 05
A5 45
D 0 01
60
B1 40
91 42
88
D 0 F9
E6 41
E6 43
88
C6 45
D 0 F0
60
address
M EM CPY
LD Y
CN T+ 0
;Set Y = CN T.L
BN E
LO O P
;If CN T.L > 0,then loop
LD A
CN T+ 1
;If CN T.H > 0,
BN E
LO O P
; then loop
RTS
;Return
LO O P
LD A
(SRC),Y
;Load A from ((SRC)+ Y)
STA
(D ST),Y
;Store A to ((D ST)+ Y)
D EY
;D ecr CN T.L
BN E
LO O P
;if CN T.L > 0,then loop
IN C
SRC+ 1
;Incr SRC + = $0100
IN C
D ST+ 1
;Incr D ST + = $0100
D EY
;D ecr CN T.L
D EC
CN T+ 1
;D ecr CN T.H
BN E
LO O P
;If CN T.H > 0,then loop
RTS
;Return
EN D
Programming our VM
Our bytecodes will be very regular
and higher level than machine
instructions
Each bytecode does a tiny bit of work
Print
1+2:
Stack code
Execution trace
ICONST 1
ICONST 2
IADD
PRINT
0000:
0002:
0004:
0005:
0006:
iconst
iconst
iadd
print
halt
1
2
stack= [ 1 ]
stack= [ 1 2 ]
stack= [ 3 ]
stack= [ ]
stack= [ ]
Instruction format
Code memory is 32-bit word
addressable
Bytecodes stored as ints but they are
bytes
Data memory is 32-bit word
addressable
+0
bytecode
Addresses
+1 integer numbers
are just
bytecode
operand
Operands
are 32-bit
integers
+2
bytecode
operand1
operand2
Sample bytecodes
Address
0000
0002
0004
0005
BytecodesAssembly bytecode
9 1ICONST 1
9 2ICONST 2
1 IADD
14 PRINT
registers
fetch
decode
code
memory
sp
fp
ip
...
execute
stack
Sample implementations
case BR :
ip = code[ip+ + ];
break;
case IAD D :
b = stack[sp--];
// 2nd opnd at top of stack
a = stack[sp--];
// 1st opnd 1 below top
stack[+ + sp] = a + b;
// push result
break;
Implementing functions
f();
call f, 0
pop
return 9;
iconst 9
ret