5.7.1 Function Return Address Stack
The 8-bit PIC devices use a hardware stack for function return addresses. This stack cannot be manipulated directly and has a limited depth, which is indicated in your device data sheet and the relevant device chipinfo file.
Nesting functions too deeply can exceed the maximum hardware stack depth and lead to program failure. Remember that interrupts and implicitly called library functions also use this stack.
The compiler can be made to manage stack usage for some devices using the
-mstackcall
option (see 4.6.1.24 Stackcall Option). This enables an
alternate means of calling functions to allow function nesting deeper than the stack alone
would otherwise allow.
A call graph is provided by the code generator in the assembler list file (see 6.3.6 Call Graph). This will indicate the stack levels at each function call and can be used as a guide to stack depth. The code generator can also produce warnings if the maximum stack depth is exceeded.
The warnings and call graphs are guides to stack usage. Optimizations and the use of interrupts can decrease or increase the program’s stack depth over that determined by the compiler.