Runtime
Environment
-
Function
Structure
ana
Calling
Conventions
5.3
Function
Structure
and
Calling
Conventions
A13
A14'
sp'
The C compiler imposes a strict set
of
rules on
function
calls. Except
for
spe-
cial runtime-support functions, any function that calls or
is
called
by
a C
function must
follow
these rules. Failure
to
adhere
to
these rules can disrupt
the
C environment and cause a program
to
fail.
Figure
5-2
illustrates a typical
function
call. Parameters are passed
to
this
function, the function uses
local variables,
but
no value is returned.
Before Call
After Passing
Arguments
Upon Entry
After
Saving
Used
Registers
Figure
5-2.
An
Example
of
a
Function
Call
After Allocating
Local Frame
5.3.1
Responsibilities
of
a
Calling
Function
5-8
A function performs the
following
tasks
when
it
calls another function. The
steps
below
show
examples
of
the code
that
the compiler
might
generate
for
a particular step. For these code examples, assume
that
a C
function
calls
another
function
f that has three arguments; the
function
call is:
f(argl,
arg2,
arg3);
1)
If
the called
function
returns a double
or
a float, the caller allocates space
on the program stack for the return
value. The caller
must
allocate this
space even
if it
doesn't use the return value.
2)
It
saves the program stack pointer
(A
14)
on
the
system stack. The caller
generates the
following:
MOVE
STK,
-*SP,
1
(This
is
only
done
when
the caller passes arguments
or
when
the called
function
returns a float or a
double
- that is,
when
the program stack is
affected.)