public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* wrong code generated in gcc-4.1.1?
@ 2007-12-04  4:00 Hiroki Kaminaga
  2007-12-04  5:45 ` [SOLVED] " Hiroki Kaminaga
  2007-12-04 13:44 ` John Love-Jensen
  0 siblings, 2 replies; 4+ messages in thread
From: Hiroki Kaminaga @ 2007-12-04  4:00 UTC (permalink / raw)
  To: gcc-help; +Cc: kaminaga

Hi,

Below sample program produced wrong code in g++ version 4.1.1.
Is this a bug or am I violating C++ standard?
(not taking range propagation into account?)

$ cat test.c
#include <stdio.h>

typedef enum QUARTET {
  PIANO = -1,
  VIOLIN,
  VIOLA,
  CELLO
} Quartet;

enum {
  FOURTY_TWO = 42
};

char *
getstring(Quartet q)
{
  char *p;
  int i = (int)q;
  switch (i) {
  case PIANO:      p = "piano";   break;
  case VIOLIN:     p = "violin";  break;
  case VIOLA:      p = "viola";   break;
  case CELLO:      p = "cello";   break;
  case FOURTY_TWO: p = "42";      break;
  default:         p = "unknown"; break;
  }
  return p;
}

int main()
{
  Quartet q = VIOLIN;
  printf("instrument: %s\n", getstring(q));
  printf("instrument: %s\n", getstring((Quartet) FOURTY_TWO));
  printf("instrument: %s\n", getstring((Quartet) 129));
  return 0;
}

$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -O2 -o test test.c && ./test
instrument: violin
instrument: unknown
instrument: unknown

My expected result is:

instrument: violin
instrument: 42
instrument: unknown

I've seen the problem going away if:

  - compiled with gcc (not g++)
  - add -fno-tree-dominator-opts option
  - set Quartet type `PIANO = ' to greater than or equal to zero
  - compile with g++ version 3.4 serise

Any hint please?


Best Regards,

(Hiroki Kaminaga)
t
--

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

end of thread, other threads:[~2007-12-05  4:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-04  4:00 wrong code generated in gcc-4.1.1? Hiroki Kaminaga
2007-12-04  5:45 ` [SOLVED] " Hiroki Kaminaga
2007-12-04 13:44 ` John Love-Jensen
2007-12-05  4:58   ` Hiroki Kaminaga

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).