This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: wrong code generated in gcc-4.1.1?
- From: John Love-Jensen <eljay at adobe dot com>
- To: Hiroki Kaminaga <kaminaga at sm dot sony dot co dot jp>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Tue, 04 Dec 2007 07:43:17 -0600
- Subject: Re: wrong code generated in gcc-4.1.1?
Hi Hiroki,
> Is this a bug or am I violating C++ standard?
The code violates the C++ standard.
The used range of Quartet is -1 ... 2, which can be expressed in 3 signed
bits (0b111 is -1, 0b010 is 2), a signed bit-range of -4 (0b100) to 3
(0b011).
42 and 129 do not fit in this range. Casting 42 and 129 into that range is
undefined.
The C++ compiler *KNOWS* that case FOURTY_TWO cannot be accessed, and is
dead code. Why? Because according to ISO 14882, Quartet *MUST* be between
-4 and 3 (3 signed bits). And it *KNOWS* that int i was set from a Quartet.
The optimization that is performing that behavior is -ftree-dominator-opts,
which is enabled by -O1 and higher.
If you compile the code as C (via gcc) instead of C++ (via g++), you'll
notice that the same -ftree-dominator-opts does not optimize away the
FOURTY_TWO. Why? Because in ISO 9899, an enum is held in a signed int or
an unsigned int.
Also some nits...
C++ should use the C++ header file, #include <cstdio>, and not the C header
file #include <stdio.h>.
And the return type should be char const*, and not char*.
And, in my opinion, C++ code should avoid using C-style casts and instead
should use C++ verbose casts.
HTH,
--Eljay