Bug 32536 - wrong generated code on complex statement;
Summary: wrong generated code on complex statement;
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2007-06-28 15:15 UTC by astier@lpnp204.in2p3.fr
Modified: 2007-06-28 16:54 UTC (History)
54 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description astier@lpnp204.in2p3.fr 2007-06-28 15:15:35 UTC

sample code:

#include <stdio.h>

int main()
  char cp[2];
  cp[0] = 'A';
  cp[1] = 'B';
  printf("%x %x\n",cp[0],cp[1]);
  cp[0] ^= (cp[1]^=(cp[0]^=cp[1]));
  printf("%x %x\n",cp[0],cp[1]);
  return 0;

The complex byte swapping instruction is far fetched but seems legal.
It actually swaps bytes if compiled with "gcc -O3". Without optimization, 
one of the bytes receives a '\0'. 
   This instruction seemed to work properly with versions 3.

System: Linux lpnp204 2.4.21-47.0.1.EL.cernsmp #1 SMP Thu Oct 19 16:35:52 CEST 2006 i686 i686 i386 GNU/Linux
Architecture: i686

host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ./configure

	Compile the code above with various degrees of optimization.
Comment 1 Andreas Schwab 2007-06-28 16:54:52 UTC
You are modifying the same object twice between two sequence points.

*** This bug has been marked as a duplicate of 11751 ***