Fortran: Add gfc_simple_for_loop aux function

Message ID fc26c56a-7bc2-4692-f581-786b3d85eb2f@codesourcery.com
State New
Headers
Series Fortran: Add gfc_simple_for_loop aux function |

Commit Message

Tobias Burnus Sept. 16, 2021, 1:06 p.m. UTC
  This patch adds a useful auxiliary function to generate a loop.

I intent to use it for:
(A) An updated/cleaned-up version of
     "[Patch] Fortran: Fix Bind(C) Array-Descriptor Conversion (Move to Front-End Code)"
     https://gcc.gnu.org/pipermail/gcc-patches/2021-September/578904.html
     → new function used four times

(B) For the SIZE handling part of
       PR94070 - Assumed-rank arrays – bounds mishandled, SIZE/SHAPE/UBOUND/LBOUND
     which I am currently writing (→ used once)

The main reason of splitting this patch off is to permit to be able to submit/commit
the two patches separately without having a code overlap.

However, in principle there is no reason that this patch cannot be reviewed and/or
committed separately instead of in the same review and/or commit with one of the other
patches.

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Comments

Thomas Koenig Sept. 21, 2021, 12:38 p.m. UTC | #1
Hi Tobias,

> This patch adds a useful auxiliary function to generate a loop.

Looks good to me (and there are probably quite a few places where this
could be useful).

Best regards

	Thomas
  

Patch

Fortran: Add gfc_simple_for_loop aux function

Function to generate a simple loop (to be used internally).
Callers will be added in follow-up commits.

gcc/fortran/
	* trans-expr.c (gfc_simple_for_loop): New.
	* trans.h (gfc_simple_for_loop): New prototype.

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 18d665192f0..761f8c65234 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -11717,3 +11717,37 @@  gfc_trans_assign (gfc_code * code)
 {
   return gfc_trans_assignment (code->expr1, code->expr2, false, true);
 }
+
+/* Generate a simple loop for internal use of the form
+   for (var = begin; var <cond> end; var += step)
+      body;  */
+void
+gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
+		     enum tree_code cond, tree step, tree body)
+{
+  tree tmp;
+
+  /* var = begin. */
+  gfc_add_modify (block, var, begin);
+
+  /* Loop: for (var = begin; var <cond> end; var += step).  */
+  tree label_loop = gfc_build_label_decl (NULL_TREE);
+  tree label_cond = gfc_build_label_decl (NULL_TREE);
+  TREE_USED (label_loop) = 1;
+  TREE_USED (label_cond) = 1;
+
+  gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
+  gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
+
+  /* Loop body.  */
+  gfc_add_expr_to_block (block, body);
+
+  /* End of loop body.  */
+  tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
+  gfc_add_modify (block, var, tmp);
+  gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
+  tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
+  tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
+		  build_empty_stmt (input_location));
+  gfc_add_expr_to_block (block, tmp);
+}
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index 78578cfd732..1b622fc1f2e 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -518,6 +518,8 @@  tree gfc_string_to_single_character (tree len, tree str, int kind);
 tree gfc_get_tree_for_caf_expr (gfc_expr *);
 void gfc_get_caf_token_offset (gfc_se*, tree *, tree *, tree, tree, gfc_expr *);
 tree gfc_caf_get_image_index (stmtblock_t *, gfc_expr *, tree);
+void gfc_simple_for_loop (stmtblock_t *, tree, tree, tree, enum tree_code, tree,
+			  tree);
 
 /* Find the decl containing the auxiliary variables for assigned variables.  */