The Abstract Machine : ZINC - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

The Abstract Machine : ZINC

Description:

Why ZINC? Abstract machine for strict functional language : SECD, FAM, CAM ... The ZINC machine (cont.) Two compilation scheme : ... – PowerPoint PPT presentation

Number of Views:134
Avg rating:3.0/5.0
Slides: 33
Provided by: sungwo8
Category:
Tags: zinc | abstract | machine | zinc

less

Transcript and Presenter's Notes

Title: The Abstract Machine : ZINC


1
The Abstract Machine ZINC
???
  • POSTECH, CSE, PL lab
  • March 5, 2007

2
Why ZINC?
  • Abstract machine for strict functional language
  • SECD, FAM, CAM
  • Then, why dont we use them?
  • One of design requirements Efficient multiple
    application.
  • The existing abstract machine Inefficient!

3
Multiple application
  • (M N) evaluate M first, then N.
  • (M N1 N2)

(M N1 N2)
(M N1 N2)
(M N1
(A1
N2)
(A1 N2) suspend A1 and start evaluating N2
Allocating closure!
  • (M N1 Nk) k - 1 intermediate closures needed
  • But, these intermediate closures are
    unnecessary.
  • Why?

4
Closure
  • A Compiler said Closure? The axis of evil!!!
  • Why?
  • Cost of handling closure gtgt Cost of some other
    thing

TO DO More efficient multiple applicationHOW?
Reduce the number of closer!!
5
Efficient multiple applications
  • N-ary function
  • ex) f (x,y,z ) x y z
  • No closures needed (well performance)
  • But?
  • Currying
  • The power of currying Partial application.

ex)
6
Goal An abstract machine where a multiple
application to k arguments does not allocate any
closure.
ZINC!
7
The Krivines machine
  • A lazy graph reducer.
  • A term in de Bruijns notation.
  • (M N1 N2)

(M N1 N2)
(M N1 N2)
.
Push!
Push!
8
De Bruijn index
  • Example

?x.?y. x (y x)
?.?. 1 (0 1)
?s.?z. s (?x. x s)
?.?. 1 (?. 0 2)
?f.(?x. f (?y. (x x) y)) (?x. f (?y. (x x) y))
?? ?????
?.(?. 1 (?. (1 1) 0)) (?. 1 (?. (1 1) 0))
9
Krivines machine
  • Only Three instructions
  • Equipped with
  • - Code Pointer
  • Register
  • Holding environment (a list of closures
    (code,env))
  • Stack

10
Krivines machine (cont.)
11
Krivines machine (Example)
(?x. (x x)) (?x. x)
(?. (0 0)) (?. 0)
(?. (0 0)) (?. 0)
GrabPush(access(0))access(0)
Push(Grabaccess(0))GrabPush(access(0))access
(0)
(?. (0 0))
(?. 0)
Grabaccess(0)
12
Example (Cont.)
13
Example (Cont.)
(?x. (x x)) (?x. x)
??
(?x. x)
14
Multiple Applications
  • Krivines machine

(M N1 Nk)
k closures built To freeze the argument.
(unavoidable with a lazy stratege)
No closures are ever bulit to represent the
intermediate applications
15
Our Goal!
16
The problem with Krivines machine
  • Our goal A machine performing strict evaluation.
  • But ,Krivines machine Lazy evaluation.

empty
(?. ?. ?.M) N1 N2 N3
empty
N3 (closure 1)
(?. ?. ?.M) N1 N2
(?. ?. ?.M) N1
N2,N3 (closure 2)
(?. ?. ?.M)
N1,N2,N3 (closure 3)
(?. ?. ?.M)
N1
N2,N3
(?. ?.M)
N3
N2,N1
(?. M)
empty
N3,N2,N1
M
It does not stop until the stack is empty
17
The modified krivines machine
  • Lets put a mark on the stack!
  • The mark says
  • dont put me in the env, stop reducing, and
    resume another reduction
  • Fourth instruction Reduce
  • And a difference semantics for Grab

18
New Instruction Reduce
  • Marked closures are written ltc,egt instread (c,e)

19
Example
Reduce(N3 grab access 0) Reduce(N2 grab
access 0)Reduce(N1 grab access
0)GrabGrabGrabM
(?. ?. ?.M) N1 N2 N3
N1N2N3(?.0)
empty
(?. ?. ?.M) N1 N2 N3
empty
lt(?. ?. ?.M) N1 N2gt
N3
(?. ?. ?.M) N1 N2
(N3)
lt(?. ?. ?.M) N1gt, (N3)
N2
(?. ?. ?.M) N1
(N2),(N3)

(N1)(N2)(N3)
(?. ?. ?.M)
(N3)(N2)(N1)
empty
M
20
Of course, it allocates k marked closures.
But, they do not need to be heap-allocated
Wow! This is expected result!
21
The ZINC machine
  • The ZINC machine A Krivines machine with
    marks on the stack extended to handle constants
  • Equipped with
  • Code Pointer
  • Accumulator hold intermediate results
  • Register
  • Stack split into two stacks (for reducing
    stack moves)
  • Argument stack hold arguments to function
    calls
  • Return stack hold unallocated closures

22
The ZINC machine (cont.)
  • Two compilation scheme

Only valid for expression E in tail-call
position
Always valid, but usually less efficient.
23
Accessing local variables
24
Application
X
X
25
Abstractions
(?x. ?y. xy) z
(?y. zy)
X
X
X
X
(?x.x) (?y.y1) 4
26
Local declarations
27
Primitives
28
Example
let add a b a b in add 1 2
a bLetadd 1 2Endlet
?. ?. (p (1,0))
a b
Cur(?.(p (1,0)))
Cur (Grab Access 0 Push Access 1 Prim()
Return)
add 1 2
Pushmark 2 Push 1 Push add Apply
29
Example (Cont.)
Caml light code
Function code Klabel 2 Kgrab Klabel 3
Kaccess 0 Kpush Kaccess 1 Kprim
ltPaddintgt Kreturn Initial code Kclosure
2 Klet Klabel 1 Kpushmark Kquote 2
Kpush Kquote 1 Kpush Kaccess 0
Kapply Kendlet 1
Cur( Grab Access 0 Push Access 1
Prim() Return ) LetPushmark
2 Push 1 Push add Apply Endlet
?? ?????
30
Example (execution)
Function code Klabel 2 Kgrab Klabel 3
Kaccess 0 Kpush Kaccess 1 Kprim
ltPaddintgt Kreturn Initial code Kclosure
2 lt-start Klet Klabel 1 Kpushmark
Kquote 2 Kpush Kquote 1
Kpush Kaccess 0 Kapply Kendlet 1
31
Example (execution)
Function code Klabel 2 lt- here Kgrab Klabel
3 Kaccess 0 Kpush Kaccess 1 Kprim
ltPaddintgt Kreturn Initial code Kclosure
2 Klet Klabel 1 Kpushmark Kquote 2
Kpush Kquote 1 Kpush Kaccess 0
Kapply Kendlet 1
32
Any Question?
Thank you!
Write a Comment
User Comments (0)
About PowerShow.com