Page 1 of 1

CONST Definition Question

Posted: Tue Apr 09, 2024 9:09 am
by gray
Compiling

Code: Select all

MODULE TestConst;
  CONST C = 07FFFFFFFH + 1;
END TestConst.
results in

Code: Select all

  Line  Col
     2   28 Error: compiler exception
System.OverflowException: Arithmetic operation resulted in an overflow.
   at ORG.ORG.AddOp(Int32 op, Item& x, Item& y)
   at ORP.ORP.SimpleExpression(Item& x)
   at ORP.ORP.expression(Item& x)
   at ORP.ORP.Declarations(Int32& varsize)
   at ORP.ORP.Module()
   at Compiler.Compiler.Compile(String source, Writer w, Int32& errcnt)
On the other hand

Code: Select all

MODULE TestConst;
  CONST C = 0FFFFFFFFH + 1;
END TestConst.
compiles, and produces the correct code when the constant is used. Is defining a constant this way across the positive/negative 2-complement boundary not permitted?

Re: CONST Definition Question

Posted: Tue Apr 09, 2024 11:56 pm
by cfbsoftware
Constant expressions are evaluated at compile time. Decimal and hexadecimal INTEGER constants are signed integers and an integer overflow in a constant expression is trapped at compile time. For example:

Code: Select all

CONST
  MaxInt = 07FFFFFFFH;
  (* PosOverflow = MaxInt + 1; Error: integer overflow *)
  MinInt = 080000000H;
  (* NegOverflow = MinInt - 1; Error: integer overflow *)
  Negative1 = 0FFFFFFFFH;
  Zero = Negative1 + 1; (* OK *)
...
...
  ASSERT(Negative1 = -1); (* OK *)
  ASSERT(Zero = 0); (* OK *)
In the next release we'll handle an integer overflow exception during addition and subtraction operations so that compilation can continue in the same way as it does currently for an overflow during a multiplication operation e.g.

Code: Select all

MulOverflow = MaxInt * 2;