07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16-Bit C <strong>Compiler</strong> User’s Guide<br />

ordering, so these functions cannot rely on other user_init functions having been<br />

previously run; these functions will be called after PSV <strong>and</strong> data initialization. A<br />

user_init may still be called by the executing program. For example:<br />

void __attribute__((user_init)) initialize_me(void) {<br />

// per<strong>for</strong>m initalization sequence alpha alpha beta<br />

}<br />

weak<br />

See Section 2.3.1 “Specifying Attributes of Variables” <strong>for</strong> in<strong>for</strong>mation on the weak<br />

attribute.<br />

2.3.3 Inline Functions<br />

By declaring a function inline, you can direct the compiler to integrate that function’s<br />

code into the code <strong>for</strong> its callers. This usually makes execution faster by eliminating the<br />

function-call overhead. In addition, if any of the actual argument values are constant,<br />

their known values may permit simplifications at compile time, so that not all of the<br />

inline function’s code needs to be included. The effect on code size is less predictable.<br />

Machine code may be larger or smaller with inline functions, depending on the<br />

particular case.<br />

Note: Function inlining will only take place when the function’s definition is visible<br />

(not just the prototype). In order to have a function inlined into more than<br />

one source file, the function definition may be placed into a header file that<br />

is included by each of the source files.<br />

To declare a function inline, use the inline keyword in its declaration, like this:<br />

inline int<br />

inc (int *a)<br />

{<br />

(*a)++;<br />

}<br />

(If you are using the -traditional option or the -ansi option, write __inline__<br />

instead of inline.) You can also make all “simple enough” functions inline with the<br />

comm<strong>and</strong>-line option -finline-functions. The compiler heuristically decides<br />

which functions are simple enough to be worth integrating in this way, based on an<br />

estimate of the function’s size.<br />

Note: The inline keyword will only be recognized with -finline or<br />

optimizations enabled.<br />

Certain usages in a function definition can make it unsuitable <strong>for</strong> inline substitution.<br />

Among these usages are: use of varargs, use of alloca, use of variable-sized data,<br />

use of computed goto <strong>and</strong> use of nonlocal goto. Using the comm<strong>and</strong>-line option<br />

-Winline will warn when a function marked inline could not be substituted, <strong>and</strong> will<br />

give the reason <strong>for</strong> the failure.<br />

In compiler syntax, the inline keyword does not affect the linkage of the function.<br />

When a function is both inline <strong>and</strong> static, if all calls to the function are integrated<br />

into the caller <strong>and</strong> the function’s address is never used, then the function’s own<br />

assembler code is never referenced. In this case, the compiler does not actually output<br />

assembler code <strong>for</strong> the function, unless you specify the comm<strong>and</strong>-line option<br />

-fkeep-inline-functions. Some calls cannot be integrated <strong>for</strong> various reasons<br />

(in particular, calls that precede the function’s definition cannot be integrated <strong>and</strong><br />

neither can recursive calls within the definition). If there is a nonintegrated call, then the<br />

function is compiled to assembler code as usual. The function must also be compiled<br />

DS51284H-page 26 © 2008 <strong>Microchip</strong> Technology Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!