View Issue Details

IDProjectCategoryView StatusLast Update
0026385FPCCompilerpublic2014-06-24 09:31
ReporterLadislav LacinaAssigned ToJonas Maebe 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionduplicate 
Platformi368OSGO32V2OS Version
Product Version2.6.4Product Build 
Target VersionFixed in Version 
Summary0026385: Bug in peephole optimizer - omits FS selector when needed
DescriptionIn the past I reported bug 18113 about bugged Level 2 optimalization.

In current FPC version the produced code is slightly different but buggy too.
The compilation of MEM pseudoarray is under certain conditions confused, omits the generation of FS selector prefix and results on not working code.

This bug accurs when Level 2 compilation is on, or/and if "register variables" is on.
Analysis is and test case are bellow:
Steps To ReproduceProcedure Confuse;
begin

end;


Procedure TestBug(chr:word);
begin
Confuse; {if you comment it, everything is fine even in Level 2}
Mem[$B800:0]:=byte(chr);
end;

begin
writeln(0000013#10#13#10);
TestBug(42); {should print '*'}
readln;
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

duplicate of 0018113 resolvedSergei Gorelkin Level 2 optimalization makes wrong code for absolute variables like Mem array (unit system) 

Activities

Ladislav Lacina

2014-06-23 22:02

reporter  

testbug.pas (3,421 bytes)

Ladislav Lacina

2014-06-23 22:04

reporter   ~0075860

{ ONLY LEVEL 1 -- without "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a8 <+0>: push %ebp
   0x000020a9 <+1>: mov %esp,%ebp
   0x000020ab <+3>: sub $0xc,%esp
   0x000020ae <+6>: mov %esi,-0xc(%ebp)
   0x000020b1 <+9>: mov %edi,-0x8(%ebp)
   0x000020b4 <+12>: mov %ax,-0x4(%ebp)
   0x000020b8 <+16>: mov $0xb8000,%edi
   0x000020bd <+21>: push %es
   0x000020be <+22>: push %fs
   0x000020c0 <+24>: pop %es
   0x000020c1 <+25>: lea -0x4(%ebp),%esi
   0x000020c4 <+28>: cld
   0x000020c5 <+29>: movsb %ds:(%esi),%es:(%edi)
   0x000020c6 <+30>: pop %es
=> 0x000020c7 <+31>: mov -0xc(%ebp),%esi
   0x000020ca <+34>: mov -0x8(%ebp),%edi
   0x000020cd <+37>: leave
   0x000020ce <+38>: ret
   0x000020cf <+39>: nop
End of assembler dump.
}


{ ONLY LEVEL 1 -- with "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a8 <+0>: push %ebp
   0x000020a9 <+1>: mov %esp,%ebp
   0x000020ab <+3>: sub $0xc,%esp
   0x000020ae <+6>: mov %esi,-0xc(%ebp)
   0x000020b1 <+9>: mov %edi,-0x8(%ebp)
   0x000020b4 <+12>: mov %ax,-0x4(%ebp)
   0x000020b8 <+16>: call 0x20a0 <CONFUSE>
   0x000020bd <+21>: mov $0xb8000,%edi
   0x000020c2 <+26>: push %es
   0x000020c3 <+27>: push %fs
   0x000020c5 <+29>: pop %es
   0x000020c6 <+30>: lea -0x4(%ebp),%esi
   0x000020c9 <+33>: cld
   0x000020ca <+34>: movsb %ds:(%esi),%es:(%edi)
   0x000020cb <+35>: pop %es
=> 0x000020cc <+36>: mov -0xc(%ebp),%esi
   0x000020cf <+39>: mov -0x8(%ebp),%edi
   0x000020d2 <+42>: leave
   0x000020d3 <+43>: ret
End of assembler dump.
}



{ REGISTER VARS -- with "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a8 <+0>: push %ebp
   0x000020a9 <+1>: mov %esp,%ebp
   0x000020ab <+3>: sub $0x4,%esp
   0x000020ae <+6>: mov %ebx,-0x4(%ebp)
   0x000020b1 <+9>: mov %ax,%bx
   0x000020b4 <+12>: call 0x20a0 <CONFUSE>
   0x000020b9 <+17>: mov %bl,0xb8000
=> 0x000020bf <+23>: mov -0x4(%ebp),%ebx
   0x000020c2 <+26>: leave
   0x000020c3 <+27>: ret
End of assembler dump.
}


{ REGISTER VARS -- without "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a8 <+0>: push %ebp
   0x000020a9 <+1>: mov %esp,%ebp
   0x000020ab <+3>: mov %al,%fs:0xb8000
=> 0x000020b1 <+9>: leave
   0x000020b2 <+10>: ret
   0x000020b3 <+11>: nop
End of assembler dump.
}



{ LEVEL 2 -- with "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a4 <+0>: sub $0x4,%esp
   0x000020a7 <+3>: mov %ebx,(%esp)
   0x000020aa <+6>: mov %ax,%bx
   0x000020ad <+9>: call 0x20a0 <CONFUSE>
   0x000020b2 <+14>: mov %bl,0xb8000
=> 0x000020b8 <+20>: mov (%esp),%ebx
   0x000020bb <+23>: add $0x4,%esp
   0x000020be <+26>: ret
   0x000020bf <+27>: nop
End of assembler dump.
}


{ LEVEL 2 -- without "Confuse" calling
Dump of assembler code for function TESTBUG:
   0x000020a4 <+0>: mov %al,%fs:0xb8000
=> 0x000020aa <+6>: ret
   0x000020ab <+7>: nop
End of assembler dump.
}

Jonas Maebe

2014-06-24 09:31

manager   ~0075864

That bug is still marked as fixed in version 2.7.1. This fix was not backported to FPC 2.6.4. This is the code 2.7.1 generates:

# Var chr located in register ax
# [7] begin
# [9] Mem[$B800:0]:=byte(chr);
        movb %al,%fs:753664
# [10] end;
        ret

The "confuse" call is gone because FPC now optimises away calls to empty procedures. If you add e.g. a writeln to "confuse", it becomes this:


# [8] begin
        pushl %ebx
# Var chr located in register bx
        movw %ax,%bx
# [9] Confuse; {if you comment it, everything is fine even in Level 2}
        call P$PROGRAM_$$_CONFUSE
# [10] Mem[$B800:0]:=byte(chr);
        movb %bl,%fs:753664
# [11] end;
        popl %ebx
        ret

Issue History

Date Modified Username Field Change
2014-06-23 22:02 Ladislav Lacina New Issue
2014-06-23 22:02 Ladislav Lacina File Added: testbug.pas
2014-06-23 22:04 Ladislav Lacina Note Added: 0075860
2014-06-24 09:31 Jonas Maebe Note Added: 0075864
2014-06-24 09:31 Jonas Maebe Relationship added duplicate of 0018113
2014-06-24 09:31 Jonas Maebe Status new => resolved
2014-06-24 09:31 Jonas Maebe Resolution open => duplicate
2014-06-24 09:31 Jonas Maebe Assigned To => Jonas Maebe