CONST Definition Question

General discussions about working with the Astrobe IDE and programming ARM Cortex-M0, M3, M4 and M7 microcontrollers.
Post Reply
gray
Posts: 116
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

CONST Definition Question

Post by gray » Tue Apr 09, 2024 9:09 am

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?

cfbsoftware
Site Admin
Posts: 499
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: CONST Definition Question

Post by cfbsoftware » Tue Apr 09, 2024 11:56 pm

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;

Post Reply