public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
@ 2024-05-02 23:56 gaius at gcc dot gnu.org
  2024-05-02 23:56 ` [Bug modula2/114929] " gaius at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-05-02 23:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

            Bug ID: 114929
           Summary: for loop fails to iterate down to zero if a cardinal
                    type (unsigned type) is used with a step of -1.
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: modula2
          Assignee: gaius at gcc dot gnu.org
          Reporter: gaius at gcc dot gnu.org
  Target Milestone: ---

An example of the bug follows:

MODULE testforloopzero ;

FROM libc IMPORT printf, exit ;


(*
   test -
*)

PROCEDURE test ;
VAR
   i, n,
   count: CARDINAL ;
BEGIN
   n := 5 ;
   count := 0 ;
   FOR i := n TO 0 BY -1 DO
      printf ("i = %d, count = %d\n", i, count);
      INC (count)
   END ;
   IF count = 6
   THEN
      printf ("for loop counting down passed\n")
   ELSE
      printf ("for loop counting down failed\n") ;
      exit (1)
   END
END test ;


BEGIN
   test
END testforloopzero.

$ gm2 testforloopzero.mod
$ ./a.out

i = 5, count = 0
for loop counting down failed

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
@ 2024-05-02 23:56 ` gaius at gcc dot gnu.org
  2024-05-03  0:01 ` gaius at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-05-02 23:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

Gaius Mulley <gaius at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2024-05-02
     Ever confirmed|0                           |1

--- Comment #1 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
  2024-05-02 23:56 ` [Bug modula2/114929] " gaius at gcc dot gnu.org
@ 2024-05-03  0:01 ` gaius at gcc dot gnu.org
  2024-05-03  0:23 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-05-03  0:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

--- Comment #2 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Created attachment 58092
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58092&action=edit
Proposed fix

Here is a proposed bug fix with 6 for loop regression tests.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
  2024-05-02 23:56 ` [Bug modula2/114929] " gaius at gcc dot gnu.org
  2024-05-03  0:01 ` gaius at gcc dot gnu.org
@ 2024-05-03  0:23 ` cvs-commit at gcc dot gnu.org
  2024-05-03  0:27 ` gaius at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-03  0:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Gaius Mulley <gaius@gcc.gnu.org>:

https://gcc.gnu.org/g:a561dc0f6c7085e102fe9e9b6abd7f2138512576

commit r15-122-ga561dc0f6c7085e102fe9e9b6abd7f2138512576
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date:   Fri May 3 01:22:10 2024 +0100

    PR modula2/114929 for loop fails to iterate down to zero when using a
cardinal type

    There is a bug in the for loop control code which is exposed when an
    unsigned type is used in the iterator variable.  See
    gm2/pim/run/pass/testforloopzero[234].mod.  The bug is in the
    calculation of the last iterator value.  The bug fix is to avoid using
    negative expressions when calculating the last iterator value with a
    negative step value.  This patch detects if e1, e2, step value are all
    constant, in which case the ztype is used internally and there is no
    overflow.  If the last iterator value is held in a variable then it
    uses a different method to calculate the last iterator depending upon
    the sign of the step value.

    gcc/m2/ChangeLog:

            PR modula2/114929
            * gm2-compiler/M2LangDump.mod (GenQualidentSymString): Add
            missing return result into identstr.
            * gm2-compiler/M2Quads.mod (ForLoopLastIteratorVariable): New
            procedure.
            (ForLoopLastIteratorConstant): Ditto.
            (ForLoopLastIterator): Ditto.
            (BuildForToByDo): Remove LastIterator calculation and call
            ForLoopLastIterator instead.
            (FinalValue): Replace with ...
            (LastIterator): ... this.

    gcc/testsuite/ChangeLog:

            PR modula2/114929
            * gm2/pim/run/pass/testforloopzero.mod: New test.
            * gm2/pim/run/pass/testforloopzero2.mod: New test.
            * gm2/pim/run/pass/testforloopzero3.mod: New test.
            * gm2/pim/run/pass/testforloopzero4.mod: New test.

    Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-05-03  0:23 ` cvs-commit at gcc dot gnu.org
@ 2024-05-03  0:27 ` gaius at gcc dot gnu.org
  2024-05-03 19:48 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-05-03  0:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

Gaius Mulley <gaius at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|14.0                        |15.0

--- Comment #4 from Gaius Mulley <gaius at gcc dot gnu.org> ---
For completeness the test code at the top of the PR passes:

$ gm2 testforloopzero.mod 
$ ./a.out 
i = 5, count = 0
i = 4, count = 1
i = 3, count = 2
i = 2, count = 3
i = 1, count = 4
i = 0, count = 5
for loop counting down passed

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-05-03  0:27 ` gaius at gcc dot gnu.org
@ 2024-05-03 19:48 ` cvs-commit at gcc dot gnu.org
  2024-05-03 21:59 ` cvs-commit at gcc dot gnu.org
  2024-05-03 22:29 ` gaius at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-03 19:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Gaius Mulley <gaius@gcc.gnu.org>:

https://gcc.gnu.org/g:c943d7b5c40f447b12431df9ad27a47dad95026d

commit r15-137-gc943d7b5c40f447b12431df9ad27a47dad95026d
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date:   Fri May 3 20:48:01 2024 +0100

    PR modula2/114929 extra for loop iteration count regression tests

    This patch introduces three more for loop tests checking the iteration
    count using the CHAR and enumeration data types.

    gcc/testsuite/ChangeLog:

            PR modula2/114929
            * gm2/pim/run/pass/testforloopchar.mod: New test.
            * gm2/pim/run/pass/testforloopchar2.mod: New test.
            * gm2/pim/run/pass/testforloopenum.mod: New test.

    Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-05-03 19:48 ` cvs-commit at gcc dot gnu.org
@ 2024-05-03 21:59 ` cvs-commit at gcc dot gnu.org
  2024-05-03 22:29 ` gaius at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-03 21:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-14 branch has been updated by Gaius Mulley
<gaius@gcc.gnu.org>:

https://gcc.gnu.org/g:d811080341adf9d805e3f79a8fd9be2e13bd9848

commit r14-10166-gd811080341adf9d805e3f79a8fd9be2e13bd9848
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date:   Fri May 3 22:58:11 2024 +0100

    [PATCH] PR modula2/114929 for loop fails to iterate down to zero

    There is a bug in the for loop control code which is exposed when an
    unsigned type is used in the iterator variable.  See
    gm2/pim/run/pass/testforloopzero[234].mod.  The bug is in the
    calculation of the last iterator value.  The bug fix is to avoid using
    negative expressions when calculating the last iterator value with a
    negative step value.  This patch detects if e1, e2, step value are all
    constant, in which case the ztype is used internally and there is no
    overflow.  If the last iterator value is held in a variable then it
    uses a different method to calculate the last iterator depending upon
    the sign of the step value.

    gcc/m2/ChangeLog:

            PR modula2/114929
            * gm2-compiler/M2Quads.mod (ForLoopLastIteratorVariable): New
            procedure.
            (ForLoopLastIteratorConstant): Ditto.
            (ForLoopLastIterator): Ditto.
            (BuildForToByDo): Remove LastIterator calculation and call
            ForLoopLastIterator instead.
            (FinalValue): Replace with ...
            (LastIterator): ... this.

    gcc/testsuite/ChangeLog:

            PR modula2/114929
            * gm2/pim/run/pass/testforloopzero.mod: New test.
            * gm2/pim/run/pass/testforloopzero2.mod: New test.
            * gm2/pim/run/pass/testforloopzero3.mod: New test.
            * gm2/pim/run/pass/testforloopzero4.mod: New test.

    (cherry picked from commit a561dc0f6c7085e102fe9e9b6abd7f2138512576)

    Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug modula2/114929] for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1.
  2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-05-03 21:59 ` cvs-commit at gcc dot gnu.org
@ 2024-05-03 22:29 ` gaius at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-05-03 22:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114929

Gaius Mulley <gaius at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #7 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Closing now the patch has been applied and back ported.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-05-03 22:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-02 23:56 [Bug modula2/114929] New: for loop fails to iterate down to zero if a cardinal type (unsigned type) is used with a step of -1 gaius at gcc dot gnu.org
2024-05-02 23:56 ` [Bug modula2/114929] " gaius at gcc dot gnu.org
2024-05-03  0:01 ` gaius at gcc dot gnu.org
2024-05-03  0:23 ` cvs-commit at gcc dot gnu.org
2024-05-03  0:27 ` gaius at gcc dot gnu.org
2024-05-03 19:48 ` cvs-commit at gcc dot gnu.org
2024-05-03 21:59 ` cvs-commit at gcc dot gnu.org
2024-05-03 22:29 ` gaius at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).