Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 171
Figure 5-3 graphically shows an example of how the compiled stack is formed.
FIGURE 5-3:
FORMATION OF THE COMPILED STACK
Each function in the program is allocated a block of memory for its parameter, auto 
and temporary variables. Each block is referred to as an auto-parameter block (APB). 
The figure shows the APB being formed for function F2(), which has two parameters, 
a
 and b, and one auto variable, c.
The parameters to the function are first grouped in an order strictly determined by the 
lexical order in which they appear in the source code. These are then followed by any 
auto
 objects; however, the auto objects may be placed in any order. So we see mem-
ory for a is followed by that for b and lastly c.
Once these variables have been grouped, the exact location of each object is not 
important at this point and we can represent this memory by one block — the APB for 
this function.
The APBs are formed for all functions in the program. Then, by analyzing the call graph, 
these blocks are assigned positions, or base values, in the compiled stack.
Memory can be saved if the following point is observed: If two functions are never 
active at the same time, then their APBs can be overlapped.
In the example shown in the figure, F4() and F1() are active at the same time, in fact 
F1()
 calls F4(). However, F2(), F3() and F1() are never active at the same time; 
F1()
 must return before F2() or F3() can be called by main(). The function main() 
will always be active and so its APB can never overlap with that of another function.
In the compiled stack, you can see that the APB for main() is allocated unique mem-
ory. The blocks for F1(), F2() and F3() are all placed on top of each other and the 
same base value in the compiled stack; however, the memory taken up by the APBs 
for F1() and F4() are unique and do not overlap.
Our example also has an interrupt function, isr(), and its call graph is used to assem-
ble the APBs for any interrupt code in the same way. Being the root of a graph, isr() 
will always be allocated unique memory, and the APBs for interrupt functions will be 
allocated memory following.
F3
F6
F2
compiled 
stack
main
F1
F4
isr
F5
F2(int a , int b ) {
         ; 
}
F2
char c
int a
int b
a
b
b
c
Formation of auto-parameter block (APB)
for function F2
Overlap of non-concurrently active APBs
to form compiled stack
Analysis of call graph
main
    F1
        F4
    F2
    F3
isr
    F5
    F6
1
2
3
3
2
1
F
4
F
2
6
F
5