Lecture12-1
Lecture12-1
Assembler
Chapter 6
abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
University of Adelaide 2
This Lecture
• VM memory model
• VM functions
University of Adelaide 3
The VM’s Memory segments
A VM program is designed to provide an interim abstraction of a
program written in some high-level language
Method level:
q Local variables
q Argument variables
University of Adelaide 4
Memory segments and access commands
The VM abstraction includes 8 separate memory segments named:
static, this, local, argument, that, constant, pointer, temp
As far as VM programming commands go, all memory segments look and behave the same
To access a particular segment entry, use the following generic syntax:
Notes:
(In all our code examples thus far, memorySegment was static)
The roles of the eight memory segments will become relevant when we talk about compiling
At the VM abstraction level, all memory segments are treated the same way.
University of Adelaide 5
VM programming
VM programs are normally written by compilers, not by humans
For example, to effect if (x > n) goto loop, we can use the following VM commands:
push x
push n
gt
if-goto loop // Note that x, n, and the truth value were removed from the stack.
University of Adelaide 6
VM programming (example)
High-level code VM code (first approx.) VM code
function int mult(x,y) function mult(x,y) function mult 2
{ push 0 push constant 0
var int result, j; pop result pop local 0
let result = 0; push y push argument 1
let j = y; pop local 1
pop j
while ~(j = 0)
{ label loop label loop
let result = result + x; push j push local 1
let j = j - 1; push 0 push constant 0
} eq eq
return result; if-goto end if-goto end
} push local 0
push result
push x push argument 0
Just after mult(7,3) is entered:
add add
Stack argument local
SP 0 7 x 0 0 sum pop result pop local 0
1 3 y 1 0 j push j push local 1
... ...
push 1 push constant 1
sub sub
Just after mult(7,3) returns: pop local 1
pop j
Stack
goto loop goto loop
21
SP label end label end
push result push local 0
return return
University of Adelaide 7
VM Programming Examples in Class XX
function int add(int x,int y) { return x + y ; }
function XX.add 0
push argument 0
push argument 1
add
return
function XX.hello 0
push argument 0
call Unix.print 1
push constant 0
return
University of Adelaide 8
VM programming: multiple functions
Compilation:
q A Jack application is a set of 1 or more class files (just like .java files).
q When we apply the Jack compiler to these files, the compiler creates a set of 1 or
more .vm files (just like .class files). Each method in the Jack app is translated
into a VM function written in the VM language
q Thus, a VM file consists of one or more VM functions.
Execution:
q At any given point of time, only one VM function is executing (the “current
function”), while 0 or more functions are waiting for it to terminate (the functions
up the “calling hierarchy”)
q For example, a main function starts running; at some point we may reach the
command call factorial, at which point the factorial function starts running;
then we may reach the command call mult, at which point the mult function starts
running, while both main and factorial are waiting for it to terminate
The stack: a global data structure, used to save and restore the resources (memory
segments) of all the VM functions up the calling hierarchy (e.g. main and factorial).
The tip of this stack if the working stack of the current function (e.g. mult).
University of Adelaide 9
VM Implementation
Goal: Specify and implement a VM model and language:
Arithmetic / Boolean commands Program flow commands
add
label (declaration)
sub
goto (label)
neg
eq if-goto (label)
not
call (a function)
Memory access commands
pop x (pop into x, which is a variable) return (from a function)
push y (y being a variable or a constant)
University of Adelaide 11
Software implementation: Our VM emulator
University of Adelaide 12
VM implementation on the Hack platform
SP 0 The stack: a global data structure, used to save
LCL 1
and restore the resources of all the VM
functions up the calling hierarchy.
ARG 2
...
University of Adelaide 13
VM implementation on the Hack platform
SP 0
Basic idea: the mapping of the stack and the global
segments on the RAM is easy (fixed);
LCL 1
the mapping of the function-level segments is
ARG 2
dynamic, using pointers
POINTER THIS 3
The stack: mapped on RAM[256 ... 2047];
THAT 4
Host The stack pointer is kept in RAM address SP
5
... Statics q eq
255 Tips:
256
... 1. The implementation of any one of these VM
Stack
2047
commands requires several Hack assembly
commands involving pointer arithmetic
2048
(using commands like A=M)
... Heap
2. If you run out of registers (you have only two ...),
you may use R13, R14, and R15.
University of Adelaide 15
VM Translator Parsing
• Memory locations R13, R14, R15 can be used as
temporary variables if required
• push constant 1
@SP
AM=M+1
A=A-1
M=1
• pop static 7 (in a VM file named Bob.vm)
@SP
AM=M-1
D=M
@Bob.7
M=D
University of Adelaide 16
VM Translator Parsing
• push constant 5
@5
D=A
@SP
AM=M+1
A=A-1
M=D
• add
@SP
AM=M-1
D=M
A=A-1
M=D+M
University of Adelaide 17
Some
language
... Some Other
language
... Jack
Perspective
Some
compiler Some Other
compiler
compiler
VM language
VM
implementation VM imp.
VM
Translator
• In this lecture we began the process of
over CISC over RISC
emulator
platforms platforms
... ...
• Modern compiler architecture:
– Front-end (translates from a high-level language to a VM language)
– Back-end (translates from the VM language to the machine
language of some target hardware platform)
University of Adelaide 18
The big picture
q JVM q CLR q VM q 7, 8
q Java q C# q Jack q 9
University of Adelaide 19