Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Code Generation/Optimization    [ Add a report in this area ]  
Report #:  124402   Status: Closed
Compiler bug when comparing chars
Project:  Delphi Build #:  20.0.15596.9843
Version:    20.0 Submitted By:   Roman Yankovsky
Report Type:  Issue Date Reported:  4/28/2014 11:47:18 PM
Severity:    Extreme corner case Last Updated: 4/29/2014 4:30:43 AM
Platform:    All platforms Internal Tracking #:  
Resolution: Test Case Error (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: 15
Description
Compiler generates invalid code for char comparison (like "if AChar = #255 then")
Steps to Reproduce:
if AChar <= #255 then:

005D734A 66817DFA4F04     cmp word ptr [ebp-$06],$044f


$044f is not equal to #255
Workarounds
It works fine when Ord function is used:

if Ord(AChar) <= 255  then
005D7352 668B45FA         mov ax,[ebp-$06]
005D7356 663DFF00         cmp ax,$00ff
Attachment
None
Comments

Tomohiro Takahashi at 4/29/2014 4:16:44 AM -
> if AChar <= #255 then
What is data type of AChar, Char, AnsiChar etc...?

and, is this a regression in Delphi XE6? What about previous versions of Delphi?

Tomohiro Takahashi at 4/29/2014 4:24:17 AM -
What system locale do you use on your OS?
With Delphi XE5 on my Windows 8 Japanese Editon, and, if I use Char(or WideChar) for AChar, the code is complied as below.
----
procedure TForm1.Button1Click(Sender: TObject);
var
  AChar: Char;
begin
  if AChar <= #255 then;
end;
----
----
Unit1.pas.30: if AChar <= #255 then;
005B46AC 66817DFAF3F8     cmp word ptr [ebp-$06],$f8f3
----

On the other hand, if I use AnsiChar for AChar, the code is compiled as below.
----
procedure TForm1.Button1Click(Sender: TObject);
var
  AChar: AnsiChar;
begin
  if AChar <= #255 then;
end;
----
----
Unit1.pas.30: if AChar <= #255 then;
005B46AC 807DFBFF         cmp byte ptr [ebp-$05],$ff
----

Tomohiro Takahashi at 4/29/2014 4:30:42 AM -
Please see article about Unicode version of Delphi.
[Delphi in a Unicode World Part III: Unicodifying Your Code - Using Character Literals]
http://edn.embarcadero.com/en/article/38693#4UsingCharacterLiterals

Server Response from: ETNACODE01