* [Patch] Fortran: Add gfc_simple_for_loop aux function
@ 2021-09-16 13:06 Tobias Burnus
2021-09-21 12:38 ` Thomas Koenig
0 siblings, 1 reply; 2+ messages in thread
From: Tobias Burnus @ 2021-09-16 13:06 UTC (permalink / raw)
To: gcc-patches, fortran
[-- Attachment #1: Type: text/plain, Size: 1121 bytes --]
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
[-- Attachment #2: gen-for-loop.diff --]
[-- Type: text/x-patch, Size: 2390 bytes --]
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. */
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-09-21 12:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-16 13:06 [Patch] Fortran: Add gfc_simple_for_loop aux function Tobias Burnus
2021-09-21 12:38 ` Thomas Koenig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).