Você está na página 1de 28

Elements of Computing Systems, Nisan & Schocken, MIT Press www.idc.ac.

il/tecs

Virtual Machine
Part I: Stack Arithmetic
Usage and Copyright Notice:

Copyright 2005 © Noam Nisan and Shimon Schocken

This presentation contains lecture materials that accompany the textbook “The Elements of
Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005.

We provide both PPT and PDF versions.

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:

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

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

direct compilation: 2-tier compilation:


language 1 language 2 ... language n language 1 language 2 ... language n

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 written in Hack


machine
language
machine
language
... a high-level
language
machine
language

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

CISC RISC written in Hack


machine
language
machine
language
... a high-level
language
machine
language

Chapters
... ... 1-6

CISC RISC other digital platforms, each equipped Any Hack


machine machine with its VM implementation computer computer

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)

Method: (a) specify the abstraction (model’s constructs and commands)


(b) propose how to implement it over the Hack platform.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 7
The VM language

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:

 Pops topmost values x,y from the stack

 Computes the value of some function f(x,y)

 Pushes the result onto the stack

(Unary operations are similar, using x and f(x) instead)

 Impact: the operands are replaced with the operation’s result

 In general: all arithmetic and Boolean operations are implemented similarly.

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)

Stack Memory Stack Memory


121 ... 121 ...
5 a 6 5 a 6
17
... ...
push b 17
SP b 108 108 b 108
... SP
...

(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)

Stack Memory Stack Memory


121 ... 121 ...
5 a 6 5 a 6
17
... ...
push b 17
SP b 108 108 b 108
... SP
...

(before) (after)

Stack Memory Stack Memory


121 ... 121 ...
5 a 6 5 a 17
... pop a
17 SP ...
SP b 108 b 108
... ...

 Classical data structure


 Elegant and powerful
 Many implementation options.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 11
Evaluation of arithmetic expressions
// d=(2-x)*(y+5)
push 2
push x
sub
push y
push 5
add
mult
pop d

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


SP push y 8 push 8 eq
8
SP 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)

Modern programming languages normally feature the following variable kinds:

 Class level

 Static variables

 Private variables (AKA “object variabls” / “fields” / “properties”)

 Method level:

 Local variables

 Argument variables

A VM abstraction must support (at least) all these variable kinds.

The memory of our VM model consists of 8 memory segments:


static, argument, local, this, that, constant, pointer, and temp.

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

Where i is a non-negative integer and segment is one of the following:

 static: holds values of global variables, shared by all functions in the same class

 argument: holds values of the argument variables of the current function

 local: holds values of the local variables of the current function

 this: holds values of the private (“object”) variables of the current object

 that: holds array values

 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

 In order to write compilers, it helps to understand the spirit of VM


programming. So we will now see how some common programming tasks can be
implemented in the VM abstraction:

 Arithmetic task

 Object handling task

 Array handling 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)

We discuss them here to give some flavor of programming at the VM level.

(One can safely skip from here to slide 21)

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

High level program view


/*
/* Assume
Assume that
that bb and
and rr were
were
RAM view
passed
passed to the function as
to the function as
x: 120 0
... its first two arguments.
its first two arguments.
b y: 80 following
compilation 3012 b
The
The following
following code
code
object radius: 50 412
... implements
implements the operation
the operation
color: 3
3012 120 b.radius=r. */
b.radius=r. */
3013 80 b
(Actual RAM locations of program variables are 3014 50 object
run-time dependent, and thus the addresses shown
//
// Get
Get b's
b's base
base address:
address:
3015 3
here are arbitrary examples.) ... push argument
push argument 0 0
//
// Point
Point the
the this
this seg.
seg. to
to b:
b:
pop
pop pointer
pointer 00
//
// Get
Get r's
r's value
value
push
push argument
argument 11
//
// Set
Set b's
b's third
third field
field to
to r:
r:
pop this
pop this 22

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

Virtual memory segments Virtual memory segments


Just before the bar[2]=19 operation: Just after the bar[2]=19 operation:

local pointer that local pointer that


(that 0
0 4315 0 0 0 4315 0 4317 0 19 is now
1 ... 1 1 ... 1 ... 1 1 ... alligned with
RAM[4317])

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

Method: (a) specify the abstraction (model’s constructs and commands)


(b) propose how to implement it over the Hack platform.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 7: Virutal Machine, Part I slide 21
Implementation

VM implementation options:

 Software-based (emulation)

 Translator-based (e.g., to the Hack language)

 Hardware-based (CPU-level)

Well-known translator-based implementations:

 JVM (runs bytecode programs in the Java platform)

 CLR (runs IL programs in the .NET platform).

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

THAT 4  local,argument,this,that: mapped on the


5
Host heap. The base addresses of these segments are
TEMP ... RAM kept in LCL,ARG,THIS,THAT. Access to the i-th
entry of a segment is implemented by accessing
12
the segment’s (base + i) word in the RAM
13
General 14
 static: static variable number j in a VM file f is
purpose
implemented by the assembly language symbol
15
f.j (and recall that the assembler maps such
16
symbols to the RAM starting from address 16)
... Statics
255
 constant: truly a virtual segment. Access to
constant i is implemented by supplying the
256
... constant i
Stack
2047  pointer,temp: see the book
2048
... Exercise: given the above game rules, write the
Heap
Hack commands that implement, say,
push constant 5 and pop local 2.

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.

hasMoreCommands -- boolean Are there more commands in the input?

Reads the next command from the input and makes it


the current command. Should be called only if
advance -- --
hasMoreCommands() is true. Initially there is no
current command.
C_ARITHMETIC,
C_PUSH, C_POP, Returns the type of the current VM command.
commandType -- C_LABEL, C_GOTO, C_ARITHMETIC is returned for all the arithmetic
C_IF, C_FUNCTION, commands.
C_RETURN, C_CALL

Returns the first argument of the current command. In


the case of C_ARITHMETIC, the command itself
arg1 -- string
(add, sub, etc.) is returned. Should not be called if the
current command is C_RETURN.

Returns the second argument of the current command.


arg2 -- int Should be called only if the current command is
C_PUSH, C_POP, C_FUNCTION, or C_CALL.

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.

Routine Arguments Returns Function

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)

Close -- -- Closes the output file.

Comment: More routines will be added to this module in chapter 8.

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

 In this lecture we began the process of


CISC RISC written in

building a compiler machine


language
machine
language
... a high-level
language
Hack

... ...

 Modern compiler architecture:

 Front end (translates from high level language to a VM language)


 Back end (implements the VM language on a target 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 includes a common software library
(can be viewed as a mini, portable OS).
 We will build such a mini OS later in the course.

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

Você também pode gostar