0% found this document useful (0 votes)
2 views

Lecture12-1

The lecture covers the Virtual Machine (VM) memory model, its functions, and the translation of VM code to assembly. It details the eight memory segments used in VM programming, the commands for accessing these segments, and the structure of VM programs, which are typically generated by compilers. Additionally, it discusses the implementation of the VM on the Hack platform and the overall architecture of modern compilers.

Uploaded by

ARYAN PURI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture12-1

The lecture covers the Virtual Machine (VM) memory model, its functions, and the translation of VM code to assembly. It details the eight memory segments used in VM programming, the commands for accessing these segments, and the structure of VM programs, which are typically generated by compilers. Additionally, it discusses the implementation of the VM on the Hack platform and the overall architecture of modern compilers.

Uploaded by

ARYAN PURI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

School of Computer Science

COMP SCI 2000 Computer Systems


Lecture 12
Review - where we are:
Human Abstract design Software
abstract interface
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8
Assembly
Language

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

• Basic VM translation to assembly code

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

Modern OO high-level languages normally feature the following


variable kinds:
Class level:
q Static variables (class-level variables)
q Private variables (aka “object variables” / “fields” / “properties”)

Method level:
q Local variables
q Argument variables

When translated into the VM language,


The static, private, local and argument variables are mapped by the
compiler on the four memory segments static, this, local, argument
In addition, there are four additional memory segments, whose
role will be presented later: that, constant, pointer, temp.

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:

Memory access VM commands:

q pop memorySegment index

q push memorySegment index


Where memorySegment is static, this, local, argument, that, constant, pointer, or temp

And index is a non-negative integer

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

However, compilers are written by humans ...

In order to write or optimize a compiler, it helps to first understand the


spirit of the compiler’s target language – the VM language

The example VM program includes four new VM commands:


q function functionSymbol int // function declaration

q label labelSymbol // label declaration

q goto labelSymbol // jump to execute the command after labelSymbol

q if-goto labelSymbol // pop x


// if x=true, jump to execute the command after labelSymbol
// else proceed to execute the next command in the program

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 void hello(String hi) { do Unix.print(hi) ; }

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)

gt This week Next week


lt
Function calling commands
and
or function (declaration)

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)

Method: (a) specify the abstraction (stack, memory segments, commands)


(b) propose how to implement the abstraction over the Hack platform.
University of Adelaide 10
VM Implementation
VM implementation options:

– Software-based (eg emulate the VM model using Java)

– Translator-based (eg translate VM programs into the Hack machine language)

– Hardware-based (realize the VM model using dedicated memory and registers)

Two well-known translator-based implementations:

JVM: Javac translates Java programs into bytecode;


The JVM translates the bytecode into
the machine language of the host computer

CLR: C# compiler translates C# programs into IL code;


The CLR translated the IL code into
the machine language of the host computer.

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

THIS 3 The tip of this stack is the working stack of the


THAT 4 current function
5
Host
static, constant, temp, pointer:
... RAM
Global memory segments, all functions
12 see the same four segments
13
local,argument,this,that:
14 these segments are local at the function level;
15 each function sees its own, private copy of
16 each one of these four segments
17
The challenge:
18 represent all these logical constructs on the
19 same single physical address space -- the host
20 RAM.
21

...

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

TEMP ... RAM static: mapped on RAM[16 ... 255];


each segment reference static i appearing in a
VM file named f is compiled to the assembly
12

13 language symbol f.i (recall that the assembler further


General 14 maps such symbols to the RAM, from address 16 onward)
purpose
15 local,argument,this,that: these method-level
16 segments are mapped somewhere from address
... Statics 256 onward, on the “stack” or the “heap”. The
255
base addresses of these segments are kept in
RAM addresses LCL, ARG, THIS, and THAT. Access
256
... to the i-th entry of any of these segments is
Stack
implemented by accessing RAM[segmentBase + i]
2047

2048 constant: a truly virtual segment:


... Heap access to constant i is implemented by
supplying the constant i.
pointer: RAM[3..4] to change THIS and THAT.
University of Adelaide 14
VM implementation on the Hack platform
SP 0 Practice exercises
LCL 1
Now that we know how the memory segments are
ARG 2 mapped on the host RAM, we can write Hack
POINTER THIS 3 commands that realize the various VM commands.
THAT 4 for example, let us write the Hack code that
5
Host implements the following VM commands:
TEMP ... RAM q push constant 1
12 q pop static 7 (suppose it appears in a VM file named f)
13
q push constant 5
General 14
purpose q add
15
q pop local 2
16

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

building a compiler CISC


machine
language
RISC
machine
language
...
written in
a high-level
language
Hack

... ...
• 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)

• Brief history of virtual machines:


– 1970’s: p-Code
– 1990’s: Java’s JVM
– 2000’s: Microsoft .NET

• A full blown VM implementation typically also includes a common


software library
(can be viewed as a mini, portable OS).
• We will build such a mini OS later in the course.

University of Adelaide 18
The big picture

q JVM q CLR q VM q 7, 8

q Java q C# q Jack q 9

q Java compiler q C# compiler q Jack compiler q 10, 11

q JRE q .NET base q Mini OS q 12


class library
(Book chapters and
Course projects)

University of Adelaide 19

You might also like