beautypg.com

Static frames, Dynamic frames – Zilog Z8F0130 User Manual

Page 225

background image

UM013037-1212

Call Frames

Zilog Developer Studio II – Z8 Encore!

User Manual

201

Static Frames

In the static frames scheme, for each function in the program, a single frame is statically
allocated at compile time for storing the call frame containing the parameters and local
variables of the function.

Static call frames can significantly increase code efficiency. However, this is a restrictive
call-frame scheme. This option must be used with some care because errors ensue if it is
applied blindly and your code uses either recursion or calls through function pointers. You
can avoid those errors by finding the functions that use those language features and declar-
ing them reentrant. In the case of function pointers, it is the functions to which the pointers
refer, not the functions that make the calls that must be marked as reentrant.

The advantage of static frames is that because the compiler knows the absolute address of
each function parameter, it can generate more compact code to access parameters than in
dynamic frames, where they must be accessed by offsetting from the stack pointer. For the
Z8 Encore! instruction set architecture, this code size savings is substantial. The savings
comes primarily not from using less space for frames, but from using less code to access
data in the frames. Thus, it is primarily a savings in code space, not in data space. It could
actually require more data space, although to mitigate this, the Z8 Encore! linker uses call-
graph techniques to overlay some function frames that cannot be simultaneously active.

The disadvantages of static frames are that they do not support two features of the C lan-
guage: recursion and making calls through function pointers. To allow a broader range of
applications to get the benefits of using static frames, the Z8 Encore! compiler provides
the

reentrant

keyword as another C language extension.

Dynamic Frames

The most familiar type of call frames, used exclusively by most desktop-oriented compil-
ers, are dynamic frames: when a function is called, space is dynamically allocated on the
stack to hold its return address, function parameters, and local variables.

Dynamic frames hold arguments and local variables on the run-time stack, allow recur-
sion, and allow reentrancy. Dynamic frames are the usual way of storing information
about an instance of a function call. Passing argument in dynamic frames is done by push-
ing the arguments on the stack in reverse (right to left) order.

Reentrant Keyword

This keyword notifies the compiler that in an application that otherwise uses static frames,
a dynamic frame must be used for any function declared reentrant.

For example, to declare the

recursive_fn

function as using a dynamic call frame, use

the following syntax:

reentrant int recursive_fn (int k)

{

if (k == 0)