This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Reordering of function calls - compiler bug or allowed by standard?
- From: Kai Henningsen <kai dot extern at googlemail dot com>
- To: jens dot kilian at verigy dot com
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 24 Sep 2008 09:57:17 +0200
- Subject: Re: Reordering of function calls - compiler bug or allowed by standard?
- References: <20080924062551.GA3183@socle119.verigy.net>
Am Mittwoch, den 24.09.2008, 08:25 +0200 schrieb Jens Kilian:
> The following somewhat contrived example models what happens in our software
> when accessing some memory-mapped IO registers. There is an address register
> into which the SW writes the address to be accessed, plus a data register
> from which it can then read the contents of that address. Both accesses
> have been replaced by printf/scanf here.
>
> ---8<--------------------------------------------------------------------8<---
> #include <stdio.h>
>
> /* model internal state of address register */
> int N;
>
> /* write address register */
> void
> writeit(int i)
> {
> printf("Writing %d\n", i);
> N = i;
> }
>
> /* read data register */
> int
> readit(void)
> {
> int x;
>
> printf("Reading %d\n", N);
> scanf("%d", &x);
>
> return x;
> }
>
> /* convenience macro to read from address i */
> #define RD(i) (writeit(i), readit())
A comma is a sequence point, so this part works.
>
> int
> main(void)
> {
> if (RD(42) == RD(23))
> return 1;
>
> return 0;
> }
On the other hand, the == operator isn't, so this part doesn't work.
> Is there any way to force a consistent order of function calls - i.e.,
Yes, of course, in the simplest way possible. Put the write and the
read inside a function. Function calls are sequence points. You still
can't predict if it's going for 42 or 43 first, but they won't be
interleaved that way.