3.10 Recursive Variable Definitions 85
code
V
e
ρ
sl = alloc n // allocates the local variables
code
C
e
1
ρ
(sl + n)
rewrite n
...
code
C
e
n
ρ
(sl + n)
rewrite 1
code
V
e
0
ρ
(sl + n)
slide n // releases the local variables
where
ρ
=
ρ
⊕{y
i
→ (L, sl + i) | i = 1,...,n}. In the case of CBV,theex-
pressions e
1
,...,e
n
are translated by the code generation function code
V
as well.
The reader should realize that, in the case of CBV, evaluating the expressions e
j
must
not access the values of variables y
i
with i ≥ j. This can be guaranteed for CBV,if
recursive definitions are only allowed for functions.
The dummy closures are overwritten sequentially. At the start of the execution
of the code for any of the e
i
, as well as at the start of the execution for the main
expression e
0
, the stack level is equal to the stack level before the letrec expression
increased by n.
Example 3.10.1 Consider the expression:
e
≡ let rec f = fun xy→ if y ≤ 1 then x
else f
(x · y)(y − 1)
in f 1
in an empty address environment
ρ
= ∅ with stack level sl = 0. Then (for CBV)we
obtain:
0 alloc 1 0 A : targ 2 4 loadc 1
1 pushloc 0 0 ... 5 mkbasic
2 mkvec 1 1 return 2 5 pushloc 4
2 mkfunval A 2 B : rewrite 1 6 apply
2 jump B 1 mark C 2 C : slide 1
where, for brevity, we have left out the code for the body of f .
We now have all parts together to translate and execute programs of the FULpro-
gramming language with CBV semantics. For the implementation of CBN,theim-
plementation of the instruction eval for evaluating closures is still missing — and of
course the translation function code
C
for constructing closures. This is the topic of
the next section.