Escolar Documentos
Profissional Documentos
Cultura Documentos
il/tecs
Virtual Machine
Part I: Stack Arithmetic
Usage and Copyright Notice:
This presentation contains lecture materials that accompany the textbook “The Elements of
Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005.
The book web site, www.idc.ac.il/tecs , features 13 such presentations, one for each book
chapter. Each presentation is designed to support about 3 hours of classroom or self-study
instruction.
You are welcome to use or edit this presentation as you see fit for instructional and non-
commercial purposes.
If you use our materials, we will appreciate it if you will include in them a reference to the book’s
web site.
If you have any questions or comments, you can reach us at tecs.ta@gmail.com
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 1
Where we are at:
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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 2
Motivation
class
class Main
Main {{
static ...
...
static int
int x;
x; @a
@a
M=D
M=D
function
function void
void main()
main() {{ @b
//
// Input
Input and
and multiply
multiply 22 numbers
numbers
Ultimate goal: @b
M=0
M=0
var int a, b,
var int a, b, x; x; (LOOP)
(LOOP)
let
let aa == Keyboard.readInt(“Enter
Keyboard.readInt(“Enter aa number”);
number”); Translate high- @a
@a
D=M
let b = Keyboard.readInt(“Enter a number”); D=M
let b = Keyboard.readInt(“Enter a number”);
let
let xx == mult(a,b);
mult(a,b);
level programs @b
@b
D=D-A
return;
return; into executable D=D-A
@END
@END
}}
}} code. D;JGT
D;JGT
@j
@j
D=M
D=M
// @temp
// Multiplies
Multiplies two two numbers.
numbers. @temp
function
function int mult(int x,
int mult(int x, int
int y)
y) {{
Compiler M=D+M
M=D+M
@j
var @j
var int
int result,
result, j; j; M=M+1
let M=M+1
let result = 0; let jj == y;
result = 0; let y; @LOOP
while not(j = 0) { @LOOP
while not(j = 0) { 0;JMP
0;JMP
let
let result
result == result
result ++ x;
x; (END)
(END)
let j = j –
let j = j – 1;1; @END
@END
}} 0;JMP
0;JMP
return
return result;
result; ...
...
}}
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 3
Compilation models
intermediate language
hardware
platform 1
hardware
platform 2
... hardware
platform m
hardware hardware ... hardware
platform 1 platform 2 platform m
.
requires n m translators requires n + m translators
Two-tier compilation:
First compilation stage depends only on the details of the source language
Second compilation stage depends only on the details of the target platform.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 4
The big picture
The intermediate code:
Some
language
... Some Other
language
... Jack
language The interface between
the 2 compilation stages
Some Jack Must be sufficiently
compiler Some Other
compiler compiler general to support many
<high-level language,
machine language> pairs
Intermediate code Can be modeled as the
language of an abstract
virtual machine (VM)
VM
VM imp.
implementation VM imp.
VM over the Hack Can be implemented in
over CISC over RISC
platforms platforms
emulator platform many different ways.
... ...
CISC RISC other digital platforms, each equipped Any Hack
machine machine with its own VM implementation computer computer
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 5
The big picture
Some
language
... Some Other
language
... Jack
language
Chapters
Some Jack
Some Other 9-13
compiler
compiler compiler
VM language
VM
implementation VM imp. VM imp.
VM over the Hack
Chapters
over CISC over RISC
platforms platforms
emulator platform 7-8
Chapters
... ... 1-6
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 6
Lecture plan
Goal: Specify and implement a VM model and language
Arithmetic
Arithmetic/ /Boolean
Booleancommands
commands Program
Programflow
flowcommands
commands
add
add label
label (declaration)
(declaration)
sub
sub
goto
goto (label)
(label)
neg
neg
eq if-goto
if-goto (label)
(label)
eq
gt
gt
This lecture Next lecture
lt
lt
and Function
Functioncalling
callingcommands
commands
and
or
or function (declaration)
function (declaration)
not
not
call
call (a
(afunction)
function)
Memory
Memoryaccess
accesscommands
commands
pop
pop xx (variable) (from
(variable) return
return (fromaafunction)
function)
push
pushyy (variable
(variableororconstant)
constant)
Important:
From here till the end of this and the next lecture we describe the VM
model used in the Hack-Jack platform
Other VM models (like JVM/JRE and IL/CLR) are similar in spirit and
different in scope and details.
Our VM features a single 16-bit data type that can be used as:
Integer
Boolean
Pointer.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 8
Stack arithmetic
17 17
4 add 9
5 SP
SP
Typical operation:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 9
Memory access (first approximation)
(before) (after)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 10
Memory access (first approximation)
(before) (after)
Memory Stack
... SP push 2 2 push x 2 sub
x 5 SP 5
y 9 SP
...
-3 -3 -3
SP push y 9 push 5 add
9
SP 5
SP
Stack Memory
...
-3 -42 SP
mult pop d x 5
14 SP
SP y 9
...
d -42
...
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 12
Evaluation of Boolean expressions
// if (x<7) or (y=8)
push x
push 7
lt
push y
push 8
eq
or
Memory Stack
... SP 12 12 lt
push x push 7
x 12 SP 7
y 8 SP
...
false true
true or SP
SP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 13
Arithmetic and Boolean commands (wrap-up)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 14
Memory access (motivation)
Class level
Static variables
Method level:
Local variables
Argument variables
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 15
Memory access commands
Command
Commandformat:
format: (Rather than pop x and push y,
pop segment
pop segment ii as was shown in previous slides,
which was a conceptual simplification)
push segment
push segment ii
static: holds values of global variables, shared by all functions in the same class
this: holds values of the private (“object”) variables of the current object
constant: holds all the constants in the range 0…32767 (pseudo memory segment)
pointer: used to align this and that with different areas in the heap
temp: fixed 8-entry segment that holds temporary variables for general
use; Shared by all VM functions in the program.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 16
VM programming
VM programs are normally written by compilers, not by humans
Arithmetic task
Disclaimer:
These programming examples don’t belong here; They belong to the compiler
chapter, since expressing programming tasks in the VM language is the business
of the compiler (e.g., translating Java programs to Bytecode programs)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 17
Arithmetic example
High-level code VM code (first approx.) VM code
function function
function mult(x,y)
mult(x,y) function
function mult
mult 22
function mult(x,y)
mult(x,y) {{
int push 0
push 0 push
push constant
constant 00
int result,
result, j;
j;
result=0; pop
pop result
result pop
pop local
local 00
result=0;
j=y; push
push yy push
push argument
argument 11
j=y;
while pop
pop jj pop
pop local
local 11
while ~(j=0)
~(j=0) {{
label
label loop
loop label
label loop
loop
result=result+x;
result=result+x; push
push jj push
push local
local 11
j=j-1;
j=j-1; push
push 00 push
push constant
constant 00
}} eq eq
eq eq
return
return result;
result; if-goto
if-goto end
end if-goto
if-goto end
end
}} push
push result
result push
push local
local 00
push x
push x push
push argument
argument 00
Just after mult(7,3) is entered:
add
add add
add
Stack argument local
x sum
pop
pop result
result pop
pop local
local 00
SP 0 7 0 0
1 3 y 1 0 j push
push jj push
push local
local 11
... ...
push
push 11 push
push constant
constant 11
sub
sub sub
sub
Just after mult(7,3) returns:
pop
pop jj pop
pop local
local 11
Stack
goto
goto loop
loop goto
goto loop
loop
21
SP label
label end
end label
label end
end
push
push result
result push
push local
local 00
return
return return
return
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 18
Object handling example
Virtual memory segments just before Virtual memory segments just after
the operation b.radius=17: the operation b.radius=17:
argument pointer this argument pointer this
0 3012 0 0 0 3012 0 3012 0 120 (this 0
1 17 1 ... 1 17 1 1 80 is now
... ... 2 17
alligned with
RAM[3012])
3 3
...
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 19
Array handling example
/*
/* Assume
Assume that
that bar
bar is
is the
the
High-level program view RAM view
first
first local
local variable
variable declared
declared
0 in
in the
the high-level
high-level program.
program. The
The
0 7
1 53 following
... code below implements
code below implements
bar 2 121 compilation 398 4315 bar bar[2]=19,
bar[2]=19, oror *(bar+2)=19.
*(bar+2)=19. */
*/
array 3 8 ...
... 4315 7
//
9 19 4316 53 // Get
Get bar's
bar's base
base address:
address:
4317 121 bar
push local
push local 00
4318 8 array push
push constant
constant 22
(Actual RAM locations of program variables are ... add
add
run-time dependent, and thus the addresses shown 4324 19
here are arbitrary examples.) ... //
// Set
Set that’s
that’s base
base to
to (bar+2):
(bar+2):
pop
pop pointer
pointer 11
push
push constant
constant 1919
// *(bar+2)=19:
// *(bar+2)=19:
pop
pop that
that 00
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 20
Lecture plan
Goal: Specify and implement a VM model and language
Arithmetic
Arithmetic/ /Boolean
Booleancommands
commands Program
Programflow
flowcommands
commands
add
add label
label (declaration)
(declaration)
sub
sub
goto
goto (label)
(label)
neg
neg
eq if-goto
if-goto (label)
(label)
eq
gt
gt
This lecture Next lecture
lt
lt
and Function
Functioncalling
callingcommands
commands
and
or
or function (declaration)
function (declaration)
not
not
call
call (a
(afunction)
function)
Memory
Memoryaccess
accesscommands
commands
pop
pop segment
segment ii (from
return
return (fromaafunction)
function)
push
pushsegment
segment ii
VM implementation options:
Software-based (emulation)
Hardware-based (CPU-level)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 22
Our VM emulator (part of the course software suite)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 23
VM implementation on the Hack platform
SP 0 The challenge: (i) map the VM constructs on the
LCL 1
host RAM, and (ii) given this mapping, figure out
how to implement each VM command using
ARG 2
assembly commands that operate on the RAM
THIS 3
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 24
Parser module (proposed design)
Parser: Handles the parsing of a single .vm file, and encapsulates access to the input code. It reads VM commands, parses them, and
provides convenient access to their components. In addition, it removes all white space and comments.
Routine Arguments Returns Function
Constructor
Input file / stream -- Opens the input file/stream and gets ready to parse it.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 25
CodeWriter module (proposed design)
CodeWriter: Translates VM commands into Hack assembly code.
Constructor Output file / stream -- Opens the output file/stream and gets ready to write
into it.
setFileName fileName (string) -- Informs the code writer that the translation of a new
VM file is started.
writeArithmetic command (string) -- Writes the assembly code that is the translation of the
given arithmetic command.
WritePushPop Command (C_PUSH or -- Writes the assembly code that is the translation of the
C_POP), given command, where command is either C_PUSH
or C_POP.
segment (string),
index (int)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 26
Some
language
... Some Other
language
. . . Jack
Perspective Some
compiler Some Other
compiler
compiler
VM language
VM
implementation VM imp.
VM
over CISC
platforms
over RISC
platforms
emulator Translator
... ...
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 27
The road ahead
Conceptually
Tasks: similar to: And to:
Complete the VM JVM CLR
specification and
implementation
(chapters 7,8)
Introduce Jack, a Java C#
high-level programming
language (chapter 9)
Build a compiler for it Java compiler C# compiler
(chapters 10,11)
Finally, build a mini-OS, JRE .NET base class library
i.e. a run-time library
(chapter 12).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 28