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

Product codes
SW006021-1
Page of 518
How To’s
 2012 Microchip Technology Inc.
DS52053B-page 69
• Make sure you do not have a "read-modify-write" problem. If the device you are 
using does not have a separate "latch" register (as is the case with mid-range PIC 
devices) this problem can occur, particularly if the port outputs are driving large 
loads, such as an LED. You may see that setting one bit turns off another or other 
unusual events. Create your own latch by using a temporary variable. Rather than 
read and write the port directly, make modifications to the latch variable. After 
modifications are complete, copy the latch as a whole to the port. This means you 
are never reading the port to modify it. Check the device literature for more 
detailed information.
3.7.5
How Do I Know If the Stack Has Overflowed?
The 8-bit PIC devices have a limited hardware stack that is only used for function (and 
interrupt function) return addresses, see Section 5.3.4 “Stack”. If the nesting of func-
tion calls and interrupts is too deep, the stack will overflow (wraps around and over-
writes previous entries). Code will then fail at a later point — sometimes much later in 
the call sequence — when it accesses the corrupted return address.
The compiler attempts to track stack depth and, when required, swap to a method of 
calling that does not need the hardware stack (PIC10/12/16 devices only). You have 
some degree of control over what happens when the stack depth has apparently over-
flowed, seSection 4.8.50 “--RUNTIME: Specify Runtime Environment” and the 
stackcall
 suboption.
A call graph shows the call hierarchy and depth that the compiler has determined. This 
graph is shown in the assembly list file. To understand the information in this graph, see 
Section 6.6.6 “Call Graph”.
Since the runtime behavior of the program cannot be determined by the compiler, it can 
only assume the worst case and may report that overflow is possible even though it is 
not. However, no overflow should go undetected if the program is written entirely in C. 
Assembly code that uses the stack is not considered by the compiler and this must be 
taken into account.
3.7.6
How Do I Fix a "Can’t find space..." Error?
There are a number of different variants of this message, but all essentially imply a sim-
ilar situation. They all relate to there being no free space large enough to place a block 
of data or instructions. Due to memory paging, banking or other fragmentation, this 
message can be issued when seemingly there is enough memory remaining. See 
Appendix B. “Error and Warning Messages” for more information on your particular 
error number.
3.7.7
How Do I Fix a "Can’t generate code..." Error?
This is a catch-all message which is generated if the compiler has exhausted all possi-
ble means of compiling a C expression, see Appendix B. “Error and Warning Mes-
sages”
. It d
oes not usually indicate a fault in your code. The inability to compile the 
code may be a deficiency in the compiler, or an expression that requires more registers 
or resources than are available at that point in the code. This is more likely to occur on 
baseline devices. In any case, simplifying the offending expression, or splitting a state-
ment into several smaller statements, usually allows the compilation to continue. You 
may need to use another variable to hold the intermediate results of complicated 
expressions.