gcc volatile behaviour
Robert J. Brown
rj@eli.elilabs.com
Sat Oct 9 10:28:00 GMT 1999
>>>>> "Zoltan" == Zoltan Kocsi <zoltan@bendor.com.au> writes:
Zoltan> I have some problem with gcc/egcs crosscompiling to m68k.
Zoltan> The problem probably would be present on any target,
Zoltan> actually.
Zoltan> When the value of an assignment expression where the
Zoltan> left-hand side is volatile is used, gcc re-fetches the
Zoltan> left-hand object. That is:
Zoltan> volatile int a, b; a = b = 0;
Zoltan> compiles to
Zoltan> clr.l b move.l b,%d0 move.l %d0,a
Zoltan> Apart from the fact that the involvement of d0 is not
Zoltan> really a necessity (with -O3), I do not understand why
Zoltan> does it fetch 'b' ?
Actually, when a and b are volatile, a = b = 0; is not exactly the
same thing as a = 0; b = 0; because assignment is left-associative,
and the value of the expression assigned to a in a=b=0; is the value
of the expression b=0; which is exactly 0 only when b is
non-volatile. When b is volatile, the value of b can only be
determined by fetching b. So the compiler did the right thing!
Zoltan> Similarly, when one does stringcopy:
Zoltan> strcpy( char *a char *b ) { while( *a++ = *b++ ); }
Zoltan> compiles the loop to the inefficient but correct
Zoltan> .L7: move.b (%a0)+,%d0
move.b %d0,(%a1)+
jbne .L7
Zoltan> Changing the declaration of 'a' to volatile char *a, the
Zoltan> result is this:
Zoltan> .L7: move.b (%a1)+,(%a0)
move.b (%a0)+,%d0
jbne .L7
Zoltan> This means that the copy is not terminated when the '\0'
Zoltan> is *written* to the area pointed by 'a' but when a zero is
Zoltan> *read back* from the given area. In case of HW buffers
Zoltan> this may not be what the programmer had in mind, for
Zoltan> buffers sometimes are write only, for example.
>From the man page:
DESCRIPTION
The strcpy() function copies the string pointed to be src
(including the terminating `\0' character) to the array
pointed to by dest. The strings may not overlap, and the
destination string dest must be large enough to receive
the copy.
IMO since the man page defines the terminator wrt the source string,
then the above implementation of strcpy is flawed! Volatile should
have nothing to do with it this time.
Zoltan> In every case, when the left hand operand of an assignment
Zoltan> expression is volatile and the value of the expression is
Zoltan> ised, gcc and egcs read back the value from the volatile
Zoltan> object instead of using the value written to the object.
Zoltan> Is there any particular reason why gcc and egcs do this ?
Zoltan> Thanks,
Zoltan> Zoltan
Zoltan> ------ Want more information? See the CrossGCC FAQ,
Zoltan> http://www.objsw.com/CrossGCC/ Want to unsubscribe? Send a
Zoltan> note to crossgcc-unsubscribe@sourceware.cygnus.com
------
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