https://gcc.gnu.org/g:7dff53cb7bff5f523390fd4da5b32f3d6b300e2d

commit r14-11067-g7dff53cb7bff5f523390fd4da5b32f3d6b300e2d
Author: Gaius Mulley <gaiusm...@gmail.com>
Date:   Fri Dec 6 13:06:58 2024 +0000

    [PATCH] PR modula2/117904: cc1gm2 ICE when compiling a const built from VAL 
and SIZE
    
    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 <gaiusm...@gmail.com>

Diff:
---
 gcc/m2/gm2-compiler/M2GenGCC.mod              | 16 +++++++++++++---
 gcc/testsuite/gm2/iso/pass/forloopbyconst.mod | 25 +++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/gcc/m2/gm2-compiler/M2GenGCC.mod b/gcc/m2/gm2-compiler/M2GenGCC.mod
index b6e34e019b04..c5f5a7825956 100644
--- a/gcc/m2/gm2-compiler/M2GenGCC.mod
+++ b/gcc/m2/gm2-compiler/M2GenGCC.mod
@@ -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
index 000000000000..c0a1a06e0191
--- /dev/null
+++ b/gcc/testsuite/gm2/iso/pass/forloopbyconst.mod
@@ -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.

Reply via email to