Recent

Author Topic: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]  (Read 2109 times)

Snify

  • New Member
  • *
  • Posts: 11
KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« on: October 18, 2021, 03:00:10 pm »
I am currently working on a project where I port Kylix Code to FPC.
Now, I have discovered a very strange thing...
( I have tested this only with Kylix - I am not sure about other Delphi Versions or loops...)
My FPC Project is configured as Linux-32bit with the regular Ansi Delphi Syntax for Kylix compatibility.

However a regular "for" loop counter behaves differently in Kylix than in Lazarus.

For exmaple (a simple loop):

Code: Pascal  [Select][+][-]
  1.      
  2. for i := 1 to 6 do
  3. begin
  4.  // some non critical operations    
  5. end;
  6. ShowMessage (inttostr(i));
  7.  

Kylix shows a value of 7 whereas FPC shows a value of 6 - at the end of the loop!
However, if you "BREAK" the loop in between (e.g. if i = 6 then break) - the value stays at 6 on both compilers.

So Kylix (for whatever reason) adds an extra value AFTER the loop has been SUCCESSFULLY finished.

This is a severe behavior throughout all loops in the project..

Could someone please give me any advice on how to solve/emulate this behavior in Lazarus, without much effort?
Is it possible to let the FPC developer know about this, so they may fix/emulate this?

Thank you so much in advance.
« Last Edit: October 18, 2021, 03:01:55 pm by Snify »

howardpc

  • Hero Member
  • *****
  • Posts: 3841
Re: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« Reply #1 on: October 18, 2021, 03:12:59 pm »
Outside a for loop the loop variable is undefined, and any value it has is implementation-dependent (as you found).
You can always make an explicit assignment:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   i, KylixEmulation: Integer;
  10.  
  11. begin
  12.   KylixEmulation := 1;
  13.   for i := KylixEmulation to 6 do
  14.     begin
  15.       Inc(KylixEmulation);
  16.     end;
  17.   WriteLn('i: ',i,' KylixEmulation: ',KylixEmulation);
  18.   ReadLn;
  19. end.

Seenkao

  • Sr. Member
  • ****
  • Posts: 261
Re: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« Reply #2 on: October 18, 2021, 03:16:26 pm »
Yandex translate:
use the "while" loop, there will be the necessary behavior.

Snify

  • New Member
  • *
  • Posts: 11
Re: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« Reply #3 on: October 18, 2021, 03:17:24 pm »
Outside a for loop the loop variable is undefined, and any value it has is implementation-dependent (as you found).
You can always make an explicit assignment:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   i, KylixEmulation: Integer;
  10.  
  11. begin
  12.   KylixEmulation := 1;
  13.   for i := KylixEmulation to 6 do
  14.     begin
  15.       Inc(KylixEmulation);
  16.     end;
  17.   WriteLn('i: ',i,' KylixEmulation: ',KylixEmulation);
  18.   ReadLn;
  19. end.

Thank you for your answer... I understand it.
So I guess, it is up to me to look through all the loops in the project and fix this.
Never use it outside the loop  :'(

Is there maybe an automated approach or so to "find" all loops in a source file? (without searching for keywords like while/for/repeat/etc....)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9713
  • FPC developer.
Re: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« Reply #4 on: October 18, 2021, 03:17:35 pm »
However a regular "for" loop counter behaves differently in Kylix than in Lazarus.

Doesn't matter. How is it /documented/ in Kylix?

Adhoc testing might depend on optimization level, architecture and codegeneration pecularities. IOW it might be a coincidence that your Kylix code always seems to do this, but not hold true always over all options, versions  etc. 

To my best knowledge, all Pascal's specify the loopvar after the for loop as undefined, with the exception of a BREAK/exit/goto in the for loop (in which case the for loop is more while loop like). This also is true for all Delphi versions, so I have no reason it was different for Kylix which was mostly D6/7 like.

So the bug is actually in relying on this coincidental behaviour.

E.g. the Delphi help says:

After the for statement terminates (provided this was not forced by a Break or an Exit procedure), the value of counter is undefined.
« Last Edit: November 02, 2021, 01:50:27 pm by marcov »

Snify

  • New Member
  • *
  • Posts: 11
Re: KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]
« Reply #5 on: October 18, 2021, 03:21:18 pm »
Thank you for the helpful information.
I used a very empty Kylix Project with different build modes and they all seem to behave like this (for now).

Better is, to just avoid the counter vars outside the loop.

Altough undefined is odd, if you see exactly the last value plus 1...
I'd except some other random value(s)
« Last Edit: October 18, 2021, 03:27:19 pm by Snify »

Kays

  • Sr. Member
  • ****
  • Posts: 383
  • Whasup!?
    • KaiBurghardt.de
Re: Kylix loop vs FPC loop [severe bug?]
« Reply #6 on: October 18, 2021, 05:21:21 pm »
Kylix shows a value of 7 whereas FPC shows a value of 6 - at the end of the loop!
Tell that the Kylix-people. It’s a “severe bug” in Kylix. A glance at ISO 7185 tells us
Quote
Code: Pascal  [Select][+][-]
  1. for v := e1 to e2 do body
shall be equivalent to
Code: Pascal  [Select][+][-]
  1. begin
  2.         temp1 := e1;
  3.         temp2 := e2;
  4.         if temp1 <= temp2 then
  5.         begin
  6.                 v := temp1;
  7.                 body;
  8.                 while v <> temp2 do
  9.                 begin
  10.                         v := succ(v);
  11.                         body
  12.                 end
  13.         end
  14. end
Note
Quote
After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined.
You (as a programmer) aren’t supposed to presume your control-variable has any particular value. In other words, you shouldn’t be “outraged” that FPC terminates with 6 and Kylix with 7.

“It’s not a bug, it’s a feature.” FPC has the “feature” that after a for-loop the control-variable holds the finalValue.
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 3547
Re: Kylix loop vs FPC loop [severe bug?]
« Reply #7 on: October 18, 2021, 05:37:01 pm »
Quote
After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined.

And I believe that's always been the case, i.e. it wasn't just a "tightening up" introduced by the ISO standard.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018