linux-mips
[Top] [All Lists]

GCC -O2 failure for mipsel

To: Fuxin Zhang <fxzhang@ict.ac.cn>
Subject: GCC -O2 failure for mipsel
From: Andrew Haley <aph@redhat.com>
Date: Thu, 1 May 2003 09:27:44 +0100
Cc: MAKE FUN PRANK CALLS <linux-mips@linux-mips.org>, gcc@gcc.gnu.org
In-reply-to: <3EB0B329.9030603@ict.ac.cn>
Original-recipient: rfc822;linux-mips@linux-mips.org
References: <3EB0B329.9030603@ict.ac.cn>
Sender: linux-mips-bounce@linux-mips.org
Fuxin Zhang writes:
 > Hello,
 > I've met a case where mipsel-linux-gcc -O2 fails,for both
 > 2.96 and the fresh new 3.2.3. Maybe someone can tell me
 > what's wrong.

Your code is incorrect.

 > I've reduced the problem to the test case below,compile it
 > with mipsel-linux-gcc -O2(FROM H.J.Lu's redhat miniport,all version,
 > and 3.2.3 is tested too)
 > 
 > 
 > #define PUT_CODE(x,code) ((x)->code = (code))
 > union test_union {
 > struct test *t;
 > int a;
 > };
 > 
 > struct test {
 > unsigned short code;
 > union test_union u[1];
 > };
 > 
 > char memory[2000];
 > 
 > struct test *test_alloc(int code)
 > {
 > struct test *t;
 > int length=sizeof(struct test);
 > 
 > t = (struct test*)memory;
 > length = (sizeof(struct test) - sizeof(union test_union)-1)/sizeof(int);
 > for (;length>=0;length--)

This is the errant line:

 > ((int*)t)[length] = 0;

You have declared t as a pointer to struct test, but you're using it
as a pointer to int.  If you look at Pointers, Section 6.2.2.3 in ISO
9899-1990 you'll see that this results in undefined behaviour.

-fno-strict-aliasing should generate the code you want, but it's
better to fix your source.  If you want to use a pointer as a
different type, put it in a union.

Andrew.

<Prev in Thread] Current Thread [Next in Thread>