02.03.2014 Views

BSP Developer's Guide

BSP Developer's Guide

BSP Developer's Guide

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.

8<br />

Writing Portable C Code<br />

Always specify fields with explicit widths, such as UINT8, UINT16, INT32, and so<br />

on. Do not use bitfields in a packed structure.<br />

_WRS_ASM(“X”)<br />

This macro is used to insert assembly code within a C function declaration. The<br />

inserted code must not interact with C variables or try to alter the return value of<br />

the function. The code uses the MIT assembly-language mnemonics and syntax.<br />

It is assumed that the compiler does not optimize or reorder any specified in-line<br />

assembly code. The insertion of a null in-line assembly statement can be used to<br />

prevent the compiler from reordering C code before the statement with C code that<br />

follows the statement.<br />

8<br />

_WRS_DATA_ALIGN_BYTES(n)<br />

This macro is used in prefix notation to declare an initialized C data element with<br />

a special alignment. The argument n is the alignment in byte units (1, 2, 4, 8, 16, and<br />

so on). This is normally used only with initialized global data elements. Use this<br />

macro with caution: overuse of this macro can result in poor memory utilization.<br />

If large numbers of variables require special alignment, it may be best to declare<br />

them in separate sections directly in assembler. The linker loader could then fit<br />

them together in an optimal fashion.<br />

_WRS_GNU_VAR_MACROS<br />

The GNU compiler system created a means to pass a variable number of<br />

arguments to pre-processor macro functions, and there are a few special instances<br />

in the VxWorks code base that use the GNU-defined syntax. Since then, the ANSI<br />

standards committee has defined an ANSI standard that is different from this<br />

practice. Currently, the GNU compiler 2.96 does not yet support the ANSI<br />

standard, and the Diab compiler supports only the ANSI standard.<br />

Code that does use variadic macros should define them both for GNU and for the<br />

ANSI standard (Diab). Rather than select upon the toolchain or compiler name, a<br />

new macro feature name, _WRS_GNU_VAR_MACROS, has been created. The GNU<br />

toolchain defines this macro; the Diab toolchain does not.<br />

If you want to port your code to another toolchain, you must choose between<br />

supporting the GNU-style syntax or the ANSI standard syntax. For example, the<br />

following code fragment demonstrates the use of an #ifdef statement to make this<br />

choice between GNU and ANSI:<br />

169

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

Saved successfully!

Ooh no, something went wrong!