gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gm2] runtime and compiletime overflow improvements


From: Gaius Mulley
Subject: [Gm2] runtime and compiletime overflow improvements
Date: Thu, 27 Jun 2019 14:10:14 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)


Hi,

I thought I'd post a message about the latest changed in gm2 trunk.
In essence I've been improving the runtime/compiletime overflow
detection and associated diagnostics.  gm2 now detects:
addition/subtraction/negation/multiplication/truncated division (pim2
DIV) overflow.  Also detected are assignment and parameter overflow
(say between INTEGER and CARDINAL).

The overflows are reported as warnings by default and are analysed
post optimisation - so GCC can perform code transformation to propagate
values (remove redundant code etc).  The gm2 plugin then looks for
calls to the exception routines which are will be called and issues
GCC warnings - which will be an improvement - as these are colorised
and also contain source code snippets!

Anyhow here are a few examples:

$ cat overflow.mod

MODULE overflow ;

FROM libc IMPORT printf ;


PROCEDURE func (x, y: CARDINAL) ;
VAR
   res: CARDINAL ;
BEGIN
   res := x DIV y ;
   printf ("res = %ud\n", res);
END func ;

VAR
   x, y: INTEGER ;
   u   : CARDINAL ;
BEGIN
   x := 1 ;
   y := -1 ;
   u := x DIV y ;
   printf ("u = %ud\n", u);
   func (x, y)
END overflow.

$ gm2 -O2 -g -fsoft-check-all -fpim2 overflow.mod
overflow.mod: In function ‘_M2_overflow_init’:
overflow.mod:20:3: warning: runtime error will occur, assignment will cause a 
range error, as the runtime instance value of ‘CARDINAL’ does not overlap with 
the type ‘INTEGER’ (in program module overflow)

   20 |    u := x DIV y ;
      |   ^

$ ./a.out
overflow.mod:20:3:assignment will cause a range error, as the runtime instance 
value of ‘CARDINAL’ does not overlap with the type ‘INTEGER’ (in program module 
overflow)
Aborted

$ cat overflow2.mod
MODULE overflow2 ;

FROM libc IMPORT printf ;


PROCEDURE func (x, y: CARDINAL) ;
VAR
   res: CARDINAL ;
BEGIN
   res := x DIV y ;
   printf ("res = %ud\n", res);
END func ;

VAR
   x, y: INTEGER ;
   u   : CARDINAL ;
BEGIN
   x := 1 ;
   y := -1 ;
   func (x, y) ;
END overflow2.

$ gm2 -g -O2 -fsoft-check-all overflow2.mod
overflow2.mod: In function ‘_M2_overflow2_init’:
overflow2.mod:20:3: warning: runtime error will occur, if this call is executed 
then the actual parameter ‘y’ will be out of range of the 2nd formal parameter 
‘y’ (in program module overflow2)

   20 |    func (x, y) ;
      |   ^

$ ./a.out
overflow2.mod:20:3:if this call is executed then the actual parameter ‘y’ will 
be out of range of the 2nd formal parameter ‘y’ (in program module overflow2)

cat overflowdiv1.mod
MODULE overflowdiv1 ;

FROM libc IMPORT printf ;


VAR
   u, x, y: [-1..4] ;
BEGIN
   printf ("hello world\n123\n");
   x := 3 ;
   y := -1 ;
   u := x DIV y ;  (* compiler should detect DIV causes overflow rather than 
assignment.  *)
   printf ("value of u = %d\n", VAL (INTEGER, u))
END overflowdiv1.

$ gm2 -fsoft-check-all -O2 -fpim2 -g overflowdiv1.mod
overflowdiv1.mod: In function ‘_M2_overflowdiv1_init’:
overflowdiv1.mod:12:3: warning: runtime error will occur, whole value truncated 
division will cause a range overflow (in _M2_overflowdiv1_init)

   12 |    u := x DIV y ;  (* compiler should detect DIV causes overflow rather 
than assignment.  *)
      |   ^

$ ./a.out
hello world
123
overflowdiv1.mod:12:3:whole value truncated division will cause a range 
overflow (in _M2_overflowdiv1_init)
Aborted


some of the column numbers need to be improved and also some of the
messages could contain more info about the types.  The pim4/iso divide
checking also need to be implemented (a combination of div ceil and
div floor) as well as modulus


regards,
Gaius



reply via email to

[Prev in Thread] Current Thread [Next in Thread]