• Tidak ada hasil yang ditemukan

lecture 11 compiler II

N/A
N/A
Protected

Academic year: 2017

Membagikan "lecture 11 compiler II"

Copied!
20
0
0

Teks penuh

(1)

www.nand2tetris.org

(2)

Course map

Assembler

Chapter 6

H.L. Language &

Operating Sys.

abstract interface

Compiler

Chapters 10 - 11

VM Translator

Chapters 7 - 8

Computer Architecture

Chapters 4 - 5

Gate Logic

Chapters 1 - 3 Electrical

Engineering

Physics

Virtual Machine

abstract interface

Software hierarchy

Assembly Language

abstract interface

Hardware hierarchy

Machine Language

abstract interface

Hardware Platform

abstract interface

Chips & Logic Gates

abstract interface

Human Thought

Abstract design

(3)

The big picture

(Chapter 11)

Jack Program

Toke-nizer Parser

Code Gene -ration

Syntax Analyzer Jack Compiler

VM code XML code

(Chapter 10)

this lecture previous

(4)

Syntax analysis

(review)

Class Bar {

method Fraction foo(int y) { var int temp; // a variable let temp = (xxx+12)*-63;

... ...

Class Bar {

method Fraction foo(int y) { var int temp; // a variable let temp = (xxx+12)*-63;

... ...

<varDec>

<keyword> var </keyword> <keyword> int </keyword>

<identifier> temp </identifier> <symbol> ; </symbol>

</varDec> <statements>

<letStatement>

<keyword> let </keyword>

<identifier> temp </identifier> <symbol> = </symbol>

<expression> <term>

<symbol> ( </symbol> <expression>

<term>

<identifier> xxx </identifier> </term>

<symbol> + </symbol> <term>

<int.Const.> 12 </int.Const.> </term>

</expression> ...

<varDec>

<keyword> var </keyword> <keyword> int </keyword>

<identifier> temp </identifier> <symbol> ; </symbol>

</varDec> <statements>

<letStatement>

<keyword> let </keyword>

<identifier> temp </identifier> <symbol> = </symbol>

<expression> <term>

<symbol> ( </symbol> <expression>

<term>

<identifier> xxx </identifier> </term>

<symbol> + </symbol> <term>

<int.Const.> 12 </int.Const.> </term>

</expression>

...

Syntax analyzer

(5)

Memory segments

(review)

3

i

+

segment

&

static

:

,

,

%

,

argument:

,

local:

,

this:

#! ,)

"$

,

,)

that:

#

%

$

constant:

0

4

32767

(

$

pointer:

this that

temp:

5+

,

6

,

2.

2.

pop

segment i

push

segment i

2.

pop

segment i

(6)

Code generation example

method int foo() { var int x;

let x = x + 1; ...

method int foo() { var int x;

let x = x + 1; ...

<letStatement>

<keyword> let </keyword> <identifier> x </identifier> <symbol> = </symbol>

<expression> <term>

<identifier> x </identifier> </term>

<symbol> + </symbol> <term>

<constant> 1 </constant> </term>

</expression> </letStatement> <letStatement>

<keyword> let </keyword> <identifier> x </identifier> <symbol> = </symbol>

<expression> <term>

<identifier> x </identifier> </term>

<symbol> + </symbol> <term>

<constant> 1 </constant> </term>

</expression> </letStatement>

Syntax

analysis

#

x

,

$

push local 0

push constant 1

add

pop local 0

push local 0

push constant 1

add

pop local 0

(7)

Handling variables

3

, %

x

%

%

1 &

3

x

0

7

%

89 #

$ 7

#:

1 &

;8.

$

3

1

,

x

7

local, static, field, argument

7

# 3

1 &

6

(8)

Handling variables:

mapping them on memory segments

(example)

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

5

<

%

static

%

=

>+,

,

(9)

Handling variables:

symbol tables

? &

,

,

,

1

,

%

&

1

& 1

(10)

Handling variables:

managing their life cycle

2

,

static

,

, 1

field

,

, 1

,)

local

,

,

% 1

argument

,

local

,

(11)

class Complex {

// Fields (properties): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex number */ public Complex (int re, int im) {

this.re = re; this.im = im; }

... }

class Foo {

public void bla() { Complex a, b, c; ...

a = new Complex(5,17); b = new Complex(12,192); ...

c = a; // Only the reference is copied ...

}

class Complex {

// Fields (properties): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex number */ public Complex (int re, int im) {

this.re = re; this.im = im; }

... }

class Foo {

public void bla() { Complex a, b, c; ...

a = new Complex(5,17); b = new Complex(12,192); ...

c = a; // Only the reference is copied ...

}

A

Handling objects:

construction / memory allocation

? &

foo = new ClassName(…) 7

foo = Memory.alloc(n)

3 n , &

,) = % Memory.alloc '

,

(12)

Handling objects:

accessing fields

class Complex {

// Properties (fields): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex number */ public Complex(int re, int im) {

this.re = re; this.im = im; }

...

/** Multiplies this Complex number by the given scalar */

public void mult (int c) { re = re * c;

im = im * c; }

... }

class Complex {

// Properties (fields): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex number */ public Complex(int re, int im) {

this.re = re; this.im = im; }

...

/** Multiplies this Complex number by the given scalar */

public void mult (int c) { re = re * c;

im = im * c; }

... }

A

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

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

times

(argument 0)

? &

im = im * c

?

1

&

,

,

,

(13)

8 b r & &

? & # A $

b.radius = r 7

// Get b's base address: push argument 0

// Point the this segment to b: pop pointer 0

// Get r's value push argument 1

// Set b's third field to r:

// Get b's base address:

push argument 0

// Point the this segment to b:

pop pointer 0

// Get r's value

push argument 1

// Set b's third field to r:

412 3012 ...

... High level program view RAM view

0

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

0 0

1

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

3012

argument pointer this

...

3

(this0

is now alligned with

RAM[3012])

... Virtual memory segments just after

the operation b.radius=17:

argument pointer this

Handling objects:

establishing access to the object’s fields

C 1

&

,)

b Ball

8

Ball x

%

y

(14)

Handling objects:

method calls

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

push foo push v1 push v2 ...

call bar class Complex {

// Properties (fields): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex object. */ public Complex(int re, int im) {

this.re = re;

public void bla() { Complex x;

...

x = new Complex(1,2); x.mult(5);

... }

}

class Complex {

// Properties (fields): int re; // Real part

int im; // Imaginary part ...

/** Constructs a new Complex object. */ public Complex(int re, int im) {

this.re = re;

public void bla() { Complex x;

...

(15)

class Bla { ...

void foo(int k) { int x, y;

int[] bar; // declare an array ...

// Construct the array: bar = new int[10]; ...

bar[k]=19; }

...

Main.foo(2); // Call the foo method ...

class Bla { ...

void foo(int k) { int x, y;

int[] bar; // declare an array ...

// Construct the array:

bar = new int[10];

...

bar[k]=19; }

...

Main.foo(2); // Call the foo method ...

A

? &

bar = new int(n) 7

bar = Memory.alloc(n)

Handling arrays:

declaration / construction

19 4315

4316

4317

4324

(bar array)

...

(local 0) (local 1) (local 2)

(16)

class Bla { ...

void foo(int k) { int x, y;

int[] bar; // declare an array ...

// Construct the array: bar = new int[10]; ...

bar[k]=19; }

...

Main.foo(2); // Call the foo method ...

class Bla { ...

void foo(int k) { int x, y;

int[] bar; // declare an array ...

// Construct the array: bar = new int[10]; ...

bar[k]=19;

} ...

Main.foo(2); // Call the foo method ...

A

? &

bar[k] = 19 7

// bar[k]=19, or *(bar+k)=19 push bar

push k add

// Use a pointer to access x[k] pop addr // addr points to bar[k] push 19

pop *addr // Set bar[k] to 19 // bar[k]=19, or *(bar+k)=19 push bar

push k add

// Use a pointer to access x[k] pop addr // addr points to bar[k] push 19

pop *addr // Set bar[k] to 19

2. # $

// bar[k]=19, or *(bar+k)=19 push local 2

push argument 0 add

// Use the that segment to access x[k] pop pointer 1

push constant 19 pop that 0

// bar[k]=19, or *(bar+k)=19 push local 2

push argument 0 add

// Use the that segment to access x[k] pop pointer 1

push constant 19 pop that 0

(bar array)

...

(local 0) (local 1) (local 2)

(argument 0) 275

(17)

syntax analysis

Handling expressions

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

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

?

+

push 5 push z add push 8 neg

call div push 4 push 2 call power call mult

push 5 push z add push 8 neg

call div push 4 push 2 call power

call mult

code

generation

2.

exp

%

&

codeWrite(

exp

)

exp

n

"push n"

exp

,

v

"push v"

exp

op(exp

1

)

codeWrite(exp1); "op";

exp

(exp

1

op exp

2

)

codeWrite(exp1); codeWrite(exp2); "op";

(18)

Handling program flow

if (cond) s1

else s2

...

if (cond) s1 else

s2 ...

?

+

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

VM code for executing s1 goto L2

label L1

VM code for executing s2 label L2

...

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

VM code for executing s1 goto L2

label L1

VM code for executing s2 label L2

...

2.

code

generation

while (cond) s

...

while (cond) s

...

?

+

label L1

VM code to compute and push !(cond)

if-goto L2

VM code for executing s

goto L1

label L2 ... label L1

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

VM code for executing s goto L1

label L2 ...

2.

code

(19)
(20)

Perspective

A 1

/

:

:

,

%

r = c.getRadius()

r = c.radius

A 1

/

%

for

%

switch

% 4

,

char

%

let x=‘c’

'

(

B

%

c=c+1 push c

%

push 1

%

%

pop c

Referensi

Dokumen terkait

Mikrokontroler adalah Sebuah sistem mikroprosessor dimana didalamnya sudah terdapat CPU, ROM, RAM, I/0, clock dan peralatan internal lainnya yang sudah terhubung dan

other digital platforms, each equipped with its own VM implementation RISC machine language Hack computer Hack machine language CISC machine language CISC machine.. a high-level

Event driven programming ◮ GUI components such as buttons, checkboxes generate high level events ◮ Each event is automatically sent to a listener ◮ Listener capability is described

Accordingly, the Committee recommended inter alia: that in all High Courts, a single grade of practitioners entitled to plead, to be called advocates not Barristers, should be

Other Examples Differentiator/High Pass Filter Integrator/Low Pass Filter... Other Examples Summing Amplifier Instrumentation

Physical education teachers are required to perform the following tasks: - ensure that the lesson passes at a high level and complete the corresponding class physical education

Vibration Isolation in Rotating Systems Vibration isolators system isolator system isolator Machine Suspensions Reduce the magnitude of force transmitted from a machine to its

Usually it is also a sign of high risk group for T2D in even next generation lack of tolerance • High weight baby if not treated • Diagnosis level is 5.1, which is low, but that’s