]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR modula2/115111 Incorrect line debugging locations at the end of the WHILE loop
authorGaius Mulley <gaiusmod2@gmail.com>
Sun, 16 Mar 2025 15:56:48 +0000 (15:56 +0000)
committerGaius Mulley <gaiusmod2@gmail.com>
Sun, 16 Mar 2025 15:56:48 +0000 (15:56 +0000)
This fix corrects the END token position used during the GotoOp at the
bottom of the WHILE loop.  The fix is to pass the relative token position
down to M2Quads.  This method should be replicated for the other loops
END or UNTIL keywords and possibly the END statements for
conditional statements.

gcc/m2/ChangeLog:

PR modula2/115111
* gm2-compiler/M2GenGCC.mod (CodeStatementNote): Add debugging.
* gm2-compiler/M2Quads.def (BuildEndWhile): New parameter reltokpos.
* gm2-compiler/M2Quads.mod (BuildEndWhile): Reimplement using new parameter.
* gm2-compiler/P3Build.bnf (WhileStatement): Call BuildEndWhile
with -1 relative position.
* gm2-gcc/m2block.cc (do_add_stmt): Tidy comment.
(GetGlobals): Ditto.
(flush_pending_note): Remove #if 0 code.
* gm2-gcc/m2pp.cc (m2pp_nop_expr): New function.
(m2pp_statement): New case clause call m2pp_nop_expr.

gcc/testsuite/ChangeLog:

PR modula2/115111
* gm2/pim/pass/whilestep.mod: New test.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
gcc/m2/gm2-compiler/M2GenGCC.mod
gcc/m2/gm2-compiler/M2Quads.def
gcc/m2/gm2-compiler/M2Quads.mod
gcc/m2/gm2-compiler/P3Build.bnf
gcc/m2/gm2-gcc/m2block.cc
gcc/m2/gm2-gcc/m2pp.cc
gcc/testsuite/gm2/pim/pass/whilestep.mod [new file with mode: 0644]

index 761e79bef29552dedfd62f4111b60ebb16e4f1a0..ec38dc2e7cb491765264d143a04644f60b11ad65 100644 (file)
@@ -1032,6 +1032,10 @@ END FoldStatementNote ;
 
 PROCEDURE CodeStatementNote (tokenno: CARDINAL) ;
 BEGIN
+   IF Debugging
+   THEN
+      MetaErrorT0 (tokenno, '{%W} statement note')
+   END ;
    CurrentQuadToken := tokenno ;
    addStmtNote (TokenToLocation (tokenno))
 END CodeStatementNote ;
index ec82accf5aaf19a65d51e30954729b518187b518..28e43aa45ebebb47b460a947cdadc35eaec47cc4 100644 (file)
@@ -1188,7 +1188,7 @@ PROCEDURE BuildDoWhile ;
                    False exit is backpatched with q+1
 *)
 
-PROCEDURE BuildEndWhile ;
+PROCEDURE BuildEndWhile (reltokpos: INTEGER) ;
 
 
 (*
index d057a27fd862b331a0053519a0277e4f85aba083..a45d67a198abb445c4c528bdfeeeef150a34d358 100644 (file)
@@ -4283,16 +4283,19 @@ END BuildDoWhile ;
                    False exit is backpatched with q+1
 *)
 
-PROCEDURE BuildEndWhile ;
+PROCEDURE BuildEndWhile (reltokpos: INTEGER) ;
 VAR
+   tok  : CARDINAL ;
    While,
    t, f : CARDINAL ;
 BEGIN
-   PopBool(t, f) ;
-   Assert(t=0) ;
-   PopT(While) ;
-   GenQuad(GotoOp, NulSym, NulSym, While) ;
-   BackPatch(f, NextQuad)
+   tok := GetTokenNo () ;
+   tok := VAL (INTEGER, tok) + reltokpos ;
+   PopBool (t, f) ;
+   Assert (t=0) ;
+   PopT (While) ;
+   GenQuadO (tok, GotoOp, NulSym, NulSym, While, FALSE) ;
+   BackPatch (f, NextQuad)
 END BuildEndWhile ;
 
 
index d181f2381dfb5ca82052beb8b3e128acb68fc47b..4f6ffb772211d46862ded31861d49664eb72944c 100644 (file)
@@ -1299,7 +1299,7 @@ WhileStatement := "WHILE"                                                  % Bui
                                                                           % BuildStmtNote (0) %
                              "DO"                                          % BuildDoWhile %
                                   StatementSequence                        % BuildStmtNote (0) %
-                                                   "END"                  % DisplayStack ; BuildEndWhile %
+                                                   "END"                  % DisplayStack ; BuildEndWhile (-1) %
                 =:
 
 RepeatStatement := "REPEAT"
index 40ab96b151318fd7226655bf545123ba90495e6b..c4877d1b0416df3138548621095cc5e46c35a35f 100644 (file)
@@ -28,6 +28,7 @@ along with GNU Modula-2; see the file COPYING3.  If not see
 #include "m2options.h"
 #include "m2tree.h"
 #include "m2treelib.h"
+#include "m2pp.h"
 
 /* For each binding contour we allocate a binding_level structure
    which records the entities defined or declared in that contour.
@@ -667,8 +668,7 @@ m2block_GetErrorNode (void)
   return error_mark_node;
 }
 
-/* GetGlobals - returns a list of global variables, functions,
-   constants.  */
+/* GetGlobals returns a list of global variables, functions and constants.  */
 
 tree
 m2block_GetGlobals (void)
@@ -685,7 +685,7 @@ m2block_GetGlobalContext (void)
   return global_binding_level->context;
 }
 
-/* do_add_stmt t is a statement.  Add it to the statement-tree.  */
+/* do_add_stmt t is a statement.  Add it to the statement-tree.  */
 
 static tree
 do_add_stmt (tree t)
@@ -695,27 +695,14 @@ do_add_stmt (tree t)
   return t;
 }
 
-/* flush_pending_note - flushes a pending_statement note if
-   necessary.  */
+/* flush_pending_note flushes a pending_statement note if necessary.  */
 
 static void
 flush_pending_note (void)
 {
   if (pending_statement && (M2Options_GetM2g ()))
     {
-#if 0
-      /* --fixme-- we need a machine independant way to generate a nop.  */
-      tree instr = m2decl_BuildStringConstant ("nop", 3);
-      tree string
-          = resolve_asm_operand_names (instr, NULL_TREE, NULL_TREE, NULL_TREE);
-      tree note = build_stmt (pending_location, ASM_EXPR, string, NULL_TREE,
-                              NULL_TREE, NULL_TREE, NULL_TREE);
-
-      ASM_BASIC_P (note) = false;
-      ASM_VOLATILE_P (note) = false;
-#else
       tree note = build_empty_stmt (pending_location);
-#endif
       pending_statement = false;
       do_add_stmt (note);
     }
index d7f5a4187c8ffd329b1b977f8b7872ce11de7625..6ec8aaa88ea0f7f9bef8495a073cd3d8949a5f43 100644 (file)
@@ -2362,6 +2362,18 @@ m2pp_asm_expr (pretty *state, tree node)
   m2pp_print (state, ");\n");
 }
 
+/* m2pp_nop_expr display the nop_expr node.  */
+
+static void
+m2pp_nop_expr (pretty *state, tree t)
+{
+  enum tree_code code = TREE_CODE (t);
+  m2pp_begin (state);
+  m2pp_print (state, "(* NOP for debug location *)");
+  m2pp_needspace (state);
+  m2pp_loc (state, t);
+}
+  
 /* m2pp_statement attempts to reconstruct a statement.  */
 
 static void
@@ -2430,6 +2442,9 @@ m2pp_statement (pretty *s, tree t)
     case IF_STMT:
       m2pp_if_stmt (s, t);
       break;
+    case NOP_EXPR:
+      m2pp_nop_expr (s, t);
+      break;
     case ERROR_MARK:
       m2pp_print (s, "<ERROR CODE>\n");
       break;
diff --git a/gcc/testsuite/gm2/pim/pass/whilestep.mod b/gcc/testsuite/gm2/pim/pass/whilestep.mod
new file mode 100644 (file)
index 0000000..452bd3b
--- /dev/null
@@ -0,0 +1,18 @@
+MODULE whilestep ;  
+
+FROM libc IMPORT printf ;
+
+PROCEDURE init ;
+VAR
+   c: CARDINAL ;
+BEGIN
+   c := 0 ;
+   WHILE c < 20 DO
+      printf ("iteration %d\n", c) ;
+      INC (c)
+   END
+END init ;
+
+BEGIN
+   init
+END whilestep.