11.07.2015 Views

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Writing efficient codeint test(); /* old declaration */int test(a,b) /* old definition */char a;int b;{.....}INTEGER TYPES AND BIT NEGATIONThere are situations when the rules for integer types and their conversion lead topossibly confusing behavior. Things to look out for are assignments or conditionals (testexpressions) involving types with different size and logical operations, especially bitnegation. Here, types also includes types of constants.In some cases there may be warnings (for example, constant conditional or pointlesscomparison), in others just a different result than what is expected. Under certaincircumstances the compiler might warn only at higher optimizations, for example, if thecompiler relies on optimizations to identify some instances of constant conditionals. Inthe following example an 8-bit character, a 16-bit integer, and two’s complement isassumed:void f1(unsigned char c1){if (c1 == ~0x80);}Here, the test is always false. On the right hand side, 0x80 is 0x0080, and ~0x0080becomes 0xFF7F. On the left hand side, c1 is an 8-bit unsigned character, so it cannotbe larger than 255. It also cannot be negative, which means that the integral promotedvalue can never have the topmost 8 bits set.PROTECTING SIMULTANEOUSLY ACCESSED VARIABLESVariables that are accessed asynchronously, for example by interrupt routines or by codeexecuting in separate threads, must be properly marked and have adequate protection.The only exception to this is a variable that is always read-only.To mark a variable properly, use the volatile keyword. This informs the compiler,among other things, that the variable can be changed from other threads. The compilerwill then avoid optimizing on the variable (for example, keeping track of the variable inregisters), will not delay writes to it, and be careful accessing the variable only thenumber of times given in the source code. To read more about the volatile typequalifier, see Declaring objects volatile, page 176.126<strong>MSP430</strong> <strong>IAR</strong> C/<strong>C++</strong> <strong>Compiler</strong>Reference Guide

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

Saved successfully!

Ooh no, something went wrong!