View Issue Details

IDProjectCategoryView StatusLast Update
0033128FPCDocumentationpublic2018-02-24 16:49
ReporterThaddy de KoningAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionnot fixable 
PlatformAllOSAllOS VersionaLL
Product Version3.1.1Product Build 
Target Version3.2.0Fixed in Version3.1.1 
Summary0033128: Compiler does not respect evaluation order with HI/LO
DescriptionWhen evaluating HI, the compiler can not assume that any calculation can be performed on the eventual value. It does not respect the initial declaration.
Steps To Reproduce{$mode delphi}{$O+}
uses sysutils;
var
  W:Word;
begin
  W := $FF00;
  Inc(W);
  writeln('Result using Inc:', HI(W)); // should be 255/$FF OK
   W := $FF00;
  writeln('Result using W+1:',HI(W+1)); // should be 255/$FF: W+1 = $FF01 overflows to zero NOT OK
end.
Additional InformationThis is probably a duplicate, but anyway: this is a BIG bug. Delphi respects the size of (W+1).
It can only be solved with a hardcast, which is wrong.
TagsNo tags attached.
Fixed in Revision1464
FPCOldBugId
FPCTarget
Attached Files

Relationships

duplicate of 0020335 resolvedJonas Maebe result of a cast expression (compatible with Delphi) 
duplicate of 0031119 resolvedFlorian FEATURE REQUEST hiword,loword,hibyte,lobyte etc. 
has duplicate 0031998 resolvedJonas Maebe LO, HI functions using WORD as parameter and MATH inside switches it to inetger; 

Activities

Thaddy de Koning

2018-02-04 19:10

reporter   ~0106237

It does not even give an overflow error when overflow checks are on....

Jonas Maebe

2018-02-04 19:24

manager   ~0106238

Delphi's "hi" function always treats the argument as a 16 bit word, and always returns basically word(x) shr 8 (see http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Hi ). FPC has overloads for hi and lo for every integer type in the system unit, and always returns the low/high "half" of the argument.

Performing a calculation on a value automatically converts that value to the native integer type (32/64 bit depending on the target processor on most targets), and hence you are getting high 16/32 bits of 0xff01 in your second call, which is 0.

As the hi/lo functions are functions in the system unit (https://www.freepascal.org/docs-html/current/rtl/system/hi.html ), this behaviour cannot be changed based on the syntax mode.

Thaddy de Koning

2018-02-04 21:02

reporter   ~0106240

Last edited: 2018-02-04 21:09

View 3 revisions

That's simply avoiding the basic issue: respecting evaluation order. You can not simply always close this. It is a basic functionality failure.
Please discuss on the mailing list. It is NOT about how Delphi behaves, it is about evaluation order and operand size. BTW (almost) Any compiler does it right. FPC gets it wrong.
You wrote it yourself: A shift from $FF01 should return $FF..... Not zero.
There is something seriously wrong there...

Thaddy de Koning

2018-02-04 21:10

reporter   ~0106241

Last edited: 2018-02-04 21:17

View 3 revisions

Maybe Florian has an explanation for this issue? I respect all compiler people but this is simply wrong.
FPC treats HI as including a sign bit?.....;)

Marco van de Voort

2018-02-04 21:29

manager   ~0106242

Last edited: 2018-02-04 21:55

View 2 revisions

(it should at least be properly documented/annotated in some known differences to Delphi paragraph? ->documentation)

Jonas Maebe

2018-02-04 22:32

manager   ~0106244

It has nothing to do with evaluation order. The expression "wordvar+1" is a longint in both Delphi and FPC (on 32 bit platforms; on 64 bit platforms, it's int64 in both compilers).

The result of hi(longint) is different and in FPC because, as mentioned in Embarcadero's documentation, hi(<any type>) is always evaluated as hi(word), while FPC (as mentioned in FPC's documentation) has overloads for all possible types.

Jonas Maebe

2018-02-04 23:01

manager   ~0106246

It's mentioned on http://www.freepascal.org/port.var , but yes, it probably also should be in the regular documentation.

Michael Van Canneyt

2018-02-24 16:49

administrator   ~0106571

Documented in user's guid and in the Hi/Lo reference documentation.

Issue History

Date Modified Username Field Change
2018-02-04 19:08 Thaddy de Koning New Issue
2018-02-04 19:10 Thaddy de Koning Note Added: 0106237
2018-02-04 19:24 Jonas Maebe Note Added: 0106238
2018-02-04 19:24 Jonas Maebe Status new => resolved
2018-02-04 19:24 Jonas Maebe Resolution open => not fixable
2018-02-04 19:24 Jonas Maebe Assigned To => Jonas Maebe
2018-02-04 19:39 Florian Relationship added duplicate of 0020335
2018-02-04 19:40 Florian Relationship added duplicate of 0031119
2018-02-04 19:41 Florian Relationship added has duplicate 0031998
2018-02-04 21:02 Thaddy de Koning Note Added: 0106240
2018-02-04 21:02 Thaddy de Koning Status resolved => feedback
2018-02-04 21:02 Thaddy de Koning Resolution not fixable => reopened
2018-02-04 21:03 Thaddy de Koning Note Edited: 0106240 View Revisions
2018-02-04 21:09 Thaddy de Koning Note Edited: 0106240 View Revisions
2018-02-04 21:10 Thaddy de Koning Note Added: 0106241
2018-02-04 21:10 Thaddy de Koning Status feedback => assigned
2018-02-04 21:14 Thaddy de Koning Note Edited: 0106241 View Revisions
2018-02-04 21:17 Thaddy de Koning Note Edited: 0106241 View Revisions
2018-02-04 21:29 Marco van de Voort Note Added: 0106242
2018-02-04 21:47 Marco van de Voort Category Compiler => Documentation
2018-02-04 21:55 Marco van de Voort Note Edited: 0106242 View Revisions
2018-02-04 22:32 Jonas Maebe Note Added: 0106244
2018-02-04 22:32 Jonas Maebe Status assigned => resolved
2018-02-04 22:32 Jonas Maebe Resolution reopened => not fixable
2018-02-04 23:00 Jonas Maebe Assigned To Jonas Maebe => Michael Van Canneyt
2018-02-04 23:00 Jonas Maebe Status resolved => assigned
2018-02-04 23:01 Jonas Maebe Note Added: 0106246
2018-02-24 16:49 Michael Van Canneyt Fixed in Revision => 1464
2018-02-24 16:49 Michael Van Canneyt Note Added: 0106571
2018-02-24 16:49 Michael Van Canneyt Status assigned => resolved
2018-02-24 16:49 Michael Van Canneyt Fixed in Version => 3.1.1
2018-02-24 16:49 Michael Van Canneyt Target Version => 3.2.0