Correct ordering of write operations

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

 



Hello.

I have the following program:

volatile int z;
int q;

void
a (void)
{
  q = 42;
  z = 2;
  q = 7;
}

gcc -std=c99 -O3 -fomit-frame-pointer -S produces this code:

a:
        movl    $2, z
        movl    $7, q
        ret

I believe that the output should include setting 'q' to '42'.  Of
course, making 'q' volatile fixes that.

I'm reading §5.1.2.3 of the ISO C99 specification here.

In paragraph 2 it states that "modifying an object" (even a non-volatile
one) is a "side effect".  In paragraph 3 it says that "an actual
implementation need not evaluate part of an expression if it can
deduce ... that no needed side effects are produced" (but in this case,
a side effect -is- produced).  Finally, in paragraph 4 it says when
processing is interrupted by receipt of a signal that "the values of
objects as of the previous sequence point may be relied on".

Assume that 'z' and 'q' started out as zero.  A signal can occur at any
time.  The original code has 4 relevant sequence points in it:


============== here: z = 0, q = 0
  q = 42;
============== here: z = 0, q = 42
  z = 2;
============== here: z = 2, q = 42
  q = 7;
============== here: z = 2, q = 7

By my reading, this means that no signal handler should be able to see
values of z = 2 and q = 0.  There is no point in the program where such
values might be in those variables.

But of course, that is exactly what happens in this case:

a:
        movl    $2, z
			<---- signal happens here
        movl    $7, q
        ret

By the C99 specification I believe I shouldn't need to use volatile in
this case, but by experimenting with GCC it appears that I do.  Based on
my reading, my understanding tells me that GCC is diverging from the
specification here.

Can someone please explain to me why my understanding is incorrect or
why GCC chooses to diverge?

Cheers


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux