public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37281]  New: bad code generation with enum and -m32
@ 2008-08-29 16:30 gmorin1 at bloomberg dot net
  2008-08-29 16:39 ` Andrew Thomas Pinski
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gmorin1 at bloomberg dot net @ 2008-08-29 16:30 UTC (permalink / raw)
  To: gcc-bugs

This is a copy of a Debian report that I made (I thought I might as well report
it directly here too): http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=496965

g++ generates bad code for this program on an amd64 machine.  It works with
fine in 64-bit and with older versions of gcc.  

#include <stdlib.h>
struct MyTypes {
    enum Type {
        NOVALUE = 1,
        VALUE   = 30,
        VALUE2 = 31,
        VALUE3 = -3
    };

    static bool isValue(MyTypes::Type value);
};

bool MyTypes::isValue(MyTypes::Type value)
{
    switch (value) {
      case MyTypes::VALUE:
      case MyTypes::VALUE2:
      case MyTypes::VALUE3: {
        return true;
      }
      default: {
        return false;
      }
    }
}

int main(int argc, char *argv[])
{
    if (true == MyTypes::isValue(MyTypes::Type(32))) {
        abort();
    }
    return 0;
}

guillaum@canard:~$ g++ -m32 -Wall -o e enumtest.cpp
guillaum@canard:~$ ./e
Aborted
guillaum@canard:~$ g++ -Wall -o e enumtest.cpp
guillaum@canard:~$ ./e
guillaum@canard:~$ g++-4.2 -m32 -Wall -o e enumtest.cpp
guillaum@canard:~$ ./e
guillaum@canard:~$ g++-4.1 -m32 -Wall -o e enumtest.cpp
guillaum@canard:~$ ./e

It works with -O2 though (not -O1):
guillaum@canard:~$ g++ -O2 -m32 -Wall -o e enumtest.cpp
guillaum@canard:~$ ./e


-- System Information:
Debian Release: lenny/sid
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'unstable')
Architecture: amd64 (x86_64)


-- 
           Summary: bad code generation with enum and -m32
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gmorin1 at bloomberg dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bug c++/37281]  New: bad code generation with enum and -m32
  2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
@ 2008-08-29 16:39 ` Andrew Thomas Pinski
  2008-08-29 16:41 ` [Bug c++/37281] " pinskia at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Andrew Thomas Pinski @ 2008-08-29 16:39 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

This code is undefined as the value of  32 is outside the range of the  
enum.

Sent from my iPhone

On Aug 29, 2008, at 9:29, "gmorin1 at bloomberg dot net" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

> This is a copy of a Debian report that I made (I thought I might as  
> well report
> it directly here too): http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=496965
>
> g++ generates bad code for this program on an amd64 machine.  It  
> works with
> fine in 64-bit and with older versions of gcc.
>
> #include <stdlib.h>
> struct MyTypes {
>    enum Type {
>        NOVALUE = 1,
>        VALUE   = 30,
>        VALUE2 = 31,
>        VALUE3 = -3
>    };
>
>    static bool isValue(MyTypes::Type value);
> };
>
> bool MyTypes::isValue(MyTypes::Type value)
> {
>    switch (value) {
>      case MyTypes::VALUE:
>      case MyTypes::VALUE2:
>      case MyTypes::VALUE3: {
>        return true;
>      }
>      default: {
>        return false;
>      }
>    }
> }
>
> int main(int argc, char *argv[])
> {
>    if (true == MyTypes::isValue(MyTypes::Type(32))) {
>        abort();
>    }
>    return 0;
> }
>
> guillaum@canard:~$ g++ -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> Aborted
> guillaum@canard:~$ g++ -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> guillaum@canard:~$ g++-4.2 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> guillaum@canard:~$ g++-4.1 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
>
> It works with -O2 though (not -O1):
> guillaum@canard:~$ g++ -O2 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
>
>
> -- System Information:
> Debian Release: lenny/sid
>  APT prefers testing
>  APT policy: (990, 'testing'), (500, 'unstable')
> Architecture: amd64 (x86_64)
>
>
> -- 
>           Summary: bad code generation with enum and -m32
>           Product: gcc
>           Version: 4.3.1
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c++
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: gmorin1 at bloomberg dot net
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/37281] bad code generation with enum and -m32
  2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
  2008-08-29 16:39 ` Andrew Thomas Pinski
@ 2008-08-29 16:41 ` pinskia at gmail dot com
  2008-08-29 16:59 ` gmorin1 at bloomberg dot net
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gmail dot com @ 2008-08-29 16:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gmail dot com  2008-08-29 16:39 -------
Subject: Re:   New: bad code generation with enum and -m32

This code is undefined as the value of  32 is outside the range of the  
enum.

Sent from my iPhone

On Aug 29, 2008, at 9:29, "gmorin1 at bloomberg dot net"
<gcc-bugzilla@gcc.gnu.org 
 > wrote:

> This is a copy of a Debian report that I made (I thought I might as  
> well report
> it directly here too): http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=496965
>
> g++ generates bad code for this program on an amd64 machine.  It  
> works with
> fine in 64-bit and with older versions of gcc.
>
> #include <stdlib.h>
> struct MyTypes {
>    enum Type {
>        NOVALUE = 1,
>        VALUE   = 30,
>        VALUE2 = 31,
>        VALUE3 = -3
>    };
>
>    static bool isValue(MyTypes::Type value);
> };
>
> bool MyTypes::isValue(MyTypes::Type value)
> {
>    switch (value) {
>      case MyTypes::VALUE:
>      case MyTypes::VALUE2:
>      case MyTypes::VALUE3: {
>        return true;
>      }
>      default: {
>        return false;
>      }
>    }
> }
>
> int main(int argc, char *argv[])
> {
>    if (true == MyTypes::isValue(MyTypes::Type(32))) {
>        abort();
>    }
>    return 0;
> }
>
> guillaum@canard:~$ g++ -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> Aborted
> guillaum@canard:~$ g++ -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> guillaum@canard:~$ g++-4.2 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
> guillaum@canard:~$ g++-4.1 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
>
> It works with -O2 though (not -O1):
> guillaum@canard:~$ g++ -O2 -m32 -Wall -o e enumtest.cpp
> guillaum@canard:~$ ./e
>
>
> -- System Information:
> Debian Release: lenny/sid
>  APT prefers testing
>  APT policy: (990, 'testing'), (500, 'unstable')
> Architecture: amd64 (x86_64)
>
>
> -- 
>           Summary: bad code generation with enum and -m32
>           Product: gcc
>           Version: 4.3.1
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c++
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: gmorin1 at bloomberg dot net
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281
>


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/37281] bad code generation with enum and -m32
  2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
  2008-08-29 16:39 ` Andrew Thomas Pinski
  2008-08-29 16:41 ` [Bug c++/37281] " pinskia at gmail dot com
@ 2008-08-29 16:59 ` gmorin1 at bloomberg dot net
  2008-08-29 17:02 ` manu at gcc dot gnu dot org
  2010-05-04  4:49 ` jason at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: gmorin1 at bloomberg dot net @ 2008-08-29 16:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from gmorin1 at bloomberg dot net  2008-08-29 16:57 -------
I thought it was correct because it seems that the standard allows to cast
explicity inside the range.  The range is not the one of the integral type but
the one of smallest bit field that can stop all the values.  So you're
completely correct.  Sorry.


-- 

gmorin1 at bloomberg dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/37281] bad code generation with enum and -m32
  2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
                   ` (2 preceding siblings ...)
  2008-08-29 16:59 ` gmorin1 at bloomberg dot net
@ 2008-08-29 17:02 ` manu at gcc dot gnu dot org
  2010-05-04  4:49 ` jason at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-29 17:02 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 304 bytes --]



------- Comment #3 from manu at gcc dot gnu dot org  2008-08-29 17:00 -------
In GCC 4.4 we warn about this with -Wconversion.

warning: the result of the conversion is unspecified because ‘32’ is outside
the range of type ‘MyTypes::Type’


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/37281] bad code generation with enum and -m32
  2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
                   ` (3 preceding siblings ...)
  2008-08-29 17:02 ` manu at gcc dot gnu dot org
@ 2010-05-04  4:49 ` jason at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-05-04  4:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jason at gcc dot gnu dot org  2010-05-04 04:48 -------
In G++ 4.6 the surprising optimization will only be performed with
-fstrict-enums.


-- 

jason at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37281


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-05-04  4:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-29 16:30 [Bug c++/37281] New: bad code generation with enum and -m32 gmorin1 at bloomberg dot net
2008-08-29 16:39 ` Andrew Thomas Pinski
2008-08-29 16:41 ` [Bug c++/37281] " pinskia at gmail dot com
2008-08-29 16:59 ` gmorin1 at bloomberg dot net
2008-08-29 17:02 ` manu at gcc dot gnu dot org
2010-05-04  4:49 ` jason at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).