]> git.ipfire.org Git - thirdparty/gcc.git/commit
PR modula2/114929 for loop fails to iterate down to zero when using a cardinal type
authorGaius Mulley <gaiusmod2@gmail.com>
Fri, 3 May 2024 00:22:10 +0000 (01:22 +0100)
committerGaius Mulley <gaiusmod2@gmail.com>
Fri, 3 May 2024 00:22:10 +0000 (01:22 +0100)
commita561dc0f6c7085e102fe9e9b6abd7f2138512576
tree995fcece3ab616e1bf342d1fbd01ad60a6f7dab2
parentf2d0116090d5b347a321506a807ebfca4bac5b3c
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>
gcc/m2/gm2-compiler/M2LangDump.mod
gcc/m2/gm2-compiler/M2Quads.mod
gcc/testsuite/gm2/pim/run/pass/testforloopzero.mod [new file with mode: 0644]
gcc/testsuite/gm2/pim/run/pass/testforloopzero2.mod [new file with mode: 0644]
gcc/testsuite/gm2/pim/run/pass/testforloopzero3.mod [new file with mode: 0644]
gcc/testsuite/gm2/pim/run/pass/testforloopzero4.mod [new file with mode: 0644]