76 3 Functional Programming Languages
Our translation scheme also generates the code executed when an application
of the function is to be evaluated. This is sensible as this code must use the same
assignment of global variables to positions in the global vector that was used when
including the variable bindings. On the other hand, this instruction sequence must
not be executed when constructing the F-object itself: this code section therefore is
skipped to continue at the location B.
The code for evaluating a function call, thus, is between the labels A and B.Es-
sentially, it consists of the V-code for the body decorated with an instruction targ
k at the beginning and an instruction return k at the end where k is the number of
arguments of the function. These two instructions are discussed in Sect. 3.9.Here
we just mention that the instruction targ k is responsible for undersupply with argu-
ments, whereas the instruction return k is responsible for finalizing the evaluation
of the call and a possible oversupply with arguments.
Example 3.7.1 Consider the function f
≡ fun b → a + b for an address environ-
ment
ρ
= {a → (L,1)} and stack level sl = 1. The call code
V
f
ρ
1 is translated
into:
1 pushloc 00pushglob 02getbasic
2 mkvec 11eval 2 add
2 mkfunval A 1 getbasic 1 mkbasic
2 jump B 1 pushloc 11return 1
0 A : targ 12eval 2 B : ...
3.8 Function Application
Function applications are analogous to function calls in C. The instruction sequence
generated for a function application must ensure that the state of the stack satisfies
the assumptions of the code for the function definition when the function is entered.
The necessary actions for evaluating a function application e
e
0
... e
m−1
are:
• allocating a stack frame;
• passing of parameters, that is, evaluating the actual parameters in the case of
CBV or constructing closures for the actual parameters in the case of CBN;
• evaluating the function e
to an F-object;
• applying this function.
For CBN, this leads to the following translation scheme: