68
Section 2: Compiler
TI
-
89 / TI
-
92 Plus Sierra C Assembler Reference Manual
Not for Distribution
Beta Version February 2, 2001
In most embedded systems, as much information as possible is placed in ROM
(e.g., the entire code (
.text
) section). The
const
type specifier is extremely
important in these environments because it causes declared data (except when
also declared
volatile
) to be placed in the
.text
section — i.e., directly in ROM.
For additional information on the use of the
const
type specifier, refer to section
2.14 Static Storage Initialization
.
2.9.9. Volatile Type Specifier
An object that is declared with the
volatile
type specifier is guaranteed to be
accessed, in its entirety, each time it is referenced in the source. As a result, the
compiler must forgo optimizations that would alter either the size of references to
the object (e.g.,
char
,
short
,
int
, etc.) or the number of references made to the
object. For example, the compiler cannot test a bit in a four-byte volatile object by
referencing only the byte that contains the bit of interest; the object must be
referenced as a four-byte entity. As the following example illustrates, the compiler
would not be able to optimize the first function, which reads data from an I/O port,
as though it were written as the second function:
volatile char
*
const stat_reg
=
(char
*
) 0x60000D;
volatile char
*
const rcvr_reg
=
(char
*
) 0x60000F;
get_char()
{
while( !(
*
stat_reg & 0x40) );
return(
*
rcvr_reg );
}
get_char()
{
register int temp;
temp
=
!(
*
stat_reg & 0x40);
while( temp ); /
*
infinite loop
*
/
return(
*
rcvr_reg );
}
The
volatile
type specifier is used for memory-mapped I/O ports, variables
shared by other processes, variables modified inside interrupt handlers and error
handlers, and any other variables that are accessed in ways not obvious to the
compiler.