36
Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

  • View
    223

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Compiler constructionin4020 – lecture 13

Koen Langendoen

Delft University of TechnologyThe Netherlands

Page 2: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Summary of lecture 12

Memory management

• manual• malloc() + free()

• garbage collection• reference counting

• mark & sweep

• two-space copying

A

C

D

E B

heap

root set

B B E E

F

Page 3: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Quiz

5.9 The pointer assignment p := q is han-dled by first incrementing the reference count of q before decrementing that of p

What goes wrong if the order is reversed?

Increment q .ref count;

Decrement p .ref count;

IF p .ref count = 0:

Free recursively (p);

SET p TO q;

??

Page 4: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Poll results

• lectures:

• practicum:

• suggestions:

++

+-• fun assignments

• poor documentation

• lot of work!

min avg max

CS 12 29 80

EE 40 64 70

improve docs:

• LLgen

• Asterix sources

• assignment 2

Page 5: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

• languages: LISP, scheme, ML, Miranda, Haskell

• features:• high abstraction level: what vs how, where, when

• equational reasoning

• functions as first class citizens

Functional programming

• languages: LISP, Scheme, ML, Miranda, Haskell

• features:

compiler must work harder!!

Page 6: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Overview of a typicalfunctional compiler

high-level language

(Haskell)

functional core

C runtime system+

optimizations

desugaringtype inference

code generation

Page 7: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Function application

• concise notation

• precedence over all other operators

Haskell C

f 11 13 f(11, 13)

f n+1 f(n) + 1

Page 8: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

• offside rule

• list notation

• pattern matching

• list comprehension

• offside rule: end-of-equation marking

• list notation

• pattern matching

• list comprehension

• offside rule

• list notation:

• pattern matching

• list comprehension

[] [1,2,3] (1:(2:(3:[])))

• offside rule

• list notation

• pattern matching: case analysis of arguments

• list comprehension

Syntactic sugar

qsort [] = []

qsort (x:xs) = qsort [y | y <- xs, y < x]

++ [x]

++ qsort [y | y <- xs, y >= x]

• offside rule

• list notation

• pattern matching

• list comprehension: mathematical sets

Page 9: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Polymorphic typing

• an expression is polymorphic if it ‘has many types’

• examples

length [] = 0

length (x:xs) = 1 + length xs

• empty list: [ ]

• list handling functions

length :: [a] -> Int

length [] = 0

length (x:xs) = 1 + length xs

Page 10: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Polymorphic type inference

map f [] = []

map f (x:xs) = f x : map f xs

map :: t1 -> t2 -> t3

map f [] = []

map :: t1 -> [a] -> [b]

map f (x:xs) = f x : map f xs

x :: a

f :: a -> b

map :: (a -> b) -> [a] -> [b]

Page 11: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Exercise (5 min.)

• infer the polymorphic type of the following higher-order function:

filter f [] = []

filter f (x:xs) = if not(f x) then filter f xs

else x : (filter f xs)

Page 12: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Answers

Page 13: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

filter f [] = []

filter f (x:xs) = if not(f x) then filter f xs

else x : (filter f xs)

Answers

filter :: t1 -> t2 -> t3

filter f [] = []

filter :: t1 -> [a] -> [b]

x :: a

f :: a -> Bool

filter :: (a -> Bool) -> [a] -> [a]

filter f (x:xs) = if not(f x) then filter f xs

else x : (filter f xs)

Page 14: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Referential transparency

f x always denotes the same value

• advantage: high-level optimization is easy

• disadvantage: no efficient in-place update

g (f x) (f x)let a = f x

in g a a

add_one [] = []

add_one (x:xs) = x+1 : add_one xs

Page 15: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Higher-order functions

• functions are first-class citizens

• higher-order functions accept functions as parameters and/or return a function as result

• functions may be created “on the fly”

D f = f where f (x) = limh0

f(x+h) – f(x)

h

diff f = f_

where

f_ x = (f (x+h) – f x)/h

h = 0.0001

Page 16: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Currying: specialize functions

Q: diff (unary function) deriv (binary function)?

deriv f x = (f (x+h) – f x)/h

where

h = 0.0001

diff f = f_

where

f_ x = (f (x+h) – f x)/h

h = 0.0001

f, x (diff f) x = deriv f xA: yes!

binary function a unary function returning a unary function

f e1 … en (n f e1) … en)

(deriv square) is a curried function

Page 17: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Lazy evaluation

An expression will only evaluated when its value is needed to progress the computation

• additional expressive power (infinite lists)

• overhead for delaying/resuming computations

nats = [1..]

squares = [x^2 | x <- nats]

main = take 100 squares

ones = 1 : ones

deriv f x = lim [(f (x+h) – f x)/h | h <- downto 0]

where

downto x = [x + 1/2^n | n <- [1..]]

lim (a:b:lst) = if abs (a/b – 1) < eps then b

else lim (b:lst)

Page 18: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Break

Page 19: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Structure of a typicalfunctional compiler

high-level language

(Haskell)

functional core

C runtime system+

optimizations

desugaringtype inference

code generation

Page 20: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Graph reduction

• implement h.o.f + lazy evaluation

• key: function application f e1 … en (n f e1) e2) … en)

• execution (interpretation)• build graph for main expression

• find reducible expression (redex func + args)

• instantiate body (build graph for rhs)

@

f e1

@

e2

@

en(n f @ e1) @ e2) … @ en)

Page 21: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Example

let

twice f x = f (f x)

square n = n*n

in

twice square 3

@

twice square

@

3

@

square @

3

* root

@

square 3

*

*

Page 22: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Reduction order

• a graph may contain multiple redexes

• lazy evaluation: choose top-most @-node

• built-in operators (+,-,*, etc) may have strict arguments that must be evaluated => recursive invocation

@

square @

3

@

square 3

*

@

square 3

@

*

@

Page 23: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Implementation

typedef struct node *Pnode;

extern Pnode eval( Pnode root);argument stack

FP

@

square 3

@

*

@

Graph reduction – three stroke engine• find next redex

• instantiate rhs

• update root

• unwind application spine (f a1 …an)

Pnode mul(Pnode arg[]) {

Pnode a = eval(arg[0]);

Pnode b = eval(arg[1]);

return Num(a->nd.num * b->nd.num);

}

• update root with result

• call f, pass arguments in array (stack)

Page 24: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Code generation

average a b = (a+b) / 2

Pnode average(Pnode arg[]) {

Pnode a = arg[0];

Pnode b = arg[1];

return Appl(Appl(fun_div,

Appl(Appl(fun_add,a),b)),

Num(2));

}

2@

/

@

b@

+

@

a

Page 25: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Short-circuiting application spines

average a b = (a+b) / 2

Pnode average(Pnode arg[]) {

Pnode a = arg[0];

Pnode b = arg[1];

return Appl(Appl(fun_div,

Appl(Appl(fun_add,a),b)),

Num(2));

}• call leftmost outermost function directly

Pnode average(Pnode arg[]) {

Pnode a = arg[0];

Pnode b = arg[1];

return div(Appl(Appl(fun_add,a),b),

Num(2));

}

2@

/

@

b@

+

@

a

Page 26: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

• call leftmost outermost function directly

Pnode average(Pnode arg[]) {

Pnode a = arg[0];

Pnode b = arg[1];

return div(Appl(Appl(fun_add,a),b),

Num(2));

}

Strict arguments

average a b = (a+b) / 2

• evaluate expressions supplied to strict built-in functions immediately

Pnode average(Pnode arg[]) {

Pnode a = arg[0];

Pnode b = arg[1];

return div(add(a,b), Num(2));

}

2@

/

@

b@

+

@

a

Page 27: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Strictness analysis

user-defined functions:• propagate sets of strict arguments up the AST

foo x y = if x>0 then x*y

else 0

if

0>

x 0

*

x y

{x} {}

{}{x} {x} {y}

{x}

{x,y}

Page 28: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Strictness propagation

Ai , if n mfunm @ A1 @ … @ An

……

C (T E)if C then T else E

L RL R

propagated setlanguage construct

strict(fun,i)m

i=1

Page 29: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Recursive functions

foo x y = if x>0 then y

else foo (x+1) y

if

>

x 0

*

x y

{x}

{}foo

y

{x} {x,y}

{}{x} {x} {y}{x}

+

x 1

{y}

{x} {}

problem: conservative estimation of strictness

Page 30: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Recursive functions

foo x y = if x>0 then y

else foo (x+1) y

if

>

x 0

*

x y

{x,y}

{x,y}foo

y

{x} {x,y}

{}{x} {x} {y}

+

x 1

{x} {y}

{x} {}

solution: optimistic estimation of strictness

iterate until result equals assumption

(+,+)

Page 31: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Exercise (6 min.)

• infer the strict arguments of the following recursive function:

• how many iterations are needed?

g x y 0 = x

g x y z = g y x (z-1)

Page 32: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Answers

step assumption result

1

2

3

4

Page 33: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Answers

g x y 0 = x

g x y z = g y x (z-1)

g x y z = if z == 0 then x

else g y x (z-1)

4

{z}{z}3

{z}{x,z}2

{x,z}{x,y,z}1

resultassumptionstep

Page 34: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

Summary

Haskell feature compiler phase

offside rule lexical analyzer

list notation

list comprehension

pattern matching

parser

polymorphic typing semantic analyzer

referential transparency

higher-order functions

lazy evaluation

run-time system

(graph reducer)

Page 35: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

The end

Page 36: Compiler construction in4020 – lecture 13 Koen Langendoen Delft University of Technology The Netherlands

TODO

• assignment 2:• make Asterix OO

• deadline June 4 08:59

• study book• chapter 1 – 7, except 4.2.6

• make appointment by e-mail for oral exam• 30 min per group

[email protected]