]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH] PR modula2/117904: cc1gm2 ICE when compiling a const built from VAL and SIZE
authorGaius Mulley <gaiusmod2@gmail.com>
Fri, 6 Dec 2024 13:06:58 +0000 (13:06 +0000)
committerGaius Mulley <gaiusmod2@gmail.com>
Fri, 6 Dec 2024 13:06:58 +0000 (13:06 +0000)
This patch fixes an ICE which occurs when a positive ZType constant
increment is used during a FOR loop.

gcc/m2/ChangeLog:

PR modula2/117904
* gm2-compiler/M2GenGCC.mod (PerformLastForIterator): Add call to
BuildConvert when increment is > 0.

gcc/testsuite/ChangeLog:

PR modula2/117904
* gm2/iso/pass/forloopbyconst.mod: New test.

(cherry picked from commit 363382ac7c2b8f6a09415e905b349bb7eaeca38a)

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
gcc/m2/gm2-compiler/M2GenGCC.mod
gcc/testsuite/gm2/iso/pass/forloopbyconst.mod [new file with mode: 0644]

index b6e34e019b0466440c3ba2dc4c2552e7957c64e6..c5f5a78259564eddec094546f581e55442791b00 100644 (file)
@@ -541,9 +541,19 @@ BEGIN
       THEN
          (* If incr > 0 then LastIterator := ((e2-e1) DIV incr) * incr + e1.  *)
          expr := BuildSub (location, e2tree, e1tree, FALSE) ;
-         expr := BuildDivFloor (location, expr, incrtree, FALSE) ;
-         expr := BuildMult (location, expr, incrtree, FALSE) ;
-         expr := BuildAdd (location, expr, e1tree, FALSE)
+         incrtree := BuildConvert (location, GetTreeType (expr), incrtree, FALSE) ;
+         IF TreeOverflow (incrtree)
+         THEN
+            MetaErrorT0 (lastpos,
+                         'the intemediate calculation for the last iterator value in the {%kFOR} loop has caused an overflow') ;
+            NoChange := FALSE ;
+            SubQuad (quad) ;
+            success := FALSE
+         ELSE
+            expr := BuildDivFloor (location, expr, incrtree, FALSE) ;
+            expr := BuildMult (location, expr, incrtree, FALSE) ;
+            expr := BuildAdd (location, expr, e1tree, FALSE)
+         END
       ELSE
          (* Else use LastIterator := e1 - ((e1-e2) DIV PositiveBy) * PositiveBy
             to avoid unsigned div signed arithmetic.  *)
diff --git a/gcc/testsuite/gm2/iso/pass/forloopbyconst.mod b/gcc/testsuite/gm2/iso/pass/forloopbyconst.mod
new file mode 100644 (file)
index 0000000..c0a1a06
--- /dev/null
@@ -0,0 +1,25 @@
+MODULE forloopbyconst ;
+
+
+CONST
+   block = 4 ;
+
+
+(*
+   init -
+*)
+
+PROCEDURE init ;
+VAR
+   i, n: CARDINAL ;
+BEGIN
+   n := 10 ;
+   FOR i := 1 TO n BY block DO
+
+   END
+END init ;
+
+
+BEGIN
+   init
+END forloopbyconst.