mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Gaius Mulley
committed
May 3, 2024
1 parent
f2d0116
commit a561dc0
Showing
6 changed files
with
290 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
MODULE testforloopzero2 ; | ||
|
||
FROM libc IMPORT printf, exit ; | ||
|
||
|
||
(* | ||
test - | ||
*) | ||
|
||
PROCEDURE test ; | ||
VAR | ||
i, n, | ||
zero, | ||
count: CARDINAL ; | ||
BEGIN | ||
n := 5 ; | ||
count := 0 ; | ||
zero := 0 ; | ||
FOR i := n TO zero 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 testforloopzero2. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
MODULE testforloopzero3 ; | ||
|
||
FROM libc IMPORT printf, exit ; | ||
|
||
|
||
(* | ||
test - | ||
*) | ||
|
||
PROCEDURE test ; | ||
VAR | ||
i, | ||
count: CARDINAL ; | ||
BEGIN | ||
count := 0 ; | ||
FOR i := 5 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 testforloopzero3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
MODULE testforloopzero4 ; | ||
|
||
FROM libc IMPORT printf, exit ; | ||
|
||
|
||
(* | ||
test - | ||
*) | ||
|
||
PROCEDURE test ; | ||
VAR | ||
i, | ||
count: INTEGER ; | ||
BEGIN | ||
count := 0 ; | ||
FOR i := 5 TO -5 BY -1 DO | ||
printf ("i = %d, count = %d\n", i, count); | ||
INC (count) | ||
END ; | ||
IF count = 11 | ||
THEN | ||
printf ("for loop counting down (%d) passed\n", count) | ||
ELSE | ||
printf ("for loop counting down (%d) failed\n", count) ; | ||
exit (1) | ||
END | ||
END test ; | ||
|
||
|
||
BEGIN | ||
test | ||
END testforloopzero4. |