Title: The Abstract Machine : ZINC
1The Abstract Machine ZINC
???
- POSTECH, CSE, PL lab
- March 5, 2007
2Why 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!
3Multiple application
- (M N) evaluate M first, then N.
(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?
4Closure
- 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!!
5Efficient 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)
6Goal An abstract machine where a multiple
application to k arguments does not allocate any
closure.
ZINC!
7The Krivines machine
- A lazy graph reducer.
- A term in de Bruijns notation.
(M N1 N2)
(M N1 N2)
.
Push!
Push!
8De Bruijn index
?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))
9Krivines machine
- Equipped with
- - Code Pointer
- Register
- Holding environment (a list of closures
(code,env)) - Stack
10Krivines machine (cont.)
11Krivines 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)
12Example (Cont.)
13Example (Cont.)
(?x. (x x)) (?x. x)
??
(?x. x)
14Multiple Applications
(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
15Our Goal!
16The 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
17The 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
18New Instruction Reduce
- Marked closures are written ltc,egt instread (c,e)
19Example
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
20Of course, it allocates k marked closures.
But, they do not need to be heap-allocated
Wow! This is expected result!
21The 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
22The ZINC machine (cont.)
Only valid for expression E in tail-call
position
Always valid, but usually less efficient.
23Accessing local variables
24Application
X
X
25Abstractions
(?x. ?y. xy) z
(?y. zy)
X
X
X
X
(?x.x) (?y.y1) 4
26Local declarations
27Primitives
28Example
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
29Example (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
?? ?????
30Example (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
31Example (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
32Any Question?
Thank you!