Compiler produces wrong code when enumarator is used in O2 and O3
Original Reporter info from Mantis: stoper
-
Reporter name: Pawel Stopinski
Original Reporter info from Mantis: stoper
- Reporter name: Pawel Stopinski
Description:
When optimization level is at least O2 following function produces wrong code:
function CompareStuffNOK(Props: TJclSimpleXMLProps;
const Attribute: string): TJclSimpleXMLProp;
var
res: TJclSimpleXMLProp;
begin
for res in Props do
begin
if res.Name = Attribute then
begin
Result := res;
Exit;
end;
end;
Result := nil;
end;
In attached assembly, at the beginning of the function second parameter is saved on stack:
0040154F 891424 mov %edx,(%esp)
But then, at one point a value is pushed on stack:
00401578 50 push %eax
Thus moving esp. Now esp points to 0 (instead of string pointer) making comparison always fail:
00401589 8b4604 mov 0x4(%esi),%eax
0040158C 8b1424 mov (%esp),%edx
0040158F e82c410000 call 0x4056c0 &LtPos;fpc_ansistr_compare_equal>
Steps to reproduce:
ppc386 bugtest.lpr
will produce working executable:
Test "maslo" ok.
Test "foo" ok.
Test "niema" ok.
test done
ppc386 bugtest.lpr -O2
will produce failing executable:
Test "maslo" FAILED!!!
Test "foo" FAILED!!!
Test "niema" ok.
test done
Additional information:
bugtest.lpr:49 begin
00401540 83ec3c sub $0x3c,%esp
00401543 895c2430 mov %ebx,0x30(%esp)
00401547 89742434 mov %esi,0x34(%esp)
0040154B 897c2438 mov %edi,0x38(%esp)
0040154F 891424 mov %edx,(%esp)
bugtest.lpr:50 for res in Props do
00401552 e8a9030000 call 0x401900 &LtPos;TJCLSIMPLEXMLPROPS__GETENUMERATOR>
00401557 89c3 mov %eax,%ebx
00401559 85c0 test %eax,%eax
0040155B 0f8494000000 je 0x4015f5 &LtPos;COMPARESTUFFNOK+181>
00401561 8d4c2404 lea 0x4(%esp),%ecx
00401565 8d542410 lea 0x10(%esp),%edx
00401569 b801000000 mov $0x1,%eax
0040156E e8ed660000 call 0x407c60 &LtPos;fpc_pushexceptaddr>
00401573 e8987d0000 call 0x409310 &LtPos;fpc_setjmp>
00401578 50 push %eax
00401579 85c0 test %eax,%eax
0040157B 752c jne 0x4015a9 &LtPos;COMPARESTUFFNOK+105>
0040157D eb1f jmp 0x40159e &LtPos;COMPARESTUFFNOK+94>
0040157F 90 nop
00401580 89d8 mov %ebx,%eax
00401582 e8d9040000 call 0x401a60 &LtPos;TJCLSIMPLEXMLPROPSENUMERATOR__GETCURRENT>
00401587 89c6 mov %eax,%esi
bugtest.lpr:52 if res.Name = Attribute then
00401589 8b4604 mov 0x4(%esi),%eax
0040158C 8b1424 mov (%esp),%edx
0040158F e82c410000 call 0x4056c0 &LtPos;fpc_ansistr_compare_equal>
00401594 85c0 test %eax,%eax
00401596 7506 jne 0x40159e &LtPos;COMPARESTUFFNOK+94>
bugtest.lpr:54 Result := res;
00401598 8974242c mov %esi,0x2c(%esp)
bugtest.lpr:55 Exit;
0040159C eb52 jmp 0x4015f0 &LtPos;COMPARESTUFFNOK+176>
0040159E 89d8 mov %ebx,%eax
004015A0 e8db040000 call 0x401a80 &LtPos;TJCLSIMPLEXMLPROPSENUMERATOR__MOVENEXT>
004015A5 84c0 test %al,%al
004015A7 75d7 jne 0x401580 &LtPos;COMPARESTUFFNOK+64>
004015A9 e882690000 call 0x407f30 &LtPos;fpc_popaddrstack>
004015AE 89df mov %ebx,%edi
004015B0 be01000000 mov $0x1,%esi
004015B5 89da mov %ebx,%edx
004015B7 85db test %ebx,%ebx
004015B9 750a jne 0x4015c5 &LtPos;COMPARESTUFFNOK+133>
004015BB b8d2000000 mov $0xd2,%eax
004015C0 e8bb790000 call 0x408f80 &LtPos;SYSTEM_HANDLEERROR$LONGINT>
004015C5 8b1a mov (%edx),%ebx
004015C7 89d8 mov %ebx,%eax
004015C9 e872150000 call 0x402b40 &LtPos;fpc_check_object>
004015CE 89d8 mov %ebx,%eax
004015D0 e86b150000 call 0x402b40 &LtPos;fpc_check_object>
004015D5 89f8 mov %edi,%eax
004015D7 89f2 mov %esi,%edx
004015D9 ff5330 call *0x30(%ebx)
004015DC 58 pop %eax
004015DD 85c0 test %eax,%eax
004015DF 7414 je 0x4015f5 &LtPos;COMPARESTUFFNOK+181>
004015E1 83e801 sub $0x1,%eax
004015E4 7405 je 0x4015eb &LtPos;COMPARESTUFFNOK+171>
004015E6 83e801 sub $0x1,%eax
004015E9 7412 je 0x4015fd &LtPos;COMPARESTUFFNOK+189>
004015EB e8b06a0000 call 0x4080a0 &LtPos;fpc_reraise>
004015F0 58 pop %eax
004015F1 6a02 push $0x2
004015F3 ebb4 jmp 0x4015a9 &LtPos;COMPARESTUFFNOK+105>
bugtest.lpr:58 Result := nil;
004015F5 c744242c00000000 movl $0x0,0x2c(%esp)
bugtest.lpr:59 end;
004015FD 8b44242c mov 0x2c(%esp),%eax
00401601 8b5c2430 mov 0x30(%esp),%ebx
00401605 8b742434 mov 0x34(%esp),%esi
00401609 8b7c2438 mov 0x38(%esp),%edi
0040160D 83c43c add $0x3c,%esp
00401610 c3 ret
Mantis conversion info:
- Mantis ID: 23641
- OS: Windows
- OS Build: Vista
- Build: 2.6.0 [2012/11/30] for i386
- Platform: i386
- Version: 2.6.0