Recent

Author Topic: likely bug in variable definitions  (Read 3532 times)

440bx

  • Hero Member
  • *****
  • Posts: 3944
likely bug in variable definitions
« on: October 01, 2022, 08:08:39 am »
Hello,

Consider the following program:
Code: Pascal  [Select][+][-]
  1. program DefinitionBug;
  2.  
  3. function A() : integer;
  4. var
  5.   Avar : qword - 0;  { is this supposed to compile ??? }
  6.  
  7. begin
  8.   result := Avar + 1;
  9. end;
  10.  
  11. begin
  12.   A();
  13. end.            

the definition in line 5 doesn't make sense but the compiler accepts it.

Comments and/or confirmation that it is a bug, welcome.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: likely bug in variable definitions
« Reply #1 on: October 01, 2022, 08:35:15 am »
That is strange. (that FPC accepts that...)

However JCF (Jedit Code Formatter) can't parse that...

Code: Text  [Select][+][-]
  1. ./lazforum/60776/DefinitionBug.pas(5,20) Error Exception TEParseError  Unexpected token, expected ";"
  2. Near -

And in my editor Line 5 Column 20 is the "-" in "qword - 0"

Can you file a bug report on this against FPC?

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: likely bug in variable definitions
« Reply #2 on: October 01, 2022, 09:07:50 am »
 :D
Code: Pascal  [Select][+][-]
  1. {$LONGSTRINGS OFF}
  2.  
  3. function Test: Integer;
  4. var
  5.   AVar: QWord - 0; { is this supposed to compile ??? }
  6. begin
  7.   Result := High(Char + 'This is also still supported'); // =255
  8. end;
  9.  
  10. begin
  11.   Test;
  12. end.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: likely bug in variable definitions
« Reply #3 on: October 01, 2022, 01:46:22 pm »
The latter one is already reported and I seem to remember it has been fixed in fpc main. Not sure though.

Bart

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: likely bug in variable definitions
« Reply #4 on: October 01, 2022, 01:58:30 pm »
 High(Char + 'This is also still supported')  ->  Error: type identifier not allowed here

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: likely bug in variable definitions
« Reply #5 on: October 01, 2022, 02:25:43 pm »
fpc main of today compiles both examples (32/64-bit on windows).
It'll emit an error on the second example in {$H+} mode, not in {$R-} mode.

Bart

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: likely bug in variable definitions
« Reply #6 on: October 01, 2022, 02:46:50 pm »
The compiler eliminates arithmetically neutral operations (+0, -0, *1 and div 1). This elimination takes place at an unusual place, though. It’s weird that the issue only arises with qWord and int64.

The latter one is already reported and I seem to remember it has been fixed in fpc main.
I did complain about it once, see #37547. It was marked as a duplicate of #32994 which is still open.
Yours Sincerely
Kai Burghardt

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: likely bug in variable definitions
« Reply #7 on: October 01, 2022, 04:11:28 pm »

Still compiles and runs for me, I just now updated my git checkout of fpc and rebuilt and reinstalled...
3.3.1-11945-g0a9e1ede72

Code: Pascal  [Select][+][-]
  1. program hellofpcworld;
  2.  
  3. {$LONGSTRINGS OFF}
  4.  
  5. function Test: Integer;
  6. var
  7.   AVar: QWord - 0; { is this supposed to compile ??? }
  8. begin
  9.   AVar := 0;
  10.   writeln('Avar = ', AVar);
  11.   Result := High(Char + 'This is also still supported'); // =255
  12. end;
  13.  
  14. const boo = 'dogÜ';
  15.  
  16. begin
  17.   writeln ('Hello FPC world! ', boo);
  18.   writeln (Test);
  19. end.
  20.  

compiles...

Code: Text  [Select][+][-]
  1. Hint: Start of reading config file /home/shared-development/fpc_usr/lib/fpc/etc/fpc.cfg
  2. Hint: End of reading config file /home/shared-development/fpc_usr/lib/fpc/etc/fpc.cfg
  3. Free Pascal Compiler version 3.3.1 [2022/10/01] for x86_64
  4. Copyright (c) 1993-2022 by Florian Klaempfl and others
  5. Target OS: Linux for x86-64
  6. Compiling lazforum/hellofpcworld.pas
  7. Linking /home/shared-development/sandbox/bin/hellofpcworld
  8. 19 lines compiled, 0.1 sec, 156832 bytes code, 1139896 bytes data
  9. 2 hint(s) issued
  10.  

runs...

Code: Text  [Select][+][-]
  1. Hello FPC world! dogÜ
  2. Avar = 0
  3. 255
  4.  

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: likely bug in variable definitions
« Reply #8 on: October 01, 2022, 04:39:38 pm »
function A() : integer;
var
  Avar : qword - 0;  { is this supposed to compile ??? }

it also works as a global variable

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var a: qword + 0;
  4.  

however a different type won't work

Code: Pascal  [Select][+][-]
  1. var a: word + 0; // project1.lpr(4,16) Error: Error in type definition
  2. var a: dword + 0; // project1.lpr(4,16) Error: Error in type definition
  3.  

same with int64/int32 types

My guess is this is because CPU register size. I am compiling in Win64, so int64/qword is okay

I can not test Win32 compilation - the Win64 Lazarus installation does not permit it...

Code: Pascal  [Select][+][-]
  1. var a: string + ''; // project1.lpr(4,15) Fatal: Syntax error, ";" expected but "+" found
fails also

so it is not just about "eliminating all the obvious no-ops", something more complex




alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: likely bug in variable definitions
« Reply #9 on: October 01, 2022, 05:14:15 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   a: qword + 0;
  4.   b: dword + 0;
  5. begin
  6. end.

Compiles without error. Win32 target.

Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 i386-win32-win32/win64
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: likely bug in variable definitions
« Reply #10 on: October 01, 2022, 05:18:22 pm »
and then what about 16-bit "word + 0"  or "int16 + 0" or "smallint + 0"?

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: likely bug in variable definitions
« Reply #11 on: October 01, 2022, 05:35:11 pm »
and then what about 16-bit "word + 0"  or "int16 + 0" or "smallint + 0"?
Code: Pascal  [Select][+][-]
  1. var a: word + 0;
Gives an error.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: likely bug in variable definitions
« Reply #12 on: October 01, 2022, 05:40:28 pm »
Q.E.D.

at least Intel back-end has thios error triggered on integer types sized in CPU general purpose register sizes ( 1x, 2x, ....)

i think you can make a 6-bytes integer type, subrange, to try intermediate point, but... whatever :-D It is not really needed, just if you think it being funny

something like

Code: Pascal  [Select][+][-]
  1. program;
  2. type  a = 0 .. $ffFFffFFffFF;
  3. var b: a + 0;
  4. begin end.
  5.  

But perhaps here SizeOf(a) and SizeOf(b) would be 8 not 6 ?  At least that is so on Win64 target
« Last Edit: October 01, 2022, 05:42:33 pm by Arioch »

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: likely bug in variable definitions
« Reply #13 on: October 01, 2022, 06:00:37 pm »
Q.E.D.
My guess is this is because CPU register size. I am compiling in Win64, so int64/qword is okay
And how that relates to the FPC parser? Because (AFAIK) it is an incorrect syntax at the first place.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: likely bug in variable definitions
« Reply #14 on: October 01, 2022, 06:05:33 pm »
i can only suspect, as vaguely as can be, that memory sizes shorter than GPR trigger a different code path, which includes typecasting, upsizing up to the GRP size multiple

but i don';t know of course, i just observe the black box

 

TinyPortal © 2005-2018