User guide
8.6. FUNCTIONS AND ROUTINES 151
ENTRY Li n C
1
. . . C
n
SAVE s
body of function or routine
ENDPROC
Li is the label alloc ate d for the entry point. As a debugging aid, the le ngt h of t he
function or routine name is given by n and i ts characters by the C
1
. . . C
n
. The SAVE
statement specifies the initial setting of S, which is just t he save space size (=3) plus
the numbe r of formal paramet er s . The state of the stack just after entry is shown in
figure 8.3.
A1 A2 An
S
P
Save space Procedure arguments
Figure 8.3: The stack frame on function or routine entry
The save space is used to hold the previous value of P, the return address and the
function entry address. Thus, the first ar gume nt of a function is always at position 3
relative to the P pointer. On some older versions of BCPL the size of the save space
was different.
The end of the body is marked by an ENDPROC statement which is non executable
but allows the code generator to keep track of nested definitions. In early versions of
OCODE, the first two argument s of ENTRY were interchanged and ENDPROC was given
a numerical argument.
The l anguage insists that arguments are laid out in consecutive locations on the
stack and that there is no limit to their number. This suggests that a good strategy is
to place the arguments of a call in the locations the y must occupy when the function or
routine is entered. Thus, a typical call E(E
1
, . . . , E
n
) is compiled by first incrementing
S to leave room for the save space in the new stack frame, then generate code to evaluate
the arguments E
1
, . . . , E
n
before generating code for E. The state is then as shown
in figure 8.4. Finally, either FNAP k or RTAP k is generated, depending on whether a
function or routine call is bei ng compiled. Notice that k is the distance between the
old and new stack frames.
The return from a routine is performed by RTRN which restores the previous value
of P and resumes execution from the return address. The return from a function is
performed by FNRN just after the function result has been evaluated on the top of the
stack. FNRN performs the same action as RTRN, after placing t he function result in a
special register (A) ready for FNAP to store it in the required location in th e previous
stack frame.