Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/C++    [ Add a report in this area ]  
Report #:  122657   Status: Closed
[bcc32] wrong codegen when switch statement covers all cases
Project:  C++Builder Build #:  6.70.4983.33517
Version:    19.2 Submitted By:   Johnny Willemsen
Report Type:  Basic functionality failure Date Reported:  2/21/2014 12:29:17 AM
Severity:    Infrequently encountered problem Last Updated: 4/15/2014 6:16:30 PM
Platform:    All versions Internal Tracking #:   47867
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 5
Description
We see a case where a switch statement doesn't work in 32bit as expected.  We don't end up in the needed code.

This can be reproduced with XE2 Update 4, and with XE5 using Windows 32bit.  It works fine in Windows 64bit.  So it appears to be a codegen bug in bcc32 only.

With all of the possible cases present, the codegen for the switch statement looks like this:

File1.cpp.18: switch (_tao_discriminant)
00401245 8A45FF           mov al,[ebp-$01]
00401248 0480             add al,$80
0040124A 2C00             sub al,$00
0040124C 7202             jb $00401250 // <-- never jumps here!
0040124E EB05             jmp $00401255 // <- always jumps here
File1.cpp.278: retval = 0;
00401250 33D2             xor edx,edx
00401252 8955F8           mov [ebp-$08],edx

But if any case is removed, the codegen looks more like this instead:

File1.cpp.18: switch (_tao_discriminant)
00401245 8A45FF           mov al,[ebp-$01]
00401248 0480             add al,$80
0040124A 2C82             sub al,$82
0040124C 7207             jb $00401255
0040124E 48               dec eax
0040124F 2C7D             sub al,$7d
00401251 7202             jb $00401255 // <-- jumps here for all cases except for the missing case
00401253 EB05             jmp $0040125a // <-- jumps here for the missing case
File1.cpp.278: retval = 0;
00401255 33D2             xor edx,edx
00401257 8955F8           mov [ebp-$08],edx
Steps to Reproduce:
Compile attached code in 32bit.

EXPECTED: you reach the line line with retval = 0;
ACTUAL: the line is never reached.
Workarounds
None
Attachment
122657.zip
Comments

Mikhael Cohen at 2/21/2014 2:27:41 AM -
The code seem working if you remove anyone of cases (except case 0 of course)
Maybe there are some bad optimization, because all of possible cases are present.

Tomohiro Takahashi at 2/21/2014 5:11:25 AM -
Johnny-san
Is this issue about bcc32 ?

Johnny Willemsen at 2/21/2014 10:43:03 AM -
Yes, bcc32, causes some TAO tests to fail

Remy Lebeau (TeamB) at 2/21/2014 9:48:39 AM -
I can reproduce it with XE2 Update 4, and with XE5 using Windows 32bit.  It works fine in Windows 64bit.  So it appears to be a codegen bug in bcc32 only.

With all of the possible cases present, the codegen for the switch statement looks like this:

File1.cpp.18: switch (_tao_discriminant)
00401245 8A45FF           mov al,[ebp-$01]
00401248 0480             add al,$80
0040124A 2C00             sub al,$00
0040124C 7202             jb $00401250 // <-- never jumps here!
0040124E EB05             jmp $00401255 // <- always jumps here
File1.cpp.278: retval = 0;
00401250 33D2             xor edx,edx
00401252 8955F8           mov [ebp-$08],edx

But if any case is removed, the codegen looks more like this instead:

File1.cpp.18: switch (_tao_discriminant)
00401245 8A45FF           mov al,[ebp-$01]
00401248 0480             add al,$80
0040124A 2C82             sub al,$82
0040124C 7207             jb $00401255
0040124E 48               dec eax
0040124F 2C7D             sub al,$7d
00401251 7202             jb $00401255 // <-- jumps here for all cases except for the missing case
00401253 EB05             jmp $0040125a // <-- jumps here for the missing case
File1.cpp.278: retval = 0;
00401255 33D2             xor edx,edx
00401257 8955F8           mov [ebp-$08],edx

Server Response from: ETNACODE01