diff options
author | Tobias Burnus <tobias@codesourcery.com> | 2021-09-22 11:11:00 +0200 |
---|---|---|
committer | Tobias Burnus <tobias@codesourcery.com> | 2021-09-22 11:11:00 +0200 |
commit | 424a4a463ac5932830a83560cf929f9c2f4564d8 (patch) | |
tree | 7d40ce9c14ff14897467857530b89a0b62f1ce35 | |
parent | b0321032658d875bee7584d8e3a8cbdd275c99d5 (diff) | |
download | gcc-424a4a463ac5932830a83560cf929f9c2f4564d8.zip gcc-424a4a463ac5932830a83560cf929f9c2f4564d8.tar.gz gcc-424a4a463ac5932830a83560cf929f9c2f4564d8.tar.bz2 |
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.
-rw-r--r-- | gcc/fortran/trans-expr.c | 34 | ||||
-rw-r--r-- | gcc/fortran/trans.h | 2 |
2 files changed, 36 insertions, 0 deletions
diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index 4a81f46..41d5452 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -11728,3 +11728,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 78578cf..4d29834 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. */ void gfc_conv_label_variable (gfc_se * se, gfc_expr * expr); |