Skip to Main Content
Programming Embedded Systems, 2nd Edition
book

Programming Embedded Systems, 2nd Edition

by Michael Barr, Anthony Massa
October 2006
Intermediate to advanced content levelIntermediate to advanced
336 pages
9h 13m
English
O'Reilly Media, Inc.
Content preview from Programming Embedded Systems, 2nd Edition

Problems with Optimizing Compilers

The GNU C compiler has several optimization command-line options, all of which are variants of –O. Specifying –O3 turns on all available gcc optimizations, regardless of their effects on the speed-versus-size tradeoff. The command-line option –Os also optimizes the code for size. For a detailed explanation of the different gcc optimization levels, refer to the gcc online manual at http://gcc.gnu.org/onlinedocs.

Murphy’s Law dictates that the first time you enable the compiler’s optimization feature, your previously working program will suddenly fail. Perhaps the most notorious of the automatic optimizations is “dead code elimination.” This optimization eliminates code that the compiler believes to be either redundant or irrelevant. For example, adding zero to a variable requires no runtime calculation whatsoever. But you might still want the compiler to generate those “irrelevant” instructions if they perform some function that the compiler doesn’t know about.

For example, given the following block of code, most optimizing compilers would remove the first statement because the value of *pControl is not used before it is overwritten (on the third line):

    *pControl = DISABLE;
    *pData    = 'a';
    *pControl = ENABLE;

But what if pControl and pData are actually pointers to memory-mapped device registers? In that case, the peripheral device would not receive the DISABLE command before the byte of data was written. This could potentially wreak havoc on all future ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming with STM32: Getting Started with the Nucleo Board and C/C++

Programming with STM32: Getting Started with the Nucleo Board and C/C++

Donald Norris

Publisher Resources

ISBN: 0596009836Supplemental ContentErrata Page