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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Automatic variables are owned by the enclosing function <strong>and</strong> do not need the boot<br />

attribute. They may be assigned initial values, as shown:<br />

void __attribute__((boot)) chuck_cookies()<br />

{<br />

int hurl;<br />

int them = 55;<br />

char *where = "far";<br />

splat(where);<br />

/* ... */<br />

}<br />

Note that the initial value of where is based on a string literal which is allocated in the<br />

PSV constant section .boot_const. The compiler will set PSVPAG to the correct<br />

value upon entrance to the function. If necessary, the compiler will also restore PSV-<br />

PAG after the call to splat().<br />

const<br />

Many functions do not examine any values except their arguments, <strong>and</strong> have no effects<br />

except the return value. Such a function can be subject to common subexpression<br />

elimination <strong>and</strong> loop optimization just as an arithmetic operator would be. These<br />

functions should be declared with the attribute const. For example:<br />

int square (int) __attribute__ ((const int));<br />

says that the hypothetical function square is safe to call fewer times than the program<br />

says.<br />

Note that a function that has pointer arguments <strong>and</strong> examines the data pointed to must<br />

not be declared const. Likewise, a function that calls a non-const function usually<br />

must not be const. It does not make sense <strong>for</strong> a const function to have a void return<br />

type.<br />

deprecated<br />

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

deprecated attribute.<br />

far<br />

The far attribute tells the compiler that the function should not be called using a more<br />

efficient <strong>for</strong>m of the call instruction.<br />

<strong>for</strong>mat (archetype, string-index, first-to-check)<br />

The <strong>for</strong>mat attribute specifies that a function takes printf, scanf or strftime<br />

style arguments which should be type-checked against a <strong>for</strong>mat string. For example,<br />

consider the declaration:<br />

extern int<br />

my_printf (void *my_object, const char *my_<strong>for</strong>mat, ...)<br />

__attribute__ ((<strong>for</strong>mat (printf, 2, 3)));<br />

This causes the compiler to check the arguments in calls to my_printf <strong>for</strong><br />

consistency with the printf style <strong>for</strong>mat string argument my_<strong>for</strong>mat.<br />

The parameter archetype determines how the <strong>for</strong>mat string is interpreted, <strong>and</strong> should<br />

be one of printf, scanf or strftime. The parameter string-index specifies<br />

which argument is the <strong>for</strong>mat string argument (arguments are numbered from the left,<br />

starting from 1), while first-to-check is the number of the first argument to check<br />

against the <strong>for</strong>mat string. For functions where the arguments are not available to be<br />

checked (such as vprintf), specify the third parameter as zero. In this case, the<br />

compiler only checks the <strong>for</strong>mat string <strong>for</strong> consistency.<br />

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

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

Saved successfully!

Ooh no, something went wrong!