11.7 Stack Allocation

The 32-bit MCU device dedicates register W15 for use as a software stack pointer. All processor stack operations, including function calls, interrupts and exceptions, use the software stack. Upon Power-on or Reset, register W15 is initialized to point to a region of memory reserved for the stack. The stack grows upward, towards higher memory addresses.

The 32-bit MCU device also supports stack overflow detection. If the stack limit register SPLIM is initialized, the device will test for overflow on all stack operations. If an overflow should occur, the processor will initiate a stack error exception. By default, this will result in a processor Reset. Applications may also install a stack error exception handler by defining an interrupt function named __StackError. See Interrupt Vector Tables [DD] for details.

By default, MPLAB XC32 linker allocates the largest stack possible from unused data memory. Therefore, care should be taken when assigning symbols to data sections so as to leave room for the stack (see Directives that Define Sections).

The location and size of the stack is reported in the link map output file, under the heading Dynamic Memory Usage. Applications can ensure that at least a minimum sized stack is available by using the --stack command option. For example:

xc32-ld -o t.exe t1.o --stack=0x100

While performing automatic stack allocation, MPLAB XC32 linker increases the minimum required size by a small amount to accommodate the processing of stack overflow exceptions. The stack limit register SPLIM is initialized to point just below this extra space, which acts as a stack overflow guardband. If not enough memory is available for the minimum size stack plus guardband, the linker will report an error.

The default stack guardband size is 16 bytes. Applications can specify a different size by using the --stackguard command option. For example:

xc32-ld -o t.exe t1.o --stackguard=32

As an alternative to automatic stack allocation, the stack may be allocated directly with a user-defined section in assembly language. For example:

.section my_stack, stack
.space 0x100

When the stack is allocated in this way, the usable stack space will be slightly less than 0x100 bytes, since a portion of the user-defined section will be reserved for the stack guardband.

An appropriate worst case stack usage requirement can be determined using the Stack Usage Guidance analysis tool, which can provide a compile-time analysis static of the end executable. For more on this tool, see the “MPLAB® XC32 C Compiler User’s Guide for PIC32A MCU” (DS-50003831), section 7.8 “Stack Usage Guidance.”

Regardless of how the stack is allocated (automatically or by user-defined section) the linker creates two symbols for use by the startup module. __SP_init defines the initial value for the stack pointer (W15), and __SPLIM_init defines the initial value for the stack limit register (SPLIM).

The start-up module uses these symbols to initialize the stack pointer and stack pointer limit register. Normally the start-up module is provided by libc99-pic30-elf.a. In special cases, the application may provide its own start-up code. The following stack initialization sequence may be used:

mov.l      #__SP_init,w15    ; initialize w15
mov.l      #__SPLIM_init,w0  ;
mov.l      w0,_SPLIM         ; initialize SPLIM