Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual
Product codes
SW006021-1
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 154
2012 Microchip Technology Inc.
Information about the pointers and their targets are shown in the pointer reference
graph, which is described in Section 6.6.5 “Pointer Reference Graph”. This graph is
printed in the assembly list file, which is controlled by the option described in
Section 4.8.17 “--ASMLIST: Generate Assembler List Files”.
graph, which is described in Section 6.6.5 “Pointer Reference Graph”. This graph is
printed in the assembly list file, which is controlled by the option described in
Section 4.8.17 “--ASMLIST: Generate Assembler List Files”.
Consider the following mid-range device program in the early stages of development.
It consists of the following code:
It consists of the following code:
int i, j;
int getValue(const int * ip) {
return *ip;
}
void main(void) {
j = getValue(&i);
// ... code that uses j
}
A pointer, ip, is a parameter to the function getValue(). The pointer target type uses
the qualifier const because we do not want the pointer to be used to write to any
objects whose addresses are passed to the function. The const qualification serves
no other purpose and does not alter the format of the pointer variable.
the qualifier const because we do not want the pointer to be used to write to any
objects whose addresses are passed to the function. The const qualification serves
no other purpose and does not alter the format of the pointer variable.
If the compiler allocates the variable i (defined in main()) to bank 0 data memory, it
will also be noted that the pointer ip (parameter to getValue()) only points to one
object that resides in bank 0 of the data memory. In this case, the pointer, ip, is made
an 8-bit wide data pointer. The generated code that dereferences ip in getValue()
will be generated assuming that the address can only be to an object in bank 0.
will also be noted that the pointer ip (parameter to getValue()) only points to one
object that resides in bank 0 of the data memory. In this case, the pointer, ip, is made
an 8-bit wide data pointer. The generated code that dereferences ip in getValue()
will be generated assuming that the address can only be to an object in bank 0.
As the program is developed, another variable, x, is defined and (unknown to the pro-
grammer) is allocated space in bank 2 data memory. The main() function now looks
like:
grammer) is allocated space in bank 2 data memory. The main() function now looks
like:
int i, j; // allocated to bank 0 in this example
int x; // allocated to bank 2 in this example
int getValue(const int * ip) {
return *ip;
}
void main(void) {
j = getValue(&i);
// ... code that uses j
j = getValue(&x);
// ... code that uses j
}
The pointer, ip, now has targets that are in bank 0 and in bank 2.To be able to accom-
modate this situation, the pointer is made 16 bits wide, and the code used to derefer-
ence the pointer will change accordingly. This takes place without any modification to
the source code.
modate this situation, the pointer is made 16 bits wide, and the code used to derefer-
ence the pointer will change accordingly. This takes place without any modification to
the source code.
One positive aspect of tracking pointer targets is less of a dependence on pointer qual-
ifiers. The standard qualifiers const and volatile must still be used in pointer defi-
nitions to indicate a read-only or externally-modifiable target object, respectively.
However, this is in strict accordance with the ANSI C standard. Non-standard qualifiers,
like near and bank2, are not required to indicate pointer targets, have no effect, and
should be avoided. Omitting these qualifiers will result in more portable and readable
code, and reduce the chance of extraneous warnings being issued by the compiler.
ifiers. The standard qualifiers const and volatile must still be used in pointer defi-
nitions to indicate a read-only or externally-modifiable target object, respectively.
However, this is in strict accordance with the ANSI C standard. Non-standard qualifiers,
like near and bank2, are not required to indicate pointer targets, have no effect, and
should be avoided. Omitting these qualifiers will result in more portable and readable
code, and reduce the chance of extraneous warnings being issued by the compiler.