gcc volatile behaviour

Zoltan Kocsi zoltan@bendor.com.au
Sat Oct 9 05:56:00 GMT 1999

I have some problem with gcc/egcs crosscompiling to m68k.
The problem probably would be present on any target, actually.

When the value of an assignment expression where the left-hand side is 
volatile is used, gcc re-fetches the left-hand object. That is:

  volatile int a, b;
  a = b = 0;

compiles to

        clr.l b
        move.l b,%d0
        move.l %d0,a
Apart from the fact that the involvement of d0 is not really a
necessity (with -O3), I do not understand why does it fetch 'b' ?

Similarly, when one does stringcopy:

   strcpy( char *a char *b )
     while( *a++ = *b++ );
compiles the loop to the inefficient but correct

        move.b (%a0)+,%d0
        move.b %d0,(%a1)+
        jbne .L7

Changing the declaration of 'a' to volatile char *a, the result is this:

        move.b (%a1)+,(%a0)
        move.b (%a0)+,%d0
        jbne .L7
This means that the copy is not terminated when the '\0' is *written*
to the area pointed by 'a' but when a zero is *read back* from the given 
area. In case of HW buffers this may not be what the programmer had in 
mind, for buffers sometimes are write only, for example.

In every case, when the left hand operand of an assignment expression
is volatile and the value of the expression is ised, gcc and egcs read 
back the value from the volatile object instead of using the value
written to the object.

Is there any particular reason why gcc and egcs do this ?



Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

More information about the crossgcc mailing list