lecture 11 compiler II

Building a Modern Computer From First Principles
www.nand2tetris.org

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 1

Course map
Human
Thought

Abstract design

Software
hierarchy

abstract interface
Chapters 9, 12

H.L. Language
&

Operating Sys.

Compiler
abstract interface
Chapters 10 - 11

Virtual
Machine

VM Translator
abstract interface
Chapters 7 - 8

Assembly
Language
Assembler
Chapter 6

abstract interface


Machine
Language

Computer
Architecture
abstract interface
Chapters 4 - 5

Hardware
Platform

Hardware
hierarchy

Gate Logic
abstract interface
Chapters 1 - 3

Chips &
Logic Gates


Electrical
Engineering

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

Physics

slide 2

The big picture
previous
lecture
this
lecture

(Chapter 10)

XML
code


Jack Compiler
Syntax Analyzer

Jack
Program

Tokenizer

Parser

Code
Gene
-ration

(Chapter 11)

VM
code


Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 3

Syntax analysis (review)
Class
Class Bar
Bar {{
method
method Fraction
Fraction foo(int
foo(int y)
y) {{
var
var int
int temp;
temp; //
// aa variable
variable
let

temp
=
(xxx+12)*-63;
let temp = (xxx+12)*-63;
...
...
...
Syntax analyzer
...

!
#

"

$

%&

'

(
& 0

#

1

)

+, &
-./%

*$




var
var


int


int

temp
temp

;


;







let

let

temp

temp

=


=





((







xxx
xxx




++




12
12






...
...

2.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 4

Memory segments (review)
2.
2.
pop
pop segment
segment ii
push
push segment
segment ii
3

i

+

&

segment
,

static:

,

%

,
,

argument:
local:

,

this:

#! ,)

"$

%

$

#

that:

,)

0 4 32767 (

constant:
pointer:
temp:

,

this

5+

$

that

,
,

6

2.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 5

Code generation example
method
method int
int foo()
foo() {{
var
var int
int x;
x;
let
x
=
x
let x = x ++ 1;
1;
...
...

Syntax
analysis

#

x




let
let


xx


=

=





xx




++




11








,

$

Code
generation

push
push local
local 00
push
push constant
constant 11
add
add
pop
pop local
local 00

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 6

Handling variables

3

, %

3

%

3

%

1

&

7

x0

#:

x%

89 #
1

$7

&

1

;8.

,

$

x7

local, static, field, argument 7
#3

1

&

6
, 0

$

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 7

Handling variables: mapping them on memory segments (example)
5
<

%

static%

=

>+,

,
static 0% static 1% static 2%

3

%&
,
,)
,
,

&

nAccounts % bankCommission

static 0,1

id% owner% balance

this 0,1,2

sum% bankAccount% when

arg 0,1,2

i% j% due

local 0,1,2

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 8

Handling variables: symbol tables

? &

,

,

,
, %

1

&
,
#

,

1 &
, 1

1
0
$

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 9

Handling variables: managing their life cycle

2

,
,

static
field

,

local

,

, 1
, 1
,
,

argument

&

local

2.

,)
%1

,
@

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 10

Handling objects: construction / memory allocation
A
class
class Complex
Complex {{
//
// Fields
Fields (properties):
(properties):
int
re;
//
int re; // Real
Real part
part
int
im;
//
Imaginary
int im; // Imaginary part
part
...
...
/**
/** Constructs
Constructs aa new
new Complex
Complex number
number */
*/
public
Complex
(int
re,
int
im)
{
public Complex (int re, int im) {
this.re
this.re == re;
re;
this.im
=
im;
this.im = im;
}}
...
...
}}

B

class
class Foo
Foo {{
public
public void
void bla()
bla() {{
Complex
Complex a,
a, b,
b, c;
c;
...
...
aa == new
new Complex(5,17);
Complex(5,17);
bb == new
Complex(12,192);
new Complex(12,192);
...
...
cc == a;
a; //
// Only
Only the
the reference
reference is
is copied
copied
...
...
}}

&

? &
7

foo = new ClassName(…)

foo = Memory.alloc(n)
3

n

,

&
,)

=

%

Memory.alloc
'
,
, 1
( n&

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 11

Handling objects: accessing fields
A
class
class Complex
Complex {{
//
// Properties
Properties (fields):
(fields):
int
re;
//
Real
int re; // Real part
part
int
im;
//
Imaginary
int im; // Imaginary part
part
...
...
/**
/** Constructs
Constructs aa new
new Complex
Complex number
number */
*/
public
Complex(int
re,
int
im)
{
public Complex(int re, int im) {
this.re
this.re == re;
re;
this.im
=
im;
this.im = im;
}}
...
...
/**
/** Multiplies
Multiplies this
this Complex
Complex number
number
by
the
given
scalar
*/
by the given scalar */
public
public void
void mult
mult (int
(int c)
c) {{
re
re == re
re ** c;
c;
im
=
im
*
c;
im = im * c;
}}
...
...
}}

? &
im = im * c ?

1

&
,

,

,

*(this
*(this ++ 1)
1) == *(this
*(this ++ 1)
1)
times
times
(argument
(argument 0)
0)

+

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

,

slide 12

Handling objects: establishing access to the object’s fields
C

1

&
,)
Ball 8 Ball
x%y
radius%
color

b

%

High level program view
b
object

x:
y:
radius:
color:

0

following
compilation

50

412

3

(Actual RAM locations of program variables are
run-time dependent, and thus the addresses shown
here are arbitrary examples.)

...
3012

...

3012

120

3013

80

3014

50

3015

3

...

Virtual memory segments just before
the operation b.radius=17:
argument

pointer

0

3012

0

1

17

1

...

this
0

...

bb

rr&&
&&

?? &&
## AA
b.radius
b.radius == rr 77

RAM view

120
80

88

$$

//
// Get
Get b's
b's base
base address:
address:
push
argument
0
push argument 0
//
// Point
Point the
the this
this segment
segment to
to b:
b:
pop
pop pointer
pointer 00
//
// Get
Get r's
r's value
value
push
argument
push argument 11
//
// Set
Set b's
b's third
third field
field to
to r:
r:
pop
pop this
this 22

b

b
object

Virtual memory segments just after
the operation b.radius=17:
argument

pointer

0

3012

0

1

17

1

...

3012

this
0

120

1

80

2

17

3

3

(this 0

is now
alligned with
RAM[3012])

...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 13

Handling objects: method calls
A

? &
class
class Complex
Complex {{
//
// Properties
Properties (fields):
(fields):
int
re;
//
Real
int re; // Real part
part
int
int im;
im; //
// Imaginary
Imaginary part
part
...
...
/**
/** Constructs
Constructs aa new
new Complex
Complex object.
object. */
*/
public
Complex(int
re,
int
im)
{
public Complex(int re, int im) {
this.re
this.re == re;
re;
this.im
=
im;
this.im = im;
}}
...
...
}}
class
class Foo
Foo {{
...
...
public
public void
void bla()
bla() {{
Complex
Complex x;
x;
...
...
xx == new
new Complex(1,2);
Complex(1,2);
x.mult(5);
x.mult(5);
...
...
}}
}}

x.mult(5)

7
,

&
mult(x,5)

&
push
push xx
push
push 55
call
call mult
mult

foo.bar(v1,v2,...)

push
push
push
...
call

foo
v1
v2
bar

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 14

Handling arrays: declaration / construction
;8.

A

0

class
class Bla
Bla {{
...
...
void
void foo(int
foo(int k)
k) {{
int
int x,
x, y;
y;
int[]
bar;
int[] bar; //
// declare
declare an
an array
array
...
...
//
// Construct
Construct the
the array:
array:
bar
=
new
int[10];
bar = new int[10];
...
...
bar[k]=19;
bar[k]=19;
}}
...
...
Main.foo(2);
Main.foo(2); //
// Call
Call the
the foo
foo method
method
...
...

...
275

x

(local 0)

276

y

(local 1)

277

4315

bar (local 2)

...
504

2

...

k

(argument 0)

4315
4316
4317
4318
4324

19

(bar array)

...
...

? &
bar = new int(n) 7

bar = Memory.alloc(n)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 15

Handling arrays: accessing an array entry by its index
A

;8.
class
class Bla
Bla {{
...
...
void
void foo(int
foo(int k)
k) {{
int
x,
y;
int x, y;
int[]
int[] bar;
bar; //
// declare
declare an
an array
array
...
...
//
// Construct
Construct the
the array:
array:
bar
=
new
int[10];
bar = new int[10];
...
...
bar[k]=19;
bar[k]=19;
}}
...
...
Main.foo(2);
Main.foo(2); //
// Call
Call the
the foo
foo method
method
...
...

2.

...
275

x

(local 0)

276

y

(local 1)

277

$

//
// bar[k]=19,
bar[k]=19, or
or *(bar+k)=19
*(bar+k)=19
push
bar
push bar
push
push kk
add
add
//
// Use
Use aa pointer
pointer to
to access
access x[k]
x[k]
pop
addr
//
addr
points
to
bar[k]
pop addr // addr points to bar[k]
push
push 19
19
pop
*addr
pop *addr //
// Set
Set bar[k]
bar[k] to
to 19
19

4315

bar (local 2)

...
504

2

...

k

(argument 0)

4315
4316
4317
4318

bar[k] = 19 7
#

bar[k] = 19

0

4324

? &

%)

2.

19

(bar array)

...
...
#

$

//
// bar[k]=19,
bar[k]=19, or
or *(bar+k)=19
*(bar+k)=19
push
local
2
push local 2
push
push argument
argument 00
add
add
//
// Use
Use the
the that
that segment
segment to
to access
access x[k]
x[k]
pop
pointer
1
pop pointer 1
push
push constant
constant 19
19
pop
that
0
pop that 0

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 16

Handling expressions
?

+

2.
syntax
analysis

((5+z)/-8)*(4^2)
((5+z)/-8)*(4^2)

2.

push
push 55
push
push zz
add
add
push
push 88
neg
neg
call
call div
div
push
4
push 4
push
push 22
call
call power
power
call
mult
call mult

code
generation

exp%

&

codeWrite(exp)

exp

n

"push n"

exp

,

exp

op(exp1)

codeWrite(exp1);

exp

(exp1 op exp2)

codeWrite(exp1); codeWrite(exp2);

exp

f (exp1, ..., expn)

codeWrite(exp1); ... codeWrite(exp1);

v

"push v"
"op";
"op";

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

"call f";
slide 17

Handling program flow
?

2.

+

if
if (cond)
(cond)
s1
s1
else
else
s2
s2

VM
VM code
code to
to compute
compute and
and push
push !(cond)
!(cond)
if-goto
if-goto L1
L1

code
generation

VM
VM code
code for
for executing
executing s1
s1
goto
goto L2
L2
label
label L1
L1
VM
VM code
code for
for executing
executing s2
s2

...
...

label
label L2
L2
...
...

?

2.

+

while
while (cond)
(cond)
ss
...
...

code
generation

label
label L1
L1
VM
VM code
code to
to compute
compute and
and push
push !(cond)
!(cond)
if-goto
if-goto L2
L2
VM
VM code
code for
for executing
executing ss
goto
goto L1
L1
label
label L2
L2
...
...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 18

Final example

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 19

Perspective
A

1
/
:
:

A

,

%

1
/

%
,

'

r = c.getRadius()
r = c.radius

for% switch% 4

%

char

let x=‘c’

(
B

.

% c=c+1

push c% push 1%

,

% pop c

4

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Compiler II: Code Generation

slide 20