re PR fortran/36112 (Bounds-checking on character length not working for array-constr...
[gcc.git] / gcc / fortran / trans-array.c
1 /* Array translation routines
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* trans-array.c-- Various array related code, including scalarization,
24 allocation, initialization and other support routines. */
25
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.
32
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.
37
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.
43
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.
48
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.
55
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.
60
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.
65
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.
71
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.
75
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. */
78
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"
95
96 static gfc_ss *gfc_walk_subexpr (gfc_ss *, gfc_expr *);
97 static bool gfc_get_array_constructor_size (mpz_t *, gfc_constructor *);
98
99 /* The contents of this structure aren't actually used, just the address. */
100 static gfc_ss gfc_ss_terminator_var;
101 gfc_ss * const gfc_ss_terminator = &gfc_ss_terminator_var;
102
103
104 static tree
105 gfc_array_dataptr_type (tree desc)
106 {
107 return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
108 }
109
110
111 /* Build expressions to access the members of an array descriptor.
112 It's surprisingly easy to mess up here, so never access
113 an array descriptor by "brute force", always use these
114 functions. This also avoids problems if we change the format
115 of an array descriptor.
116
117 To understand these magic numbers, look at the comments
118 before gfc_build_array_type() in trans-types.c.
119
120 The code within these defines should be the only code which knows the format
121 of an array descriptor.
122
123 Any code just needing to read obtain the bounds of an array should use
124 gfc_conv_array_* rather than the following functions as these will return
125 know constant values, and work with arrays which do not have descriptors.
126
127 Don't forget to #undef these! */
128
129 #define DATA_FIELD 0
130 #define OFFSET_FIELD 1
131 #define DTYPE_FIELD 2
132 #define DIMENSION_FIELD 3
133
134 #define STRIDE_SUBFIELD 0
135 #define LBOUND_SUBFIELD 1
136 #define UBOUND_SUBFIELD 2
137
138 /* This provides READ-ONLY access to the data field. The field itself
139 doesn't have the proper type. */
140
141 tree
142 gfc_conv_descriptor_data_get (tree desc)
143 {
144 tree field, type, t;
145
146 type = TREE_TYPE (desc);
147 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
148
149 field = TYPE_FIELDS (type);
150 gcc_assert (DATA_FIELD == 0);
151
152 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
153 t = fold_convert (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), t);
154
155 return t;
156 }
157
158 /* This provides WRITE access to the data field.
159
160 TUPLES_P is true if we are generating tuples.
161
162 This function gets called through the following macros:
163 gfc_conv_descriptor_data_set
164 gfc_conv_descriptor_data_set_tuples. */
165
166 void
167 gfc_conv_descriptor_data_set_internal (stmtblock_t *block,
168 tree desc, tree value,
169 bool tuples_p)
170 {
171 tree field, type, t;
172
173 type = TREE_TYPE (desc);
174 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
175
176 field = TYPE_FIELDS (type);
177 gcc_assert (DATA_FIELD == 0);
178
179 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
180 gfc_add_modify (block, t, fold_convert (TREE_TYPE (field), value), tuples_p);
181 }
182
183
184 /* This provides address access to the data field. This should only be
185 used by array allocation, passing this on to the runtime. */
186
187 tree
188 gfc_conv_descriptor_data_addr (tree desc)
189 {
190 tree field, type, t;
191
192 type = TREE_TYPE (desc);
193 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
194
195 field = TYPE_FIELDS (type);
196 gcc_assert (DATA_FIELD == 0);
197
198 t = fold_build3 (COMPONENT_REF, TREE_TYPE (field), desc, field, NULL_TREE);
199 return build_fold_addr_expr (t);
200 }
201
202 tree
203 gfc_conv_descriptor_offset (tree desc)
204 {
205 tree type;
206 tree field;
207
208 type = TREE_TYPE (desc);
209 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
210
211 field = gfc_advance_chain (TYPE_FIELDS (type), OFFSET_FIELD);
212 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
213
214 return fold_build3 (COMPONENT_REF, TREE_TYPE (field),
215 desc, field, NULL_TREE);
216 }
217
218 tree
219 gfc_conv_descriptor_dtype (tree desc)
220 {
221 tree field;
222 tree type;
223
224 type = TREE_TYPE (desc);
225 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
226
227 field = gfc_advance_chain (TYPE_FIELDS (type), DTYPE_FIELD);
228 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
229
230 return fold_build3 (COMPONENT_REF, TREE_TYPE (field),
231 desc, field, NULL_TREE);
232 }
233
234 static tree
235 gfc_conv_descriptor_dimension (tree desc, tree dim)
236 {
237 tree field;
238 tree type;
239 tree tmp;
240
241 type = TREE_TYPE (desc);
242 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
243
244 field = gfc_advance_chain (TYPE_FIELDS (type), DIMENSION_FIELD);
245 gcc_assert (field != NULL_TREE
246 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
247 && TREE_CODE (TREE_TYPE (TREE_TYPE (field))) == RECORD_TYPE);
248
249 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
250 desc, field, NULL_TREE);
251 tmp = gfc_build_array_ref (tmp, dim, NULL);
252 return tmp;
253 }
254
255 tree
256 gfc_conv_descriptor_stride (tree desc, tree dim)
257 {
258 tree tmp;
259 tree field;
260
261 tmp = gfc_conv_descriptor_dimension (desc, dim);
262 field = TYPE_FIELDS (TREE_TYPE (tmp));
263 field = gfc_advance_chain (field, STRIDE_SUBFIELD);
264 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
265
266 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
267 tmp, field, NULL_TREE);
268 return tmp;
269 }
270
271 tree
272 gfc_conv_descriptor_lbound (tree desc, tree dim)
273 {
274 tree tmp;
275 tree field;
276
277 tmp = gfc_conv_descriptor_dimension (desc, dim);
278 field = TYPE_FIELDS (TREE_TYPE (tmp));
279 field = gfc_advance_chain (field, LBOUND_SUBFIELD);
280 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
281
282 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
283 tmp, field, NULL_TREE);
284 return tmp;
285 }
286
287 tree
288 gfc_conv_descriptor_ubound (tree desc, tree dim)
289 {
290 tree tmp;
291 tree field;
292
293 tmp = gfc_conv_descriptor_dimension (desc, dim);
294 field = TYPE_FIELDS (TREE_TYPE (tmp));
295 field = gfc_advance_chain (field, UBOUND_SUBFIELD);
296 gcc_assert (field != NULL_TREE && TREE_TYPE (field) == gfc_array_index_type);
297
298 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
299 tmp, field, NULL_TREE);
300 return tmp;
301 }
302
303
304 /* Build a null array descriptor constructor. */
305
306 tree
307 gfc_build_null_descriptor (tree type)
308 {
309 tree field;
310 tree tmp;
311
312 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
313 gcc_assert (DATA_FIELD == 0);
314 field = TYPE_FIELDS (type);
315
316 /* Set a NULL data pointer. */
317 tmp = build_constructor_single (type, field, null_pointer_node);
318 TREE_CONSTANT (tmp) = 1;
319 /* All other fields are ignored. */
320
321 return tmp;
322 }
323
324
325 /* Cleanup those #defines. */
326
327 #undef DATA_FIELD
328 #undef OFFSET_FIELD
329 #undef DTYPE_FIELD
330 #undef DIMENSION_FIELD
331 #undef STRIDE_SUBFIELD
332 #undef LBOUND_SUBFIELD
333 #undef UBOUND_SUBFIELD
334
335
336 /* Mark a SS chain as used. Flags specifies in which loops the SS is used.
337 flags & 1 = Main loop body.
338 flags & 2 = temp copy loop. */
339
340 void
341 gfc_mark_ss_chain_used (gfc_ss * ss, unsigned flags)
342 {
343 for (; ss != gfc_ss_terminator; ss = ss->next)
344 ss->useflags = flags;
345 }
346
347 static void gfc_free_ss (gfc_ss *);
348
349
350 /* Free a gfc_ss chain. */
351
352 static void
353 gfc_free_ss_chain (gfc_ss * ss)
354 {
355 gfc_ss *next;
356
357 while (ss != gfc_ss_terminator)
358 {
359 gcc_assert (ss != NULL);
360 next = ss->next;
361 gfc_free_ss (ss);
362 ss = next;
363 }
364 }
365
366
367 /* Free a SS. */
368
369 static void
370 gfc_free_ss (gfc_ss * ss)
371 {
372 int n;
373
374 switch (ss->type)
375 {
376 case GFC_SS_SECTION:
377 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
378 {
379 if (ss->data.info.subscript[n])
380 gfc_free_ss_chain (ss->data.info.subscript[n]);
381 }
382 break;
383
384 default:
385 break;
386 }
387
388 gfc_free (ss);
389 }
390
391
392 /* Free all the SS associated with a loop. */
393
394 void
395 gfc_cleanup_loop (gfc_loopinfo * loop)
396 {
397 gfc_ss *ss;
398 gfc_ss *next;
399
400 ss = loop->ss;
401 while (ss != gfc_ss_terminator)
402 {
403 gcc_assert (ss != NULL);
404 next = ss->loop_chain;
405 gfc_free_ss (ss);
406 ss = next;
407 }
408 }
409
410
411 /* Associate a SS chain with a loop. */
412
413 void
414 gfc_add_ss_to_loop (gfc_loopinfo * loop, gfc_ss * head)
415 {
416 gfc_ss *ss;
417
418 if (head == gfc_ss_terminator)
419 return;
420
421 ss = head;
422 for (; ss && ss != gfc_ss_terminator; ss = ss->next)
423 {
424 if (ss->next == gfc_ss_terminator)
425 ss->loop_chain = loop->ss;
426 else
427 ss->loop_chain = ss->next;
428 }
429 gcc_assert (ss == gfc_ss_terminator);
430 loop->ss = head;
431 }
432
433
434 /* Generate an initializer for a static pointer or allocatable array. */
435
436 void
437 gfc_trans_static_array_pointer (gfc_symbol * sym)
438 {
439 tree type;
440
441 gcc_assert (TREE_STATIC (sym->backend_decl));
442 /* Just zero the data member. */
443 type = TREE_TYPE (sym->backend_decl);
444 DECL_INITIAL (sym->backend_decl) = gfc_build_null_descriptor (type);
445 }
446
447
448 /* If the bounds of SE's loop have not yet been set, see if they can be
449 determined from array spec AS, which is the array spec of a called
450 function. MAPPING maps the callee's dummy arguments to the values
451 that the caller is passing. Add any initialization and finalization
452 code to SE. */
453
454 void
455 gfc_set_loop_bounds_from_array_spec (gfc_interface_mapping * mapping,
456 gfc_se * se, gfc_array_spec * as)
457 {
458 int n, dim;
459 gfc_se tmpse;
460 tree lower;
461 tree upper;
462 tree tmp;
463
464 if (as && as->type == AS_EXPLICIT)
465 for (dim = 0; dim < se->loop->dimen; dim++)
466 {
467 n = se->loop->order[dim];
468 if (se->loop->to[n] == NULL_TREE)
469 {
470 /* Evaluate the lower bound. */
471 gfc_init_se (&tmpse, NULL);
472 gfc_apply_interface_mapping (mapping, &tmpse, as->lower[dim]);
473 gfc_add_block_to_block (&se->pre, &tmpse.pre);
474 gfc_add_block_to_block (&se->post, &tmpse.post);
475 lower = fold_convert (gfc_array_index_type, tmpse.expr);
476
477 /* ...and the upper bound. */
478 gfc_init_se (&tmpse, NULL);
479 gfc_apply_interface_mapping (mapping, &tmpse, as->upper[dim]);
480 gfc_add_block_to_block (&se->pre, &tmpse.pre);
481 gfc_add_block_to_block (&se->post, &tmpse.post);
482 upper = fold_convert (gfc_array_index_type, tmpse.expr);
483
484 /* Set the upper bound of the loop to UPPER - LOWER. */
485 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
486 tmp = gfc_evaluate_now (tmp, &se->pre);
487 se->loop->to[n] = tmp;
488 }
489 }
490 }
491
492
493 /* Generate code to allocate an array temporary, or create a variable to
494 hold the data. If size is NULL, zero the descriptor so that the
495 callee will allocate the array. If DEALLOC is true, also generate code to
496 free the array afterwards.
497
498 Initialization code is added to PRE and finalization code to POST.
499 DYNAMIC is true if the caller may want to extend the array later
500 using realloc. This prevents us from putting the array on the stack. */
501
502 static void
503 gfc_trans_allocate_array_storage (stmtblock_t * pre, stmtblock_t * post,
504 gfc_ss_info * info, tree size, tree nelem,
505 bool dynamic, bool dealloc)
506 {
507 tree tmp;
508 tree desc;
509 bool onstack;
510
511 desc = info->descriptor;
512 info->offset = gfc_index_zero_node;
513 if (size == NULL_TREE || integer_zerop (size))
514 {
515 /* A callee allocated array. */
516 gfc_conv_descriptor_data_set (pre, desc, null_pointer_node);
517 onstack = FALSE;
518 }
519 else
520 {
521 /* Allocate the temporary. */
522 onstack = !dynamic && gfc_can_put_var_on_stack (size);
523
524 if (onstack)
525 {
526 /* Make a temporary variable to hold the data. */
527 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (nelem), nelem,
528 gfc_index_one_node);
529 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
530 tmp);
531 tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
532 tmp);
533 tmp = gfc_create_var (tmp, "A");
534 tmp = build_fold_addr_expr (tmp);
535 gfc_conv_descriptor_data_set (pre, desc, tmp);
536 }
537 else
538 {
539 /* Allocate memory to hold the data. */
540 tmp = gfc_call_malloc (pre, NULL, size);
541 tmp = gfc_evaluate_now (tmp, pre);
542 gfc_conv_descriptor_data_set (pre, desc, tmp);
543 }
544 }
545 info->data = gfc_conv_descriptor_data_get (desc);
546
547 /* The offset is zero because we create temporaries with a zero
548 lower bound. */
549 tmp = gfc_conv_descriptor_offset (desc);
550 gfc_add_modify_expr (pre, tmp, gfc_index_zero_node);
551
552 if (dealloc && !onstack)
553 {
554 /* Free the temporary. */
555 tmp = gfc_conv_descriptor_data_get (desc);
556 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
557 gfc_add_expr_to_block (post, tmp);
558 }
559 }
560
561
562 /* Generate code to create and initialize the descriptor for a temporary
563 array. This is used for both temporaries needed by the scalarizer, and
564 functions returning arrays. Adjusts the loop variables to be
565 zero-based, and calculates the loop bounds for callee allocated arrays.
566 Allocate the array unless it's callee allocated (we have a callee
567 allocated array if 'callee_alloc' is true, or if loop->to[n] is
568 NULL_TREE for any n). Also fills in the descriptor, data and offset
569 fields of info if known. Returns the size of the array, or NULL for a
570 callee allocated array.
571
572 PRE, POST, DYNAMIC and DEALLOC are as for gfc_trans_allocate_array_storage.
573 */
574
575 tree
576 gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post,
577 gfc_loopinfo * loop, gfc_ss_info * info,
578 tree eltype, bool dynamic, bool dealloc,
579 bool callee_alloc)
580 {
581 tree type;
582 tree desc;
583 tree tmp;
584 tree size;
585 tree nelem;
586 tree cond;
587 tree or_expr;
588 int n;
589 int dim;
590
591 gcc_assert (info->dimen > 0);
592 /* Set the lower bound to zero. */
593 for (dim = 0; dim < info->dimen; dim++)
594 {
595 n = loop->order[dim];
596 /* TODO: Investigate why "if (n < loop->temp_dim)
597 gcc_assert (integer_zerop (loop->from[n]));" fails here. */
598 if (n >= loop->temp_dim)
599 {
600 /* Callee allocated arrays may not have a known bound yet. */
601 if (loop->to[n])
602 loop->to[n] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
603 loop->to[n], loop->from[n]);
604 loop->from[n] = gfc_index_zero_node;
605 }
606
607 info->delta[dim] = gfc_index_zero_node;
608 info->start[dim] = gfc_index_zero_node;
609 info->end[dim] = gfc_index_zero_node;
610 info->stride[dim] = gfc_index_one_node;
611 info->dim[dim] = dim;
612 }
613
614 /* Initialize the descriptor. */
615 type =
616 gfc_get_array_type_bounds (eltype, info->dimen, loop->from, loop->to, 1,
617 GFC_ARRAY_UNKNOWN);
618 desc = gfc_create_var (type, "atmp");
619 GFC_DECL_PACKED_ARRAY (desc) = 1;
620
621 info->descriptor = desc;
622 size = gfc_index_one_node;
623
624 /* Fill in the array dtype. */
625 tmp = gfc_conv_descriptor_dtype (desc);
626 gfc_add_modify_expr (pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
627
628 /*
629 Fill in the bounds and stride. This is a packed array, so:
630
631 size = 1;
632 for (n = 0; n < rank; n++)
633 {
634 stride[n] = size
635 delta = ubound[n] + 1 - lbound[n];
636 size = size * delta;
637 }
638 size = size * sizeof(element);
639 */
640
641 or_expr = NULL_TREE;
642
643 for (n = 0; n < info->dimen; n++)
644 {
645 if (loop->to[n] == NULL_TREE)
646 {
647 /* For a callee allocated array express the loop bounds in terms
648 of the descriptor fields. */
649 tmp =
650 fold_build2 (MINUS_EXPR, gfc_array_index_type,
651 gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]),
652 gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]));
653 loop->to[n] = tmp;
654 size = NULL_TREE;
655 continue;
656 }
657
658 /* Store the stride and bound components in the descriptor. */
659 tmp = gfc_conv_descriptor_stride (desc, gfc_rank_cst[n]);
660 gfc_add_modify_expr (pre, tmp, size);
661
662 tmp = gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]);
663 gfc_add_modify_expr (pre, tmp, gfc_index_zero_node);
664
665 tmp = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]);
666 gfc_add_modify_expr (pre, tmp, loop->to[n]);
667
668 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
669 loop->to[n], gfc_index_one_node);
670
671 /* Check whether the size for this dimension is negative. */
672 cond = fold_build2 (LE_EXPR, boolean_type_node, tmp,
673 gfc_index_zero_node);
674 cond = gfc_evaluate_now (cond, pre);
675
676 if (n == 0)
677 or_expr = cond;
678 else
679 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
680
681 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
682 size = gfc_evaluate_now (size, pre);
683 }
684
685 /* Get the size of the array. */
686
687 if (size && !callee_alloc)
688 {
689 /* If or_expr is true, then the extent in at least one
690 dimension is zero and the size is set to zero. */
691 size = fold_build3 (COND_EXPR, gfc_array_index_type,
692 or_expr, gfc_index_zero_node, size);
693
694 nelem = size;
695 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
696 fold_convert (gfc_array_index_type,
697 TYPE_SIZE_UNIT (gfc_get_element_type (type))));
698 }
699 else
700 {
701 nelem = size;
702 size = NULL_TREE;
703 }
704
705 gfc_trans_allocate_array_storage (pre, post, info, size, nelem, dynamic,
706 dealloc);
707
708 if (info->dimen > loop->temp_dim)
709 loop->temp_dim = info->dimen;
710
711 return size;
712 }
713
714
715 /* Generate code to transpose array EXPR by creating a new descriptor
716 in which the dimension specifications have been reversed. */
717
718 void
719 gfc_conv_array_transpose (gfc_se * se, gfc_expr * expr)
720 {
721 tree dest, src, dest_index, src_index;
722 gfc_loopinfo *loop;
723 gfc_ss_info *dest_info, *src_info;
724 gfc_ss *dest_ss, *src_ss;
725 gfc_se src_se;
726 int n;
727
728 loop = se->loop;
729
730 src_ss = gfc_walk_expr (expr);
731 dest_ss = se->ss;
732
733 src_info = &src_ss->data.info;
734 dest_info = &dest_ss->data.info;
735 gcc_assert (dest_info->dimen == 2);
736 gcc_assert (src_info->dimen == 2);
737
738 /* Get a descriptor for EXPR. */
739 gfc_init_se (&src_se, NULL);
740 gfc_conv_expr_descriptor (&src_se, expr, src_ss);
741 gfc_add_block_to_block (&se->pre, &src_se.pre);
742 gfc_add_block_to_block (&se->post, &src_se.post);
743 src = src_se.expr;
744
745 /* Allocate a new descriptor for the return value. */
746 dest = gfc_create_var (TREE_TYPE (src), "atmp");
747 dest_info->descriptor = dest;
748 se->expr = dest;
749
750 /* Copy across the dtype field. */
751 gfc_add_modify_expr (&se->pre,
752 gfc_conv_descriptor_dtype (dest),
753 gfc_conv_descriptor_dtype (src));
754
755 /* Copy the dimension information, renumbering dimension 1 to 0 and
756 0 to 1. */
757 for (n = 0; n < 2; n++)
758 {
759 dest_info->delta[n] = gfc_index_zero_node;
760 dest_info->start[n] = gfc_index_zero_node;
761 dest_info->end[n] = gfc_index_zero_node;
762 dest_info->stride[n] = gfc_index_one_node;
763 dest_info->dim[n] = n;
764
765 dest_index = gfc_rank_cst[n];
766 src_index = gfc_rank_cst[1 - n];
767
768 gfc_add_modify_expr (&se->pre,
769 gfc_conv_descriptor_stride (dest, dest_index),
770 gfc_conv_descriptor_stride (src, src_index));
771
772 gfc_add_modify_expr (&se->pre,
773 gfc_conv_descriptor_lbound (dest, dest_index),
774 gfc_conv_descriptor_lbound (src, src_index));
775
776 gfc_add_modify_expr (&se->pre,
777 gfc_conv_descriptor_ubound (dest, dest_index),
778 gfc_conv_descriptor_ubound (src, src_index));
779
780 if (!loop->to[n])
781 {
782 gcc_assert (integer_zerop (loop->from[n]));
783 loop->to[n] =
784 fold_build2 (MINUS_EXPR, gfc_array_index_type,
785 gfc_conv_descriptor_ubound (dest, dest_index),
786 gfc_conv_descriptor_lbound (dest, dest_index));
787 }
788 }
789
790 /* Copy the data pointer. */
791 dest_info->data = gfc_conv_descriptor_data_get (src);
792 gfc_conv_descriptor_data_set (&se->pre, dest, dest_info->data);
793
794 /* Copy the offset. This is not changed by transposition; the top-left
795 element is still at the same offset as before, except where the loop
796 starts at zero. */
797 if (!integer_zerop (loop->from[0]))
798 dest_info->offset = gfc_conv_descriptor_offset (src);
799 else
800 dest_info->offset = gfc_index_zero_node;
801
802 gfc_add_modify_expr (&se->pre,
803 gfc_conv_descriptor_offset (dest),
804 dest_info->offset);
805
806 if (dest_info->dimen > loop->temp_dim)
807 loop->temp_dim = dest_info->dimen;
808 }
809
810
811 /* Return the number of iterations in a loop that starts at START,
812 ends at END, and has step STEP. */
813
814 static tree
815 gfc_get_iteration_count (tree start, tree end, tree step)
816 {
817 tree tmp;
818 tree type;
819
820 type = TREE_TYPE (step);
821 tmp = fold_build2 (MINUS_EXPR, type, end, start);
822 tmp = fold_build2 (FLOOR_DIV_EXPR, type, tmp, step);
823 tmp = fold_build2 (PLUS_EXPR, type, tmp, build_int_cst (type, 1));
824 tmp = fold_build2 (MAX_EXPR, type, tmp, build_int_cst (type, 0));
825 return fold_convert (gfc_array_index_type, tmp);
826 }
827
828
829 /* Extend the data in array DESC by EXTRA elements. */
830
831 static void
832 gfc_grow_array (stmtblock_t * pblock, tree desc, tree extra)
833 {
834 tree arg0, arg1;
835 tree tmp;
836 tree size;
837 tree ubound;
838
839 if (integer_zerop (extra))
840 return;
841
842 ubound = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[0]);
843
844 /* Add EXTRA to the upper bound. */
845 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, extra);
846 gfc_add_modify_expr (pblock, ubound, tmp);
847
848 /* Get the value of the current data pointer. */
849 arg0 = gfc_conv_descriptor_data_get (desc);
850
851 /* Calculate the new array size. */
852 size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (desc)));
853 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
854 ubound, gfc_index_one_node);
855 arg1 = fold_build2 (MULT_EXPR, size_type_node,
856 fold_convert (size_type_node, tmp),
857 fold_convert (size_type_node, size));
858
859 /* Call the realloc() function. */
860 tmp = gfc_call_realloc (pblock, arg0, arg1);
861 gfc_conv_descriptor_data_set (pblock, desc, tmp);
862 }
863
864
865 /* Return true if the bounds of iterator I can only be determined
866 at run time. */
867
868 static inline bool
869 gfc_iterator_has_dynamic_bounds (gfc_iterator * i)
870 {
871 return (i->start->expr_type != EXPR_CONSTANT
872 || i->end->expr_type != EXPR_CONSTANT
873 || i->step->expr_type != EXPR_CONSTANT);
874 }
875
876
877 /* Split the size of constructor element EXPR into the sum of two terms,
878 one of which can be determined at compile time and one of which must
879 be calculated at run time. Set *SIZE to the former and return true
880 if the latter might be nonzero. */
881
882 static bool
883 gfc_get_array_constructor_element_size (mpz_t * size, gfc_expr * expr)
884 {
885 if (expr->expr_type == EXPR_ARRAY)
886 return gfc_get_array_constructor_size (size, expr->value.constructor);
887 else if (expr->rank > 0)
888 {
889 /* Calculate everything at run time. */
890 mpz_set_ui (*size, 0);
891 return true;
892 }
893 else
894 {
895 /* A single element. */
896 mpz_set_ui (*size, 1);
897 return false;
898 }
899 }
900
901
902 /* Like gfc_get_array_constructor_element_size, but applied to the whole
903 of array constructor C. */
904
905 static bool
906 gfc_get_array_constructor_size (mpz_t * size, gfc_constructor * c)
907 {
908 gfc_iterator *i;
909 mpz_t val;
910 mpz_t len;
911 bool dynamic;
912
913 mpz_set_ui (*size, 0);
914 mpz_init (len);
915 mpz_init (val);
916
917 dynamic = false;
918 for (; c; c = c->next)
919 {
920 i = c->iterator;
921 if (i && gfc_iterator_has_dynamic_bounds (i))
922 dynamic = true;
923 else
924 {
925 dynamic |= gfc_get_array_constructor_element_size (&len, c->expr);
926 if (i)
927 {
928 /* Multiply the static part of the element size by the
929 number of iterations. */
930 mpz_sub (val, i->end->value.integer, i->start->value.integer);
931 mpz_fdiv_q (val, val, i->step->value.integer);
932 mpz_add_ui (val, val, 1);
933 if (mpz_sgn (val) > 0)
934 mpz_mul (len, len, val);
935 else
936 mpz_set_ui (len, 0);
937 }
938 mpz_add (*size, *size, len);
939 }
940 }
941 mpz_clear (len);
942 mpz_clear (val);
943 return dynamic;
944 }
945
946
947 /* Make sure offset is a variable. */
948
949 static void
950 gfc_put_offset_into_var (stmtblock_t * pblock, tree * poffset,
951 tree * offsetvar)
952 {
953 /* We should have already created the offset variable. We cannot
954 create it here because we may be in an inner scope. */
955 gcc_assert (*offsetvar != NULL_TREE);
956 gfc_add_modify_expr (pblock, *offsetvar, *poffset);
957 *poffset = *offsetvar;
958 TREE_USED (*offsetvar) = 1;
959 }
960
961
962 /* Variables needed for bounds-checking. */
963 static bool first_len;
964 static tree first_len_val;
965 static bool typespec_chararray_ctor;
966
967 static void
968 gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
969 tree offset, gfc_se * se, gfc_expr * expr)
970 {
971 tree tmp;
972
973 gfc_conv_expr (se, expr);
974
975 /* Store the value. */
976 tmp = build_fold_indirect_ref (gfc_conv_descriptor_data_get (desc));
977 tmp = gfc_build_array_ref (tmp, offset, NULL);
978
979 if (expr->ts.type == BT_CHARACTER)
980 {
981 int i = gfc_validate_kind (BT_CHARACTER, expr->ts.kind, false);
982 tree esize;
983
984 esize = size_in_bytes (gfc_get_element_type (TREE_TYPE (desc)));
985 esize = fold_convert (gfc_charlen_type_node, esize);
986 esize = fold_build2 (TRUNC_DIV_EXPR, gfc_charlen_type_node, esize,
987 build_int_cst (gfc_charlen_type_node,
988 gfc_character_kinds[i].bit_size / 8));
989
990 gfc_conv_string_parameter (se);
991 if (POINTER_TYPE_P (TREE_TYPE (tmp)))
992 {
993 /* The temporary is an array of pointers. */
994 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
995 gfc_add_modify_expr (&se->pre, tmp, se->expr);
996 }
997 else
998 {
999 /* The temporary is an array of string values. */
1000 tmp = gfc_build_addr_expr (gfc_get_pchar_type (expr->ts.kind), tmp);
1001 /* We know the temporary and the value will be the same length,
1002 so can use memcpy. */
1003 gfc_trans_string_copy (&se->pre, esize, tmp, expr->ts.kind,
1004 se->string_length, se->expr, expr->ts.kind);
1005 }
1006 if (flag_bounds_check && !typespec_chararray_ctor)
1007 {
1008 if (first_len)
1009 {
1010 gfc_add_modify_expr (&se->pre, first_len_val,
1011 se->string_length);
1012 first_len = false;
1013 }
1014 else
1015 {
1016 /* Verify that all constructor elements are of the same
1017 length. */
1018 tree cond = fold_build2 (NE_EXPR, boolean_type_node,
1019 first_len_val, se->string_length);
1020 gfc_trans_runtime_check
1021 (cond, &se->pre, &expr->where,
1022 "Different CHARACTER lengths (%ld/%ld) in array constructor",
1023 fold_convert (long_integer_type_node, first_len_val),
1024 fold_convert (long_integer_type_node, se->string_length));
1025 }
1026 }
1027 }
1028 else
1029 {
1030 /* TODO: Should the frontend already have done this conversion? */
1031 se->expr = fold_convert (TREE_TYPE (tmp), se->expr);
1032 gfc_add_modify_expr (&se->pre, tmp, se->expr);
1033 }
1034
1035 gfc_add_block_to_block (pblock, &se->pre);
1036 gfc_add_block_to_block (pblock, &se->post);
1037 }
1038
1039
1040 /* Add the contents of an array to the constructor. DYNAMIC is as for
1041 gfc_trans_array_constructor_value. */
1042
1043 static void
1044 gfc_trans_array_constructor_subarray (stmtblock_t * pblock,
1045 tree type ATTRIBUTE_UNUSED,
1046 tree desc, gfc_expr * expr,
1047 tree * poffset, tree * offsetvar,
1048 bool dynamic)
1049 {
1050 gfc_se se;
1051 gfc_ss *ss;
1052 gfc_loopinfo loop;
1053 stmtblock_t body;
1054 tree tmp;
1055 tree size;
1056 int n;
1057
1058 /* We need this to be a variable so we can increment it. */
1059 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1060
1061 gfc_init_se (&se, NULL);
1062
1063 /* Walk the array expression. */
1064 ss = gfc_walk_expr (expr);
1065 gcc_assert (ss != gfc_ss_terminator);
1066
1067 /* Initialize the scalarizer. */
1068 gfc_init_loopinfo (&loop);
1069 gfc_add_ss_to_loop (&loop, ss);
1070
1071 /* Initialize the loop. */
1072 gfc_conv_ss_startstride (&loop);
1073 gfc_conv_loop_setup (&loop);
1074
1075 /* Make sure the constructed array has room for the new data. */
1076 if (dynamic)
1077 {
1078 /* Set SIZE to the total number of elements in the subarray. */
1079 size = gfc_index_one_node;
1080 for (n = 0; n < loop.dimen; n++)
1081 {
1082 tmp = gfc_get_iteration_count (loop.from[n], loop.to[n],
1083 gfc_index_one_node);
1084 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1085 }
1086
1087 /* Grow the constructed array by SIZE elements. */
1088 gfc_grow_array (&loop.pre, desc, size);
1089 }
1090
1091 /* Make the loop body. */
1092 gfc_mark_ss_chain_used (ss, 1);
1093 gfc_start_scalarized_body (&loop, &body);
1094 gfc_copy_loopinfo_to_se (&se, &loop);
1095 se.ss = ss;
1096
1097 gfc_trans_array_ctor_element (&body, desc, *poffset, &se, expr);
1098 gcc_assert (se.ss == gfc_ss_terminator);
1099
1100 /* Increment the offset. */
1101 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1102 *poffset, gfc_index_one_node);
1103 gfc_add_modify_expr (&body, *poffset, tmp);
1104
1105 /* Finish the loop. */
1106 gfc_trans_scalarizing_loops (&loop, &body);
1107 gfc_add_block_to_block (&loop.pre, &loop.post);
1108 tmp = gfc_finish_block (&loop.pre);
1109 gfc_add_expr_to_block (pblock, tmp);
1110
1111 gfc_cleanup_loop (&loop);
1112 }
1113
1114
1115 /* Assign the values to the elements of an array constructor. DYNAMIC
1116 is true if descriptor DESC only contains enough data for the static
1117 size calculated by gfc_get_array_constructor_size. When true, memory
1118 for the dynamic parts must be allocated using realloc. */
1119
1120 static void
1121 gfc_trans_array_constructor_value (stmtblock_t * pblock, tree type,
1122 tree desc, gfc_constructor * c,
1123 tree * poffset, tree * offsetvar,
1124 bool dynamic)
1125 {
1126 tree tmp;
1127 stmtblock_t body;
1128 gfc_se se;
1129 mpz_t size;
1130
1131 mpz_init (size);
1132 for (; c; c = c->next)
1133 {
1134 /* If this is an iterator or an array, the offset must be a variable. */
1135 if ((c->iterator || c->expr->rank > 0) && INTEGER_CST_P (*poffset))
1136 gfc_put_offset_into_var (pblock, poffset, offsetvar);
1137
1138 gfc_start_block (&body);
1139
1140 if (c->expr->expr_type == EXPR_ARRAY)
1141 {
1142 /* Array constructors can be nested. */
1143 gfc_trans_array_constructor_value (&body, type, desc,
1144 c->expr->value.constructor,
1145 poffset, offsetvar, dynamic);
1146 }
1147 else if (c->expr->rank > 0)
1148 {
1149 gfc_trans_array_constructor_subarray (&body, type, desc, c->expr,
1150 poffset, offsetvar, dynamic);
1151 }
1152 else
1153 {
1154 /* This code really upsets the gimplifier so don't bother for now. */
1155 gfc_constructor *p;
1156 HOST_WIDE_INT n;
1157 HOST_WIDE_INT size;
1158
1159 p = c;
1160 n = 0;
1161 while (p && !(p->iterator || p->expr->expr_type != EXPR_CONSTANT))
1162 {
1163 p = p->next;
1164 n++;
1165 }
1166 if (n < 4)
1167 {
1168 /* Scalar values. */
1169 gfc_init_se (&se, NULL);
1170 gfc_trans_array_ctor_element (&body, desc, *poffset,
1171 &se, c->expr);
1172
1173 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1174 *poffset, gfc_index_one_node);
1175 }
1176 else
1177 {
1178 /* Collect multiple scalar constants into a constructor. */
1179 tree list;
1180 tree init;
1181 tree bound;
1182 tree tmptype;
1183
1184 p = c;
1185 list = NULL_TREE;
1186 /* Count the number of consecutive scalar constants. */
1187 while (p && !(p->iterator
1188 || p->expr->expr_type != EXPR_CONSTANT))
1189 {
1190 gfc_init_se (&se, NULL);
1191 gfc_conv_constant (&se, p->expr);
1192
1193 /* For constant character array constructors we build
1194 an array of pointers. */
1195 if (p->expr->ts.type == BT_CHARACTER
1196 && POINTER_TYPE_P (type))
1197 se.expr = gfc_build_addr_expr
1198 (gfc_get_pchar_type (p->expr->ts.kind),
1199 se.expr);
1200
1201 list = tree_cons (NULL_TREE, se.expr, list);
1202 c = p;
1203 p = p->next;
1204 }
1205
1206 bound = build_int_cst (NULL_TREE, n - 1);
1207 /* Create an array type to hold them. */
1208 tmptype = build_range_type (gfc_array_index_type,
1209 gfc_index_zero_node, bound);
1210 tmptype = build_array_type (type, tmptype);
1211
1212 init = build_constructor_from_list (tmptype, nreverse (list));
1213 TREE_CONSTANT (init) = 1;
1214 TREE_STATIC (init) = 1;
1215 /* Create a static variable to hold the data. */
1216 tmp = gfc_create_var (tmptype, "data");
1217 TREE_STATIC (tmp) = 1;
1218 TREE_CONSTANT (tmp) = 1;
1219 TREE_READONLY (tmp) = 1;
1220 DECL_INITIAL (tmp) = init;
1221 init = tmp;
1222
1223 /* Use BUILTIN_MEMCPY to assign the values. */
1224 tmp = gfc_conv_descriptor_data_get (desc);
1225 tmp = build_fold_indirect_ref (tmp);
1226 tmp = gfc_build_array_ref (tmp, *poffset, NULL);
1227 tmp = build_fold_addr_expr (tmp);
1228 init = build_fold_addr_expr (init);
1229
1230 size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
1231 bound = build_int_cst (NULL_TREE, n * size);
1232 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMCPY], 3,
1233 tmp, init, bound);
1234 gfc_add_expr_to_block (&body, tmp);
1235
1236 *poffset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1237 *poffset,
1238 build_int_cst (gfc_array_index_type, n));
1239 }
1240 if (!INTEGER_CST_P (*poffset))
1241 {
1242 gfc_add_modify_expr (&body, *offsetvar, *poffset);
1243 *poffset = *offsetvar;
1244 }
1245 }
1246
1247 /* The frontend should already have done any expansions possible
1248 at compile-time. */
1249 if (!c->iterator)
1250 {
1251 /* Pass the code as is. */
1252 tmp = gfc_finish_block (&body);
1253 gfc_add_expr_to_block (pblock, tmp);
1254 }
1255 else
1256 {
1257 /* Build the implied do-loop. */
1258 tree cond;
1259 tree end;
1260 tree step;
1261 tree loopvar;
1262 tree exit_label;
1263 tree loopbody;
1264 tree tmp2;
1265 tree tmp_loopvar;
1266
1267 loopbody = gfc_finish_block (&body);
1268
1269 if (c->iterator->var->symtree->n.sym->backend_decl)
1270 {
1271 gfc_init_se (&se, NULL);
1272 gfc_conv_expr (&se, c->iterator->var);
1273 gfc_add_block_to_block (pblock, &se.pre);
1274 loopvar = se.expr;
1275 }
1276 else
1277 {
1278 /* If the iterator appears in a specification expression in
1279 an interface mapping, we need to make a temp for the loop
1280 variable because it is not declared locally. */
1281 loopvar = gfc_typenode_for_spec (&c->iterator->var->ts);
1282 loopvar = gfc_create_var (loopvar, "loopvar");
1283 }
1284
1285 /* Make a temporary, store the current value in that
1286 and return it, once the loop is done. */
1287 tmp_loopvar = gfc_create_var (TREE_TYPE (loopvar), "loopvar");
1288 gfc_add_modify_expr (pblock, tmp_loopvar, loopvar);
1289
1290 /* Initialize the loop. */
1291 gfc_init_se (&se, NULL);
1292 gfc_conv_expr_val (&se, c->iterator->start);
1293 gfc_add_block_to_block (pblock, &se.pre);
1294 gfc_add_modify_expr (pblock, loopvar, se.expr);
1295
1296 gfc_init_se (&se, NULL);
1297 gfc_conv_expr_val (&se, c->iterator->end);
1298 gfc_add_block_to_block (pblock, &se.pre);
1299 end = gfc_evaluate_now (se.expr, pblock);
1300
1301 gfc_init_se (&se, NULL);
1302 gfc_conv_expr_val (&se, c->iterator->step);
1303 gfc_add_block_to_block (pblock, &se.pre);
1304 step = gfc_evaluate_now (se.expr, pblock);
1305
1306 /* If this array expands dynamically, and the number of iterations
1307 is not constant, we won't have allocated space for the static
1308 part of C->EXPR's size. Do that now. */
1309 if (dynamic && gfc_iterator_has_dynamic_bounds (c->iterator))
1310 {
1311 /* Get the number of iterations. */
1312 tmp = gfc_get_iteration_count (loopvar, end, step);
1313
1314 /* Get the static part of C->EXPR's size. */
1315 gfc_get_array_constructor_element_size (&size, c->expr);
1316 tmp2 = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1317
1318 /* Grow the array by TMP * TMP2 elements. */
1319 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, tmp2);
1320 gfc_grow_array (pblock, desc, tmp);
1321 }
1322
1323 /* Generate the loop body. */
1324 exit_label = gfc_build_label_decl (NULL_TREE);
1325 gfc_start_block (&body);
1326
1327 /* Generate the exit condition. Depending on the sign of
1328 the step variable we have to generate the correct
1329 comparison. */
1330 tmp = fold_build2 (GT_EXPR, boolean_type_node, step,
1331 build_int_cst (TREE_TYPE (step), 0));
1332 cond = fold_build3 (COND_EXPR, boolean_type_node, tmp,
1333 fold_build2 (GT_EXPR, boolean_type_node,
1334 loopvar, end),
1335 fold_build2 (LT_EXPR, boolean_type_node,
1336 loopvar, end));
1337 tmp = build1_v (GOTO_EXPR, exit_label);
1338 TREE_USED (exit_label) = 1;
1339 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
1340 gfc_add_expr_to_block (&body, tmp);
1341
1342 /* The main loop body. */
1343 gfc_add_expr_to_block (&body, loopbody);
1344
1345 /* Increase loop variable by step. */
1346 tmp = fold_build2 (PLUS_EXPR, TREE_TYPE (loopvar), loopvar, step);
1347 gfc_add_modify_expr (&body, loopvar, tmp);
1348
1349 /* Finish the loop. */
1350 tmp = gfc_finish_block (&body);
1351 tmp = build1_v (LOOP_EXPR, tmp);
1352 gfc_add_expr_to_block (pblock, tmp);
1353
1354 /* Add the exit label. */
1355 tmp = build1_v (LABEL_EXPR, exit_label);
1356 gfc_add_expr_to_block (pblock, tmp);
1357
1358 /* Restore the original value of the loop counter. */
1359 gfc_add_modify_expr (pblock, loopvar, tmp_loopvar);
1360 }
1361 }
1362 mpz_clear (size);
1363 }
1364
1365
1366 /* Figure out the string length of a variable reference expression.
1367 Used by get_array_ctor_strlen. */
1368
1369 static void
1370 get_array_ctor_var_strlen (gfc_expr * expr, tree * len)
1371 {
1372 gfc_ref *ref;
1373 gfc_typespec *ts;
1374 mpz_t char_len;
1375
1376 /* Don't bother if we already know the length is a constant. */
1377 if (*len && INTEGER_CST_P (*len))
1378 return;
1379
1380 ts = &expr->symtree->n.sym->ts;
1381 for (ref = expr->ref; ref; ref = ref->next)
1382 {
1383 switch (ref->type)
1384 {
1385 case REF_ARRAY:
1386 /* Array references don't change the string length. */
1387 break;
1388
1389 case REF_COMPONENT:
1390 /* Use the length of the component. */
1391 ts = &ref->u.c.component->ts;
1392 break;
1393
1394 case REF_SUBSTRING:
1395 if (ref->u.ss.start->expr_type != EXPR_CONSTANT
1396 || ref->u.ss.end->expr_type != EXPR_CONSTANT)
1397 break;
1398 mpz_init_set_ui (char_len, 1);
1399 mpz_add (char_len, char_len, ref->u.ss.end->value.integer);
1400 mpz_sub (char_len, char_len, ref->u.ss.start->value.integer);
1401 *len = gfc_conv_mpz_to_tree (char_len, gfc_default_integer_kind);
1402 *len = convert (gfc_charlen_type_node, *len);
1403 mpz_clear (char_len);
1404 return;
1405
1406 default:
1407 /* TODO: Substrings are tricky because we can't evaluate the
1408 expression more than once. For now we just give up, and hope
1409 we can figure it out elsewhere. */
1410 return;
1411 }
1412 }
1413
1414 *len = ts->cl->backend_decl;
1415 }
1416
1417
1418 /* A catch-all to obtain the string length for anything that is not a
1419 constant, array or variable. */
1420 static void
1421 get_array_ctor_all_strlen (stmtblock_t *block, gfc_expr *e, tree *len)
1422 {
1423 gfc_se se;
1424 gfc_ss *ss;
1425
1426 /* Don't bother if we already know the length is a constant. */
1427 if (*len && INTEGER_CST_P (*len))
1428 return;
1429
1430 if (!e->ref && e->ts.cl && e->ts.cl->length
1431 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
1432 {
1433 /* This is easy. */
1434 gfc_conv_const_charlen (e->ts.cl);
1435 *len = e->ts.cl->backend_decl;
1436 }
1437 else
1438 {
1439 /* Otherwise, be brutal even if inefficient. */
1440 ss = gfc_walk_expr (e);
1441 gfc_init_se (&se, NULL);
1442
1443 /* No function call, in case of side effects. */
1444 se.no_function_call = 1;
1445 if (ss == gfc_ss_terminator)
1446 gfc_conv_expr (&se, e);
1447 else
1448 gfc_conv_expr_descriptor (&se, e, ss);
1449
1450 /* Fix the value. */
1451 *len = gfc_evaluate_now (se.string_length, &se.pre);
1452
1453 gfc_add_block_to_block (block, &se.pre);
1454 gfc_add_block_to_block (block, &se.post);
1455
1456 e->ts.cl->backend_decl = *len;
1457 }
1458 }
1459
1460
1461 /* Figure out the string length of a character array constructor.
1462 If len is NULL, don't calculate the length; this happens for recursive calls
1463 when a sub-array-constructor is an element but not at the first position,
1464 so when we're not interested in the length.
1465 Returns TRUE if all elements are character constants. */
1466
1467 bool
1468 get_array_ctor_strlen (stmtblock_t *block, gfc_constructor * c, tree * len)
1469 {
1470 bool is_const;
1471
1472 is_const = TRUE;
1473
1474 if (c == NULL)
1475 {
1476 if (len)
1477 *len = build_int_cstu (gfc_charlen_type_node, 0);
1478 return is_const;
1479 }
1480
1481 /* Loop over all constructor elements to find out is_const, but in len we
1482 want to store the length of the first, not the last, element. We can
1483 of course exit the loop as soon as is_const is found to be false. */
1484 for (; c && is_const; c = c->next)
1485 {
1486 switch (c->expr->expr_type)
1487 {
1488 case EXPR_CONSTANT:
1489 if (len && !(*len && INTEGER_CST_P (*len)))
1490 *len = build_int_cstu (gfc_charlen_type_node,
1491 c->expr->value.character.length);
1492 break;
1493
1494 case EXPR_ARRAY:
1495 if (!get_array_ctor_strlen (block, c->expr->value.constructor, len))
1496 is_const = false;
1497 break;
1498
1499 case EXPR_VARIABLE:
1500 is_const = false;
1501 if (len)
1502 get_array_ctor_var_strlen (c->expr, len);
1503 break;
1504
1505 default:
1506 is_const = false;
1507 if (len)
1508 get_array_ctor_all_strlen (block, c->expr, len);
1509 break;
1510 }
1511
1512 /* After the first iteration, we don't want the length modified. */
1513 len = NULL;
1514 }
1515
1516 return is_const;
1517 }
1518
1519 /* Check whether the array constructor C consists entirely of constant
1520 elements, and if so returns the number of those elements, otherwise
1521 return zero. Note, an empty or NULL array constructor returns zero. */
1522
1523 unsigned HOST_WIDE_INT
1524 gfc_constant_array_constructor_p (gfc_constructor * c)
1525 {
1526 unsigned HOST_WIDE_INT nelem = 0;
1527
1528 while (c)
1529 {
1530 if (c->iterator
1531 || c->expr->rank > 0
1532 || c->expr->expr_type != EXPR_CONSTANT)
1533 return 0;
1534 c = c->next;
1535 nelem++;
1536 }
1537 return nelem;
1538 }
1539
1540
1541 /* Given EXPR, the constant array constructor specified by an EXPR_ARRAY,
1542 and the tree type of it's elements, TYPE, return a static constant
1543 variable that is compile-time initialized. */
1544
1545 tree
1546 gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
1547 {
1548 tree tmptype, list, init, tmp;
1549 HOST_WIDE_INT nelem;
1550 gfc_constructor *c;
1551 gfc_array_spec as;
1552 gfc_se se;
1553 int i;
1554
1555 /* First traverse the constructor list, converting the constants
1556 to tree to build an initializer. */
1557 nelem = 0;
1558 list = NULL_TREE;
1559 c = expr->value.constructor;
1560 while (c)
1561 {
1562 gfc_init_se (&se, NULL);
1563 gfc_conv_constant (&se, c->expr);
1564 if (c->expr->ts.type == BT_CHARACTER && POINTER_TYPE_P (type))
1565 se.expr = gfc_build_addr_expr (gfc_get_pchar_type (c->expr->ts.kind),
1566 se.expr);
1567 list = tree_cons (NULL_TREE, se.expr, list);
1568 c = c->next;
1569 nelem++;
1570 }
1571
1572 /* Next determine the tree type for the array. We use the gfortran
1573 front-end's gfc_get_nodesc_array_type in order to create a suitable
1574 GFC_ARRAY_TYPE_P that may be used by the scalarizer. */
1575
1576 memset (&as, 0, sizeof (gfc_array_spec));
1577
1578 as.rank = expr->rank;
1579 as.type = AS_EXPLICIT;
1580 if (!expr->shape)
1581 {
1582 as.lower[0] = gfc_int_expr (0);
1583 as.upper[0] = gfc_int_expr (nelem - 1);
1584 }
1585 else
1586 for (i = 0; i < expr->rank; i++)
1587 {
1588 int tmp = (int) mpz_get_si (expr->shape[i]);
1589 as.lower[i] = gfc_int_expr (0);
1590 as.upper[i] = gfc_int_expr (tmp - 1);
1591 }
1592
1593 tmptype = gfc_get_nodesc_array_type (type, &as, PACKED_STATIC);
1594
1595 init = build_constructor_from_list (tmptype, nreverse (list));
1596
1597 TREE_CONSTANT (init) = 1;
1598 TREE_STATIC (init) = 1;
1599
1600 tmp = gfc_create_var (tmptype, "A");
1601 TREE_STATIC (tmp) = 1;
1602 TREE_CONSTANT (tmp) = 1;
1603 TREE_READONLY (tmp) = 1;
1604 DECL_INITIAL (tmp) = init;
1605
1606 return tmp;
1607 }
1608
1609
1610 /* Translate a constant EXPR_ARRAY array constructor for the scalarizer.
1611 This mostly initializes the scalarizer state info structure with the
1612 appropriate values to directly use the array created by the function
1613 gfc_build_constant_array_constructor. */
1614
1615 static void
1616 gfc_trans_constant_array_constructor (gfc_loopinfo * loop,
1617 gfc_ss * ss, tree type)
1618 {
1619 gfc_ss_info *info;
1620 tree tmp;
1621 int i;
1622
1623 tmp = gfc_build_constant_array_constructor (ss->expr, type);
1624
1625 info = &ss->data.info;
1626
1627 info->descriptor = tmp;
1628 info->data = build_fold_addr_expr (tmp);
1629 info->offset = fold_build1 (NEGATE_EXPR, gfc_array_index_type,
1630 loop->from[0]);
1631
1632 for (i = 0; i < info->dimen; i++)
1633 {
1634 info->delta[i] = gfc_index_zero_node;
1635 info->start[i] = gfc_index_zero_node;
1636 info->end[i] = gfc_index_zero_node;
1637 info->stride[i] = gfc_index_one_node;
1638 info->dim[i] = i;
1639 }
1640
1641 if (info->dimen > loop->temp_dim)
1642 loop->temp_dim = info->dimen;
1643 }
1644
1645 /* Helper routine of gfc_trans_array_constructor to determine if the
1646 bounds of the loop specified by LOOP are constant and simple enough
1647 to use with gfc_trans_constant_array_constructor. Returns the
1648 the iteration count of the loop if suitable, and NULL_TREE otherwise. */
1649
1650 static tree
1651 constant_array_constructor_loop_size (gfc_loopinfo * loop)
1652 {
1653 tree size = gfc_index_one_node;
1654 tree tmp;
1655 int i;
1656
1657 for (i = 0; i < loop->dimen; i++)
1658 {
1659 /* If the bounds aren't constant, return NULL_TREE. */
1660 if (!INTEGER_CST_P (loop->from[i]) || !INTEGER_CST_P (loop->to[i]))
1661 return NULL_TREE;
1662 if (!integer_zerop (loop->from[i]))
1663 {
1664 /* Only allow nonzero "from" in one-dimensional arrays. */
1665 if (loop->dimen != 1)
1666 return NULL_TREE;
1667 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1668 loop->to[i], loop->from[i]);
1669 }
1670 else
1671 tmp = loop->to[i];
1672 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1673 tmp, gfc_index_one_node);
1674 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
1675 }
1676
1677 return size;
1678 }
1679
1680
1681 /* Array constructors are handled by constructing a temporary, then using that
1682 within the scalarization loop. This is not optimal, but seems by far the
1683 simplest method. */
1684
1685 static void
1686 gfc_trans_array_constructor (gfc_loopinfo * loop, gfc_ss * ss)
1687 {
1688 gfc_constructor *c;
1689 tree offset;
1690 tree offsetvar;
1691 tree desc;
1692 tree type;
1693 tree loopfrom;
1694 bool dynamic;
1695
1696 /* Do bounds-checking here and in gfc_trans_array_ctor_element only if no
1697 typespec was given for the array constructor. */
1698 typespec_chararray_ctor = (ss->expr->ts.cl
1699 && ss->expr->ts.cl->length_from_typespec);
1700
1701 if (flag_bounds_check && ss->expr->ts.type == BT_CHARACTER
1702 && !typespec_chararray_ctor)
1703 {
1704 first_len_val = gfc_create_var (gfc_charlen_type_node, "len");
1705 first_len = true;
1706 }
1707
1708 ss->data.info.dimen = loop->dimen;
1709
1710 c = ss->expr->value.constructor;
1711 if (ss->expr->ts.type == BT_CHARACTER)
1712 {
1713 bool const_string;
1714
1715 /* get_array_ctor_strlen walks the elements of the constructor, if a
1716 typespec was given, we already know the string length and want the one
1717 specified there. */
1718 if (typespec_chararray_ctor && ss->expr->ts.cl->length
1719 && ss->expr->ts.cl->length->expr_type != EXPR_CONSTANT)
1720 {
1721 gfc_se length_se;
1722
1723 const_string = false;
1724 gfc_init_se (&length_se, NULL);
1725 gfc_conv_expr_type (&length_se, ss->expr->ts.cl->length,
1726 gfc_charlen_type_node);
1727 ss->string_length = length_se.expr;
1728 gfc_add_block_to_block (&loop->pre, &length_se.pre);
1729 gfc_add_block_to_block (&loop->post, &length_se.post);
1730 }
1731 else
1732 const_string = get_array_ctor_strlen (&loop->pre, c,
1733 &ss->string_length);
1734
1735 /* Complex character array constructors should have been taken care of
1736 and not end up here. */
1737 gcc_assert (ss->string_length);
1738
1739 ss->expr->ts.cl->backend_decl = ss->string_length;
1740
1741 type = gfc_get_character_type_len (ss->expr->ts.kind, ss->string_length);
1742 if (const_string)
1743 type = build_pointer_type (type);
1744 }
1745 else
1746 type = gfc_typenode_for_spec (&ss->expr->ts);
1747
1748 /* See if the constructor determines the loop bounds. */
1749 dynamic = false;
1750
1751 if (ss->expr->shape && loop->dimen > 1 && loop->to[0] == NULL_TREE)
1752 {
1753 /* We have a multidimensional parameter. */
1754 int n;
1755 for (n = 0; n < ss->expr->rank; n++)
1756 {
1757 loop->from[n] = gfc_index_zero_node;
1758 loop->to[n] = gfc_conv_mpz_to_tree (ss->expr->shape [n],
1759 gfc_index_integer_kind);
1760 loop->to[n] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1761 loop->to[n], gfc_index_one_node);
1762 }
1763 }
1764
1765 if (loop->to[0] == NULL_TREE)
1766 {
1767 mpz_t size;
1768
1769 /* We should have a 1-dimensional, zero-based loop. */
1770 gcc_assert (loop->dimen == 1);
1771 gcc_assert (integer_zerop (loop->from[0]));
1772
1773 /* Split the constructor size into a static part and a dynamic part.
1774 Allocate the static size up-front and record whether the dynamic
1775 size might be nonzero. */
1776 mpz_init (size);
1777 dynamic = gfc_get_array_constructor_size (&size, c);
1778 mpz_sub_ui (size, size, 1);
1779 loop->to[0] = gfc_conv_mpz_to_tree (size, gfc_index_integer_kind);
1780 mpz_clear (size);
1781 }
1782
1783 /* Special case constant array constructors. */
1784 if (!dynamic)
1785 {
1786 unsigned HOST_WIDE_INT nelem = gfc_constant_array_constructor_p (c);
1787 if (nelem > 0)
1788 {
1789 tree size = constant_array_constructor_loop_size (loop);
1790 if (size && compare_tree_int (size, nelem) == 0)
1791 {
1792 gfc_trans_constant_array_constructor (loop, ss, type);
1793 return;
1794 }
1795 }
1796 }
1797
1798 /* Temporarily reset the loop variables, so that the returned temporary
1799 has the right size and bounds. This seems only to be necessary for
1800 1D arrays. */
1801 if (!integer_zerop (loop->from[0]) && loop->dimen == 1)
1802 {
1803 loopfrom = loop->from[0];
1804 loop->from[0] = gfc_index_zero_node;
1805 loop->to[0] = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1806 loop->to[0], loopfrom);
1807 }
1808 else
1809 loopfrom = NULL_TREE;
1810
1811 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop, &ss->data.info,
1812 type, dynamic, true, false);
1813
1814 if (loopfrom != NULL_TREE)
1815 {
1816 loop->from[0] = loopfrom;
1817 loop->to[0] = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1818 loop->to[0], loopfrom);
1819 /* In the case of a non-zero from, the temporary needs an offset
1820 so that subsequent indexing is correct. */
1821 ss->data.info.offset = fold_build1 (NEGATE_EXPR,
1822 gfc_array_index_type,
1823 loop->from[0]);
1824 }
1825
1826 desc = ss->data.info.descriptor;
1827 offset = gfc_index_zero_node;
1828 offsetvar = gfc_create_var_np (gfc_array_index_type, "offset");
1829 TREE_NO_WARNING (offsetvar) = 1;
1830 TREE_USED (offsetvar) = 0;
1831 gfc_trans_array_constructor_value (&loop->pre, type, desc, c,
1832 &offset, &offsetvar, dynamic);
1833
1834 /* If the array grows dynamically, the upper bound of the loop variable
1835 is determined by the array's final upper bound. */
1836 if (dynamic)
1837 loop->to[0] = gfc_conv_descriptor_ubound (desc, gfc_rank_cst[0]);
1838
1839 if (TREE_USED (offsetvar))
1840 pushdecl (offsetvar);
1841 else
1842 gcc_assert (INTEGER_CST_P (offset));
1843 #if 0
1844 /* Disable bound checking for now because it's probably broken. */
1845 if (flag_bounds_check)
1846 {
1847 gcc_unreachable ();
1848 }
1849 #endif
1850 }
1851
1852
1853 /* INFO describes a GFC_SS_SECTION in loop LOOP, and this function is
1854 called after evaluating all of INFO's vector dimensions. Go through
1855 each such vector dimension and see if we can now fill in any missing
1856 loop bounds. */
1857
1858 static void
1859 gfc_set_vector_loop_bounds (gfc_loopinfo * loop, gfc_ss_info * info)
1860 {
1861 gfc_se se;
1862 tree tmp;
1863 tree desc;
1864 tree zero;
1865 int n;
1866 int dim;
1867
1868 for (n = 0; n < loop->dimen; n++)
1869 {
1870 dim = info->dim[n];
1871 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR
1872 && loop->to[n] == NULL)
1873 {
1874 /* Loop variable N indexes vector dimension DIM, and we don't
1875 yet know the upper bound of loop variable N. Set it to the
1876 difference between the vector's upper and lower bounds. */
1877 gcc_assert (loop->from[n] == gfc_index_zero_node);
1878 gcc_assert (info->subscript[dim]
1879 && info->subscript[dim]->type == GFC_SS_VECTOR);
1880
1881 gfc_init_se (&se, NULL);
1882 desc = info->subscript[dim]->data.info.descriptor;
1883 zero = gfc_rank_cst[0];
1884 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1885 gfc_conv_descriptor_ubound (desc, zero),
1886 gfc_conv_descriptor_lbound (desc, zero));
1887 tmp = gfc_evaluate_now (tmp, &loop->pre);
1888 loop->to[n] = tmp;
1889 }
1890 }
1891 }
1892
1893
1894 /* Add the pre and post chains for all the scalar expressions in a SS chain
1895 to loop. This is called after the loop parameters have been calculated,
1896 but before the actual scalarizing loops. */
1897
1898 static void
1899 gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript)
1900 {
1901 gfc_se se;
1902 int n;
1903
1904 /* TODO: This can generate bad code if there are ordering dependencies.
1905 eg. a callee allocated function and an unknown size constructor. */
1906 gcc_assert (ss != NULL);
1907
1908 for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
1909 {
1910 gcc_assert (ss);
1911
1912 switch (ss->type)
1913 {
1914 case GFC_SS_SCALAR:
1915 /* Scalar expression. Evaluate this now. This includes elemental
1916 dimension indices, but not array section bounds. */
1917 gfc_init_se (&se, NULL);
1918 gfc_conv_expr (&se, ss->expr);
1919 gfc_add_block_to_block (&loop->pre, &se.pre);
1920
1921 if (ss->expr->ts.type != BT_CHARACTER)
1922 {
1923 /* Move the evaluation of scalar expressions outside the
1924 scalarization loop, except for WHERE assignments. */
1925 if (subscript)
1926 se.expr = convert(gfc_array_index_type, se.expr);
1927 if (!ss->where)
1928 se.expr = gfc_evaluate_now (se.expr, &loop->pre);
1929 gfc_add_block_to_block (&loop->pre, &se.post);
1930 }
1931 else
1932 gfc_add_block_to_block (&loop->post, &se.post);
1933
1934 ss->data.scalar.expr = se.expr;
1935 ss->string_length = se.string_length;
1936 break;
1937
1938 case GFC_SS_REFERENCE:
1939 /* Scalar reference. Evaluate this now. */
1940 gfc_init_se (&se, NULL);
1941 gfc_conv_expr_reference (&se, ss->expr);
1942 gfc_add_block_to_block (&loop->pre, &se.pre);
1943 gfc_add_block_to_block (&loop->post, &se.post);
1944
1945 ss->data.scalar.expr = gfc_evaluate_now (se.expr, &loop->pre);
1946 ss->string_length = se.string_length;
1947 break;
1948
1949 case GFC_SS_SECTION:
1950 /* Add the expressions for scalar and vector subscripts. */
1951 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1952 if (ss->data.info.subscript[n])
1953 gfc_add_loop_ss_code (loop, ss->data.info.subscript[n], true);
1954
1955 gfc_set_vector_loop_bounds (loop, &ss->data.info);
1956 break;
1957
1958 case GFC_SS_VECTOR:
1959 /* Get the vector's descriptor and store it in SS. */
1960 gfc_init_se (&se, NULL);
1961 gfc_conv_expr_descriptor (&se, ss->expr, gfc_walk_expr (ss->expr));
1962 gfc_add_block_to_block (&loop->pre, &se.pre);
1963 gfc_add_block_to_block (&loop->post, &se.post);
1964 ss->data.info.descriptor = se.expr;
1965 break;
1966
1967 case GFC_SS_INTRINSIC:
1968 gfc_add_intrinsic_ss_code (loop, ss);
1969 break;
1970
1971 case GFC_SS_FUNCTION:
1972 /* Array function return value. We call the function and save its
1973 result in a temporary for use inside the loop. */
1974 gfc_init_se (&se, NULL);
1975 se.loop = loop;
1976 se.ss = ss;
1977 gfc_conv_expr (&se, ss->expr);
1978 gfc_add_block_to_block (&loop->pre, &se.pre);
1979 gfc_add_block_to_block (&loop->post, &se.post);
1980 ss->string_length = se.string_length;
1981 break;
1982
1983 case GFC_SS_CONSTRUCTOR:
1984 if (ss->expr->ts.type == BT_CHARACTER
1985 && ss->string_length == NULL
1986 && ss->expr->ts.cl
1987 && ss->expr->ts.cl->length)
1988 {
1989 gfc_init_se (&se, NULL);
1990 gfc_conv_expr_type (&se, ss->expr->ts.cl->length,
1991 gfc_charlen_type_node);
1992 ss->string_length = se.expr;
1993 gfc_add_block_to_block (&loop->pre, &se.pre);
1994 gfc_add_block_to_block (&loop->post, &se.post);
1995 }
1996 gfc_trans_array_constructor (loop, ss);
1997 break;
1998
1999 case GFC_SS_TEMP:
2000 case GFC_SS_COMPONENT:
2001 /* Do nothing. These are handled elsewhere. */
2002 break;
2003
2004 default:
2005 gcc_unreachable ();
2006 }
2007 }
2008 }
2009
2010
2011 /* Translate expressions for the descriptor and data pointer of a SS. */
2012 /*GCC ARRAYS*/
2013
2014 static void
2015 gfc_conv_ss_descriptor (stmtblock_t * block, gfc_ss * ss, int base)
2016 {
2017 gfc_se se;
2018 tree tmp;
2019
2020 /* Get the descriptor for the array to be scalarized. */
2021 gcc_assert (ss->expr->expr_type == EXPR_VARIABLE);
2022 gfc_init_se (&se, NULL);
2023 se.descriptor_only = 1;
2024 gfc_conv_expr_lhs (&se, ss->expr);
2025 gfc_add_block_to_block (block, &se.pre);
2026 ss->data.info.descriptor = se.expr;
2027 ss->string_length = se.string_length;
2028
2029 if (base)
2030 {
2031 /* Also the data pointer. */
2032 tmp = gfc_conv_array_data (se.expr);
2033 /* If this is a variable or address of a variable we use it directly.
2034 Otherwise we must evaluate it now to avoid breaking dependency
2035 analysis by pulling the expressions for elemental array indices
2036 inside the loop. */
2037 if (!(DECL_P (tmp)
2038 || (TREE_CODE (tmp) == ADDR_EXPR
2039 && DECL_P (TREE_OPERAND (tmp, 0)))))
2040 tmp = gfc_evaluate_now (tmp, block);
2041 ss->data.info.data = tmp;
2042
2043 tmp = gfc_conv_array_offset (se.expr);
2044 ss->data.info.offset = gfc_evaluate_now (tmp, block);
2045 }
2046 }
2047
2048
2049 /* Initialize a gfc_loopinfo structure. */
2050
2051 void
2052 gfc_init_loopinfo (gfc_loopinfo * loop)
2053 {
2054 int n;
2055
2056 memset (loop, 0, sizeof (gfc_loopinfo));
2057 gfc_init_block (&loop->pre);
2058 gfc_init_block (&loop->post);
2059
2060 /* Initially scalarize in order. */
2061 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
2062 loop->order[n] = n;
2063
2064 loop->ss = gfc_ss_terminator;
2065 }
2066
2067
2068 /* Copies the loop variable info to a gfc_se structure. Does not copy the SS
2069 chain. */
2070
2071 void
2072 gfc_copy_loopinfo_to_se (gfc_se * se, gfc_loopinfo * loop)
2073 {
2074 se->loop = loop;
2075 }
2076
2077
2078 /* Return an expression for the data pointer of an array. */
2079
2080 tree
2081 gfc_conv_array_data (tree descriptor)
2082 {
2083 tree type;
2084
2085 type = TREE_TYPE (descriptor);
2086 if (GFC_ARRAY_TYPE_P (type))
2087 {
2088 if (TREE_CODE (type) == POINTER_TYPE)
2089 return descriptor;
2090 else
2091 {
2092 /* Descriptorless arrays. */
2093 return build_fold_addr_expr (descriptor);
2094 }
2095 }
2096 else
2097 return gfc_conv_descriptor_data_get (descriptor);
2098 }
2099
2100
2101 /* Return an expression for the base offset of an array. */
2102
2103 tree
2104 gfc_conv_array_offset (tree descriptor)
2105 {
2106 tree type;
2107
2108 type = TREE_TYPE (descriptor);
2109 if (GFC_ARRAY_TYPE_P (type))
2110 return GFC_TYPE_ARRAY_OFFSET (type);
2111 else
2112 return gfc_conv_descriptor_offset (descriptor);
2113 }
2114
2115
2116 /* Get an expression for the array stride. */
2117
2118 tree
2119 gfc_conv_array_stride (tree descriptor, int dim)
2120 {
2121 tree tmp;
2122 tree type;
2123
2124 type = TREE_TYPE (descriptor);
2125
2126 /* For descriptorless arrays use the array size. */
2127 tmp = GFC_TYPE_ARRAY_STRIDE (type, dim);
2128 if (tmp != NULL_TREE)
2129 return tmp;
2130
2131 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[dim]);
2132 return tmp;
2133 }
2134
2135
2136 /* Like gfc_conv_array_stride, but for the lower bound. */
2137
2138 tree
2139 gfc_conv_array_lbound (tree descriptor, int dim)
2140 {
2141 tree tmp;
2142 tree type;
2143
2144 type = TREE_TYPE (descriptor);
2145
2146 tmp = GFC_TYPE_ARRAY_LBOUND (type, dim);
2147 if (tmp != NULL_TREE)
2148 return tmp;
2149
2150 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[dim]);
2151 return tmp;
2152 }
2153
2154
2155 /* Like gfc_conv_array_stride, but for the upper bound. */
2156
2157 tree
2158 gfc_conv_array_ubound (tree descriptor, int dim)
2159 {
2160 tree tmp;
2161 tree type;
2162
2163 type = TREE_TYPE (descriptor);
2164
2165 tmp = GFC_TYPE_ARRAY_UBOUND (type, dim);
2166 if (tmp != NULL_TREE)
2167 return tmp;
2168
2169 /* This should only ever happen when passing an assumed shape array
2170 as an actual parameter. The value will never be used. */
2171 if (GFC_ARRAY_TYPE_P (TREE_TYPE (descriptor)))
2172 return gfc_index_zero_node;
2173
2174 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[dim]);
2175 return tmp;
2176 }
2177
2178
2179 /* Generate code to perform an array index bound check. */
2180
2181 static tree
2182 gfc_trans_array_bound_check (gfc_se * se, tree descriptor, tree index, int n,
2183 locus * where, bool check_upper)
2184 {
2185 tree fault;
2186 tree tmp;
2187 char *msg;
2188 const char * name = NULL;
2189
2190 if (!flag_bounds_check)
2191 return index;
2192
2193 index = gfc_evaluate_now (index, &se->pre);
2194
2195 /* We find a name for the error message. */
2196 if (se->ss)
2197 name = se->ss->expr->symtree->name;
2198
2199 if (!name && se->loop && se->loop->ss && se->loop->ss->expr
2200 && se->loop->ss->expr->symtree)
2201 name = se->loop->ss->expr->symtree->name;
2202
2203 if (!name && se->loop && se->loop->ss && se->loop->ss->loop_chain
2204 && se->loop->ss->loop_chain->expr
2205 && se->loop->ss->loop_chain->expr->symtree)
2206 name = se->loop->ss->loop_chain->expr->symtree->name;
2207
2208 if (!name && se->loop && se->loop->ss && se->loop->ss->loop_chain
2209 && se->loop->ss->loop_chain->expr->symtree)
2210 name = se->loop->ss->loop_chain->expr->symtree->name;
2211
2212 if (!name && se->loop && se->loop->ss && se->loop->ss->expr)
2213 {
2214 if (se->loop->ss->expr->expr_type == EXPR_FUNCTION
2215 && se->loop->ss->expr->value.function.name)
2216 name = se->loop->ss->expr->value.function.name;
2217 else
2218 if (se->loop->ss->type == GFC_SS_CONSTRUCTOR
2219 || se->loop->ss->type == GFC_SS_SCALAR)
2220 name = "unnamed constant";
2221 }
2222
2223 /* Check lower bound. */
2224 tmp = gfc_conv_array_lbound (descriptor, n);
2225 fault = fold_build2 (LT_EXPR, boolean_type_node, index, tmp);
2226 if (name)
2227 asprintf (&msg, "%s for array '%s', lower bound of dimension %d exceeded"
2228 "(%%ld < %%ld)", gfc_msg_fault, name, n+1);
2229 else
2230 asprintf (&msg, "%s, lower bound of dimension %d exceeded (%%ld < %%ld)",
2231 gfc_msg_fault, n+1);
2232 gfc_trans_runtime_check (fault, &se->pre, where, msg,
2233 fold_convert (long_integer_type_node, index),
2234 fold_convert (long_integer_type_node, tmp));
2235 gfc_free (msg);
2236
2237 /* Check upper bound. */
2238 if (check_upper)
2239 {
2240 tmp = gfc_conv_array_ubound (descriptor, n);
2241 fault = fold_build2 (GT_EXPR, boolean_type_node, index, tmp);
2242 if (name)
2243 asprintf (&msg, "%s for array '%s', upper bound of dimension %d "
2244 " exceeded (%%ld > %%ld)", gfc_msg_fault, name, n+1);
2245 else
2246 asprintf (&msg, "%s, upper bound of dimension %d exceeded (%%ld > %%ld)",
2247 gfc_msg_fault, n+1);
2248 gfc_trans_runtime_check (fault, &se->pre, where, msg,
2249 fold_convert (long_integer_type_node, index),
2250 fold_convert (long_integer_type_node, tmp));
2251 gfc_free (msg);
2252 }
2253
2254 return index;
2255 }
2256
2257
2258 /* Return the offset for an index. Performs bound checking for elemental
2259 dimensions. Single element references are processed separately. */
2260
2261 static tree
2262 gfc_conv_array_index_offset (gfc_se * se, gfc_ss_info * info, int dim, int i,
2263 gfc_array_ref * ar, tree stride)
2264 {
2265 tree index;
2266 tree desc;
2267 tree data;
2268
2269 /* Get the index into the array for this dimension. */
2270 if (ar)
2271 {
2272 gcc_assert (ar->type != AR_ELEMENT);
2273 switch (ar->dimen_type[dim])
2274 {
2275 case DIMEN_ELEMENT:
2276 /* Elemental dimension. */
2277 gcc_assert (info->subscript[dim]
2278 && info->subscript[dim]->type == GFC_SS_SCALAR);
2279 /* We've already translated this value outside the loop. */
2280 index = info->subscript[dim]->data.scalar.expr;
2281
2282 index = gfc_trans_array_bound_check (se, info->descriptor,
2283 index, dim, &ar->where,
2284 (ar->as->type != AS_ASSUMED_SIZE
2285 && !ar->as->cp_was_assumed) || dim < ar->dimen - 1);
2286 break;
2287
2288 case DIMEN_VECTOR:
2289 gcc_assert (info && se->loop);
2290 gcc_assert (info->subscript[dim]
2291 && info->subscript[dim]->type == GFC_SS_VECTOR);
2292 desc = info->subscript[dim]->data.info.descriptor;
2293
2294 /* Get a zero-based index into the vector. */
2295 index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2296 se->loop->loopvar[i], se->loop->from[i]);
2297
2298 /* Multiply the index by the stride. */
2299 index = fold_build2 (MULT_EXPR, gfc_array_index_type,
2300 index, gfc_conv_array_stride (desc, 0));
2301
2302 /* Read the vector to get an index into info->descriptor. */
2303 data = build_fold_indirect_ref (gfc_conv_array_data (desc));
2304 index = gfc_build_array_ref (data, index, NULL);
2305 index = gfc_evaluate_now (index, &se->pre);
2306
2307 /* Do any bounds checking on the final info->descriptor index. */
2308 index = gfc_trans_array_bound_check (se, info->descriptor,
2309 index, dim, &ar->where,
2310 (ar->as->type != AS_ASSUMED_SIZE
2311 && !ar->as->cp_was_assumed) || dim < ar->dimen - 1);
2312 break;
2313
2314 case DIMEN_RANGE:
2315 /* Scalarized dimension. */
2316 gcc_assert (info && se->loop);
2317
2318 /* Multiply the loop variable by the stride and delta. */
2319 index = se->loop->loopvar[i];
2320 if (!integer_onep (info->stride[i]))
2321 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index,
2322 info->stride[i]);
2323 if (!integer_zerop (info->delta[i]))
2324 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index,
2325 info->delta[i]);
2326 break;
2327
2328 default:
2329 gcc_unreachable ();
2330 }
2331 }
2332 else
2333 {
2334 /* Temporary array or derived type component. */
2335 gcc_assert (se->loop);
2336 index = se->loop->loopvar[se->loop->order[i]];
2337 if (!integer_zerop (info->delta[i]))
2338 index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2339 index, info->delta[i]);
2340 }
2341
2342 /* Multiply by the stride. */
2343 if (!integer_onep (stride))
2344 index = fold_build2 (MULT_EXPR, gfc_array_index_type, index, stride);
2345
2346 return index;
2347 }
2348
2349
2350 /* Build a scalarized reference to an array. */
2351
2352 static void
2353 gfc_conv_scalarized_array_ref (gfc_se * se, gfc_array_ref * ar)
2354 {
2355 gfc_ss_info *info;
2356 tree decl = NULL_TREE;
2357 tree index;
2358 tree tmp;
2359 int n;
2360
2361 info = &se->ss->data.info;
2362 if (ar)
2363 n = se->loop->order[0];
2364 else
2365 n = 0;
2366
2367 index = gfc_conv_array_index_offset (se, info, info->dim[n], n, ar,
2368 info->stride0);
2369 /* Add the offset for this dimension to the stored offset for all other
2370 dimensions. */
2371 if (!integer_zerop (info->offset))
2372 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, info->offset);
2373
2374 if (se->ss->expr && is_subref_array (se->ss->expr))
2375 decl = se->ss->expr->symtree->n.sym->backend_decl;
2376
2377 tmp = build_fold_indirect_ref (info->data);
2378 se->expr = gfc_build_array_ref (tmp, index, decl);
2379 }
2380
2381
2382 /* Translate access of temporary array. */
2383
2384 void
2385 gfc_conv_tmp_array_ref (gfc_se * se)
2386 {
2387 se->string_length = se->ss->string_length;
2388 gfc_conv_scalarized_array_ref (se, NULL);
2389 }
2390
2391
2392 /* Build an array reference. se->expr already holds the array descriptor.
2393 This should be either a variable, indirect variable reference or component
2394 reference. For arrays which do not have a descriptor, se->expr will be
2395 the data pointer.
2396 a(i, j, k) = base[offset + i * stride[0] + j * stride[1] + k * stride[2]]*/
2397
2398 void
2399 gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, gfc_symbol * sym,
2400 locus * where)
2401 {
2402 int n;
2403 tree index;
2404 tree tmp;
2405 tree stride;
2406 gfc_se indexse;
2407
2408 /* Handle scalarized references separately. */
2409 if (ar->type != AR_ELEMENT)
2410 {
2411 gfc_conv_scalarized_array_ref (se, ar);
2412 gfc_advance_se_ss_chain (se);
2413 return;
2414 }
2415
2416 index = gfc_index_zero_node;
2417
2418 /* Calculate the offsets from all the dimensions. */
2419 for (n = 0; n < ar->dimen; n++)
2420 {
2421 /* Calculate the index for this dimension. */
2422 gfc_init_se (&indexse, se);
2423 gfc_conv_expr_type (&indexse, ar->start[n], gfc_array_index_type);
2424 gfc_add_block_to_block (&se->pre, &indexse.pre);
2425
2426 if (flag_bounds_check)
2427 {
2428 /* Check array bounds. */
2429 tree cond;
2430 char *msg;
2431
2432 /* Evaluate the indexse.expr only once. */
2433 indexse.expr = save_expr (indexse.expr);
2434
2435 /* Lower bound. */
2436 tmp = gfc_conv_array_lbound (se->expr, n);
2437 cond = fold_build2 (LT_EXPR, boolean_type_node,
2438 indexse.expr, tmp);
2439 asprintf (&msg, "%s for array '%s', "
2440 "lower bound of dimension %d exceeded (%%ld < %%ld)",
2441 gfc_msg_fault, sym->name, n+1);
2442 gfc_trans_runtime_check (cond, &se->pre, where, msg,
2443 fold_convert (long_integer_type_node,
2444 indexse.expr),
2445 fold_convert (long_integer_type_node, tmp));
2446 gfc_free (msg);
2447
2448 /* Upper bound, but not for the last dimension of assumed-size
2449 arrays. */
2450 if (n < ar->dimen - 1
2451 || (ar->as->type != AS_ASSUMED_SIZE && !ar->as->cp_was_assumed))
2452 {
2453 tmp = gfc_conv_array_ubound (se->expr, n);
2454 cond = fold_build2 (GT_EXPR, boolean_type_node,
2455 indexse.expr, tmp);
2456 asprintf (&msg, "%s for array '%s', "
2457 "upper bound of dimension %d exceeded (%%ld > %%ld)",
2458 gfc_msg_fault, sym->name, n+1);
2459 gfc_trans_runtime_check (cond, &se->pre, where, msg,
2460 fold_convert (long_integer_type_node,
2461 indexse.expr),
2462 fold_convert (long_integer_type_node, tmp));
2463 gfc_free (msg);
2464 }
2465 }
2466
2467 /* Multiply the index by the stride. */
2468 stride = gfc_conv_array_stride (se->expr, n);
2469 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, indexse.expr,
2470 stride);
2471
2472 /* And add it to the total. */
2473 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
2474 }
2475
2476 tmp = gfc_conv_array_offset (se->expr);
2477 if (!integer_zerop (tmp))
2478 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, index, tmp);
2479
2480 /* Access the calculated element. */
2481 tmp = gfc_conv_array_data (se->expr);
2482 tmp = build_fold_indirect_ref (tmp);
2483 se->expr = gfc_build_array_ref (tmp, index, sym->backend_decl);
2484 }
2485
2486
2487 /* Generate the code to be executed immediately before entering a
2488 scalarization loop. */
2489
2490 static void
2491 gfc_trans_preloop_setup (gfc_loopinfo * loop, int dim, int flag,
2492 stmtblock_t * pblock)
2493 {
2494 tree index;
2495 tree stride;
2496 gfc_ss_info *info;
2497 gfc_ss *ss;
2498 gfc_se se;
2499 int i;
2500
2501 /* This code will be executed before entering the scalarization loop
2502 for this dimension. */
2503 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2504 {
2505 if ((ss->useflags & flag) == 0)
2506 continue;
2507
2508 if (ss->type != GFC_SS_SECTION
2509 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2510 && ss->type != GFC_SS_COMPONENT)
2511 continue;
2512
2513 info = &ss->data.info;
2514
2515 if (dim >= info->dimen)
2516 continue;
2517
2518 if (dim == info->dimen - 1)
2519 {
2520 /* For the outermost loop calculate the offset due to any
2521 elemental dimensions. It will have been initialized with the
2522 base offset of the array. */
2523 if (info->ref)
2524 {
2525 for (i = 0; i < info->ref->u.ar.dimen; i++)
2526 {
2527 if (info->ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2528 continue;
2529
2530 gfc_init_se (&se, NULL);
2531 se.loop = loop;
2532 se.expr = info->descriptor;
2533 stride = gfc_conv_array_stride (info->descriptor, i);
2534 index = gfc_conv_array_index_offset (&se, info, i, -1,
2535 &info->ref->u.ar,
2536 stride);
2537 gfc_add_block_to_block (pblock, &se.pre);
2538
2539 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2540 info->offset, index);
2541 info->offset = gfc_evaluate_now (info->offset, pblock);
2542 }
2543
2544 i = loop->order[0];
2545 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2546 }
2547 else
2548 stride = gfc_conv_array_stride (info->descriptor, 0);
2549
2550 /* Calculate the stride of the innermost loop. Hopefully this will
2551 allow the backend optimizers to do their stuff more effectively.
2552 */
2553 info->stride0 = gfc_evaluate_now (stride, pblock);
2554 }
2555 else
2556 {
2557 /* Add the offset for the previous loop dimension. */
2558 gfc_array_ref *ar;
2559
2560 if (info->ref)
2561 {
2562 ar = &info->ref->u.ar;
2563 i = loop->order[dim + 1];
2564 }
2565 else
2566 {
2567 ar = NULL;
2568 i = dim + 1;
2569 }
2570
2571 gfc_init_se (&se, NULL);
2572 se.loop = loop;
2573 se.expr = info->descriptor;
2574 stride = gfc_conv_array_stride (info->descriptor, info->dim[i]);
2575 index = gfc_conv_array_index_offset (&se, info, info->dim[i], i,
2576 ar, stride);
2577 gfc_add_block_to_block (pblock, &se.pre);
2578 info->offset = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2579 info->offset, index);
2580 info->offset = gfc_evaluate_now (info->offset, pblock);
2581 }
2582
2583 /* Remember this offset for the second loop. */
2584 if (dim == loop->temp_dim - 1)
2585 info->saved_offset = info->offset;
2586 }
2587 }
2588
2589
2590 /* Start a scalarized expression. Creates a scope and declares loop
2591 variables. */
2592
2593 void
2594 gfc_start_scalarized_body (gfc_loopinfo * loop, stmtblock_t * pbody)
2595 {
2596 int dim;
2597 int n;
2598 int flags;
2599
2600 gcc_assert (!loop->array_parameter);
2601
2602 for (dim = loop->dimen - 1; dim >= 0; dim--)
2603 {
2604 n = loop->order[dim];
2605
2606 gfc_start_block (&loop->code[n]);
2607
2608 /* Create the loop variable. */
2609 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "S");
2610
2611 if (dim < loop->temp_dim)
2612 flags = 3;
2613 else
2614 flags = 1;
2615 /* Calculate values that will be constant within this loop. */
2616 gfc_trans_preloop_setup (loop, dim, flags, &loop->code[n]);
2617 }
2618 gfc_start_block (pbody);
2619 }
2620
2621
2622 /* Generates the actual loop code for a scalarization loop. */
2623
2624 static void
2625 gfc_trans_scalarized_loop_end (gfc_loopinfo * loop, int n,
2626 stmtblock_t * pbody)
2627 {
2628 stmtblock_t block;
2629 tree cond;
2630 tree tmp;
2631 tree loopbody;
2632 tree exit_label;
2633
2634 loopbody = gfc_finish_block (pbody);
2635
2636 /* Initialize the loopvar. */
2637 gfc_add_modify_expr (&loop->code[n], loop->loopvar[n], loop->from[n]);
2638
2639 exit_label = gfc_build_label_decl (NULL_TREE);
2640
2641 /* Generate the loop body. */
2642 gfc_init_block (&block);
2643
2644 /* The exit condition. */
2645 cond = fold_build2 (GT_EXPR, boolean_type_node,
2646 loop->loopvar[n], loop->to[n]);
2647 tmp = build1_v (GOTO_EXPR, exit_label);
2648 TREE_USED (exit_label) = 1;
2649 tmp = build3_v (COND_EXPR, cond, tmp, build_empty_stmt ());
2650 gfc_add_expr_to_block (&block, tmp);
2651
2652 /* The main body. */
2653 gfc_add_expr_to_block (&block, loopbody);
2654
2655 /* Increment the loopvar. */
2656 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2657 loop->loopvar[n], gfc_index_one_node);
2658 gfc_add_modify_expr (&block, loop->loopvar[n], tmp);
2659
2660 /* Build the loop. */
2661 tmp = gfc_finish_block (&block);
2662 tmp = build1_v (LOOP_EXPR, tmp);
2663 gfc_add_expr_to_block (&loop->code[n], tmp);
2664
2665 /* Add the exit label. */
2666 tmp = build1_v (LABEL_EXPR, exit_label);
2667 gfc_add_expr_to_block (&loop->code[n], tmp);
2668 }
2669
2670
2671 /* Finishes and generates the loops for a scalarized expression. */
2672
2673 void
2674 gfc_trans_scalarizing_loops (gfc_loopinfo * loop, stmtblock_t * body)
2675 {
2676 int dim;
2677 int n;
2678 gfc_ss *ss;
2679 stmtblock_t *pblock;
2680 tree tmp;
2681
2682 pblock = body;
2683 /* Generate the loops. */
2684 for (dim = 0; dim < loop->dimen; dim++)
2685 {
2686 n = loop->order[dim];
2687 gfc_trans_scalarized_loop_end (loop, n, pblock);
2688 loop->loopvar[n] = NULL_TREE;
2689 pblock = &loop->code[n];
2690 }
2691
2692 tmp = gfc_finish_block (pblock);
2693 gfc_add_expr_to_block (&loop->pre, tmp);
2694
2695 /* Clear all the used flags. */
2696 for (ss = loop->ss; ss; ss = ss->loop_chain)
2697 ss->useflags = 0;
2698 }
2699
2700
2701 /* Finish the main body of a scalarized expression, and start the secondary
2702 copying body. */
2703
2704 void
2705 gfc_trans_scalarized_loop_boundary (gfc_loopinfo * loop, stmtblock_t * body)
2706 {
2707 int dim;
2708 int n;
2709 stmtblock_t *pblock;
2710 gfc_ss *ss;
2711
2712 pblock = body;
2713 /* We finish as many loops as are used by the temporary. */
2714 for (dim = 0; dim < loop->temp_dim - 1; dim++)
2715 {
2716 n = loop->order[dim];
2717 gfc_trans_scalarized_loop_end (loop, n, pblock);
2718 loop->loopvar[n] = NULL_TREE;
2719 pblock = &loop->code[n];
2720 }
2721
2722 /* We don't want to finish the outermost loop entirely. */
2723 n = loop->order[loop->temp_dim - 1];
2724 gfc_trans_scalarized_loop_end (loop, n, pblock);
2725
2726 /* Restore the initial offsets. */
2727 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2728 {
2729 if ((ss->useflags & 2) == 0)
2730 continue;
2731
2732 if (ss->type != GFC_SS_SECTION
2733 && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
2734 && ss->type != GFC_SS_COMPONENT)
2735 continue;
2736
2737 ss->data.info.offset = ss->data.info.saved_offset;
2738 }
2739
2740 /* Restart all the inner loops we just finished. */
2741 for (dim = loop->temp_dim - 2; dim >= 0; dim--)
2742 {
2743 n = loop->order[dim];
2744
2745 gfc_start_block (&loop->code[n]);
2746
2747 loop->loopvar[n] = gfc_create_var (gfc_array_index_type, "Q");
2748
2749 gfc_trans_preloop_setup (loop, dim, 2, &loop->code[n]);
2750 }
2751
2752 /* Start a block for the secondary copying code. */
2753 gfc_start_block (body);
2754 }
2755
2756
2757 /* Calculate the upper bound of an array section. */
2758
2759 static tree
2760 gfc_conv_section_upper_bound (gfc_ss * ss, int n, stmtblock_t * pblock)
2761 {
2762 int dim;
2763 gfc_expr *end;
2764 tree desc;
2765 tree bound;
2766 gfc_se se;
2767 gfc_ss_info *info;
2768
2769 gcc_assert (ss->type == GFC_SS_SECTION);
2770
2771 info = &ss->data.info;
2772 dim = info->dim[n];
2773
2774 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2775 /* We'll calculate the upper bound once we have access to the
2776 vector's descriptor. */
2777 return NULL;
2778
2779 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2780 desc = info->descriptor;
2781 end = info->ref->u.ar.end[dim];
2782
2783 if (end)
2784 {
2785 /* The upper bound was specified. */
2786 gfc_init_se (&se, NULL);
2787 gfc_conv_expr_type (&se, end, gfc_array_index_type);
2788 gfc_add_block_to_block (pblock, &se.pre);
2789 bound = se.expr;
2790 }
2791 else
2792 {
2793 /* No upper bound was specified, so use the bound of the array. */
2794 bound = gfc_conv_array_ubound (desc, dim);
2795 }
2796
2797 return bound;
2798 }
2799
2800
2801 /* Calculate the lower bound of an array section. */
2802
2803 static void
2804 gfc_conv_section_startstride (gfc_loopinfo * loop, gfc_ss * ss, int n)
2805 {
2806 gfc_expr *start;
2807 gfc_expr *end;
2808 gfc_expr *stride;
2809 tree desc;
2810 gfc_se se;
2811 gfc_ss_info *info;
2812 int dim;
2813
2814 gcc_assert (ss->type == GFC_SS_SECTION);
2815
2816 info = &ss->data.info;
2817 dim = info->dim[n];
2818
2819 if (info->ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
2820 {
2821 /* We use a zero-based index to access the vector. */
2822 info->start[n] = gfc_index_zero_node;
2823 info->end[n] = gfc_index_zero_node;
2824 info->stride[n] = gfc_index_one_node;
2825 return;
2826 }
2827
2828 gcc_assert (info->ref->u.ar.dimen_type[dim] == DIMEN_RANGE);
2829 desc = info->descriptor;
2830 start = info->ref->u.ar.start[dim];
2831 end = info->ref->u.ar.end[dim];
2832 stride = info->ref->u.ar.stride[dim];
2833
2834 /* Calculate the start of the range. For vector subscripts this will
2835 be the range of the vector. */
2836 if (start)
2837 {
2838 /* Specified section start. */
2839 gfc_init_se (&se, NULL);
2840 gfc_conv_expr_type (&se, start, gfc_array_index_type);
2841 gfc_add_block_to_block (&loop->pre, &se.pre);
2842 info->start[n] = se.expr;
2843 }
2844 else
2845 {
2846 /* No lower bound specified so use the bound of the array. */
2847 info->start[n] = gfc_conv_array_lbound (desc, dim);
2848 }
2849 info->start[n] = gfc_evaluate_now (info->start[n], &loop->pre);
2850
2851 /* Similarly calculate the end. Although this is not used in the
2852 scalarizer, it is needed when checking bounds and where the end
2853 is an expression with side-effects. */
2854 if (end)
2855 {
2856 /* Specified section start. */
2857 gfc_init_se (&se, NULL);
2858 gfc_conv_expr_type (&se, end, gfc_array_index_type);
2859 gfc_add_block_to_block (&loop->pre, &se.pre);
2860 info->end[n] = se.expr;
2861 }
2862 else
2863 {
2864 /* No upper bound specified so use the bound of the array. */
2865 info->end[n] = gfc_conv_array_ubound (desc, dim);
2866 }
2867 info->end[n] = gfc_evaluate_now (info->end[n], &loop->pre);
2868
2869 /* Calculate the stride. */
2870 if (stride == NULL)
2871 info->stride[n] = gfc_index_one_node;
2872 else
2873 {
2874 gfc_init_se (&se, NULL);
2875 gfc_conv_expr_type (&se, stride, gfc_array_index_type);
2876 gfc_add_block_to_block (&loop->pre, &se.pre);
2877 info->stride[n] = gfc_evaluate_now (se.expr, &loop->pre);
2878 }
2879 }
2880
2881
2882 /* Calculates the range start and stride for a SS chain. Also gets the
2883 descriptor and data pointer. The range of vector subscripts is the size
2884 of the vector. Array bounds are also checked. */
2885
2886 void
2887 gfc_conv_ss_startstride (gfc_loopinfo * loop)
2888 {
2889 int n;
2890 tree tmp;
2891 gfc_ss *ss;
2892 tree desc;
2893
2894 loop->dimen = 0;
2895 /* Determine the rank of the loop. */
2896 for (ss = loop->ss;
2897 ss != gfc_ss_terminator && loop->dimen == 0; ss = ss->loop_chain)
2898 {
2899 switch (ss->type)
2900 {
2901 case GFC_SS_SECTION:
2902 case GFC_SS_CONSTRUCTOR:
2903 case GFC_SS_FUNCTION:
2904 case GFC_SS_COMPONENT:
2905 loop->dimen = ss->data.info.dimen;
2906 break;
2907
2908 /* As usual, lbound and ubound are exceptions!. */
2909 case GFC_SS_INTRINSIC:
2910 switch (ss->expr->value.function.isym->id)
2911 {
2912 case GFC_ISYM_LBOUND:
2913 case GFC_ISYM_UBOUND:
2914 loop->dimen = ss->data.info.dimen;
2915
2916 default:
2917 break;
2918 }
2919
2920 default:
2921 break;
2922 }
2923 }
2924
2925 /* We should have determined the rank of the expression by now. If
2926 not, that's bad news. */
2927 gcc_assert (loop->dimen != 0);
2928
2929 /* Loop over all the SS in the chain. */
2930 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2931 {
2932 if (ss->expr && ss->expr->shape && !ss->shape)
2933 ss->shape = ss->expr->shape;
2934
2935 switch (ss->type)
2936 {
2937 case GFC_SS_SECTION:
2938 /* Get the descriptor for the array. */
2939 gfc_conv_ss_descriptor (&loop->pre, ss, !loop->array_parameter);
2940
2941 for (n = 0; n < ss->data.info.dimen; n++)
2942 gfc_conv_section_startstride (loop, ss, n);
2943 break;
2944
2945 case GFC_SS_INTRINSIC:
2946 switch (ss->expr->value.function.isym->id)
2947 {
2948 /* Fall through to supply start and stride. */
2949 case GFC_ISYM_LBOUND:
2950 case GFC_ISYM_UBOUND:
2951 break;
2952 default:
2953 continue;
2954 }
2955
2956 case GFC_SS_CONSTRUCTOR:
2957 case GFC_SS_FUNCTION:
2958 for (n = 0; n < ss->data.info.dimen; n++)
2959 {
2960 ss->data.info.start[n] = gfc_index_zero_node;
2961 ss->data.info.end[n] = gfc_index_zero_node;
2962 ss->data.info.stride[n] = gfc_index_one_node;
2963 }
2964 break;
2965
2966 default:
2967 break;
2968 }
2969 }
2970
2971 /* The rest is just runtime bound checking. */
2972 if (flag_bounds_check)
2973 {
2974 stmtblock_t block;
2975 tree lbound, ubound;
2976 tree end;
2977 tree size[GFC_MAX_DIMENSIONS];
2978 tree stride_pos, stride_neg, non_zerosized, tmp2;
2979 gfc_ss_info *info;
2980 char *msg;
2981 int dim;
2982
2983 gfc_start_block (&block);
2984
2985 for (n = 0; n < loop->dimen; n++)
2986 size[n] = NULL_TREE;
2987
2988 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
2989 {
2990 stmtblock_t inner;
2991
2992 if (ss->type != GFC_SS_SECTION)
2993 continue;
2994
2995 gfc_start_block (&inner);
2996
2997 /* TODO: range checking for mapped dimensions. */
2998 info = &ss->data.info;
2999
3000 /* This code only checks ranges. Elemental and vector
3001 dimensions are checked later. */
3002 for (n = 0; n < loop->dimen; n++)
3003 {
3004 bool check_upper;
3005
3006 dim = info->dim[n];
3007 if (info->ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
3008 continue;
3009
3010 if (dim == info->ref->u.ar.dimen - 1
3011 && (info->ref->u.ar.as->type == AS_ASSUMED_SIZE
3012 || info->ref->u.ar.as->cp_was_assumed))
3013 check_upper = false;
3014 else
3015 check_upper = true;
3016
3017 /* Zero stride is not allowed. */
3018 tmp = fold_build2 (EQ_EXPR, boolean_type_node, info->stride[n],
3019 gfc_index_zero_node);
3020 asprintf (&msg, "Zero stride is not allowed, for dimension %d "
3021 "of array '%s'", info->dim[n]+1,
3022 ss->expr->symtree->name);
3023 gfc_trans_runtime_check (tmp, &inner, &ss->expr->where, msg);
3024 gfc_free (msg);
3025
3026 desc = ss->data.info.descriptor;
3027
3028 /* This is the run-time equivalent of resolve.c's
3029 check_dimension(). The logical is more readable there
3030 than it is here, with all the trees. */
3031 lbound = gfc_conv_array_lbound (desc, dim);
3032 end = info->end[n];
3033 if (check_upper)
3034 ubound = gfc_conv_array_ubound (desc, dim);
3035 else
3036 ubound = NULL;
3037
3038 /* non_zerosized is true when the selected range is not
3039 empty. */
3040 stride_pos = fold_build2 (GT_EXPR, boolean_type_node,
3041 info->stride[n], gfc_index_zero_node);
3042 tmp = fold_build2 (LE_EXPR, boolean_type_node, info->start[n],
3043 end);
3044 stride_pos = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3045 stride_pos, tmp);
3046
3047 stride_neg = fold_build2 (LT_EXPR, boolean_type_node,
3048 info->stride[n], gfc_index_zero_node);
3049 tmp = fold_build2 (GE_EXPR, boolean_type_node, info->start[n],
3050 end);
3051 stride_neg = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3052 stride_neg, tmp);
3053 non_zerosized = fold_build2 (TRUTH_OR_EXPR, boolean_type_node,
3054 stride_pos, stride_neg);
3055
3056 /* Check the start of the range against the lower and upper
3057 bounds of the array, if the range is not empty. */
3058 tmp = fold_build2 (LT_EXPR, boolean_type_node, info->start[n],
3059 lbound);
3060 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3061 non_zerosized, tmp);
3062 asprintf (&msg, "%s, lower bound of dimension %d of array '%s'"
3063 " exceeded (%%ld < %%ld)", gfc_msg_fault,
3064 info->dim[n]+1, ss->expr->symtree->name);
3065 gfc_trans_runtime_check (tmp, &inner, &ss->expr->where, msg,
3066 fold_convert (long_integer_type_node,
3067 info->start[n]),
3068 fold_convert (long_integer_type_node,
3069 lbound));
3070 gfc_free (msg);
3071
3072 if (check_upper)
3073 {
3074 tmp = fold_build2 (GT_EXPR, boolean_type_node,
3075 info->start[n], ubound);
3076 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3077 non_zerosized, tmp);
3078 asprintf (&msg, "%s, upper bound of dimension %d of array "
3079 "'%s' exceeded (%%ld > %%ld)", gfc_msg_fault,
3080 info->dim[n]+1, ss->expr->symtree->name);
3081 gfc_trans_runtime_check (tmp, &inner, &ss->expr->where, msg,
3082 fold_convert (long_integer_type_node, info->start[n]),
3083 fold_convert (long_integer_type_node, ubound));
3084 gfc_free (msg);
3085 }
3086
3087 /* Compute the last element of the range, which is not
3088 necessarily "end" (think 0:5:3, which doesn't contain 5)
3089 and check it against both lower and upper bounds. */
3090 tmp2 = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
3091 info->start[n]);
3092 tmp2 = fold_build2 (TRUNC_MOD_EXPR, gfc_array_index_type, tmp2,
3093 info->stride[n]);
3094 tmp2 = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
3095 tmp2);
3096
3097 tmp = fold_build2 (LT_EXPR, boolean_type_node, tmp2, lbound);
3098 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3099 non_zerosized, tmp);
3100 asprintf (&msg, "%s, lower bound of dimension %d of array '%s'"
3101 " exceeded (%%ld < %%ld)", gfc_msg_fault,
3102 info->dim[n]+1, ss->expr->symtree->name);
3103 gfc_trans_runtime_check (tmp, &inner, &ss->expr->where, msg,
3104 fold_convert (long_integer_type_node,
3105 tmp2),
3106 fold_convert (long_integer_type_node,
3107 lbound));
3108 gfc_free (msg);
3109
3110 if (check_upper)
3111 {
3112 tmp = fold_build2 (GT_EXPR, boolean_type_node, tmp2, ubound);
3113 tmp = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
3114 non_zerosized, tmp);
3115 asprintf (&msg, "%s, upper bound of dimension %d of array "
3116 "'%s' exceeded (%%ld > %%ld)", gfc_msg_fault,
3117 info->dim[n]+1, ss->expr->symtree->name);
3118 gfc_trans_runtime_check (tmp, &inner, &ss->expr->where, msg,
3119 fold_convert (long_integer_type_node, tmp2),
3120 fold_convert (long_integer_type_node, ubound));
3121 gfc_free (msg);
3122 }
3123
3124 /* Check the section sizes match. */
3125 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, end,
3126 info->start[n]);
3127 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type, tmp,
3128 info->stride[n]);
3129 tmp = fold_build2 (MAX_EXPR, gfc_array_index_type, tmp,
3130 build_int_cst (gfc_array_index_type, 0));
3131 /* We remember the size of the first section, and check all the
3132 others against this. */
3133 if (size[n])
3134 {
3135 tree tmp3;
3136
3137 tmp3 = fold_build2 (NE_EXPR, boolean_type_node, tmp, size[n]);
3138 asprintf (&msg, "%s, size mismatch for dimension %d "
3139 "of array '%s' (%%ld/%%ld)", gfc_msg_bounds,
3140 info->dim[n]+1, ss->expr->symtree->name);
3141 gfc_trans_runtime_check (tmp3, &inner, &ss->expr->where, msg,
3142 fold_convert (long_integer_type_node, tmp),
3143 fold_convert (long_integer_type_node, size[n]));
3144 gfc_free (msg);
3145 }
3146 else
3147 size[n] = gfc_evaluate_now (tmp, &inner);
3148 }
3149
3150 tmp = gfc_finish_block (&inner);
3151
3152 /* For optional arguments, only check bounds if the argument is
3153 present. */
3154 if (ss->expr->symtree->n.sym->attr.optional
3155 || ss->expr->symtree->n.sym->attr.not_always_present)
3156 tmp = build3_v (COND_EXPR,
3157 gfc_conv_expr_present (ss->expr->symtree->n.sym),
3158 tmp, build_empty_stmt ());
3159
3160 gfc_add_expr_to_block (&block, tmp);
3161
3162 }
3163
3164 tmp = gfc_finish_block (&block);
3165 gfc_add_expr_to_block (&loop->pre, tmp);
3166 }
3167 }
3168
3169
3170 /* Return true if the two SS could be aliased, i.e. both point to the same data
3171 object. */
3172 /* TODO: resolve aliases based on frontend expressions. */
3173
3174 static int
3175 gfc_could_be_alias (gfc_ss * lss, gfc_ss * rss)
3176 {
3177 gfc_ref *lref;
3178 gfc_ref *rref;
3179 gfc_symbol *lsym;
3180 gfc_symbol *rsym;
3181
3182 lsym = lss->expr->symtree->n.sym;
3183 rsym = rss->expr->symtree->n.sym;
3184 if (gfc_symbols_could_alias (lsym, rsym))
3185 return 1;
3186
3187 if (rsym->ts.type != BT_DERIVED
3188 && lsym->ts.type != BT_DERIVED)
3189 return 0;
3190
3191 /* For derived types we must check all the component types. We can ignore
3192 array references as these will have the same base type as the previous
3193 component ref. */
3194 for (lref = lss->expr->ref; lref != lss->data.info.ref; lref = lref->next)
3195 {
3196 if (lref->type != REF_COMPONENT)
3197 continue;
3198
3199 if (gfc_symbols_could_alias (lref->u.c.sym, rsym))
3200 return 1;
3201
3202 for (rref = rss->expr->ref; rref != rss->data.info.ref;
3203 rref = rref->next)
3204 {
3205 if (rref->type != REF_COMPONENT)
3206 continue;
3207
3208 if (gfc_symbols_could_alias (lref->u.c.sym, rref->u.c.sym))
3209 return 1;
3210 }
3211 }
3212
3213 for (rref = rss->expr->ref; rref != rss->data.info.ref; rref = rref->next)
3214 {
3215 if (rref->type != REF_COMPONENT)
3216 break;
3217
3218 if (gfc_symbols_could_alias (rref->u.c.sym, lsym))
3219 return 1;
3220 }
3221
3222 return 0;
3223 }
3224
3225
3226 /* Resolve array data dependencies. Creates a temporary if required. */
3227 /* TODO: Calc dependencies with gfc_expr rather than gfc_ss, and move to
3228 dependency.c. */
3229
3230 void
3231 gfc_conv_resolve_dependencies (gfc_loopinfo * loop, gfc_ss * dest,
3232 gfc_ss * rss)
3233 {
3234 gfc_ss *ss;
3235 gfc_ref *lref;
3236 gfc_ref *rref;
3237 gfc_ref *aref;
3238 int nDepend = 0;
3239 int temp_dim = 0;
3240
3241 loop->temp_ss = NULL;
3242 aref = dest->data.info.ref;
3243 temp_dim = 0;
3244
3245 for (ss = rss; ss != gfc_ss_terminator; ss = ss->next)
3246 {
3247 if (ss->type != GFC_SS_SECTION)
3248 continue;
3249
3250 if (gfc_could_be_alias (dest, ss)
3251 || gfc_are_equivalenced_arrays (dest->expr, ss->expr))
3252 {
3253 nDepend = 1;
3254 break;
3255 }
3256
3257 if (dest->expr->symtree->n.sym == ss->expr->symtree->n.sym)
3258 {
3259 lref = dest->expr->ref;
3260 rref = ss->expr->ref;
3261
3262 nDepend = gfc_dep_resolver (lref, rref);
3263 if (nDepend == 1)
3264 break;
3265 #if 0
3266 /* TODO : loop shifting. */
3267 if (nDepend == 1)
3268 {
3269 /* Mark the dimensions for LOOP SHIFTING */
3270 for (n = 0; n < loop->dimen; n++)
3271 {
3272 int dim = dest->data.info.dim[n];
3273
3274 if (lref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
3275 depends[n] = 2;
3276 else if (! gfc_is_same_range (&lref->u.ar,
3277 &rref->u.ar, dim, 0))
3278 depends[n] = 1;
3279 }
3280
3281 /* Put all the dimensions with dependencies in the
3282 innermost loops. */
3283 dim = 0;
3284 for (n = 0; n < loop->dimen; n++)
3285 {
3286 gcc_assert (loop->order[n] == n);
3287 if (depends[n])
3288 loop->order[dim++] = n;
3289 }
3290 temp_dim = dim;
3291 for (n = 0; n < loop->dimen; n++)
3292 {
3293 if (! depends[n])
3294 loop->order[dim++] = n;
3295 }
3296
3297 gcc_assert (dim == loop->dimen);
3298 break;
3299 }
3300 #endif
3301 }
3302 }
3303
3304 if (nDepend == 1)
3305 {
3306 tree base_type = gfc_typenode_for_spec (&dest->expr->ts);
3307 if (GFC_ARRAY_TYPE_P (base_type)
3308 || GFC_DESCRIPTOR_TYPE_P (base_type))
3309 base_type = gfc_get_element_type (base_type);
3310 loop->temp_ss = gfc_get_ss ();
3311 loop->temp_ss->type = GFC_SS_TEMP;
3312 loop->temp_ss->data.temp.type = base_type;
3313 loop->temp_ss->string_length = dest->string_length;
3314 loop->temp_ss->data.temp.dimen = loop->dimen;
3315 loop->temp_ss->next = gfc_ss_terminator;
3316 gfc_add_ss_to_loop (loop, loop->temp_ss);
3317 }
3318 else
3319 loop->temp_ss = NULL;
3320 }
3321
3322
3323 /* Initialize the scalarization loop. Creates the loop variables. Determines
3324 the range of the loop variables. Creates a temporary if required.
3325 Calculates how to transform from loop variables to array indices for each
3326 expression. Also generates code for scalar expressions which have been
3327 moved outside the loop. */
3328
3329 void
3330 gfc_conv_loop_setup (gfc_loopinfo * loop)
3331 {
3332 int n;
3333 int dim;
3334 gfc_ss_info *info;
3335 gfc_ss_info *specinfo;
3336 gfc_ss *ss;
3337 tree tmp;
3338 tree len;
3339 gfc_ss *loopspec[GFC_MAX_DIMENSIONS];
3340 bool dynamic[GFC_MAX_DIMENSIONS];
3341 gfc_constructor *c;
3342 mpz_t *cshape;
3343 mpz_t i;
3344
3345 mpz_init (i);
3346 for (n = 0; n < loop->dimen; n++)
3347 {
3348 loopspec[n] = NULL;
3349 dynamic[n] = false;
3350 /* We use one SS term, and use that to determine the bounds of the
3351 loop for this dimension. We try to pick the simplest term. */
3352 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3353 {
3354 if (ss->shape)
3355 {
3356 /* The frontend has worked out the size for us. */
3357 loopspec[n] = ss;
3358 continue;
3359 }
3360
3361 if (ss->type == GFC_SS_CONSTRUCTOR)
3362 {
3363 /* An unknown size constructor will always be rank one.
3364 Higher rank constructors will either have known shape,
3365 or still be wrapped in a call to reshape. */
3366 gcc_assert (loop->dimen == 1);
3367
3368 /* Always prefer to use the constructor bounds if the size
3369 can be determined at compile time. Prefer not to otherwise,
3370 since the general case involves realloc, and it's better to
3371 avoid that overhead if possible. */
3372 c = ss->expr->value.constructor;
3373 dynamic[n] = gfc_get_array_constructor_size (&i, c);
3374 if (!dynamic[n] || !loopspec[n])
3375 loopspec[n] = ss;
3376 continue;
3377 }
3378
3379 /* TODO: Pick the best bound if we have a choice between a
3380 function and something else. */
3381 if (ss->type == GFC_SS_FUNCTION)
3382 {
3383 loopspec[n] = ss;
3384 continue;
3385 }
3386
3387 if (ss->type != GFC_SS_SECTION)
3388 continue;
3389
3390 if (loopspec[n])
3391 specinfo = &loopspec[n]->data.info;
3392 else
3393 specinfo = NULL;
3394 info = &ss->data.info;
3395
3396 if (!specinfo)
3397 loopspec[n] = ss;
3398 /* Criteria for choosing a loop specifier (most important first):
3399 doesn't need realloc
3400 stride of one
3401 known stride
3402 known lower bound
3403 known upper bound
3404 */
3405 else if (loopspec[n]->type == GFC_SS_CONSTRUCTOR && dynamic[n])
3406 loopspec[n] = ss;
3407 else if (integer_onep (info->stride[n])
3408 && !integer_onep (specinfo->stride[n]))
3409 loopspec[n] = ss;
3410 else if (INTEGER_CST_P (info->stride[n])
3411 && !INTEGER_CST_P (specinfo->stride[n]))
3412 loopspec[n] = ss;
3413 else if (INTEGER_CST_P (info->start[n])
3414 && !INTEGER_CST_P (specinfo->start[n]))
3415 loopspec[n] = ss;
3416 /* We don't work out the upper bound.
3417 else if (INTEGER_CST_P (info->finish[n])
3418 && ! INTEGER_CST_P (specinfo->finish[n]))
3419 loopspec[n] = ss; */
3420 }
3421
3422 /* We should have found the scalarization loop specifier. If not,
3423 that's bad news. */
3424 gcc_assert (loopspec[n]);
3425
3426 info = &loopspec[n]->data.info;
3427
3428 /* Set the extents of this range. */
3429 cshape = loopspec[n]->shape;
3430 if (cshape && INTEGER_CST_P (info->start[n])
3431 && INTEGER_CST_P (info->stride[n]))
3432 {
3433 loop->from[n] = info->start[n];
3434 mpz_set (i, cshape[n]);
3435 mpz_sub_ui (i, i, 1);
3436 /* To = from + (size - 1) * stride. */
3437 tmp = gfc_conv_mpz_to_tree (i, gfc_index_integer_kind);
3438 if (!integer_onep (info->stride[n]))
3439 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3440 tmp, info->stride[n]);
3441 loop->to[n] = fold_build2 (PLUS_EXPR, gfc_array_index_type,
3442 loop->from[n], tmp);
3443 }
3444 else
3445 {
3446 loop->from[n] = info->start[n];
3447 switch (loopspec[n]->type)
3448 {
3449 case GFC_SS_CONSTRUCTOR:
3450 /* The upper bound is calculated when we expand the
3451 constructor. */
3452 gcc_assert (loop->to[n] == NULL_TREE);
3453 break;
3454
3455 case GFC_SS_SECTION:
3456 loop->to[n] = gfc_conv_section_upper_bound (loopspec[n], n,
3457 &loop->pre);
3458 break;
3459
3460 case GFC_SS_FUNCTION:
3461 /* The loop bound will be set when we generate the call. */
3462 gcc_assert (loop->to[n] == NULL_TREE);
3463 break;
3464
3465 default:
3466 gcc_unreachable ();
3467 }
3468 }
3469
3470 /* Transform everything so we have a simple incrementing variable. */
3471 if (integer_onep (info->stride[n]))
3472 info->delta[n] = gfc_index_zero_node;
3473 else
3474 {
3475 /* Set the delta for this section. */
3476 info->delta[n] = gfc_evaluate_now (loop->from[n], &loop->pre);
3477 /* Number of iterations is (end - start + step) / step.
3478 with start = 0, this simplifies to
3479 last = end / step;
3480 for (i = 0; i<=last; i++){...}; */
3481 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3482 loop->to[n], loop->from[n]);
3483 tmp = fold_build2 (FLOOR_DIV_EXPR, gfc_array_index_type,
3484 tmp, info->stride[n]);
3485 tmp = fold_build2 (MAX_EXPR, gfc_array_index_type, tmp,
3486 build_int_cst (gfc_array_index_type, -1));
3487 loop->to[n] = gfc_evaluate_now (tmp, &loop->pre);
3488 /* Make the loop variable start at 0. */
3489 loop->from[n] = gfc_index_zero_node;
3490 }
3491 }
3492
3493 /* Add all the scalar code that can be taken out of the loops.
3494 This may include calculating the loop bounds, so do it before
3495 allocating the temporary. */
3496 gfc_add_loop_ss_code (loop, loop->ss, false);
3497
3498 /* If we want a temporary then create it. */
3499 if (loop->temp_ss != NULL)
3500 {
3501 gcc_assert (loop->temp_ss->type == GFC_SS_TEMP);
3502
3503 /* Make absolutely sure that this is a complete type. */
3504 if (loop->temp_ss->string_length)
3505 loop->temp_ss->data.temp.type
3506 = gfc_get_character_type_len_for_eltype
3507 (TREE_TYPE (loop->temp_ss->data.temp.type),
3508 loop->temp_ss->string_length);
3509
3510 tmp = loop->temp_ss->data.temp.type;
3511 len = loop->temp_ss->string_length;
3512 n = loop->temp_ss->data.temp.dimen;
3513 memset (&loop->temp_ss->data.info, 0, sizeof (gfc_ss_info));
3514 loop->temp_ss->type = GFC_SS_SECTION;
3515 loop->temp_ss->data.info.dimen = n;
3516 gfc_trans_create_temp_array (&loop->pre, &loop->post, loop,
3517 &loop->temp_ss->data.info, tmp, false, true,
3518 false);
3519 }
3520
3521 for (n = 0; n < loop->temp_dim; n++)
3522 loopspec[loop->order[n]] = NULL;
3523
3524 mpz_clear (i);
3525
3526 /* For array parameters we don't have loop variables, so don't calculate the
3527 translations. */
3528 if (loop->array_parameter)
3529 return;
3530
3531 /* Calculate the translation from loop variables to array indices. */
3532 for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
3533 {
3534 if (ss->type != GFC_SS_SECTION && ss->type != GFC_SS_COMPONENT)
3535 continue;
3536
3537 info = &ss->data.info;
3538
3539 for (n = 0; n < info->dimen; n++)
3540 {
3541 dim = info->dim[n];
3542
3543 /* If we are specifying the range the delta is already set. */
3544 if (loopspec[n] != ss)
3545 {
3546 /* Calculate the offset relative to the loop variable.
3547 First multiply by the stride. */
3548 tmp = loop->from[n];
3549 if (!integer_onep (info->stride[n]))
3550 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3551 tmp, info->stride[n]);
3552
3553 /* Then subtract this from our starting value. */
3554 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3555 info->start[n], tmp);
3556
3557 info->delta[n] = gfc_evaluate_now (tmp, &loop->pre);
3558 }
3559 }
3560 }
3561 }
3562
3563
3564 /* Fills in an array descriptor, and returns the size of the array. The size
3565 will be a simple_val, ie a variable or a constant. Also calculates the
3566 offset of the base. Returns the size of the array.
3567 {
3568 stride = 1;
3569 offset = 0;
3570 for (n = 0; n < rank; n++)
3571 {
3572 a.lbound[n] = specified_lower_bound;
3573 offset = offset + a.lbond[n] * stride;
3574 size = 1 - lbound;
3575 a.ubound[n] = specified_upper_bound;
3576 a.stride[n] = stride;
3577 size = siz >= 0 ? ubound + size : 0; //size = ubound + 1 - lbound
3578 stride = stride * size;
3579 }
3580 return (stride);
3581 } */
3582 /*GCC ARRAYS*/
3583
3584 static tree
3585 gfc_array_init_size (tree descriptor, int rank, tree * poffset,
3586 gfc_expr ** lower, gfc_expr ** upper,
3587 stmtblock_t * pblock)
3588 {
3589 tree type;
3590 tree tmp;
3591 tree size;
3592 tree offset;
3593 tree stride;
3594 tree cond;
3595 tree or_expr;
3596 tree thencase;
3597 tree elsecase;
3598 tree var;
3599 stmtblock_t thenblock;
3600 stmtblock_t elseblock;
3601 gfc_expr *ubound;
3602 gfc_se se;
3603 int n;
3604
3605 type = TREE_TYPE (descriptor);
3606
3607 stride = gfc_index_one_node;
3608 offset = gfc_index_zero_node;
3609
3610 /* Set the dtype. */
3611 tmp = gfc_conv_descriptor_dtype (descriptor);
3612 gfc_add_modify_expr (pblock, tmp, gfc_get_dtype (TREE_TYPE (descriptor)));
3613
3614 or_expr = NULL_TREE;
3615
3616 for (n = 0; n < rank; n++)
3617 {
3618 /* We have 3 possibilities for determining the size of the array:
3619 lower == NULL => lbound = 1, ubound = upper[n]
3620 upper[n] = NULL => lbound = 1, ubound = lower[n]
3621 upper[n] != NULL => lbound = lower[n], ubound = upper[n] */
3622 ubound = upper[n];
3623
3624 /* Set lower bound. */
3625 gfc_init_se (&se, NULL);
3626 if (lower == NULL)
3627 se.expr = gfc_index_one_node;
3628 else
3629 {
3630 gcc_assert (lower[n]);
3631 if (ubound)
3632 {
3633 gfc_conv_expr_type (&se, lower[n], gfc_array_index_type);
3634 gfc_add_block_to_block (pblock, &se.pre);
3635 }
3636 else
3637 {
3638 se.expr = gfc_index_one_node;
3639 ubound = lower[n];
3640 }
3641 }
3642 tmp = gfc_conv_descriptor_lbound (descriptor, gfc_rank_cst[n]);
3643 gfc_add_modify_expr (pblock, tmp, se.expr);
3644
3645 /* Work out the offset for this component. */
3646 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, se.expr, stride);
3647 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
3648
3649 /* Start the calculation for the size of this dimension. */
3650 size = fold_build2 (MINUS_EXPR, gfc_array_index_type,
3651 gfc_index_one_node, se.expr);
3652
3653 /* Set upper bound. */
3654 gfc_init_se (&se, NULL);
3655 gcc_assert (ubound);
3656 gfc_conv_expr_type (&se, ubound, gfc_array_index_type);
3657 gfc_add_block_to_block (pblock, &se.pre);
3658
3659 tmp = gfc_conv_descriptor_ubound (descriptor, gfc_rank_cst[n]);
3660 gfc_add_modify_expr (pblock, tmp, se.expr);
3661
3662 /* Store the stride. */
3663 tmp = gfc_conv_descriptor_stride (descriptor, gfc_rank_cst[n]);
3664 gfc_add_modify_expr (pblock, tmp, stride);
3665
3666 /* Calculate the size of this dimension. */
3667 size = fold_build2 (PLUS_EXPR, gfc_array_index_type, se.expr, size);
3668
3669 /* Check whether the size for this dimension is negative. */
3670 cond = fold_build2 (LE_EXPR, boolean_type_node, size,
3671 gfc_index_zero_node);
3672 if (n == 0)
3673 or_expr = cond;
3674 else
3675 or_expr = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, or_expr, cond);
3676
3677 size = fold_build3 (COND_EXPR, gfc_array_index_type, cond,
3678 gfc_index_zero_node, size);
3679
3680 /* Multiply the stride by the number of elements in this dimension. */
3681 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, size);
3682 stride = gfc_evaluate_now (stride, pblock);
3683 }
3684
3685 /* The stride is the number of elements in the array, so multiply by the
3686 size of an element to get the total size. */
3687 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3688 size = fold_build2 (MULT_EXPR, gfc_array_index_type, stride,
3689 fold_convert (gfc_array_index_type, tmp));
3690
3691 if (poffset != NULL)
3692 {
3693 offset = gfc_evaluate_now (offset, pblock);
3694 *poffset = offset;
3695 }
3696
3697 if (integer_zerop (or_expr))
3698 return size;
3699 if (integer_onep (or_expr))
3700 return gfc_index_zero_node;
3701
3702 var = gfc_create_var (TREE_TYPE (size), "size");
3703 gfc_start_block (&thenblock);
3704 gfc_add_modify_expr (&thenblock, var, gfc_index_zero_node);
3705 thencase = gfc_finish_block (&thenblock);
3706
3707 gfc_start_block (&elseblock);
3708 gfc_add_modify_expr (&elseblock, var, size);
3709 elsecase = gfc_finish_block (&elseblock);
3710
3711 tmp = gfc_evaluate_now (or_expr, pblock);
3712 tmp = build3_v (COND_EXPR, tmp, thencase, elsecase);
3713 gfc_add_expr_to_block (pblock, tmp);
3714
3715 return var;
3716 }
3717
3718
3719 /* Initializes the descriptor and generates a call to _gfor_allocate. Does
3720 the work for an ALLOCATE statement. */
3721 /*GCC ARRAYS*/
3722
3723 bool
3724 gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree pstat)
3725 {
3726 tree tmp;
3727 tree pointer;
3728 tree offset;
3729 tree size;
3730 gfc_expr **lower;
3731 gfc_expr **upper;
3732 gfc_ref *ref, *prev_ref = NULL;
3733 bool allocatable_array;
3734
3735 ref = expr->ref;
3736
3737 /* Find the last reference in the chain. */
3738 while (ref && ref->next != NULL)
3739 {
3740 gcc_assert (ref->type != REF_ARRAY || ref->u.ar.type == AR_ELEMENT);
3741 prev_ref = ref;
3742 ref = ref->next;
3743 }
3744
3745 if (ref == NULL || ref->type != REF_ARRAY)
3746 return false;
3747
3748 if (!prev_ref)
3749 allocatable_array = expr->symtree->n.sym->attr.allocatable;
3750 else
3751 allocatable_array = prev_ref->u.c.component->allocatable;
3752
3753 /* Figure out the size of the array. */
3754 switch (ref->u.ar.type)
3755 {
3756 case AR_ELEMENT:
3757 lower = NULL;
3758 upper = ref->u.ar.start;
3759 break;
3760
3761 case AR_FULL:
3762 gcc_assert (ref->u.ar.as->type == AS_EXPLICIT);
3763
3764 lower = ref->u.ar.as->lower;
3765 upper = ref->u.ar.as->upper;
3766 break;
3767
3768 case AR_SECTION:
3769 lower = ref->u.ar.start;
3770 upper = ref->u.ar.end;
3771 break;
3772
3773 default:
3774 gcc_unreachable ();
3775 break;
3776 }
3777
3778 size = gfc_array_init_size (se->expr, ref->u.ar.as->rank, &offset,
3779 lower, upper, &se->pre);
3780
3781 /* Allocate memory to store the data. */
3782 pointer = gfc_conv_descriptor_data_get (se->expr);
3783 STRIP_NOPS (pointer);
3784
3785 /* The allocate_array variants take the old pointer as first argument. */
3786 if (allocatable_array)
3787 tmp = gfc_allocate_array_with_status (&se->pre, pointer, size, pstat);
3788 else
3789 tmp = gfc_allocate_with_status (&se->pre, size, pstat);
3790 tmp = fold_build2 (MODIFY_EXPR, void_type_node, pointer, tmp);
3791 gfc_add_expr_to_block (&se->pre, tmp);
3792
3793 tmp = gfc_conv_descriptor_offset (se->expr);
3794 gfc_add_modify_expr (&se->pre, tmp, offset);
3795
3796 if (expr->ts.type == BT_DERIVED
3797 && expr->ts.derived->attr.alloc_comp)
3798 {
3799 tmp = gfc_nullify_alloc_comp (expr->ts.derived, se->expr,
3800 ref->u.ar.as->rank);
3801 gfc_add_expr_to_block (&se->pre, tmp);
3802 }
3803
3804 return true;
3805 }
3806
3807
3808 /* Deallocate an array variable. Also used when an allocated variable goes
3809 out of scope. */
3810 /*GCC ARRAYS*/
3811
3812 tree
3813 gfc_array_deallocate (tree descriptor, tree pstat)
3814 {
3815 tree var;
3816 tree tmp;
3817 stmtblock_t block;
3818
3819 gfc_start_block (&block);
3820 /* Get a pointer to the data. */
3821 var = gfc_conv_descriptor_data_get (descriptor);
3822 STRIP_NOPS (var);
3823
3824 /* Parameter is the address of the data component. */
3825 tmp = gfc_deallocate_with_status (var, pstat, false);
3826 gfc_add_expr_to_block (&block, tmp);
3827
3828 /* Zero the data pointer. */
3829 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
3830 var, build_int_cst (TREE_TYPE (var), 0));
3831 gfc_add_expr_to_block (&block, tmp);
3832
3833 return gfc_finish_block (&block);
3834 }
3835
3836
3837 /* Create an array constructor from an initialization expression.
3838 We assume the frontend already did any expansions and conversions. */
3839
3840 tree
3841 gfc_conv_array_initializer (tree type, gfc_expr * expr)
3842 {
3843 gfc_constructor *c;
3844 tree tmp;
3845 mpz_t maxval;
3846 gfc_se se;
3847 HOST_WIDE_INT hi;
3848 unsigned HOST_WIDE_INT lo;
3849 tree index, range;
3850 VEC(constructor_elt,gc) *v = NULL;
3851
3852 switch (expr->expr_type)
3853 {
3854 case EXPR_CONSTANT:
3855 case EXPR_STRUCTURE:
3856 /* A single scalar or derived type value. Create an array with all
3857 elements equal to that value. */
3858 gfc_init_se (&se, NULL);
3859
3860 if (expr->expr_type == EXPR_CONSTANT)
3861 gfc_conv_constant (&se, expr);
3862 else
3863 gfc_conv_structure (&se, expr, 1);
3864
3865 tmp = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3866 gcc_assert (tmp && INTEGER_CST_P (tmp));
3867 hi = TREE_INT_CST_HIGH (tmp);
3868 lo = TREE_INT_CST_LOW (tmp);
3869 lo++;
3870 if (lo == 0)
3871 hi++;
3872 /* This will probably eat buckets of memory for large arrays. */
3873 while (hi != 0 || lo != 0)
3874 {
3875 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, se.expr);
3876 if (lo == 0)
3877 hi--;
3878 lo--;
3879 }
3880 break;
3881
3882 case EXPR_ARRAY:
3883 /* Create a vector of all the elements. */
3884 for (c = expr->value.constructor; c; c = c->next)
3885 {
3886 if (c->iterator)
3887 {
3888 /* Problems occur when we get something like
3889 integer :: a(lots) = (/(i, i=1,lots)/) */
3890 /* TODO: Unexpanded array initializers. */
3891 internal_error
3892 ("Possible frontend bug: array constructor not expanded");
3893 }
3894 if (mpz_cmp_si (c->n.offset, 0) != 0)
3895 index = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
3896 else
3897 index = NULL_TREE;
3898 mpz_init (maxval);
3899 if (mpz_cmp_si (c->repeat, 0) != 0)
3900 {
3901 tree tmp1, tmp2;
3902
3903 mpz_set (maxval, c->repeat);
3904 mpz_add (maxval, c->n.offset, maxval);
3905 mpz_sub_ui (maxval, maxval, 1);
3906 tmp2 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
3907 if (mpz_cmp_si (c->n.offset, 0) != 0)
3908 {
3909 mpz_add_ui (maxval, c->n.offset, 1);
3910 tmp1 = gfc_conv_mpz_to_tree (maxval, gfc_index_integer_kind);
3911 }
3912 else
3913 tmp1 = gfc_conv_mpz_to_tree (c->n.offset, gfc_index_integer_kind);
3914
3915 range = fold_build2 (RANGE_EXPR, integer_type_node, tmp1, tmp2);
3916 }
3917 else
3918 range = NULL;
3919 mpz_clear (maxval);
3920
3921 gfc_init_se (&se, NULL);
3922 switch (c->expr->expr_type)
3923 {
3924 case EXPR_CONSTANT:
3925 gfc_conv_constant (&se, c->expr);
3926 if (range == NULL_TREE)
3927 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3928 else
3929 {
3930 if (index != NULL_TREE)
3931 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3932 CONSTRUCTOR_APPEND_ELT (v, range, se.expr);
3933 }
3934 break;
3935
3936 case EXPR_STRUCTURE:
3937 gfc_conv_structure (&se, c->expr, 1);
3938 CONSTRUCTOR_APPEND_ELT (v, index, se.expr);
3939 break;
3940
3941 default:
3942 gcc_unreachable ();
3943 }
3944 }
3945 break;
3946
3947 case EXPR_NULL:
3948 return gfc_build_null_descriptor (type);
3949
3950 default:
3951 gcc_unreachable ();
3952 }
3953
3954 /* Create a constructor from the list of elements. */
3955 tmp = build_constructor (type, v);
3956 TREE_CONSTANT (tmp) = 1;
3957 return tmp;
3958 }
3959
3960
3961 /* Generate code to evaluate non-constant array bounds. Sets *poffset and
3962 returns the size (in elements) of the array. */
3963
3964 static tree
3965 gfc_trans_array_bounds (tree type, gfc_symbol * sym, tree * poffset,
3966 stmtblock_t * pblock)
3967 {
3968 gfc_array_spec *as;
3969 tree size;
3970 tree stride;
3971 tree offset;
3972 tree ubound;
3973 tree lbound;
3974 tree tmp;
3975 gfc_se se;
3976
3977 int dim;
3978
3979 as = sym->as;
3980
3981 size = gfc_index_one_node;
3982 offset = gfc_index_zero_node;
3983 for (dim = 0; dim < as->rank; dim++)
3984 {
3985 /* Evaluate non-constant array bound expressions. */
3986 lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
3987 if (as->lower[dim] && !INTEGER_CST_P (lbound))
3988 {
3989 gfc_init_se (&se, NULL);
3990 gfc_conv_expr_type (&se, as->lower[dim], gfc_array_index_type);
3991 gfc_add_block_to_block (pblock, &se.pre);
3992 gfc_add_modify_expr (pblock, lbound, se.expr);
3993 }
3994 ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
3995 if (as->upper[dim] && !INTEGER_CST_P (ubound))
3996 {
3997 gfc_init_se (&se, NULL);
3998 gfc_conv_expr_type (&se, as->upper[dim], gfc_array_index_type);
3999 gfc_add_block_to_block (pblock, &se.pre);
4000 gfc_add_modify_expr (pblock, ubound, se.expr);
4001 }
4002 /* The offset of this dimension. offset = offset - lbound * stride. */
4003 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, size);
4004 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
4005
4006 /* The size of this dimension, and the stride of the next. */
4007 if (dim + 1 < as->rank)
4008 stride = GFC_TYPE_ARRAY_STRIDE (type, dim + 1);
4009 else
4010 stride = GFC_TYPE_ARRAY_SIZE (type);
4011
4012 if (ubound != NULL_TREE && !(stride && INTEGER_CST_P (stride)))
4013 {
4014 /* Calculate stride = size * (ubound + 1 - lbound). */
4015 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4016 gfc_index_one_node, lbound);
4017 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, ubound, tmp);
4018 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, size, tmp);
4019 if (stride)
4020 gfc_add_modify_expr (pblock, stride, tmp);
4021 else
4022 stride = gfc_evaluate_now (tmp, pblock);
4023
4024 /* Make sure that negative size arrays are translated
4025 to being zero size. */
4026 tmp = fold_build2 (GE_EXPR, boolean_type_node,
4027 stride, gfc_index_zero_node);
4028 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, tmp,
4029 stride, gfc_index_zero_node);
4030 gfc_add_modify_expr (pblock, stride, tmp);
4031 }
4032
4033 size = stride;
4034 }
4035
4036 gfc_trans_vla_type_sizes (sym, pblock);
4037
4038 *poffset = offset;
4039 return size;
4040 }
4041
4042
4043 /* Generate code to initialize/allocate an array variable. */
4044
4045 tree
4046 gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym, tree fnbody)
4047 {
4048 stmtblock_t block;
4049 tree type;
4050 tree tmp;
4051 tree size;
4052 tree offset;
4053 bool onstack;
4054
4055 gcc_assert (!(sym->attr.pointer || sym->attr.allocatable));
4056
4057 /* Do nothing for USEd variables. */
4058 if (sym->attr.use_assoc)
4059 return fnbody;
4060
4061 type = TREE_TYPE (decl);
4062 gcc_assert (GFC_ARRAY_TYPE_P (type));
4063 onstack = TREE_CODE (type) != POINTER_TYPE;
4064
4065 gfc_start_block (&block);
4066
4067 /* Evaluate character string length. */
4068 if (sym->ts.type == BT_CHARACTER
4069 && onstack && !INTEGER_CST_P (sym->ts.cl->backend_decl))
4070 {
4071 gfc_conv_string_length (sym->ts.cl, &block);
4072
4073 gfc_trans_vla_type_sizes (sym, &block);
4074
4075 /* Emit a DECL_EXPR for this variable, which will cause the
4076 gimplifier to allocate storage, and all that good stuff. */
4077 tmp = fold_build1 (DECL_EXPR, TREE_TYPE (decl), decl);
4078 gfc_add_expr_to_block (&block, tmp);
4079 }
4080
4081 if (onstack)
4082 {
4083 gfc_add_expr_to_block (&block, fnbody);
4084 return gfc_finish_block (&block);
4085 }
4086
4087 type = TREE_TYPE (type);
4088
4089 gcc_assert (!sym->attr.use_assoc);
4090 gcc_assert (!TREE_STATIC (decl));
4091 gcc_assert (!sym->module);
4092
4093 if (sym->ts.type == BT_CHARACTER
4094 && !INTEGER_CST_P (sym->ts.cl->backend_decl))
4095 gfc_conv_string_length (sym->ts.cl, &block);
4096
4097 size = gfc_trans_array_bounds (type, sym, &offset, &block);
4098
4099 /* Don't actually allocate space for Cray Pointees. */
4100 if (sym->attr.cray_pointee)
4101 {
4102 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
4103 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
4104 gfc_add_expr_to_block (&block, fnbody);
4105 return gfc_finish_block (&block);
4106 }
4107
4108 /* The size is the number of elements in the array, so multiply by the
4109 size of an element to get the total size. */
4110 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
4111 size = fold_build2 (MULT_EXPR, gfc_array_index_type, size,
4112 fold_convert (gfc_array_index_type, tmp));
4113
4114 /* Allocate memory to hold the data. */
4115 tmp = gfc_call_malloc (&block, TREE_TYPE (decl), size);
4116 gfc_add_modify_expr (&block, decl, tmp);
4117
4118 /* Set offset of the array. */
4119 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
4120 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
4121
4122
4123 /* Automatic arrays should not have initializers. */
4124 gcc_assert (!sym->value);
4125
4126 gfc_add_expr_to_block (&block, fnbody);
4127
4128 /* Free the temporary. */
4129 tmp = gfc_call_free (convert (pvoid_type_node, decl));
4130 gfc_add_expr_to_block (&block, tmp);
4131
4132 return gfc_finish_block (&block);
4133 }
4134
4135
4136 /* Generate entry and exit code for g77 calling convention arrays. */
4137
4138 tree
4139 gfc_trans_g77_array (gfc_symbol * sym, tree body)
4140 {
4141 tree parm;
4142 tree type;
4143 locus loc;
4144 tree offset;
4145 tree tmp;
4146 tree stmt;
4147 stmtblock_t block;
4148
4149 gfc_get_backend_locus (&loc);
4150 gfc_set_backend_locus (&sym->declared_at);
4151
4152 /* Descriptor type. */
4153 parm = sym->backend_decl;
4154 type = TREE_TYPE (parm);
4155 gcc_assert (GFC_ARRAY_TYPE_P (type));
4156
4157 gfc_start_block (&block);
4158
4159 if (sym->ts.type == BT_CHARACTER
4160 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
4161 gfc_conv_string_length (sym->ts.cl, &block);
4162
4163 /* Evaluate the bounds of the array. */
4164 gfc_trans_array_bounds (type, sym, &offset, &block);
4165
4166 /* Set the offset. */
4167 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
4168 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
4169
4170 /* Set the pointer itself if we aren't using the parameter directly. */
4171 if (TREE_CODE (parm) != PARM_DECL)
4172 {
4173 tmp = convert (TREE_TYPE (parm), GFC_DECL_SAVED_DESCRIPTOR (parm));
4174 gfc_add_modify_expr (&block, parm, tmp);
4175 }
4176 stmt = gfc_finish_block (&block);
4177
4178 gfc_set_backend_locus (&loc);
4179
4180 gfc_start_block (&block);
4181
4182 /* Add the initialization code to the start of the function. */
4183
4184 if (sym->attr.optional || sym->attr.not_always_present)
4185 {
4186 tmp = gfc_conv_expr_present (sym);
4187 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
4188 }
4189
4190 gfc_add_expr_to_block (&block, stmt);
4191 gfc_add_expr_to_block (&block, body);
4192
4193 return gfc_finish_block (&block);
4194 }
4195
4196
4197 /* Modify the descriptor of an array parameter so that it has the
4198 correct lower bound. Also move the upper bound accordingly.
4199 If the array is not packed, it will be copied into a temporary.
4200 For each dimension we set the new lower and upper bounds. Then we copy the
4201 stride and calculate the offset for this dimension. We also work out
4202 what the stride of a packed array would be, and see it the two match.
4203 If the array need repacking, we set the stride to the values we just
4204 calculated, recalculate the offset and copy the array data.
4205 Code is also added to copy the data back at the end of the function.
4206 */
4207
4208 tree
4209 gfc_trans_dummy_array_bias (gfc_symbol * sym, tree tmpdesc, tree body)
4210 {
4211 tree size;
4212 tree type;
4213 tree offset;
4214 locus loc;
4215 stmtblock_t block;
4216 stmtblock_t cleanup;
4217 tree lbound;
4218 tree ubound;
4219 tree dubound;
4220 tree dlbound;
4221 tree dumdesc;
4222 tree tmp;
4223 tree stmt;
4224 tree stride, stride2;
4225 tree stmt_packed;
4226 tree stmt_unpacked;
4227 tree partial;
4228 gfc_se se;
4229 int n;
4230 int checkparm;
4231 int no_repack;
4232 bool optional_arg;
4233
4234 /* Do nothing for pointer and allocatable arrays. */
4235 if (sym->attr.pointer || sym->attr.allocatable)
4236 return body;
4237
4238 if (sym->attr.dummy && gfc_is_nodesc_array (sym))
4239 return gfc_trans_g77_array (sym, body);
4240
4241 gfc_get_backend_locus (&loc);
4242 gfc_set_backend_locus (&sym->declared_at);
4243
4244 /* Descriptor type. */
4245 type = TREE_TYPE (tmpdesc);
4246 gcc_assert (GFC_ARRAY_TYPE_P (type));
4247 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
4248 dumdesc = build_fold_indirect_ref (dumdesc);
4249 gfc_start_block (&block);
4250
4251 if (sym->ts.type == BT_CHARACTER
4252 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
4253 gfc_conv_string_length (sym->ts.cl, &block);
4254
4255 checkparm = (sym->as->type == AS_EXPLICIT && flag_bounds_check);
4256
4257 no_repack = !(GFC_DECL_PACKED_ARRAY (tmpdesc)
4258 || GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc));
4259
4260 if (GFC_DECL_PARTIAL_PACKED_ARRAY (tmpdesc))
4261 {
4262 /* For non-constant shape arrays we only check if the first dimension
4263 is contiguous. Repacking higher dimensions wouldn't gain us
4264 anything as we still don't know the array stride. */
4265 partial = gfc_create_var (boolean_type_node, "partial");
4266 TREE_USED (partial) = 1;
4267 tmp = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
4268 tmp = fold_build2 (EQ_EXPR, boolean_type_node, tmp, gfc_index_one_node);
4269 gfc_add_modify_expr (&block, partial, tmp);
4270 }
4271 else
4272 {
4273 partial = NULL_TREE;
4274 }
4275
4276 /* The naming of stmt_unpacked and stmt_packed may be counter-intuitive
4277 here, however I think it does the right thing. */
4278 if (no_repack)
4279 {
4280 /* Set the first stride. */
4281 stride = gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[0]);
4282 stride = gfc_evaluate_now (stride, &block);
4283
4284 tmp = fold_build2 (EQ_EXPR, boolean_type_node,
4285 stride, gfc_index_zero_node);
4286 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, tmp,
4287 gfc_index_one_node, stride);
4288 stride = GFC_TYPE_ARRAY_STRIDE (type, 0);
4289 gfc_add_modify_expr (&block, stride, tmp);
4290
4291 /* Allow the user to disable array repacking. */
4292 stmt_unpacked = NULL_TREE;
4293 }
4294 else
4295 {
4296 gcc_assert (integer_onep (GFC_TYPE_ARRAY_STRIDE (type, 0)));
4297 /* A library call to repack the array if necessary. */
4298 tmp = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
4299 stmt_unpacked = build_call_expr (gfor_fndecl_in_pack, 1, tmp);
4300
4301 stride = gfc_index_one_node;
4302 }
4303
4304 /* This is for the case where the array data is used directly without
4305 calling the repack function. */
4306 if (no_repack || partial != NULL_TREE)
4307 stmt_packed = gfc_conv_descriptor_data_get (dumdesc);
4308 else
4309 stmt_packed = NULL_TREE;
4310
4311 /* Assign the data pointer. */
4312 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
4313 {
4314 /* Don't repack unknown shape arrays when the first stride is 1. */
4315 tmp = fold_build3 (COND_EXPR, TREE_TYPE (stmt_packed),
4316 partial, stmt_packed, stmt_unpacked);
4317 }
4318 else
4319 tmp = stmt_packed != NULL_TREE ? stmt_packed : stmt_unpacked;
4320 gfc_add_modify_expr (&block, tmpdesc, fold_convert (type, tmp));
4321
4322 offset = gfc_index_zero_node;
4323 size = gfc_index_one_node;
4324
4325 /* Evaluate the bounds of the array. */
4326 for (n = 0; n < sym->as->rank; n++)
4327 {
4328 if (checkparm || !sym->as->upper[n])
4329 {
4330 /* Get the bounds of the actual parameter. */
4331 dubound = gfc_conv_descriptor_ubound (dumdesc, gfc_rank_cst[n]);
4332 dlbound = gfc_conv_descriptor_lbound (dumdesc, gfc_rank_cst[n]);
4333 }
4334 else
4335 {
4336 dubound = NULL_TREE;
4337 dlbound = NULL_TREE;
4338 }
4339
4340 lbound = GFC_TYPE_ARRAY_LBOUND (type, n);
4341 if (!INTEGER_CST_P (lbound))
4342 {
4343 gfc_init_se (&se, NULL);
4344 gfc_conv_expr_type (&se, sym->as->lower[n],
4345 gfc_array_index_type);
4346 gfc_add_block_to_block (&block, &se.pre);
4347 gfc_add_modify_expr (&block, lbound, se.expr);
4348 }
4349
4350 ubound = GFC_TYPE_ARRAY_UBOUND (type, n);
4351 /* Set the desired upper bound. */
4352 if (sym->as->upper[n])
4353 {
4354 /* We know what we want the upper bound to be. */
4355 if (!INTEGER_CST_P (ubound))
4356 {
4357 gfc_init_se (&se, NULL);
4358 gfc_conv_expr_type (&se, sym->as->upper[n],
4359 gfc_array_index_type);
4360 gfc_add_block_to_block (&block, &se.pre);
4361 gfc_add_modify_expr (&block, ubound, se.expr);
4362 }
4363
4364 /* Check the sizes match. */
4365 if (checkparm)
4366 {
4367 /* Check (ubound(a) - lbound(a) == ubound(b) - lbound(b)). */
4368 char * msg;
4369
4370 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4371 ubound, lbound);
4372 stride2 = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4373 dubound, dlbound);
4374 tmp = fold_build2 (NE_EXPR, gfc_array_index_type, tmp, stride2);
4375 asprintf (&msg, "%s for dimension %d of array '%s'",
4376 gfc_msg_bounds, n+1, sym->name);
4377 gfc_trans_runtime_check (tmp, &block, &loc, msg);
4378 gfc_free (msg);
4379 }
4380 }
4381 else
4382 {
4383 /* For assumed shape arrays move the upper bound by the same amount
4384 as the lower bound. */
4385 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4386 dubound, dlbound);
4387 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, lbound);
4388 gfc_add_modify_expr (&block, ubound, tmp);
4389 }
4390 /* The offset of this dimension. offset = offset - lbound * stride. */
4391 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, lbound, stride);
4392 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
4393
4394 /* The size of this dimension, and the stride of the next. */
4395 if (n + 1 < sym->as->rank)
4396 {
4397 stride = GFC_TYPE_ARRAY_STRIDE (type, n + 1);
4398
4399 if (no_repack || partial != NULL_TREE)
4400 {
4401 stmt_unpacked =
4402 gfc_conv_descriptor_stride (dumdesc, gfc_rank_cst[n+1]);
4403 }
4404
4405 /* Figure out the stride if not a known constant. */
4406 if (!INTEGER_CST_P (stride))
4407 {
4408 if (no_repack)
4409 stmt_packed = NULL_TREE;
4410 else
4411 {
4412 /* Calculate stride = size * (ubound + 1 - lbound). */
4413 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4414 gfc_index_one_node, lbound);
4415 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4416 ubound, tmp);
4417 size = fold_build2 (MULT_EXPR, gfc_array_index_type,
4418 size, tmp);
4419 stmt_packed = size;
4420 }
4421
4422 /* Assign the stride. */
4423 if (stmt_packed != NULL_TREE && stmt_unpacked != NULL_TREE)
4424 tmp = fold_build3 (COND_EXPR, gfc_array_index_type, partial,
4425 stmt_unpacked, stmt_packed);
4426 else
4427 tmp = (stmt_packed != NULL_TREE) ? stmt_packed : stmt_unpacked;
4428 gfc_add_modify_expr (&block, stride, tmp);
4429 }
4430 }
4431 else
4432 {
4433 stride = GFC_TYPE_ARRAY_SIZE (type);
4434
4435 if (stride && !INTEGER_CST_P (stride))
4436 {
4437 /* Calculate size = stride * (ubound + 1 - lbound). */
4438 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
4439 gfc_index_one_node, lbound);
4440 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4441 ubound, tmp);
4442 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
4443 GFC_TYPE_ARRAY_STRIDE (type, n), tmp);
4444 gfc_add_modify_expr (&block, stride, tmp);
4445 }
4446 }
4447 }
4448
4449 /* Set the offset. */
4450 if (TREE_CODE (GFC_TYPE_ARRAY_OFFSET (type)) == VAR_DECL)
4451 gfc_add_modify_expr (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
4452
4453 gfc_trans_vla_type_sizes (sym, &block);
4454
4455 stmt = gfc_finish_block (&block);
4456
4457 gfc_start_block (&block);
4458
4459 /* Only do the entry/initialization code if the arg is present. */
4460 dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
4461 optional_arg = (sym->attr.optional
4462 || (sym->ns->proc_name->attr.entry_master
4463 && sym->attr.dummy));
4464 if (optional_arg)
4465 {
4466 tmp = gfc_conv_expr_present (sym);
4467 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
4468 }
4469 gfc_add_expr_to_block (&block, stmt);
4470
4471 /* Add the main function body. */
4472 gfc_add_expr_to_block (&block, body);
4473
4474 /* Cleanup code. */
4475 if (!no_repack)
4476 {
4477 gfc_start_block (&cleanup);
4478
4479 if (sym->attr.intent != INTENT_IN)
4480 {
4481 /* Copy the data back. */
4482 tmp = build_call_expr (gfor_fndecl_in_unpack, 2, dumdesc, tmpdesc);
4483 gfc_add_expr_to_block (&cleanup, tmp);
4484 }
4485
4486 /* Free the temporary. */
4487 tmp = gfc_call_free (tmpdesc);
4488 gfc_add_expr_to_block (&cleanup, tmp);
4489
4490 stmt = gfc_finish_block (&cleanup);
4491
4492 /* Only do the cleanup if the array was repacked. */
4493 tmp = build_fold_indirect_ref (dumdesc);
4494 tmp = gfc_conv_descriptor_data_get (tmp);
4495 tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, tmpdesc);
4496 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
4497
4498 if (optional_arg)
4499 {
4500 tmp = gfc_conv_expr_present (sym);
4501 stmt = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
4502 }
4503 gfc_add_expr_to_block (&block, stmt);
4504 }
4505 /* We don't need to free any memory allocated by internal_pack as it will
4506 be freed at the end of the function by pop_context. */
4507 return gfc_finish_block (&block);
4508 }
4509
4510
4511 /* Calculate the overall offset, including subreferences. */
4512 static void
4513 gfc_get_dataptr_offset (stmtblock_t *block, tree parm, tree desc, tree offset,
4514 bool subref, gfc_expr *expr)
4515 {
4516 tree tmp;
4517 tree field;
4518 tree stride;
4519 tree index;
4520 gfc_ref *ref;
4521 gfc_se start;
4522 int n;
4523
4524 /* If offset is NULL and this is not a subreferenced array, there is
4525 nothing to do. */
4526 if (offset == NULL_TREE)
4527 {
4528 if (subref)
4529 offset = gfc_index_zero_node;
4530 else
4531 return;
4532 }
4533
4534 tmp = gfc_conv_array_data (desc);
4535 tmp = build_fold_indirect_ref (tmp);
4536 tmp = gfc_build_array_ref (tmp, offset, NULL);
4537
4538 /* Offset the data pointer for pointer assignments from arrays with
4539 subreferences; eg. my_integer => my_type(:)%integer_component. */
4540 if (subref)
4541 {
4542 /* Go past the array reference. */
4543 for (ref = expr->ref; ref; ref = ref->next)
4544 if (ref->type == REF_ARRAY &&
4545 ref->u.ar.type != AR_ELEMENT)
4546 {
4547 ref = ref->next;
4548 break;
4549 }
4550
4551 /* Calculate the offset for each subsequent subreference. */
4552 for (; ref; ref = ref->next)
4553 {
4554 switch (ref->type)
4555 {
4556 case REF_COMPONENT:
4557 field = ref->u.c.component->backend_decl;
4558 gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
4559 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field),
4560 tmp, field, NULL_TREE);
4561 break;
4562
4563 case REF_SUBSTRING:
4564 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE);
4565 gfc_init_se (&start, NULL);
4566 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
4567 gfc_add_block_to_block (block, &start.pre);
4568 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
4569 break;
4570
4571 case REF_ARRAY:
4572 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE
4573 && ref->u.ar.type == AR_ELEMENT);
4574
4575 /* TODO - Add bounds checking. */
4576 stride = gfc_index_one_node;
4577 index = gfc_index_zero_node;
4578 for (n = 0; n < ref->u.ar.dimen; n++)
4579 {
4580 tree itmp;
4581 tree jtmp;
4582
4583 /* Update the index. */
4584 gfc_init_se (&start, NULL);
4585 gfc_conv_expr_type (&start, ref->u.ar.start[n], gfc_array_index_type);
4586 itmp = gfc_evaluate_now (start.expr, block);
4587 gfc_init_se (&start, NULL);
4588 gfc_conv_expr_type (&start, ref->u.ar.as->lower[n], gfc_array_index_type);
4589 jtmp = gfc_evaluate_now (start.expr, block);
4590 itmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, itmp, jtmp);
4591 itmp = fold_build2 (MULT_EXPR, gfc_array_index_type, itmp, stride);
4592 index = fold_build2 (PLUS_EXPR, gfc_array_index_type, itmp, index);
4593 index = gfc_evaluate_now (index, block);
4594
4595 /* Update the stride. */
4596 gfc_init_se (&start, NULL);
4597 gfc_conv_expr_type (&start, ref->u.ar.as->upper[n], gfc_array_index_type);
4598 itmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, start.expr, jtmp);
4599 itmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
4600 gfc_index_one_node, itmp);
4601 stride = fold_build2 (MULT_EXPR, gfc_array_index_type, stride, itmp);
4602 stride = gfc_evaluate_now (stride, block);
4603 }
4604
4605 /* Apply the index to obtain the array element. */
4606 tmp = gfc_build_array_ref (tmp, index, NULL);
4607 break;
4608
4609 default:
4610 gcc_unreachable ();
4611 break;
4612 }
4613 }
4614 }
4615
4616 /* Set the target data pointer. */
4617 offset = gfc_build_addr_expr (gfc_array_dataptr_type (desc), tmp);
4618 gfc_conv_descriptor_data_set (block, parm, offset);
4619 }
4620
4621
4622 /* gfc_conv_expr_descriptor needs the character length of elemental
4623 functions before the function is called so that the size of the
4624 temporary can be obtained. The only way to do this is to convert
4625 the expression, mapping onto the actual arguments. */
4626 static void
4627 get_elemental_fcn_charlen (gfc_expr *expr, gfc_se *se)
4628 {
4629 gfc_interface_mapping mapping;
4630 gfc_formal_arglist *formal;
4631 gfc_actual_arglist *arg;
4632 gfc_se tse;
4633
4634 formal = expr->symtree->n.sym->formal;
4635 arg = expr->value.function.actual;
4636 gfc_init_interface_mapping (&mapping);
4637
4638 /* Set se = NULL in the calls to the interface mapping, to supress any
4639 backend stuff. */
4640 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
4641 {
4642 if (!arg->expr)
4643 continue;
4644 if (formal->sym)
4645 gfc_add_interface_mapping (&mapping, formal->sym, NULL, arg->expr);
4646 }
4647
4648 gfc_init_se (&tse, NULL);
4649
4650 /* Build the expression for the character length and convert it. */
4651 gfc_apply_interface_mapping (&mapping, &tse, expr->ts.cl->length);
4652
4653 gfc_add_block_to_block (&se->pre, &tse.pre);
4654 gfc_add_block_to_block (&se->post, &tse.post);
4655 tse.expr = fold_convert (gfc_charlen_type_node, tse.expr);
4656 tse.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tse.expr,
4657 build_int_cst (gfc_charlen_type_node, 0));
4658 expr->ts.cl->backend_decl = tse.expr;
4659 gfc_free_interface_mapping (&mapping);
4660 }
4661
4662
4663 /* Convert an array for passing as an actual argument. Expressions and
4664 vector subscripts are evaluated and stored in a temporary, which is then
4665 passed. For whole arrays the descriptor is passed. For array sections
4666 a modified copy of the descriptor is passed, but using the original data.
4667
4668 This function is also used for array pointer assignments, and there
4669 are three cases:
4670
4671 - se->want_pointer && !se->direct_byref
4672 EXPR is an actual argument. On exit, se->expr contains a
4673 pointer to the array descriptor.
4674
4675 - !se->want_pointer && !se->direct_byref
4676 EXPR is an actual argument to an intrinsic function or the
4677 left-hand side of a pointer assignment. On exit, se->expr
4678 contains the descriptor for EXPR.
4679
4680 - !se->want_pointer && se->direct_byref
4681 EXPR is the right-hand side of a pointer assignment and
4682 se->expr is the descriptor for the previously-evaluated
4683 left-hand side. The function creates an assignment from
4684 EXPR to se->expr. */
4685
4686 void
4687 gfc_conv_expr_descriptor (gfc_se * se, gfc_expr * expr, gfc_ss * ss)
4688 {
4689 gfc_loopinfo loop;
4690 gfc_ss *secss;
4691 gfc_ss_info *info;
4692 int need_tmp;
4693 int n;
4694 tree tmp;
4695 tree desc;
4696 stmtblock_t block;
4697 tree start;
4698 tree offset;
4699 int full;
4700 bool subref_array_target = false;
4701
4702 gcc_assert (ss != gfc_ss_terminator);
4703
4704 /* Special case things we know we can pass easily. */
4705 switch (expr->expr_type)
4706 {
4707 case EXPR_VARIABLE:
4708 /* If we have a linear array section, we can pass it directly.
4709 Otherwise we need to copy it into a temporary. */
4710
4711 /* Find the SS for the array section. */
4712 secss = ss;
4713 while (secss != gfc_ss_terminator && secss->type != GFC_SS_SECTION)
4714 secss = secss->next;
4715
4716 gcc_assert (secss != gfc_ss_terminator);
4717 info = &secss->data.info;
4718
4719 /* Get the descriptor for the array. */
4720 gfc_conv_ss_descriptor (&se->pre, secss, 0);
4721 desc = info->descriptor;
4722
4723 subref_array_target = se->direct_byref && is_subref_array (expr);
4724 need_tmp = gfc_ref_needs_temporary_p (expr->ref)
4725 && !subref_array_target;
4726
4727 if (need_tmp)
4728 full = 0;
4729 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
4730 {
4731 /* Create a new descriptor if the array doesn't have one. */
4732 full = 0;
4733 }
4734 else if (info->ref->u.ar.type == AR_FULL)
4735 full = 1;
4736 else if (se->direct_byref)
4737 full = 0;
4738 else
4739 full = gfc_full_array_ref_p (info->ref);
4740
4741 if (full)
4742 {
4743 if (se->direct_byref)
4744 {
4745 /* Copy the descriptor for pointer assignments. */
4746 gfc_add_modify_expr (&se->pre, se->expr, desc);
4747
4748 /* Add any offsets from subreferences. */
4749 gfc_get_dataptr_offset (&se->pre, se->expr, desc, NULL_TREE,
4750 subref_array_target, expr);
4751 }
4752 else if (se->want_pointer)
4753 {
4754 /* We pass full arrays directly. This means that pointers and
4755 allocatable arrays should also work. */
4756 se->expr = build_fold_addr_expr (desc);
4757 }
4758 else
4759 {
4760 se->expr = desc;
4761 }
4762
4763 if (expr->ts.type == BT_CHARACTER)
4764 se->string_length = gfc_get_expr_charlen (expr);
4765
4766 return;
4767 }
4768 break;
4769
4770 case EXPR_FUNCTION:
4771 /* A transformational function return value will be a temporary
4772 array descriptor. We still need to go through the scalarizer
4773 to create the descriptor. Elemental functions ar handled as
4774 arbitrary expressions, i.e. copy to a temporary. */
4775 secss = ss;
4776 /* Look for the SS for this function. */
4777 while (secss != gfc_ss_terminator
4778 && (secss->type != GFC_SS_FUNCTION || secss->expr != expr))
4779 secss = secss->next;
4780
4781 if (se->direct_byref)
4782 {
4783 gcc_assert (secss != gfc_ss_terminator);
4784
4785 /* For pointer assignments pass the descriptor directly. */
4786 se->ss = secss;
4787 se->expr = build_fold_addr_expr (se->expr);
4788 gfc_conv_expr (se, expr);
4789 return;
4790 }
4791
4792 if (secss == gfc_ss_terminator)
4793 {
4794 /* Elemental function. */
4795 need_tmp = 1;
4796 if (expr->ts.type == BT_CHARACTER
4797 && expr->ts.cl->length->expr_type != EXPR_CONSTANT)
4798 get_elemental_fcn_charlen (expr, se);
4799
4800 info = NULL;
4801 }
4802 else
4803 {
4804 /* Transformational function. */
4805 info = &secss->data.info;
4806 need_tmp = 0;
4807 }
4808 break;
4809
4810 case EXPR_ARRAY:
4811 /* Constant array constructors don't need a temporary. */
4812 if (ss->type == GFC_SS_CONSTRUCTOR
4813 && expr->ts.type != BT_CHARACTER
4814 && gfc_constant_array_constructor_p (expr->value.constructor))
4815 {
4816 need_tmp = 0;
4817 info = &ss->data.info;
4818 secss = ss;
4819 }
4820 else
4821 {
4822 need_tmp = 1;
4823 secss = NULL;
4824 info = NULL;
4825 }
4826 break;
4827
4828 default:
4829 /* Something complicated. Copy it into a temporary. */
4830 need_tmp = 1;
4831 secss = NULL;
4832 info = NULL;
4833 break;
4834 }
4835
4836
4837 gfc_init_loopinfo (&loop);
4838
4839 /* Associate the SS with the loop. */
4840 gfc_add_ss_to_loop (&loop, ss);
4841
4842 /* Tell the scalarizer not to bother creating loop variables, etc. */
4843 if (!need_tmp)
4844 loop.array_parameter = 1;
4845 else
4846 /* The right-hand side of a pointer assignment mustn't use a temporary. */
4847 gcc_assert (!se->direct_byref);
4848
4849 /* Setup the scalarizing loops and bounds. */
4850 gfc_conv_ss_startstride (&loop);
4851
4852 if (need_tmp)
4853 {
4854 /* Tell the scalarizer to make a temporary. */
4855 loop.temp_ss = gfc_get_ss ();
4856 loop.temp_ss->type = GFC_SS_TEMP;
4857 loop.temp_ss->next = gfc_ss_terminator;
4858
4859 if (expr->ts.type == BT_CHARACTER && !expr->ts.cl->backend_decl)
4860 gfc_conv_string_length (expr->ts.cl, &se->pre);
4861
4862 loop.temp_ss->data.temp.type = gfc_typenode_for_spec (&expr->ts);
4863
4864 if (expr->ts.type == BT_CHARACTER)
4865 loop.temp_ss->string_length = expr->ts.cl->backend_decl;
4866 else
4867 loop.temp_ss->string_length = NULL;
4868
4869 se->string_length = loop.temp_ss->string_length;
4870 loop.temp_ss->data.temp.dimen = loop.dimen;
4871 gfc_add_ss_to_loop (&loop, loop.temp_ss);
4872 }
4873
4874 gfc_conv_loop_setup (&loop);
4875
4876 if (need_tmp)
4877 {
4878 /* Copy into a temporary and pass that. We don't need to copy the data
4879 back because expressions and vector subscripts must be INTENT_IN. */
4880 /* TODO: Optimize passing function return values. */
4881 gfc_se lse;
4882 gfc_se rse;
4883
4884 /* Start the copying loops. */
4885 gfc_mark_ss_chain_used (loop.temp_ss, 1);
4886 gfc_mark_ss_chain_used (ss, 1);
4887 gfc_start_scalarized_body (&loop, &block);
4888
4889 /* Copy each data element. */
4890 gfc_init_se (&lse, NULL);
4891 gfc_copy_loopinfo_to_se (&lse, &loop);
4892 gfc_init_se (&rse, NULL);
4893 gfc_copy_loopinfo_to_se (&rse, &loop);
4894
4895 lse.ss = loop.temp_ss;
4896 rse.ss = ss;
4897
4898 gfc_conv_scalarized_array_ref (&lse, NULL);
4899 if (expr->ts.type == BT_CHARACTER)
4900 {
4901 gfc_conv_expr (&rse, expr);
4902 if (POINTER_TYPE_P (TREE_TYPE (rse.expr)))
4903 rse.expr = build_fold_indirect_ref (rse.expr);
4904 }
4905 else
4906 gfc_conv_expr_val (&rse, expr);
4907
4908 gfc_add_block_to_block (&block, &rse.pre);
4909 gfc_add_block_to_block (&block, &lse.pre);
4910
4911 lse.string_length = rse.string_length;
4912 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true,
4913 expr->expr_type == EXPR_VARIABLE);
4914 gfc_add_expr_to_block (&block, tmp);
4915
4916 /* Finish the copying loops. */
4917 gfc_trans_scalarizing_loops (&loop, &block);
4918
4919 desc = loop.temp_ss->data.info.descriptor;
4920
4921 gcc_assert (is_gimple_lvalue (desc));
4922 }
4923 else if (expr->expr_type == EXPR_FUNCTION)
4924 {
4925 desc = info->descriptor;
4926 se->string_length = ss->string_length;
4927 }
4928 else
4929 {
4930 /* We pass sections without copying to a temporary. Make a new
4931 descriptor and point it at the section we want. The loop variable
4932 limits will be the limits of the section.
4933 A function may decide to repack the array to speed up access, but
4934 we're not bothered about that here. */
4935 int dim, ndim;
4936 tree parm;
4937 tree parmtype;
4938 tree stride;
4939 tree from;
4940 tree to;
4941 tree base;
4942
4943 /* Set the string_length for a character array. */
4944 if (expr->ts.type == BT_CHARACTER)
4945 se->string_length = gfc_get_expr_charlen (expr);
4946
4947 desc = info->descriptor;
4948 gcc_assert (secss && secss != gfc_ss_terminator);
4949 if (se->direct_byref)
4950 {
4951 /* For pointer assignments we fill in the destination. */
4952 parm = se->expr;
4953 parmtype = TREE_TYPE (parm);
4954 }
4955 else
4956 {
4957 /* Otherwise make a new one. */
4958 parmtype = gfc_get_element_type (TREE_TYPE (desc));
4959 parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen,
4960 loop.from, loop.to, 0,
4961 GFC_ARRAY_UNKNOWN);
4962 parm = gfc_create_var (parmtype, "parm");
4963 }
4964
4965 offset = gfc_index_zero_node;
4966 dim = 0;
4967
4968 /* The following can be somewhat confusing. We have two
4969 descriptors, a new one and the original array.
4970 {parm, parmtype, dim} refer to the new one.
4971 {desc, type, n, secss, loop} refer to the original, which maybe
4972 a descriptorless array.
4973 The bounds of the scalarization are the bounds of the section.
4974 We don't have to worry about numeric overflows when calculating
4975 the offsets because all elements are within the array data. */
4976
4977 /* Set the dtype. */
4978 tmp = gfc_conv_descriptor_dtype (parm);
4979 gfc_add_modify_expr (&loop.pre, tmp, gfc_get_dtype (parmtype));
4980
4981 /* Set offset for assignments to pointer only to zero if it is not
4982 the full array. */
4983 if (se->direct_byref
4984 && info->ref && info->ref->u.ar.type != AR_FULL)
4985 base = gfc_index_zero_node;
4986 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
4987 base = gfc_evaluate_now (gfc_conv_array_offset (desc), &loop.pre);
4988 else
4989 base = NULL_TREE;
4990
4991 ndim = info->ref ? info->ref->u.ar.dimen : info->dimen;
4992 for (n = 0; n < ndim; n++)
4993 {
4994 stride = gfc_conv_array_stride (desc, n);
4995
4996 /* Work out the offset. */
4997 if (info->ref
4998 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
4999 {
5000 gcc_assert (info->subscript[n]
5001 && info->subscript[n]->type == GFC_SS_SCALAR);
5002 start = info->subscript[n]->data.scalar.expr;
5003 }
5004 else
5005 {
5006 /* Check we haven't somehow got out of sync. */
5007 gcc_assert (info->dim[dim] == n);
5008
5009 /* Evaluate and remember the start of the section. */
5010 start = info->start[dim];
5011 stride = gfc_evaluate_now (stride, &loop.pre);
5012 }
5013
5014 tmp = gfc_conv_array_lbound (desc, n);
5015 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), start, tmp);
5016
5017 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (tmp), tmp, stride);
5018 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (tmp), offset, tmp);
5019
5020 if (info->ref
5021 && info->ref->u.ar.dimen_type[n] == DIMEN_ELEMENT)
5022 {
5023 /* For elemental dimensions, we only need the offset. */
5024 continue;
5025 }
5026
5027 /* Vector subscripts need copying and are handled elsewhere. */
5028 if (info->ref)
5029 gcc_assert (info->ref->u.ar.dimen_type[n] == DIMEN_RANGE);
5030
5031 /* Set the new lower bound. */
5032 from = loop.from[dim];
5033 to = loop.to[dim];
5034
5035 /* If we have an array section or are assigning make sure that
5036 the lower bound is 1. References to the full
5037 array should otherwise keep the original bounds. */
5038 if ((!info->ref
5039 || info->ref->u.ar.type != AR_FULL)
5040 && !integer_onep (from))
5041 {
5042 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
5043 gfc_index_one_node, from);
5044 to = fold_build2 (PLUS_EXPR, gfc_array_index_type, to, tmp);
5045 from = gfc_index_one_node;
5046 }
5047 tmp = gfc_conv_descriptor_lbound (parm, gfc_rank_cst[dim]);
5048 gfc_add_modify_expr (&loop.pre, tmp, from);
5049
5050 /* Set the new upper bound. */
5051 tmp = gfc_conv_descriptor_ubound (parm, gfc_rank_cst[dim]);
5052 gfc_add_modify_expr (&loop.pre, tmp, to);
5053
5054 /* Multiply the stride by the section stride to get the
5055 total stride. */
5056 stride = fold_build2 (MULT_EXPR, gfc_array_index_type,
5057 stride, info->stride[dim]);
5058
5059 if (se->direct_byref && info->ref && info->ref->u.ar.type != AR_FULL)
5060 {
5061 base = fold_build2 (MINUS_EXPR, TREE_TYPE (base),
5062 base, stride);
5063 }
5064 else if (GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
5065 {
5066 tmp = gfc_conv_array_lbound (desc, n);
5067 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (base),
5068 tmp, loop.from[dim]);
5069 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (base),
5070 tmp, gfc_conv_array_stride (desc, n));
5071 base = fold_build2 (PLUS_EXPR, TREE_TYPE (base),
5072 tmp, base);
5073 }
5074
5075 /* Store the new stride. */
5076 tmp = gfc_conv_descriptor_stride (parm, gfc_rank_cst[dim]);
5077 gfc_add_modify_expr (&loop.pre, tmp, stride);
5078
5079 dim++;
5080 }
5081
5082 if (se->data_not_needed)
5083 gfc_conv_descriptor_data_set (&loop.pre, parm, gfc_index_zero_node);
5084 else
5085 /* Point the data pointer at the first element in the section. */
5086 gfc_get_dataptr_offset (&loop.pre, parm, desc, offset,
5087 subref_array_target, expr);
5088
5089 if ((se->direct_byref || GFC_ARRAY_TYPE_P (TREE_TYPE (desc)))
5090 && !se->data_not_needed)
5091 {
5092 /* Set the offset. */
5093 tmp = gfc_conv_descriptor_offset (parm);
5094 gfc_add_modify_expr (&loop.pre, tmp, base);
5095 }
5096 else
5097 {
5098 /* Only the callee knows what the correct offset it, so just set
5099 it to zero here. */
5100 tmp = gfc_conv_descriptor_offset (parm);
5101 gfc_add_modify_expr (&loop.pre, tmp, gfc_index_zero_node);
5102 }
5103 desc = parm;
5104 }
5105
5106 if (!se->direct_byref)
5107 {
5108 /* Get a pointer to the new descriptor. */
5109 if (se->want_pointer)
5110 se->expr = build_fold_addr_expr (desc);
5111 else
5112 se->expr = desc;
5113 }
5114
5115 gfc_add_block_to_block (&se->pre, &loop.pre);
5116 gfc_add_block_to_block (&se->post, &loop.post);
5117
5118 /* Cleanup the scalarizer. */
5119 gfc_cleanup_loop (&loop);
5120 }
5121
5122
5123 /* Convert an array for passing as an actual parameter. */
5124 /* TODO: Optimize passing g77 arrays. */
5125
5126 void
5127 gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr, gfc_ss * ss, int g77)
5128 {
5129 tree ptr;
5130 tree desc;
5131 tree tmp = NULL_TREE;
5132 tree stmt;
5133 tree parent = DECL_CONTEXT (current_function_decl);
5134 bool full_array_var, this_array_result;
5135 gfc_symbol *sym;
5136 stmtblock_t block;
5137
5138 full_array_var = (expr->expr_type == EXPR_VARIABLE
5139 && expr->ref->u.ar.type == AR_FULL);
5140 sym = full_array_var ? expr->symtree->n.sym : NULL;
5141
5142 if (expr->expr_type == EXPR_ARRAY && expr->ts.type == BT_CHARACTER)
5143 {
5144 get_array_ctor_strlen (&se->pre, expr->value.constructor, &tmp);
5145 expr->ts.cl->backend_decl = tmp;
5146 se->string_length = tmp;
5147 }
5148
5149 /* Is this the result of the enclosing procedure? */
5150 this_array_result = (full_array_var && sym->attr.flavor == FL_PROCEDURE);
5151 if (this_array_result
5152 && (sym->backend_decl != current_function_decl)
5153 && (sym->backend_decl != parent))
5154 this_array_result = false;
5155
5156 /* Passing address of the array if it is not pointer or assumed-shape. */
5157 if (full_array_var && g77 && !this_array_result)
5158 {
5159 tmp = gfc_get_symbol_decl (sym);
5160
5161 if (sym->ts.type == BT_CHARACTER)
5162 se->string_length = sym->ts.cl->backend_decl;
5163 if (!sym->attr.pointer && sym->as->type != AS_ASSUMED_SHAPE
5164 && !sym->attr.allocatable)
5165 {
5166 /* Some variables are declared directly, others are declared as
5167 pointers and allocated on the heap. */
5168 if (sym->attr.dummy || POINTER_TYPE_P (TREE_TYPE (tmp)))
5169 se->expr = tmp;
5170 else
5171 se->expr = build_fold_addr_expr (tmp);
5172 return;
5173 }
5174 if (sym->attr.allocatable)
5175 {
5176 if (sym->attr.dummy || sym->attr.result)
5177 {
5178 gfc_conv_expr_descriptor (se, expr, ss);
5179 se->expr = gfc_conv_array_data (se->expr);
5180 }
5181 else
5182 se->expr = gfc_conv_array_data (tmp);
5183 return;
5184 }
5185 }
5186
5187 if (this_array_result)
5188 {
5189 /* Result of the enclosing function. */
5190 gfc_conv_expr_descriptor (se, expr, ss);
5191 se->expr = build_fold_addr_expr (se->expr);
5192
5193 if (g77 && TREE_TYPE (TREE_TYPE (se->expr)) != NULL_TREE
5194 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
5195 se->expr = gfc_conv_array_data (build_fold_indirect_ref (se->expr));
5196
5197 return;
5198 }
5199 else
5200 {
5201 /* Every other type of array. */
5202 se->want_pointer = 1;
5203 gfc_conv_expr_descriptor (se, expr, ss);
5204 }
5205
5206
5207 /* Deallocate the allocatable components of structures that are
5208 not variable. */
5209 if (expr->ts.type == BT_DERIVED
5210 && expr->ts.derived->attr.alloc_comp
5211 && expr->expr_type != EXPR_VARIABLE)
5212 {
5213 tmp = build_fold_indirect_ref (se->expr);
5214 tmp = gfc_deallocate_alloc_comp (expr->ts.derived, tmp, expr->rank);
5215 gfc_add_expr_to_block (&se->post, tmp);
5216 }
5217
5218 if (g77)
5219 {
5220 desc = se->expr;
5221 /* Repack the array. */
5222 ptr = build_call_expr (gfor_fndecl_in_pack, 1, desc);
5223 ptr = gfc_evaluate_now (ptr, &se->pre);
5224 se->expr = ptr;
5225
5226 gfc_start_block (&block);
5227
5228 /* Copy the data back. */
5229 tmp = build_call_expr (gfor_fndecl_in_unpack, 2, desc, ptr);
5230 gfc_add_expr_to_block (&block, tmp);
5231
5232 /* Free the temporary. */
5233 tmp = gfc_call_free (convert (pvoid_type_node, ptr));
5234 gfc_add_expr_to_block (&block, tmp);
5235
5236 stmt = gfc_finish_block (&block);
5237
5238 gfc_init_block (&block);
5239 /* Only if it was repacked. This code needs to be executed before the
5240 loop cleanup code. */
5241 tmp = build_fold_indirect_ref (desc);
5242 tmp = gfc_conv_array_data (tmp);
5243 tmp = fold_build2 (NE_EXPR, boolean_type_node,
5244 fold_convert (TREE_TYPE (tmp), ptr), tmp);
5245 tmp = build3_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
5246
5247 gfc_add_expr_to_block (&block, tmp);
5248 gfc_add_block_to_block (&block, &se->post);
5249
5250 gfc_init_block (&se->post);
5251 gfc_add_block_to_block (&se->post, &block);
5252 }
5253 }
5254
5255
5256 /* Generate code to deallocate an array, if it is allocated. */
5257
5258 tree
5259 gfc_trans_dealloc_allocated (tree descriptor)
5260 {
5261 tree tmp;
5262 tree var;
5263 stmtblock_t block;
5264
5265 gfc_start_block (&block);
5266
5267 var = gfc_conv_descriptor_data_get (descriptor);
5268 STRIP_NOPS (var);
5269
5270 /* Call array_deallocate with an int * present in the second argument.
5271 Although it is ignored here, it's presence ensures that arrays that
5272 are already deallocated are ignored. */
5273 tmp = gfc_deallocate_with_status (var, NULL_TREE, true);
5274 gfc_add_expr_to_block (&block, tmp);
5275
5276 /* Zero the data pointer. */
5277 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
5278 var, build_int_cst (TREE_TYPE (var), 0));
5279 gfc_add_expr_to_block (&block, tmp);
5280
5281 return gfc_finish_block (&block);
5282 }
5283
5284
5285 /* This helper function calculates the size in words of a full array. */
5286
5287 static tree
5288 get_full_array_size (stmtblock_t *block, tree decl, int rank)
5289 {
5290 tree idx;
5291 tree nelems;
5292 tree tmp;
5293 idx = gfc_rank_cst[rank - 1];
5294 nelems = gfc_conv_descriptor_ubound (decl, idx);
5295 tmp = gfc_conv_descriptor_lbound (decl, idx);
5296 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, nelems, tmp);
5297 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
5298 tmp, gfc_index_one_node);
5299 tmp = gfc_evaluate_now (tmp, block);
5300
5301 nelems = gfc_conv_descriptor_stride (decl, idx);
5302 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type, nelems, tmp);
5303 return gfc_evaluate_now (tmp, block);
5304 }
5305
5306
5307 /* Allocate dest to the same size as src, and copy src -> dest. */
5308
5309 tree
5310 gfc_duplicate_allocatable(tree dest, tree src, tree type, int rank)
5311 {
5312 tree tmp;
5313 tree size;
5314 tree nelems;
5315 tree null_cond;
5316 tree null_data;
5317 stmtblock_t block;
5318
5319 /* If the source is null, set the destination to null. */
5320 gfc_init_block (&block);
5321 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
5322 null_data = gfc_finish_block (&block);
5323
5324 gfc_init_block (&block);
5325
5326 nelems = get_full_array_size (&block, src, rank);
5327 size = fold_build2 (MULT_EXPR, gfc_array_index_type, nelems,
5328 fold_convert (gfc_array_index_type,
5329 TYPE_SIZE_UNIT (gfc_get_element_type (type))));
5330
5331 /* Allocate memory to the destination. */
5332 tmp = gfc_call_malloc (&block, TREE_TYPE (gfc_conv_descriptor_data_get (src)),
5333 size);
5334 gfc_conv_descriptor_data_set (&block, dest, tmp);
5335
5336 /* We know the temporary and the value will be the same length,
5337 so can use memcpy. */
5338 tmp = built_in_decls[BUILT_IN_MEMCPY];
5339 tmp = build_call_expr (tmp, 3, gfc_conv_descriptor_data_get (dest),
5340 gfc_conv_descriptor_data_get (src), size);
5341 gfc_add_expr_to_block (&block, tmp);
5342 tmp = gfc_finish_block (&block);
5343
5344 /* Null the destination if the source is null; otherwise do
5345 the allocate and copy. */
5346 null_cond = gfc_conv_descriptor_data_get (src);
5347 null_cond = convert (pvoid_type_node, null_cond);
5348 null_cond = fold_build2 (NE_EXPR, boolean_type_node,
5349 null_cond, null_pointer_node);
5350 return build3_v (COND_EXPR, null_cond, tmp, null_data);
5351 }
5352
5353
5354 /* Recursively traverse an object of derived type, generating code to
5355 deallocate, nullify or copy allocatable components. This is the work horse
5356 function for the functions named in this enum. */
5357
5358 enum {DEALLOCATE_ALLOC_COMP = 1, NULLIFY_ALLOC_COMP, COPY_ALLOC_COMP};
5359
5360 static tree
5361 structure_alloc_comps (gfc_symbol * der_type, tree decl,
5362 tree dest, int rank, int purpose)
5363 {
5364 gfc_component *c;
5365 gfc_loopinfo loop;
5366 stmtblock_t fnblock;
5367 stmtblock_t loopbody;
5368 tree tmp;
5369 tree comp;
5370 tree dcmp;
5371 tree nelems;
5372 tree index;
5373 tree var;
5374 tree cdecl;
5375 tree ctype;
5376 tree vref, dref;
5377 tree null_cond = NULL_TREE;
5378
5379 gfc_init_block (&fnblock);
5380
5381 if (POINTER_TYPE_P (TREE_TYPE (decl)))
5382 decl = build_fold_indirect_ref (decl);
5383
5384 /* If this an array of derived types with allocatable components
5385 build a loop and recursively call this function. */
5386 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5387 || GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
5388 {
5389 tmp = gfc_conv_array_data (decl);
5390 var = build_fold_indirect_ref (tmp);
5391
5392 /* Get the number of elements - 1 and set the counter. */
5393 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
5394 {
5395 /* Use the descriptor for an allocatable array. Since this
5396 is a full array reference, we only need the descriptor
5397 information from dimension = rank. */
5398 tmp = get_full_array_size (&fnblock, decl, rank);
5399 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
5400 tmp, gfc_index_one_node);
5401
5402 null_cond = gfc_conv_descriptor_data_get (decl);
5403 null_cond = fold_build2 (NE_EXPR, boolean_type_node, null_cond,
5404 build_int_cst (TREE_TYPE (null_cond), 0));
5405 }
5406 else
5407 {
5408 /* Otherwise use the TYPE_DOMAIN information. */
5409 tmp = array_type_nelts (TREE_TYPE (decl));
5410 tmp = fold_convert (gfc_array_index_type, tmp);
5411 }
5412
5413 /* Remember that this is, in fact, the no. of elements - 1. */
5414 nelems = gfc_evaluate_now (tmp, &fnblock);
5415 index = gfc_create_var (gfc_array_index_type, "S");
5416
5417 /* Build the body of the loop. */
5418 gfc_init_block (&loopbody);
5419
5420 vref = gfc_build_array_ref (var, index, NULL);
5421
5422 if (purpose == COPY_ALLOC_COMP)
5423 {
5424 tmp = gfc_duplicate_allocatable (dest, decl, TREE_TYPE(decl), rank);
5425 gfc_add_expr_to_block (&fnblock, tmp);
5426
5427 tmp = build_fold_indirect_ref (gfc_conv_descriptor_data_get (dest));
5428 dref = gfc_build_array_ref (tmp, index, NULL);
5429 tmp = structure_alloc_comps (der_type, vref, dref, rank, purpose);
5430 }
5431 else
5432 tmp = structure_alloc_comps (der_type, vref, NULL_TREE, rank, purpose);
5433
5434 gfc_add_expr_to_block (&loopbody, tmp);
5435
5436 /* Build the loop and return. */
5437 gfc_init_loopinfo (&loop);
5438 loop.dimen = 1;
5439 loop.from[0] = gfc_index_zero_node;
5440 loop.loopvar[0] = index;
5441 loop.to[0] = nelems;
5442 gfc_trans_scalarizing_loops (&loop, &loopbody);
5443 gfc_add_block_to_block (&fnblock, &loop.pre);
5444
5445 tmp = gfc_finish_block (&fnblock);
5446 if (null_cond != NULL_TREE)
5447 tmp = build3_v (COND_EXPR, null_cond, tmp, build_empty_stmt ());
5448
5449 return tmp;
5450 }
5451
5452 /* Otherwise, act on the components or recursively call self to
5453 act on a chain of components. */
5454 for (c = der_type->components; c; c = c->next)
5455 {
5456 bool cmp_has_alloc_comps = (c->ts.type == BT_DERIVED)
5457 && c->ts.derived->attr.alloc_comp;
5458 cdecl = c->backend_decl;
5459 ctype = TREE_TYPE (cdecl);
5460
5461 switch (purpose)
5462 {
5463 case DEALLOCATE_ALLOC_COMP:
5464 /* Do not deallocate the components of ultimate pointer
5465 components. */
5466 if (cmp_has_alloc_comps && !c->pointer)
5467 {
5468 comp = fold_build3 (COMPONENT_REF, ctype,
5469 decl, cdecl, NULL_TREE);
5470 rank = c->as ? c->as->rank : 0;
5471 tmp = structure_alloc_comps (c->ts.derived, comp, NULL_TREE,
5472 rank, purpose);
5473 gfc_add_expr_to_block (&fnblock, tmp);
5474 }
5475
5476 if (c->allocatable)
5477 {
5478 comp = fold_build3 (COMPONENT_REF, ctype,
5479 decl, cdecl, NULL_TREE);
5480 tmp = gfc_trans_dealloc_allocated (comp);
5481 gfc_add_expr_to_block (&fnblock, tmp);
5482 }
5483 break;
5484
5485 case NULLIFY_ALLOC_COMP:
5486 if (c->pointer)
5487 continue;
5488 else if (c->allocatable)
5489 {
5490 comp = fold_build3 (COMPONENT_REF, ctype,
5491 decl, cdecl, NULL_TREE);
5492 gfc_conv_descriptor_data_set (&fnblock, comp, null_pointer_node);
5493 }
5494 else if (cmp_has_alloc_comps)
5495 {
5496 comp = fold_build3 (COMPONENT_REF, ctype,
5497 decl, cdecl, NULL_TREE);
5498 rank = c->as ? c->as->rank : 0;
5499 tmp = structure_alloc_comps (c->ts.derived, comp, NULL_TREE,
5500 rank, purpose);
5501 gfc_add_expr_to_block (&fnblock, tmp);
5502 }
5503 break;
5504
5505 case COPY_ALLOC_COMP:
5506 if (c->pointer)
5507 continue;
5508
5509 /* We need source and destination components. */
5510 comp = fold_build3 (COMPONENT_REF, ctype, decl, cdecl, NULL_TREE);
5511 dcmp = fold_build3 (COMPONENT_REF, ctype, dest, cdecl, NULL_TREE);
5512 dcmp = fold_convert (TREE_TYPE (comp), dcmp);
5513
5514 if (c->allocatable && !cmp_has_alloc_comps)
5515 {
5516 tmp = gfc_duplicate_allocatable(dcmp, comp, ctype, c->as->rank);
5517 gfc_add_expr_to_block (&fnblock, tmp);
5518 }
5519
5520 if (cmp_has_alloc_comps)
5521 {
5522 rank = c->as ? c->as->rank : 0;
5523 tmp = fold_convert (TREE_TYPE (dcmp), comp);
5524 gfc_add_modify_expr (&fnblock, dcmp, tmp);
5525 tmp = structure_alloc_comps (c->ts.derived, comp, dcmp,
5526 rank, purpose);
5527 gfc_add_expr_to_block (&fnblock, tmp);
5528 }
5529 break;
5530
5531 default:
5532 gcc_unreachable ();
5533 break;
5534 }
5535 }
5536
5537 return gfc_finish_block (&fnblock);
5538 }
5539
5540 /* Recursively traverse an object of derived type, generating code to
5541 nullify allocatable components. */
5542
5543 tree
5544 gfc_nullify_alloc_comp (gfc_symbol * der_type, tree decl, int rank)
5545 {
5546 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
5547 NULLIFY_ALLOC_COMP);
5548 }
5549
5550
5551 /* Recursively traverse an object of derived type, generating code to
5552 deallocate allocatable components. */
5553
5554 tree
5555 gfc_deallocate_alloc_comp (gfc_symbol * der_type, tree decl, int rank)
5556 {
5557 return structure_alloc_comps (der_type, decl, NULL_TREE, rank,
5558 DEALLOCATE_ALLOC_COMP);
5559 }
5560
5561
5562 /* Recursively traverse an object of derived type, generating code to
5563 copy its allocatable components. */
5564
5565 tree
5566 gfc_copy_alloc_comp (gfc_symbol * der_type, tree decl, tree dest, int rank)
5567 {
5568 return structure_alloc_comps (der_type, decl, dest, rank, COPY_ALLOC_COMP);
5569 }
5570
5571
5572 /* NULLIFY an allocatable/pointer array on function entry, free it on exit.
5573 Do likewise, recursively if necessary, with the allocatable components of
5574 derived types. */
5575
5576 tree
5577 gfc_trans_deferred_array (gfc_symbol * sym, tree body)
5578 {
5579 tree type;
5580 tree tmp;
5581 tree descriptor;
5582 stmtblock_t fnblock;
5583 locus loc;
5584 int rank;
5585 bool sym_has_alloc_comp;
5586
5587 sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
5588 && sym->ts.derived->attr.alloc_comp;
5589
5590 /* Make sure the frontend gets these right. */
5591 if (!(sym->attr.pointer || sym->attr.allocatable || sym_has_alloc_comp))
5592 fatal_error ("Possible frontend bug: Deferred array size without pointer, "
5593 "allocatable attribute or derived type without allocatable "
5594 "components.");
5595
5596 gfc_init_block (&fnblock);
5597
5598 gcc_assert (TREE_CODE (sym->backend_decl) == VAR_DECL
5599 || TREE_CODE (sym->backend_decl) == PARM_DECL);
5600
5601 if (sym->ts.type == BT_CHARACTER
5602 && !INTEGER_CST_P (sym->ts.cl->backend_decl))
5603 {
5604 gfc_conv_string_length (sym->ts.cl, &fnblock);
5605 gfc_trans_vla_type_sizes (sym, &fnblock);
5606 }
5607
5608 /* Dummy and use associated variables don't need anything special. */
5609 if (sym->attr.dummy || sym->attr.use_assoc)
5610 {
5611 gfc_add_expr_to_block (&fnblock, body);
5612
5613 return gfc_finish_block (&fnblock);
5614 }
5615
5616 gfc_get_backend_locus (&loc);
5617 gfc_set_backend_locus (&sym->declared_at);
5618 descriptor = sym->backend_decl;
5619
5620 /* Although static, derived types with default initializers and
5621 allocatable components must not be nulled wholesale; instead they
5622 are treated component by component. */
5623 if (TREE_STATIC (descriptor) && !sym_has_alloc_comp)
5624 {
5625 /* SAVEd variables are not freed on exit. */
5626 gfc_trans_static_array_pointer (sym);
5627 return body;
5628 }
5629
5630 /* Get the descriptor type. */
5631 type = TREE_TYPE (sym->backend_decl);
5632
5633 if (sym_has_alloc_comp && !(sym->attr.pointer || sym->attr.allocatable))
5634 {
5635 if (!sym->attr.save)
5636 {
5637 rank = sym->as ? sym->as->rank : 0;
5638 tmp = gfc_nullify_alloc_comp (sym->ts.derived, descriptor, rank);
5639 gfc_add_expr_to_block (&fnblock, tmp);
5640 if (sym->value)
5641 {
5642 tmp = gfc_init_default_dt (sym, NULL);
5643 gfc_add_expr_to_block (&fnblock, tmp);
5644 }
5645 }
5646 }
5647 else if (!GFC_DESCRIPTOR_TYPE_P (type))
5648 {
5649 /* If the backend_decl is not a descriptor, we must have a pointer
5650 to one. */
5651 descriptor = build_fold_indirect_ref (sym->backend_decl);
5652 type = TREE_TYPE (descriptor);
5653 }
5654
5655 /* NULLIFY the data pointer. */
5656 if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save)
5657 gfc_conv_descriptor_data_set (&fnblock, descriptor, null_pointer_node);
5658
5659 gfc_add_expr_to_block (&fnblock, body);
5660
5661 gfc_set_backend_locus (&loc);
5662
5663 /* Allocatable arrays need to be freed when they go out of scope.
5664 The allocatable components of pointers must not be touched. */
5665 if (sym_has_alloc_comp && !(sym->attr.function || sym->attr.result)
5666 && !sym->attr.pointer && !sym->attr.save)
5667 {
5668 int rank;
5669 rank = sym->as ? sym->as->rank : 0;
5670 tmp = gfc_deallocate_alloc_comp (sym->ts.derived, descriptor, rank);
5671 gfc_add_expr_to_block (&fnblock, tmp);
5672 }
5673
5674 if (sym->attr.allocatable && !sym->attr.save)
5675 {
5676 tmp = gfc_trans_dealloc_allocated (sym->backend_decl);
5677 gfc_add_expr_to_block (&fnblock, tmp);
5678 }
5679
5680 return gfc_finish_block (&fnblock);
5681 }
5682
5683 /************ Expression Walking Functions ******************/
5684
5685 /* Walk a variable reference.
5686
5687 Possible extension - multiple component subscripts.
5688 x(:,:) = foo%a(:)%b(:)
5689 Transforms to
5690 forall (i=..., j=...)
5691 x(i,j) = foo%a(j)%b(i)
5692 end forall
5693 This adds a fair amount of complexity because you need to deal with more
5694 than one ref. Maybe handle in a similar manner to vector subscripts.
5695 Maybe not worth the effort. */
5696
5697
5698 static gfc_ss *
5699 gfc_walk_variable_expr (gfc_ss * ss, gfc_expr * expr)
5700 {
5701 gfc_ref *ref;
5702 gfc_array_ref *ar;
5703 gfc_ss *newss;
5704 gfc_ss *head;
5705 int n;
5706
5707 for (ref = expr->ref; ref; ref = ref->next)
5708 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
5709 break;
5710
5711 for (; ref; ref = ref->next)
5712 {
5713 if (ref->type == REF_SUBSTRING)
5714 {
5715 newss = gfc_get_ss ();
5716 newss->type = GFC_SS_SCALAR;
5717 newss->expr = ref->u.ss.start;
5718 newss->next = ss;
5719 ss = newss;
5720
5721 newss = gfc_get_ss ();
5722 newss->type = GFC_SS_SCALAR;
5723 newss->expr = ref->u.ss.end;
5724 newss->next = ss;
5725 ss = newss;
5726 }
5727
5728 /* We're only interested in array sections from now on. */
5729 if (ref->type != REF_ARRAY)
5730 continue;
5731
5732 ar = &ref->u.ar;
5733 switch (ar->type)
5734 {
5735 case AR_ELEMENT:
5736 for (n = 0; n < ar->dimen; n++)
5737 {
5738 newss = gfc_get_ss ();
5739 newss->type = GFC_SS_SCALAR;
5740 newss->expr = ar->start[n];
5741 newss->next = ss;
5742 ss = newss;
5743 }
5744 break;
5745
5746 case AR_FULL:
5747 newss = gfc_get_ss ();
5748 newss->type = GFC_SS_SECTION;
5749 newss->expr = expr;
5750 newss->next = ss;
5751 newss->data.info.dimen = ar->as->rank;
5752 newss->data.info.ref = ref;
5753
5754 /* Make sure array is the same as array(:,:), this way
5755 we don't need to special case all the time. */
5756 ar->dimen = ar->as->rank;
5757 for (n = 0; n < ar->dimen; n++)
5758 {
5759 newss->data.info.dim[n] = n;
5760 ar->dimen_type[n] = DIMEN_RANGE;
5761
5762 gcc_assert (ar->start[n] == NULL);
5763 gcc_assert (ar->end[n] == NULL);
5764 gcc_assert (ar->stride[n] == NULL);
5765 }
5766 ss = newss;
5767 break;
5768
5769 case AR_SECTION:
5770 newss = gfc_get_ss ();
5771 newss->type = GFC_SS_SECTION;
5772 newss->expr = expr;
5773 newss->next = ss;
5774 newss->data.info.dimen = 0;
5775 newss->data.info.ref = ref;
5776
5777 head = newss;
5778
5779 /* We add SS chains for all the subscripts in the section. */
5780 for (n = 0; n < ar->dimen; n++)
5781 {
5782 gfc_ss *indexss;
5783
5784 switch (ar->dimen_type[n])
5785 {
5786 case DIMEN_ELEMENT:
5787 /* Add SS for elemental (scalar) subscripts. */
5788 gcc_assert (ar->start[n]);
5789 indexss = gfc_get_ss ();
5790 indexss->type = GFC_SS_SCALAR;
5791 indexss->expr = ar->start[n];
5792 indexss->next = gfc_ss_terminator;
5793 indexss->loop_chain = gfc_ss_terminator;
5794 newss->data.info.subscript[n] = indexss;
5795 break;
5796
5797 case DIMEN_RANGE:
5798 /* We don't add anything for sections, just remember this
5799 dimension for later. */
5800 newss->data.info.dim[newss->data.info.dimen] = n;
5801 newss->data.info.dimen++;
5802 break;
5803
5804 case DIMEN_VECTOR:
5805 /* Create a GFC_SS_VECTOR index in which we can store
5806 the vector's descriptor. */
5807 indexss = gfc_get_ss ();
5808 indexss->type = GFC_SS_VECTOR;
5809 indexss->expr = ar->start[n];
5810 indexss->next = gfc_ss_terminator;
5811 indexss->loop_chain = gfc_ss_terminator;
5812 newss->data.info.subscript[n] = indexss;
5813 newss->data.info.dim[newss->data.info.dimen] = n;
5814 newss->data.info.dimen++;
5815 break;
5816
5817 default:
5818 /* We should know what sort of section it is by now. */
5819 gcc_unreachable ();
5820 }
5821 }
5822 /* We should have at least one non-elemental dimension. */
5823 gcc_assert (newss->data.info.dimen > 0);
5824 ss = newss;
5825 break;
5826
5827 default:
5828 /* We should know what sort of section it is by now. */
5829 gcc_unreachable ();
5830 }
5831
5832 }
5833 return ss;
5834 }
5835
5836
5837 /* Walk an expression operator. If only one operand of a binary expression is
5838 scalar, we must also add the scalar term to the SS chain. */
5839
5840 static gfc_ss *
5841 gfc_walk_op_expr (gfc_ss * ss, gfc_expr * expr)
5842 {
5843 gfc_ss *head;
5844 gfc_ss *head2;
5845 gfc_ss *newss;
5846
5847 head = gfc_walk_subexpr (ss, expr->value.op.op1);
5848 if (expr->value.op.op2 == NULL)
5849 head2 = head;
5850 else
5851 head2 = gfc_walk_subexpr (head, expr->value.op.op2);
5852
5853 /* All operands are scalar. Pass back and let the caller deal with it. */
5854 if (head2 == ss)
5855 return head2;
5856
5857 /* All operands require scalarization. */
5858 if (head != ss && (expr->value.op.op2 == NULL || head2 != head))
5859 return head2;
5860
5861 /* One of the operands needs scalarization, the other is scalar.
5862 Create a gfc_ss for the scalar expression. */
5863 newss = gfc_get_ss ();
5864 newss->type = GFC_SS_SCALAR;
5865 if (head == ss)
5866 {
5867 /* First operand is scalar. We build the chain in reverse order, so
5868 add the scarar SS after the second operand. */
5869 head = head2;
5870 while (head && head->next != ss)
5871 head = head->next;
5872 /* Check we haven't somehow broken the chain. */
5873 gcc_assert (head);
5874 newss->next = ss;
5875 head->next = newss;
5876 newss->expr = expr->value.op.op1;
5877 }
5878 else /* head2 == head */
5879 {
5880 gcc_assert (head2 == head);
5881 /* Second operand is scalar. */
5882 newss->next = head2;
5883 head2 = newss;
5884 newss->expr = expr->value.op.op2;
5885 }
5886
5887 return head2;
5888 }
5889
5890
5891 /* Reverse a SS chain. */
5892
5893 gfc_ss *
5894 gfc_reverse_ss (gfc_ss * ss)
5895 {
5896 gfc_ss *next;
5897 gfc_ss *head;
5898
5899 gcc_assert (ss != NULL);
5900
5901 head = gfc_ss_terminator;
5902 while (ss != gfc_ss_terminator)
5903 {
5904 next = ss->next;
5905 /* Check we didn't somehow break the chain. */
5906 gcc_assert (next != NULL);
5907 ss->next = head;
5908 head = ss;
5909 ss = next;
5910 }
5911
5912 return (head);
5913 }
5914
5915
5916 /* Walk the arguments of an elemental function. */
5917
5918 gfc_ss *
5919 gfc_walk_elemental_function_args (gfc_ss * ss, gfc_actual_arglist *arg,
5920 gfc_ss_type type)
5921 {
5922 int scalar;
5923 gfc_ss *head;
5924 gfc_ss *tail;
5925 gfc_ss *newss;
5926
5927 head = gfc_ss_terminator;
5928 tail = NULL;
5929 scalar = 1;
5930 for (; arg; arg = arg->next)
5931 {
5932 if (!arg->expr)
5933 continue;
5934
5935 newss = gfc_walk_subexpr (head, arg->expr);
5936 if (newss == head)
5937 {
5938 /* Scalar argument. */
5939 newss = gfc_get_ss ();
5940 newss->type = type;
5941 newss->expr = arg->expr;
5942 newss->next = head;
5943 }
5944 else
5945 scalar = 0;
5946
5947 head = newss;
5948 if (!tail)
5949 {
5950 tail = head;
5951 while (tail->next != gfc_ss_terminator)
5952 tail = tail->next;
5953 }
5954 }
5955
5956 if (scalar)
5957 {
5958 /* If all the arguments are scalar we don't need the argument SS. */
5959 gfc_free_ss_chain (head);
5960 /* Pass it back. */
5961 return ss;
5962 }
5963
5964 /* Add it onto the existing chain. */
5965 tail->next = ss;
5966 return head;
5967 }
5968
5969
5970 /* Walk a function call. Scalar functions are passed back, and taken out of
5971 scalarization loops. For elemental functions we walk their arguments.
5972 The result of functions returning arrays is stored in a temporary outside
5973 the loop, so that the function is only called once. Hence we do not need
5974 to walk their arguments. */
5975
5976 static gfc_ss *
5977 gfc_walk_function_expr (gfc_ss * ss, gfc_expr * expr)
5978 {
5979 gfc_ss *newss;
5980 gfc_intrinsic_sym *isym;
5981 gfc_symbol *sym;
5982
5983 isym = expr->value.function.isym;
5984
5985 /* Handle intrinsic functions separately. */
5986 if (isym)
5987 return gfc_walk_intrinsic_function (ss, expr, isym);
5988
5989 sym = expr->value.function.esym;
5990 if (!sym)
5991 sym = expr->symtree->n.sym;
5992
5993 /* A function that returns arrays. */
5994 if (gfc_return_by_reference (sym) && sym->result->attr.dimension)
5995 {
5996 newss = gfc_get_ss ();
5997 newss->type = GFC_SS_FUNCTION;
5998 newss->expr = expr;
5999 newss->next = ss;
6000 newss->data.info.dimen = expr->rank;
6001 return newss;
6002 }
6003
6004 /* Walk the parameters of an elemental function. For now we always pass
6005 by reference. */
6006 if (sym->attr.elemental)
6007 return gfc_walk_elemental_function_args (ss, expr->value.function.actual,
6008 GFC_SS_REFERENCE);
6009
6010 /* Scalar functions are OK as these are evaluated outside the scalarization
6011 loop. Pass back and let the caller deal with it. */
6012 return ss;
6013 }
6014
6015
6016 /* An array temporary is constructed for array constructors. */
6017
6018 static gfc_ss *
6019 gfc_walk_array_constructor (gfc_ss * ss, gfc_expr * expr)
6020 {
6021 gfc_ss *newss;
6022 int n;
6023
6024 newss = gfc_get_ss ();
6025 newss->type = GFC_SS_CONSTRUCTOR;
6026 newss->expr = expr;
6027 newss->next = ss;
6028 newss->data.info.dimen = expr->rank;
6029 for (n = 0; n < expr->rank; n++)
6030 newss->data.info.dim[n] = n;
6031
6032 return newss;
6033 }
6034
6035
6036 /* Walk an expression. Add walked expressions to the head of the SS chain.
6037 A wholly scalar expression will not be added. */
6038
6039 static gfc_ss *
6040 gfc_walk_subexpr (gfc_ss * ss, gfc_expr * expr)
6041 {
6042 gfc_ss *head;
6043
6044 switch (expr->expr_type)
6045 {
6046 case EXPR_VARIABLE:
6047 head = gfc_walk_variable_expr (ss, expr);
6048 return head;
6049
6050 case EXPR_OP:
6051 head = gfc_walk_op_expr (ss, expr);
6052 return head;
6053
6054 case EXPR_FUNCTION:
6055 head = gfc_walk_function_expr (ss, expr);
6056 return head;
6057
6058 case EXPR_CONSTANT:
6059 case EXPR_NULL:
6060 case EXPR_STRUCTURE:
6061 /* Pass back and let the caller deal with it. */
6062 break;
6063
6064 case EXPR_ARRAY:
6065 head = gfc_walk_array_constructor (ss, expr);
6066 return head;
6067
6068 case EXPR_SUBSTRING:
6069 /* Pass back and let the caller deal with it. */
6070 break;
6071
6072 default:
6073 internal_error ("bad expression type during walk (%d)",
6074 expr->expr_type);
6075 }
6076 return ss;
6077 }
6078
6079
6080 /* Entry point for expression walking.
6081 A return value equal to the passed chain means this is
6082 a scalar expression. It is up to the caller to take whatever action is
6083 necessary to translate these. */
6084
6085 gfc_ss *
6086 gfc_walk_expr (gfc_expr * expr)
6087 {
6088 gfc_ss *res;
6089
6090 res = gfc_walk_subexpr (gfc_ss_terminator, expr);
6091 return gfc_reverse_ss (res);
6092 }