I have a piece of code that does not follow my understanding of pascal. As I don't want to bother you in reading long code, here is a tiny model, that (mis)behave the same. 1=1 and 1=2 are to avoid discussing "when it does occur" or "once it stopped". I just don't know wether it is a bug or an error of mine.
1 program model(input, output); 2 var 3 i : integer; 4 5 begin 6 repeat 7 for i:=1 to 10 do 8 if 1=1 then 9 writeln('*'); 10 until 1=2; 11 end.
As I understand it, once reached line 7 (the 'for' loop), the following instruction ('if then') is executed ten times, so the output consists in a row of ten stars as the 1=1 is always true. Then the 'repeat until' loop is executed, forever since 1 never equals 2. It is not obvious the programme does not work if one executes it, but the debugger will betray another behaviour (if you change something in the structure, it is not certain it will still "not work"). Once reached the 'for' statement, only one star is printed then the pointer directely jumps on repeat, not printing ten stars in a row, just one. Nothing is changed in 'begin end;' insertion in the code.
If it is some kind of bug, I theoretically use last GPC beta version.
Cheers.
F.P.L
According to FPL:
[...]
1 program model(input, output); 2 var 3 i : integer; 4 5 begin 6 repeat 7 for i:=1 to 10 do 8 if 1=1 then 9 writeln('*'); 10 until 1=2; 11 end.
[Debugging the program above ...] Once reached the 'for' statement, only one star is printed then the pointer directely jumps on repeat, not printing ten stars in a row, just one. Nothing is changed in 'begin end;' insertion in the code.
That's not a bug, that's optimization. GPC sees
* that "if 1=1" is always true ("if true") and eliminates that line, and
* that the program just prints stars forever independent of the counter `i' and eliminates the counting (the `for' loop).
This results in
repeat writeln ( '*' ); until false;
OTOH, if you observe the same behaviour with all optimization turned *off* (i.e. you do not give the `-O' switch to the call to `gpc'), it *is* a bug, not in code generation but in GPC's generation of debugging information.
Please let us know which is the case.
Greetings,
Peter
Peter,
OTOH, if you observe the same behaviour with all optimization turned *off* (i.e. you do not give the `-O' switch to the call to `gpc'), it *is* a bug, not in code generation but in GPC's generation of debugging information.
Please let us know which is the case.
-O does not change anything in debugging the little code. However, my actual code is not giving 1=1, but instead something like
repeat for Ibalaye_ligne:=1 to Ilargeur_pattern do if (Pbalaye_fichier^.contenu[Ibalaye_ligne]='*') or (Pbalaye_fichier^.contenu[Ibalaye_ligne]='O') then begin
univers.courant[Ixinstall_pattern+Ibalaye_ligne][Iyinstall_pattern]:=True; new(Pajoute_cellule); Pajoute_cellule^.x:=Ixinstall_pattern; Pajoute_cellule^.y:=Iyinstall_pattern; Pajoute_cellule^.suivante:=NIL; Pbalaye_liste_cellule^.suivante:=Pajoute_cellule; Pbalaye_liste_cellule:=Pajoute_cellule; end; Iyinstall_pattern:=Iyinstall_pattern+1; Pbalaye_fichier:=Pbalaye_fichier^.suivante; until Pbalaye_fichier^.suivante=NIL; end;
where GPC should not simplify anything as there is nothing to simplify.
If it is only debugging problem, the problem should go away when putting the breakpoint far away, isn't it? And it does not!
(sorry Peter if I mail directly to you, it is to speed up the mailing)
According to FPL:
-O does not change anything in debugging the little code. [...]
Then this is indeed a bug. Thanks for the report!
[...] where GPC should not simplify anything as there is nothing to simplify.
Oh - GPC will find something to simplify, but not that much.
If it is only debugging problem, the problem should go away when putting the breakpoint far away, isn't it? And it does not!
Then there is also a bug in your program.
Please insert some `writeln' statements between the lines of your program. If the resulting output is too much, write to a file instead of the screen which you can examine with a text editor or `less' afterwards.
(sorry Peter if I mail directly to you, it is to speed up the mailing)
That's okay as we `cc' the mail to the list.
Good luck,
Peter