Indirect Calls

When functions are called indirectly using a pointer, the compiler employs a variety of techniques to call the intended function.

The PIC18 and Enhanced Mid-range devices all use the value in the function pointer to load the program counter with the appropriate address. For PIC18 devices, the code loads the TOS registers and executes a return to perform the call. For Enhanced Mid-range devices, the callw instruction is used. The number of functions that can be called indirectly is limited only by the available memory of the device.

The Baseline and Mid-range devices all use a lookup table which is loaded with jump instructions. The lookup table code is called and an offset is used to execute the appropriate jump in the table. The table increases in size as more functions are called indirectly, but cannot grow beyond 0xFF bytes in size. This places a limit on the number of functions that can be called indirectly, and typically this limit is approximately 120 functions. Note that this limit does not affect the number of function pointers a program can define, which are subject to the normal limitations of available memory on the device.

Indirect calls are not affected by the -mstackcall option and the depth of indirect calls on Baseline and Mid-range devices are limited by the hardware stack depth.