Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 52
 2012 Microchip Technology Inc.
3.4.5
Functions
This section examines questions that relate to functions.
3.4.5.1
WHAT IS THE OPTIMUM SIZE FOR FUNCTIONS?
Generally speaking, the source code for functions should be kept small as this aids in 
readability and debugging. It is much easier to describe and debug the operation of a 
function which performs a small number of tasks and they typically have less side 
effects, which can be the source of coding errors. In the embedded programming world, 
a large number of small functions, and the calls necessary to execute them may result 
in excessive memory and stack usage, so a compromise is often necessary.
The PIC10/12/16 devices use pages in the program memory which is where the func-
tion code is stored and executed. Although the compiler will allow, and can encode, 
functions whose size (the size of the assembly code they generate) exceeds that of a 
program memory page, functions of such a size should be avoided and split into 
smaller routines where possible. The assembly call and jump sequences to locations 
in other pages are much longer than those made to destinations in the same page. If a 
function is so large as to cross a page boundary, then loops, or other code constructs 
that require jumps within that function, may use the longer form of jump on each itera-
tion, see Section 5.8.3 “Allocation of Executable Code”.
PIC18 devices are less affected by internal memory paging and the instruction set 
allows for calls and jumps to any destination with no penalty, but you should still 
endeavor to keep functions as small as possible.
With all devices, the smaller the function, the easier it is for the linker to allocate them 
to memory without errors.
3.4.5.2
HOW DO I STOP AN UNUSED FUNCTION BEING REMOVED?
If a C function’s symbol is referenced in hand-written assembly code, the function will 
never be removed, even if it is not called or never had its address taken in C code.
Create an assembly source file and add this file to your project. You only have to refer-
ence the symbol in this file, so the file can contain the following
GLOBAL _myFunc
where myFunc is the C name of the function in question (note the leading underscore 
in the assembly name, see Section 5.12.3.1 “Equivalent Assembly Symbols”). This 
is sufficient to prevent the function removal optimization from being performed.
3.4.5.3
HOW DO I MAKE A FUNCTION INLINE?
You can ask the compiler to inline a function by using the inline specifier. This is only 
a suggestion to the compiler and may not always be obeyed. Do not confuse this spec-
ifier with the inline pragma (Section 5.14.4.4 “The #pragma Intrinsic Directive”
which is for functions that have no corresponding source code and which will be spe-
cifically expanded by the code generator during compilation.