avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: AW: [avr-gcc-list] Gcc bug??


From: Peter Hierholzer
Subject: AW: AW: [avr-gcc-list] Gcc bug??
Date: Thu, 4 Mar 2004 11:16:06 +0100

> 
> A quick question about "volatile".  If I have follow code.
> 
> =============================
> static uint8_t cnt;
> 
> void incr_cnt()
> {
>   cnt++;
> }
> 
> SIGNAL(SIG_OUTPUT_COMPARE0)
> {
>   uint8_t tmp;
>   tmp = cnt;
> }
> =====================
> 
> Since the variable is static, it should be easy for gcc to recongize this
> "cnt" variable and not generate wrong code.  On the other hand, if
> incr_cnt() and SIGNAL(SIG_OUTPUT_COMPARE0) are in two different files, and
> there is no "volatile" declaration, then gcc might generate wrong code.
> 
> Am I correct on this?  Could some compiler experts help to explain the
> detail about volatile keyword in the context of embedded system?  A
> pointer to a page might work as well.
> 
> Thank you very much,
> Simon

Hi Simon,

In both cases (same file, different files) the compiler can generate wrong
code. 

The compiler assumes that the control flow starts in the main() routine and
it takes all function calls below main into consideration when it does the
optimization. It also compiles the SIGNAL function, but sees no connection
between the main-flow and the SIGNAL function. (The connection between these
two routines is done by the controller hardware only and the compiler has no
idea about the controller hardware).

The volatile keyword tells the compiler that somebody else can use this
variable at any time. This somebody else can be an interrupt routine or some
external event changing a hardware register. Therefore the compiler disables
optimization for this variable.

Volatile is nothing special for embedded systems. It is part of the language
C. You need it more often on embedded systems, but you might need it also
for programs (e.g. device drivers) running on PCs. 

Hope it helps

Peter


_______________________________________________
avr-gcc-list mailing list
address@hidden
http://www.avr1.org/mailman/listinfo/avr-gcc-list


reply via email to

[Prev in Thread] Current Thread [Next in Thread]