Sequential Computational Model
A function call can be described as a change of execution of instructions to the execution of instructions of the called functions, and where the arguments of the function call are made available to the called function. After the execution of instructions of the called function is finished, the result is made available at the point of the function call, and the execution of instructions prior to the change is continued.
Sequential Function Execution
A function call is implemented in the machine model using some convention. Usually that involves a stack for storing the information that is to be exchanged between caller and function. Below we describe such a scheme that is based on a calling sequence described in ''The C Language Calling Sequence'' (S.C. Johnson and D.M. Ritchie, Computing Science Technical Report No. 102, Bell Laboratories, September 1981) and the generated assembly code of some C compilers.- The arguments for the function are pushed onto the stack.
- The return address is pushed onto the stack.
- Control is passed to the function by setting the program counter to
the start of the function.
- The contents of registers used inside the function are saved on the stack.
- Stack space is allocated for local variables of the function.
- The body of the function is executed.
- The return value is stored somewhere on the stack so that it can be obtained by the caller.
- Stack space is freed and register values are restored.
- The return address is popped of the stack.
- Control is given back to the caller by setting the program counter to the return address.
- The return value is taken from the stack.
- All the values that had been pushed onto the stack are now popped from the stack and execution continues.
Generic Sequential Function Execution
In the scheme described above, the data on the stack is usually accessed through the register called stack pointer. The part of the stack that contains the data for a particular function call is called a stack frame, and holds the arguments for the function, the return address, and the local variables of the function. Alternatively, such a frame may be accessed through a special register called frame pointer pointing to the position of the frame on the stack, to allow for manipulation of the stack pointer during execution of the function.We can describe this more general without the use of a stack.
- The arguments for the function and the return address are put in a frame.
- The frame is saved in a place that is available to the function.
- An environment for the function is set up.
- Control is passed to the called function.
- The function builds up its own frame for storing local variables and saving contents of registes used inside the function.
- Arguments are taken from the frame.
- The body of the function is executed.
- The return value is saved in the frame of the caller.
- The function disposes it own frame.
- The return address is taken from the frame and control is given back to the caller.
- The environment for the functio is taken down.
- The return value is taken from the frame.
- The frame is disposed off.
Abstract Sequential Function Call
Using the generic model above, we can obtain an abstract model that hides the details of how a function call is implemented. From the caller's viewpoint in abstraction the call of the function can be seen as making the arguments available to the function. Making a similar abstraction on the function side the following scheme results.- Caller makes arguments and return address available.
- An environment for the function is set up.
- Control is passed to the function.
- Function initializes.
- Function gets arguments.
- Function executes its body.
- Function makes return value available.
- Function cleans up.
- Control is given back.
- Environment is take down.
- Caller gets return value.