User account creation filtered due to spam.

Bug 27838 - Wrong code generated for for()-loop with enumerated type as index
Summary: Wrong code generated for for()-loop with enumerated type as index
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-31 12:51 UTC by Oleh Derevenko
Modified: 2006-05-31 13:04 UTC (History)
61 users (show)

See Also:
Host: x86
Target: x86
Build: x86
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Oleh Derevenko 2006-05-31 12:51:39 UTC
I have enumerated type defined as follows

enum EQUEUEDWRITERFILEERROR
{
	QWFE__MIN,

	QWFE_REOPEN = QWFE__MIN,
	QWFE_WRITE,
	QWFE_OVERFLOW,

	QWFE__MAX,
};

For it I have prefix increment operator defined with macro
#define DEFINE_ENUM_INC_DEC(EnumType) \
	static inline EnumType &operator ++(EnumType &Value) { return (EnumType &)(++((int &)Value)); }

For following for-loop operator
	for (EQUEUEDWRITERFILEERROR qwfeError = QWFE__MIN; qwfeError != QWFE__MAX; ++qwfeError)

compiler generated wrong code for condition check
0x8090981 mov    0xffffffd7(%ebp),%edx
0x8090984 mov    0xffffffd7(%ebp),%al
0x8090987 inc    %edx
0x8090988 cmp    $0x3,%al
0x809098a mov    %edx,0xffffffd7(%ebp)
0x809098d jne    0x8090940 

That is, variable is incremented in parallel with loop condition check and result of increment has effect to loop condition only on the next pass. This causes the loop to execute one time more than required.

Compilation options used
-malign-double -fshort-enums -freg-struct-return -fno-exceptions -g -O3 -march=pentium -fno-rtti -fconserve-space
Comment 1 Andreas Schwab 2006-05-31 13:04:37 UTC
You are violating the aliasing rules.

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