29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

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.

<strong>Compiler</strong> Coding Practices<br />

5.18 Recommendation of postfix syntax when qualifying functions with <strong>ARM</strong> function<br />

modifiers<br />

Many <strong>ARM</strong> keyword extensions modify <strong>the</strong> behavior or calling sequence of a function. For<br />

example, __pure, __irq, __swi, __swi_indirect , __softfp, and __value_in_regs all behave in this<br />

way.<br />

These function modifiers all have a common syntax. A function modifier such as __pure can<br />

qualify a function declaration ei<strong>the</strong>r:<br />

• Be<strong>for</strong>e <strong>the</strong> function declaration. For example:<br />

__pure int foo(int);<br />

• After <strong>the</strong> closing paren<strong>the</strong>sis on <strong>the</strong> parameter list. For example:<br />

int foo(int) __pure;<br />

For simple function declarations, each syntax is unambiguous. However, <strong>for</strong> a function whose<br />

return type or arguments are function pointers, <strong>the</strong> prefix syntax is imprecise. For example, <strong>the</strong><br />

following function returns a function pointer, but it is not clear whe<strong>the</strong>r __pure modifies <strong>the</strong><br />

function itself or its returned pointer type:<br />

__pure int (*foo(int)) (int); /* declares 'foo' as a (pure?) function that<br />

returns a pointer to a (pure?) function.<br />

It is ambiguous which of <strong>the</strong> two function<br />

types is pure. */<br />

In fact, <strong>the</strong> single __pure keyword at <strong>the</strong> front of <strong>the</strong> declaration of foo modifies both foo itself<br />

and <strong>the</strong> function pointer type returned by foo.<br />

In contrast, <strong>the</strong> postfix syntax enables clear distinction between whe<strong>the</strong>r __pure applies to <strong>the</strong><br />

argument, <strong>the</strong> return type, or <strong>the</strong> base function, when declaring a function whose argument and<br />

return types are function pointers. For example:<br />

int (*foo1(int) __pure) (int); /* foo1 is a pure function returning<br />

a pointer to a normal function */<br />

int (*foo2(int)) (int) __pure; /* foo2 is a function returning<br />

a pointer to a pure function */<br />

int (*foo3(int) __pure) (int) __pure; /* foo3 is a pure function returning<br />

a pointer to a pure function */<br />

In this example:<br />

• foo1 and foo3 are modified <strong>the</strong>mselves<br />

• foo2 and foo3 return a pointer to a modified function<br />

• <strong>the</strong> functions foo3 and foo are identical.<br />

Because <strong>the</strong> postfix syntax is more precise than <strong>the</strong> prefix syntax, it is recommended that, where<br />

possible, you make use of <strong>the</strong> postfix syntax when qualifying functions with <strong>ARM</strong> function<br />

modifiers.<br />

5.18.1 See also<br />

Reference<br />

<strong>Compiler</strong> Reference:<br />

• __irq on page 5-8<br />

• __pure on page 5-11<br />

• __softfp on page 5-12<br />

• __svc on page 5-13<br />

• __svc_indirect on page 5-14<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-27<br />

ID061811<br />

Non-Confidential

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

Saved successfully!

Ooh no, something went wrong!