Bookstore

Recent

Author Topic: directive {$OVERFLOWCHECKS}  (Read 264 times)

avk

  • Sr. Member
  • ****
  • Posts: 256
    • my self-education project
directive {$OVERFLOWCHECKS}
« on: March 26, 2020, 07:06:14 am »
Hmm, it looks like the FPC is ignoring the {$OVERFLOWCHECKS} directive.
If you compile this:
Code: Pascal  [Select]
  1. program overflowcheck;
  2.  
  3. {$mode delphi}{$Q+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   I: DWord   = High(DWord);
  10.   J: Integer = High(Integer);
  11.  
  12. begin
  13.   try
  14.     Inc(I);
  15.     WriteLn('I = ', I);
  16.   except
  17.     on e: Exception do
  18.       WriteLn(Format('%s with message "%s" during test I', [e.ClassName, e.Message]));
  19.   end;
  20.   try
  21.     Inc(J);
  22.     WriteLn('J = ', J);
  23.   except
  24.     on e: Exception do
  25.       WriteLn(Format('%s with message "%s" during test J', [e.ClassName, e.Message]));
  26.   end;
  27. end.
  28.  
as
Code: Text  [Select]
  1.   fpc -O3 overflowcheck.pas
  2.  
it prints
Code: Text  [Select]
  1. I = 0
  2. J = -2147483648
  3.  
What's the matter?

JernejL

  • Jr. Member
  • **
  • Posts: 73
Re: directive {$OVERFLOWCHECKS}
« Reply #1 on: March 26, 2020, 07:25:40 am »
On what processor architecture and platform are you running this?
 
Also check if range checks are actually off with IFOPT:
 
Code: Pascal  [Select]
  1. const
  2.  
  3.         {$IfOpt O+}
  4.     compiler_optimizations = true;
  5.         {$Else}
  6.     compiler_optimizations = false;
  7.         {$EndIf}
  8.  
  9.         {$IfOpt I+}
  10.     compiler_io_checks = true;
  11.     {$Else}
  12.     compiler_io_checks = false;
  13.     {$EndIf}
  14.  
  15.         {$IfOpt R+}
  16.     compiler_range_checks = true;
  17.     {$Else}
  18.     compiler_range_checks = false;
  19.     {$EndIf}
  20.  
  21.         {$IfOpt Q+}
  22.     compiler_overflow_checks = true;
  23.     {$Else}
  24.     compiler_overflow_checks = false;
  25.     {$EndIf}
  26.  
  27.         {$IfOpt S+}
  28.     compiler_stack_checks = true;
  29.     {$Else}
  30.     compiler_stack_checks = false;
  31.     {$EndIf}
  32.  
  33.         {$IfOpt C+}
  34.     compiler_assert_checks = true;
  35.     {$Else}
  36.     compiler_assert_checks = false;
  37.     {$EndIf}
  38.  
  39.  

avk

  • Sr. Member
  • ****
  • Posts: 256
    • my self-education project
Re: directive {$OVERFLOWCHECKS}
« Reply #2 on: March 26, 2020, 07:40:16 am »
On what processor architecture and platform are you running this?
win32, win64, Linux-x86_64, both FPC 3.04 and FPC 3.3.1
Also check if range checks are actually off with IFOPT:
For what?

JernejL

  • Jr. Member
  • **
  • Posts: 73
Re: directive {$OVERFLOWCHECKS}
« Reply #3 on: March 26, 2020, 07:41:17 am »
For what?

for.. verifying that your compiler options are set to what you think they are? add that into your function and print the constant values.
 

avk

  • Sr. Member
  • ****
  • Posts: 256
    • my self-education project
Re: directive {$OVERFLOWCHECKS}
« Reply #4 on: March 26, 2020, 07:51:47 am »
The documentation says:
Quote
The {$Q+} or {$OV+} (MACPAS mode only) or {$OVERFLOWCHECKS ON} directive turns on integer overflow checking. This means that the compiler inserts code to check for overflow when doing computations with integers. When an overflow occurs, the run-time library will generate a run-time error 215: it prints a message Overflow at xxx, and exits the program with exit code 215.
Remark:Overflow checking behaviour is not the same as in Turbo Pascal since all arithmetic operations are done via 32-bit or 64-bit values. Furthermore, the Inc() and Dec standard system procedures are checked for overflow in Free Pascal, while in Turbo Pascal they are not.
Using the {$Q-} switch (or the {$OV-} switch in MACPAS mode) switches off the overflow checking code generation.
The generation of overflow checking code can also be controlled using the -Co command line compiler option (see the User’s Guide).
In Delphi, overflow checking is only switchable at the procedure level. In Free Pascal, the {$Q } directive can be used at the expression level.
I didn't see any mention of "range checks".

PascalDragon

  • Hero Member
  • *****
  • Posts: 1088
  • Compiler Developer
Re: directive {$OVERFLOWCHECKS}
« Reply #5 on: March 26, 2020, 09:57:50 am »
I just checked the code. It's implemented for the two parameter variants of Inc and Dec (meaning Inc(MyVar, 42)), but not the one parameter ones... Please report a bug.

avk

  • Sr. Member
  • ****
  • Posts: 256
    • my self-education project
Re: directive {$OVERFLOWCHECKS}
« Reply #6 on: March 26, 2020, 11:20:38 am »
Thank you, done.