Shuffle ChangeLog entries into new files ChangeLog-1998,
[official-gcc.git] / gcc / fortran / trans-array.c
blob87e37ea63088e0d878e92f4967ef73f02c6d08cf
1 /* Array translation routines
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* trans-array.c-- Various array related code, including scalarization,
24 allocation, initialization and other support routines. */
26 /* How the scalarizer works.
27 In gfortran, array expressions use the same core routines as scalar
28 expressions.
29 First, a Scalarization State (SS) chain is built. This is done by walking
30 the expression tree, and building a linear list of the terms in the
31 expression. As the tree is walked, scalar subexpressions are translated.
33 The scalarization parameters are stored in a gfc_loopinfo structure.
34 First the start and stride of each term is calculated by
35 gfc_conv_ss_startstride. During this process the expressions for the array
36 descriptors and data pointers are also translated.
38 If the expression is an assignment, we must then resolve any dependencies.
39 In fortran all the rhs values of an assignment must be evaluated before
40 any assignments take place. This can require a temporary array to store the
41 values. We also require a temporary when we are passing array expressions
42 or vector subecripts as procedure parameters.
44 Array sections are passed without copying to a temporary. These use the
45 scalarizer to determine the shape of the section. The flag
46 loop->array_parameter tells the scalarizer that the actual values and loop
47 variables will not be required.
49 The function gfc_conv_loop_setup generates the scalarization setup code.
50 It determines the range of the scalarizing loop variables. If a temporary
51 is required, this is created and initialized. Code for scalar expressions
52 taken outside the loop is also generated at this time. Next the offset and
53 scaling required to translate from loop variables to array indices for each
54 term is calculated.
56 A call to gfc_start_scalarized_body marks the start of the scalarized
57 expression. This creates a scope and declares the loop variables. Before
58 calling this gfc_make_ss_chain_used must be used to indicate which terms
59 will be used inside this loop.
61 The scalar gfc_conv_* functions are then used to build the main body of the
62 scalarization loop. Scalarization loop variables and precalculated scalar
63 values are automatically substituted. Note that gfc_advance_se_ss_chain
64 must be used, rather than changing the se->ss directly.
66 For assignment expressions requiring a temporary two sub loops are
67 generated. The first stores the result of the expression in the temporary,
68 the second copies it to the result. A call to
69 gfc_trans_scalarized_loop_boundary marks the end of the main loop code and
70 the start of the copying loop. The temporary may be less than full rank.
72 Finally gfc_trans_scalarizing_loops is called to generate the implicit do
73 loops. The loops are added to the pre chain of the loopinfo. The post
74 chain may still contain cleanup code.
76 After the loop code has been added into its parent scope gfc_cleanup_loop
77 is called to free all the SS allocated by the scalarizer. */
79 #include "config.h"
80 #include "system.h"
81 #include "coretypes.h"
82 #include "tree.h"
83 #include "tree-gimple.h"
84 #include "ggc.h"
85 #include "toplev.h"
86 #include "real.h"
87 #include "flags.h"
88 #include "gfortran.h"
89 #include "trans.h"
90 #include "trans-stmt.h"
91 #include "trans-types.h"
92 #include "trans-array.h"
93 #include "trans-const.h"
94 #include "dependency.h"
96 static gfc_ss *gfc_walk_subexpr (gfc_ss *, gfc_expr *);
98 /* The contents of this structure aren't actually used, just the address. */
99 static gfc_ss gfc_ss_terminator_var;
100 gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
103 static tree
104 gfc_array_dataptr_type (tree desc)
106 return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
110 /* Build expressions to access the members of an array descriptor.
111 It's surprisingly easy to mess up here, so never access
112 an array descriptor by "brute force", always use these
113 functions. This also avoids problems if we change the format
114 of an array descriptor.
116 To understand these magic numbers, look at the comments
117 before gfc_build_array_type() in trans-types.c.
119 The code within these defines should be the only code which knows the format
120 of an array descriptor.
122 Any code just needing to read obtain the bounds of an array should use
123 gfc_conv_array_* rather than the following functions as these will return
124 know constant values, and work with arrays which do not have descriptors.
126 Don't forget to #undef these! */
128 #define DATA_FIELD 0
129 #define OFFSET_FIELD 1
130 #define DTYPE_FIELD 2
131 #define DIMENSION_FIELD 3
133 #define STRIDE_SUBFIELD 0
134 #define LBOUND_SUBFIELD 1
135 #define UBOUND_SUBFIELD 2
137 tree
138 gfc_conv_descriptor_data (tree desc)
140 tree field;
141 tree type;
143 type = TREE_TYPE (desc);
144 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
146 field = TYPE_FIELDS (type);
147 gcc_assert (DATA_FIELD == 0);
148 gcc_assert (field != NULL_TREE
149 && TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE
150 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == ARRAY_TYPE);
152 return build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
155 tree
156 gfc_conv_descriptor_offset (tree desc)
158 tree type;
159 tree field;
161 type = TREE_TYPE (desc);
162 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
164 field = gfc_advance_chain (TYPE_FIELDS (type), OFFSET_FIELD);
165 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
167 return build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
170 tree
171 gfc_conv_descriptor_dtype (tree desc)
173 tree field;
174 tree type;
176 type = TREE_TYPE (desc);
177 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
179 field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
180 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
182 return build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
185 static tree
186 gfc_conv_descriptor_dimension (tree desc, tree dim)
188 tree field;
189 tree type;
190 tree tmp;
192 type = TREE_TYPE (desc);
193 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
195 field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
196 gcc_assert (field != NULL_TREE
197 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
198 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
200 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
201 tmp = gfc_build_array_ref (tmp, dim);
202 return tmp;
205 tree
206 gfc_conv_descriptor_stride (tree desc, tree dim)
208 tree tmp;
209 tree field;
211 tmp = gfc_conv_descriptor_dimension (desc, dim);
212 field = TYPE_FIELDS (TREE_TYPE (tmp));
213 field = gfc_advance_chain (field, STRIDE_SUBFIELD);
214 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
216 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
217 return tmp;
220 tree
221 gfc_conv_descriptor_lbound (tree desc, tree dim)
223 tree tmp;
224 tree field;
226 tmp = gfc_conv_descriptor_dimension (desc, dim);
227 field = TYPE_FIELDS (TREE_TYPE (tmp));
228 field = gfc_advance_chain (field, LBOUND_SUBFIELD);
229 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
231 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
232 return tmp;
235 tree
236 gfc_conv_descriptor_ubound (tree desc, tree dim)
238 tree tmp;
239 tree field;
241 tmp = gfc_conv_descriptor_dimension (desc, dim);
242 field = TYPE_FIELDS (TREE_TYPE (tmp));
243 field = gfc_advance_chain (field, UBOUND_SUBFIELD);
244 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
246 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), tmp, field, NULL_TREE);
247 return tmp;
251 /* Build an null array descriptor constructor. */
253 tree
254 gfc_build_null_descriptor (tree type)
256 tree field;
257 tree tmp;
259 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
260 gcc_assert (DATA_FIELD == 0);
261 field = TYPE_FIELDS (type);
263 /* Set a NULL data pointer. */
264 tmp = tree_cons (field, null_pointer_node, NULL_TREE);
265 tmp = build1 (CONSTRUCTOR, type, tmp);
266 TREE_CONSTANT (tmp) = 1;
267 TREE_INVARIANT (tmp) = 1;
268 /* All other fields are ignored. */
270 return tmp;
274 /* Cleanup those #defines. */
276 #undef DATA_FIELD
277 #undef OFFSET_FIELD
278 #undef DTYPE_FIELD
279 #undef DIMENSION_FIELD
280 #undef STRIDE_SUBFIELD
281 #undef LBOUND_SUBFIELD
282 #undef UBOUND_SUBFIELD
285 /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
286 flags & 1 = Main loop body.
287 flags & 2 = temp copy loop. */
289 void
290 gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
292 for (; ss != gfc_ss_terminator; ss = ss->next)
293 ss->useflags = flags;
296 static void gfc_free_ss (gfc_ss *);
299 /* Free a gfc_ss chain. */
301 static void
302 gfc_free_ss_chain (gfc_ss * ss)
304 gfc_ss *next;
306 while (ss != gfc_ss_terminator)
308 gcc_assert (ss != NULL);
309 next = ss->next;
310 gfc_free_ss (ss);
311 ss = next;
316 /* Free a SS. */
318 static void
319 gfc_free_ss (gfc_ss * ss)
321 int n;
323 switch (ss->type)
325 case GFC_SS_SECTION:
326 case GFC_SS_VECTOR:
327 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
329 if (ss->data.info.subscript[n])
330 gfc_free_ss_chain (ss->data.info.subscript[n]);
332 break;
334 default:
335 break;
338 gfc_free (ss);
342 /* Free all the SS associated with a loop. */
344 void
345 gfc_cleanup_loop (gfc_loopinfo * loop)
347 gfc_ss *ss;
348 gfc_ss *next;
350 ss = loop->ss;
351 while (ss != gfc_ss_terminator)
353 gcc_assert (ss != NULL);
354 next = ss->loop_chain;
355 gfc_free_ss (ss);
356 ss = next;
361 /* Associate a SS chain with a loop. */
363 void
364 gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
366 gfc_ss *ss;
368 if (head == gfc_ss_terminator)
369 return;
371 ss = head;
372 for (; ss && ss != gfc_ss_terminator; ss = ss->next)
374 if (ss->next == gfc_ss_terminator)
375 ss->loop_chain = loop->ss;
376 else
377 ss->loop_chain = ss->next;
379 gcc_assert (ss == gfc_ss_terminator);
380 loop->ss = head;
384 /* Generate an initializer for a static pointer or allocatable array. */
386 void
387 gfc_trans_static_array_pointer (gfc_symbol * sym)
389 tree type;
391 gcc_assert (TREE_STATIC (sym->backend_decl));
392 /* Just zero the data member. */
393 type = TREE_TYPE (sym->backend_decl);
394 DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
398 /* Generate code to allocate an array temporary, or create a variable to
399 hold the data. If size is NULL zero the descriptor so that so that the
400 callee will allocate the array. Also generates code to free the array
401 afterwards. */
403 static void
404 gfc_trans_allocate_array_storage (gfc_loopinfo * loop, gfc_ss_info * info,
405 tree size, tree nelem)
407 tree tmp;
408 tree args;
409 tree desc;
410 tree data;
411 bool onstack;
413 desc = info->descriptor;
414 data = gfc_conv_descriptor_data (desc);
415 if (size == NULL_TREE)
417 /* A callee allocated array. */
418 gfc_add_modify_expr (&loop->pre, data, convert (TREE_TYPE (data),
419 gfc_index_zero_node));
420 info->data = data;
421 info->offset = gfc_index_zero_node;
422 onstack = FALSE;
424 else
426 /* Allocate the temporary. */
427 onstack = gfc_can_put_var_on_stack (size);
429 if (onstack)
431 /* Make a temporary variable to hold the data. */
432 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (nelem), nelem,
433 integer_one_node);
434 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
435 tmp);
436 tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
437 tmp);
438 tmp = gfc_create_var (tmp, "A");
439 tmp = gfc_build_addr_expr (TREE_TYPE (data), tmp);
440 gfc_add_modify_expr (&loop->pre, data, tmp);
441 info->data = data;
442 info->offset = gfc_index_zero_node;
445 else
447 /* Allocate memory to hold the data. */
448 args = gfc_chainon_list (NULL_TREE, size);
450 if (gfc_index_integer_kind == 4)
451 tmp = gfor_fndecl_internal_malloc;
452 else if (gfc_index_integer_kind == 8)
453 tmp = gfor_fndecl_internal_malloc64;
454 else
455 gcc_unreachable ();
456 tmp = gfc_build_function_call (tmp, args);
457 tmp = convert (TREE_TYPE (data), tmp);
458 gfc_add_modify_expr (&loop->pre, data, tmp);
460 info->data = data;
461 info->offset = gfc_index_zero_node;
465 /* The offset is zero because we create temporaries with a zero
466 lower bound. */
467 tmp = gfc_conv_descriptor_offset (desc);
468 gfc_add_modify_expr (&loop->pre, tmp, gfc_index_zero_node);
470 if (!onstack)
472 /* Free the temporary. */
473 tmp = convert (pvoid_type_node, info->data);
474 tmp = gfc_chainon_list (NULL_TREE, tmp);
475 tmp = gfc_build_function_call (gfor_fndecl_internal_free, tmp);
476 gfc_add_expr_to_block (&loop->post, tmp);
481 /* Generate code to allocate and initialize the descriptor for a temporary
482 array. This is used for both temporaries needed by the scalarizer, and
483 functions returning arrays. Adjusts the loop variables to be zero-based,
484 and calculates the loop bounds for callee allocated arrays.
485 Also fills in the descriptor, data and offset fields of info if known.
486 Returns the size of the array, or NULL for a callee allocated array. */
488 tree
489 gfc_trans_allocate_temp_array (gfc_loopinfo * loop, gfc_ss_info * info,
490 tree eltype)
492 tree type;
493 tree desc;
494 tree tmp;
495 tree size;
496 tree nelem;
497 int n;
498 int dim;
500 gcc_assert (info->dimen > 0);
501 /* Set the lower bound to zero. */
502 for (dim = 0; dim < info->dimen; dim++)
504 n = loop->order[dim];
505 if (n < loop->temp_dim)
506 gcc_assert (integer_zerop (loop->from[n]));
507 else
509 /* Callee allocated arrays may not have a known bound yet. */
510 if (loop->to[n])
511 loop->to[n] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
512 loop->to[n], loop->from[n]);
513 loop->from[n] = gfc_index_zero_node;
516 info->delta[dim] = gfc_index_zero_node;
517 info->start[dim] = gfc_index_zero_node;
518 info->stride[dim] = gfc_index_one_node;
519 info->dim[dim] = dim;
522 /* Initialize the descriptor. */
523 type =
524 gfc_get_array_type_bounds (eltype, info->dimen, loop->from, loop->to, 1);
525 desc = gfc_create_var (type, "atmp");
526 GFC_DECL_PACKED_ARRAY (desc) = 1;
528 info->descriptor = desc;
529 size = gfc_index_one_node;
531 /* Fill in the array dtype. */
532 tmp = gfc_conv_descriptor_dtype (desc);
533 gfc_add_modify_expr (&loop->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
536 Fill in the bounds and stride. This is a packed array, so:
538 size = 1;
539 for (n = 0; n < rank; n++)
541 stride[n] = size
542 delta = ubound[n] + 1 - lbound[n];
543 size = size * delta;
545 size = size * sizeof(element);
548 for (n = 0; n < info->dimen; n++)
550 if (loop->to[n] == NULL_TREE)
552 /* For a callee allocated array express the loop bounds in terms
553 of the descriptor fields. */
554 tmp = build2 (MINUS_EXPR, gfc_array_index_type,
555 gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]),
556 gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]));
557 loop->to[n] = tmp;
558 size = NULL_TREE;
559 continue;
562 /* Store the stride and bound components in the descriptor. */
563 tmp = gfc_conv_descriptor_stride (desc, gfc_rank_cst[n]);
564 gfc_add_modify_expr (&loop->pre, tmp, size);
566 tmp = gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]);
567 gfc_add_modify_expr (&loop->pre, tmp, gfc_index_zero_node);
569 tmp = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]);
570 gfc_add_modify_expr (&loop->pre, tmp, loop->to[n]);
572 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
573 loop->to[n], gfc_index_one_node);
575 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
576 size = gfc_evaluate_now (size, &loop->pre);
579 /* Get the size of the array. */
580 nelem = size;
581 if (size)
582 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
583 TYPE_SIZE_UNIT (gfc_get_element_type (type)));
585 gfc_trans_allocate_array_storage (loop, info, size, nelem);
587 if (info->dimen > loop->temp_dim)
588 loop->temp_dim = info->dimen;
590 return size;
594 /* Make sure offset is a variable. */
596 static void
597 gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
598 tree * offsetvar)
600 /* We should have already created the offset variable. We cannot
601 create it here because we may be in an inner scope. */
602 gcc_assert (*offsetvar != NULL_TREE);
603 gfc_add_modify_expr (pblock, *offsetvar, *poffset);
604 *poffset = *offsetvar;
605 TREE_USED (*offsetvar) = 1;
609 /* Assign an element of an array constructor. */
611 static void
612 gfc_trans_array_ctor_element (stmtblock_t * pblock, tree pointer,
613 tree offset, gfc_se * se, gfc_expr * expr)
615 tree tmp;
616 tree args;
618 gfc_conv_expr (se, expr);
620 /* Store the value. */
621 tmp = gfc_build_indirect_ref (pointer);
622 tmp = gfc_build_array_ref (tmp, offset);
623 if (expr->ts.type == BT_CHARACTER)
625 gfc_conv_string_parameter (se);
626 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
628 /* The temporary is an array of pointers. */
629 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
630 gfc_add_modify_expr (&se->pre, tmp, se->expr);
632 else
634 /* The temporary is an array of string values. */
635 tmp = gfc_build_addr_expr (pchar_type_node, tmp);
636 /* We know the temporary and the value will be the same length,
637 so can use memcpy. */
638 args = gfc_chainon_list (NULL_TREE, tmp);
639 args = gfc_chainon_list (args, se->expr);
640 args = gfc_chainon_list (args, se->string_length);
641 tmp = built_in_decls[BUILT_IN_MEMCPY];
642 tmp = gfc_build_function_call (tmp, args);
643 gfc_add_expr_to_block (&se->pre, tmp);
646 else
648 /* TODO: Should the frontend already have done this conversion? */
649 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
650 gfc_add_modify_expr (&se->pre, tmp, se->expr);
653 gfc_add_block_to_block (pblock, &se->pre);
654 gfc_add_block_to_block (pblock, &se->post);
658 /* Add the contents of an array to the constructor. */
660 static void
661 gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
662 tree type ATTRIBUTE_UNUSED,
663 tree pointer, gfc_expr * expr,
664 tree * poffset, tree * offsetvar)
666 gfc_se se;
667 gfc_ss *ss;
668 gfc_loopinfo loop;
669 stmtblock_t body;
670 tree tmp;
672 /* We need this to be a variable so we can increment it. */
673 gfc_put_offset_into_var (pblock, poffset, offsetvar);
675 gfc_init_se (&se, NULL);
677 /* Walk the array expression. */
678 ss = gfc_walk_expr (expr);
679 gcc_assert (ss != gfc_ss_terminator);
681 /* Initialize the scalarizer. */
682 gfc_init_loopinfo (&loop);
683 gfc_add_ss_to_loop (&loop, ss);
685 /* Initialize the loop. */
686 gfc_conv_ss_startstride (&loop);
687 gfc_conv_loop_setup (&loop);
689 /* Make the loop body. */
690 gfc_mark_ss_chain_used (ss, 1);
691 gfc_start_scalarized_body (&loop, &body);
692 gfc_copy_loopinfo_to_se (&se, &loop);
693 se.ss = ss;
695 if (expr->ts.type == BT_CHARACTER)
696 gfc_todo_error ("character arrays in constructors");
698 gfc_trans_array_ctor_element (&body, pointer, *poffset, &se, expr);
699 gcc_assert (se.ss == gfc_ss_terminator);
701 /* Increment the offset. */
702 tmp = build2 (PLUS_EXPR, gfc_array_index_type, *poffset, gfc_index_one_node);
703 gfc_add_modify_expr (&body, *poffset, tmp);
705 /* Finish the loop. */
706 gfc_trans_scalarizing_loops (&loop, &body);
707 gfc_add_block_to_block (&loop.pre, &loop.post);
708 tmp = gfc_finish_block (&loop.pre);
709 gfc_add_expr_to_block (pblock, tmp);
711 gfc_cleanup_loop (&loop);
715 /* Assign the values to the elements of an array constructor. */
717 static void
718 gfc_trans_array_constructor_value (stmtblock_t * pblock, tree type,
719 tree pointer, gfc_constructor * c,
720 tree * poffset, tree * offsetvar)
722 tree tmp;
723 stmtblock_t body;
724 tree loopbody;
725 gfc_se se;
727 for (; c; c = c->next)
729 /* If this is an iterator or an array, the offset must be a variable. */
730 if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
731 gfc_put_offset_into_var (pblock, poffset, offsetvar);
733 gfc_start_block (&body);
735 if (c->expr->expr_type == EXPR_ARRAY)
737 /* Array constructors can be nested. */
738 gfc_trans_array_constructor_value (&body, type, pointer,
739 c->expr->value.constructor,
740 poffset, offsetvar);
742 else if (c->expr->rank > 0)
744 gfc_trans_array_constructor_subarray (&body, type, pointer,
745 c->expr, poffset, offsetvar);
747 else
749 /* This code really upsets the gimplifier so don't bother for now. */
750 gfc_constructor *p;
751 HOST_WIDE_INT n;
752 HOST_WIDE_INT size;
754 p = c;
755 n = 0;
756 while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
758 p = p->next;
759 n++;
761 if (n < 4)
763 /* Scalar values. */
764 gfc_init_se (&se, NULL);
765 gfc_trans_array_ctor_element (&body, pointer, *poffset, &se,
766 c->expr);
768 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
769 *poffset, gfc_index_one_node);
771 else
773 /* Collect multiple scalar constants into a constructor. */
774 tree list;
775 tree init;
776 tree bound;
777 tree tmptype;
779 p = c;
780 list = NULL_TREE;
781 /* Count the number of consecutive scalar constants. */
782 while (p && !(p->iterator
783 || p->expr->expr_type != EXPR_CONSTANT))
785 gfc_init_se (&se, NULL);
786 gfc_conv_constant (&se, p->expr);
787 if (p->expr->ts.type == BT_CHARACTER
788 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE
789 (TREE_TYPE (pointer)))))
791 /* For constant character array constructors we build
792 an array of pointers. */
793 se.expr = gfc_build_addr_expr (pchar_type_node,
794 se.expr);
797 list = tree_cons (NULL_TREE, se.expr, list);
798 c = p;
799 p = p->next;
802 bound = build_int_cst (NULL_TREE, n - 1);
803 /* Create an array type to hold them. */
804 tmptype = build_range_type (gfc_array_index_type,
805 gfc_index_zero_node, bound);
806 tmptype = build_array_type (type, tmptype);
808 init = build1 (CONSTRUCTOR, tmptype, nreverse (list));
809 TREE_CONSTANT (init) = 1;
810 TREE_INVARIANT (init) = 1;
811 TREE_STATIC (init) = 1;
812 /* Create a static variable to hold the data. */
813 tmp = gfc_create_var (tmptype, "data");
814 TREE_STATIC (tmp) = 1;
815 TREE_CONSTANT (tmp) = 1;
816 TREE_INVARIANT (tmp) = 1;
817 DECL_INITIAL (tmp) = init;
818 init = tmp;
820 /* Use BUILTIN_MEMCPY to assign the values. */
821 tmp = gfc_build_indirect_ref (pointer);
822 tmp = gfc_build_array_ref (tmp, *poffset);
823 tmp = gfc_build_addr_expr (NULL, tmp);
824 init = gfc_build_addr_expr (NULL, init);
826 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
827 bound = build_int_cst (NULL_TREE, n * size);
828 tmp = gfc_chainon_list (NULL_TREE, tmp);
829 tmp = gfc_chainon_list (tmp, init);
830 tmp = gfc_chainon_list (tmp, bound);
831 tmp = gfc_build_function_call (built_in_decls[BUILT_IN_MEMCPY],
832 tmp);
833 gfc_add_expr_to_block (&body, tmp);
835 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
836 *poffset, bound);
838 if (!INTEGER_CST_P (*poffset))
840 gfc_add_modify_expr (&body, *offsetvar, *poffset);
841 *poffset = *offsetvar;
845 /* The frontend should already have done any expansions. */
846 if (c->iterator)
848 tree end;
849 tree step;
850 tree loopvar;
851 tree exit_label;
853 loopbody = gfc_finish_block (&body);
855 gfc_init_se (&se, NULL);
856 gfc_conv_expr (&se, c->iterator->var);
857 gfc_add_block_to_block (pblock, &se.pre);
858 loopvar = se.expr;
860 /* Initialize the loop. */
861 gfc_init_se (&se, NULL);
862 gfc_conv_expr_val (&se, c->iterator->start);
863 gfc_add_block_to_block (pblock, &se.pre);
864 gfc_add_modify_expr (pblock, loopvar, se.expr);
866 gfc_init_se (&se, NULL);
867 gfc_conv_expr_val (&se, c->iterator->end);
868 gfc_add_block_to_block (pblock, &se.pre);
869 end = gfc_evaluate_now (se.expr, pblock);
871 gfc_init_se (&se, NULL);
872 gfc_conv_expr_val (&se, c->iterator->step);
873 gfc_add_block_to_block (pblock, &se.pre);
874 step = gfc_evaluate_now (se.expr, pblock);
876 /* Generate the loop body. */
877 exit_label = gfc_build_label_decl (NULL_TREE);
878 gfc_start_block (&body);
880 /* Generate the exit condition. */
881 end = build2 (GT_EXPR, boolean_type_node, loopvar, end);
882 tmp = build1_v (GOTO_EXPR, exit_label);
883 TREE_USED (exit_label) = 1;
884 tmp = build3_v (COND_EXPR, end, tmp, build_empty_stmt ());
885 gfc_add_expr_to_block (&body, tmp);
887 /* The main loop body. */
888 gfc_add_expr_to_block (&body, loopbody);
890 /* Increment the loop variable. */
891 tmp = build2 (PLUS_EXPR, TREE_TYPE (loopvar), loopvar, step);
892 gfc_add_modify_expr (&body, loopvar, tmp);
894 /* Finish the loop. */
895 tmp = gfc_finish_block (&body);
896 tmp = build1_v (LOOP_EXPR, tmp);
897 gfc_add_expr_to_block (pblock, tmp);
899 /* Add the exit label. */
900 tmp = build1_v (LABEL_EXPR, exit_label);
901 gfc_add_expr_to_block (pblock, tmp);
903 else
905 /* Pass the code as is. */
906 tmp = gfc_finish_block (&body);
907 gfc_add_expr_to_block (pblock, tmp);
913 /* Get the size of an expression. Returns -1 if the size isn't constant.
914 Implied do loops with non-constant bounds are tricky because we must only
915 evaluate the bounds once. */
917 static void
918 gfc_get_array_cons_size (mpz_t * size, gfc_constructor * c)
920 gfc_iterator *i;
921 mpz_t val;
922 mpz_t len;
924 mpz_set_ui (*size, 0);
925 mpz_init (len);
926 mpz_init (val);
928 for (; c; c = c->next)
930 if (c->expr->expr_type == EXPR_ARRAY)
932 /* A nested array constructor. */
933 gfc_get_array_cons_size (&len, c->expr->value.constructor);
934 if (mpz_sgn (len) < 0)
936 mpz_set (*size, len);
937 mpz_clear (len);
938 mpz_clear (val);
939 return;
942 else
944 if (c->expr->rank > 0)
946 mpz_set_si (*size, -1);
947 mpz_clear (len);
948 mpz_clear (val);
949 return;
951 mpz_set_ui (len, 1);
954 if (c->iterator)
956 i = c->iterator;
958 if (i->start->expr_type != EXPR_CONSTANT
959 || i->end->expr_type != EXPR_CONSTANT
960 || i->step->expr_type != EXPR_CONSTANT)
962 mpz_set_si (*size, -1);
963 mpz_clear (len);
964 mpz_clear (val);
965 return;
968 mpz_add (val, i->end->value.integer, i->start->value.integer);
969 mpz_tdiv_q (val, val, i->step->value.integer);
970 mpz_add_ui (val, val, 1);
971 mpz_mul (len, len, val);
973 mpz_add (*size, *size, len);
975 mpz_clear (len);
976 mpz_clear (val);
980 /* Figure out the string length of a variable reference expression.
981 Used by get_array_ctor_strlen. */
983 static void
984 get_array_ctor_var_strlen (gfc_expr * expr, tree * len)
986 gfc_ref *ref;
987 gfc_typespec *ts;
989 /* Don't bother if we already know the length is a constant. */
990 if (*len && INTEGER_CST_P (*len))
991 return;
993 ts = &expr->symtree->n.sym->ts;
994 for (ref = expr->ref; ref; ref = ref->next)
996 switch (ref->type)
998 case REF_ARRAY:
999 /* Array references don't change the string length. */
1000 break;
1002 case COMPONENT_REF:
1003 /* Use the length of the component. */
1004 ts = &ref->u.c.component->ts;
1005 break;
1007 default:
1008 /* TODO: Substrings are tricky because we can't evaluate the
1009 expression more than once. For now we just give up, and hope
1010 we can figure it out elsewhere. */
1011 return;
1015 *len = ts->cl->backend_decl;
1019 /* Figure out the string length of a character array constructor.
1020 Returns TRUE if all elements are character constants. */
1022 static bool
1023 get_array_ctor_strlen (gfc_constructor * c, tree * len)
1025 bool is_const;
1027 is_const = TRUE;
1028 for (; c; c = c->next)
1030 switch (c->expr->expr_type)
1032 case EXPR_CONSTANT:
1033 if (!(*len && INTEGER_CST_P (*len)))
1034 *len = build_int_cstu (gfc_charlen_type_node,
1035 c->expr->value.character.length);
1036 break;
1038 case EXPR_ARRAY:
1039 if (!get_array_ctor_strlen (c->expr->value.constructor, len))
1040 is_const = FALSE;
1041 break;
1043 case EXPR_VARIABLE:
1044 is_const = false;
1045 get_array_ctor_var_strlen (c->expr, len);
1046 break;
1048 default:
1049 is_const = FALSE;
1050 /* TODO: For now we just ignore anything we don't know how to
1051 handle, and hope we can figure it out a different way. */
1052 break;
1056 return is_const;
1060 /* Array constructors are handled by constructing a temporary, then using that
1061 within the scalarization loop. This is not optimal, but seems by far the
1062 simplest method. */
1064 static void
1065 gfc_trans_array_constructor (gfc_loopinfo * loop, gfc_ss * ss)
1067 tree offset;
1068 tree offsetvar;
1069 tree desc;
1070 tree size;
1071 tree type;
1072 bool const_string;
1074 ss->data.info.dimen = loop->dimen;
1076 if (ss->expr->ts.type == BT_CHARACTER)
1078 const_string = get_array_ctor_strlen (ss->expr->value.constructor,
1079 &ss->string_length);
1080 if (!ss->string_length)
1081 gfc_todo_error ("complex character array constructors");
1083 type = gfc_get_character_type_len (ss->expr->ts.kind, ss->string_length);
1084 if (const_string)
1085 type = build_pointer_type (type);
1087 else
1089 const_string = TRUE;
1090 type = gfc_typenode_for_spec (&ss->expr->ts);
1093 size = gfc_trans_allocate_temp_array (loop, &ss->data.info, type);
1095 desc = ss->data.info.descriptor;
1096 offset = gfc_index_zero_node;
1097 offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
1098 TREE_USED (offsetvar) = 0;
1099 gfc_trans_array_constructor_value (&loop->pre, type,
1100 ss->data.info.data,
1101 ss->expr->value.constructor, &offset,
1102 &offsetvar);
1104 if (TREE_USED (offsetvar))
1105 pushdecl (offsetvar);
1106 else
1107 gcc_assert (INTEGER_CST_P (offset));
1108 #if 0
1109 /* Disable bound checking for now because it's probably broken. */
1110 if (flag_bounds_check)
1112 gcc_unreachable ();
1114 #endif
1118 /* Add the pre and post chains for all the scalar expressions in a SS chain
1119 to loop. This is called after the loop parameters have been calculated,
1120 but before the actual scalarizing loops. */
1122 static void
1123 gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript)
1125 gfc_se se;
1126 int n;
1128 /* TODO: This can generate bad code if there are ordering dependencies.
1129 eg. a callee allocated function and an unknown size constructor. */
1130 gcc_assert (ss != NULL);
1132 for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
1134 gcc_assert (ss);
1136 switch (ss->type)
1138 case GFC_SS_SCALAR:
1139 /* Scalar expression. Evaluate this now. This includes elemental
1140 dimension indices, but not array section bounds. */
1141 gfc_init_se (&se, NULL);
1142 gfc_conv_expr (&se, ss->expr);
1143 gfc_add_block_to_block (&loop->pre, &se.pre);
1145 if (ss->expr->ts.type != BT_CHARACTER)
1147 /* Move the evaluation of scalar expressions outside the
1148 scalarization loop. */
1149 if (subscript)
1150 se.expr = convert(gfc_array_index_type, se.expr);
1151 se.expr = gfc_evaluate_now (se.expr, &loop->pre);
1152 gfc_add_block_to_block (&loop->pre, &se.post);
1154 else
1155 gfc_add_block_to_block (&loop->post, &se.post);
1157 ss->data.scalar.expr = se.expr;
1158 ss->string_length = se.string_length;
1159 break;
1161 case GFC_SS_REFERENCE:
1162 /* Scalar reference. Evaluate this now. */
1163 gfc_init_se (&se, NULL);
1164 gfc_conv_expr_reference (&se, ss->expr);
1165 gfc_add_block_to_block (&loop->pre, &se.pre);
1166 gfc_add_block_to_block (&loop->post, &se.post);
1168 ss->data.scalar.expr = gfc_evaluate_now (se.expr, &loop->pre);
1169 ss->string_length = se.string_length;
1170 break;
1172 case GFC_SS_SECTION:
1173 case GFC_SS_VECTOR:
1174 /* Scalarized expression. Evaluate any scalar subscripts. */
1175 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1177 /* Add the expressions for scalar subscripts. */
1178 if (ss->data.info.subscript[n])
1179 gfc_add_loop_ss_code (loop, ss->data.info.subscript[n], true);
1181 break;
1183 case GFC_SS_INTRINSIC:
1184 gfc_add_intrinsic_ss_code (loop, ss);
1185 break;
1187 case GFC_SS_FUNCTION:
1188 /* Array function return value. We call the function and save its
1189 result in a temporary for use inside the loop. */
1190 gfc_init_se (&se, NULL);
1191 se.loop = loop;
1192 se.ss = ss;
1193 gfc_conv_expr (&se, ss->expr);
1194 gfc_add_block_to_block (&loop->pre, &se.pre);
1195 gfc_add_block_to_block (&loop->post, &se.post);
1196 break;
1198 case GFC_SS_CONSTRUCTOR:
1199 gfc_trans_array_constructor (loop, ss);
1200 break;
1202 case GFC_SS_TEMP:
1203 case GFC_SS_COMPONENT:
1204 /* Do nothing. These are handled elsewhere. */
1205 break;
1207 default:
1208 gcc_unreachable ();
1214 /* Translate expressions for the descriptor and data pointer of a SS. */
1215 /*GCC ARRAYS*/
1217 static void
1218 gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
1220 gfc_se se;
1221 tree tmp;
1223 /* Get the descriptor for the array to be scalarized. */
1224 gcc_assert (ss->expr->expr_type == EXPR_VARIABLE);
1225 gfc_init_se (&se, NULL);
1226 se.descriptor_only = 1;
1227 gfc_conv_expr_lhs (&se, ss->expr);
1228 gfc_add_block_to_block (block, &se.pre);
1229 ss->data.info.descriptor = se.expr;
1230 ss->string_length = se.string_length;
1232 if (base)
1234 /* Also the data pointer. */
1235 tmp = gfc_conv_array_data (se.expr);
1236 /* If this is a variable or address of a variable we use it directly.
1237 Otherwise we must evaluate it now to avoid breaking dependency
1238 analysis by pulling the expressions for elemental array indices
1239 inside the loop. */
1240 if (!(DECL_P (tmp)
1241 || (TREE_CODE (tmp) == ADDR_EXPR
1242 && DECL_P (TREE_OPERAND (tmp, 0)))))
1243 tmp = gfc_evaluate_now (tmp, block);
1244 ss->data.info.data = tmp;
1246 tmp = gfc_conv_array_offset (se.expr);
1247 ss->data.info.offset = gfc_evaluate_now (tmp, block);
1252 /* Initialize a gfc_loopinfo structure. */
1254 void
1255 gfc_init_loopinfo (gfc_loopinfo * loop)
1257 int n;
1259 memset (loop, 0, sizeof (gfc_loopinfo));
1260 gfc_init_block (&loop->pre);
1261 gfc_init_block (&loop->post);
1263 /* Initially scalarize in order. */
1264 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1265 loop->order[n] = n;
1267 loop->ss = gfc_ss_terminator;
1271 /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
1272 chain. */
1274 void
1275 gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
1277 se->loop = loop;
1281 /* Return an expression for the data pointer of an array. */
1283 tree
1284 gfc_conv_array_data (tree descriptor)
1286 tree type;
1288 type = TREE_TYPE (descriptor);
1289 if (GFC_ARRAY_TYPE_P (type))
1291 if (TREE_CODE (type) == POINTER_TYPE)
1292 return descriptor;
1293 else
1295 /* Descriptorless arrays. */
1296 return gfc_build_addr_expr (NULL, descriptor);
1299 else
1300 return gfc_conv_descriptor_data (descriptor);
1304 /* Return an expression for the base offset of an array. */
1306 tree
1307 gfc_conv_array_offset (tree descriptor)
1309 tree type;
1311 type = TREE_TYPE (descriptor);
1312 if (GFC_ARRAY_TYPE_P (type))
1313 return GFC_TYPE_ARRAY_OFFSET (type);
1314 else
1315 return gfc_conv_descriptor_offset (descriptor);
1319 /* Get an expression for the array stride. */
1321 tree
1322 gfc_conv_array_stride (tree descriptor, int dim)
1324 tree tmp;
1325 tree type;
1327 type = TREE_TYPE (descriptor);
1329 /* For descriptorless arrays use the array size. */
1330 tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
1331 if (tmp != NULL_TREE)
1332 return tmp;
1334 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[dim]);
1335 return tmp;
1339 /* Like gfc_conv_array_stride, but for the lower bound. */
1341 tree
1342 gfc_conv_array_lbound (tree descriptor, int dim)
1344 tree tmp;
1345 tree type;
1347 type = TREE_TYPE (descriptor);
1349 tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
1350 if (tmp != NULL_TREE)
1351 return tmp;
1353 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[dim]);
1354 return tmp;
1358 /* Like gfc_conv_array_stride, but for the upper bound. */
1360 tree
1361 gfc_conv_array_ubound (tree descriptor, int dim)
1363 tree tmp;
1364 tree type;
1366 type = TREE_TYPE (descriptor);
1368 tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
1369 if (tmp != NULL_TREE)
1370 return tmp;
1372 /* This should only ever happen when passing an assumed shape array
1373 as an actual parameter. The value will never be used. */
1374 if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
1375 return gfc_index_zero_node;
1377 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[dim]);
1378 return tmp;
1382 /* Translate an array reference. The descriptor should be in se->expr.
1383 Do not use this function, it wil be removed soon. */
1384 /*GCC ARRAYS*/
1386 static void
1387 gfc_conv_array_index_ref (gfc_se * se, tree pointer, tree * indices,
1388 tree offset, int dimen)
1390 tree array;
1391 tree tmp;
1392 tree index;
1393 int n;
1395 array = gfc_build_indirect_ref (pointer);
1397 index = offset;
1398 for (n = 0; n < dimen; n++)
1400 /* index = index + stride[n]*indices[n] */
1401 tmp = gfc_conv_array_stride (se->expr, n);
1402 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, indices[n], tmp);
1404 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
1407 /* Result = data[index]. */
1408 tmp = gfc_build_array_ref (array, index);
1410 /* Check we've used the correct number of dimensions. */
1411 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) != ARRAY_TYPE);
1413 se->expr = tmp;
1417 /* Generate code to perform an array index bound check. */
1419 static tree
1420 gfc_trans_array_bound_check (gfc_se * se, tree descriptor, tree index, int n)
1422 tree cond;
1423 tree fault;
1424 tree tmp;
1426 if (!flag_bounds_check)
1427 return index;
1429 index = gfc_evaluate_now (index, &se->pre);
1430 /* Check lower bound. */
1431 tmp = gfc_conv_array_lbound (descriptor, n);
1432 fault = fold_build2 (LT_EXPR, boolean_type_node, index, tmp);
1433 /* Check upper bound. */
1434 tmp = gfc_conv_array_ubound (descriptor, n);
1435 cond = fold_build2 (GT_EXPR, boolean_type_node, index, tmp);
1436 fault = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, fault, cond);
1438 gfc_trans_runtime_check (fault, gfc_strconst_fault, &se->pre);
1440 return index;
1444 /* A reference to an array vector subscript. Uses recursion to handle nested
1445 vector subscripts. */
1447 static tree
1448 gfc_conv_vector_array_index (gfc_se * se, tree index, gfc_ss * ss)
1450 tree descsave;
1451 tree indices[GFC_MAX_DIMENSIONS];
1452 gfc_array_ref *ar;
1453 gfc_ss_info *info;
1454 int n;
1456 gcc_assert (ss && ss->type == GFC_SS_VECTOR);
1458 /* Save the descriptor. */
1459 descsave = se->expr;
1460 info = &ss->data.info;
1461 se->expr = info->descriptor;
1463 ar = &info->ref->u.ar;
1464 for (n = 0; n < ar->dimen; n++)
1466 switch (ar->dimen_type[n])
1468 case DIMEN_ELEMENT:
1469 gcc_assert (info->subscript[n] != gfc_ss_terminator
1470 && info->subscript[n]->type == GFC_SS_SCALAR);
1471 indices[n] = info->subscript[n]->data.scalar.expr;
1472 break;
1474 case DIMEN_RANGE:
1475 indices[n] = index;
1476 break;
1478 case DIMEN_VECTOR:
1479 index = gfc_conv_vector_array_index (se, index, info->subscript[n]);
1481 indices[n] =
1482 gfc_trans_array_bound_check (se, info->descriptor, index, n);
1483 break;
1485 default:
1486 gcc_unreachable ();
1489 /* Get the index from the vector. */
1490 gfc_conv_array_index_ref (se, info->data, indices, info->offset, ar->dimen);
1491 index = se->expr;
1492 /* Put the descriptor back. */
1493 se->expr = descsave;
1495 return index;
1499 /* Return the offset for an index. Performs bound checking for elemental
1500 dimensions. Single element references are processed separately. */
1502 static tree
1503 gfc_conv_array_index_offset (gfc_se * se, gfc_ss_info * info, int dim, int i,
1504 gfc_array_ref * ar, tree stride)
1506 tree index;
1508 /* Get the index into the array for this dimension. */
1509 if (ar)
1511 gcc_assert (ar->type != AR_ELEMENT);
1512 if (ar->dimen_type[dim] == DIMEN_ELEMENT)
1514 gcc_assert (i == -1);
1515 /* Elemental dimension. */
1516 gcc_assert (info->subscript[dim]
1517 && info->subscript[dim]->type == GFC_SS_SCALAR);
1518 /* We've already translated this value outside the loop. */
1519 index = info->subscript[dim]->data.scalar.expr;
1521 index =
1522 gfc_trans_array_bound_check (se, info->descriptor, index, dim);
1524 else
1526 /* Scalarized dimension. */
1527 gcc_assert (info && se->loop);
1529 /* Multiply the loop variable by the stride and delta. */
1530 index = se->loop->loopvar[i];
1531 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index,
1532 info->stride[i]);
1533 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index,
1534 info->delta[i]);
1536 if (ar->dimen_type[dim] == DIMEN_VECTOR)
1538 /* Handle vector subscripts. */
1539 index = gfc_conv_vector_array_index (se, index,
1540 info->subscript[dim]);
1541 index =
1542 gfc_trans_array_bound_check (se, info->descriptor, index,
1543 dim);
1545 else
1546 gcc_assert (ar->dimen_type[dim] == DIMEN_RANGE);
1549 else
1551 /* Temporary array or derived type component. */
1552 gcc_assert (se->loop);
1553 index = se->loop->loopvar[se->loop->order[i]];
1554 if (!integer_zerop (info->delta[i]))
1555 index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1556 index, info->delta[i]);
1559 /* Multiply by the stride. */
1560 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index, stride);
1562 return index;
1566 /* Build a scalarized reference to an array. */
1568 static void
1569 gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar)
1571 gfc_ss_info *info;
1572 tree index;
1573 tree tmp;
1574 int n;
1576 info = &se->ss->data.info;
1577 if (ar)
1578 n = se->loop->order[0];
1579 else
1580 n = 0;
1582 index = gfc_conv_array_index_offset (se, info, info->dim[n], n, ar,
1583 info->stride0);
1584 /* Add the offset for this dimension to the stored offset for all other
1585 dimensions. */
1586 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, info->offset);
1588 tmp = gfc_build_indirect_ref (info->data);
1589 se->expr = gfc_build_array_ref (tmp, index);
1593 /* Translate access of temporary array. */
1595 void
1596 gfc_conv_tmp_array_ref (gfc_se * se)
1598 se->string_length = se->ss->string_length;
1599 gfc_conv_scalarized_array_ref (se, NULL);
1603 /* Build an array reference. se->expr already holds the array descriptor.
1604 This should be either a variable, indirect variable reference or component
1605 reference. For arrays which do not have a descriptor, se->expr will be
1606 the data pointer.
1607 a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
1609 void
1610 gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar)
1612 int n;
1613 tree index;
1614 tree tmp;
1615 tree stride;
1616 tree fault;
1617 gfc_se indexse;
1619 /* Handle scalarized references separately. */
1620 if (ar->type != AR_ELEMENT)
1622 gfc_conv_scalarized_array_ref (se, ar);
1623 return;
1626 index = gfc_index_zero_node;
1628 fault = gfc_index_zero_node;
1630 /* Calculate the offsets from all the dimensions. */
1631 for (n = 0; n < ar->dimen; n++)
1633 /* Calculate the index for this dimension. */
1634 gfc_init_se (&indexse, NULL);
1635 gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
1636 gfc_add_block_to_block (&se->pre, &indexse.pre);
1638 if (flag_bounds_check)
1640 /* Check array bounds. */
1641 tree cond;
1643 indexse.expr = gfc_evaluate_now (indexse.expr, &se->pre);
1645 tmp = gfc_conv_array_lbound (se->expr, n);
1646 cond = fold_build2 (LT_EXPR, boolean_type_node,
1647 indexse.expr, tmp);
1648 fault =
1649 fold_build2 (TRUTH_OR_EXPR, boolean_type_node, fault, cond);
1651 tmp = gfc_conv_array_ubound (se->expr, n);
1652 cond = fold_build2 (GT_EXPR, boolean_type_node,
1653 indexse.expr, tmp);
1654 fault =
1655 fold_build2 (TRUTH_OR_EXPR, boolean_type_node, fault, cond);
1658 /* Multiply the index by the stride. */
1659 stride = gfc_conv_array_stride (se->expr, n);
1660 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, indexse.expr,
1661 stride);
1663 /* And add it to the total. */
1664 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
1667 if (flag_bounds_check)
1668 gfc_trans_runtime_check (fault, gfc_strconst_fault, &se->pre);
1670 tmp = gfc_conv_array_offset (se->expr);
1671 if (!integer_zerop (tmp))
1672 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
1674 /* Access the calculated element. */
1675 tmp = gfc_conv_array_data (se->expr);
1676 tmp = gfc_build_indirect_ref (tmp);
1677 se->expr = gfc_build_array_ref (tmp, index);
1681 /* Generate the code to be executed immediately before entering a
1682 scalarization loop. */
1684 static void
1685 gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
1686 stmtblock_t * pblock)
1688 tree index;
1689 tree stride;
1690 gfc_ss_info *info;
1691 gfc_ss *ss;
1692 gfc_se se;
1693 int i;
1695 /* This code will be executed before entering the scalarization loop
1696 for this dimension. */
1697 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
1699 if ((ss->useflags & flag) == 0)
1700 continue;
1702 if (ss->type != GFC_SS_SECTION
1703 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
1704 && ss->type != GFC_SS_COMPONENT)
1705 continue;
1707 info = &ss->data.info;
1709 if (dim >= info->dimen)
1710 continue;
1712 if (dim == info->dimen - 1)
1714 /* For the outermost loop calculate the offset due to any
1715 elemental dimensions. It will have been initialized with the
1716 base offset of the array. */
1717 if (info->ref)
1719 for (i = 0; i < info->ref->u.ar.dimen; i++)
1721 if (info->ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
1722 continue;
1724 gfc_init_se (&se, NULL);
1725 se.loop = loop;
1726 se.expr = info->descriptor;
1727 stride = gfc_conv_array_stride (info->descriptor, i);
1728 index = gfc_conv_array_index_offset (&se, info, i, -1,
1729 &info->ref->u.ar,
1730 stride);
1731 gfc_add_block_to_block (pblock, &se.pre);
1733 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1734 info->offset, index);
1735 info->offset = gfc_evaluate_now (info->offset, pblock);
1738 i = loop->order[0];
1739 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
1741 else
1742 stride = gfc_conv_array_stride (info->descriptor, 0);
1744 /* Calculate the stride of the innermost loop. Hopefully this will
1745 allow the backend optimizers to do their stuff more effectively.
1747 info->stride0 = gfc_evaluate_now (stride, pblock);
1749 else
1751 /* Add the offset for the previous loop dimension. */
1752 gfc_array_ref *ar;
1754 if (info->ref)
1756 ar = &info->ref->u.ar;
1757 i = loop->order[dim + 1];
1759 else
1761 ar = NULL;
1762 i = dim + 1;
1765 gfc_init_se (&se, NULL);
1766 se.loop = loop;
1767 se.expr = info->descriptor;
1768 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
1769 index = gfc_conv_array_index_offset (&se, info, info->dim[i], i,
1770 ar, stride);
1771 gfc_add_block_to_block (pblock, &se.pre);
1772 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1773 info->offset, index);
1774 info->offset = gfc_evaluate_now (info->offset, pblock);
1777 /* Remember this offset for the second loop. */
1778 if (dim == loop->temp_dim - 1)
1779 info->saved_offset = info->offset;
1784 /* Start a scalarized expression. Creates a scope and declares loop
1785 variables. */
1787 void
1788 gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
1790 int dim;
1791 int n;
1792 int flags;
1794 gcc_assert (!loop->array_parameter);
1796 for (dim = loop->dimen - 1; dim >= 0; dim--)
1798 n = loop->order[dim];
1800 gfc_start_block (&loop->code[n]);
1802 /* Create the loop variable. */
1803 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
1805 if (dim < loop->temp_dim)
1806 flags = 3;
1807 else
1808 flags = 1;
1809 /* Calculate values that will be constant within this loop. */
1810 gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
1812 gfc_start_block (pbody);
1816 /* Generates the actual loop code for a scalarization loop. */
1818 static void
1819 gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
1820 stmtblock_t * pbody)
1822 stmtblock_t block;
1823 tree cond;
1824 tree tmp;
1825 tree loopbody;
1826 tree exit_label;
1828 loopbody = gfc_finish_block (pbody);
1830 /* Initialize the loopvar. */
1831 gfc_add_modify_expr (&loop->code[n], loop->loopvar[n], loop->from[n]);
1833 exit_label = gfc_build_label_decl (NULL_TREE);
1835 /* Generate the loop body. */
1836 gfc_init_block (&block);
1838 /* The exit condition. */
1839 cond = build2 (GT_EXPR, boolean_type_node, loop->loopvar[n], loop->to[n]);
1840 tmp = build1_v (GOTO_EXPR, exit_label);
1841 TREE_USED (exit_label) = 1;
1842 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
1843 gfc_add_expr_to_block (&block, tmp);
1845 /* The main body. */
1846 gfc_add_expr_to_block (&block, loopbody);
1848 /* Increment the loopvar. */
1849 tmp = build2 (PLUS_EXPR, gfc_array_index_type,
1850 loop->loopvar[n], gfc_index_one_node);
1851 gfc_add_modify_expr (&block, loop->loopvar[n], tmp);
1853 /* Build the loop. */
1854 tmp = gfc_finish_block (&block);
1855 tmp = build1_v (LOOP_EXPR, tmp);
1856 gfc_add_expr_to_block (&loop->code[n], tmp);
1858 /* Add the exit label. */
1859 tmp = build1_v (LABEL_EXPR, exit_label);
1860 gfc_add_expr_to_block (&loop->code[n], tmp);
1864 /* Finishes and generates the loops for a scalarized expression. */
1866 void
1867 gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
1869 int dim;
1870 int n;
1871 gfc_ss *ss;
1872 stmtblock_t *pblock;
1873 tree tmp;
1875 pblock = body;
1876 /* Generate the loops. */
1877 for (dim = 0; dim < loop->dimen; dim++)
1879 n = loop->order[dim];
1880 gfc_trans_scalarized_loop_end (loop, n, pblock);
1881 loop->loopvar[n] = NULL_TREE;
1882 pblock = &loop->code[n];
1885 tmp = gfc_finish_block (pblock);
1886 gfc_add_expr_to_block (&loop->pre, tmp);
1888 /* Clear all the used flags. */
1889 for (ss = loop->ss; ss; ss = ss->loop_chain)
1890 ss->useflags = 0;
1894 /* Finish the main body of a scalarized expression, and start the secondary
1895 copying body. */
1897 void
1898 gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
1900 int dim;
1901 int n;
1902 stmtblock_t *pblock;
1903 gfc_ss *ss;
1905 pblock = body;
1906 /* We finish as many loops as are used by the temporary. */
1907 for (dim = 0; dim < loop->temp_dim - 1; dim++)
1909 n = loop->order[dim];
1910 gfc_trans_scalarized_loop_end (loop, n, pblock);
1911 loop->loopvar[n] = NULL_TREE;
1912 pblock = &loop->code[n];
1915 /* We don't want to finish the outermost loop entirely. */
1916 n = loop->order[loop->temp_dim - 1];
1917 gfc_trans_scalarized_loop_end (loop, n, pblock);
1919 /* Restore the initial offsets. */
1920 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
1922 if ((ss->useflags & 2) == 0)
1923 continue;
1925 if (ss->type != GFC_SS_SECTION
1926 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
1927 && ss->type != GFC_SS_COMPONENT)
1928 continue;
1930 ss->data.info.offset = ss->data.info.saved_offset;
1933 /* Restart all the inner loops we just finished. */
1934 for (dim = loop->temp_dim - 2; dim >= 0; dim--)
1936 n = loop->order[dim];
1938 gfc_start_block (&loop->code[n]);
1940 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
1942 gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
1945 /* Start a block for the secondary copying code. */
1946 gfc_start_block (body);
1950 /* Calculate the upper bound of an array section. */
1952 static tree
1953 gfc_conv_section_upper_bound (gfc_ss * ss, int n, stmtblock_t * pblock)
1955 int dim;
1956 gfc_ss *vecss;
1957 gfc_expr *end;
1958 tree desc;
1959 tree bound;
1960 gfc_se se;
1962 gcc_assert (ss->type == GFC_SS_SECTION);
1964 /* For vector array subscripts we want the size of the vector. */
1965 dim = ss->data.info.dim[n];
1966 vecss = ss;
1967 while (vecss->data.info.ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
1969 vecss = vecss->data.info.subscript[dim];
1970 gcc_assert (vecss && vecss->type == GFC_SS_VECTOR);
1971 dim = vecss->data.info.dim[0];
1974 gcc_assert (vecss->data.info.ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
1975 end = vecss->data.info.ref->u.ar.end[dim];
1976 desc = vecss->data.info.descriptor;
1978 if (end)
1980 /* The upper bound was specified. */
1981 gfc_init_se (&se, NULL);
1982 gfc_conv_expr_type (&se, end, gfc_array_index_type);
1983 gfc_add_block_to_block (pblock, &se.pre);
1984 bound = se.expr;
1986 else
1988 /* No upper bound was specified, so use the bound of the array. */
1989 bound = gfc_conv_array_ubound (desc, dim);
1992 return bound;
1996 /* Calculate the lower bound of an array section. */
1998 static void
1999 gfc_conv_section_startstride (gfc_loopinfo * loop, gfc_ss * ss, int n)
2001 gfc_expr *start;
2002 gfc_expr *stride;
2003 gfc_ss *vecss;
2004 tree desc;
2005 gfc_se se;
2006 gfc_ss_info *info;
2007 int dim;
2009 info = &ss->data.info;
2011 dim = info->dim[n];
2013 /* For vector array subscripts we want the size of the vector. */
2014 vecss = ss;
2015 while (vecss->data.info.ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2017 vecss = vecss->data.info.subscript[dim];
2018 gcc_assert (vecss && vecss->type == GFC_SS_VECTOR);
2019 /* Get the descriptors for the vector subscripts as well. */
2020 if (!vecss->data.info.descriptor)
2021 gfc_conv_ss_descriptor (&loop->pre, vecss, !loop->array_parameter);
2022 dim = vecss->data.info.dim[0];
2025 gcc_assert (vecss->data.info.ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2026 start = vecss->data.info.ref->u.ar.start[dim];
2027 stride = vecss->data.info.ref->u.ar.stride[dim];
2028 desc = vecss->data.info.descriptor;
2030 /* Calculate the start of the range. For vector subscripts this will
2031 be the range of the vector. */
2032 if (start)
2034 /* Specified section start. */
2035 gfc_init_se (&se, NULL);
2036 gfc_conv_expr_type (&se, start, gfc_array_index_type);
2037 gfc_add_block_to_block (&loop->pre, &se.pre);
2038 info->start[n] = se.expr;
2040 else
2042 /* No lower bound specified so use the bound of the array. */
2043 info->start[n] = gfc_conv_array_lbound (desc, dim);
2045 info->start[n] = gfc_evaluate_now (info->start[n], &loop->pre);
2047 /* Calculate the stride. */
2048 if (stride == NULL)
2049 info->stride[n] = gfc_index_one_node;
2050 else
2052 gfc_init_se (&se, NULL);
2053 gfc_conv_expr_type (&se, stride, gfc_array_index_type);
2054 gfc_add_block_to_block (&loop->pre, &se.pre);
2055 info->stride[n] = gfc_evaluate_now (se.expr, &loop->pre);
2060 /* Calculates the range start and stride for a SS chain. Also gets the
2061 descriptor and data pointer. The range of vector subscripts is the size
2062 of the vector. Array bounds are also checked. */
2064 void
2065 gfc_conv_ss_startstride (gfc_loopinfo * loop)
2067 int n;
2068 tree tmp;
2069 gfc_ss *ss;
2070 gfc_ss *vecss;
2071 tree desc;
2073 loop->dimen = 0;
2074 /* Determine the rank of the loop. */
2075 for (ss = loop->ss;
2076 ss != gfc_ss_terminator && loop->dimen == 0; ss = ss->loop_chain)
2078 switch (ss->type)
2080 case GFC_SS_SECTION:
2081 case GFC_SS_CONSTRUCTOR:
2082 case GFC_SS_FUNCTION:
2083 case GFC_SS_COMPONENT:
2084 loop->dimen = ss->data.info.dimen;
2085 break;
2087 default:
2088 break;
2092 if (loop->dimen == 0)
2093 gfc_todo_error ("Unable to determine rank of expression");
2096 /* Loop over all the SS in the chain. */
2097 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2099 if (ss->expr && ss->expr->shape && !ss->shape)
2100 ss->shape = ss->expr->shape;
2102 switch (ss->type)
2104 case GFC_SS_SECTION:
2105 /* Get the descriptor for the array. */
2106 gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
2108 for (n = 0; n < ss->data.info.dimen; n++)
2109 gfc_conv_section_startstride (loop, ss, n);
2110 break;
2112 case GFC_SS_CONSTRUCTOR:
2113 case GFC_SS_FUNCTION:
2114 for (n = 0; n < ss->data.info.dimen; n++)
2116 ss->data.info.start[n] = gfc_index_zero_node;
2117 ss->data.info.stride[n] = gfc_index_one_node;
2119 break;
2121 default:
2122 break;
2126 /* The rest is just runtime bound checking. */
2127 if (flag_bounds_check)
2129 stmtblock_t block;
2130 tree fault;
2131 tree bound;
2132 tree end;
2133 tree size[GFC_MAX_DIMENSIONS];
2134 gfc_ss_info *info;
2135 int dim;
2137 gfc_start_block (&block);
2139 fault = integer_zero_node;
2140 for (n = 0; n < loop->dimen; n++)
2141 size[n] = NULL_TREE;
2143 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2145 if (ss->type != GFC_SS_SECTION)
2146 continue;
2148 /* TODO: range checking for mapped dimensions. */
2149 info = &ss->data.info;
2151 /* This only checks scalarized dimensions, elemental dimensions are
2152 checked later. */
2153 for (n = 0; n < loop->dimen; n++)
2155 dim = info->dim[n];
2156 vecss = ss;
2157 while (vecss->data.info.ref->u.ar.dimen_type[dim]
2158 == DIMEN_VECTOR)
2160 vecss = vecss->data.info.subscript[dim];
2161 gcc_assert (vecss && vecss->type == GFC_SS_VECTOR);
2162 dim = vecss->data.info.dim[0];
2164 gcc_assert (vecss->data.info.ref->u.ar.dimen_type[dim]
2165 == DIMEN_RANGE);
2166 desc = vecss->data.info.descriptor;
2168 /* Check lower bound. */
2169 bound = gfc_conv_array_lbound (desc, dim);
2170 tmp = info->start[n];
2171 tmp = fold_build2 (LT_EXPR, boolean_type_node, tmp, bound);
2172 fault = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, fault,
2173 tmp);
2175 /* Check the upper bound. */
2176 bound = gfc_conv_array_ubound (desc, dim);
2177 end = gfc_conv_section_upper_bound (ss, n, &block);
2178 tmp = fold_build2 (GT_EXPR, boolean_type_node, end, bound);
2179 fault = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, fault,
2180 tmp);
2182 /* Check the section sizes match. */
2183 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
2184 info->start[n]);
2185 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type, tmp,
2186 info->stride[n]);
2187 /* We remember the size of the first section, and check all the
2188 others against this. */
2189 if (size[n])
2191 tmp =
2192 fold_build2 (NE_EXPR, boolean_type_node, tmp, size[n]);
2193 fault =
2194 build2 (TRUTH_OR_EXPR, boolean_type_node, fault, tmp);
2196 else
2197 size[n] = gfc_evaluate_now (tmp, &block);
2200 gfc_trans_runtime_check (fault, gfc_strconst_bounds, &block);
2202 tmp = gfc_finish_block (&block);
2203 gfc_add_expr_to_block (&loop->pre, tmp);
2208 /* Return true if the two SS could be aliased, i.e. both point to the same data
2209 object. */
2210 /* TODO: resolve aliases based on frontend expressions. */
2212 static int
2213 gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
2215 gfc_ref *lref;
2216 gfc_ref *rref;
2217 gfc_symbol *lsym;
2218 gfc_symbol *rsym;
2220 lsym = lss->expr->symtree->n.sym;
2221 rsym = rss->expr->symtree->n.sym;
2222 if (gfc_symbols_could_alias (lsym, rsym))
2223 return 1;
2225 if (rsym->ts.type != BT_DERIVED
2226 && lsym->ts.type != BT_DERIVED)
2227 return 0;
2229 /* For derived types we must check all the component types. We can ignore
2230 array references as these will have the same base type as the previous
2231 component ref. */
2232 for (lref = lss->expr->ref; lref != lss->data.info.ref; lref = lref->next)
2234 if (lref->type != REF_COMPONENT)
2235 continue;
2237 if (gfc_symbols_could_alias (lref->u.c.sym, rsym))
2238 return 1;
2240 for (rref = rss->expr->ref; rref != rss->data.info.ref;
2241 rref = rref->next)
2243 if (rref->type != REF_COMPONENT)
2244 continue;
2246 if (gfc_symbols_could_alias (lref->u.c.sym, rref->u.c.sym))
2247 return 1;
2251 for (rref = rss->expr->ref; rref != rss->data.info.ref; rref = rref->next)
2253 if (rref->type != REF_COMPONENT)
2254 break;
2256 if (gfc_symbols_could_alias (rref->u.c.sym, lsym))
2257 return 1;
2260 return 0;
2264 /* Resolve array data dependencies. Creates a temporary if required. */
2265 /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
2266 dependency.c. */
2268 void
2269 gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
2270 gfc_ss * rss)
2272 gfc_ss *ss;
2273 gfc_ref *lref;
2274 gfc_ref *rref;
2275 gfc_ref *aref;
2276 int nDepend = 0;
2277 int temp_dim = 0;
2279 loop->temp_ss = NULL;
2280 aref = dest->data.info.ref;
2281 temp_dim = 0;
2283 for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
2285 if (ss->type != GFC_SS_SECTION)
2286 continue;
2288 if (gfc_could_be_alias (dest, ss))
2290 nDepend = 1;
2291 break;
2294 if (dest->expr->symtree->n.sym == ss->expr->symtree->n.sym)
2296 lref = dest->expr->ref;
2297 rref = ss->expr->ref;
2299 nDepend = gfc_dep_resolver (lref, rref);
2300 #if 0
2301 /* TODO : loop shifting. */
2302 if (nDepend == 1)
2304 /* Mark the dimensions for LOOP SHIFTING */
2305 for (n = 0; n < loop->dimen; n++)
2307 int dim = dest->data.info.dim[n];
2309 if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2310 depends[n] = 2;
2311 else if (! gfc_is_same_range (&lref->u.ar,
2312 &rref->u.ar, dim, 0))
2313 depends[n] = 1;
2316 /* Put all the dimensions with dependencies in the
2317 innermost loops. */
2318 dim = 0;
2319 for (n = 0; n < loop->dimen; n++)
2321 gcc_assert (loop->order[n] == n);
2322 if (depends[n])
2323 loop->order[dim++] = n;
2325 temp_dim = dim;
2326 for (n = 0; n < loop->dimen; n++)
2328 if (! depends[n])
2329 loop->order[dim++] = n;
2332 gcc_assert (dim == loop->dimen);
2333 break;
2335 #endif
2339 if (nDepend == 1)
2341 loop->temp_ss = gfc_get_ss ();
2342 loop->temp_ss->type = GFC_SS_TEMP;
2343 loop->temp_ss->data.temp.type =
2344 gfc_get_element_type (TREE_TYPE (dest->data.info.descriptor));
2345 loop->temp_ss->string_length = NULL_TREE;
2346 loop->temp_ss->data.temp.dimen = loop->dimen;
2347 loop->temp_ss->next = gfc_ss_terminator;
2348 gfc_add_ss_to_loop (loop, loop->temp_ss);
2350 else
2351 loop->temp_ss = NULL;
2355 /* Initialize the scalarization loop. Creates the loop variables. Determines
2356 the range of the loop variables. Creates a temporary if required.
2357 Calculates how to transform from loop variables to array indices for each
2358 expression. Also generates code for scalar expressions which have been
2359 moved outside the loop. */
2361 void
2362 gfc_conv_loop_setup (gfc_loopinfo * loop)
2364 int n;
2365 int dim;
2366 gfc_ss_info *info;
2367 gfc_ss_info *specinfo;
2368 gfc_ss *ss;
2369 tree tmp;
2370 tree len;
2371 gfc_ss *loopspec[GFC_MAX_DIMENSIONS];
2372 mpz_t *cshape;
2373 mpz_t i;
2375 mpz_init (i);
2376 for (n = 0; n < loop->dimen; n++)
2378 loopspec[n] = NULL;
2379 /* We use one SS term, and use that to determine the bounds of the
2380 loop for this dimension. We try to pick the simplest term. */
2381 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2383 if (ss->shape)
2385 /* The frontend has worked out the size for us. */
2386 loopspec[n] = ss;
2387 continue;
2390 if (ss->type == GFC_SS_CONSTRUCTOR)
2392 /* An unknown size constructor will always be rank one.
2393 Higher rank constructors will either have known shape,
2394 or still be wrapped in a call to reshape. */
2395 gcc_assert (loop->dimen == 1);
2396 /* Try to figure out the size of the constructor. */
2397 /* TODO: avoid this by making the frontend set the shape. */
2398 gfc_get_array_cons_size (&i, ss->expr->value.constructor);
2399 /* A negative value means we failed. */
2400 if (mpz_sgn (i) > 0)
2402 mpz_sub_ui (i, i, 1);
2403 loop->to[n] =
2404 gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
2405 loopspec[n] = ss;
2407 continue;
2410 /* TODO: Pick the best bound if we have a choice between a
2411 function and something else. */
2412 if (ss->type == GFC_SS_FUNCTION)
2414 loopspec[n] = ss;
2415 continue;
2418 if (ss->type != GFC_SS_SECTION)
2419 continue;
2421 if (loopspec[n])
2422 specinfo = &loopspec[n]->data.info;
2423 else
2424 specinfo = NULL;
2425 info = &ss->data.info;
2427 /* Criteria for choosing a loop specifier (most important first):
2428 stride of one
2429 known stride
2430 known lower bound
2431 known upper bound
2433 if (!specinfo)
2434 loopspec[n] = ss;
2435 /* TODO: Is != constructor correct? */
2436 else if (loopspec[n]->type != GFC_SS_CONSTRUCTOR)
2438 if (integer_onep (info->stride[n])
2439 && !integer_onep (specinfo->stride[n]))
2440 loopspec[n] = ss;
2441 else if (INTEGER_CST_P (info->stride[n])
2442 && !INTEGER_CST_P (specinfo->stride[n]))
2443 loopspec[n] = ss;
2444 else if (INTEGER_CST_P (info->start[n])
2445 && !INTEGER_CST_P (specinfo->start[n]))
2446 loopspec[n] = ss;
2447 /* We don't work out the upper bound.
2448 else if (INTEGER_CST_P (info->finish[n])
2449 && ! INTEGER_CST_P (specinfo->finish[n]))
2450 loopspec[n] = ss; */
2454 if (!loopspec[n])
2455 gfc_todo_error ("Unable to find scalarization loop specifier");
2457 info = &loopspec[n]->data.info;
2459 /* Set the extents of this range. */
2460 cshape = loopspec[n]->shape;
2461 if (cshape && INTEGER_CST_P (info->start[n])
2462 && INTEGER_CST_P (info->stride[n]))
2464 loop->from[n] = info->start[n];
2465 mpz_set (i, cshape[n]);
2466 mpz_sub_ui (i, i, 1);
2467 /* To = from + (size - 1) * stride. */
2468 tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
2469 if (!integer_onep (info->stride[n]))
2470 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
2471 tmp, info->stride[n]);
2472 loop->to[n] = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2473 loop->from[n], tmp);
2475 else
2477 loop->from[n] = info->start[n];
2478 switch (loopspec[n]->type)
2480 case GFC_SS_CONSTRUCTOR:
2481 gcc_assert (info->dimen == 1);
2482 gcc_assert (loop->to[n]);
2483 break;
2485 case GFC_SS_SECTION:
2486 loop->to[n] = gfc_conv_section_upper_bound (loopspec[n], n,
2487 &loop->pre);
2488 break;
2490 case GFC_SS_FUNCTION:
2491 /* The loop bound will be set when we generate the call. */
2492 gcc_assert (loop->to[n] == NULL_TREE);
2493 break;
2495 default:
2496 gcc_unreachable ();
2500 /* Transform everything so we have a simple incrementing variable. */
2501 if (integer_onep (info->stride[n]))
2502 info->delta[n] = gfc_index_zero_node;
2503 else
2505 /* Set the delta for this section. */
2506 info->delta[n] = gfc_evaluate_now (loop->from[n], &loop->pre);
2507 /* Number of iterations is (end - start + step) / step.
2508 with start = 0, this simplifies to
2509 last = end / step;
2510 for (i = 0; i<=last; i++){...}; */
2511 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2512 loop->to[n], loop->from[n]);
2513 tmp = fold_build2 (TRUNC_DIV_EXPR, gfc_array_index_type,
2514 tmp, info->stride[n]);
2515 loop->to[n] = gfc_evaluate_now (tmp, &loop->pre);
2516 /* Make the loop variable start at 0. */
2517 loop->from[n] = gfc_index_zero_node;
2521 /* Add all the scalar code that can be taken out of the loops.
2522 This may include calculating the loop bounds, so do it before
2523 allocating the temporary. */
2524 gfc_add_loop_ss_code (loop, loop->ss, false);
2526 /* If we want a temporary then create it. */
2527 if (loop->temp_ss != NULL)
2529 gcc_assert (loop->temp_ss->type == GFC_SS_TEMP);
2530 tmp = loop->temp_ss->data.temp.type;
2531 len = loop->temp_ss->string_length;
2532 n = loop->temp_ss->data.temp.dimen;
2533 memset (&loop->temp_ss->data.info, 0, sizeof (gfc_ss_info));
2534 loop->temp_ss->type = GFC_SS_SECTION;
2535 loop->temp_ss->data.info.dimen = n;
2536 gfc_trans_allocate_temp_array (loop, &loop->temp_ss->data.info, tmp);
2539 for (n = 0; n < loop->temp_dim; n++)
2540 loopspec[loop->order[n]] = NULL;
2542 mpz_clear (i);
2544 /* For array parameters we don't have loop variables, so don't calculate the
2545 translations. */
2546 if (loop->array_parameter)
2547 return;
2549 /* Calculate the translation from loop variables to array indices. */
2550 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2552 if (ss->type != GFC_SS_SECTION && ss->type != GFC_SS_COMPONENT)
2553 continue;
2555 info = &ss->data.info;
2557 for (n = 0; n < info->dimen; n++)
2559 dim = info->dim[n];
2561 /* If we are specifying the range the delta is already set. */
2562 if (loopspec[n] != ss)
2564 /* Calculate the offset relative to the loop variable.
2565 First multiply by the stride. */
2566 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
2567 loop->from[n], info->stride[n]);
2569 /* Then subtract this from our starting value. */
2570 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2571 info->start[n], tmp);
2573 info->delta[n] = gfc_evaluate_now (tmp, &loop->pre);
2580 /* Fills in an array descriptor, and returns the size of the array. The size
2581 will be a simple_val, ie a variable or a constant. Also calculates the
2582 offset of the base. Returns the size of the array.
2584 stride = 1;
2585 offset = 0;
2586 for (n = 0; n < rank; n++)
2588 a.lbound[n] = specified_lower_bound;
2589 offset = offset + a.lbond[n] * stride;
2590 size = 1 - lbound;
2591 a.ubound[n] = specified_upper_bound;
2592 a.stride[n] = stride;
2593 size = ubound + size; //size = ubound + 1 - lbound
2594 stride = stride * size;
2596 return (stride);
2597 } */
2598 /*GCC ARRAYS*/
2600 static tree
2601 gfc_array_init_size (tree descriptor, int rank, tree * poffset,
2602 gfc_expr ** lower, gfc_expr ** upper,
2603 stmtblock_t * pblock)
2605 tree type;
2606 tree tmp;
2607 tree size;
2608 tree offset;
2609 tree stride;
2610 gfc_expr *ubound;
2611 gfc_se se;
2612 int n;
2614 type = TREE_TYPE (descriptor);
2616 stride = gfc_index_one_node;
2617 offset = gfc_index_zero_node;
2619 /* Set the dtype. */
2620 tmp = gfc_conv_descriptor_dtype (descriptor);
2621 gfc_add_modify_expr (pblock, tmp, gfc_get_dtype (TREE_TYPE (descriptor)));
2623 for (n = 0; n < rank; n++)
2625 /* We have 3 possibilities for determining the size of the array:
2626 lower == NULL => lbound = 1, ubound = upper[n]
2627 upper[n] = NULL => lbound = 1, ubound = lower[n]
2628 upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
2629 ubound = upper[n];
2631 /* Set lower bound. */
2632 gfc_init_se (&se, NULL);
2633 if (lower == NULL)
2634 se.expr = gfc_index_one_node;
2635 else
2637 gcc_assert (lower[n]);
2638 if (ubound)
2640 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
2641 gfc_add_block_to_block (pblock, &se.pre);
2643 else
2645 se.expr = gfc_index_one_node;
2646 ubound = lower[n];
2649 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[n]);
2650 gfc_add_modify_expr (pblock, tmp, se.expr);
2652 /* Work out the offset for this component. */
2653 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, se.expr, stride);
2654 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
2656 /* Start the calculation for the size of this dimension. */
2657 size = build2 (MINUS_EXPR, gfc_array_index_type,
2658 gfc_index_one_node, se.expr);
2660 /* Set upper bound. */
2661 gfc_init_se (&se, NULL);
2662 gcc_assert (ubound);
2663 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
2664 gfc_add_block_to_block (pblock, &se.pre);
2666 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[n]);
2667 gfc_add_modify_expr (pblock, tmp, se.expr);
2669 /* Store the stride. */
2670 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[n]);
2671 gfc_add_modify_expr (pblock, tmp, stride);
2673 /* Calculate the size of this dimension. */
2674 size = fold_build2 (PLUS_EXPR, gfc_array_index_type, se.expr, size);
2676 /* Multiply the stride by the number of elements in this dimension. */
2677 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, size);
2678 stride = gfc_evaluate_now (stride, pblock);
2681 /* The stride is the number of elements in the array, so multiply by the
2682 size of an element to get the total size. */
2683 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
2684 size = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, tmp);
2686 if (poffset != NULL)
2688 offset = gfc_evaluate_now (offset, pblock);
2689 *poffset = offset;
2692 size = gfc_evaluate_now (size, pblock);
2693 return size;
2697 /* Initializes the descriptor and generates a call to _gfor_allocate. Does
2698 the work for an ALLOCATE statement. */
2699 /*GCC ARRAYS*/
2701 void
2702 gfc_array_allocate (gfc_se * se, gfc_ref * ref, tree pstat)
2704 tree tmp;
2705 tree pointer;
2706 tree allocate;
2707 tree offset;
2708 tree size;
2709 gfc_expr **lower;
2710 gfc_expr **upper;
2712 /* Figure out the size of the array. */
2713 switch (ref->u.ar.type)
2715 case AR_ELEMENT:
2716 lower = NULL;
2717 upper = ref->u.ar.start;
2718 break;
2720 case AR_FULL:
2721 gcc_assert (ref->u.ar.as->type == AS_EXPLICIT);
2723 lower = ref->u.ar.as->lower;
2724 upper = ref->u.ar.as->upper;
2725 break;
2727 case AR_SECTION:
2728 lower = ref->u.ar.start;
2729 upper = ref->u.ar.end;
2730 break;
2732 default:
2733 gcc_unreachable ();
2734 break;
2737 size = gfc_array_init_size (se->expr, ref->u.ar.as->rank, &offset,
2738 lower, upper, &se->pre);
2740 /* Allocate memory to store the data. */
2741 tmp = gfc_conv_descriptor_data (se->expr);
2742 pointer = gfc_build_addr_expr (NULL, tmp);
2743 pointer = gfc_evaluate_now (pointer, &se->pre);
2745 if (TYPE_PRECISION (gfc_array_index_type) == 32)
2746 allocate = gfor_fndecl_allocate;
2747 else if (TYPE_PRECISION (gfc_array_index_type) == 64)
2748 allocate = gfor_fndecl_allocate64;
2749 else
2750 gcc_unreachable ();
2752 tmp = gfc_chainon_list (NULL_TREE, pointer);
2753 tmp = gfc_chainon_list (tmp, size);
2754 tmp = gfc_chainon_list (tmp, pstat);
2755 tmp = gfc_build_function_call (allocate, tmp);
2756 gfc_add_expr_to_block (&se->pre, tmp);
2758 pointer = gfc_conv_descriptor_data (se->expr);
2760 tmp = gfc_conv_descriptor_offset (se->expr);
2761 gfc_add_modify_expr (&se->pre, tmp, offset);
2765 /* Deallocate an array variable. Also used when an allocated variable goes
2766 out of scope. */
2767 /*GCC ARRAYS*/
2769 tree
2770 gfc_array_deallocate (tree descriptor)
2772 tree var;
2773 tree tmp;
2774 stmtblock_t block;
2776 gfc_start_block (&block);
2777 /* Get a pointer to the data. */
2778 tmp = gfc_conv_descriptor_data (descriptor);
2779 tmp = gfc_build_addr_expr (NULL, tmp);
2780 var = gfc_create_var (TREE_TYPE (tmp), "ptr");
2781 gfc_add_modify_expr (&block, var, tmp);
2783 /* Parameter is the address of the data component. */
2784 tmp = gfc_chainon_list (NULL_TREE, var);
2785 tmp = gfc_chainon_list (tmp, integer_zero_node);
2786 tmp = gfc_build_function_call (gfor_fndecl_deallocate, tmp);
2787 gfc_add_expr_to_block (&block, tmp);
2789 return gfc_finish_block (&block);
2793 /* Create an array constructor from an initialization expression.
2794 We assume the frontend already did any expansions and conversions. */
2796 tree
2797 gfc_conv_array_initializer (tree type, gfc_expr * expr)
2799 gfc_constructor *c;
2800 tree list;
2801 tree tmp;
2802 mpz_t maxval;
2803 gfc_se se;
2804 HOST_WIDE_INT hi;
2805 unsigned HOST_WIDE_INT lo;
2806 tree index, range;
2808 list = NULL_TREE;
2809 switch (expr->expr_type)
2811 case EXPR_CONSTANT:
2812 case EXPR_STRUCTURE:
2813 /* A single scalar or derived type value. Create an array with all
2814 elements equal to that value. */
2815 gfc_init_se (&se, NULL);
2817 if (expr->expr_type == EXPR_CONSTANT)
2818 gfc_conv_constant (&se, expr);
2819 else
2820 gfc_conv_structure (&se, expr, 1);
2822 tmp = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2823 gcc_assert (tmp && INTEGER_CST_P (tmp));
2824 hi = TREE_INT_CST_HIGH (tmp);
2825 lo = TREE_INT_CST_LOW (tmp);
2826 lo++;
2827 if (lo == 0)
2828 hi++;
2829 /* This will probably eat buckets of memory for large arrays. */
2830 while (hi != 0 || lo != 0)
2832 list = tree_cons (NULL_TREE, se.expr, list);
2833 if (lo == 0)
2834 hi--;
2835 lo--;
2837 break;
2839 case EXPR_ARRAY:
2840 /* Create a list of all the elements. */
2841 for (c = expr->value.constructor; c; c = c->next)
2843 if (c->iterator)
2845 /* Problems occur when we get something like
2846 integer :: a(lots) = (/(i, i=1,lots)/) */
2847 /* TODO: Unexpanded array initializers. */
2848 internal_error
2849 ("Possible frontend bug: array constructor not expanded");
2851 if (mpz_cmp_si (c->n.offset, 0) != 0)
2852 index = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
2853 else
2854 index = NULL_TREE;
2855 mpz_init (maxval);
2856 if (mpz_cmp_si (c->repeat, 0) != 0)
2858 tree tmp1, tmp2;
2860 mpz_set (maxval, c->repeat);
2861 mpz_add (maxval, c->n.offset, maxval);
2862 mpz_sub_ui (maxval, maxval, 1);
2863 tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
2864 if (mpz_cmp_si (c->n.offset, 0) != 0)
2866 mpz_add_ui (maxval, c->n.offset, 1);
2867 tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
2869 else
2870 tmp1 = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
2872 range = build2 (RANGE_EXPR, integer_type_node, tmp1, tmp2);
2874 else
2875 range = NULL;
2876 mpz_clear (maxval);
2878 gfc_init_se (&se, NULL);
2879 switch (c->expr->expr_type)
2881 case EXPR_CONSTANT:
2882 gfc_conv_constant (&se, c->expr);
2883 if (range == NULL_TREE)
2884 list = tree_cons (index, se.expr, list);
2885 else
2887 if (index != NULL_TREE)
2888 list = tree_cons (index, se.expr, list);
2889 list = tree_cons (range, se.expr, list);
2891 break;
2893 case EXPR_STRUCTURE:
2894 gfc_conv_structure (&se, c->expr, 1);
2895 list = tree_cons (index, se.expr, list);
2896 break;
2898 default:
2899 gcc_unreachable ();
2902 /* We created the list in reverse order. */
2903 list = nreverse (list);
2904 break;
2906 default:
2907 gcc_unreachable ();
2910 /* Create a constructor from the list of elements. */
2911 tmp = build1 (CONSTRUCTOR, type, list);
2912 TREE_CONSTANT (tmp) = 1;
2913 TREE_INVARIANT (tmp) = 1;
2914 return tmp;
2918 /* Generate code to evaluate non-constant array bounds. Sets *poffset and
2919 returns the size (in elements) of the array. */
2921 static tree
2922 gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
2923 stmtblock_t * pblock)
2925 gfc_array_spec *as;
2926 tree size;
2927 tree stride;
2928 tree offset;
2929 tree ubound;
2930 tree lbound;
2931 tree tmp;
2932 gfc_se se;
2934 int dim;
2936 as = sym->as;
2938 size = gfc_index_one_node;
2939 offset = gfc_index_zero_node;
2940 for (dim = 0; dim < as->rank; dim++)
2942 /* Evaluate non-constant array bound expressions. */
2943 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
2944 if (as->lower[dim] && !INTEGER_CST_P (lbound))
2946 gfc_init_se (&se, NULL);
2947 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
2948 gfc_add_block_to_block (pblock, &se.pre);
2949 gfc_add_modify_expr (pblock, lbound, se.expr);
2951 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
2952 if (as->upper[dim] && !INTEGER_CST_P (ubound))
2954 gfc_init_se (&se, NULL);
2955 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
2956 gfc_add_block_to_block (pblock, &se.pre);
2957 gfc_add_modify_expr (pblock, ubound, se.expr);
2959 /* The offset of this dimension. offset = offset - lbound * stride. */
2960 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, size);
2961 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
2963 /* The size of this dimension, and the stride of the next. */
2964 if (dim + 1 < as->rank)
2965 stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
2966 else
2967 stride = NULL_TREE;
2969 if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
2971 /* Calculate stride = size * (ubound + 1 - lbound). */
2972 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2973 gfc_index_one_node, lbound);
2974 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, tmp);
2975 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
2976 if (stride)
2977 gfc_add_modify_expr (pblock, stride, tmp);
2978 else
2979 stride = gfc_evaluate_now (tmp, pblock);
2982 size = stride;
2985 *poffset = offset;
2986 return size;
2990 /* Generate code to initialize/allocate an array variable. */
2992 tree
2993 gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym, tree fnbody)
2995 stmtblock_t block;
2996 tree type;
2997 tree tmp;
2998 tree fndecl;
2999 tree size;
3000 tree offset;
3001 bool onstack;
3003 gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
3005 /* Do nothing for USEd variables. */
3006 if (sym->attr.use_assoc)
3007 return fnbody;
3009 type = TREE_TYPE (decl);
3010 gcc_assert (GFC_ARRAY_TYPE_P (type));
3011 onstack = TREE_CODE (type) != POINTER_TYPE;
3013 gfc_start_block (&block);
3015 /* Evaluate character string length. */
3016 if (sym->ts.type == BT_CHARACTER
3017 && onstack && !INTEGER_CST_P (sym->ts.cl->backend_decl))
3019 gfc_trans_init_string_length (sym->ts.cl, &block);
3021 /* Emit a DECL_EXPR for this variable, which will cause the
3022 gimplifier to allocate storage, and all that good stuff. */
3023 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
3024 gfc_add_expr_to_block (&block, tmp);
3027 if (onstack)
3029 gfc_add_expr_to_block (&block, fnbody);
3030 return gfc_finish_block (&block);
3033 type = TREE_TYPE (type);
3035 gcc_assert (!sym->attr.use_assoc);
3036 gcc_assert (!TREE_STATIC (decl));
3037 gcc_assert (!sym->module);
3039 if (sym->ts.type == BT_CHARACTER
3040 && !INTEGER_CST_P (sym->ts.cl->backend_decl))
3041 gfc_trans_init_string_length (sym->ts.cl, &block);
3043 size = gfc_trans_array_bounds (type, sym, &offset, &block);
3045 /* The size is the number of elements in the array, so multiply by the
3046 size of an element to get the total size. */
3047 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3048 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
3050 /* Allocate memory to hold the data. */
3051 tmp = gfc_chainon_list (NULL_TREE, size);
3053 if (gfc_index_integer_kind == 4)
3054 fndecl = gfor_fndecl_internal_malloc;
3055 else if (gfc_index_integer_kind == 8)
3056 fndecl = gfor_fndecl_internal_malloc64;
3057 else
3058 gcc_unreachable ();
3059 tmp = gfc_build_function_call (fndecl, tmp);
3060 tmp = fold (convert (TREE_TYPE (decl), tmp));
3061 gfc_add_modify_expr (&block, decl, tmp);
3063 /* Set offset of the array. */
3064 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3065 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3068 /* Automatic arrays should not have initializers. */
3069 gcc_assert (!sym->value);
3071 gfc_add_expr_to_block (&block, fnbody);
3073 /* Free the temporary. */
3074 tmp = convert (pvoid_type_node, decl);
3075 tmp = gfc_chainon_list (NULL_TREE, tmp);
3076 tmp = gfc_build_function_call (gfor_fndecl_internal_free, tmp);
3077 gfc_add_expr_to_block (&block, tmp);
3079 return gfc_finish_block (&block);
3083 /* Generate entry and exit code for g77 calling convention arrays. */
3085 tree
3086 gfc_trans_g77_array (gfc_symbol * sym, tree body)
3088 tree parm;
3089 tree type;
3090 locus loc;
3091 tree offset;
3092 tree tmp;
3093 stmtblock_t block;
3095 gfc_get_backend_locus (&loc);
3096 gfc_set_backend_locus (&sym->declared_at);
3098 /* Descriptor type. */
3099 parm = sym->backend_decl;
3100 type = TREE_TYPE (parm);
3101 gcc_assert (GFC_ARRAY_TYPE_P (type));
3103 gfc_start_block (&block);
3105 if (sym->ts.type == BT_CHARACTER
3106 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
3107 gfc_trans_init_string_length (sym->ts.cl, &block);
3109 /* Evaluate the bounds of the array. */
3110 gfc_trans_array_bounds (type, sym, &offset, &block);
3112 /* Set the offset. */
3113 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3114 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3116 /* Set the pointer itself if we aren't using the parameter directly. */
3117 if (TREE_CODE (parm) != PARM_DECL)
3119 tmp = convert (TREE_TYPE (parm), GFC_DECL_SAVED_DESCRIPTOR (parm));
3120 gfc_add_modify_expr (&block, parm, tmp);
3122 tmp = gfc_finish_block (&block);
3124 gfc_set_backend_locus (&loc);
3126 gfc_start_block (&block);
3127 /* Add the initialization code to the start of the function. */
3128 gfc_add_expr_to_block (&block, tmp);
3129 gfc_add_expr_to_block (&block, body);
3131 return gfc_finish_block (&block);
3135 /* Modify the descriptor of an array parameter so that it has the
3136 correct lower bound. Also move the upper bound accordingly.
3137 If the array is not packed, it will be copied into a temporary.
3138 For each dimension we set the new lower and upper bounds. Then we copy the
3139 stride and calculate the offset for this dimension. We also work out
3140 what the stride of a packed array would be, and see it the two match.
3141 If the array need repacking, we set the stride to the values we just
3142 calculated, recalculate the offset and copy the array data.
3143 Code is also added to copy the data back at the end of the function.
3146 tree
3147 gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc, tree body)
3149 tree size;
3150 tree type;
3151 tree offset;
3152 locus loc;
3153 stmtblock_t block;
3154 stmtblock_t cleanup;
3155 tree lbound;
3156 tree ubound;
3157 tree dubound;
3158 tree dlbound;
3159 tree dumdesc;
3160 tree tmp;
3161 tree stmt;
3162 tree stride;
3163 tree stmt_packed;
3164 tree stmt_unpacked;
3165 tree partial;
3166 gfc_se se;
3167 int n;
3168 int checkparm;
3169 int no_repack;
3170 bool optional_arg;
3172 /* Do nothing for pointer and allocatable arrays. */
3173 if (sym->attr.pointer || sym->attr.allocatable)
3174 return body;
3176 if (sym->attr.dummy && gfc_is_nodesc_array (sym))
3177 return gfc_trans_g77_array (sym, body);
3179 gfc_get_backend_locus (&loc);
3180 gfc_set_backend_locus (&sym->declared_at);
3182 /* Descriptor type. */
3183 type = TREE_TYPE (tmpdesc);
3184 gcc_assert (GFC_ARRAY_TYPE_P (type));
3185 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
3186 dumdesc = gfc_build_indirect_ref (dumdesc);
3187 gfc_start_block (&block);
3189 if (sym->ts.type == BT_CHARACTER
3190 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
3191 gfc_trans_init_string_length (sym->ts.cl, &block);
3193 checkparm = (sym->as->type == AS_EXPLICIT && flag_bounds_check);
3195 no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
3196 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
3198 if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
3200 /* For non-constant shape arrays we only check if the first dimension
3201 is contiguous. Repacking higher dimensions wouldn't gain us
3202 anything as we still don't know the array stride. */
3203 partial = gfc_create_var (boolean_type_node, "partial");
3204 TREE_USED (partial) = 1;
3205 tmp = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
3206 tmp = fold_build2 (EQ_EXPR, boolean_type_node, tmp, integer_one_node);
3207 gfc_add_modify_expr (&block, partial, tmp);
3209 else
3211 partial = NULL_TREE;
3214 /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
3215 here, however I think it does the right thing. */
3216 if (no_repack)
3218 /* Set the first stride. */
3219 stride = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
3220 stride = gfc_evaluate_now (stride, &block);
3222 tmp = build2 (EQ_EXPR, boolean_type_node, stride, integer_zero_node);
3223 tmp = build3 (COND_EXPR, gfc_array_index_type, tmp,
3224 gfc_index_one_node, stride);
3225 stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
3226 gfc_add_modify_expr (&block, stride, tmp);
3228 /* Allow the user to disable array repacking. */
3229 stmt_unpacked = NULL_TREE;
3231 else
3233 gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
3234 /* A library call to repack the array if necessary. */
3235 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
3236 tmp = gfc_chainon_list (NULL_TREE, tmp);
3237 stmt_unpacked = gfc_build_function_call (gfor_fndecl_in_pack, tmp);
3239 stride = gfc_index_one_node;
3242 /* This is for the case where the array data is used directly without
3243 calling the repack function. */
3244 if (no_repack || partial != NULL_TREE)
3245 stmt_packed = gfc_conv_descriptor_data (dumdesc);
3246 else
3247 stmt_packed = NULL_TREE;
3249 /* Assign the data pointer. */
3250 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
3252 /* Don't repack unknown shape arrays when the first stride is 1. */
3253 tmp = build3 (COND_EXPR, TREE_TYPE (stmt_packed), partial,
3254 stmt_packed, stmt_unpacked);
3256 else
3257 tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
3258 gfc_add_modify_expr (&block, tmpdesc, fold_convert (type, tmp));
3260 offset = gfc_index_zero_node;
3261 size = gfc_index_one_node;
3263 /* Evaluate the bounds of the array. */
3264 for (n = 0; n < sym->as->rank; n++)
3266 if (checkparm || !sym->as->upper[n])
3268 /* Get the bounds of the actual parameter. */
3269 dubound = gfc_conv_descriptor_ubound (dumdesc, gfc_rank_cst[n]);
3270 dlbound = gfc_conv_descriptor_lbound (dumdesc, gfc_rank_cst[n]);
3272 else
3274 dubound = NULL_TREE;
3275 dlbound = NULL_TREE;
3278 lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
3279 if (!INTEGER_CST_P (lbound))
3281 gfc_init_se (&se, NULL);
3282 gfc_conv_expr_type (&se, sym->as->upper[n],
3283 gfc_array_index_type);
3284 gfc_add_block_to_block (&block, &se.pre);
3285 gfc_add_modify_expr (&block, lbound, se.expr);
3288 ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
3289 /* Set the desired upper bound. */
3290 if (sym->as->upper[n])
3292 /* We know what we want the upper bound to be. */
3293 if (!INTEGER_CST_P (ubound))
3295 gfc_init_se (&se, NULL);
3296 gfc_conv_expr_type (&se, sym->as->upper[n],
3297 gfc_array_index_type);
3298 gfc_add_block_to_block (&block, &se.pre);
3299 gfc_add_modify_expr (&block, ubound, se.expr);
3302 /* Check the sizes match. */
3303 if (checkparm)
3305 /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
3307 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3308 ubound, lbound);
3309 stride = build2 (MINUS_EXPR, gfc_array_index_type,
3310 dubound, dlbound);
3311 tmp = fold_build2 (NE_EXPR, gfc_array_index_type, tmp, stride);
3312 gfc_trans_runtime_check (tmp, gfc_strconst_bounds, &block);
3315 else
3317 /* For assumed shape arrays move the upper bound by the same amount
3318 as the lower bound. */
3319 tmp = build2 (MINUS_EXPR, gfc_array_index_type, dubound, dlbound);
3320 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, lbound);
3321 gfc_add_modify_expr (&block, ubound, tmp);
3323 /* The offset of this dimension. offset = offset - lbound * stride. */
3324 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, stride);
3325 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
3327 /* The size of this dimension, and the stride of the next. */
3328 if (n + 1 < sym->as->rank)
3330 stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
3332 if (no_repack || partial != NULL_TREE)
3334 stmt_unpacked =
3335 gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[n+1]);
3338 /* Figure out the stride if not a known constant. */
3339 if (!INTEGER_CST_P (stride))
3341 if (no_repack)
3342 stmt_packed = NULL_TREE;
3343 else
3345 /* Calculate stride = size * (ubound + 1 - lbound). */
3346 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3347 gfc_index_one_node, lbound);
3348 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
3349 ubound, tmp);
3350 size = fold_build2 (MULT_EXPR, gfc_array_index_type,
3351 size, tmp);
3352 stmt_packed = size;
3355 /* Assign the stride. */
3356 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
3357 tmp = build3 (COND_EXPR, gfc_array_index_type, partial,
3358 stmt_unpacked, stmt_packed);
3359 else
3360 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
3361 gfc_add_modify_expr (&block, stride, tmp);
3366 /* Set the offset. */
3367 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
3368 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
3370 stmt = gfc_finish_block (&block);
3372 gfc_start_block (&block);
3374 /* Only do the entry/initialization code if the arg is present. */
3375 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
3376 optional_arg = (sym->attr.optional
3377 || (sym->ns->proc_name->attr.entry_master
3378 && sym->attr.dummy));
3379 if (optional_arg)
3381 tmp = gfc_conv_expr_present (sym);
3382 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
3384 gfc_add_expr_to_block (&block, stmt);
3386 /* Add the main function body. */
3387 gfc_add_expr_to_block (&block, body);
3389 /* Cleanup code. */
3390 if (!no_repack)
3392 gfc_start_block (&cleanup);
3394 if (sym->attr.intent != INTENT_IN)
3396 /* Copy the data back. */
3397 tmp = gfc_chainon_list (NULL_TREE, dumdesc);
3398 tmp = gfc_chainon_list (tmp, tmpdesc);
3399 tmp = gfc_build_function_call (gfor_fndecl_in_unpack, tmp);
3400 gfc_add_expr_to_block (&cleanup, tmp);
3403 /* Free the temporary. */
3404 tmp = gfc_chainon_list (NULL_TREE, tmpdesc);
3405 tmp = gfc_build_function_call (gfor_fndecl_internal_free, tmp);
3406 gfc_add_expr_to_block (&cleanup, tmp);
3408 stmt = gfc_finish_block (&cleanup);
3410 /* Only do the cleanup if the array was repacked. */
3411 tmp = gfc_build_indirect_ref (dumdesc);
3412 tmp = gfc_conv_descriptor_data (tmp);
3413 tmp = build2 (NE_EXPR, boolean_type_node, tmp, tmpdesc);
3414 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
3416 if (optional_arg)
3418 tmp = gfc_conv_expr_present (sym);
3419 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
3421 gfc_add_expr_to_block (&block, stmt);
3423 /* We don't need to free any memory allocated by internal_pack as it will
3424 be freed at the end of the function by pop_context. */
3425 return gfc_finish_block (&block);
3429 /* Convert an array for passing as an actual parameter. Expressions and
3430 vector subscripts are evaluated and stored in a temporary, which is then
3431 passed. For whole arrays the descriptor is passed. For array sections
3432 a modified copy of the descriptor is passed, but using the original data.
3433 Also used for array pointer assignments by setting se->direct_byref. */
3435 void
3436 gfc_conv_expr_descriptor (gfc_se * se, gfc_expr * expr, gfc_ss * ss)
3438 gfc_loopinfo loop;
3439 gfc_ss *secss;
3440 gfc_ss_info *info;
3441 int need_tmp;
3442 int n;
3443 tree tmp;
3444 tree desc;
3445 stmtblock_t block;
3446 tree start;
3447 tree offset;
3448 int full;
3449 gfc_ss *vss;
3450 gfc_ref *ref;
3452 gcc_assert (ss != gfc_ss_terminator);
3454 /* TODO: Pass constant array constructors without a temporary. */
3455 /* Special case things we know we can pass easily. */
3456 switch (expr->expr_type)
3458 case EXPR_VARIABLE:
3459 /* If we have a linear array section, we can pass it directly.
3460 Otherwise we need to copy it into a temporary. */
3462 /* Find the SS for the array section. */
3463 secss = ss;
3464 while (secss != gfc_ss_terminator && secss->type != GFC_SS_SECTION)
3465 secss = secss->next;
3467 gcc_assert (secss != gfc_ss_terminator);
3469 need_tmp = 0;
3470 for (n = 0; n < secss->data.info.dimen; n++)
3472 vss = secss->data.info.subscript[secss->data.info.dim[n]];
3473 if (vss && vss->type == GFC_SS_VECTOR)
3474 need_tmp = 1;
3477 info = &secss->data.info;
3479 /* Get the descriptor for the array. */
3480 gfc_conv_ss_descriptor (&se->pre, secss, 0);
3481 desc = info->descriptor;
3482 if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
3484 /* Create a new descriptor if the array doesn't have one. */
3485 full = 0;
3487 else if (info->ref->u.ar.type == AR_FULL)
3488 full = 1;
3489 else if (se->direct_byref)
3490 full = 0;
3491 else
3493 ref = info->ref;
3494 gcc_assert (ref->u.ar.type == AR_SECTION);
3496 full = 1;
3497 for (n = 0; n < ref->u.ar.dimen; n++)
3499 /* Detect passing the full array as a section. This could do
3500 even more checking, but it doesn't seem worth it. */
3501 if (ref->u.ar.start[n]
3502 || ref->u.ar.end[n]
3503 || (ref->u.ar.stride[n]
3504 && !gfc_expr_is_one (ref->u.ar.stride[n], 0)))
3506 full = 0;
3507 break;
3512 /* Check for substring references. */
3513 ref = expr->ref;
3514 if (!need_tmp && ref && expr->ts.type == BT_CHARACTER)
3516 while (ref->next)
3517 ref = ref->next;
3518 if (ref->type == REF_SUBSTRING)
3520 /* In general character substrings need a copy. Character
3521 array strides are expressed as multiples of the element
3522 size (consistent with other array types), not in
3523 characters. */
3524 full = 0;
3525 need_tmp = 1;
3529 if (full)
3531 if (se->direct_byref)
3533 /* Copy the descriptor for pointer assignments. */
3534 gfc_add_modify_expr (&se->pre, se->expr, desc);
3536 else if (se->want_pointer)
3538 /* We pass full arrays directly. This means that pointers and
3539 allocatable arrays should also work. */
3540 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
3542 else
3544 se->expr = desc;
3547 if (expr->ts.type == BT_CHARACTER)
3548 se->string_length = gfc_get_expr_charlen (expr);
3550 return;
3552 break;
3554 case EXPR_FUNCTION:
3555 /* A transformational function return value will be a temporary
3556 array descriptor. We still need to go through the scalarizer
3557 to create the descriptor. Elemental functions ar handled as
3558 arbitrary expressions, i.e. copy to a temporary. */
3559 secss = ss;
3560 /* Look for the SS for this function. */
3561 while (secss != gfc_ss_terminator
3562 && (secss->type != GFC_SS_FUNCTION || secss->expr != expr))
3563 secss = secss->next;
3565 if (se->direct_byref)
3567 gcc_assert (secss != gfc_ss_terminator);
3569 /* For pointer assignments pass the descriptor directly. */
3570 se->ss = secss;
3571 se->expr = gfc_build_addr_expr (NULL, se->expr);
3572 gfc_conv_expr (se, expr);
3573 return;
3576 if (secss == gfc_ss_terminator)
3578 /* Elemental function. */
3579 need_tmp = 1;
3580 info = NULL;
3582 else
3584 /* Transformational function. */
3585 info = &secss->data.info;
3586 need_tmp = 0;
3588 break;
3590 default:
3591 /* Something complicated. Copy it into a temporary. */
3592 need_tmp = 1;
3593 secss = NULL;
3594 info = NULL;
3595 break;
3599 gfc_init_loopinfo (&loop);
3601 /* Associate the SS with the loop. */
3602 gfc_add_ss_to_loop (&loop, ss);
3604 /* Tell the scalarizer not to bother creating loop variables, etc. */
3605 if (!need_tmp)
3606 loop.array_parameter = 1;
3607 else
3608 gcc_assert (se->want_pointer && !se->direct_byref);
3610 /* Setup the scalarizing loops and bounds. */
3611 gfc_conv_ss_startstride (&loop);
3613 if (need_tmp)
3615 /* Tell the scalarizer to make a temporary. */
3616 loop.temp_ss = gfc_get_ss ();
3617 loop.temp_ss->type = GFC_SS_TEMP;
3618 loop.temp_ss->next = gfc_ss_terminator;
3619 loop.temp_ss->data.temp.type = gfc_typenode_for_spec (&expr->ts);
3620 /* ... which can hold our string, if present. */
3621 if (expr->ts.type == BT_CHARACTER)
3622 se->string_length = loop.temp_ss->string_length
3623 = TYPE_SIZE_UNIT (loop.temp_ss->data.temp.type);
3624 else
3625 loop.temp_ss->string_length = NULL;
3626 loop.temp_ss->data.temp.dimen = loop.dimen;
3627 gfc_add_ss_to_loop (&loop, loop.temp_ss);
3630 gfc_conv_loop_setup (&loop);
3632 if (need_tmp)
3634 /* Copy into a temporary and pass that. We don't need to copy the data
3635 back because expressions and vector subscripts must be INTENT_IN. */
3636 /* TODO: Optimize passing function return values. */
3637 gfc_se lse;
3638 gfc_se rse;
3640 /* Start the copying loops. */
3641 gfc_mark_ss_chain_used (loop.temp_ss, 1);
3642 gfc_mark_ss_chain_used (ss, 1);
3643 gfc_start_scalarized_body (&loop, &block);
3645 /* Copy each data element. */
3646 gfc_init_se (&lse, NULL);
3647 gfc_copy_loopinfo_to_se (&lse, &loop);
3648 gfc_init_se (&rse, NULL);
3649 gfc_copy_loopinfo_to_se (&rse, &loop);
3651 lse.ss = loop.temp_ss;
3652 rse.ss = ss;
3654 gfc_conv_scalarized_array_ref (&lse, NULL);
3655 gfc_conv_expr_val (&rse, expr);
3657 gfc_add_block_to_block (&block, &rse.pre);
3658 gfc_add_block_to_block (&block, &lse.pre);
3660 gfc_add_modify_expr (&block, lse.expr, rse.expr);
3662 /* Finish the copying loops. */
3663 gfc_trans_scalarizing_loops (&loop, &block);
3665 /* Set the first stride component to zero to indicate a temporary. */
3666 desc = loop.temp_ss->data.info.descriptor;
3667 tmp = gfc_conv_descriptor_stride (desc, gfc_rank_cst[0]);
3668 gfc_add_modify_expr (&loop.pre, tmp, gfc_index_zero_node);
3670 gcc_assert (is_gimple_lvalue (desc));
3671 se->expr = gfc_build_addr_expr (NULL, desc);
3673 else if (expr->expr_type == EXPR_FUNCTION)
3675 desc = info->descriptor;
3677 if (se->want_pointer)
3678 se->expr = gfc_build_addr_expr (NULL_TREE, desc);
3679 else
3680 se->expr = desc;
3682 if (expr->ts.type == BT_CHARACTER)
3683 se->string_length = expr->symtree->n.sym->ts.cl->backend_decl;
3685 else
3687 /* We pass sections without copying to a temporary. Make a new
3688 descriptor and point it at the section we want. The loop variable
3689 limits will be the limits of the section.
3690 A function may decide to repack the array to speed up access, but
3691 we're not bothered about that here. */
3692 int dim;
3693 tree parm;
3694 tree parmtype;
3695 tree stride;
3696 tree from;
3697 tree to;
3698 tree base;
3700 /* Set the string_length for a character array. */
3701 if (expr->ts.type == BT_CHARACTER)
3702 se->string_length = gfc_get_expr_charlen (expr);
3704 desc = info->descriptor;
3705 gcc_assert (secss && secss != gfc_ss_terminator);
3706 if (se->direct_byref)
3708 /* For pointer assignments we fill in the destination. */
3709 parm = se->expr;
3710 parmtype = TREE_TYPE (parm);
3712 else
3714 /* Otherwise make a new one. */
3715 parmtype = gfc_get_element_type (TREE_TYPE (desc));
3716 parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen,
3717 loop.from, loop.to, 0);
3718 parm = gfc_create_var (parmtype, "parm");
3721 offset = gfc_index_zero_node;
3722 dim = 0;
3724 /* The following can be somewhat confusing. We have two
3725 descriptors, a new one and the original array.
3726 {parm, parmtype, dim} refer to the new one.
3727 {desc, type, n, secss, loop} refer to the original, which maybe
3728 a descriptorless array.
3729 The bounds of the scalarization are the bounds of the section.
3730 We don't have to worry about numeric overflows when calculating
3731 the offsets because all elements are within the array data. */
3733 /* Set the dtype. */
3734 tmp = gfc_conv_descriptor_dtype (parm);
3735 gfc_add_modify_expr (&loop.pre, tmp, gfc_get_dtype (parmtype));
3737 if (se->direct_byref)
3738 base = gfc_index_zero_node;
3739 else
3740 base = NULL_TREE;
3742 for (n = 0; n < info->ref->u.ar.dimen; n++)
3744 stride = gfc_conv_array_stride (desc, n);
3746 /* Work out the offset. */
3747 if (info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
3749 gcc_assert (info->subscript[n]
3750 && info->subscript[n]->type == GFC_SS_SCALAR);
3751 start = info->subscript[n]->data.scalar.expr;
3753 else
3755 /* Check we haven't somehow got out of sync. */
3756 gcc_assert (info->dim[dim] == n);
3758 /* Evaluate and remember the start of the section. */
3759 start = info->start[dim];
3760 stride = gfc_evaluate_now (stride, &loop.pre);
3763 tmp = gfc_conv_array_lbound (desc, n);
3764 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), start, tmp);
3766 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp, stride);
3767 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (tmp), offset, tmp);
3769 if (info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
3771 /* For elemental dimensions, we only need the offset. */
3772 continue;
3775 /* Vector subscripts need copying and are handled elsewhere. */
3776 gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
3778 /* Set the new lower bound. */
3779 from = loop.from[dim];
3780 to = loop.to[dim];
3781 if (!integer_onep (from))
3783 /* Make sure the new section starts at 1. */
3784 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3785 gfc_index_one_node, from);
3786 to = fold_build2 (PLUS_EXPR, gfc_array_index_type, to, tmp);
3787 from = gfc_index_one_node;
3789 tmp = gfc_conv_descriptor_lbound (parm, gfc_rank_cst[dim]);
3790 gfc_add_modify_expr (&loop.pre, tmp, from);
3792 /* Set the new upper bound. */
3793 tmp = gfc_conv_descriptor_ubound (parm, gfc_rank_cst[dim]);
3794 gfc_add_modify_expr (&loop.pre, tmp, to);
3796 /* Multiply the stride by the section stride to get the
3797 total stride. */
3798 stride = fold_build2 (MULT_EXPR, gfc_array_index_type,
3799 stride, info->stride[dim]);
3801 if (se->direct_byref)
3802 base = fold_build2 (MINUS_EXPR, TREE_TYPE (base),
3803 base, stride);
3805 /* Store the new stride. */
3806 tmp = gfc_conv_descriptor_stride (parm, gfc_rank_cst[dim]);
3807 gfc_add_modify_expr (&loop.pre, tmp, stride);
3809 dim++;
3812 /* Point the data pointer at the first element in the section. */
3813 tmp = gfc_conv_array_data (desc);
3814 tmp = gfc_build_indirect_ref (tmp);
3815 tmp = gfc_build_array_ref (tmp, offset);
3816 offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
3818 tmp = gfc_conv_descriptor_data (parm);
3819 gfc_add_modify_expr (&loop.pre, tmp,
3820 fold_convert (TREE_TYPE (tmp), offset));
3822 if (se->direct_byref)
3824 /* Set the offset. */
3825 tmp = gfc_conv_descriptor_offset (parm);
3826 gfc_add_modify_expr (&loop.pre, tmp, base);
3828 else
3830 /* Only the callee knows what the correct offset it, so just set
3831 it to zero here. */
3832 tmp = gfc_conv_descriptor_offset (parm);
3833 gfc_add_modify_expr (&loop.pre, tmp, gfc_index_zero_node);
3836 if (!se->direct_byref)
3838 /* Get a pointer to the new descriptor. */
3839 if (se->want_pointer)
3840 se->expr = gfc_build_addr_expr (NULL, parm);
3841 else
3842 se->expr = parm;
3846 gfc_add_block_to_block (&se->pre, &loop.pre);
3847 gfc_add_block_to_block (&se->post, &loop.post);
3849 /* Cleanup the scalarizer. */
3850 gfc_cleanup_loop (&loop);
3854 /* Convert an array for passing as an actual parameter. */
3855 /* TODO: Optimize passing g77 arrays. */
3857 void
3858 gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr, gfc_ss * ss, int g77)
3860 tree ptr;
3861 tree desc;
3862 tree tmp;
3863 tree stmt;
3864 gfc_symbol *sym;
3865 stmtblock_t block;
3867 /* Passing address of the array if it is not pointer or assumed-shape. */
3868 if (expr->expr_type == EXPR_VARIABLE
3869 && expr->ref->u.ar.type == AR_FULL && g77)
3871 sym = expr->symtree->n.sym;
3872 tmp = gfc_get_symbol_decl (sym);
3873 if (sym->ts.type == BT_CHARACTER)
3874 se->string_length = sym->ts.cl->backend_decl;
3875 if (!sym->attr.pointer && sym->as->type != AS_ASSUMED_SHAPE
3876 && !sym->attr.allocatable)
3878 /* Some variables are declared directly, others are declared as
3879 pointers and allocated on the heap. */
3880 if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
3881 se->expr = tmp;
3882 else
3883 se->expr = gfc_build_addr_expr (NULL, tmp);
3884 return;
3886 if (sym->attr.allocatable)
3888 se->expr = gfc_conv_array_data (tmp);
3889 return;
3893 se->want_pointer = 1;
3894 gfc_conv_expr_descriptor (se, expr, ss);
3896 if (g77)
3898 desc = se->expr;
3899 /* Repack the array. */
3900 tmp = gfc_chainon_list (NULL_TREE, desc);
3901 ptr = gfc_build_function_call (gfor_fndecl_in_pack, tmp);
3902 ptr = gfc_evaluate_now (ptr, &se->pre);
3903 se->expr = ptr;
3905 gfc_start_block (&block);
3907 /* Copy the data back. */
3908 tmp = gfc_chainon_list (NULL_TREE, desc);
3909 tmp = gfc_chainon_list (tmp, ptr);
3910 tmp = gfc_build_function_call (gfor_fndecl_in_unpack, tmp);
3911 gfc_add_expr_to_block (&block, tmp);
3913 /* Free the temporary. */
3914 tmp = convert (pvoid_type_node, ptr);
3915 tmp = gfc_chainon_list (NULL_TREE, tmp);
3916 tmp = gfc_build_function_call (gfor_fndecl_internal_free, tmp);
3917 gfc_add_expr_to_block (&block, tmp);
3919 stmt = gfc_finish_block (&block);
3921 gfc_init_block (&block);
3922 /* Only if it was repacked. This code needs to be executed before the
3923 loop cleanup code. */
3924 tmp = gfc_build_indirect_ref (desc);
3925 tmp = gfc_conv_array_data (tmp);
3926 tmp = build2 (NE_EXPR, boolean_type_node, ptr, tmp);
3927 tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
3929 gfc_add_expr_to_block (&block, tmp);
3930 gfc_add_block_to_block (&block, &se->post);
3932 gfc_init_block (&se->post);
3933 gfc_add_block_to_block (&se->post, &block);
3938 /* NULLIFY an allocated/pointer array on function entry, free it on exit. */
3940 tree
3941 gfc_trans_deferred_array (gfc_symbol * sym, tree body)
3943 tree type;
3944 tree tmp;
3945 tree descriptor;
3946 tree deallocate;
3947 stmtblock_t block;
3948 stmtblock_t fnblock;
3949 locus loc;
3951 /* Make sure the frontend gets these right. */
3952 if (!(sym->attr.pointer || sym->attr.allocatable))
3953 fatal_error
3954 ("Possible frontend bug: Deferred array size without pointer or allocatable attribute.");
3956 gfc_init_block (&fnblock);
3958 gcc_assert (TREE_CODE (sym->backend_decl) == VAR_DECL);
3959 if (sym->ts.type == BT_CHARACTER
3960 && !INTEGER_CST_P (sym->ts.cl->backend_decl))
3961 gfc_trans_init_string_length (sym->ts.cl, &fnblock);
3963 /* Parameter and use associated variables don't need anything special. */
3964 if (sym->attr.dummy || sym->attr.use_assoc)
3966 gfc_add_expr_to_block (&fnblock, body);
3968 return gfc_finish_block (&fnblock);
3971 gfc_get_backend_locus (&loc);
3972 gfc_set_backend_locus (&sym->declared_at);
3973 descriptor = sym->backend_decl;
3975 if (TREE_STATIC (descriptor))
3977 /* SAVEd variables are not freed on exit. */
3978 gfc_trans_static_array_pointer (sym);
3979 return body;
3982 /* Get the descriptor type. */
3983 type = TREE_TYPE (sym->backend_decl);
3984 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
3986 /* NULLIFY the data pointer. */
3987 tmp = gfc_conv_descriptor_data (descriptor);
3988 gfc_add_modify_expr (&fnblock, tmp,
3989 convert (TREE_TYPE (tmp), integer_zero_node));
3991 gfc_add_expr_to_block (&fnblock, body);
3993 gfc_set_backend_locus (&loc);
3994 /* Allocatable arrays need to be freed when they go out of scope. */
3995 if (sym->attr.allocatable)
3997 gfc_start_block (&block);
3999 /* Deallocate if still allocated at the end of the procedure. */
4000 deallocate = gfc_array_deallocate (descriptor);
4002 tmp = gfc_conv_descriptor_data (descriptor);
4003 tmp = build2 (NE_EXPR, boolean_type_node, tmp, integer_zero_node);
4004 tmp = build3_v (COND_EXPR, tmp, deallocate, build_empty_stmt ());
4005 gfc_add_expr_to_block (&block, tmp);
4007 tmp = gfc_finish_block (&block);
4008 gfc_add_expr_to_block (&fnblock, tmp);
4011 return gfc_finish_block (&fnblock);
4014 /************ Expression Walking Functions ******************/
4016 /* Walk a variable reference.
4018 Possible extension - multiple component subscripts.
4019 x(:,:) = foo%a(:)%b(:)
4020 Transforms to
4021 forall (i=..., j=...)
4022 x(i,j) = foo%a(j)%b(i)
4023 end forall
4024 This adds a fair amout of complexity because you need to deal with more
4025 than one ref. Maybe handle in a similar manner to vector subscripts.
4026 Maybe not worth the effort. */
4029 static gfc_ss *
4030 gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
4032 gfc_ref *ref;
4033 gfc_array_ref *ar;
4034 gfc_ss *newss;
4035 gfc_ss *head;
4036 int n;
4038 for (ref = expr->ref; ref; ref = ref->next)
4040 /* We're only interested in array sections. */
4041 if (ref->type != REF_ARRAY)
4042 continue;
4044 ar = &ref->u.ar;
4045 switch (ar->type)
4047 case AR_ELEMENT:
4048 /* TODO: Take elemental array references out of scalarization
4049 loop. */
4050 break;
4052 case AR_FULL:
4053 newss = gfc_get_ss ();
4054 newss->type = GFC_SS_SECTION;
4055 newss->expr = expr;
4056 newss->next = ss;
4057 newss->data.info.dimen = ar->as->rank;
4058 newss->data.info.ref = ref;
4060 /* Make sure array is the same as array(:,:), this way
4061 we don't need to special case all the time. */
4062 ar->dimen = ar->as->rank;
4063 for (n = 0; n < ar->dimen; n++)
4065 newss->data.info.dim[n] = n;
4066 ar->dimen_type[n] = DIMEN_RANGE;
4068 gcc_assert (ar->start[n] == NULL);
4069 gcc_assert (ar->end[n] == NULL);
4070 gcc_assert (ar->stride[n] == NULL);
4072 return newss;
4074 case AR_SECTION:
4075 newss = gfc_get_ss ();
4076 newss->type = GFC_SS_SECTION;
4077 newss->expr = expr;
4078 newss->next = ss;
4079 newss->data.info.dimen = 0;
4080 newss->data.info.ref = ref;
4082 head = newss;
4084 /* We add SS chains for all the subscripts in the section. */
4085 for (n = 0; n < ar->dimen; n++)
4087 gfc_ss *indexss;
4089 switch (ar->dimen_type[n])
4091 case DIMEN_ELEMENT:
4092 /* Add SS for elemental (scalar) subscripts. */
4093 gcc_assert (ar->start[n]);
4094 indexss = gfc_get_ss ();
4095 indexss->type = GFC_SS_SCALAR;
4096 indexss->expr = ar->start[n];
4097 indexss->next = gfc_ss_terminator;
4098 indexss->loop_chain = gfc_ss_terminator;
4099 newss->data.info.subscript[n] = indexss;
4100 break;
4102 case DIMEN_RANGE:
4103 /* We don't add anything for sections, just remember this
4104 dimension for later. */
4105 newss->data.info.dim[newss->data.info.dimen] = n;
4106 newss->data.info.dimen++;
4107 break;
4109 case DIMEN_VECTOR:
4110 /* Get a SS for the vector. This will not be added to the
4111 chain directly. */
4112 indexss = gfc_walk_expr (ar->start[n]);
4113 if (indexss == gfc_ss_terminator)
4114 internal_error ("scalar vector subscript???");
4116 /* We currently only handle really simple vector
4117 subscripts. */
4118 if (indexss->next != gfc_ss_terminator)
4119 gfc_todo_error ("vector subscript expressions");
4120 indexss->loop_chain = gfc_ss_terminator;
4122 /* Mark this as a vector subscript. We don't add this
4123 directly into the chain, but as a subscript of the
4124 existing SS for this term. */
4125 indexss->type = GFC_SS_VECTOR;
4126 newss->data.info.subscript[n] = indexss;
4127 /* Also remember this dimension. */
4128 newss->data.info.dim[newss->data.info.dimen] = n;
4129 newss->data.info.dimen++;
4130 break;
4132 default:
4133 /* We should know what sort of section it is by now. */
4134 gcc_unreachable ();
4137 /* We should have at least one non-elemental dimension. */
4138 gcc_assert (newss->data.info.dimen > 0);
4139 return head;
4140 break;
4142 default:
4143 /* We should know what sort of section it is by now. */
4144 gcc_unreachable ();
4148 return ss;
4152 /* Walk an expression operator. If only one operand of a binary expression is
4153 scalar, we must also add the scalar term to the SS chain. */
4155 static gfc_ss *
4156 gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
4158 gfc_ss *head;
4159 gfc_ss *head2;
4160 gfc_ss *newss;
4162 head = gfc_walk_subexpr (ss, expr->value.op.op1);
4163 if (expr->value.op.op2 == NULL)
4164 head2 = head;
4165 else
4166 head2 = gfc_walk_subexpr (head, expr->value.op.op2);
4168 /* All operands are scalar. Pass back and let the caller deal with it. */
4169 if (head2 == ss)
4170 return head2;
4172 /* All operands require scalarization. */
4173 if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
4174 return head2;
4176 /* One of the operands needs scalarization, the other is scalar.
4177 Create a gfc_ss for the scalar expression. */
4178 newss = gfc_get_ss ();
4179 newss->type = GFC_SS_SCALAR;
4180 if (head == ss)
4182 /* First operand is scalar. We build the chain in reverse order, so
4183 add the scarar SS after the second operand. */
4184 head = head2;
4185 while (head && head->next != ss)
4186 head = head->next;
4187 /* Check we haven't somehow broken the chain. */
4188 gcc_assert (head);
4189 newss->next = ss;
4190 head->next = newss;
4191 newss->expr = expr->value.op.op1;
4193 else /* head2 == head */
4195 gcc_assert (head2 == head);
4196 /* Second operand is scalar. */
4197 newss->next = head2;
4198 head2 = newss;
4199 newss->expr = expr->value.op.op2;
4202 return head2;
4206 /* Reverse a SS chain. */
4208 static gfc_ss *
4209 gfc_reverse_ss (gfc_ss * ss)
4211 gfc_ss *next;
4212 gfc_ss *head;
4214 gcc_assert (ss != NULL);
4216 head = gfc_ss_terminator;
4217 while (ss != gfc_ss_terminator)
4219 next = ss->next;
4220 /* Check we didn't somehow break the chain. */
4221 gcc_assert (next != NULL);
4222 ss->next = head;
4223 head = ss;
4224 ss = next;
4227 return (head);
4231 /* Walk the arguments of an elemental function. */
4233 gfc_ss *
4234 gfc_walk_elemental_function_args (gfc_ss * ss, gfc_expr * expr,
4235 gfc_ss_type type)
4237 gfc_actual_arglist *arg;
4238 int scalar;
4239 gfc_ss *head;
4240 gfc_ss *tail;
4241 gfc_ss *newss;
4243 head = gfc_ss_terminator;
4244 tail = NULL;
4245 scalar = 1;
4246 for (arg = expr->value.function.actual; arg; arg = arg->next)
4248 if (!arg->expr)
4249 continue;
4251 newss = gfc_walk_subexpr (head, arg->expr);
4252 if (newss == head)
4254 /* Scalar argument. */
4255 newss = gfc_get_ss ();
4256 newss->type = type;
4257 newss->expr = arg->expr;
4258 newss->next = head;
4260 else
4261 scalar = 0;
4263 head = newss;
4264 if (!tail)
4266 tail = head;
4267 while (tail->next != gfc_ss_terminator)
4268 tail = tail->next;
4272 if (scalar)
4274 /* If all the arguments are scalar we don't need the argument SS. */
4275 gfc_free_ss_chain (head);
4276 /* Pass it back. */
4277 return ss;
4280 /* Add it onto the existing chain. */
4281 tail->next = ss;
4282 return head;
4286 /* Walk a function call. Scalar functions are passed back, and taken out of
4287 scalarization loops. For elemental functions we walk their arguments.
4288 The result of functions returning arrays is stored in a temporary outside
4289 the loop, so that the function is only called once. Hence we do not need
4290 to walk their arguments. */
4292 static gfc_ss *
4293 gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
4295 gfc_ss *newss;
4296 gfc_intrinsic_sym *isym;
4297 gfc_symbol *sym;
4299 isym = expr->value.function.isym;
4301 /* Handle intrinsic functions separately. */
4302 if (isym)
4303 return gfc_walk_intrinsic_function (ss, expr, isym);
4305 sym = expr->value.function.esym;
4306 if (!sym)
4307 sym = expr->symtree->n.sym;
4309 /* A function that returns arrays. */
4310 if (gfc_return_by_reference (sym) && sym->result->attr.dimension)
4312 newss = gfc_get_ss ();
4313 newss->type = GFC_SS_FUNCTION;
4314 newss->expr = expr;
4315 newss->next = ss;
4316 newss->data.info.dimen = expr->rank;
4317 return newss;
4320 /* Walk the parameters of an elemental function. For now we always pass
4321 by reference. */
4322 if (sym->attr.elemental)
4323 return gfc_walk_elemental_function_args (ss, expr, GFC_SS_REFERENCE);
4325 /* Scalar functions are OK as these are evaluated outside the scalarization
4326 loop. Pass back and let the caller deal with it. */
4327 return ss;
4331 /* An array temporary is constructed for array constructors. */
4333 static gfc_ss *
4334 gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
4336 gfc_ss *newss;
4337 int n;
4339 newss = gfc_get_ss ();
4340 newss->type = GFC_SS_CONSTRUCTOR;
4341 newss->expr = expr;
4342 newss->next = ss;
4343 newss->data.info.dimen = expr->rank;
4344 for (n = 0; n < expr->rank; n++)
4345 newss->data.info.dim[n] = n;
4347 return newss;
4351 /* Walk an expression. Add walked expressions to the head of the SS chain.
4352 A wholy scalar expression will not be added. */
4354 static gfc_ss *
4355 gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
4357 gfc_ss *head;
4359 switch (expr->expr_type)
4361 case EXPR_VARIABLE:
4362 head = gfc_walk_variable_expr (ss, expr);
4363 return head;
4365 case EXPR_OP:
4366 head = gfc_walk_op_expr (ss, expr);
4367 return head;
4369 case EXPR_FUNCTION:
4370 head = gfc_walk_function_expr (ss, expr);
4371 return head;
4373 case EXPR_CONSTANT:
4374 case EXPR_NULL:
4375 case EXPR_STRUCTURE:
4376 /* Pass back and let the caller deal with it. */
4377 break;
4379 case EXPR_ARRAY:
4380 head = gfc_walk_array_constructor (ss, expr);
4381 return head;
4383 case EXPR_SUBSTRING:
4384 /* Pass back and let the caller deal with it. */
4385 break;
4387 default:
4388 internal_error ("bad expression type during walk (%d)",
4389 expr->expr_type);
4391 return ss;
4395 /* Entry point for expression walking.
4396 A return value equal to the passed chain means this is
4397 a scalar expression. It is up to the caller to take whatever action is
4398 necessary to translate these. */
4400 gfc_ss *
4401 gfc_walk_expr (gfc_expr * expr)
4403 gfc_ss *res;
4405 res = gfc_walk_subexpr (gfc_ss_terminator, expr);
4406 return gfc_reverse_ss (res);