1 /* Array translation routines
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 /* trans-array.c-- Various array related code, including scalarization,
25 allocation, initialization and other support routines. */
27 /* How the scalarizer works.
28 In gfortran, array expressions use the same core routines as scalar
30 First, a Scalarization State (SS) chain is built. This is done by walking
31 the expression tree, and building a linear list of the terms in the
32 expression. As the tree is walked, scalar subexpressions are translated.
34 The scalarization parameters are stored in a gfc_loopinfo structure.
35 First the start and stride of each term is calculated by
36 gfc_conv_ss_startstride. During this process the expressions for the array
37 descriptors and data pointers are also translated.
39 If the expression is an assignment, we must then resolve any dependencies.
40 In fortran all the rhs values of an assignment must be evaluated before
41 any assignments take place. This can require a temporary array to store the
42 values. We also require a temporary when we are passing array expressions
43 or vector subecripts as procedure parameters.
45 Array sections are passed without copying to a temporary. These use the
46 scalarizer to determine the shape of the section. The flag
47 loop->array_parameter tells the scalarizer that the actual values and loop
48 variables will not be required.
50 The function gfc_conv_loop_setup generates the scalarization setup code.
51 It determines the range of the scalarizing loop variables. If a temporary
52 is required, this is created and initialized. Code for scalar expressions
53 taken outside the loop is also generated at this time. Next the offset and
54 scaling required to translate from loop variables to array indices for each
57 A call to gfc_start_scalarized_body marks the start of the scalarized
58 expression. This creates a scope and declares the loop variables. Before
59 calling this gfc_make_ss_chain_used must be used to indicate which terms
60 will be used inside this loop.
62 The scalar gfc_conv_* functions are then used to build the main body of the
63 scalarization loop. Scalarization loop variables and precalculated scalar
64 values are automatically substituted. Note that gfc_advance_se_ss_chain
65 must be used, rather than changing the se->ss directly.
67 For assignment expressions requiring a temporary two sub loops are
68 generated. The first stores the result of the expression in the temporary,
69 the second copies it to the result. A call to
70 gfc_trans_scalarized_loop_boundary marks the end of the main loop code and
71 the start of the copying loop. The temporary may be less than full rank.
73 Finally gfc_trans_scalarizing_loops is called to generate the implicit do
74 loops. The loops are added to the pre chain of the loopinfo. The post
75 chain may still contain cleanup code.
77 After the loop code has been added into its parent scope gfc_cleanup_loop
78 is called to free all the SS allocated by the scalarizer. */
82 #include "coretypes.h"
84 #include "tree-gimple.h"
91 #include "trans-stmt.h"
92 #include "trans-types.h"
93 #include "trans-array.h"
94 #include "trans-const.h"
95 #include "dependency.h"
97 static gfc_ss *gfc_walk_subexpr (gfc_ss *, gfc_expr *);
98 static bool gfc_get_array_constructor_size (mpz_t *, gfc_constructor *);
100 /* The contents of this structure aren't actually used, just the address. */
101 static gfc_ss gfc_ss_terminator_var;
102 gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
106 gfc_array_dataptr_type (tree desc)
108 return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
112 /* Build expressions to access the members of an array descriptor.
113 It's surprisingly easy to mess up here, so never access
114 an array descriptor by "brute force", always use these
115 functions. This also avoids problems if we change the format
116 of an array descriptor.
118 To understand these magic numbers, look at the comments
119 before gfc_build_array_type() in trans-types.c.
121 The code within these defines should be the only code which knows the format
122 of an array descriptor.
124 Any code just needing to read obtain the bounds of an array should use
125 gfc_conv_array_* rather than the following functions as these will return
126 know constant values, and work with arrays which do not have descriptors.
128 Don't forget to #undef these! */
131 #define OFFSET_FIELD 1
132 #define DTYPE_FIELD 2
133 #define DIMENSION_FIELD 3
135 #define STRIDE_SUBFIELD 0
136 #define LBOUND_SUBFIELD 1
137 #define UBOUND_SUBFIELD 2
139 /* This provides READ-ONLY access to the data field. The field itself
140 doesn't have the proper type. */
143 gfc_conv_descriptor_data_get (tree desc)
147 type = TREE_TYPE (desc);
148 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
150 field = TYPE_FIELDS (type);
151 gcc_assert (DATA_FIELD == 0);
153 t = build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
154 t = fold_convert (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), t);
159 /* This provides WRITE access to the data field.
161 TUPLES_P is true if we are generating tuples.
163 This function gets called through the following macros:
164 gfc_conv_descriptor_data_set
165 gfc_conv_descriptor_data_set_tuples. */
168 gfc_conv_descriptor_data_set_internal (stmtblock_t *block,
169 tree desc, tree value,
174 type = TREE_TYPE (desc);
175 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
177 field = TYPE_FIELDS (type);
178 gcc_assert (DATA_FIELD == 0);
180 t = build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
181 gfc_add_modify (block, t, fold_convert (TREE_TYPE (field), value), tuples_p);
185 /* This provides address access to the data field. This should only be
186 used by array allocation, passing this on to the runtime. */
189 gfc_conv_descriptor_data_addr (tree desc)
193 type = TREE_TYPE (desc);
194 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
196 field = TYPE_FIELDS (type);
197 gcc_assert (DATA_FIELD == 0);
199 t = build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
200 return build_fold_addr_expr (t);
204 gfc_conv_descriptor_offset (tree desc)
209 type = TREE_TYPE (desc);
210 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
212 field = gfc_advance_chain (TYPE_FIELDS (type), OFFSET_FIELD);
213 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
215 return build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
219 gfc_conv_descriptor_dtype (tree desc)
224 type = TREE_TYPE (desc);
225 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
227 field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
228 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
230 return build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
234 gfc_conv_descriptor_dimension (tree desc, tree dim)
240 type = TREE_TYPE (desc);
241 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
243 field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
244 gcc_assert (field != NULL_TREE
245 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
246 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
248 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
249 tmp = gfc_build_array_ref (tmp, dim);
254 gfc_conv_descriptor_stride (tree desc, tree dim)
259 tmp = gfc_conv_descriptor_dimension (desc, dim);
260 field = TYPE_FIELDS (TREE_TYPE (tmp));
261 field = gfc_advance_chain (field, STRIDE_SUBFIELD);
262 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
264 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
269 gfc_conv_descriptor_lbound (tree desc, tree dim)
274 tmp = gfc_conv_descriptor_dimension (desc, dim);
275 field = TYPE_FIELDS (TREE_TYPE (tmp));
276 field = gfc_advance_chain (field, LBOUND_SUBFIELD);
277 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
279 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
284 gfc_conv_descriptor_ubound (tree desc, tree dim)
289 tmp = gfc_conv_descriptor_dimension (desc, dim);
290 field = TYPE_FIELDS (TREE_TYPE (tmp));
291 field = gfc_advance_chain (field, UBOUND_SUBFIELD);
292 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
294 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
299 /* Build a null array descriptor constructor. */
302 gfc_build_null_descriptor (tree type)
307 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
308 gcc_assert (DATA_FIELD == 0);
309 field = TYPE_FIELDS (type);
311 /* Set a NULL data pointer. */
312 tmp = build_constructor_single (type, field, null_pointer_node);
313 TREE_CONSTANT (tmp) = 1;
314 TREE_INVARIANT (tmp) = 1;
315 /* All other fields are ignored. */
321 /* Cleanup those #defines. */
326 #undef DIMENSION_FIELD
327 #undef STRIDE_SUBFIELD
328 #undef LBOUND_SUBFIELD
329 #undef UBOUND_SUBFIELD
332 /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
333 flags & 1 = Main loop body.
334 flags & 2 = temp copy loop. */
337 gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
339 for (; ss != gfc_ss_terminator; ss = ss->next)
340 ss->useflags = flags;
343 static void gfc_free_ss (gfc_ss *);
346 /* Free a gfc_ss chain. */
349 gfc_free_ss_chain (gfc_ss * ss)
353 while (ss != gfc_ss_terminator)
355 gcc_assert (ss != NULL);
366 gfc_free_ss (gfc_ss * ss)
373 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
375 if (ss->data.info.subscript[n])
376 gfc_free_ss_chain (ss->data.info.subscript[n]);
388 /* Free all the SS associated with a loop. */
391 gfc_cleanup_loop (gfc_loopinfo * loop)
397 while (ss != gfc_ss_terminator)
399 gcc_assert (ss != NULL);
400 next = ss->loop_chain;
407 /* Associate a SS chain with a loop. */
410 gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
414 if (head == gfc_ss_terminator)
418 for (; ss && ss != gfc_ss_terminator; ss = ss->next)
420 if (ss->next == gfc_ss_terminator)
421 ss->loop_chain = loop->ss;
423 ss->loop_chain = ss->next;
425 gcc_assert (ss == gfc_ss_terminator);
430 /* Generate an initializer for a static pointer or allocatable array. */
433 gfc_trans_static_array_pointer (gfc_symbol * sym)
437 gcc_assert (TREE_STATIC (sym->backend_decl));
438 /* Just zero the data member. */
439 type = TREE_TYPE (sym->backend_decl);
440 DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
444 /* If the bounds of SE's loop have not yet been set, see if they can be
445 determined from array spec AS, which is the array spec of a called
446 function. MAPPING maps the callee's dummy arguments to the values
447 that the caller is passing. Add any initialization and finalization
451 gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
452 gfc_se * se, gfc_array_spec * as)
460 if (as && as->type == AS_EXPLICIT)
461 for (dim = 0; dim < se->loop->dimen; dim++)
463 n = se->loop->order[dim];
464 if (se->loop->to[n] == NULL_TREE)
466 /* Evaluate the lower bound. */
467 gfc_init_se (&tmpse, NULL);
468 gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
469 gfc_add_block_to_block (&se->pre, &tmpse.pre);
470 gfc_add_block_to_block (&se->post, &tmpse.post);
473 /* ...and the upper bound. */
474 gfc_init_se (&tmpse, NULL);
475 gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
476 gfc_add_block_to_block (&se->pre, &tmpse.pre);
477 gfc_add_block_to_block (&se->post, &tmpse.post);
480 /* Set the upper bound of the loop to UPPER - LOWER. */
481 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
482 tmp = gfc_evaluate_now (tmp, &se->pre);
483 se->loop->to[n] = tmp;
489 /* Generate code to allocate an array temporary, or create a variable to
490 hold the data. If size is NULL, zero the descriptor so that the
491 callee will allocate the array. If DEALLOC is true, also generate code to
492 free the array afterwards.
494 Initialization code is added to PRE and finalization code to POST.
495 DYNAMIC is true if the caller may want to extend the array later
496 using realloc. This prevents us from putting the array on the stack. */
499 gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
500 gfc_ss_info * info, tree size, tree nelem,
501 bool dynamic, bool dealloc)
507 desc = info->descriptor;
508 info->offset = gfc_index_zero_node;
509 if (size == NULL_TREE || integer_zerop (size))
511 /* A callee allocated array. */
512 gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
517 /* Allocate the temporary. */
518 onstack = !dynamic && gfc_can_put_var_on_stack (size);
522 /* Make a temporary variable to hold the data. */
523 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (nelem), nelem,
525 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
527 tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
529 tmp = gfc_create_var (tmp, "A");
530 tmp = build_fold_addr_expr (tmp);
531 gfc_conv_descriptor_data_set (pre, desc, tmp);
535 /* Allocate memory to hold the data. */
536 if (gfc_index_integer_kind == 4)
537 tmp = gfor_fndecl_internal_malloc;
538 else if (gfc_index_integer_kind == 8)
539 tmp = gfor_fndecl_internal_malloc64;
542 tmp = build_call_expr (tmp, 1, size);
543 tmp = gfc_evaluate_now (tmp, pre);
544 gfc_conv_descriptor_data_set (pre, desc, tmp);
547 info->data = gfc_conv_descriptor_data_get (desc);
549 /* The offset is zero because we create temporaries with a zero
551 tmp = gfc_conv_descriptor_offset (desc);
552 gfc_add_modify_expr (pre, tmp, gfc_index_zero_node);
554 if (dealloc && !onstack)
556 /* Free the temporary. */
557 tmp = gfc_conv_descriptor_data_get (desc);
558 tmp = fold_convert (pvoid_type_node, tmp);
559 tmp = build_call_expr (gfor_fndecl_internal_free, 1, tmp);
560 gfc_add_expr_to_block (post, tmp);
565 /* Generate code to create and initialize the descriptor for a temporary
566 array. This is used for both temporaries needed by the scalarizer, and
567 functions returning arrays. Adjusts the loop variables to be
568 zero-based, and calculates the loop bounds for callee allocated arrays.
569 Allocate the array unless it's callee allocated (we have a callee
570 allocated array if 'callee_alloc' is true, or if loop->to[n] is
571 NULL_TREE for any n). Also fills in the descriptor, data and offset
572 fields of info if known. Returns the size of the array, or NULL for a
573 callee allocated array.
575 PRE, POST, DYNAMIC and DEALLOC are as for gfc_trans_allocate_array_storage.
579 gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post,
580 gfc_loopinfo * loop, gfc_ss_info * info,
581 tree eltype, bool dynamic, bool dealloc,
594 gcc_assert (info->dimen > 0);
595 /* Set the lower bound to zero. */
596 for (dim = 0; dim < info->dimen; dim++)
598 n = loop->order[dim];
599 if (n < loop->temp_dim)
600 gcc_assert (integer_zerop (loop->from[n]));
603 /* Callee allocated arrays may not have a known bound yet. */
605 loop->to[n] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
606 loop->to[n], loop->from[n]);
607 loop->from[n] = gfc_index_zero_node;
610 info->delta[dim] = gfc_index_zero_node;
611 info->start[dim] = gfc_index_zero_node;
612 info->end[dim] = gfc_index_zero_node;
613 info->stride[dim] = gfc_index_one_node;
614 info->dim[dim] = dim;
617 /* Initialize the descriptor. */
619 gfc_get_array_type_bounds (eltype, info->dimen, loop->from, loop->to, 1);
620 desc = gfc_create_var (type, "atmp");
621 GFC_DECL_PACKED_ARRAY (desc) = 1;
623 info->descriptor = desc;
624 size = gfc_index_one_node;
626 /* Fill in the array dtype. */
627 tmp = gfc_conv_descriptor_dtype (desc);
628 gfc_add_modify_expr (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
631 Fill in the bounds and stride. This is a packed array, so:
634 for (n = 0; n < rank; n++)
637 delta = ubound[n] + 1 - lbound[n];
640 size = size * sizeof(element);
645 for (n = 0; n < info->dimen; n++)
647 if (loop->to[n] == NULL_TREE)
649 /* For a callee allocated array express the loop bounds in terms
650 of the descriptor fields. */
651 tmp = build2 (MINUS_EXPR, gfc_array_index_type,
652 gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]),
653 gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]));
659 /* Store the stride and bound components in the descriptor. */
660 tmp = gfc_conv_descriptor_stride (desc, gfc_rank_cst[n]);
661 gfc_add_modify_expr (pre, tmp, size);
663 tmp = gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]);
664 gfc_add_modify_expr (pre, tmp, gfc_index_zero_node);
666 tmp = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]);
667 gfc_add_modify_expr (pre, tmp, loop->to[n]);
669 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
670 loop->to[n], gfc_index_one_node);
672 /* Check whether the size for this dimension is negative. */
673 cond = fold_build2 (LE_EXPR, boolean_type_node, tmp,
674 gfc_index_zero_node);
675 cond = gfc_evaluate_now (cond, pre);
680 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
682 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
683 size = gfc_evaluate_now (size, pre);
686 /* Get the size of the array. */
688 if (size && !callee_alloc)
690 /* If or_expr is true, then the extent in at least one
691 dimension is zero and the size is set to zero. */
692 size = fold_build3 (COND_EXPR, gfc_array_index_type,
693 or_expr, gfc_index_zero_node, size);
696 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
697 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
705 gfc_trans_allocate_array_storage (pre, post, info, size, nelem, dynamic,
708 if (info->dimen > loop->temp_dim)
709 loop->temp_dim = info->dimen;
715 /* Generate code to transpose array EXPR by creating a new descriptor
716 in which the dimension specifications have been reversed. */
719 gfc_conv_array_transpose (gfc_se * se, gfc_expr * expr)
721 tree dest, src, dest_index, src_index;
723 gfc_ss_info *dest_info, *src_info;
724 gfc_ss *dest_ss, *src_ss;
730 src_ss = gfc_walk_expr (expr);
733 src_info = &src_ss->data.info;
734 dest_info = &dest_ss->data.info;
735 gcc_assert (dest_info->dimen == 2);
736 gcc_assert (src_info->dimen == 2);
738 /* Get a descriptor for EXPR. */
739 gfc_init_se (&src_se, NULL);
740 gfc_conv_expr_descriptor (&src_se, expr, src_ss);
741 gfc_add_block_to_block (&se->pre, &src_se.pre);
742 gfc_add_block_to_block (&se->post, &src_se.post);
745 /* Allocate a new descriptor for the return value. */
746 dest = gfc_create_var (TREE_TYPE (src), "atmp");
747 dest_info->descriptor = dest;
750 /* Copy across the dtype field. */
751 gfc_add_modify_expr (&se->pre,
752 gfc_conv_descriptor_dtype (dest),
753 gfc_conv_descriptor_dtype (src));
755 /* Copy the dimension information, renumbering dimension 1 to 0 and
757 for (n = 0; n < 2; n++)
759 dest_info->delta[n] = gfc_index_zero_node;
760 dest_info->start[n] = gfc_index_zero_node;
761 dest_info->end[n] = gfc_index_zero_node;
762 dest_info->stride[n] = gfc_index_one_node;
763 dest_info->dim[n] = n;
765 dest_index = gfc_rank_cst[n];
766 src_index = gfc_rank_cst[1 - n];
768 gfc_add_modify_expr (&se->pre,
769 gfc_conv_descriptor_stride (dest, dest_index),
770 gfc_conv_descriptor_stride (src, src_index));
772 gfc_add_modify_expr (&se->pre,
773 gfc_conv_descriptor_lbound (dest, dest_index),
774 gfc_conv_descriptor_lbound (src, src_index));
776 gfc_add_modify_expr (&se->pre,
777 gfc_conv_descriptor_ubound (dest, dest_index),
778 gfc_conv_descriptor_ubound (src, src_index));
782 gcc_assert (integer_zerop (loop->from[n]));
783 loop->to[n] = build2 (MINUS_EXPR, gfc_array_index_type,
784 gfc_conv_descriptor_ubound (dest, dest_index),
785 gfc_conv_descriptor_lbound (dest, dest_index));
789 /* Copy the data pointer. */
790 dest_info->data = gfc_conv_descriptor_data_get (src);
791 gfc_conv_descriptor_data_set (&se->pre, dest, dest_info->data);
793 /* Copy the offset. This is not changed by transposition: the top-left
794 element is still at the same offset as before. */
795 dest_info->offset = gfc_conv_descriptor_offset (src);
796 gfc_add_modify_expr (&se->pre,
797 gfc_conv_descriptor_offset (dest),
800 if (dest_info->dimen > loop->temp_dim)
801 loop->temp_dim = dest_info->dimen;
805 /* Return the number of iterations in a loop that starts at START,
806 ends at END, and has step STEP. */
809 gfc_get_iteration_count (tree start, tree end, tree step)
814 type = TREE_TYPE (step);
815 tmp = fold_build2 (MINUS_EXPR, type, end, start);
816 tmp = fold_build2 (FLOOR_DIV_EXPR, type, tmp, step);
817 tmp = fold_build2 (PLUS_EXPR, type, tmp, build_int_cst (type, 1));
818 tmp = fold_build2 (MAX_EXPR, type, tmp, build_int_cst (type, 0));
819 return fold_convert (gfc_array_index_type, tmp);
823 /* Extend the data in array DESC by EXTRA elements. */
826 gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
833 if (integer_zerop (extra))
836 ubound = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[0]);
838 /* Add EXTRA to the upper bound. */
839 tmp = build2 (PLUS_EXPR, gfc_array_index_type, ubound, extra);
840 gfc_add_modify_expr (pblock, ubound, tmp);
842 /* Get the value of the current data pointer. */
843 arg0 = gfc_conv_descriptor_data_get (desc);
845 /* Calculate the new array size. */
846 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
847 tmp = build2 (PLUS_EXPR, gfc_array_index_type, ubound, gfc_index_one_node);
848 arg1 = build2 (MULT_EXPR, gfc_array_index_type, tmp, size);
850 /* Pick the appropriate realloc function. */
851 if (gfc_index_integer_kind == 4)
852 tmp = gfor_fndecl_internal_realloc;
853 else if (gfc_index_integer_kind == 8)
854 tmp = gfor_fndecl_internal_realloc64;
858 /* Set the new data pointer. */
859 tmp = build_call_expr (tmp, 2, arg0, arg1);
860 gfc_conv_descriptor_data_set (pblock, desc, tmp);
864 /* Return true if the bounds of iterator I can only be determined
868 gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
870 return (i->start->expr_type != EXPR_CONSTANT
871 || i->end->expr_type != EXPR_CONSTANT
872 || i->step->expr_type != EXPR_CONSTANT);
876 /* Split the size of constructor element EXPR into the sum of two terms,
877 one of which can be determined at compile time and one of which must
878 be calculated at run time. Set *SIZE to the former and return true
879 if the latter might be nonzero. */
882 gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
884 if (expr->expr_type == EXPR_ARRAY)
885 return gfc_get_array_constructor_size (size, expr->value.constructor);
886 else if (expr->rank > 0)
888 /* Calculate everything at run time. */
889 mpz_set_ui (*size, 0);
894 /* A single element. */
895 mpz_set_ui (*size, 1);
901 /* Like gfc_get_array_constructor_element_size, but applied to the whole
902 of array constructor C. */
905 gfc_get_array_constructor_size (mpz_t * size, gfc_constructor * c)
912 mpz_set_ui (*size, 0);
917 for (; c; c = c->next)
920 if (i && gfc_iterator_has_dynamic_bounds (i))
924 dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
927 /* Multiply the static part of the element size by the
928 number of iterations. */
929 mpz_sub (val, i->end->value.integer, i->start->value.integer);
930 mpz_fdiv_q (val, val, i->step->value.integer);
931 mpz_add_ui (val, val, 1);
932 if (mpz_sgn (val) > 0)
933 mpz_mul (len, len, val);
937 mpz_add (*size, *size, len);
946 /* Make sure offset is a variable. */
949 gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
952 /* We should have already created the offset variable. We cannot
953 create it here because we may be in an inner scope. */
954 gcc_assert (*offsetvar != NULL_TREE);
955 gfc_add_modify_expr (pblock, *offsetvar, *poffset);
956 *poffset = *offsetvar;
957 TREE_USED (*offsetvar) = 1;
961 /* Assign an element of an array constructor. */
964 gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
965 tree offset, gfc_se * se, gfc_expr * expr)
969 gfc_conv_expr (se, expr);
971 /* Store the value. */
972 tmp = build_fold_indirect_ref (gfc_conv_descriptor_data_get (desc));
973 tmp = gfc_build_array_ref (tmp, offset);
974 if (expr->ts.type == BT_CHARACTER)
976 gfc_conv_string_parameter (se);
977 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
979 /* The temporary is an array of pointers. */
980 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
981 gfc_add_modify_expr (&se->pre, tmp, se->expr);
985 /* The temporary is an array of string values. */
986 tmp = gfc_build_addr_expr (pchar_type_node, tmp);
987 /* We know the temporary and the value will be the same length,
988 so can use memcpy. */
989 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMCPY], 3,
990 tmp, se->expr, se->string_length);
991 gfc_add_expr_to_block (&se->pre, tmp);
996 /* TODO: Should the frontend already have done this conversion? */
997 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
998 gfc_add_modify_expr (&se->pre, tmp, se->expr);
1001 gfc_add_block_to_block (pblock, &se->pre);
1002 gfc_add_block_to_block (pblock, &se->post);
1006 /* Add the contents of an array to the constructor. DYNAMIC is as for
1007 gfc_trans_array_constructor_value. */
1010 gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
1011 tree type ATTRIBUTE_UNUSED,
1012 tree desc, gfc_expr * expr,
1013 tree * poffset, tree * offsetvar,
1024 /* We need this to be a variable so we can increment it. */
1025 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1027 gfc_init_se (&se, NULL);
1029 /* Walk the array expression. */
1030 ss = gfc_walk_expr (expr);
1031 gcc_assert (ss != gfc_ss_terminator);
1033 /* Initialize the scalarizer. */
1034 gfc_init_loopinfo (&loop);
1035 gfc_add_ss_to_loop (&loop, ss);
1037 /* Initialize the loop. */
1038 gfc_conv_ss_startstride (&loop);
1039 gfc_conv_loop_setup (&loop);
1041 /* Make sure the constructed array has room for the new data. */
1044 /* Set SIZE to the total number of elements in the subarray. */
1045 size = gfc_index_one_node;
1046 for (n = 0; n < loop.dimen; n++)
1048 tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
1049 gfc_index_one_node);
1050 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1053 /* Grow the constructed array by SIZE elements. */
1054 gfc_grow_array (&loop.pre, desc, size);
1057 /* Make the loop body. */
1058 gfc_mark_ss_chain_used (ss, 1);
1059 gfc_start_scalarized_body (&loop, &body);
1060 gfc_copy_loopinfo_to_se (&se, &loop);
1063 gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
1064 gcc_assert (se.ss == gfc_ss_terminator);
1066 /* Increment the offset. */
1067 tmp = build2 (PLUS_EXPR, gfc_array_index_type, *poffset, gfc_index_one_node);
1068 gfc_add_modify_expr (&body, *poffset, tmp);
1070 /* Finish the loop. */
1071 gfc_trans_scalarizing_loops (&loop, &body);
1072 gfc_add_block_to_block (&loop.pre, &loop.post);
1073 tmp = gfc_finish_block (&loop.pre);
1074 gfc_add_expr_to_block (pblock, tmp);
1076 gfc_cleanup_loop (&loop);
1080 /* Assign the values to the elements of an array constructor. DYNAMIC
1081 is true if descriptor DESC only contains enough data for the static
1082 size calculated by gfc_get_array_constructor_size. When true, memory
1083 for the dynamic parts must be allocated using realloc. */
1086 gfc_trans_array_constructor_value (stmtblock_t * pblock, tree type,
1087 tree desc, gfc_constructor * c,
1088 tree * poffset, tree * offsetvar,
1097 for (; c; c = c->next)
1099 /* If this is an iterator or an array, the offset must be a variable. */
1100 if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
1101 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1103 gfc_start_block (&body);
1105 if (c->expr->expr_type == EXPR_ARRAY)
1107 /* Array constructors can be nested. */
1108 gfc_trans_array_constructor_value (&body, type, desc,
1109 c->expr->value.constructor,
1110 poffset, offsetvar, dynamic);
1112 else if (c->expr->rank > 0)
1114 gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
1115 poffset, offsetvar, dynamic);
1119 /* This code really upsets the gimplifier so don't bother for now. */
1126 while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
1133 /* Scalar values. */
1134 gfc_init_se (&se, NULL);
1135 gfc_trans_array_ctor_element (&body, desc, *poffset,
1138 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1139 *poffset, gfc_index_one_node);
1143 /* Collect multiple scalar constants into a constructor. */
1151 /* Count the number of consecutive scalar constants. */
1152 while (p && !(p->iterator
1153 || p->expr->expr_type != EXPR_CONSTANT))
1155 gfc_init_se (&se, NULL);
1156 gfc_conv_constant (&se, p->expr);
1157 if (p->expr->ts.type == BT_CHARACTER
1158 && POINTER_TYPE_P (type))
1160 /* For constant character array constructors we build
1161 an array of pointers. */
1162 se.expr = gfc_build_addr_expr (pchar_type_node,
1166 list = tree_cons (NULL_TREE, se.expr, list);
1171 bound = build_int_cst (NULL_TREE, n - 1);
1172 /* Create an array type to hold them. */
1173 tmptype = build_range_type (gfc_array_index_type,
1174 gfc_index_zero_node, bound);
1175 tmptype = build_array_type (type, tmptype);
1177 init = build_constructor_from_list (tmptype, nreverse (list));
1178 TREE_CONSTANT (init) = 1;
1179 TREE_INVARIANT (init) = 1;
1180 TREE_STATIC (init) = 1;
1181 /* Create a static variable to hold the data. */
1182 tmp = gfc_create_var (tmptype, "data");
1183 TREE_STATIC (tmp) = 1;
1184 TREE_CONSTANT (tmp) = 1;
1185 TREE_INVARIANT (tmp) = 1;
1186 TREE_READONLY (tmp) = 1;
1187 DECL_INITIAL (tmp) = init;
1190 /* Use BUILTIN_MEMCPY to assign the values. */
1191 tmp = gfc_conv_descriptor_data_get (desc);
1192 tmp = build_fold_indirect_ref (tmp);
1193 tmp = gfc_build_array_ref (tmp, *poffset);
1194 tmp = build_fold_addr_expr (tmp);
1195 init = build_fold_addr_expr (init);
1197 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
1198 bound = build_int_cst (NULL_TREE, n * size);
1199 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMCPY], 3,
1201 gfc_add_expr_to_block (&body, tmp);
1203 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1204 *poffset, build_int_cst (NULL_TREE, n));
1206 if (!INTEGER_CST_P (*poffset))
1208 gfc_add_modify_expr (&body, *offsetvar, *poffset);
1209 *poffset = *offsetvar;
1213 /* The frontend should already have done any expansions possible
1217 /* Pass the code as is. */
1218 tmp = gfc_finish_block (&body);
1219 gfc_add_expr_to_block (pblock, tmp);
1223 /* Build the implied do-loop. */
1233 loopbody = gfc_finish_block (&body);
1235 gfc_init_se (&se, NULL);
1236 gfc_conv_expr (&se, c->iterator->var);
1237 gfc_add_block_to_block (pblock, &se.pre);
1240 /* Make a temporary, store the current value in that
1241 and return it, once the loop is done. */
1242 tmp_loopvar = gfc_create_var (TREE_TYPE (loopvar), "loopvar");
1243 gfc_add_modify_expr (pblock, tmp_loopvar, loopvar);
1245 /* Initialize the loop. */
1246 gfc_init_se (&se, NULL);
1247 gfc_conv_expr_val (&se, c->iterator->start);
1248 gfc_add_block_to_block (pblock, &se.pre);
1249 gfc_add_modify_expr (pblock, loopvar, se.expr);
1251 gfc_init_se (&se, NULL);
1252 gfc_conv_expr_val (&se, c->iterator->end);
1253 gfc_add_block_to_block (pblock, &se.pre);
1254 end = gfc_evaluate_now (se.expr, pblock);
1256 gfc_init_se (&se, NULL);
1257 gfc_conv_expr_val (&se, c->iterator->step);
1258 gfc_add_block_to_block (pblock, &se.pre);
1259 step = gfc_evaluate_now (se.expr, pblock);
1261 /* If this array expands dynamically, and the number of iterations
1262 is not constant, we won't have allocated space for the static
1263 part of C->EXPR's size. Do that now. */
1264 if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
1266 /* Get the number of iterations. */
1267 tmp = gfc_get_iteration_count (loopvar, end, step);
1269 /* Get the static part of C->EXPR's size. */
1270 gfc_get_array_constructor_element_size (&size, c->expr);
1271 tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1273 /* Grow the array by TMP * TMP2 elements. */
1274 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, tmp2);
1275 gfc_grow_array (pblock, desc, tmp);
1278 /* Generate the loop body. */
1279 exit_label = gfc_build_label_decl (NULL_TREE);
1280 gfc_start_block (&body);
1282 /* Generate the exit condition. Depending on the sign of
1283 the step variable we have to generate the correct
1285 tmp = fold_build2 (GT_EXPR, boolean_type_node, step,
1286 build_int_cst (TREE_TYPE (step), 0));
1287 cond = fold_build3 (COND_EXPR, boolean_type_node, tmp,
1288 build2 (GT_EXPR, boolean_type_node,
1290 build2 (LT_EXPR, boolean_type_node,
1292 tmp = build1_v (GOTO_EXPR, exit_label);
1293 TREE_USED (exit_label) = 1;
1294 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
1295 gfc_add_expr_to_block (&body, tmp);
1297 /* The main loop body. */
1298 gfc_add_expr_to_block (&body, loopbody);
1300 /* Increase loop variable by step. */
1301 tmp = build2 (PLUS_EXPR, TREE_TYPE (loopvar), loopvar, step);
1302 gfc_add_modify_expr (&body, loopvar, tmp);
1304 /* Finish the loop. */
1305 tmp = gfc_finish_block (&body);
1306 tmp = build1_v (LOOP_EXPR, tmp);
1307 gfc_add_expr_to_block (pblock, tmp);
1309 /* Add the exit label. */
1310 tmp = build1_v (LABEL_EXPR, exit_label);
1311 gfc_add_expr_to_block (pblock, tmp);
1313 /* Restore the original value of the loop counter. */
1314 gfc_add_modify_expr (pblock, loopvar, tmp_loopvar);
1321 /* Figure out the string length of a variable reference expression.
1322 Used by get_array_ctor_strlen. */
1325 get_array_ctor_var_strlen (gfc_expr * expr, tree * len)
1331 /* Don't bother if we already know the length is a constant. */
1332 if (*len && INTEGER_CST_P (*len))
1335 ts = &expr->symtree->n.sym->ts;
1336 for (ref = expr->ref; ref; ref = ref->next)
1341 /* Array references don't change the string length. */
1345 /* Use the length of the component. */
1346 ts = &ref->u.c.component->ts;
1350 if (ref->u.ss.start->expr_type != EXPR_CONSTANT
1351 || ref->u.ss.start->expr_type != EXPR_CONSTANT)
1353 mpz_init_set_ui (char_len, 1);
1354 mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
1355 mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
1356 *len = gfc_conv_mpz_to_tree (char_len,
1357 gfc_default_character_kind);
1358 *len = convert (gfc_charlen_type_node, *len);
1359 mpz_clear (char_len);
1363 /* TODO: Substrings are tricky because we can't evaluate the
1364 expression more than once. For now we just give up, and hope
1365 we can figure it out elsewhere. */
1370 *len = ts->cl->backend_decl;
1374 /* Figure out the string length of a character array constructor.
1375 Returns TRUE if all elements are character constants. */
1378 get_array_ctor_strlen (gfc_constructor * c, tree * len)
1383 for (; c; c = c->next)
1385 switch (c->expr->expr_type)
1388 if (!(*len && INTEGER_CST_P (*len)))
1389 *len = build_int_cstu (gfc_charlen_type_node,
1390 c->expr->value.character.length);
1394 if (!get_array_ctor_strlen (c->expr->value.constructor, len))
1400 get_array_ctor_var_strlen (c->expr, len);
1406 /* Hope that whatever we have possesses a constant character
1408 if (!(*len && INTEGER_CST_P (*len)) && c->expr->ts.cl)
1410 gfc_conv_const_charlen (c->expr->ts.cl);
1411 *len = c->expr->ts.cl->backend_decl;
1413 /* TODO: For now we just ignore anything we don't know how to
1414 handle, and hope we can figure it out a different way. */
1422 /* Check whether the array constructor C consists entirely of constant
1423 elements, and if so returns the number of those elements, otherwise
1424 return zero. Note, an empty or NULL array constructor returns zero. */
1426 unsigned HOST_WIDE_INT
1427 gfc_constant_array_constructor_p (gfc_constructor * c)
1429 unsigned HOST_WIDE_INT nelem = 0;
1434 || c->expr->rank > 0
1435 || c->expr->expr_type != EXPR_CONSTANT)
1444 /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
1445 and the tree type of it's elements, TYPE, return a static constant
1446 variable that is compile-time initialized. */
1449 gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
1451 tree tmptype, list, init, tmp;
1452 HOST_WIDE_INT nelem;
1458 /* First traverse the constructor list, converting the constants
1459 to tree to build an initializer. */
1462 c = expr->value.constructor;
1465 gfc_init_se (&se, NULL);
1466 gfc_conv_constant (&se, c->expr);
1467 if (c->expr->ts.type == BT_CHARACTER
1468 && POINTER_TYPE_P (type))
1469 se.expr = gfc_build_addr_expr (pchar_type_node, se.expr);
1470 list = tree_cons (NULL_TREE, se.expr, list);
1475 /* Next determine the tree type for the array. We use the gfortran
1476 front-end's gfc_get_nodesc_array_type in order to create a suitable
1477 GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
1479 memset (&as, 0, sizeof (gfc_array_spec));
1481 as.rank = expr->rank;
1482 as.type = AS_EXPLICIT;
1485 as.lower[0] = gfc_int_expr (0);
1486 as.upper[0] = gfc_int_expr (nelem - 1);
1489 for (i = 0; i < expr->rank; i++)
1491 int tmp = (int) mpz_get_si (expr->shape[i]);
1492 as.lower[i] = gfc_int_expr (0);
1493 as.upper[i] = gfc_int_expr (tmp - 1);
1496 tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC);
1498 init = build_constructor_from_list (tmptype, nreverse (list));
1500 TREE_CONSTANT (init) = 1;
1501 TREE_INVARIANT (init) = 1;
1502 TREE_STATIC (init) = 1;
1504 tmp = gfc_create_var (tmptype, "A");
1505 TREE_STATIC (tmp) = 1;
1506 TREE_CONSTANT (tmp) = 1;
1507 TREE_INVARIANT (tmp) = 1;
1508 TREE_READONLY (tmp) = 1;
1509 DECL_INITIAL (tmp) = init;
1515 /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
1516 This mostly initializes the scalarizer state info structure with the
1517 appropriate values to directly use the array created by the function
1518 gfc_build_constant_array_constructor. */
1521 gfc_trans_constant_array_constructor (gfc_loopinfo * loop,
1522 gfc_ss * ss, tree type)
1528 tmp = gfc_build_constant_array_constructor (ss->expr, type);
1530 info = &ss->data.info;
1532 info->descriptor = tmp;
1533 info->data = build_fold_addr_expr (tmp);
1534 info->offset = fold_build1 (NEGATE_EXPR, gfc_array_index_type,
1537 for (i = 0; i < info->dimen; i++)
1539 info->delta[i] = gfc_index_zero_node;
1540 info->start[i] = gfc_index_zero_node;
1541 info->end[i] = gfc_index_zero_node;
1542 info->stride[i] = gfc_index_one_node;
1546 if (info->dimen > loop->temp_dim)
1547 loop->temp_dim = info->dimen;
1550 /* Helper routine of gfc_trans_array_constructor to determine if the
1551 bounds of the loop specified by LOOP are constant and simple enough
1552 to use with gfc_trans_constant_array_constructor. Returns the
1553 the iteration count of the loop if suitable, and NULL_TREE otherwise. */
1556 constant_array_constructor_loop_size (gfc_loopinfo * loop)
1558 tree size = gfc_index_one_node;
1562 for (i = 0; i < loop->dimen; i++)
1564 /* If the bounds aren't constant, return NULL_TREE. */
1565 if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
1567 if (!integer_zerop (loop->from[i]))
1569 /* Only allow non-zero "from" in one-dimensional arrays. */
1570 if (loop->dimen != 1)
1572 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1573 loop->to[i], loop->from[i]);
1577 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1578 tmp, gfc_index_one_node);
1579 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1586 /* Array constructors are handled by constructing a temporary, then using that
1587 within the scalarization loop. This is not optimal, but seems by far the
1591 gfc_trans_array_constructor (gfc_loopinfo * loop, gfc_ss * ss)
1600 ss->data.info.dimen = loop->dimen;
1602 c = ss->expr->value.constructor;
1603 if (ss->expr->ts.type == BT_CHARACTER)
1605 bool const_string = get_array_ctor_strlen (c, &ss->string_length);
1606 if (!ss->string_length)
1607 gfc_todo_error ("complex character array constructors");
1609 type = gfc_get_character_type_len (ss->expr->ts.kind, ss->string_length);
1611 type = build_pointer_type (type);
1614 type = gfc_typenode_for_spec (&ss->expr->ts);
1616 /* See if the constructor determines the loop bounds. */
1618 if (loop->to[0] == NULL_TREE)
1622 /* We should have a 1-dimensional, zero-based loop. */
1623 gcc_assert (loop->dimen == 1);
1624 gcc_assert (integer_zerop (loop->from[0]));
1626 /* Split the constructor size into a static part and a dynamic part.
1627 Allocate the static size up-front and record whether the dynamic
1628 size might be nonzero. */
1630 dynamic = gfc_get_array_constructor_size (&size, c);
1631 mpz_sub_ui (size, size, 1);
1632 loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1636 /* Special case constant array constructors. */
1639 unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
1642 tree size = constant_array_constructor_loop_size (loop);
1643 if (size && compare_tree_int (size, nelem) == 0)
1645 gfc_trans_constant_array_constructor (loop, ss, type);
1651 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop, &ss->data.info,
1652 type, dynamic, true, false);
1654 desc = ss->data.info.descriptor;
1655 offset = gfc_index_zero_node;
1656 offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
1657 TREE_USED (offsetvar) = 0;
1658 gfc_trans_array_constructor_value (&loop->pre, type, desc, c,
1659 &offset, &offsetvar, dynamic);
1661 /* If the array grows dynamically, the upper bound of the loop variable
1662 is determined by the array's final upper bound. */
1664 loop->to[0] = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[0]);
1666 if (TREE_USED (offsetvar))
1667 pushdecl (offsetvar);
1669 gcc_assert (INTEGER_CST_P (offset));
1671 /* Disable bound checking for now because it's probably broken. */
1672 if (flag_bounds_check)
1680 /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
1681 called after evaluating all of INFO's vector dimensions. Go through
1682 each such vector dimension and see if we can now fill in any missing
1686 gfc_set_vector_loop_bounds (gfc_loopinfo * loop, gfc_ss_info * info)
1695 for (n = 0; n < loop->dimen; n++)
1698 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR
1699 && loop->to[n] == NULL)
1701 /* Loop variable N indexes vector dimension DIM, and we don't
1702 yet know the upper bound of loop variable N. Set it to the
1703 difference between the vector's upper and lower bounds. */
1704 gcc_assert (loop->from[n] == gfc_index_zero_node);
1705 gcc_assert (info->subscript[dim]
1706 && info->subscript[dim]->type == GFC_SS_VECTOR);
1708 gfc_init_se (&se, NULL);
1709 desc = info->subscript[dim]->data.info.descriptor;
1710 zero = gfc_rank_cst[0];
1711 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1712 gfc_conv_descriptor_ubound (desc, zero),
1713 gfc_conv_descriptor_lbound (desc, zero));
1714 tmp = gfc_evaluate_now (tmp, &loop->pre);
1721 /* Add the pre and post chains for all the scalar expressions in a SS chain
1722 to loop. This is called after the loop parameters have been calculated,
1723 but before the actual scalarizing loops. */
1726 gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript)
1731 /* TODO: This can generate bad code if there are ordering dependencies.
1732 eg. a callee allocated function and an unknown size constructor. */
1733 gcc_assert (ss != NULL);
1735 for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
1742 /* Scalar expression. Evaluate this now. This includes elemental
1743 dimension indices, but not array section bounds. */
1744 gfc_init_se (&se, NULL);
1745 gfc_conv_expr (&se, ss->expr);
1746 gfc_add_block_to_block (&loop->pre, &se.pre);
1748 if (ss->expr->ts.type != BT_CHARACTER)
1750 /* Move the evaluation of scalar expressions outside the
1751 scalarization loop. */
1753 se.expr = convert(gfc_array_index_type, se.expr);
1754 se.expr = gfc_evaluate_now (se.expr, &loop->pre);
1755 gfc_add_block_to_block (&loop->pre, &se.post);
1758 gfc_add_block_to_block (&loop->post, &se.post);
1760 ss->data.scalar.expr = se.expr;
1761 ss->string_length = se.string_length;
1764 case GFC_SS_REFERENCE:
1765 /* Scalar reference. Evaluate this now. */
1766 gfc_init_se (&se, NULL);
1767 gfc_conv_expr_reference (&se, ss->expr);
1768 gfc_add_block_to_block (&loop->pre, &se.pre);
1769 gfc_add_block_to_block (&loop->post, &se.post);
1771 ss->data.scalar.expr = gfc_evaluate_now (se.expr, &loop->pre);
1772 ss->string_length = se.string_length;
1775 case GFC_SS_SECTION:
1776 /* Add the expressions for scalar and vector subscripts. */
1777 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1778 if (ss->data.info.subscript[n])
1779 gfc_add_loop_ss_code (loop, ss->data.info.subscript[n], true);
1781 gfc_set_vector_loop_bounds (loop, &ss->data.info);
1785 /* Get the vector's descriptor and store it in SS. */
1786 gfc_init_se (&se, NULL);
1787 gfc_conv_expr_descriptor (&se, ss->expr, gfc_walk_expr (ss->expr));
1788 gfc_add_block_to_block (&loop->pre, &se.pre);
1789 gfc_add_block_to_block (&loop->post, &se.post);
1790 ss->data.info.descriptor = se.expr;
1793 case GFC_SS_INTRINSIC:
1794 gfc_add_intrinsic_ss_code (loop, ss);
1797 case GFC_SS_FUNCTION:
1798 /* Array function return value. We call the function and save its
1799 result in a temporary for use inside the loop. */
1800 gfc_init_se (&se, NULL);
1803 gfc_conv_expr (&se, ss->expr);
1804 gfc_add_block_to_block (&loop->pre, &se.pre);
1805 gfc_add_block_to_block (&loop->post, &se.post);
1806 ss->string_length = se.string_length;
1809 case GFC_SS_CONSTRUCTOR:
1810 gfc_trans_array_constructor (loop, ss);
1814 case GFC_SS_COMPONENT:
1815 /* Do nothing. These are handled elsewhere. */
1825 /* Translate expressions for the descriptor and data pointer of a SS. */
1829 gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
1834 /* Get the descriptor for the array to be scalarized. */
1835 gcc_assert (ss->expr->expr_type == EXPR_VARIABLE);
1836 gfc_init_se (&se, NULL);
1837 se.descriptor_only = 1;
1838 gfc_conv_expr_lhs (&se, ss->expr);
1839 gfc_add_block_to_block (block, &se.pre);
1840 ss->data.info.descriptor = se.expr;
1841 ss->string_length = se.string_length;
1845 /* Also the data pointer. */
1846 tmp = gfc_conv_array_data (se.expr);
1847 /* If this is a variable or address of a variable we use it directly.
1848 Otherwise we must evaluate it now to avoid breaking dependency
1849 analysis by pulling the expressions for elemental array indices
1852 || (TREE_CODE (tmp) == ADDR_EXPR
1853 && DECL_P (TREE_OPERAND (tmp, 0)))))
1854 tmp = gfc_evaluate_now (tmp, block);
1855 ss->data.info.data = tmp;
1857 tmp = gfc_conv_array_offset (se.expr);
1858 ss->data.info.offset = gfc_evaluate_now (tmp, block);
1863 /* Initialize a gfc_loopinfo structure. */
1866 gfc_init_loopinfo (gfc_loopinfo * loop)
1870 memset (loop, 0, sizeof (gfc_loopinfo));
1871 gfc_init_block (&loop->pre);
1872 gfc_init_block (&loop->post);
1874 /* Initially scalarize in order. */
1875 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1878 loop->ss = gfc_ss_terminator;
1882 /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
1886 gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
1892 /* Return an expression for the data pointer of an array. */
1895 gfc_conv_array_data (tree descriptor)
1899 type = TREE_TYPE (descriptor);
1900 if (GFC_ARRAY_TYPE_P (type))
1902 if (TREE_CODE (type) == POINTER_TYPE)
1906 /* Descriptorless arrays. */
1907 return build_fold_addr_expr (descriptor);
1911 return gfc_conv_descriptor_data_get (descriptor);
1915 /* Return an expression for the base offset of an array. */
1918 gfc_conv_array_offset (tree descriptor)
1922 type = TREE_TYPE (descriptor);
1923 if (GFC_ARRAY_TYPE_P (type))
1924 return GFC_TYPE_ARRAY_OFFSET (type);
1926 return gfc_conv_descriptor_offset (descriptor);
1930 /* Get an expression for the array stride. */
1933 gfc_conv_array_stride (tree descriptor, int dim)
1938 type = TREE_TYPE (descriptor);
1940 /* For descriptorless arrays use the array size. */
1941 tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
1942 if (tmp != NULL_TREE)
1945 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[dim]);
1950 /* Like gfc_conv_array_stride, but for the lower bound. */
1953 gfc_conv_array_lbound (tree descriptor, int dim)
1958 type = TREE_TYPE (descriptor);
1960 tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
1961 if (tmp != NULL_TREE)
1964 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[dim]);
1969 /* Like gfc_conv_array_stride, but for the upper bound. */
1972 gfc_conv_array_ubound (tree descriptor, int dim)
1977 type = TREE_TYPE (descriptor);
1979 tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
1980 if (tmp != NULL_TREE)
1983 /* This should only ever happen when passing an assumed shape array
1984 as an actual parameter. The value will never be used. */
1985 if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
1986 return gfc_index_zero_node;
1988 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[dim]);
1993 /* Generate code to perform an array index bound check. */
1996 gfc_trans_array_bound_check (gfc_se * se, tree descriptor, tree index, int n,
2002 const char * name = NULL;
2004 if (!flag_bounds_check)
2007 index = gfc_evaluate_now (index, &se->pre);
2009 /* We find a name for the error message. */
2011 name = se->ss->expr->symtree->name;
2013 if (!name && se->loop && se->loop->ss && se->loop->ss->expr
2014 && se->loop->ss->expr->symtree)
2015 name = se->loop->ss->expr->symtree->name;
2017 if (!name && se->loop && se->loop->ss && se->loop->ss->loop_chain
2018 && se->loop->ss->loop_chain->expr
2019 && se->loop->ss->loop_chain->expr->symtree)
2020 name = se->loop->ss->loop_chain->expr->symtree->name;
2022 if (!name && se->loop && se->loop->ss && se->loop->ss->loop_chain
2023 && se->loop->ss->loop_chain->expr->symtree)
2024 name = se->loop->ss->loop_chain->expr->symtree->name;
2026 if (!name && se->loop && se->loop->ss && se->loop->ss->expr)
2028 if (se->loop->ss->expr->expr_type == EXPR_FUNCTION
2029 && se->loop->ss->expr->value.function.name)
2030 name = se->loop->ss->expr->value.function.name;
2032 if (se->loop->ss->type == GFC_SS_CONSTRUCTOR
2033 || se->loop->ss->type == GFC_SS_SCALAR)
2034 name = "unnamed constant";
2037 /* Check lower bound. */
2038 tmp = gfc_conv_array_lbound (descriptor, n);
2039 fault = fold_build2 (LT_EXPR, boolean_type_node, index, tmp);
2041 asprintf (&msg, "%s for array '%s', lower bound of dimension %d exceeded",
2042 gfc_msg_fault, name, n+1);
2044 asprintf (&msg, "%s, lower bound of dimension %d exceeded",
2045 gfc_msg_fault, n+1);
2046 gfc_trans_runtime_check (fault, msg, &se->pre, where);
2049 /* Check upper bound. */
2050 tmp = gfc_conv_array_ubound (descriptor, n);
2051 fault = fold_build2 (GT_EXPR, boolean_type_node, index, tmp);
2053 asprintf (&msg, "%s for array '%s', upper bound of dimension %d exceeded",
2054 gfc_msg_fault, name, n+1);
2056 asprintf (&msg, "%s, upper bound of dimension %d exceeded",
2057 gfc_msg_fault, n+1);
2058 gfc_trans_runtime_check (fault, msg, &se->pre, where);
2065 /* Return the offset for an index. Performs bound checking for elemental
2066 dimensions. Single element references are processed separately. */
2069 gfc_conv_array_index_offset (gfc_se * se, gfc_ss_info * info, int dim, int i,
2070 gfc_array_ref * ar, tree stride)
2076 /* Get the index into the array for this dimension. */
2079 gcc_assert (ar->type != AR_ELEMENT);
2080 switch (ar->dimen_type[dim])
2083 gcc_assert (i == -1);
2084 /* Elemental dimension. */
2085 gcc_assert (info->subscript[dim]
2086 && info->subscript[dim]->type == GFC_SS_SCALAR);
2087 /* We've already translated this value outside the loop. */
2088 index = info->subscript[dim]->data.scalar.expr;
2090 if ((ar->as->type != AS_ASSUMED_SIZE && !ar->as->cp_was_assumed)
2091 || dim < ar->dimen - 1)
2092 index = gfc_trans_array_bound_check (se, info->descriptor,
2093 index, dim, &ar->where);
2097 gcc_assert (info && se->loop);
2098 gcc_assert (info->subscript[dim]
2099 && info->subscript[dim]->type == GFC_SS_VECTOR);
2100 desc = info->subscript[dim]->data.info.descriptor;
2102 /* Get a zero-based index into the vector. */
2103 index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2104 se->loop->loopvar[i], se->loop->from[i]);
2106 /* Multiply the index by the stride. */
2107 index = fold_build2 (MULT_EXPR, gfc_array_index_type,
2108 index, gfc_conv_array_stride (desc, 0));
2110 /* Read the vector to get an index into info->descriptor. */
2111 data = build_fold_indirect_ref (gfc_conv_array_data (desc));
2112 index = gfc_build_array_ref (data, index);
2113 index = gfc_evaluate_now (index, &se->pre);
2115 /* Do any bounds checking on the final info->descriptor index. */
2116 if ((ar->as->type != AS_ASSUMED_SIZE && !ar->as->cp_was_assumed)
2117 || dim < ar->dimen - 1)
2118 index = gfc_trans_array_bound_check (se, info->descriptor,
2119 index, dim, &ar->where);
2123 /* Scalarized dimension. */
2124 gcc_assert (info && se->loop);
2126 /* Multiply the loop variable by the stride and delta. */
2127 index = se->loop->loopvar[i];
2128 if (!integer_onep (info->stride[i]))
2129 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index,
2131 if (!integer_zerop (info->delta[i]))
2132 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index,
2142 /* Temporary array or derived type component. */
2143 gcc_assert (se->loop);
2144 index = se->loop->loopvar[se->loop->order[i]];
2145 if (!integer_zerop (info->delta[i]))
2146 index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2147 index, info->delta[i]);
2150 /* Multiply by the stride. */
2151 if (!integer_onep (stride))
2152 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index, stride);
2158 /* Build a scalarized reference to an array. */
2161 gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar)
2168 info = &se->ss->data.info;
2170 n = se->loop->order[0];
2174 index = gfc_conv_array_index_offset (se, info, info->dim[n], n, ar,
2176 /* Add the offset for this dimension to the stored offset for all other
2178 if (!integer_zerop (info->offset))
2179 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, info->offset);
2181 tmp = build_fold_indirect_ref (info->data);
2182 se->expr = gfc_build_array_ref (tmp, index);
2186 /* Translate access of temporary array. */
2189 gfc_conv_tmp_array_ref (gfc_se * se)
2191 se->string_length = se->ss->string_length;
2192 gfc_conv_scalarized_array_ref (se, NULL);
2196 /* Build an array reference. se->expr already holds the array descriptor.
2197 This should be either a variable, indirect variable reference or component
2198 reference. For arrays which do not have a descriptor, se->expr will be
2200 a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
2203 gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_symbol * sym,
2212 /* Handle scalarized references separately. */
2213 if (ar->type != AR_ELEMENT)
2215 gfc_conv_scalarized_array_ref (se, ar);
2216 gfc_advance_se_ss_chain (se);
2220 index = gfc_index_zero_node;
2222 /* Calculate the offsets from all the dimensions. */
2223 for (n = 0; n < ar->dimen; n++)
2225 /* Calculate the index for this dimension. */
2226 gfc_init_se (&indexse, se);
2227 gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
2228 gfc_add_block_to_block (&se->pre, &indexse.pre);
2230 if (flag_bounds_check &&
2231 ((ar->as->type != AS_ASSUMED_SIZE && !ar->as->cp_was_assumed)
2232 || n < ar->dimen - 1))
2234 /* Check array bounds. */
2238 tmp = gfc_conv_array_lbound (se->expr, n);
2239 cond = fold_build2 (LT_EXPR, boolean_type_node,
2241 asprintf (&msg, "%s for array '%s', "
2242 "lower bound of dimension %d exceeded", gfc_msg_fault,
2244 gfc_trans_runtime_check (cond, msg, &se->pre, where);
2247 tmp = gfc_conv_array_ubound (se->expr, n);
2248 cond = fold_build2 (GT_EXPR, boolean_type_node,
2250 asprintf (&msg, "%s for array '%s', "
2251 "upper bound of dimension %d exceeded", gfc_msg_fault,
2253 gfc_trans_runtime_check (cond, msg, &se->pre, where);
2257 /* Multiply the index by the stride. */
2258 stride = gfc_conv_array_stride (se->expr, n);
2259 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, indexse.expr,
2262 /* And add it to the total. */
2263 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
2266 tmp = gfc_conv_array_offset (se->expr);
2267 if (!integer_zerop (tmp))
2268 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
2270 /* Access the calculated element. */
2271 tmp = gfc_conv_array_data (se->expr);
2272 tmp = build_fold_indirect_ref (tmp);
2273 se->expr = gfc_build_array_ref (tmp, index);
2277 /* Generate the code to be executed immediately before entering a
2278 scalarization loop. */
2281 gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
2282 stmtblock_t * pblock)
2291 /* This code will be executed before entering the scalarization loop
2292 for this dimension. */
2293 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2295 if ((ss->useflags & flag) == 0)
2298 if (ss->type != GFC_SS_SECTION
2299 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2300 && ss->type != GFC_SS_COMPONENT)
2303 info = &ss->data.info;
2305 if (dim >= info->dimen)
2308 if (dim == info->dimen - 1)
2310 /* For the outermost loop calculate the offset due to any
2311 elemental dimensions. It will have been initialized with the
2312 base offset of the array. */
2315 for (i = 0; i < info->ref->u.ar.dimen; i++)
2317 if (info->ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2320 gfc_init_se (&se, NULL);
2322 se.expr = info->descriptor;
2323 stride = gfc_conv_array_stride (info->descriptor, i);
2324 index = gfc_conv_array_index_offset (&se, info, i, -1,
2327 gfc_add_block_to_block (pblock, &se.pre);
2329 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2330 info->offset, index);
2331 info->offset = gfc_evaluate_now (info->offset, pblock);
2335 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2338 stride = gfc_conv_array_stride (info->descriptor, 0);
2340 /* Calculate the stride of the innermost loop. Hopefully this will
2341 allow the backend optimizers to do their stuff more effectively.
2343 info->stride0 = gfc_evaluate_now (stride, pblock);
2347 /* Add the offset for the previous loop dimension. */
2352 ar = &info->ref->u.ar;
2353 i = loop->order[dim + 1];
2361 gfc_init_se (&se, NULL);
2363 se.expr = info->descriptor;
2364 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2365 index = gfc_conv_array_index_offset (&se, info, info->dim[i], i,
2367 gfc_add_block_to_block (pblock, &se.pre);
2368 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2369 info->offset, index);
2370 info->offset = gfc_evaluate_now (info->offset, pblock);
2373 /* Remember this offset for the second loop. */
2374 if (dim == loop->temp_dim - 1)
2375 info->saved_offset = info->offset;
2380 /* Start a scalarized expression. Creates a scope and declares loop
2384 gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
2390 gcc_assert (!loop->array_parameter);
2392 for (dim = loop->dimen - 1; dim >= 0; dim--)
2394 n = loop->order[dim];
2396 gfc_start_block (&loop->code[n]);
2398 /* Create the loop variable. */
2399 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
2401 if (dim < loop->temp_dim)
2405 /* Calculate values that will be constant within this loop. */
2406 gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
2408 gfc_start_block (pbody);
2412 /* Generates the actual loop code for a scalarization loop. */
2415 gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
2416 stmtblock_t * pbody)
2424 loopbody = gfc_finish_block (pbody);
2426 /* Initialize the loopvar. */
2427 gfc_add_modify_expr (&loop->code[n], loop->loopvar[n], loop->from[n]);
2429 exit_label = gfc_build_label_decl (NULL_TREE);
2431 /* Generate the loop body. */
2432 gfc_init_block (&block);
2434 /* The exit condition. */
2435 cond = build2 (GT_EXPR, boolean_type_node, loop->loopvar[n], loop->to[n]);
2436 tmp = build1_v (GOTO_EXPR, exit_label);
2437 TREE_USED (exit_label) = 1;
2438 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
2439 gfc_add_expr_to_block (&block, tmp);
2441 /* The main body. */
2442 gfc_add_expr_to_block (&block, loopbody);
2444 /* Increment the loopvar. */
2445 tmp = build2 (PLUS_EXPR, gfc_array_index_type,
2446 loop->loopvar[n], gfc_index_one_node);
2447 gfc_add_modify_expr (&block, loop->loopvar[n], tmp);
2449 /* Build the loop. */
2450 tmp = gfc_finish_block (&block);
2451 tmp = build1_v (LOOP_EXPR, tmp);
2452 gfc_add_expr_to_block (&loop->code[n], tmp);
2454 /* Add the exit label. */
2455 tmp = build1_v (LABEL_EXPR, exit_label);
2456 gfc_add_expr_to_block (&loop->code[n], tmp);
2460 /* Finishes and generates the loops for a scalarized expression. */
2463 gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
2468 stmtblock_t *pblock;
2472 /* Generate the loops. */
2473 for (dim = 0; dim < loop->dimen; dim++)
2475 n = loop->order[dim];
2476 gfc_trans_scalarized_loop_end (loop, n, pblock);
2477 loop->loopvar[n] = NULL_TREE;
2478 pblock = &loop->code[n];
2481 tmp = gfc_finish_block (pblock);
2482 gfc_add_expr_to_block (&loop->pre, tmp);
2484 /* Clear all the used flags. */
2485 for (ss = loop->ss; ss; ss = ss->loop_chain)
2490 /* Finish the main body of a scalarized expression, and start the secondary
2494 gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
2498 stmtblock_t *pblock;
2502 /* We finish as many loops as are used by the temporary. */
2503 for (dim = 0; dim < loop->temp_dim - 1; dim++)
2505 n = loop->order[dim];
2506 gfc_trans_scalarized_loop_end (loop, n, pblock);
2507 loop->loopvar[n] = NULL_TREE;
2508 pblock = &loop->code[n];
2511 /* We don't want to finish the outermost loop entirely. */
2512 n = loop->order[loop->temp_dim - 1];
2513 gfc_trans_scalarized_loop_end (loop, n, pblock);
2515 /* Restore the initial offsets. */
2516 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2518 if ((ss->useflags & 2) == 0)
2521 if (ss->type != GFC_SS_SECTION
2522 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2523 && ss->type != GFC_SS_COMPONENT)
2526 ss->data.info.offset = ss->data.info.saved_offset;
2529 /* Restart all the inner loops we just finished. */
2530 for (dim = loop->temp_dim - 2; dim >= 0; dim--)
2532 n = loop->order[dim];
2534 gfc_start_block (&loop->code[n]);
2536 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
2538 gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
2541 /* Start a block for the secondary copying code. */
2542 gfc_start_block (body);
2546 /* Calculate the upper bound of an array section. */
2549 gfc_conv_section_upper_bound (gfc_ss * ss, int n, stmtblock_t * pblock)
2558 gcc_assert (ss->type == GFC_SS_SECTION);
2560 info = &ss->data.info;
2563 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2564 /* We'll calculate the upper bound once we have access to the
2565 vector's descriptor. */
2568 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2569 desc = info->descriptor;
2570 end = info->ref->u.ar.end[dim];
2574 /* The upper bound was specified. */
2575 gfc_init_se (&se, NULL);
2576 gfc_conv_expr_type (&se, end, gfc_array_index_type);
2577 gfc_add_block_to_block (pblock, &se.pre);
2582 /* No upper bound was specified, so use the bound of the array. */
2583 bound = gfc_conv_array_ubound (desc, dim);
2590 /* Calculate the lower bound of an array section. */
2593 gfc_conv_section_startstride (gfc_loopinfo * loop, gfc_ss * ss, int n)
2603 gcc_assert (ss->type == GFC_SS_SECTION);
2605 info = &ss->data.info;
2608 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2610 /* We use a zero-based index to access the vector. */
2611 info->start[n] = gfc_index_zero_node;
2612 info->end[n] = gfc_index_zero_node;
2613 info->stride[n] = gfc_index_one_node;
2617 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2618 desc = info->descriptor;
2619 start = info->ref->u.ar.start[dim];
2620 end = info->ref->u.ar.end[dim];
2621 stride = info->ref->u.ar.stride[dim];
2623 /* Calculate the start of the range. For vector subscripts this will
2624 be the range of the vector. */
2627 /* Specified section start. */
2628 gfc_init_se (&se, NULL);
2629 gfc_conv_expr_type (&se, start, gfc_array_index_type);
2630 gfc_add_block_to_block (&loop->pre, &se.pre);
2631 info->start[n] = se.expr;
2635 /* No lower bound specified so use the bound of the array. */
2636 info->start[n] = gfc_conv_array_lbound (desc, dim);
2638 info->start[n] = gfc_evaluate_now (info->start[n], &loop->pre);
2640 /* Similarly calculate the end. Although this is not used in the
2641 scalarizer, it is needed when checking bounds and where the end
2642 is an expression with side-effects. */
2645 /* Specified section start. */
2646 gfc_init_se (&se, NULL);
2647 gfc_conv_expr_type (&se, end, gfc_array_index_type);
2648 gfc_add_block_to_block (&loop->pre, &se.pre);
2649 info->end[n] = se.expr;
2653 /* No upper bound specified so use the bound of the array. */
2654 info->end[n] = gfc_conv_array_ubound (desc, dim);
2656 info->end[n] = gfc_evaluate_now (info->end[n], &loop->pre);
2658 /* Calculate the stride. */
2660 info->stride[n] = gfc_index_one_node;
2663 gfc_init_se (&se, NULL);
2664 gfc_conv_expr_type (&se, stride, gfc_array_index_type);
2665 gfc_add_block_to_block (&loop->pre, &se.pre);
2666 info->stride[n] = gfc_evaluate_now (se.expr, &loop->pre);
2671 /* Calculates the range start and stride for a SS chain. Also gets the
2672 descriptor and data pointer. The range of vector subscripts is the size
2673 of the vector. Array bounds are also checked. */
2676 gfc_conv_ss_startstride (gfc_loopinfo * loop)
2684 /* Determine the rank of the loop. */
2686 ss != gfc_ss_terminator && loop->dimen == 0; ss = ss->loop_chain)
2690 case GFC_SS_SECTION:
2691 case GFC_SS_CONSTRUCTOR:
2692 case GFC_SS_FUNCTION:
2693 case GFC_SS_COMPONENT:
2694 loop->dimen = ss->data.info.dimen;
2697 /* As usual, lbound and ubound are exceptions!. */
2698 case GFC_SS_INTRINSIC:
2699 switch (ss->expr->value.function.isym->generic_id)
2701 case GFC_ISYM_LBOUND:
2702 case GFC_ISYM_UBOUND:
2703 loop->dimen = ss->data.info.dimen;
2714 if (loop->dimen == 0)
2715 gfc_todo_error ("Unable to determine rank of expression");
2718 /* Loop over all the SS in the chain. */
2719 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2721 if (ss->expr && ss->expr->shape && !ss->shape)
2722 ss->shape = ss->expr->shape;
2726 case GFC_SS_SECTION:
2727 /* Get the descriptor for the array. */
2728 gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
2730 for (n = 0; n < ss->data.info.dimen; n++)
2731 gfc_conv_section_startstride (loop, ss, n);
2734 case GFC_SS_INTRINSIC:
2735 switch (ss->expr->value.function.isym->generic_id)
2737 /* Fall through to supply start and stride. */
2738 case GFC_ISYM_LBOUND:
2739 case GFC_ISYM_UBOUND:
2745 case GFC_SS_CONSTRUCTOR:
2746 case GFC_SS_FUNCTION:
2747 for (n = 0; n < ss->data.info.dimen; n++)
2749 ss->data.info.start[n] = gfc_index_zero_node;
2750 ss->data.info.end[n] = gfc_index_zero_node;
2751 ss->data.info.stride[n] = gfc_index_one_node;
2760 /* The rest is just runtime bound checking. */
2761 if (flag_bounds_check)
2764 tree lbound, ubound;
2766 tree size[GFC_MAX_DIMENSIONS];
2767 tree stride_pos, stride_neg, non_zerosized, tmp2;
2772 gfc_start_block (&block);
2774 for (n = 0; n < loop->dimen; n++)
2775 size[n] = NULL_TREE;
2777 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2779 if (ss->type != GFC_SS_SECTION)
2782 /* TODO: range checking for mapped dimensions. */
2783 info = &ss->data.info;
2785 /* This code only checks ranges. Elemental and vector
2786 dimensions are checked later. */
2787 for (n = 0; n < loop->dimen; n++)
2790 if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
2792 if (n == info->ref->u.ar.dimen - 1
2793 && (info->ref->u.ar.as->type == AS_ASSUMED_SIZE
2794 || info->ref->u.ar.as->cp_was_assumed))
2797 desc = ss->data.info.descriptor;
2799 /* This is the run-time equivalent of resolve.c's
2800 check_dimension(). The logical is more readable there
2801 than it is here, with all the trees. */
2802 lbound = gfc_conv_array_lbound (desc, dim);
2803 ubound = gfc_conv_array_ubound (desc, dim);
2806 /* Zero stride is not allowed. */
2807 tmp = fold_build2 (EQ_EXPR, boolean_type_node, info->stride[n],
2808 gfc_index_zero_node);
2809 asprintf (&msg, "Zero stride is not allowed, for dimension %d "
2810 "of array '%s'", info->dim[n]+1,
2811 ss->expr->symtree->name);
2812 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2815 /* non_zerosized is true when the selected range is not
2817 stride_pos = fold_build2 (GT_EXPR, boolean_type_node,
2818 info->stride[n], gfc_index_zero_node);
2819 tmp = fold_build2 (LE_EXPR, boolean_type_node, info->start[n],
2821 stride_pos = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2824 stride_neg = fold_build2 (LT_EXPR, boolean_type_node,
2825 info->stride[n], gfc_index_zero_node);
2826 tmp = fold_build2 (GE_EXPR, boolean_type_node, info->start[n],
2828 stride_neg = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2830 non_zerosized = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
2831 stride_pos, stride_neg);
2833 /* Check the start of the range against the lower and upper
2834 bounds of the array, if the range is not empty. */
2835 tmp = fold_build2 (LT_EXPR, boolean_type_node, info->start[n],
2837 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2838 non_zerosized, tmp);
2839 asprintf (&msg, "%s, lower bound of dimension %d of array '%s'"
2840 " exceeded", gfc_msg_fault, info->dim[n]+1,
2841 ss->expr->symtree->name);
2842 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2845 tmp = fold_build2 (GT_EXPR, boolean_type_node, info->start[n],
2847 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2848 non_zerosized, tmp);
2849 asprintf (&msg, "%s, upper bound of dimension %d of array '%s'"
2850 " exceeded", gfc_msg_fault, info->dim[n]+1,
2851 ss->expr->symtree->name);
2852 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2855 /* Compute the last element of the range, which is not
2856 necessarily "end" (think 0:5:3, which doesn't contain 5)
2857 and check it against both lower and upper bounds. */
2858 tmp2 = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
2860 tmp2 = fold_build2 (TRUNC_MOD_EXPR, gfc_array_index_type, tmp2,
2862 tmp2 = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
2865 tmp = fold_build2 (LT_EXPR, boolean_type_node, tmp2, lbound);
2866 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2867 non_zerosized, tmp);
2868 asprintf (&msg, "%s, lower bound of dimension %d of array '%s'"
2869 " exceeded", gfc_msg_fault, info->dim[n]+1,
2870 ss->expr->symtree->name);
2871 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2874 tmp = fold_build2 (GT_EXPR, boolean_type_node, tmp2, ubound);
2875 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2876 non_zerosized, tmp);
2877 asprintf (&msg, "%s, upper bound of dimension %d of array '%s'"
2878 " exceeded", gfc_msg_fault, info->dim[n]+1,
2879 ss->expr->symtree->name);
2880 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2883 /* Check the section sizes match. */
2884 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
2886 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type, tmp,
2888 /* We remember the size of the first section, and check all the
2889 others against this. */
2893 fold_build2 (NE_EXPR, boolean_type_node, tmp, size[n]);
2894 asprintf (&msg, "%s, size mismatch for dimension %d "
2895 "of array '%s'", gfc_msg_bounds, info->dim[n]+1,
2896 ss->expr->symtree->name);
2897 gfc_trans_runtime_check (tmp, msg, &block, &ss->expr->where);
2901 size[n] = gfc_evaluate_now (tmp, &block);
2905 tmp = gfc_finish_block (&block);
2906 gfc_add_expr_to_block (&loop->pre, tmp);
2911 /* Return true if the two SS could be aliased, i.e. both point to the same data
2913 /* TODO: resolve aliases based on frontend expressions. */
2916 gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
2923 lsym = lss->expr->symtree->n.sym;
2924 rsym = rss->expr->symtree->n.sym;
2925 if (gfc_symbols_could_alias (lsym, rsym))
2928 if (rsym->ts.type != BT_DERIVED
2929 && lsym->ts.type != BT_DERIVED)
2932 /* For derived types we must check all the component types. We can ignore
2933 array references as these will have the same base type as the previous
2935 for (lref = lss->expr->ref; lref != lss->data.info.ref; lref = lref->next)
2937 if (lref->type != REF_COMPONENT)
2940 if (gfc_symbols_could_alias (lref->u.c.sym, rsym))
2943 for (rref = rss->expr->ref; rref != rss->data.info.ref;
2946 if (rref->type != REF_COMPONENT)
2949 if (gfc_symbols_could_alias (lref->u.c.sym, rref->u.c.sym))
2954 for (rref = rss->expr->ref; rref != rss->data.info.ref; rref = rref->next)
2956 if (rref->type != REF_COMPONENT)
2959 if (gfc_symbols_could_alias (rref->u.c.sym, lsym))
2967 /* Resolve array data dependencies. Creates a temporary if required. */
2968 /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
2972 gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
2982 loop->temp_ss = NULL;
2983 aref = dest->data.info.ref;
2986 for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
2988 if (ss->type != GFC_SS_SECTION)
2991 if (gfc_could_be_alias (dest, ss)
2992 || gfc_are_equivalenced_arrays (dest->expr, ss->expr))
2998 if (dest->expr->symtree->n.sym == ss->expr->symtree->n.sym)
3000 lref = dest->expr->ref;
3001 rref = ss->expr->ref;
3003 nDepend = gfc_dep_resolver (lref, rref);
3007 /* TODO : loop shifting. */
3010 /* Mark the dimensions for LOOP SHIFTING */
3011 for (n = 0; n < loop->dimen; n++)
3013 int dim = dest->data.info.dim[n];
3015 if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
3017 else if (! gfc_is_same_range (&lref->u.ar,
3018 &rref->u.ar, dim, 0))
3022 /* Put all the dimensions with dependencies in the
3025 for (n = 0; n < loop->dimen; n++)
3027 gcc_assert (loop->order[n] == n);
3029 loop->order[dim++] = n;
3032 for (n = 0; n < loop->dimen; n++)
3035 loop->order[dim++] = n;
3038 gcc_assert (dim == loop->dimen);
3047 tree base_type = gfc_typenode_for_spec (&dest->expr->ts);
3048 if (GFC_ARRAY_TYPE_P (base_type)
3049 || GFC_DESCRIPTOR_TYPE_P (base_type))
3050 base_type = gfc_get_element_type (base_type);
3051 loop->temp_ss = gfc_get_ss ();
3052 loop->temp_ss->type = GFC_SS_TEMP;
3053 loop->temp_ss->data.temp.type = base_type;
3054 loop->temp_ss->string_length = dest->string_length;
3055 loop->temp_ss->data.temp.dimen = loop->dimen;
3056 loop->temp_ss->next = gfc_ss_terminator;
3057 gfc_add_ss_to_loop (loop, loop->temp_ss);
3060 loop->temp_ss = NULL;
3064 /* Initialize the scalarization loop. Creates the loop variables. Determines
3065 the range of the loop variables. Creates a temporary if required.
3066 Calculates how to transform from loop variables to array indices for each
3067 expression. Also generates code for scalar expressions which have been
3068 moved outside the loop. */
3071 gfc_conv_loop_setup (gfc_loopinfo * loop)
3076 gfc_ss_info *specinfo;
3080 gfc_ss *loopspec[GFC_MAX_DIMENSIONS];
3081 bool dynamic[GFC_MAX_DIMENSIONS];
3087 for (n = 0; n < loop->dimen; n++)
3091 /* We use one SS term, and use that to determine the bounds of the
3092 loop for this dimension. We try to pick the simplest term. */
3093 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3097 /* The frontend has worked out the size for us. */
3102 if (ss->type == GFC_SS_CONSTRUCTOR)
3104 /* An unknown size constructor will always be rank one.
3105 Higher rank constructors will either have known shape,
3106 or still be wrapped in a call to reshape. */
3107 gcc_assert (loop->dimen == 1);
3109 /* Always prefer to use the constructor bounds if the size
3110 can be determined at compile time. Prefer not to otherwise,
3111 since the general case involves realloc, and it's better to
3112 avoid that overhead if possible. */
3113 c = ss->expr->value.constructor;
3114 dynamic[n] = gfc_get_array_constructor_size (&i, c);
3115 if (!dynamic[n] || !loopspec[n])
3120 /* TODO: Pick the best bound if we have a choice between a
3121 function and something else. */
3122 if (ss->type == GFC_SS_FUNCTION)
3128 if (ss->type != GFC_SS_SECTION)
3132 specinfo = &loopspec[n]->data.info;
3135 info = &ss->data.info;
3139 /* Criteria for choosing a loop specifier (most important first):
3140 doesn't need realloc
3146 else if (loopspec[n]->type == GFC_SS_CONSTRUCTOR && dynamic[n])
3148 else if (integer_onep (info->stride[n])
3149 && !integer_onep (specinfo->stride[n]))
3151 else if (INTEGER_CST_P (info->stride[n])
3152 && !INTEGER_CST_P (specinfo->stride[n]))
3154 else if (INTEGER_CST_P (info->start[n])
3155 && !INTEGER_CST_P (specinfo->start[n]))
3157 /* We don't work out the upper bound.
3158 else if (INTEGER_CST_P (info->finish[n])
3159 && ! INTEGER_CST_P (specinfo->finish[n]))
3160 loopspec[n] = ss; */
3164 gfc_todo_error ("Unable to find scalarization loop specifier");
3166 info = &loopspec[n]->data.info;
3168 /* Set the extents of this range. */
3169 cshape = loopspec[n]->shape;
3170 if (cshape && INTEGER_CST_P (info->start[n])
3171 && INTEGER_CST_P (info->stride[n]))
3173 loop->from[n] = info->start[n];
3174 mpz_set (i, cshape[n]);
3175 mpz_sub_ui (i, i, 1);
3176 /* To = from + (size - 1) * stride. */
3177 tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
3178 if (!integer_onep (info->stride[n]))
3179 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3180 tmp, info->stride[n]);
3181 loop->to[n] = fold_build2 (PLUS_EXPR, gfc_array_index_type,
3182 loop->from[n], tmp);
3186 loop->from[n] = info->start[n];
3187 switch (loopspec[n]->type)
3189 case GFC_SS_CONSTRUCTOR:
3190 /* The upper bound is calculated when we expand the
3192 gcc_assert (loop->to[n] == NULL_TREE);
3195 case GFC_SS_SECTION:
3196 loop->to[n] = gfc_conv_section_upper_bound (loopspec[n], n,
3200 case GFC_SS_FUNCTION:
3201 /* The loop bound will be set when we generate the call. */
3202 gcc_assert (loop->to[n] == NULL_TREE);
3210 /* Transform everything so we have a simple incrementing variable. */
3211 if (integer_onep (info->stride[n]))
3212 info->delta[n] = gfc_index_zero_node;
3215 /* Set the delta for this section. */
3216 info->delta[n] = gfc_evaluate_now (loop->from[n], &loop->pre);
3217 /* Number of iterations is (end - start + step) / step.
3218 with start = 0, this simplifies to
3220 for (i = 0; i<=last; i++){...}; */
3221 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3222 loop->to[n], loop->from[n]);
3223 tmp = fold_build2 (TRUNC_DIV_EXPR, gfc_array_index_type,
3224 tmp, info->stride[n]);
3225 loop->to[n] = gfc_evaluate_now (tmp, &loop->pre);
3226 /* Make the loop variable start at 0. */
3227 loop->from[n] = gfc_index_zero_node;
3231 /* Add all the scalar code that can be taken out of the loops.
3232 This may include calculating the loop bounds, so do it before
3233 allocating the temporary. */
3234 gfc_add_loop_ss_code (loop, loop->ss, false);
3236 /* If we want a temporary then create it. */
3237 if (loop->temp_ss != NULL)
3239 gcc_assert (loop->temp_ss->type == GFC_SS_TEMP);
3240 tmp = loop->temp_ss->data.temp.type;
3241 len = loop->temp_ss->string_length;
3242 n = loop->temp_ss->data.temp.dimen;
3243 memset (&loop->temp_ss->data.info, 0, sizeof (gfc_ss_info));
3244 loop->temp_ss->type = GFC_SS_SECTION;
3245 loop->temp_ss->data.info.dimen = n;
3246 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop,
3247 &loop->temp_ss->data.info, tmp, false, true,
3251 for (n = 0; n < loop->temp_dim; n++)
3252 loopspec[loop->order[n]] = NULL;
3256 /* For array parameters we don't have loop variables, so don't calculate the
3258 if (loop->array_parameter)
3261 /* Calculate the translation from loop variables to array indices. */
3262 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3264 if (ss->type != GFC_SS_SECTION && ss->type != GFC_SS_COMPONENT)
3267 info = &ss->data.info;
3269 for (n = 0; n < info->dimen; n++)
3273 /* If we are specifying the range the delta is already set. */
3274 if (loopspec[n] != ss)
3276 /* Calculate the offset relative to the loop variable.
3277 First multiply by the stride. */
3278 tmp = loop->from[n];
3279 if (!integer_onep (info->stride[n]))
3280 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3281 tmp, info->stride[n]);
3283 /* Then subtract this from our starting value. */
3284 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3285 info->start[n], tmp);
3287 info->delta[n] = gfc_evaluate_now (tmp, &loop->pre);
3294 /* Fills in an array descriptor, and returns the size of the array. The size
3295 will be a simple_val, ie a variable or a constant. Also calculates the
3296 offset of the base. Returns the size of the array.
3300 for (n = 0; n < rank; n++)
3302 a.lbound[n] = specified_lower_bound;
3303 offset = offset + a.lbond[n] * stride;
3305 a.ubound[n] = specified_upper_bound;
3306 a.stride[n] = stride;
3307 size = ubound + size; //size = ubound + 1 - lbound
3308 stride = stride * size;
3315 gfc_array_init_size (tree descriptor, int rank, tree * poffset,
3316 gfc_expr ** lower, gfc_expr ** upper,
3317 stmtblock_t * pblock)
3329 stmtblock_t thenblock;
3330 stmtblock_t elseblock;
3335 type = TREE_TYPE (descriptor);
3337 stride = gfc_index_one_node;
3338 offset = gfc_index_zero_node;
3340 /* Set the dtype. */
3341 tmp = gfc_conv_descriptor_dtype (descriptor);
3342 gfc_add_modify_expr (pblock, tmp, gfc_get_dtype (TREE_TYPE (descriptor)));
3344 or_expr = NULL_TREE;
3346 for (n = 0; n < rank; n++)
3348 /* We have 3 possibilities for determining the size of the array:
3349 lower == NULL => lbound = 1, ubound = upper[n]
3350 upper[n] = NULL => lbound = 1, ubound = lower[n]
3351 upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
3354 /* Set lower bound. */
3355 gfc_init_se (&se, NULL);
3357 se.expr = gfc_index_one_node;
3360 gcc_assert (lower[n]);
3363 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
3364 gfc_add_block_to_block (pblock, &se.pre);
3368 se.expr = gfc_index_one_node;
3372 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[n]);
3373 gfc_add_modify_expr (pblock, tmp, se.expr);
3375 /* Work out the offset for this component. */
3376 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, se.expr, stride);
3377 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
3379 /* Start the calculation for the size of this dimension. */
3380 size = build2 (MINUS_EXPR, gfc_array_index_type,
3381 gfc_index_one_node, se.expr);
3383 /* Set upper bound. */
3384 gfc_init_se (&se, NULL);
3385 gcc_assert (ubound);
3386 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
3387 gfc_add_block_to_block (pblock, &se.pre);
3389 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[n]);
3390 gfc_add_modify_expr (pblock, tmp, se.expr);
3392 /* Store the stride. */
3393 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[n]);
3394 gfc_add_modify_expr (pblock, tmp, stride);
3396 /* Calculate the size of this dimension. */
3397 size = fold_build2 (PLUS_EXPR, gfc_array_index_type, se.expr, size);
3399 /* Check whether the size for this dimension is negative. */
3400 cond = fold_build2 (LE_EXPR, boolean_type_node, size,
3401 gfc_index_zero_node);
3405 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
3407 /* Multiply the stride by the number of elements in this dimension. */
3408 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, size);
3409 stride = gfc_evaluate_now (stride, pblock);
3412 /* The stride is the number of elements in the array, so multiply by the
3413 size of an element to get the total size. */
3414 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3415 size = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, tmp);
3417 if (poffset != NULL)
3419 offset = gfc_evaluate_now (offset, pblock);
3423 if (integer_zerop (or_expr))
3425 if (integer_onep (or_expr))
3426 return gfc_index_zero_node;
3428 var = gfc_create_var (TREE_TYPE (size), "size");
3429 gfc_start_block (&thenblock);
3430 gfc_add_modify_expr (&thenblock, var, gfc_index_zero_node);
3431 thencase = gfc_finish_block (&thenblock);
3433 gfc_start_block (&elseblock);
3434 gfc_add_modify_expr (&elseblock, var, size);
3435 elsecase = gfc_finish_block (&elseblock);
3437 tmp = gfc_evaluate_now (or_expr, pblock);
3438 tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
3439 gfc_add_expr_to_block (pblock, tmp);
3445 /* Initializes the descriptor and generates a call to _gfor_allocate. Does
3446 the work for an ALLOCATE statement. */
3450 gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree pstat)
3459 gfc_ref *ref, *prev_ref = NULL;
3460 bool allocatable_array;
3464 /* Find the last reference in the chain. */
3465 while (ref && ref->next != NULL)
3467 gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT);
3472 if (ref == NULL || ref->type != REF_ARRAY)
3476 allocatable_array = expr->symtree->n.sym->attr.allocatable;
3478 allocatable_array = prev_ref->u.c.component->allocatable;
3480 /* Figure out the size of the array. */
3481 switch (ref->u.ar.type)
3485 upper = ref->u.ar.start;
3489 gcc_assert (ref->u.ar.as->type == AS_EXPLICIT);
3491 lower = ref->u.ar.as->lower;
3492 upper = ref->u.ar.as->upper;
3496 lower = ref->u.ar.start;
3497 upper = ref->u.ar.end;
3505 size = gfc_array_init_size (se->expr, ref->u.ar.as->rank, &offset,
3506 lower, upper, &se->pre);
3508 /* Allocate memory to store the data. */
3509 pointer = gfc_conv_descriptor_data_get (se->expr);
3510 STRIP_NOPS (pointer);
3512 if (TYPE_PRECISION (gfc_array_index_type) == 32)
3514 if (allocatable_array)
3515 allocate = gfor_fndecl_allocate_array;
3517 allocate = gfor_fndecl_allocate;
3519 else if (TYPE_PRECISION (gfc_array_index_type) == 64)
3521 if (allocatable_array)
3522 allocate = gfor_fndecl_allocate64_array;
3524 allocate = gfor_fndecl_allocate64;
3529 /* The allocate_array variants take the old pointer as first argument. */
3530 if (allocatable_array)
3531 tmp = build_call_expr (allocate, 3, pointer, size, pstat);
3533 tmp = build_call_expr (allocate, 2, size, pstat);
3534 tmp = build2 (MODIFY_EXPR, void_type_node, pointer, tmp);
3535 gfc_add_expr_to_block (&se->pre, tmp);
3537 tmp = gfc_conv_descriptor_offset (se->expr);
3538 gfc_add_modify_expr (&se->pre, tmp, offset);
3540 if (expr->ts.type == BT_DERIVED
3541 && expr->ts.derived->attr.alloc_comp)
3543 tmp = gfc_nullify_alloc_comp (expr->ts.derived, se->expr,
3544 ref->u.ar.as->rank);
3545 gfc_add_expr_to_block (&se->pre, tmp);
3552 /* Deallocate an array variable. Also used when an allocated variable goes
3557 gfc_array_deallocate (tree descriptor, tree pstat)
3563 gfc_start_block (&block);
3564 /* Get a pointer to the data. */
3565 var = gfc_conv_descriptor_data_get (descriptor);
3568 /* Parameter is the address of the data component. */
3569 tmp = build_call_expr (gfor_fndecl_deallocate, 2, var, pstat);
3570 gfc_add_expr_to_block (&block, tmp);
3572 /* Zero the data pointer. */
3573 tmp = build2 (MODIFY_EXPR, void_type_node,
3574 var, build_int_cst (TREE_TYPE (var), 0));
3575 gfc_add_expr_to_block (&block, tmp);
3577 return gfc_finish_block (&block);
3581 /* Create an array constructor from an initialization expression.
3582 We assume the frontend already did any expansions and conversions. */
3585 gfc_conv_array_initializer (tree type, gfc_expr * expr)
3592 unsigned HOST_WIDE_INT lo;
3594 VEC(constructor_elt,gc) *v = NULL;
3596 switch (expr->expr_type)
3599 case EXPR_STRUCTURE:
3600 /* A single scalar or derived type value. Create an array with all
3601 elements equal to that value. */
3602 gfc_init_se (&se, NULL);
3604 if (expr->expr_type == EXPR_CONSTANT)
3605 gfc_conv_constant (&se, expr);
3607 gfc_conv_structure (&se, expr, 1);
3609 tmp = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3610 gcc_assert (tmp && INTEGER_CST_P (tmp));
3611 hi = TREE_INT_CST_HIGH (tmp);
3612 lo = TREE_INT_CST_LOW (tmp);
3616 /* This will probably eat buckets of memory for large arrays. */
3617 while (hi != 0 || lo != 0)
3619 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, se.expr);
3627 /* Create a vector of all the elements. */
3628 for (c = expr->value.constructor; c; c = c->next)
3632 /* Problems occur when we get something like
3633 integer :: a(lots) = (/(i, i=1,lots)/) */
3634 /* TODO: Unexpanded array initializers. */
3636 ("Possible frontend bug: array constructor not expanded");
3638 if (mpz_cmp_si (c->n.offset, 0) != 0)
3639 index = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
3643 if (mpz_cmp_si (c->repeat, 0) != 0)
3647 mpz_set (maxval, c->repeat);
3648 mpz_add (maxval, c->n.offset, maxval);
3649 mpz_sub_ui (maxval, maxval, 1);
3650 tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
3651 if (mpz_cmp_si (c->n.offset, 0) != 0)
3653 mpz_add_ui (maxval, c->n.offset, 1);
3654 tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
3657 tmp1 = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
3659 range = build2 (RANGE_EXPR, integer_type_node, tmp1, tmp2);
3665 gfc_init_se (&se, NULL);
3666 switch (c->expr->expr_type)
3669 gfc_conv_constant (&se, c->expr);
3670 if (range == NULL_TREE)
3671 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3674 if (index != NULL_TREE)
3675 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3676 CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
3680 case EXPR_STRUCTURE:
3681 gfc_conv_structure (&se, c->expr, 1);
3682 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3692 return gfc_build_null_descriptor (type);
3698 /* Create a constructor from the list of elements. */
3699 tmp = build_constructor (type, v);
3700 TREE_CONSTANT (tmp) = 1;
3701 TREE_INVARIANT (tmp) = 1;
3706 /* Generate code to evaluate non-constant array bounds. Sets *poffset and
3707 returns the size (in elements) of the array. */
3710 gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
3711 stmtblock_t * pblock)
3726 size = gfc_index_one_node;
3727 offset = gfc_index_zero_node;
3728 for (dim = 0; dim < as->rank; dim++)
3730 /* Evaluate non-constant array bound expressions. */
3731 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
3732 if (as->lower[dim] && !INTEGER_CST_P (lbound))
3734 gfc_init_se (&se, NULL);
3735 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
3736 gfc_add_block_to_block (pblock, &se.pre);
3737 gfc_add_modify_expr (pblock, lbound, se.expr);
3739 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
3740 if (as->upper[dim] && !INTEGER_CST_P (ubound))
3742 gfc_init_se (&se, NULL);
3743 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
3744 gfc_add_block_to_block (pblock, &se.pre);
3745 gfc_add_modify_expr (pblock, ubound, se.expr);
3747 /* The offset of this dimension. offset = offset - lbound * stride. */
3748 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, size);
3749 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
3751 /* The size of this dimension, and the stride of the next. */
3752 if (dim + 1 < as->rank)
3753 stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
3755 stride = GFC_TYPE_ARRAY_SIZE (type);
3757 if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
3759 /* Calculate stride = size * (ubound + 1 - lbound). */
3760 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3761 gfc_index_one_node, lbound);
3762 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, tmp);
3763 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
3765 gfc_add_modify_expr (pblock, stride, tmp);
3767 stride = gfc_evaluate_now (tmp, pblock);
3769 /* Make sure that negative size arrays are translated
3770 to being zero size. */
3771 tmp = build2 (GE_EXPR, boolean_type_node,
3772 stride, gfc_index_zero_node);
3773 tmp = build3 (COND_EXPR, gfc_array_index_type, tmp,
3774 stride, gfc_index_zero_node);
3775 gfc_add_modify_expr (pblock, stride, tmp);
3781 gfc_trans_vla_type_sizes (sym, pblock);
3788 /* Generate code to initialize/allocate an array variable. */
3791 gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym, tree fnbody)
3801 gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
3803 /* Do nothing for USEd variables. */
3804 if (sym->attr.use_assoc)
3807 type = TREE_TYPE (decl);
3808 gcc_assert (GFC_ARRAY_TYPE_P (type));
3809 onstack = TREE_CODE (type) != POINTER_TYPE;
3811 gfc_start_block (&block);
3813 /* Evaluate character string length. */
3814 if (sym->ts.type == BT_CHARACTER
3815 && onstack && !INTEGER_CST_P (sym->ts.cl->backend_decl))
3817 gfc_trans_init_string_length (sym->ts.cl, &block);
3819 gfc_trans_vla_type_sizes (sym, &block);
3821 /* Emit a DECL_EXPR for this variable, which will cause the
3822 gimplifier to allocate storage, and all that good stuff. */
3823 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
3824 gfc_add_expr_to_block (&block, tmp);
3829 gfc_add_expr_to_block (&block, fnbody);
3830 return gfc_finish_block (&block);
3833 type = TREE_TYPE (type);
3835 gcc_assert (!sym->attr.use_assoc);
3836 gcc_assert (!TREE_STATIC (decl));
3837 gcc_assert (!sym->module);
3839 if (sym->ts.type == BT_CHARACTER
3840 && !INTEGER_CST_P (sym->ts.cl->backend_decl))
3841 gfc_trans_init_string_length (sym->ts.cl, &block);
3843 size = gfc_trans_array_bounds (type, sym, &offset, &block);
3845 /* Don't actually allocate space for Cray Pointees. */
3846 if (sym->attr.cray_pointee)
3848 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3849 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3850 gfc_add_expr_to_block (&block, fnbody);
3851 return gfc_finish_block (&block);
3854 /* The size is the number of elements in the array, so multiply by the
3855 size of an element to get the total size. */
3856 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3857 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
3859 /* Allocate memory to hold the data. */
3860 if (gfc_index_integer_kind == 4)
3861 fndecl = gfor_fndecl_internal_malloc;
3862 else if (gfc_index_integer_kind == 8)
3863 fndecl = gfor_fndecl_internal_malloc64;
3866 tmp = build_call_expr (fndecl, 1, size);
3867 tmp = fold_convert (TREE_TYPE (decl), tmp);
3868 gfc_add_modify_expr (&block, decl, tmp);
3870 /* Set offset of the array. */
3871 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3872 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3875 /* Automatic arrays should not have initializers. */
3876 gcc_assert (!sym->value);
3878 gfc_add_expr_to_block (&block, fnbody);
3880 /* Free the temporary. */
3881 tmp = convert (pvoid_type_node, decl);
3882 tmp = build_call_expr (gfor_fndecl_internal_free, 1, tmp);
3883 gfc_add_expr_to_block (&block, tmp);
3885 return gfc_finish_block (&block);
3889 /* Generate entry and exit code for g77 calling convention arrays. */
3892 gfc_trans_g77_array (gfc_symbol * sym, tree body)
3902 gfc_get_backend_locus (&loc);
3903 gfc_set_backend_locus (&sym->declared_at);
3905 /* Descriptor type. */
3906 parm = sym->backend_decl;
3907 type = TREE_TYPE (parm);
3908 gcc_assert (GFC_ARRAY_TYPE_P (type));
3910 gfc_start_block (&block);
3912 if (sym->ts.type == BT_CHARACTER
3913 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
3914 gfc_trans_init_string_length (sym->ts.cl, &block);
3916 /* Evaluate the bounds of the array. */
3917 gfc_trans_array_bounds (type, sym, &offset, &block);
3919 /* Set the offset. */
3920 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3921 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3923 /* Set the pointer itself if we aren't using the parameter directly. */
3924 if (TREE_CODE (parm) != PARM_DECL)
3926 tmp = convert (TREE_TYPE (parm), GFC_DECL_SAVED_DESCRIPTOR (parm));
3927 gfc_add_modify_expr (&block, parm, tmp);
3929 stmt = gfc_finish_block (&block);
3931 gfc_set_backend_locus (&loc);
3933 gfc_start_block (&block);
3935 /* Add the initialization code to the start of the function. */
3937 if (sym->attr.optional || sym->attr.not_always_present)
3939 tmp = gfc_conv_expr_present (sym);
3940 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
3943 gfc_add_expr_to_block (&block, stmt);
3944 gfc_add_expr_to_block (&block, body);
3946 return gfc_finish_block (&block);
3950 /* Modify the descriptor of an array parameter so that it has the
3951 correct lower bound. Also move the upper bound accordingly.
3952 If the array is not packed, it will be copied into a temporary.
3953 For each dimension we set the new lower and upper bounds. Then we copy the
3954 stride and calculate the offset for this dimension. We also work out
3955 what the stride of a packed array would be, and see it the two match.
3956 If the array need repacking, we set the stride to the values we just
3957 calculated, recalculate the offset and copy the array data.
3958 Code is also added to copy the data back at the end of the function.
3962 gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc, tree body)
3969 stmtblock_t cleanup;
3977 tree stride, stride2;
3987 /* Do nothing for pointer and allocatable arrays. */
3988 if (sym->attr.pointer || sym->attr.allocatable)
3991 if (sym->attr.dummy && gfc_is_nodesc_array (sym))
3992 return gfc_trans_g77_array (sym, body);
3994 gfc_get_backend_locus (&loc);
3995 gfc_set_backend_locus (&sym->declared_at);
3997 /* Descriptor type. */
3998 type = TREE_TYPE (tmpdesc);
3999 gcc_assert (GFC_ARRAY_TYPE_P (type));
4000 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
4001 dumdesc = build_fold_indirect_ref (dumdesc);
4002 gfc_start_block (&block);
4004 if (sym->ts.type == BT_CHARACTER
4005 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
4006 gfc_trans_init_string_length (sym->ts.cl, &block);
4008 checkparm = (sym->as->type == AS_EXPLICIT && flag_bounds_check);
4010 no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
4011 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
4013 if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
4015 /* For non-constant shape arrays we only check if the first dimension
4016 is contiguous. Repacking higher dimensions wouldn't gain us
4017 anything as we still don't know the array stride. */
4018 partial = gfc_create_var (boolean_type_node, "partial");
4019 TREE_USED (partial) = 1;
4020 tmp = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
4021 tmp = fold_build2 (EQ_EXPR, boolean_type_node, tmp, gfc_index_one_node);
4022 gfc_add_modify_expr (&block, partial, tmp);
4026 partial = NULL_TREE;
4029 /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
4030 here, however I think it does the right thing. */
4033 /* Set the first stride. */
4034 stride = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
4035 stride = gfc_evaluate_now (stride, &block);
4037 tmp = build2 (EQ_EXPR, boolean_type_node, stride, gfc_index_zero_node);