Debian Codegraph @

gcc/fortran/trans-stmt.cc

raw
1 /* Statement translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <[email protected]>
4 and Steven Bosscher <[email protected]>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #define INCLUDE_VECTOR
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "gfortran.h"
29 #include "trans.h"
30 #include "stringpool.h"
31 #include "fold-const.h"
32 #include "trans-stmt.h"
33 #include "trans-types.h"
34 #include "trans-array.h"
35 #include "trans-const.h"
36 #include "dependency.h"
37
38 typedef struct iter_info
39 {
40 tree var;
41 tree start;
42 tree end;
43 tree step;
44 gfc_loop_annot annot;
45 struct iter_info *next;
46 }
47 iter_info;
48
49 typedef struct forall_info
50 {
51 iter_info *this_loop;
52 tree mask;
53 tree maskindex;
54 int nvar;
55 tree size;
56 struct forall_info *prev_nest;
57 bool do_concurrent;
58 }
59 forall_info;
60
61 static void gfc_trans_where_2 (gfc_code *, tree, bool,
62 forall_info *, stmtblock_t *);
63
64 /* Translate a F95 label number to a LABEL_EXPR. */
65
66 tree
67 gfc_trans_label_here (gfc_code * code)
68 {
69 return build1_v (LABEL_EXPR, gfc_get_label_decl (code->here));
70 }
71
72
73 /* Given a variable expression which has been ASSIGNed to, find the decl
74 containing the auxiliary variables. For variables in common blocks this
75 is a field_decl. */
76
77 void
78 gfc_conv_label_variable (gfc_se * se, gfc_expr * expr)
79 {
80 gcc_assert (expr->symtree->n.sym->attr.assign == 1);
81 gfc_conv_expr (se, expr);
82 /* Deals with variable in common block. Get the field declaration. */
83 if (TREE_CODE (se->expr) == COMPONENT_REF)
84 se->expr = TREE_OPERAND (se->expr, 1);
85 /* Deals with dummy argument. Get the parameter declaration. */
86 else if (INDIRECT_REF_P (se->expr))
87 se->expr = TREE_OPERAND (se->expr, 0);
88 }
89
90 /* Translate a label assignment statement. */
91
92 tree
93 gfc_trans_label_assign (gfc_code * code)
94 {
95 tree label_tree;
96 gfc_se se;
97 tree len;
98 tree addr;
99 tree len_tree;
100 int label_len;
101
102 /* Start a new block. */
103 gfc_init_se (&se, NULL);
104 gfc_start_block (&se.pre);
105 gfc_conv_label_variable (&se, code->expr1);
106
107 len = GFC_DECL_STRING_LEN (se.expr);
108 addr = GFC_DECL_ASSIGN_ADDR (se.expr);
109
110 label_tree = gfc_get_label_decl (code->label1);
111
112 if (code->label1->defined == ST_LABEL_TARGET
113 || code->label1->defined == ST_LABEL_DO_TARGET)
114 {
115 label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
116 len_tree = build_int_cst (gfc_charlen_type_node, -1);
117 }
118 else
119 {
120 gfc_expr *format = code->label1->format;
121
122 label_len = format->value.character.length;
123 len_tree = build_int_cst (gfc_charlen_type_node, label_len);
124 label_tree = gfc_build_wide_string_const (format->ts.kind, label_len + 1,
125 format->value.character.string);
126 label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
127 }
128
129 gfc_add_modify (&se.pre, len, fold_convert (TREE_TYPE (len), len_tree));
130 gfc_add_modify (&se.pre, addr, label_tree);
131
132 return gfc_finish_block (&se.pre);
133 }
134
135 /* Translate a GOTO statement. */
136
137 tree
138 gfc_trans_goto (gfc_code * code)
139 {
140 locus loc = code->loc;
141 tree assigned_goto;
142 tree target;
143 tree tmp;
144 gfc_se se;
145
146 if (code->label1 != NULL)
147 return build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
148
149 /* ASSIGNED GOTO. */
150 gfc_init_se (&se, NULL);
151 gfc_start_block (&se.pre);
152 gfc_conv_label_variable (&se, code->expr1);
153 tmp = GFC_DECL_STRING_LEN (se.expr);
154 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
155 build_int_cst (TREE_TYPE (tmp), -1));
156 gfc_trans_runtime_check (true, false, tmp, &se.pre, &loc,
157 "Assigned label is not a target label");
158
159 assigned_goto = GFC_DECL_ASSIGN_ADDR (se.expr);
160
161 /* We're going to ignore a label list. It does not really change the
162 statement's semantics (because it is just a further restriction on
163 what's legal code); before, we were comparing label addresses here, but
164 that's a very fragile business and may break with optimization. So
165 just ignore it. */
166
167 target = fold_build1_loc (input_location, GOTO_EXPR, void_type_node,
168 assigned_goto);
169 gfc_add_expr_to_block (&se.pre, target);
170 return gfc_finish_block (&se.pre);
171 }
172
173
174 /* Translate an ENTRY statement. Just adds a label for this entry point. */
175 tree
176 gfc_trans_entry (gfc_code * code)
177 {
178 return build1_v (LABEL_EXPR, code->ext.entry->label);
179 }
180
181
182 /* Replace a gfc_ss structure by another both in the gfc_se struct
183 and the gfc_loopinfo struct. This is used in gfc_conv_elemental_dependencies
184 to replace a variable ss by the corresponding temporary. */
185
186 static void
187 replace_ss (gfc_se *se, gfc_ss *old_ss, gfc_ss *new_ss)
188 {
189 gfc_ss **sess, **loopss;
190
191 /* The old_ss is a ss for a single variable. */
192 gcc_assert (old_ss->info->type == GFC_SS_SECTION);
193
194 for (sess = &(se->ss); *sess != gfc_ss_terminator; sess = &((*sess)->next))
195 if (*sess == old_ss)
196 break;
197 gcc_assert (*sess != gfc_ss_terminator);
198
199 *sess = new_ss;
200 new_ss->next = old_ss->next;
201
202 /* Make sure that trailing references are not lost. */
203 if (old_ss->info
204 && old_ss->info->data.array.ref
205 && old_ss->info->data.array.ref->next
206 && !(new_ss->info->data.array.ref
207 && new_ss->info->data.array.ref->next))
208 new_ss->info->data.array.ref = old_ss->info->data.array.ref;
209
210 for (loopss = &(se->loop->ss); *loopss != gfc_ss_terminator;
211 loopss = &((*loopss)->loop_chain))
212 if (*loopss == old_ss)
213 break;
214 gcc_assert (*loopss != gfc_ss_terminator);
215
216 *loopss = new_ss;
217 new_ss->loop_chain = old_ss->loop_chain;
218 new_ss->loop = old_ss->loop;
219
220 gfc_free_ss (old_ss);
221 }
222
223
224 /* Check for dependencies between INTENT(IN) and INTENT(OUT) arguments of
225 elemental subroutines. Make temporaries for output arguments if any such
226 dependencies are found. Output arguments are chosen because internal_unpack
227 can be used, as is, to copy the result back to the variable. */
228 static void
229 gfc_conv_elemental_dependencies (gfc_se * se, gfc_se * loopse,
230 gfc_symbol * sym, gfc_actual_arglist * arg,
231 gfc_dep_check check_variable)
232 {
233 gfc_actual_arglist *arg0;
234 gfc_expr *e;
235 gfc_formal_arglist *formal;
236 gfc_se parmse;
237 gfc_ss *ss;
238 gfc_symbol *fsym;
239 tree data;
240 tree size;
241 tree tmp;
242
243 if (loopse->ss == NULL)
244 return;
245
246 ss = loopse->ss;
247 arg0 = arg;
248 formal = gfc_sym_get_dummy_args (sym);
249
250 /* Loop over all the arguments testing for dependencies. */
251 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
252 {
253 e = arg->expr;
254 if (e == NULL)
255 continue;
256
257 /* Obtain the info structure for the current argument. */
258 for (ss = loopse->ss; ss && ss != gfc_ss_terminator; ss = ss->next)
259 if (ss->info->expr == e)
260 break;
261
262 /* If there is a dependency, create a temporary and use it
263 instead of the variable. */
264 fsym = formal ? formal->sym : NULL;
265 if (e->expr_type == EXPR_VARIABLE
266 && e->rank && fsym
267 && fsym->attr.intent != INTENT_IN
268 && !fsym->attr.value
269 && gfc_check_fncall_dependency (e, fsym->attr.intent,
270 sym, arg0, check_variable))
271 {
272 tree initial, temptype;
273 stmtblock_t temp_post;
274 gfc_ss *tmp_ss;
275
276 tmp_ss = gfc_get_array_ss (gfc_ss_terminator, NULL, ss->dimen,
277 GFC_SS_SECTION);
278 gfc_mark_ss_chain_used (tmp_ss, 1);
279 tmp_ss->info->expr = ss->info->expr;
280 replace_ss (loopse, ss, tmp_ss);
281
282 /* Obtain the argument descriptor for unpacking. */
283 gfc_init_se (&parmse, NULL);
284 parmse.want_pointer = 1;
285 gfc_conv_expr_descriptor (&parmse, e);
286 gfc_add_block_to_block (&se->pre, &parmse.pre);
287
288 /* If we've got INTENT(INOUT) or a derived type with INTENT(OUT),
289 initialize the array temporary with a copy of the values. */
290 if (fsym->attr.intent == INTENT_INOUT
291 || (fsym->ts.type ==BT_DERIVED
292 && fsym->attr.intent == INTENT_OUT))
293 initial = parmse.expr;
294 /* For class expressions, we always initialize with the copy of
295 the values. */
296 else if (e->ts.type == BT_CLASS)
297 initial = parmse.expr;
298 else
299 initial = NULL_TREE;
300
301 if (e->ts.type != BT_CLASS)
302 {
303 /* Find the type of the temporary to create; we don't use the type
304 of e itself as this breaks for subcomponent-references in e
305 (where the type of e is that of the final reference, but
306 parmse.expr's type corresponds to the full derived-type). */
307 /* TODO: Fix this somehow so we don't need a temporary of the whole
308 array but instead only the components referenced. */
309 temptype = TREE_TYPE (parmse.expr); /* Pointer to descriptor. */
310 gcc_assert (TREE_CODE (temptype) == POINTER_TYPE);
311 temptype = TREE_TYPE (temptype);
312 temptype = gfc_get_element_type (temptype);
313 }
314
315 else
316 /* For class arrays signal that the size of the dynamic type has to
317 be obtained from the vtable, using the 'initial' expression. */
318 temptype = NULL_TREE;
319
320 /* Generate the temporary. Cleaning up the temporary should be the
321 very last thing done, so we add the code to a new block and add it
322 to se->post as last instructions. */
323 size = gfc_create_var (gfc_array_index_type, NULL);
324 data = gfc_create_var (pvoid_type_node, NULL);
325 gfc_init_block (&temp_post);
326 tmp = gfc_trans_create_temp_array (&se->pre, &temp_post, tmp_ss,
327 temptype, initial, false, true,
328 false, &arg->expr->where);
329 gfc_add_modify (&se->pre, size, tmp);
330 tmp = fold_convert (pvoid_type_node, tmp_ss->info->data.array.data);
331 gfc_add_modify (&se->pre, data, tmp);
332
333 /* Update other ss' delta. */
334 gfc_set_delta (loopse->loop);
335
336 /* Copy the result back using unpack..... */
337 if (e->ts.type != BT_CLASS)
338 tmp = build_call_expr_loc (input_location,
339 gfor_fndecl_in_unpack, 2, parmse.expr, data);
340 else
341 {
342 /* ... except for class results where the copy is
343 unconditional. */
344 tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
345 tmp = gfc_conv_descriptor_data_get (tmp);
346 tmp = build_call_expr_loc (input_location,
347 builtin_decl_explicit (BUILT_IN_MEMCPY),
348 3, tmp, data,
349 fold_convert (size_type_node, size));
350 }
351 gfc_add_expr_to_block (&se->post, tmp);
352
353 /* parmse.pre is already added above. */
354 gfc_add_block_to_block (&se->post, &parmse.post);
355 gfc_add_block_to_block (&se->post, &temp_post);
356 }
357 }
358 }
359
360
361 /* Given an executable statement referring to an intrinsic function call,
362 returns the intrinsic symbol. */
363
364 static gfc_intrinsic_sym *
365 get_intrinsic_for_code (gfc_code *code)
366 {
367 if (code->op == EXEC_CALL)
368 {
369 gfc_intrinsic_sym * const isym = code->resolved_isym;
370 if (isym)
371 return isym;
372 else
373 return gfc_get_intrinsic_for_expr (code->expr1);
374 }
375
376 return NULL;
377 }
378
379
380 /* Translate the CALL statement. Builds a call to an F95 subroutine. */
381
382 tree
383 gfc_trans_call (gfc_code * code, bool dependency_check,
384 tree mask, tree count1, bool invert)
385 {
386 gfc_se se;
387 gfc_ss * ss;
388 int has_alternate_specifier;
389 gfc_dep_check check_variable;
390 tree index = NULL_TREE;
391 tree maskexpr = NULL_TREE;
392 tree tmp;
393 bool is_intrinsic_mvbits;
394
395 /* A CALL starts a new block because the actual arguments may have to
396 be evaluated first. */
397 gfc_init_se (&se, NULL);
398 gfc_start_block (&se.pre);
399
400 gcc_assert (code->resolved_sym);
401
402 ss = gfc_ss_terminator;
403 if (code->resolved_sym->attr.elemental)
404 ss = gfc_walk_elemental_function_args (ss, code->ext.actual,
405 get_intrinsic_for_code (code),
406 GFC_SS_REFERENCE);
407
408 /* MVBITS is inlined but needs the dependency checking found here. */
409 is_intrinsic_mvbits = code->resolved_isym
410 && code->resolved_isym->id == GFC_ISYM_MVBITS;
411
412 /* Is not an elemental subroutine call with array valued arguments. */
413 if (ss == gfc_ss_terminator)
414 {
415
416 if (is_intrinsic_mvbits)
417 {
418 has_alternate_specifier = 0;
419 gfc_conv_intrinsic_mvbits (&se, code->ext.actual, NULL);
420 }
421 else
422 {
423 /* Translate the call. */
424 has_alternate_specifier =
425 gfc_conv_procedure_call (&se, code->resolved_sym,
426 code->ext.actual, code->expr1, NULL);
427
428 /* A subroutine without side-effect, by definition, does nothing! */
429 TREE_SIDE_EFFECTS (se.expr) = 1;
430 }
431
432 /* Chain the pieces together and return the block. */
433 if (has_alternate_specifier)
434 {
435 gfc_code *select_code;
436 gfc_symbol *sym;
437 select_code = code->next;
438 gcc_assert(select_code->op == EXEC_SELECT);
439 sym = select_code->expr1->symtree->n.sym;
440 se.expr = convert (gfc_typenode_for_spec (&sym->ts), se.expr);
441 if (sym->backend_decl == NULL)
442 sym->backend_decl = gfc_get_symbol_decl (sym);
443 gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
444 }
445 else
446 gfc_add_expr_to_block (&se.pre, se.expr);
447
448 gfc_add_block_to_block (&se.finalblock, &se.post);
449 gfc_add_block_to_block (&se.pre, &se.finalblock);
450 }
451
452 else
453 {
454 /* An elemental subroutine call with array valued arguments has
455 to be scalarized. */
456 gfc_loopinfo loop;
457 stmtblock_t body;
458 stmtblock_t block;
459 gfc_se loopse;
460 gfc_se depse;
461
462 /* gfc_walk_elemental_function_args renders the ss chain in the
463 reverse order to the actual argument order. */
464 ss = gfc_reverse_ss (ss);
465
466 /* Initialize the loop. */
467 gfc_init_se (&loopse, NULL);
468 gfc_init_loopinfo (&loop);
469 gfc_add_ss_to_loop (&loop, ss);
470
471 gfc_conv_ss_startstride (&loop);
472 /* TODO: gfc_conv_loop_setup generates a temporary for vector
473 subscripts. This could be prevented in the elemental case
474 as temporaries are handled separately
475 (below in gfc_conv_elemental_dependencies). */
476 if (code->expr1)
477 gfc_conv_loop_setup (&loop, &code->expr1->where);
478 else
479 gfc_conv_loop_setup (&loop, &code->loc);
480
481 gfc_mark_ss_chain_used (ss, 1);
482
483 /* Convert the arguments, checking for dependencies. */
484 gfc_copy_loopinfo_to_se (&loopse, &loop);
485 loopse.ss = ss;
486
487 /* For operator assignment, do dependency checking. */
488 if (dependency_check)
489 check_variable = ELEM_CHECK_VARIABLE;
490 else
491 check_variable = ELEM_DONT_CHECK_VARIABLE;
492
493 gfc_init_se (&depse, NULL);
494 gfc_conv_elemental_dependencies (&depse, &loopse, code->resolved_sym,
495 code->ext.actual, check_variable);
496
497 gfc_add_block_to_block (&loop.pre, &depse.pre);
498 gfc_add_block_to_block (&loop.post, &depse.post);
499
500 /* Generate the loop body. */
501 gfc_start_scalarized_body (&loop, &body);
502 gfc_init_block (&block);
503
504 if (mask && count1)
505 {
506 /* Form the mask expression according to the mask. */
507 index = count1;
508 maskexpr = gfc_build_array_ref (mask, index, NULL);
509 if (invert)
510 maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
511 TREE_TYPE (maskexpr), maskexpr);
512 }
513
514 if (is_intrinsic_mvbits)
515 {
516 has_alternate_specifier = 0;
517 gfc_conv_intrinsic_mvbits (&loopse, code->ext.actual, &loop);
518 }
519 else
520 {
521 /* Add the subroutine call to the block. */
522 gfc_conv_procedure_call (&loopse, code->resolved_sym,
523 code->ext.actual, code->expr1,
524 NULL);
525 }
526
527 if (mask && count1)
528 {
529 tmp = build3_v (COND_EXPR, maskexpr, loopse.expr,
530 build_empty_stmt (input_location));
531 gfc_add_expr_to_block (&loopse.pre, tmp);
532 tmp = fold_build2_loc (input_location, PLUS_EXPR,
533 gfc_array_index_type,
534 count1, gfc_index_one_node);
535 gfc_add_modify (&loopse.pre, count1, tmp);
536 }
537 else
538 gfc_add_expr_to_block (&loopse.pre, loopse.expr);
539
540 gfc_add_block_to_block (&block, &loopse.pre);
541 gfc_add_block_to_block (&block, &loopse.post);
542
543 /* Finish up the loop block and the loop. */
544 gfc_add_expr_to_block (&body, gfc_finish_block (&block));
545 gfc_trans_scalarizing_loops (&loop, &body);
546 gfc_add_block_to_block (&se.pre, &loop.pre);
547 gfc_add_block_to_block (&se.pre, &loop.post);
548 gfc_add_block_to_block (&se.pre, &loopse.finalblock);
549 gfc_add_block_to_block (&se.pre, &se.post);
550 gfc_cleanup_loop (&loop);
551 }
552
553 return gfc_finish_block (&se.pre);
554 }
555
556
557 /* Translate the RETURN statement. */
558
559 tree
560 gfc_trans_return (gfc_code * code)
561 {
562 if (code->expr1)
563 {
564 gfc_se se;
565 tree tmp;
566 tree result;
567
568 /* If code->expr is not NULL, this return statement must appear
569 in a subroutine and current_fake_result_decl has already
570 been generated. */
571
572 result = gfc_get_fake_result_decl (NULL, 0);
573 if (!result)
574 {
575 gfc_warning (0,
576 "An alternate return at %L without a * dummy argument",
577 &code->expr1->where);
578 return gfc_generate_return ();
579 }
580
581 /* Start a new block for this statement. */
582 gfc_init_se (&se, NULL);
583 gfc_start_block (&se.pre);
584
585 gfc_conv_expr (&se, code->expr1);
586
587 /* Note that the actually returned expression is a simple value and
588 does not depend on any pointers or such; thus we can clean-up with
589 se.post before returning. */
590 tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (result),
591 result, fold_convert (TREE_TYPE (result),
592 se.expr));
593 gfc_add_expr_to_block (&se.pre, tmp);
594 gfc_add_block_to_block (&se.pre, &se.post);
595
596 tmp = gfc_generate_return ();
597 gfc_add_expr_to_block (&se.pre, tmp);
598 return gfc_finish_block (&se.pre);
599 }
600
601 return gfc_generate_return ();
602 }
603
604
605 /* Translate the PAUSE statement. We have to translate this statement
606 to a runtime library call. */
607
608 tree
609 gfc_trans_pause (gfc_code * code)
610 {
611 tree gfc_int8_type_node = gfc_get_int_type (8);
612 gfc_se se;
613 tree tmp;
614
615 /* Start a new block for this statement. */
616 gfc_init_se (&se, NULL);
617 gfc_start_block (&se.pre);
618
619
620 if (code->expr1 == NULL)
621 {
622 tmp = build_int_cst (size_type_node, 0);
623 tmp = build_call_expr_loc (input_location,
624 gfor_fndecl_pause_string, 2,
625 build_int_cst (pchar_type_node, 0), tmp);
626 }
627 else if (code->expr1->ts.type == BT_INTEGER)
628 {
629 gfc_conv_expr (&se, code->expr1);
630 tmp = build_call_expr_loc (input_location,
631 gfor_fndecl_pause_numeric, 1,
632 fold_convert (gfc_int8_type_node, se.expr));
633 }
634 else
635 {
636 gfc_conv_expr_reference (&se, code->expr1);
637 tmp = build_call_expr_loc (input_location,
638 gfor_fndecl_pause_string, 2,
639 se.expr, fold_convert (size_type_node,
640 se.string_length));
641 }
642
643 gfc_add_expr_to_block (&se.pre, tmp);
644
645 gfc_add_block_to_block (&se.pre, &se.post);
646
647 return gfc_finish_block (&se.pre);
648 }
649
650
651 /* Translate the STOP statement. We have to translate this statement
652 to a runtime library call. */
653
654 tree
655 gfc_trans_stop (gfc_code *code, bool error_stop)
656 {
657 gfc_se se;
658 tree tmp;
659 tree quiet;
660
661 /* Start a new block for this statement. */
662 gfc_init_se (&se, NULL);
663 gfc_start_block (&se.pre);
664
665 if (code->expr2)
666 {
667 gfc_conv_expr_val (&se, code->expr2);
668 quiet = fold_convert (boolean_type_node, se.expr);
669 }
670 else
671 quiet = boolean_false_node;
672
673 if (code->expr1 == NULL)
674 {
675 tmp = build_int_cst (size_type_node, 0);
676 tmp = build_call_expr_loc (input_location,
677 error_stop
678 ? (flag_coarray == GFC_FCOARRAY_LIB
679 ? gfor_fndecl_caf_error_stop_str
680 : gfor_fndecl_error_stop_string)
681 : (flag_coarray == GFC_FCOARRAY_LIB
682 ? gfor_fndecl_caf_stop_str
683 : gfor_fndecl_stop_string),
684 3, build_int_cst (pchar_type_node, 0), tmp,
685 quiet);
686 }
687 else if (code->expr1->ts.type == BT_INTEGER)
688 {
689 gfc_conv_expr (&se, code->expr1);
690 tmp = build_call_expr_loc (input_location,
691 error_stop
692 ? (flag_coarray == GFC_FCOARRAY_LIB
693 ? gfor_fndecl_caf_error_stop
694 : gfor_fndecl_error_stop_numeric)
695 : (flag_coarray == GFC_FCOARRAY_LIB
696 ? gfor_fndecl_caf_stop_numeric
697 : gfor_fndecl_stop_numeric), 2,
698 fold_convert (integer_type_node, se.expr),
699 quiet);
700 }
701 else
702 {
703 gfc_conv_expr_reference (&se, code->expr1);
704 tmp = build_call_expr_loc (input_location,
705 error_stop
706 ? (flag_coarray == GFC_FCOARRAY_LIB
707 ? gfor_fndecl_caf_error_stop_str
708 : gfor_fndecl_error_stop_string)
709 : (flag_coarray == GFC_FCOARRAY_LIB
710 ? gfor_fndecl_caf_stop_str
711 : gfor_fndecl_stop_string),
712 3, se.expr, fold_convert (size_type_node,
713 se.string_length),
714 quiet);
715 }
716
717 gfc_add_expr_to_block (&se.pre, tmp);
718
719 gfc_add_block_to_block (&se.pre, &se.post);
720
721 return gfc_finish_block (&se.pre);
722 }
723
724 /* Translate the FAIL IMAGE statement. */
725
726 tree
727 gfc_trans_fail_image (gfc_code *code ATTRIBUTE_UNUSED)
728 {
729 if (flag_coarray == GFC_FCOARRAY_LIB)
730 return build_call_expr_loc (input_location,
731 gfor_fndecl_caf_fail_image, 0);
732 else
733 {
734 const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
735 gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
736 tree tmp = gfc_get_symbol_decl (exsym);
737 return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
738 }
739 }
740
741 /* Translate the FORM TEAM statement. */
742
743 tree
744 gfc_trans_form_team (gfc_code *code)
745 {
746 if (flag_coarray == GFC_FCOARRAY_LIB)
747 {
748 gfc_se se;
749 gfc_se argse1, argse2;
750 tree team_id, team_type, tmp;
751
752 gfc_init_se (&se, NULL);
753 gfc_init_se (&argse1, NULL);
754 gfc_init_se (&argse2, NULL);
755 gfc_start_block (&se.pre);
756
757 gfc_conv_expr_val (&argse1, code->expr1);
758 gfc_conv_expr_val (&argse2, code->expr2);
759 team_id = fold_convert (integer_type_node, argse1.expr);
760 team_type = gfc_build_addr_expr (ppvoid_type_node, argse2.expr);
761
762 gfc_add_block_to_block (&se.pre, &argse1.pre);
763 gfc_add_block_to_block (&se.pre, &argse2.pre);
764 tmp = build_call_expr_loc (input_location,
765 gfor_fndecl_caf_form_team, 3,
766 team_id, team_type,
767 integer_zero_node);
768 gfc_add_expr_to_block (&se.pre, tmp);
769 gfc_add_block_to_block (&se.pre, &argse1.post);
770 gfc_add_block_to_block (&se.pre, &argse2.post);
771 return gfc_finish_block (&se.pre);
772 }
773 else
774 {
775 const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
776 gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
777 tree tmp = gfc_get_symbol_decl (exsym);
778 return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
779 }
780 }
781
782 /* Translate the CHANGE TEAM statement. */
783
784 tree
785 gfc_trans_change_team (gfc_code *code)
786 {
787 if (flag_coarray == GFC_FCOARRAY_LIB)
788 {
789 gfc_se argse;
790 tree team_type, tmp;
791
792 gfc_init_se (&argse, NULL);
793 gfc_conv_expr_val (&argse, code->expr1);
794 team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
795
796 tmp = build_call_expr_loc (input_location,
797 gfor_fndecl_caf_change_team, 2, team_type,
798 integer_zero_node);
799 gfc_add_expr_to_block (&argse.pre, tmp);
800 gfc_add_block_to_block (&argse.pre, &argse.post);
801 return gfc_finish_block (&argse.pre);
802 }
803 else
804 {
805 const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
806 gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
807 tree tmp = gfc_get_symbol_decl (exsym);
808 return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
809 }
810 }
811
812 /* Translate the END TEAM statement. */
813
814 tree
815 gfc_trans_end_team (gfc_code *code ATTRIBUTE_UNUSED)
816 {
817 if (flag_coarray == GFC_FCOARRAY_LIB)
818 {
819 return build_call_expr_loc (input_location,
820 gfor_fndecl_caf_end_team, 1,
821 build_int_cst (pchar_type_node, 0));
822 }
823 else
824 {
825 const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
826 gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
827 tree tmp = gfc_get_symbol_decl (exsym);
828 return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
829 }
830 }
831
832 /* Translate the SYNC TEAM statement. */
833
834 tree
835 gfc_trans_sync_team (gfc_code *code)
836 {
837 if (flag_coarray == GFC_FCOARRAY_LIB)
838 {
839 gfc_se argse;
840 tree team_type, tmp;
841
842 gfc_init_se (&argse, NULL);
843 gfc_conv_expr_val (&argse, code->expr1);
844 team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
845
846 tmp = build_call_expr_loc (input_location,
847 gfor_fndecl_caf_sync_team, 2,
848 team_type,
849 integer_zero_node);
850 gfc_add_expr_to_block (&argse.pre, tmp);
851 gfc_add_block_to_block (&argse.pre, &argse.post);
852 return gfc_finish_block (&argse.pre);
853 }
854 else
855 {
856 const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
857 gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
858 tree tmp = gfc_get_symbol_decl (exsym);
859 return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
860 }
861 }
862
863 tree
864 gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
865 {
866 gfc_se se, argse;
867 tree stat = NULL_TREE, stat2 = NULL_TREE;
868 tree lock_acquired = NULL_TREE, lock_acquired2 = NULL_TREE;
869
870 /* Short cut: For single images without STAT= or LOCK_ACQUIRED
871 return early. (ERRMSG= is always untouched for -fcoarray=single.) */
872 if (!code->expr2 && !code->expr4 && flag_coarray != GFC_FCOARRAY_LIB)
873 return NULL_TREE;
874
875 if (code->expr2)
876 {
877 gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
878 gfc_init_se (&argse, NULL);
879 gfc_conv_expr_val (&argse, code->expr2);
880 stat = argse.expr;
881 }
882 else if (flag_coarray == GFC_FCOARRAY_LIB)
883 stat = null_pointer_node;
884
885 if (code->expr4)
886 {
887 gcc_assert (code->expr4->expr_type == EXPR_VARIABLE);
888 gfc_init_se (&argse, NULL);
889 gfc_conv_expr_val (&argse, code->expr4);
890 lock_acquired = argse.expr;
891 }
892 else if (flag_coarray == GFC_FCOARRAY_LIB)
893 lock_acquired = null_pointer_node;
894
895 gfc_start_block (&se.pre);
896 if (flag_coarray == GFC_FCOARRAY_LIB)
897 {
898 tree tmp, token, image_index, errmsg, errmsg_len;
899 tree index = build_zero_cst (gfc_array_index_type);
900 tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
901
902 if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
903 || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
904 != INTMOD_ISO_FORTRAN_ENV
905 || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
906 != ISOFORTRAN_LOCK_TYPE)
907 {
908 gfc_error ("Sorry, the lock component of derived type at %L is not "
909 "yet supported", &code->expr1->where);
910 return NULL_TREE;
911 }
912
913 gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
914 code->expr1);
915
916 if (gfc_is_coindexed (code->expr1))
917 image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
918 else
919 image_index = integer_zero_node;
920
921 /* For arrays, obtain the array index. */
922 if (gfc_expr_attr (code->expr1).dimension)
923 {
924 tree desc, tmp, extent, lbound, ubound;
925 gfc_array_ref *ar, ar2;
926 int i, rank;
927
928 /* TODO: Extend this, once DT components are supported. */
929 ar = &code->expr1->ref->u.ar;
930 ar2 = *ar;
931 memset (ar, '\0', sizeof (*ar));
932 ar->as = ar2.as;
933 ar->type = AR_FULL;
934 rank = code->expr1->rank;
935 code->expr1->rank = ar->as->rank;
936
937 gfc_init_se (&argse, NULL);
938 argse.descriptor_only = 1;
939 gfc_conv_expr_descriptor (&argse, code->expr1);
940 gfc_add_block_to_block (&se.pre, &argse.pre);
941 desc = argse.expr;
942 *ar = ar2;
943 code->expr1->rank = rank;
944
945 extent = build_one_cst (gfc_array_index_type);
946 for (i = 0; i < ar->dimen; i++)
947 {
948 gfc_init_se (&argse, NULL);
949 gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
950 gfc_add_block_to_block (&argse.pre, &argse.pre);
951 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
952 tmp = fold_build2_loc (input_location, MINUS_EXPR,
953 TREE_TYPE (lbound), argse.expr, lbound);
954 tmp = fold_build2_loc (input_location, MULT_EXPR,
955 TREE_TYPE (tmp), extent, tmp);
956 index = fold_build2_loc (input_location, PLUS_EXPR,
957 TREE_TYPE (tmp), index, tmp);
958 if (i < ar->dimen - 1)
959 {
960 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
961 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
962 extent = fold_build2_loc (input_location, MULT_EXPR,
963 TREE_TYPE (tmp), extent, tmp);
964 }
965 }
966 }
967
968 /* errmsg. */
969 if (code->expr3)
970 {
971 gfc_init_se (&argse, NULL);
972 argse.want_pointer = 1;
973 gfc_conv_expr (&argse, code->expr3);
974 gfc_add_block_to_block (&se.pre, &argse.pre);
975 errmsg = argse.expr;
976 errmsg_len = fold_convert (size_type_node, argse.string_length);
977 }
978 else
979 {
980 errmsg = null_pointer_node;
981 errmsg_len = build_zero_cst (size_type_node);
982 }
983
984 if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
985 {
986 stat2 = stat;
987 stat = gfc_create_var (integer_type_node, "stat");
988 }
989
990 if (lock_acquired != null_pointer_node
991 && TREE_TYPE (lock_acquired) != integer_type_node)
992 {
993 lock_acquired2 = lock_acquired;
994 lock_acquired = gfc_create_var (integer_type_node, "acquired");
995 }
996
997 index = fold_convert (size_type_node, index);
998 if (op == EXEC_LOCK)
999 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
1000 token, index, image_index,
1001 lock_acquired != null_pointer_node
1002 ? gfc_build_addr_expr (NULL, lock_acquired)
1003 : lock_acquired,
1004 stat != null_pointer_node
1005 ? gfc_build_addr_expr (NULL, stat) : stat,
1006 errmsg, errmsg_len);
1007 else
1008 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
1009 token, index, image_index,
1010 stat != null_pointer_node
1011 ? gfc_build_addr_expr (NULL, stat) : stat,
1012 errmsg, errmsg_len);
1013 gfc_add_expr_to_block (&se.pre, tmp);
1014
1015 /* It guarantees memory consistency within the same segment */
1016 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1017 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1018 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1019 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1020 ASM_VOLATILE_P (tmp) = 1;
1021
1022 gfc_add_expr_to_block (&se.pre, tmp);
1023
1024 if (stat2 != NULL_TREE)
1025 gfc_add_modify (&se.pre, stat2,
1026 fold_convert (TREE_TYPE (stat2), stat));
1027
1028 if (lock_acquired2 != NULL_TREE)
1029 gfc_add_modify (&se.pre, lock_acquired2,
1030 fold_convert (TREE_TYPE (lock_acquired2),
1031 lock_acquired));
1032
1033 return gfc_finish_block (&se.pre);
1034 }
1035
1036 if (stat != NULL_TREE)
1037 gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1038
1039 if (lock_acquired != NULL_TREE)
1040 gfc_add_modify (&se.pre, lock_acquired,
1041 fold_convert (TREE_TYPE (lock_acquired),
1042 boolean_true_node));
1043
1044 return gfc_finish_block (&se.pre);
1045 }
1046
1047 tree
1048 gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
1049 {
1050 gfc_se se, argse;
1051 tree stat = NULL_TREE, stat2 = NULL_TREE;
1052 tree until_count = NULL_TREE;
1053
1054 if (code->expr2)
1055 {
1056 gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
1057 gfc_init_se (&argse, NULL);
1058 gfc_conv_expr_val (&argse, code->expr2);
1059 stat = argse.expr;
1060 }
1061 else if (flag_coarray == GFC_FCOARRAY_LIB)
1062 stat = null_pointer_node;
1063
1064 if (code->expr4)
1065 {
1066 gfc_init_se (&argse, NULL);
1067 gfc_conv_expr_val (&argse, code->expr4);
1068 until_count = fold_convert (integer_type_node, argse.expr);
1069 }
1070 else
1071 until_count = integer_one_node;
1072
1073 if (flag_coarray != GFC_FCOARRAY_LIB)
1074 {
1075 gfc_start_block (&se.pre);
1076 gfc_init_se (&argse, NULL);
1077 gfc_conv_expr_val (&argse, code->expr1);
1078
1079 if (op == EXEC_EVENT_POST)
1080 gfc_add_modify (&se.pre, argse.expr,
1081 fold_build2_loc (input_location, PLUS_EXPR,
1082 TREE_TYPE (argse.expr), argse.expr,
1083 build_int_cst (TREE_TYPE (argse.expr), 1)));
1084 else
1085 gfc_add_modify (&se.pre, argse.expr,
1086 fold_build2_loc (input_location, MINUS_EXPR,
1087 TREE_TYPE (argse.expr), argse.expr,
1088 fold_convert (TREE_TYPE (argse.expr),
1089 until_count)));
1090 if (stat != NULL_TREE)
1091 gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1092
1093 return gfc_finish_block (&se.pre);
1094 }
1095
1096 gfc_start_block (&se.pre);
1097 tree tmp, token, image_index, errmsg, errmsg_len;
1098 tree index = build_zero_cst (gfc_array_index_type);
1099 tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
1100
1101 if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
1102 || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
1103 != INTMOD_ISO_FORTRAN_ENV
1104 || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
1105 != ISOFORTRAN_EVENT_TYPE)
1106 {
1107 gfc_error ("Sorry, the event component of derived type at %L is not "
1108 "yet supported", &code->expr1->where);
1109 return NULL_TREE;
1110 }
1111
1112 gfc_init_se (&argse, NULL);
1113 gfc_get_caf_token_offset (&argse, &token, NULL, caf_decl, NULL_TREE,
1114 code->expr1);
1115 gfc_add_block_to_block (&se.pre, &argse.pre);
1116
1117 if (gfc_is_coindexed (code->expr1))
1118 image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
1119 else
1120 image_index = integer_zero_node;
1121
1122 /* For arrays, obtain the array index. */
1123 if (gfc_expr_attr (code->expr1).dimension)
1124 {
1125 tree desc, tmp, extent, lbound, ubound;
1126 gfc_array_ref *ar, ar2;
1127 int i;
1128
1129 /* TODO: Extend this, once DT components are supported. */
1130 ar = &code->expr1->ref->u.ar;
1131 ar2 = *ar;
1132 memset (ar, '\0', sizeof (*ar));
1133 ar->as = ar2.as;
1134 ar->type = AR_FULL;
1135
1136 gfc_init_se (&argse, NULL);
1137 argse.descriptor_only = 1;
1138 gfc_conv_expr_descriptor (&argse, code->expr1);
1139 gfc_add_block_to_block (&se.pre, &argse.pre);
1140 desc = argse.expr;
1141 *ar = ar2;
1142
1143 extent = build_one_cst (gfc_array_index_type);
1144 for (i = 0; i < ar->dimen; i++)
1145 {
1146 gfc_init_se (&argse, NULL);
1147 gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
1148 gfc_add_block_to_block (&argse.pre, &argse.pre);
1149 lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1150 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1151 TREE_TYPE (lbound), argse.expr, lbound);
1152 tmp = fold_build2_loc (input_location, MULT_EXPR,
1153 TREE_TYPE (tmp), extent, tmp);
1154 index = fold_build2_loc (input_location, PLUS_EXPR,
1155 TREE_TYPE (tmp), index, tmp);
1156 if (i < ar->dimen - 1)
1157 {
1158 ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1159 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1160 extent = fold_build2_loc (input_location, MULT_EXPR,
1161 TREE_TYPE (tmp), extent, tmp);
1162 }
1163 }
1164 }
1165
1166 /* errmsg. */
1167 if (code->expr3)
1168 {
1169 gfc_init_se (&argse, NULL);
1170 argse.want_pointer = 1;
1171 gfc_conv_expr (&argse, code->expr3);
1172 gfc_add_block_to_block (&se.pre, &argse.pre);
1173 errmsg = argse.expr;
1174 errmsg_len = fold_convert (size_type_node, argse.string_length);
1175 }
1176 else
1177 {
1178 errmsg = null_pointer_node;
1179 errmsg_len = build_zero_cst (size_type_node);
1180 }
1181
1182 if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
1183 {
1184 stat2 = stat;
1185 stat = gfc_create_var (integer_type_node, "stat");
1186 }
1187
1188 index = fold_convert (size_type_node, index);
1189 if (op == EXEC_EVENT_POST)
1190 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_post, 6,
1191 token, index, image_index,
1192 stat != null_pointer_node
1193 ? gfc_build_addr_expr (NULL, stat) : stat,
1194 errmsg, errmsg_len);
1195 else
1196 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_wait, 6,
1197 token, index, until_count,
1198 stat != null_pointer_node
1199 ? gfc_build_addr_expr (NULL, stat) : stat,
1200 errmsg, errmsg_len);
1201 gfc_add_expr_to_block (&se.pre, tmp);
1202
1203 /* It guarantees memory consistency within the same segment */
1204 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1205 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1206 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1207 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1208 ASM_VOLATILE_P (tmp) = 1;
1209 gfc_add_expr_to_block (&se.pre, tmp);
1210
1211 if (stat2 != NULL_TREE)
1212 gfc_add_modify (&se.pre, stat2, fold_convert (TREE_TYPE (stat2), stat));
1213
1214 return gfc_finish_block (&se.pre);
1215 }
1216
1217 tree
1218 gfc_trans_sync (gfc_code *code, gfc_exec_op type)
1219 {
1220 gfc_se se, argse;
1221 tree tmp;
1222 tree images = NULL_TREE, stat = NULL_TREE,
1223 errmsg = NULL_TREE, errmsglen = NULL_TREE;
1224
1225 /* Short cut: For single images without bound checking or without STAT=,
1226 return early. (ERRMSG= is always untouched for -fcoarray=single.) */
1227 if (!code->expr2 && !(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1228 && flag_coarray != GFC_FCOARRAY_LIB)
1229 return NULL_TREE;
1230
1231 gfc_init_se (&se, NULL);
1232 gfc_start_block (&se.pre);
1233
1234 if (code->expr1 && code->expr1->rank == 0)
1235 {
1236 gfc_init_se (&argse, NULL);
1237 gfc_conv_expr_val (&argse, code->expr1);
1238 images = argse.expr;
1239 }
1240
1241 if (code->expr2)
1242 {
1243 gcc_assert (code->expr2->expr_type == EXPR_VARIABLE
1244 || code->expr2->expr_type == EXPR_FUNCTION);
1245 gfc_init_se (&argse, NULL);
1246 gfc_conv_expr_val (&argse, code->expr2);
1247 stat = argse.expr;
1248 }
1249 else
1250 stat = null_pointer_node;
1251
1252 if (code->expr3 && flag_coarray == GFC_FCOARRAY_LIB)
1253 {
1254 gcc_assert (code->expr3->expr_type == EXPR_VARIABLE
1255 || code->expr3->expr_type == EXPR_FUNCTION);
1256 gfc_init_se (&argse, NULL);
1257 argse.want_pointer = 1;
1258 gfc_conv_expr (&argse, code->expr3);
1259 gfc_conv_string_parameter (&argse);
1260 errmsg = gfc_build_addr_expr (NULL, argse.expr);
1261 errmsglen = fold_convert (size_type_node, argse.string_length);
1262 }
1263 else if (flag_coarray == GFC_FCOARRAY_LIB)
1264 {
1265 errmsg = null_pointer_node;
1266 errmsglen = build_int_cst (size_type_node, 0);
1267 }
1268
1269 /* Check SYNC IMAGES(imageset) for valid image index.
1270 FIXME: Add a check for image-set arrays. */
1271 if (code->expr1 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1272 && code->expr1->rank == 0)
1273 {
1274 tree images2 = fold_convert (integer_type_node, images);
1275 tree cond;
1276 if (flag_coarray != GFC_FCOARRAY_LIB)
1277 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1278 images, build_int_cst (TREE_TYPE (images), 1));
1279 else
1280 {
1281 tree cond2;
1282 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
1283 2, integer_zero_node,
1284 build_int_cst (integer_type_node, -1));
1285 cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1286 images2, tmp);
1287 cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1288 images,
1289 build_int_cst (TREE_TYPE (images), 1));
1290 cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1291 logical_type_node, cond, cond2);
1292 }
1293 gfc_trans_runtime_check (true, false, cond, &se.pre,
1294 &code->expr1->where, "Invalid image number "
1295 "%d in SYNC IMAGES", images2);
1296 }
1297
1298 /* Per F2008, 8.5.1, a SYNC MEMORY is implied by calling the
1299 image control statements SYNC IMAGES and SYNC ALL. */
1300 if (flag_coarray == GFC_FCOARRAY_LIB)
1301 {
1302 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1303 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1304 gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
1305 tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
1306 ASM_VOLATILE_P (tmp) = 1;
1307 gfc_add_expr_to_block (&se.pre, tmp);
1308 }
1309
1310 if (flag_coarray != GFC_FCOARRAY_LIB)
1311 {
1312 /* Set STAT to zero. */
1313 if (code->expr2)
1314 gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1315 }
1316 else if (type == EXEC_SYNC_ALL || type == EXEC_SYNC_MEMORY)
1317 {
1318 /* SYNC ALL => stat == null_pointer_node
1319 SYNC ALL(stat=s) => stat has an integer type
1320
1321 If "stat" has the wrong integer type, use a temp variable of
1322 the right type and later cast the result back into "stat". */
1323 if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1324 {
1325 if (TREE_TYPE (stat) == integer_type_node)
1326 stat = gfc_build_addr_expr (NULL, stat);
1327
1328 if(type == EXEC_SYNC_MEMORY)
1329 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_memory,
1330 3, stat, errmsg, errmsglen);
1331 else
1332 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
1333 3, stat, errmsg, errmsglen);
1334
1335 gfc_add_expr_to_block (&se.pre, tmp);
1336 }
1337 else
1338 {
1339 tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1340
1341 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
1342 3, gfc_build_addr_expr (NULL, tmp_stat),
1343 errmsg, errmsglen);
1344 gfc_add_expr_to_block (&se.pre, tmp);
1345
1346 gfc_add_modify (&se.pre, stat,
1347 fold_convert (TREE_TYPE (stat), tmp_stat));
1348 }
1349 }
1350 else
1351 {
1352 tree len;
1353
1354 gcc_assert (type == EXEC_SYNC_IMAGES);
1355
1356 if (!code->expr1)
1357 {
1358 len = build_int_cst (integer_type_node, -1);
1359 images = null_pointer_node;
1360 }
1361 else if (code->expr1->rank == 0)
1362 {
1363 len = integer_one_node;
1364 images = gfc_build_addr_expr (NULL_TREE, images);
1365 }
1366 else
1367 {
1368 /* FIXME. */
1369 if (code->expr1->ts.kind != gfc_c_int_kind)
1370 gfc_fatal_error ("Sorry, only support for integer kind %d "
1371 "implemented for image-set at %L",
1372 gfc_c_int_kind, &code->expr1->where);
1373
1374 gfc_conv_array_parameter (&se, code->expr1, true, NULL, NULL, &len);
1375 images = se.expr;
1376
1377 tmp = gfc_typenode_for_spec (&code->expr1->ts);
1378 if (GFC_ARRAY_TYPE_P (tmp) || GFC_DESCRIPTOR_TYPE_P (tmp))
1379 tmp = gfc_get_element_type (tmp);
1380
1381 len = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
1382 TREE_TYPE (len), len,
1383 fold_convert (TREE_TYPE (len),
1384 TYPE_SIZE_UNIT (tmp)));
1385 len = fold_convert (integer_type_node, len);
1386 }
1387
1388 /* SYNC IMAGES(imgs) => stat == null_pointer_node
1389 SYNC IMAGES(imgs,stat=s) => stat has an integer type
1390
1391 If "stat" has the wrong integer type, use a temp variable of
1392 the right type and later cast the result back into "stat". */
1393 if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1394 {
1395 if (TREE_TYPE (stat) == integer_type_node)
1396 stat = gfc_build_addr_expr (NULL, stat);
1397
1398 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
1399 5, fold_convert (integer_type_node, len),
1400 images, stat, errmsg, errmsglen);
1401 gfc_add_expr_to_block (&se.pre, tmp);
1402 }
1403 else
1404 {
1405 tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1406
1407 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_images,
1408 5, fold_convert (integer_type_node, len),
1409 images, gfc_build_addr_expr (NULL, tmp_stat),
1410 errmsg, errmsglen);
1411 gfc_add_expr_to_block (&se.pre, tmp);
1412
1413 gfc_add_modify (&se.pre, stat,
1414 fold_convert (TREE_TYPE (stat), tmp_stat));
1415 }
1416 }
1417
1418 return gfc_finish_block (&se.pre);
1419 }
1420
1421
1422 /* Generate GENERIC for the IF construct. This function also deals with
1423 the simple IF statement, because the front end translates the IF
1424 statement into an IF construct.
1425
1426 We translate:
1427
1428 IF (cond) THEN
1429 then_clause
1430 ELSEIF (cond2)
1431 elseif_clause
1432 ELSE
1433 else_clause
1434 ENDIF
1435
1436 into:
1437
1438 pre_cond_s;
1439 if (cond_s)
1440 {
1441 then_clause;
1442 }
1443 else
1444 {
1445 pre_cond_s
1446 if (cond_s)
1447 {
1448 elseif_clause
1449 }
1450 else
1451 {
1452 else_clause;
1453 }
1454 }
1455
1456 where COND_S is the simplified version of the predicate. PRE_COND_S
1457 are the pre side-effects produced by the translation of the
1458 conditional.
1459 We need to build the chain recursively otherwise we run into
1460 problems with folding incomplete statements. */
1461
1462 static tree
1463 gfc_trans_if_1 (gfc_code * code)
1464 {
1465 gfc_se if_se;
1466 tree stmt, elsestmt;
1467 location_t loc, saved_loc = UNKNOWN_LOCATION;
1468
1469 /* Check for an unconditional ELSE clause. */
1470 if (!code->expr1)
1471 return gfc_trans_code (code->next);
1472
1473 /* Initialize a statement builder for each block. Puts in NULL_TREEs. */
1474 gfc_init_se (&if_se, NULL);
1475 gfc_start_block (&if_se.pre);
1476
1477 /* Calculate the IF condition expression. */
1478 if (GFC_LOCUS_IS_SET (code->expr1->where))
1479 {
1480 saved_loc = input_location;
1481 input_location = gfc_get_location (&code->expr1->where);
1482 }
1483
1484 gfc_conv_expr_val (&if_se, code->expr1);
1485
1486 if (saved_loc != UNKNOWN_LOCATION)
1487 input_location = saved_loc;
1488
1489 /* Translate the THEN clause. */
1490 stmt = gfc_trans_code (code->next);
1491
1492 /* Translate the ELSE clause. */
1493 if (code->block)
1494 elsestmt = gfc_trans_if_1 (code->block);
1495 else
1496 elsestmt = build_empty_stmt (input_location);
1497
1498 /* Build the condition expression and add it to the condition block. */
1499 loc = (GFC_LOCUS_IS_SET (code->expr1->where)
1500 ? gfc_get_location (&code->expr1->where) : input_location);
1501 stmt = fold_build3_loc (loc, COND_EXPR, void_type_node, if_se.expr, stmt,
1502 elsestmt);
1503
1504 gfc_add_expr_to_block (&if_se.pre, stmt);
1505
1506 /* Finish off this statement. */
1507 return gfc_finish_block (&if_se.pre);
1508 }
1509
1510 tree
1511 gfc_trans_if (gfc_code * code)
1512 {
1513 stmtblock_t body;
1514 tree exit_label;
1515
1516 /* Create exit label so it is available for trans'ing the body code. */
1517 exit_label = gfc_build_label_decl (NULL_TREE);
1518 code->exit_label = exit_label;
1519
1520 /* Translate the actual code in code->block. */
1521 gfc_init_block (&body);
1522 gfc_add_expr_to_block (&body, gfc_trans_if_1 (code->block));
1523
1524 /* Add exit label. */
1525 gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
1526
1527 return gfc_finish_block (&body);
1528 }
1529
1530
1531 /* Translate an arithmetic IF expression.
1532
1533 IF (cond) label1, label2, label3 translates to
1534
1535 if (cond <= 0)
1536 {
1537 if (cond < 0)
1538 goto label1;
1539 else // cond == 0
1540 goto label2;
1541 }
1542 else // cond > 0
1543 goto label3;
1544
1545 An optimized version can be generated in case of equal labels.
1546 E.g., if label1 is equal to label2, we can translate it to
1547
1548 if (cond <= 0)
1549 goto label1;
1550 else
1551 goto label3;
1552 */
1553
1554 tree
1555 gfc_trans_arithmetic_if (gfc_code * code)
1556 {
1557 gfc_se se;
1558 tree tmp;
1559 tree branch1;
1560 tree branch2;
1561 tree zero;
1562
1563 /* Start a new block. */
1564 gfc_init_se (&se, NULL);
1565 gfc_start_block (&se.pre);
1566
1567 /* Pre-evaluate COND. */
1568 gfc_conv_expr_val (&se, code->expr1);
1569 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1570
1571 /* Build something to compare with. */
1572 zero = gfc_build_const (TREE_TYPE (se.expr), integer_zero_node);
1573
1574 if (code->label1->value != code->label2->value)
1575 {
1576 /* If (cond < 0) take branch1 else take branch2.
1577 First build jumps to the COND .LT. 0 and the COND .EQ. 0 cases. */
1578 branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1579 branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label2));
1580
1581 if (code->label1->value != code->label3->value)
1582 tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1583 se.expr, zero);
1584 else
1585 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1586 se.expr, zero);
1587
1588 branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1589 tmp, branch1, branch2);
1590 }
1591 else
1592 branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1593
1594 if (code->label1->value != code->label3->value
1595 && code->label2->value != code->label3->value)
1596 {
1597 /* if (cond <= 0) take branch1 else take branch2. */
1598 branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label3));
1599 tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1600 se.expr, zero);
1601 branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1602 tmp, branch1, branch2);
1603 }
1604
1605 /* Append the COND_EXPR to the evaluation of COND, and return. */
1606 gfc_add_expr_to_block (&se.pre, branch1);
1607 return gfc_finish_block (&se.pre);
1608 }
1609
1610
1611 /* Translate a CRITICAL block. */
1612 tree
1613 gfc_trans_critical (gfc_code *code)
1614 {
1615 stmtblock_t block;
1616 tree tmp, token = NULL_TREE;
1617
1618 gfc_start_block (&block);
1619
1620 if (flag_coarray == GFC_FCOARRAY_LIB)
1621 {
1622 tree zero_size = build_zero_cst (size_type_node);
1623 token = gfc_get_symbol_decl (code->resolved_sym);
1624 token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (token));
1625 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
1626 token, zero_size, integer_one_node,
1627 null_pointer_node, null_pointer_node,
1628 null_pointer_node, zero_size);
1629 gfc_add_expr_to_block (&block, tmp);
1630
1631 /* It guarantees memory consistency within the same segment */
1632 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1633 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1634 gfc_build_string_const (1, ""),
1635 NULL_TREE, NULL_TREE,
1636 tree_cons (NULL_TREE, tmp, NULL_TREE),
1637 NULL_TREE);
1638 ASM_VOLATILE_P (tmp) = 1;
1639
1640 gfc_add_expr_to_block (&block, tmp);
1641 }
1642
1643 tmp = gfc_trans_code (code->block->next);
1644 gfc_add_expr_to_block (&block, tmp);
1645
1646 if (flag_coarray == GFC_FCOARRAY_LIB)
1647 {
1648 tree zero_size = build_zero_cst (size_type_node);
1649 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
1650 token, zero_size, integer_one_node,
1651 null_pointer_node, null_pointer_node,
1652 zero_size);
1653 gfc_add_expr_to_block (&block, tmp);
1654
1655 /* It guarantees memory consistency within the same segment */
1656 tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1657 tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
1658 gfc_build_string_const (1, ""),
1659 NULL_TREE, NULL_TREE,
1660 tree_cons (NULL_TREE, tmp, NULL_TREE),
1661 NULL_TREE);
1662 ASM_VOLATILE_P (tmp) = 1;
1663
1664 gfc_add_expr_to_block (&block, tmp);
1665 }
1666
1667 return gfc_finish_block (&block);
1668 }
1669
1670
1671 /* Return true, when the class has a _len component. */
1672
1673 static bool
1674 class_has_len_component (gfc_symbol *sym)
1675 {
1676 gfc_component *comp = sym->ts.u.derived->components;
1677 while (comp)
1678 {
1679 if (strcmp (comp->name, "_len") == 0)
1680 return true;
1681 comp = comp->next;
1682 }
1683 return false;
1684 }
1685
1686
1687 static void
1688 copy_descriptor (stmtblock_t *block, tree dst, tree src, int rank)
1689 {
1690 int n;
1691 tree dim;
1692 tree tmp;
1693 tree tmp2;
1694 tree size;
1695 tree offset;
1696
1697 offset = gfc_index_zero_node;
1698
1699 /* Use memcpy to copy the descriptor. The size is the minimum of
1700 the sizes of 'src' and 'dst'. This avoids a non-trivial conversion. */
1701 tmp = TYPE_SIZE_UNIT (TREE_TYPE (src));
1702 tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (dst));
1703 size = fold_build2_loc (input_location, MIN_EXPR,
1704 TREE_TYPE (tmp), tmp, tmp2);
1705 tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
1706 tmp = build_call_expr_loc (input_location, tmp, 3,
1707 gfc_build_addr_expr (NULL_TREE, dst),
1708 gfc_build_addr_expr (NULL_TREE, src),
1709 fold_convert (size_type_node, size));
1710 gfc_add_expr_to_block (block, tmp);
1711
1712 /* Set the offset correctly. */
1713 for (n = 0; n < rank; n++)
1714 {
1715 dim = gfc_rank_cst[n];
1716 tmp = gfc_conv_descriptor_lbound_get (src, dim);
1717 tmp2 = gfc_conv_descriptor_stride_get (src, dim);
1718 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
1719 tmp, tmp2);
1720 offset = fold_build2_loc (input_location, MINUS_EXPR,
1721 TREE_TYPE (offset), offset, tmp);
1722 offset = gfc_evaluate_now (offset, block);
1723 }
1724
1725 gfc_conv_descriptor_offset_set (block, dst, offset);
1726 }
1727
1728
1729 /* Do proper initialization for ASSOCIATE names. */
1730
1731 static void
1732 trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
1733 {
1734 gfc_expr *e;
1735 tree tmp;
1736 bool class_target;
1737 bool unlimited;
1738 tree desc;
1739 tree offset;
1740 tree dim;
1741 int n;
1742 tree charlen;
1743 bool need_len_assign;
1744 bool whole_array = true;
1745 bool same_class;
1746 gfc_ref *ref;
1747 gfc_symbol *sym2;
1748
1749 gcc_assert (sym->assoc);
1750 e = sym->assoc->target;
1751
1752 class_target = (e->expr_type == EXPR_VARIABLE)
1753 && e->ts.type == BT_CLASS
1754 && (gfc_is_class_scalar_expr (e)
1755 || gfc_is_class_array_ref (e, NULL));
1756 same_class = class_target && sym->ts.type == BT_CLASS
1757 && strcmp (sym->ts.u.derived->name, e->ts.u.derived->name) == 0;
1758
1759 unlimited = UNLIMITED_POLY (e);
1760
1761 for (ref = e->ref; ref; ref = ref->next)
1762 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
1763 && ref->u.ar.dimen != 0 && ref->next)
1764 {
1765 whole_array = false;
1766 break;
1767 }
1768
1769 /* Assignments to the string length need to be generated, when
1770 ( sym is a char array or
1771 sym has a _len component)
1772 and the associated expression is unlimited polymorphic, which is
1773 not (yet) correctly in 'unlimited', because for an already associated
1774 BT_DERIVED the u-poly flag is not set, i.e.,
1775 __tmp_CHARACTER_0_1 => w => arg
1776 ^ generated temp ^ from code, the w does not have the u-poly
1777 flag set, where UNLIMITED_POLY(e) expects it. */
1778 need_len_assign = ((unlimited || (e->ts.type == BT_DERIVED
1779 && e->ts.u.derived->attr.unlimited_polymorphic))
1780 && (sym->ts.type == BT_CHARACTER
1781 || ((sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED)
1782 && class_has_len_component (sym)))
1783 && !sym->attr.select_rank_temporary);
1784
1785 /* Do a `pointer assignment' with updated descriptor (or assign descriptor
1786 to array temporary) for arrays with either unknown shape or if associating
1787 to a variable. Select rank temporaries need somewhat different treatment
1788 to other associate names and case temporaries. This because the selector
1789 is assumed rank and so the offset in particular has to be changed. Also,
1790 the case temporaries carry both allocatable and target attributes if
1791 present in the selector. This means that an allocatation or change of
1792 association can occur and so has to be dealt with. */
1793 if (sym->attr.select_rank_temporary)
1794 {
1795 gfc_se se;
1796 tree class_decl = NULL_TREE;
1797 int rank = 0;
1798 bool class_ptr;
1799
1800 sym2 = e->symtree->n.sym;
1801 gfc_init_se (&se, NULL);
1802 if (e->ts.type == BT_CLASS)
1803 {
1804 /* Go straight to the class data. */
1805 if (sym2->attr.dummy && !sym2->attr.optional)
1806 {
1807 class_decl = sym2->backend_decl;
1808 if (DECL_LANG_SPECIFIC (class_decl)
1809 && GFC_DECL_SAVED_DESCRIPTOR (class_decl))
1810 class_decl = GFC_DECL_SAVED_DESCRIPTOR (class_decl);
1811 if (POINTER_TYPE_P (TREE_TYPE (class_decl)))
1812 class_decl = build_fold_indirect_ref_loc (input_location,
1813 class_decl);
1814 gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (class_decl)));
1815 se.expr = gfc_class_data_get (class_decl);
1816 }
1817 else
1818 {
1819 class_decl = sym2->backend_decl;
1820 gfc_conv_expr_descriptor (&se, e);
1821 if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1822 se.expr = build_fold_indirect_ref_loc (input_location,
1823 se.expr);
1824 }
1825
1826 if (CLASS_DATA (sym)->as && CLASS_DATA (sym)->as->rank > 0)
1827 rank = CLASS_DATA (sym)->as->rank;
1828 }
1829 else
1830 {
1831 gfc_conv_expr_descriptor (&se, e);
1832 if (sym->as && sym->as->rank > 0)
1833 rank = sym->as->rank;
1834 }
1835
1836 desc = sym->backend_decl;
1837
1838 /* The SELECT TYPE mechanisms turn class temporaries into pointers, which
1839 point to the selector. */
1840 class_ptr = class_decl != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (desc));
1841 if (class_ptr)
1842 {
1843 tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (desc)), "class");
1844 tmp = gfc_build_addr_expr (NULL, tmp);
1845 gfc_add_modify (&se.pre, desc, tmp);
1846
1847 tmp = gfc_class_vptr_get (class_decl);
1848 gfc_add_modify (&se.pre, gfc_class_vptr_get (desc), tmp);
1849 if (UNLIMITED_POLY (sym))
1850 gfc_add_modify (&se.pre, gfc_class_len_get (desc),
1851 gfc_class_len_get (class_decl));
1852
1853 desc = gfc_class_data_get (desc);
1854 }
1855
1856 /* SELECT RANK temporaries can carry the allocatable and pointer
1857 attributes so the selector descriptor must be copied in and
1858 copied out. */
1859 if (rank > 0)
1860 copy_descriptor (&se.pre, desc, se.expr, rank);
1861 else
1862 {
1863 tmp = gfc_conv_descriptor_data_get (se.expr);
1864 gfc_add_modify (&se.pre, desc,
1865 fold_convert (TREE_TYPE (desc), tmp));
1866 }
1867
1868 /* Deal with associate_name => selector. Class associate names are
1869 treated in the same way as in SELECT TYPE. */
1870 sym2 = sym->assoc->target->symtree->n.sym;
1871 if (sym2->assoc && sym->assoc->target && sym2->ts.type != BT_CLASS)
1872 {
1873 sym2 = sym2->assoc->target->symtree->n.sym;
1874 se.expr = sym2->backend_decl;
1875
1876 if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1877 se.expr = build_fold_indirect_ref_loc (input_location,
1878 se.expr);
1879 }
1880
1881 /* There could have been reallocation. Copy descriptor back to the
1882 selector and update the offset. */
1883 if (sym->attr.allocatable || sym->attr.pointer
1884 || (sym->ts.type == BT_CLASS
1885 && (CLASS_DATA (sym)->attr.allocatable
1886 || CLASS_DATA (sym)->attr.pointer)))
1887 {
1888 if (rank > 0)
1889 copy_descriptor (&se.post, se.expr, desc, rank);
1890 else
1891 gfc_conv_descriptor_data_set (&se.post, se.expr, desc);
1892
1893 /* The dynamic type could have changed too. */
1894 if (sym->ts.type == BT_CLASS)
1895 {
1896 tmp = sym->backend_decl;
1897 if (class_ptr)
1898 tmp = build_fold_indirect_ref_loc (input_location, tmp);
1899 gfc_add_modify (&se.post, gfc_class_vptr_get (class_decl),
1900 gfc_class_vptr_get (tmp));
1901 if (UNLIMITED_POLY (sym))
1902 gfc_add_modify (&se.post, gfc_class_len_get (class_decl),
1903 gfc_class_len_get (tmp));
1904 }
1905 }
1906
1907 tmp = gfc_finish_block (&se.post);
1908
1909 gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), tmp);
1910 }
1911
1912 /* Now all the other kinds of associate variable. */
1913
1914 /* First we do the F202y ASSOCIATE construct with an assumed rank selector.
1915 Since this requires rank remapping, the simplest implementation builds an
1916 array reference, using the array ref attached to the association_list,
1917 followed by gfc_trans_pointer_assignment. */
1918 else if (e->rank == -1 && sym->assoc->ar)
1919 {
1920 gfc_array_ref *ar;
1921 gfc_expr *expr1 = gfc_lval_expr_from_sym (sym);
1922 stmtblock_t init;
1923 gfc_init_block (&init);
1924
1925 /* Build the array reference and add to expr1. */
1926 gfc_free_ref_list (expr1->ref);
1927 expr1->ref = gfc_get_ref();
1928 expr1->ref->type = REF_ARRAY;
1929 ar = gfc_copy_array_ref (sym->assoc->ar);
1930 expr1->ref->u.ar = *ar;
1931 expr1->ref->u.ar.type = AR_SECTION;
1932
1933 /* For class objects, insert the _data component reference. Since the
1934 associate-name is a pointer, it needs a target, which is created using
1935 its typespec. If unlimited polymorphic, the _len field will be filled
1936 by the pointer assignment. */
1937 if (expr1->ts.type == BT_CLASS)
1938 {
1939 need_len_assign = false;
1940 gfc_ref *ref;
1941 gfc_find_component (expr1->ts.u.derived, "_data", true, true, &ref);
1942 ref->next = expr1->ref;
1943 expr1->ref = ref;
1944 expr1->rank = CLASS_DATA (sym)->as->rank;
1945 tmp = gfc_create_var (gfc_typenode_for_spec (&sym->ts), "class");
1946 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1947 gfc_add_modify (&init, sym->backend_decl, tmp);
1948 }
1949
1950 /* Do the pointer assignment and clean up. */
1951 gfc_expr *expr2 = gfc_copy_expr (e);
1952 gfc_add_expr_to_block (&init,
1953 gfc_trans_pointer_assignment (expr1, expr2));
1954 gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL);
1955 gfc_free_expr (expr1);
1956 gfc_free_expr (expr2);
1957 }
1958 else if ((sym->attr.dimension || sym->attr.codimension) && !class_target
1959 && (sym->as->type == AS_DEFERRED || sym->assoc->variable))
1960 {
1961 gfc_se se;
1962 tree desc;
1963 bool cst_array_ctor;
1964 stmtblock_t init;
1965 gfc_init_block (&init);
1966
1967 desc = sym->backend_decl;
1968 cst_array_ctor = e->expr_type == EXPR_ARRAY
1969 && gfc_constant_array_constructor_p (e->value.constructor)
1970 && e->ts.type != BT_CHARACTER;
1971
1972 /* If association is to an expression, evaluate it and create temporary.
1973 Otherwise, get descriptor of target for pointer assignment. */
1974 gfc_init_se (&se, NULL);
1975
1976 if (sym->assoc->variable || cst_array_ctor)
1977 {
1978 se.direct_byref = 1;
1979 se.use_offset = 1;
1980 se.expr = desc;
1981 GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
1982 }
1983
1984 if (sym->attr.codimension && !sym->attr.dimension)
1985 se.want_coarray = 1;
1986
1987 gfc_conv_expr_descriptor (&se, e);
1988
1989 if (sym->ts.type == BT_CHARACTER
1990 && !sym->attr.select_type_temporary
1991 && sym->ts.u.cl->backend_decl
1992 && VAR_P (sym->ts.u.cl->backend_decl)
1993 && se.string_length
1994 && se.string_length != sym->ts.u.cl->backend_decl)
1995 {
1996 /* When the target is a variable, its length is already known. */
1997 tree len = fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
1998 se.string_length);
1999 if (e->expr_type == EXPR_VARIABLE)
2000 gfc_add_modify (&init, sym->ts.u.cl->backend_decl, len);
2001 else
2002 gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl, len);
2003 }
2004
2005 /* If we didn't already do the pointer assignment, set associate-name
2006 descriptor to the one generated for the temporary. */
2007 if ((!sym->assoc->variable && !cst_array_ctor)
2008 || !whole_array)
2009 {
2010 int dim;
2011
2012 if (whole_array)
2013 gfc_add_modify (&se.pre, desc, se.expr);
2014
2015 /* The generated descriptor has lower bound zero (as array
2016 temporary), shift bounds so we get lower bounds of 1. */
2017 for (dim = 0; dim < e->rank; ++dim)
2018 gfc_conv_shift_descriptor_lbound (&se.pre, desc,
2019 dim, gfc_index_one_node);
2020 }
2021
2022 /* If this is a subreference array pointer associate name use the
2023 associate variable element size for the value of 'span'. */
2024 if (sym->attr.subref_array_pointer && !se.direct_byref)
2025 {
2026 gcc_assert (e->expr_type == EXPR_VARIABLE);
2027 tmp = gfc_get_array_span (se.expr, e);
2028
2029 gfc_conv_descriptor_span_set (&se.pre, desc, tmp);
2030 }
2031
2032 if (e->expr_type == EXPR_FUNCTION
2033 && sym->ts.type == BT_DERIVED
2034 && sym->ts.u.derived
2035 && sym->ts.u.derived->attr.pdt_type)
2036 {
2037 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, se.expr,
2038 sym->as->rank);
2039 gfc_add_expr_to_block (&se.post, tmp);
2040 }
2041
2042 /* Done, register stuff as init / cleanup code. */
2043 gfc_add_block_to_block (&init, &se.pre);
2044 gfc_add_init_cleanup (block, gfc_finish_block (&init),
2045 gfc_finish_block (&se.post));
2046 }
2047
2048 /* Temporaries, arising from TYPE IS, just need the descriptor of class
2049 arrays to be assigned directly. */
2050 else if (class_target && (sym->attr.dimension || sym->attr.codimension)
2051 && (sym->ts.type == BT_DERIVED || unlimited))
2052 {
2053 gfc_se se;
2054
2055 gfc_init_se (&se, NULL);
2056 se.descriptor_only = 1;
2057 /* In a select type the (temporary) associate variable shall point to
2058 a standard fortran array (lower bound == 1), but conv_expr ()
2059 just maps to the input array in the class object, whose lbound may
2060 be arbitrary. conv_expr_descriptor solves this by inserting a
2061 temporary array descriptor. */
2062 gfc_conv_expr_descriptor (&se, e);
2063
2064 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
2065 || GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)));
2066 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)));
2067
2068 if (sym->ts.type == BT_CHARACTER)
2069 {
2070 /* Emit a DECL_EXPR for the variable sized array type in so the
2071 gimplification of its type sizes works correctly. */
2072 tree arraytype;
2073 tmp = TREE_TYPE (sym->backend_decl);
2074 arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (tmp));
2075 if (! TYPE_NAME (arraytype))
2076 TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
2077 NULL_TREE, arraytype);
2078 gfc_add_expr_to_block (&se.pre, build1 (DECL_EXPR,
2079 arraytype, TYPE_NAME (arraytype)));
2080 }
2081
2082 if (GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)))
2083 {
2084 if (INDIRECT_REF_P (se.expr))
2085 tmp = TREE_OPERAND (se.expr, 0);
2086 else
2087 tmp = se.expr;
2088
2089 gfc_add_modify (&se.pre, sym->backend_decl,
2090 gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (tmp)));
2091 }
2092 else
2093 gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
2094
2095 if (unlimited)
2096 {
2097 /* Recover the dtype, which has been overwritten by the
2098 assignment from an unlimited polymorphic object. */
2099 tmp = gfc_conv_descriptor_dtype (sym->backend_decl);
2100 gfc_add_modify (&se.pre, tmp,
2101 gfc_get_dtype (TREE_TYPE (sym->backend_decl)));
2102 }
2103
2104 gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2105 gfc_finish_block (&se.post));
2106 }
2107
2108 /* Do a scalar pointer assignment; this is for scalar variable targets. */
2109 else if (gfc_is_associate_pointer (sym))
2110 {
2111 gfc_se se;
2112
2113 gcc_assert (!sym->attr.dimension && !sym->attr.codimension);
2114
2115 gfc_init_se (&se, NULL);
2116
2117 /* Class associate-names come this way because they are
2118 unconditionally associate pointers and the symbol is scalar. */
2119 if (sym->ts.type == BT_CLASS && e->expr_type == EXPR_FUNCTION)
2120 {
2121 gfc_conv_expr (&se, e);
2122 se.expr = gfc_evaluate_now (se.expr, &se.pre);
2123 /* Finalize the expression and free if it is allocatable. */
2124 gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (e), e->rank);
2125 gfc_add_block_to_block (&se.post, &se.finalblock);
2126 need_len_assign = false;
2127 }
2128 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.dimension)
2129 {
2130 tree target_expr;
2131 /* For a class array we need a descriptor for the selector. */
2132 gfc_conv_expr_descriptor (&se, e);
2133 /* Needed to get/set the _len component below. */
2134 target_expr = se.expr;
2135
2136 /* Obtain a temporary class container for the result. */
2137 gfc_conv_class_to_class (&se, e, sym->ts, false, true, false, false);
2138 se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
2139
2140 desc = gfc_class_data_get (se.expr);
2141
2142 /* Set the offset. */
2143 offset = gfc_index_zero_node;
2144 for (n = 0; n < e->rank; n++)
2145 {
2146 dim = gfc_rank_cst[n];
2147 tmp = fold_build2_loc (input_location, MULT_EXPR,
2148 gfc_array_index_type,
2149 gfc_conv_descriptor_stride_get (desc, dim),
2150 gfc_conv_descriptor_lbound_get (desc, dim));
2151 offset = fold_build2_loc (input_location, MINUS_EXPR,
2152 gfc_array_index_type,
2153 offset, tmp);
2154 }
2155 gfc_conv_descriptor_offset_set (&se.pre, desc, offset);
2156
2157 if (need_len_assign)
2158 {
2159 if (e->symtree
2160 && DECL_LANG_SPECIFIC (e->symtree->n.sym->backend_decl)
2161 && GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl)
2162 && TREE_CODE (target_expr) != COMPONENT_REF)
2163 /* Use the original class descriptor stored in the saved
2164 descriptor to get the target_expr. */
2165 target_expr =
2166 GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl);
2167 else
2168 /* Strip the _data component from the target_expr. */
2169 target_expr = TREE_OPERAND (target_expr, 0);
2170 /* Add a reference to the _len comp to the target expr. */
2171 tmp = gfc_class_len_get (target_expr);
2172 /* Get the component-ref for the temp structure's _len comp. */
2173 charlen = gfc_class_len_get (se.expr);
2174 /* Add the assign to the beginning of the block... */
2175 gfc_add_modify (&se.pre, charlen,
2176 fold_convert (TREE_TYPE (charlen), tmp));
2177 /* and the oposite way at the end of the block, to hand changes
2178 on the string length back. */
2179 gfc_add_modify (&se.post, tmp,
2180 fold_convert (TREE_TYPE (tmp), charlen));
2181 /* Length assignment done, prevent adding it again below. */
2182 need_len_assign = false;
2183 }
2184 }
2185 else if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS
2186 && CLASS_DATA (e)->attr.dimension)
2187 {
2188 /* This is bound to be a class array element. */
2189 gfc_conv_expr_reference (&se, e);
2190 /* Obtain a temporary class container for the result. */
2191 gfc_conv_derived_to_class (&se, e, sym, se.expr, false, false,
2192 e->symtree->name);
2193 need_len_assign = false;
2194 }
2195 else if (whole_array && (same_class || unlimited)
2196 && e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.codimension)
2197 {
2198 gfc_expr *class_e = gfc_find_and_cut_at_last_class_ref (e);
2199 gfc_conv_expr (&se, class_e);
2200 gfc_free_expr (class_e);
2201 need_len_assign = false;
2202 }
2203 else
2204 {
2205 /* For BT_CLASS and BT_DERIVED, this boils down to a pointer assign,
2206 which has the string length included. For CHARACTERS it is still
2207 needed and will be done at the end of this routine. */
2208 gfc_conv_expr (&se, e);
2209 need_len_assign = need_len_assign && sym->ts.type == BT_CHARACTER;
2210 }
2211
2212 if (sym->ts.type == BT_CHARACTER
2213 && !sym->attr.select_type_temporary
2214 && VAR_P (sym->ts.u.cl->backend_decl)
2215 && se.string_length != sym->ts.u.cl->backend_decl)
2216 {
2217 gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl,
2218 fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
2219 se.string_length));
2220 if (e->expr_type == EXPR_FUNCTION)
2221 {
2222 tmp = gfc_call_free (sym->backend_decl);
2223 gfc_add_expr_to_block (&se.post, tmp);
2224 }
2225 }
2226
2227 if (sym->ts.type == BT_CHARACTER && e->ts.type == BT_CHARACTER
2228 && POINTER_TYPE_P (TREE_TYPE (se.expr)))
2229 {
2230 /* These are pointer types already. */
2231 tmp = fold_convert (TREE_TYPE (sym->backend_decl), se.expr);
2232 }
2233 else
2234 {
2235 tree ctree = gfc_get_class_from_expr (se.expr);
2236 tmp = TREE_TYPE (sym->backend_decl);
2237
2238 if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
2239 {
2240 /* F2018:19.5.1.6 "If a selector has the POINTER attribute,
2241 it shall be associated; the associate name is associated
2242 with the target of the pointer and does not have the
2243 POINTER attribute." */
2244 if (e->rank == 0 && ctree
2245 && (!GFC_CLASS_TYPE_P (TREE_TYPE (se.expr))
2246 || CLASS_DATA (e)->attr.class_pointer))
2247 {
2248 tree stmp;
2249 tree dtmp;
2250 tree ctmp;
2251
2252 ctmp = ctree;
2253 dtmp = TREE_TYPE (TREE_TYPE (sym->backend_decl));
2254 ctree = gfc_create_var (dtmp, "class");
2255
2256 if (IS_INFERRED_TYPE (e)
2257 && !GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
2258 stmp = se.expr;
2259 else
2260 stmp = gfc_class_data_get (ctmp);
2261
2262 if (!CLASS_DATA (sym)->attr.codimension
2263 && !POINTER_TYPE_P (TREE_TYPE (stmp)))
2264 stmp = gfc_build_addr_expr (NULL, stmp);
2265
2266 dtmp = gfc_class_data_get (ctree);
2267 stmp = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dtmp), stmp);
2268 gfc_add_modify (&se.pre, dtmp, stmp);
2269 stmp = gfc_class_vptr_get (ctmp);
2270 dtmp = gfc_class_vptr_get (ctree);
2271 stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2272 gfc_add_modify (&se.pre, dtmp, stmp);
2273 if (UNLIMITED_POLY (sym))
2274 {
2275 stmp = gfc_class_len_get (ctmp);
2276 dtmp = gfc_class_len_get (ctree);
2277 stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2278 gfc_add_modify (&se.pre, dtmp, stmp);
2279 need_len_assign = false;
2280 }
2281 se.expr = ctree;
2282 }
2283 else if (CLASS_DATA (sym)->attr.codimension)
2284 {
2285 gfc_conv_class_to_class (&se, e, sym->ts, false, false, false,
2286 false);
2287 tmp = se.expr;
2288 }
2289 }
2290 /* For non-pointer types in se.expr, the first condition holds.
2291 For pointer or reference types in se.expr, a double TREE_TYPE ()
2292 is possible and an associate variable always is a pointer. */
2293 if (!POINTER_TYPE_P (TREE_TYPE (se.expr))
2294 || TREE_TYPE (TREE_TYPE (se.expr))
2295 != TREE_TYPE (TREE_TYPE (sym->backend_decl)))
2296 tmp = gfc_build_addr_expr (tmp, se.expr);
2297 }
2298
2299 gfc_add_modify (&se.pre, sym->backend_decl, tmp);
2300
2301 gfc_add_init_cleanup (block, gfc_finish_block( &se.pre),
2302 gfc_finish_block (&se.post));
2303 }
2304
2305 /* Do a simple assignment. This is for scalar expressions, where we
2306 can simply use expression assignment. */
2307 else
2308 {
2309 gfc_expr *lhs;
2310 tree res;
2311 gfc_se se;
2312 stmtblock_t final_block;
2313
2314 gfc_init_se (&se, NULL);
2315
2316 /* resolve.cc converts some associate names to allocatable so that
2317 allocation can take place automatically in gfc_trans_assignment.
2318 The frontend prevents them from being either allocated,
2319 deallocated or reallocated. */
2320 if (sym->ts.type == BT_DERIVED
2321 && sym->ts.u.derived->attr.alloc_comp)
2322 {
2323 tmp = sym->backend_decl;
2324 tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, tmp,
2325 sym->attr.dimension ? sym->as->rank : 0);
2326 gfc_add_expr_to_block (&se.pre, tmp);
2327 }
2328
2329 if (sym->attr.allocatable)
2330 {
2331 tmp = sym->backend_decl;
2332 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2333 tmp = gfc_conv_descriptor_data_get (tmp);
2334 gfc_add_modify (&se.pre, tmp, fold_convert (TREE_TYPE (tmp),
2335 null_pointer_node));
2336 }
2337
2338 lhs = gfc_lval_expr_from_sym (sym);
2339 lhs->must_finalize = 0;
2340 res = gfc_trans_assignment (lhs, e, false, true);
2341 gfc_add_expr_to_block (&se.pre, res);
2342
2343 gfc_init_block (&final_block);
2344
2345 if (sym->attr.associate_var
2346 && sym->ts.type == BT_DERIVED
2347 && sym->ts.u.derived->attr.defined_assign_comp
2348 && gfc_may_be_finalized (sym->ts)
2349 && e->expr_type == EXPR_FUNCTION)
2350 {
2351 gfc_expr *ef;
2352 ef = gfc_lval_expr_from_sym (sym);
2353 gfc_add_finalizer_call (&final_block, ef);
2354 gfc_free_expr (ef);
2355 }
2356
2357 if (sym->ts.type == BT_DERIVED
2358 && sym->ts.u.derived->attr.alloc_comp)
2359 {
2360 tmp = sym->backend_decl;
2361 tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived,
2362 tmp, 0);
2363 gfc_add_expr_to_block (&final_block, tmp);
2364 }
2365
2366 tmp = sym->backend_decl;
2367 if (e->expr_type == EXPR_FUNCTION
2368 && sym->ts.type == BT_DERIVED
2369 && sym->ts.u.derived
2370 && sym->ts.u.derived->attr.pdt_type)
2371 {
2372 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, tmp,
2373 0);
2374 }
2375 else if (e->expr_type == EXPR_FUNCTION
2376 && sym->ts.type == BT_CLASS
2377 && CLASS_DATA (sym)->ts.u.derived
2378 && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
2379 {
2380 tmp = gfc_class_data_get (tmp);
2381 tmp = gfc_deallocate_pdt_comp (CLASS_DATA (sym)->ts.u.derived,
2382 tmp, 0);
2383 }
2384 else if (sym->attr.allocatable)
2385 {
2386 tmp = sym->backend_decl;
2387
2388 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2389 tmp = gfc_conv_descriptor_data_get (tmp);
2390
2391 /* A simple call to free suffices here. */
2392 tmp = gfc_call_free (tmp);
2393
2394 /* Make sure that reallocation on assignment cannot occur. */
2395 sym->attr.allocatable = 0;
2396 }
2397 else
2398 tmp = NULL_TREE;
2399
2400 gfc_add_expr_to_block (&final_block, tmp);
2401 tmp = gfc_finish_block (&final_block);
2402 res = gfc_finish_block (&se.pre);
2403 gfc_add_init_cleanup (block, res, tmp);
2404 gfc_free_expr (lhs);
2405 }
2406
2407 /* Set the stringlength, when needed. */
2408 if (need_len_assign)
2409 {
2410 gfc_se se;
2411 gfc_init_se (&se, NULL);
2412 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2413 {
2414 /* Deferred strings are dealt with in the preceding. */
2415 gcc_assert (!e->symtree->n.sym->ts.deferred);
2416 tmp = e->symtree->n.sym->ts.u.cl->backend_decl;
2417 }
2418 else if (e->symtree->n.sym->attr.function
2419 && e->symtree->n.sym == e->symtree->n.sym->result)
2420 {
2421 tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
2422 tmp = gfc_class_len_get (tmp);
2423 }
2424 else
2425 tmp = gfc_class_len_get (gfc_get_symbol_decl (e->symtree->n.sym));
2426 gfc_get_symbol_decl (sym);
2427 charlen = sym->ts.type == BT_CHARACTER ? sym->ts.u.cl->backend_decl
2428 : gfc_class_len_get (sym->backend_decl);
2429 /* Prevent adding a noop len= len. */
2430 if (tmp != charlen)
2431 {
2432 gfc_add_modify (&se.pre, charlen,
2433 fold_convert (TREE_TYPE (charlen), tmp));
2434 gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2435 gfc_finish_block (&se.post));
2436 }
2437 }
2438 }
2439
2440
2441 /* Translate a BLOCK construct. This is basically what we would do for a
2442 procedure body. */
2443
2444 tree
2445 gfc_trans_block_construct (gfc_code* code)
2446 {
2447 gfc_namespace* ns;
2448 gfc_symbol* sym;
2449 gfc_wrapped_block block;
2450 tree exit_label;
2451 stmtblock_t body;
2452 gfc_association_list *ass;
2453 tree translated_body;
2454
2455 ns = code->ext.block.ns;
2456 gcc_assert (ns);
2457 sym = ns->proc_name;
2458 gcc_assert (sym);
2459
2460 /* Process local variables. */
2461 gcc_assert (!sym->tlink);
2462 sym->tlink = sym;
2463 gfc_process_block_locals (ns);
2464
2465 /* Generate code including exit-label. */
2466 gfc_init_block (&body);
2467 exit_label = gfc_build_label_decl (NULL_TREE);
2468 code->exit_label = exit_label;
2469
2470 finish_oacc_declare (ns, sym, true);
2471
2472 translated_body = gfc_trans_code (ns->code);
2473 if (ns->omp_structured_block)
2474 translated_body = build1 (OMP_STRUCTURED_BLOCK, void_type_node,
2475 translated_body);
2476 gfc_add_expr_to_block (&body, translated_body);
2477 gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
2478
2479 /* Finish everything. */
2480 gfc_start_wrapped_block (&block, gfc_finish_block (&body));
2481 gfc_trans_deferred_vars (sym, &block);
2482 for (ass = code->ext.block.assoc; ass; ass = ass->next)
2483 trans_associate_var (ass->st->n.sym, &block);
2484
2485 return gfc_finish_wrapped_block (&block);
2486 }
2487
2488 /* Translate the simple DO construct in a C-style manner.
2489 This is where the loop variable has integer type and step +-1.
2490 Following code will generate infinite loop in case where TO is INT_MAX
2491 (for +1 step) or INT_MIN (for -1 step)
2492
2493 We translate a do loop from:
2494
2495 DO dovar = from, to, step
2496 body
2497 END DO
2498
2499 to:
2500
2501 [Evaluate loop bounds and step]
2502 dovar = from;
2503 for (;;)
2504 {
2505 if (dovar > to)
2506 goto end_label;
2507 body;
2508 cycle_label:
2509 dovar += step;
2510 }
2511 end_label:
2512
2513 This helps the optimizers by avoiding the extra pre-header condition and
2514 we save a register as we just compare the updated IV (not a value in
2515 previous step). */
2516
2517 static tree
2518 gfc_trans_simple_do (gfc_code * code, stmtblock_t *pblock, tree dovar,
2519 tree from, tree to, tree step, tree exit_cond)
2520 {
2521 stmtblock_t body;
2522 tree type;
2523 tree cond;
2524 tree tmp;
2525 tree saved_dovar = NULL;
2526 tree cycle_label;
2527 tree exit_label;
2528 location_t loc;
2529 type = TREE_TYPE (dovar);
2530 bool is_step_positive = tree_int_cst_sgn (step) > 0;
2531
2532 loc = gfc_get_location (&code->ext.iterator->start->where);
2533
2534 /* Initialize the DO variable: dovar = from. */
2535 gfc_add_modify_loc (loc, pblock, dovar,
2536 fold_convert (TREE_TYPE (dovar), from));
2537
2538 /* Save value for do-tinkering checking. */
2539 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2540 {
2541 saved_dovar = gfc_create_var (type, ".saved_dovar");
2542 gfc_add_modify_loc (loc, pblock, saved_dovar, dovar);
2543 }
2544
2545 /* Cycle and exit statements are implemented with gotos. */
2546 cycle_label = gfc_build_label_decl (NULL_TREE);
2547 exit_label = gfc_build_label_decl (NULL_TREE);
2548
2549 /* Put the labels where they can be found later. See gfc_trans_do(). */
2550 code->cycle_label = cycle_label;
2551 code->exit_label = exit_label;
2552
2553 /* Loop body. */
2554 gfc_start_block (&body);
2555
2556 /* Exit the loop if there is an I/O result condition or error. */
2557 if (exit_cond)
2558 {
2559 tmp = build1_v (GOTO_EXPR, exit_label);
2560 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2561 exit_cond, tmp,
2562 build_empty_stmt (loc));
2563 gfc_add_expr_to_block (&body, tmp);
2564 }
2565
2566 /* Evaluate the loop condition. */
2567 if (is_step_positive)
2568 cond = fold_build2_loc (loc, GT_EXPR, logical_type_node, dovar,
2569 fold_convert (type, to));
2570 else
2571 cond = fold_build2_loc (loc, LT_EXPR, logical_type_node, dovar,
2572 fold_convert (type, to));
2573
2574 cond = gfc_evaluate_now_loc (loc, cond, &body);
2575 if (code->ext.iterator->annot.unroll && cond != error_mark_node)
2576 cond
2577 = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2578 build_int_cst (integer_type_node, annot_expr_unroll_kind),
2579 build_int_cst (integer_type_node,
2580 code->ext.iterator->annot.unroll));
2581
2582 if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
2583 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2584 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
2585 integer_zero_node);
2586 if (code->ext.iterator->annot.vector && cond != error_mark_node)
2587 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2588 build_int_cst (integer_type_node, annot_expr_vector_kind),
2589 integer_zero_node);
2590 if (code->ext.iterator->annot.novector && cond != error_mark_node)
2591 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2592 build_int_cst (integer_type_node, annot_expr_no_vector_kind),
2593 integer_zero_node);
2594
2595 /* The loop exit. */
2596 tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
2597 TREE_USED (exit_label) = 1;
2598 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2599 cond, tmp, build_empty_stmt (loc));
2600 gfc_add_expr_to_block (&body, tmp);
2601
2602 /* Check whether the induction variable is equal to INT_MAX
2603 (respectively to INT_MIN). */
2604 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2605 {
2606 tree boundary = is_step_positive ? TYPE_MAX_VALUE (type)
2607 : TYPE_MIN_VALUE (type);
2608
2609 tmp = fold_build2_loc (loc, EQ_EXPR, logical_type_node,
2610 dovar, boundary);
2611 gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2612 "Loop iterates infinitely");
2613 }
2614
2615 /* Main loop body. */
2616 tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2617 gfc_add_expr_to_block (&body, tmp);
2618
2619 /* Label for cycle statements (if needed). */
2620 if (TREE_USED (cycle_label))
2621 {
2622 tmp = build1_v (LABEL_EXPR, cycle_label);
2623 gfc_add_expr_to_block (&body, tmp);
2624 }
2625
2626 /* Check whether someone has modified the loop variable. */
2627 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2628 {
2629 tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node,
2630 dovar, saved_dovar);
2631 gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2632 "Loop variable has been modified");
2633 }
2634
2635 /* Increment the loop variable. */
2636 tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2637 gfc_add_modify_loc (loc, &body, dovar, tmp);
2638
2639 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2640 gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
2641
2642 /* Finish the loop body. */
2643 tmp = gfc_finish_block (&body);
2644 tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
2645
2646 gfc_add_expr_to_block (pblock, tmp);
2647
2648 /* Add the exit label. */
2649 tmp = build1_v (LABEL_EXPR, exit_label);
2650 gfc_add_expr_to_block (pblock, tmp);
2651
2652 return gfc_finish_block (pblock);
2653 }
2654
2655 /* Translate the DO construct. This obviously is one of the most
2656 important ones to get right with any compiler, but especially
2657 so for Fortran.
2658
2659 We special case some loop forms as described in gfc_trans_simple_do.
2660 For other cases we implement them with a separate loop count,
2661 as described in the standard.
2662
2663 We translate a do loop from:
2664
2665 DO dovar = from, to, step
2666 body
2667 END DO
2668
2669 to:
2670
2671 [evaluate loop bounds and step]
2672 empty = (step > 0 ? to < from : to > from);
2673 countm1 = (to - from) / step;
2674 dovar = from;
2675 if (empty) goto exit_label;
2676 for (;;)
2677 {
2678 body;
2679 cycle_label:
2680 dovar += step
2681 countm1t = countm1;
2682 countm1--;
2683 if (countm1t == 0) goto exit_label;
2684 }
2685 exit_label:
2686
2687 countm1 is an unsigned integer. It is equal to the loop count minus one,
2688 because the loop count itself can overflow. */
2689
2690 tree
2691 gfc_trans_do (gfc_code * code, tree exit_cond)
2692 {
2693 gfc_se se;
2694 tree dovar;
2695 tree saved_dovar = NULL;
2696 tree from;
2697 tree to;
2698 tree step;
2699 tree countm1;
2700 tree type;
2701 tree utype;
2702 tree cond;
2703 tree cycle_label;
2704 tree exit_label;
2705 tree tmp;
2706 stmtblock_t block;
2707 stmtblock_t body;
2708 location_t loc;
2709
2710 gfc_start_block (&block);
2711
2712 loc = gfc_get_location (&code->ext.iterator->start->where);
2713
2714 /* Evaluate all the expressions in the iterator. */
2715 gfc_init_se (&se, NULL);
2716 gfc_conv_expr_lhs (&se, code->ext.iterator->var);
2717 gfc_add_block_to_block (&block, &se.pre);
2718 dovar = se.expr;
2719 type = TREE_TYPE (dovar);
2720
2721 gfc_init_se (&se, NULL);
2722 gfc_conv_expr_val (&se, code->ext.iterator->start);
2723 gfc_add_block_to_block (&block, &se.pre);
2724 from = gfc_evaluate_now (se.expr, &block);
2725
2726 gfc_init_se (&se, NULL);
2727 gfc_conv_expr_val (&se, code->ext.iterator->end);
2728 gfc_add_block_to_block (&block, &se.pre);
2729 to = gfc_evaluate_now (se.expr, &block);
2730
2731 gfc_init_se (&se, NULL);
2732 gfc_conv_expr_val (&se, code->ext.iterator->step);
2733 gfc_add_block_to_block (&block, &se.pre);
2734 step = gfc_evaluate_now (se.expr, &block);
2735
2736 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2737 {
2738 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, step,
2739 build_zero_cst (type));
2740 gfc_trans_runtime_check (true, false, tmp, &block, &code->loc,
2741 "DO step value is zero");
2742 }
2743
2744 /* Special case simple loops. */
2745 if (TREE_CODE (type) == INTEGER_TYPE
2746 && (integer_onep (step)
2747 || tree_int_cst_equal (step, integer_minus_one_node)))
2748 return gfc_trans_simple_do (code, &block, dovar, from, to, step,
2749 exit_cond);
2750
2751 if (TREE_CODE (type) == INTEGER_TYPE)
2752 utype = unsigned_type_for (type);
2753 else
2754 utype = unsigned_type_for (gfc_array_index_type);
2755 countm1 = gfc_create_var (utype, "countm1");
2756
2757 /* Cycle and exit statements are implemented with gotos. */
2758 cycle_label = gfc_build_label_decl (NULL_TREE);
2759 exit_label = gfc_build_label_decl (NULL_TREE);
2760 TREE_USED (exit_label) = 1;
2761
2762 /* Put these labels where they can be found later. */
2763 code->cycle_label = cycle_label;
2764 code->exit_label = exit_label;
2765
2766 /* Initialize the DO variable: dovar = from. */
2767 gfc_add_modify (&block, dovar, from);
2768
2769 /* Save value for do-tinkering checking. */
2770 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2771 {
2772 saved_dovar = gfc_create_var (type, ".saved_dovar");
2773 gfc_add_modify_loc (loc, &block, saved_dovar, dovar);
2774 }
2775
2776 /* Initialize loop count and jump to exit label if the loop is empty.
2777 This code is executed before we enter the loop body. We generate:
2778 if (step > 0)
2779 {
2780 countm1 = (to - from) / step;
2781 if (to < from)
2782 goto exit_label;
2783 }
2784 else
2785 {
2786 countm1 = (from - to) / -step;
2787 if (to > from)
2788 goto exit_label;
2789 }
2790 */
2791
2792 if (TREE_CODE (type) == INTEGER_TYPE)
2793 {
2794 tree pos, neg, tou, fromu, stepu, tmp2;
2795
2796 /* The distance from FROM to TO cannot always be represented in a signed
2797 type, thus use unsigned arithmetic, also to avoid any undefined
2798 overflow issues. */
2799 tou = fold_convert (utype, to);
2800 fromu = fold_convert (utype, from);
2801 stepu = fold_convert (utype, step);
2802
2803 /* For a positive step, when to < from, exit, otherwise compute
2804 countm1 = ((unsigned)to - (unsigned)from) / (unsigned)step */
2805 tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, to, from);
2806 tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
2807 fold_build2_loc (loc, MINUS_EXPR, utype,
2808 tou, fromu),
2809 stepu);
2810 pos = build2 (COMPOUND_EXPR, void_type_node,
2811 fold_build2 (MODIFY_EXPR, void_type_node,
2812 countm1, tmp2),
2813 build3_loc (loc, COND_EXPR, void_type_node,
2814 gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
2815 build1_loc (loc, GOTO_EXPR, void_type_node,
2816 exit_label), NULL_TREE));
2817
2818 /* For a negative step, when to > from, exit, otherwise compute
2819 countm1 = ((unsigned)from - (unsigned)to) / -(unsigned)step */
2820 tmp = fold_build2_loc (loc, GT_EXPR, logical_type_node, to, from);
2821 tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
2822 fold_build2_loc (loc, MINUS_EXPR, utype,
2823 fromu, tou),
2824 fold_build1_loc (loc, NEGATE_EXPR, utype, stepu));
2825 neg = build2 (COMPOUND_EXPR, void_type_node,
2826 fold_build2 (MODIFY_EXPR, void_type_node,
2827 countm1, tmp2),
2828 build3_loc (loc, COND_EXPR, void_type_node,
2829 gfc_unlikely (tmp, PRED_FORTRAN_LOOP_PREHEADER),
2830 build1_loc (loc, GOTO_EXPR, void_type_node,
2831 exit_label), NULL_TREE));
2832
2833 tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, step,
2834 build_int_cst (TREE_TYPE (step), 0));
2835 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp, neg, pos);
2836
2837 gfc_add_expr_to_block (&block, tmp);
2838 }
2839 else
2840 {
2841 tree pos_step;
2842
2843 /* TODO: We could use the same width as the real type.
2844 This would probably cause more problems that it solves
2845 when we implement "long double" types. */
2846
2847 tmp = fold_build2_loc (loc, MINUS_EXPR, type, to, from);
2848 tmp = fold_build2_loc (loc, RDIV_EXPR, type, tmp, step);
2849 tmp = fold_build1_loc (loc, FIX_TRUNC_EXPR, utype, tmp);
2850 gfc_add_modify (&block, countm1, tmp);
2851
2852 /* We need a special check for empty loops:
2853 empty = (step > 0 ? to < from : to > from); */
2854 pos_step = fold_build2_loc (loc, GT_EXPR, logical_type_node, step,
2855 build_zero_cst (type));
2856 tmp = fold_build3_loc (loc, COND_EXPR, logical_type_node, pos_step,
2857 fold_build2_loc (loc, LT_EXPR,
2858 logical_type_node, to, from),
2859 fold_build2_loc (loc, GT_EXPR,
2860 logical_type_node, to, from));
2861 /* If the loop is empty, go directly to the exit label. */
2862 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp,
2863 build1_v (GOTO_EXPR, exit_label),
2864 build_empty_stmt (input_location));
2865 gfc_add_expr_to_block (&block, tmp);
2866 }
2867
2868 /* Loop body. */
2869 gfc_start_block (&body);
2870
2871 /* Main loop body. */
2872 tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2873 gfc_add_expr_to_block (&body, tmp);
2874
2875 /* Label for cycle statements (if needed). */
2876 if (TREE_USED (cycle_label))
2877 {
2878 tmp = build1_v (LABEL_EXPR, cycle_label);
2879 gfc_add_expr_to_block (&body, tmp);
2880 }
2881
2882 /* Check whether someone has modified the loop variable. */
2883 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2884 {
2885 tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node, dovar,
2886 saved_dovar);
2887 gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2888 "Loop variable has been modified");
2889 }
2890
2891 /* Exit the loop if there is an I/O result condition or error. */
2892 if (exit_cond)
2893 {
2894 tmp = build1_v (GOTO_EXPR, exit_label);
2895 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2896 exit_cond, tmp,
2897 build_empty_stmt (input_location));
2898 gfc_add_expr_to_block (&body, tmp);
2899 }
2900
2901 /* Increment the loop variable. */
2902 tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2903 gfc_add_modify_loc (loc, &body, dovar, tmp);
2904
2905 if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2906 gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
2907
2908 /* Initialize countm1t. */
2909 tree countm1t = gfc_create_var (utype, "countm1t");
2910 gfc_add_modify_loc (loc, &body, countm1t, countm1);
2911
2912 /* Decrement the loop count. */
2913 tmp = fold_build2_loc (loc, MINUS_EXPR, utype, countm1,
2914 build_int_cst (utype, 1));
2915 gfc_add_modify_loc (loc, &body, countm1, tmp);
2916
2917 /* End with the loop condition. Loop until countm1t == 0. */
2918 cond = fold_build2_loc (loc, EQ_EXPR, logical_type_node, countm1t,
2919 build_int_cst (utype, 0));
2920 if (code->ext.iterator->annot.unroll && cond != error_mark_node)
2921 cond
2922 = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2923 build_int_cst (integer_type_node, annot_expr_unroll_kind),
2924 build_int_cst (integer_type_node,
2925 code->ext.iterator->annot.unroll));
2926
2927 if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
2928 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2929 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
2930 integer_zero_node);
2931 if (code->ext.iterator->annot.vector && cond != error_mark_node)
2932 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2933 build_int_cst (integer_type_node, annot_expr_vector_kind),
2934 integer_zero_node);
2935 if (code->ext.iterator->annot.novector && cond != error_mark_node)
2936 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2937 build_int_cst (integer_type_node, annot_expr_no_vector_kind),
2938 integer_zero_node);
2939
2940 tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
2941 tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2942 cond, tmp, build_empty_stmt (loc));
2943 gfc_add_expr_to_block (&body, tmp);
2944
2945 /* End of loop body. */
2946 tmp = gfc_finish_block (&body);
2947
2948 /* The for loop itself. */
2949 tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
2950 gfc_add_expr_to_block (&block, tmp);
2951
2952 /* Add the exit label. */
2953 tmp = build1_v (LABEL_EXPR, exit_label);
2954 gfc_add_expr_to_block (&block, tmp);
2955
2956 return gfc_finish_block (&block);
2957 }
2958
2959
2960 /* Translate the DO WHILE construct.
2961
2962 We translate
2963
2964 DO WHILE (cond)
2965 body
2966 END DO
2967
2968 to:
2969
2970 for ( ; ; )
2971 {
2972 pre_cond;
2973 if (! cond) goto exit_label;
2974 body;
2975 cycle_label:
2976 }
2977 exit_label:
2978
2979 Because the evaluation of the exit condition `cond' may have side
2980 effects, we can't do much for empty loop bodies. The backend optimizers
2981 should be smart enough to eliminate any dead loops. */
2982
2983 tree
2984 gfc_trans_do_while (gfc_code * code)
2985 {
2986 gfc_se cond;
2987 tree tmp;
2988 tree cycle_label;
2989 tree exit_label;
2990 stmtblock_t block;
2991
2992 /* Everything we build here is part of the loop body. */
2993 gfc_start_block (&block);
2994
2995 /* Cycle and exit statements are implemented with gotos. */
2996 cycle_label = gfc_build_label_decl (NULL_TREE);
2997 exit_label = gfc_build_label_decl (NULL_TREE);
2998
2999 /* Put the labels where they can be found later. See gfc_trans_do(). */
3000 code->cycle_label = cycle_label;
3001 code->exit_label = exit_label;
3002
3003 /* Create a GIMPLE version of the exit condition. */
3004 gfc_init_se (&cond, NULL);
3005 gfc_conv_expr_val (&cond, code->expr1);
3006 gfc_add_block_to_block (&block, &cond.pre);
3007 cond.expr = fold_build1_loc (gfc_get_location (&code->expr1->where),
3008 TRUTH_NOT_EXPR, TREE_TYPE (cond.expr),
3009 cond.expr);
3010
3011 /* Build "IF (! cond) GOTO exit_label". */
3012 tmp = build1_v (GOTO_EXPR, exit_label);
3013 TREE_USED (exit_label) = 1;
3014 tmp = fold_build3_loc (gfc_get_location (&code->expr1->where), COND_EXPR,
3015 void_type_node, cond.expr, tmp,
3016 build_empty_stmt (gfc_get_location (
3017 &code->expr1->where)));
3018 gfc_add_expr_to_block (&block, tmp);
3019
3020 /* The main body of the loop. */
3021 tmp = gfc_trans_code (code->block->next);
3022 gfc_add_expr_to_block (&block, tmp);
3023
3024 /* Label for cycle statements (if needed). */
3025 if (TREE_USED (cycle_label))
3026 {
3027 tmp = build1_v (LABEL_EXPR, cycle_label);
3028 gfc_add_expr_to_block (&block, tmp);
3029 }
3030
3031 /* End of loop body. */
3032 tmp = gfc_finish_block (&block);
3033
3034 gfc_init_block (&block);
3035 /* Build the loop. */
3036 tmp = fold_build1_loc (gfc_get_location (&code->expr1->where), LOOP_EXPR,
3037 void_type_node, tmp);
3038 gfc_add_expr_to_block (&block, tmp);
3039
3040 /* Add the exit label. */
3041 tmp = build1_v (LABEL_EXPR, exit_label);
3042 gfc_add_expr_to_block (&block, tmp);
3043
3044 return gfc_finish_block (&block);
3045 }
3046
3047
3048 /* Deal with the particular case of SELECT_TYPE, where the vtable
3049 addresses are used for the selection. Since these are not sorted,
3050 the selection has to be made by a series of if statements. */
3051
3052 static tree
3053 gfc_trans_select_type_cases (gfc_code * code)
3054 {
3055 gfc_code *c;
3056 gfc_case *cp;
3057 tree tmp;
3058 tree cond;
3059 tree low;
3060 tree high;
3061 gfc_se se;
3062 gfc_se cse;
3063 stmtblock_t block;
3064 stmtblock_t body;
3065 bool def = false;
3066 gfc_expr *e;
3067 gfc_start_block (&block);
3068
3069 /* Calculate the switch expression. */
3070 gfc_init_se (&se, NULL);
3071 gfc_conv_expr_val (&se, code->expr1);
3072 gfc_add_block_to_block (&block, &se.pre);
3073
3074 /* Generate an expression for the selector hash value, for
3075 use to resolve character cases. */
3076 e = gfc_copy_expr (code->expr1->value.function.actual->expr);
3077 gfc_add_hash_component (e);
3078
3079 TREE_USED (code->exit_label) = 0;
3080
3081 repeat:
3082 for (c = code->block; c; c = c->block)
3083 {
3084 cp = c->ext.block.case_list;
3085
3086 /* Assume it's the default case. */
3087 low = NULL_TREE;
3088 high = NULL_TREE;
3089 tmp = NULL_TREE;
3090
3091 /* Put the default case at the end. */
3092 if ((!def && !cp->low) || (def && cp->low))
3093 continue;
3094
3095 if (cp->low && (cp->ts.type == BT_CLASS
3096 || cp->ts.type == BT_DERIVED))
3097 {
3098 gfc_init_se (&cse, NULL);
3099 gfc_conv_expr_val (&cse, cp->low);
3100 gfc_add_block_to_block (&block, &cse.pre);
3101 low = cse.expr;
3102 }
3103 else if (cp->ts.type != BT_UNKNOWN)
3104 {
3105 gcc_assert (cp->high);
3106 gfc_init_se (&cse, NULL);
3107 gfc_conv_expr_val (&cse, cp->high);
3108 gfc_add_block_to_block (&block, &cse.pre);
3109 high = cse.expr;
3110 }
3111
3112 gfc_init_block (&body);
3113
3114 /* Add the statements for this case. */
3115 tmp = gfc_trans_code (c->next);
3116 gfc_add_expr_to_block (&body, tmp);
3117
3118 /* Break to the end of the SELECT TYPE construct. The default
3119 case just falls through. */
3120 if (!def)
3121 {
3122 TREE_USED (code->exit_label) = 1;
3123 tmp = build1_v (GOTO_EXPR, code->exit_label);
3124 gfc_add_expr_to_block (&body, tmp);
3125 }
3126
3127 tmp = gfc_finish_block (&body);
3128
3129 if (low != NULL_TREE)
3130 {
3131 /* Compare vtable pointers. */
3132 cond = fold_build2_loc (input_location, EQ_EXPR,
3133 TREE_TYPE (se.expr), se.expr, low);
3134 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3135 cond, tmp,
3136 build_empty_stmt (input_location));
3137 }
3138 else if (high != NULL_TREE)
3139 {
3140 /* Compare hash values for character cases. */
3141 gfc_init_se (&cse, NULL);
3142 gfc_conv_expr_val (&cse, e);
3143 gfc_add_block_to_block (&block, &cse.pre);
3144
3145 cond = fold_build2_loc (input_location, EQ_EXPR,
3146 TREE_TYPE (se.expr), high, cse.expr);
3147 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3148 cond, tmp,
3149 build_empty_stmt (input_location));
3150 }
3151
3152 gfc_add_expr_to_block (&block, tmp);
3153 }
3154
3155 if (!def)
3156 {
3157 def = true;
3158 goto repeat;
3159 }
3160
3161 gfc_free_expr (e);
3162
3163 return gfc_finish_block (&block);
3164 }
3165
3166
3167 /* Translate the SELECT CASE construct for INTEGER case expressions,
3168 without killing all potential optimizations. The problem is that
3169 Fortran allows unbounded cases, but the back-end does not, so we
3170 need to intercept those before we enter the equivalent SWITCH_EXPR
3171 we can build.
3172
3173 For example, we translate this,
3174
3175 SELECT CASE (expr)
3176 CASE (:100,101,105:115)
3177 block_1
3178 CASE (190:199,200:)
3179 block_2
3180 CASE (300)
3181 block_3
3182 CASE DEFAULT
3183 block_4
3184 END SELECT
3185
3186 to the GENERIC equivalent,
3187
3188 switch (expr)
3189 {
3190 case (minimum value for typeof(expr) ... 100:
3191 case 101:
3192 case 105 ... 114:
3193 block1:
3194 goto end_label;
3195
3196 case 200 ... (maximum value for typeof(expr):
3197 case 190 ... 199:
3198 block2;
3199 goto end_label;
3200
3201 case 300:
3202 block_3;
3203 goto end_label;
3204
3205 default:
3206 block_4;
3207 goto end_label;
3208 }
3209
3210 end_label: */
3211
3212 static tree
3213 gfc_trans_integer_select (gfc_code * code)
3214 {
3215 gfc_code *c;
3216 gfc_case *cp;
3217 tree end_label;
3218 tree tmp;
3219 gfc_se se;
3220 stmtblock_t block;
3221 stmtblock_t body;
3222
3223 gfc_start_block (&block);
3224
3225 /* Calculate the switch expression. */
3226 gfc_init_se (&se, NULL);
3227 gfc_conv_expr_val (&se, code->expr1);
3228 gfc_add_block_to_block (&block, &se.pre);
3229
3230 end_label = gfc_build_label_decl (NULL_TREE);
3231
3232 gfc_init_block (&body);
3233
3234 for (c = code->block; c; c = c->block)
3235 {
3236 for (cp = c->ext.block.case_list; cp; cp = cp->next)
3237 {
3238 tree low, high;
3239 tree label;
3240
3241 /* Assume it's the default case. */
3242 low = high = NULL_TREE;
3243
3244 if (cp->low)
3245 {
3246 if (cp->low->ts.type == BT_INTEGER)
3247 low = gfc_conv_mpz_to_tree (cp->low->value.integer,
3248 cp->low->ts.kind);
3249 else
3250 low = gfc_conv_mpz_unsigned_to_tree (cp->low->value.integer,
3251 cp->low->ts.kind);
3252
3253 /* If there's only a lower bound, set the high bound to the
3254 maximum value of the case expression. */
3255 if (!cp->high)
3256 high = TYPE_MAX_VALUE (TREE_TYPE (se.expr));
3257 }
3258
3259 if (cp->high)
3260 {
3261 /* Three cases are possible here:
3262
3263 1) There is no lower bound, e.g. CASE (:N).
3264 2) There is a lower bound .NE. high bound, that is
3265 a case range, e.g. CASE (N:M) where M>N (we make
3266 sure that M>N during type resolution).
3267 3) There is a lower bound, and it has the same value
3268 as the high bound, e.g. CASE (N:N). This is our
3269 internal representation of CASE(N).
3270
3271 In the first and second case, we need to set a value for
3272 high. In the third case, we don't because the GCC middle
3273 end represents a single case value by just letting high be
3274 a NULL_TREE. We can't do that because we need to be able
3275 to represent unbounded cases. */
3276
3277 if (!cp->low
3278 || (mpz_cmp (cp->low->value.integer,
3279 cp->high->value.integer) != 0))
3280 {
3281 if (cp->high->ts.type == BT_INTEGER)
3282 high = gfc_conv_mpz_to_tree (cp->high->value.integer,
3283 cp->high->ts.kind);
3284 else
3285 high
3286 = gfc_conv_mpz_unsigned_to_tree (cp->high->value.integer,
3287 cp->high->ts.kind);
3288 }
3289
3290 /* Unbounded case. */
3291 if (!cp->low)
3292 low = TYPE_MIN_VALUE (TREE_TYPE (se.expr));
3293 }
3294
3295 /* Build a label. */
3296 label = gfc_build_label_decl (NULL_TREE);
3297
3298 /* Add this case label.
3299 Add parameter 'label', make it match GCC backend. */
3300 tmp = build_case_label (low, high, label);
3301 gfc_add_expr_to_block (&body, tmp);
3302 }
3303
3304 /* Add the statements for this case. */
3305 tmp = gfc_trans_code (c->next);
3306 gfc_add_expr_to_block (&body, tmp);
3307
3308 /* Break to the end of the construct. */
3309 tmp = build1_v (GOTO_EXPR, end_label);
3310 gfc_add_expr_to_block (&body, tmp);
3311 }
3312
3313 tmp = gfc_finish_block (&body);
3314 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, se.expr, tmp);
3315 gfc_add_expr_to_block (&block, tmp);
3316
3317 tmp = build1_v (LABEL_EXPR, end_label);
3318 gfc_add_expr_to_block (&block, tmp);
3319
3320 return gfc_finish_block (&block);
3321 }
3322
3323
3324 /* Translate the SELECT CASE construct for LOGICAL case expressions.
3325
3326 There are only two cases possible here, even though the standard
3327 does allow three cases in a LOGICAL SELECT CASE construct: .TRUE.,
3328 .FALSE., and DEFAULT.
3329
3330 We never generate more than two blocks here. Instead, we always
3331 try to eliminate the DEFAULT case. This way, we can translate this
3332 kind of SELECT construct to a simple
3333
3334 if {} else {};
3335
3336 expression in GENERIC. */
3337
3338 static tree
3339 gfc_trans_logical_select (gfc_code * code)
3340 {
3341 gfc_code *c;
3342 gfc_code *t, *f, *d;
3343 gfc_case *cp;
3344 gfc_se se;
3345 stmtblock_t block;
3346
3347 /* Assume we don't have any cases at all. */
3348 t = f = d = NULL;
3349
3350 /* Now see which ones we actually do have. We can have at most two
3351 cases in a single case list: one for .TRUE. and one for .FALSE.
3352 The default case is always separate. If the cases for .TRUE. and
3353 .FALSE. are in the same case list, the block for that case list
3354 always executed, and we don't generate code a COND_EXPR. */
3355 for (c = code->block; c; c = c->block)
3356 {
3357 for (cp = c->ext.block.case_list; cp; cp = cp->next)
3358 {
3359 if (cp->low)
3360 {
3361 if (cp->low->value.logical == 0) /* .FALSE. */
3362 f = c;
3363 else /* if (cp->value.logical != 0), thus .TRUE. */
3364 t = c;
3365 }
3366 else
3367 d = c;
3368 }
3369 }
3370
3371 /* Start a new block. */
3372 gfc_start_block (&block);
3373
3374 /* Calculate the switch expression. We always need to do this
3375 because it may have side effects. */
3376 gfc_init_se (&se, NULL);
3377 gfc_conv_expr_val (&se, code->expr1);
3378 gfc_add_block_to_block (&block, &se.pre);
3379
3380 if (t == f && t != NULL)
3381 {
3382 /* Cases for .TRUE. and .FALSE. are in the same block. Just
3383 translate the code for these cases, append it to the current
3384 block. */
3385 gfc_add_expr_to_block (&block, gfc_trans_code (t->next));
3386 }
3387 else
3388 {
3389 tree true_tree, false_tree, stmt;
3390
3391 true_tree = build_empty_stmt (input_location);
3392 false_tree = build_empty_stmt (input_location);
3393
3394 /* If we have a case for .TRUE. and for .FALSE., discard the default case.
3395 Otherwise, if .TRUE. or .FALSE. is missing and there is a default case,
3396 make the missing case the default case. */
3397 if (t != NULL && f != NULL)
3398 d = NULL;
3399 else if (d != NULL)
3400 {
3401 if (t == NULL)
3402 t = d;
3403 else
3404 f = d;
3405 }
3406
3407 /* Translate the code for each of these blocks, and append it to
3408 the current block. */
3409 if (t != NULL)
3410 true_tree = gfc_trans_code (t->next);
3411
3412 if (f != NULL)
3413 false_tree = gfc_trans_code (f->next);
3414
3415 stmt = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3416 se.expr, true_tree, false_tree);
3417 gfc_add_expr_to_block (&block, stmt);
3418 }
3419
3420 return gfc_finish_block (&block);
3421 }
3422
3423
3424 /* The jump table types are stored in static variables to avoid
3425 constructing them from scratch every single time. */
3426 static GTY(()) tree select_struct[2];
3427
3428 /* Translate the SELECT CASE construct for CHARACTER case expressions.
3429 Instead of generating compares and jumps, it is far simpler to
3430 generate a data structure describing the cases in order and call a
3431 library subroutine that locates the right case.
3432 This is particularly true because this is the only case where we
3433 might have to dispose of a temporary.
3434 The library subroutine returns a pointer to jump to or NULL if no
3435 branches are to be taken. */
3436
3437 static tree
3438 gfc_trans_character_select (gfc_code *code)
3439 {
3440 tree init, end_label, tmp, type, case_num, label, fndecl;
3441 stmtblock_t block, body;
3442 gfc_case *cp, *d;
3443 gfc_code *c;
3444 gfc_se se, expr1se;
3445 int n, k;
3446 vec<constructor_elt, va_gc> *inits = NULL;
3447
3448 tree pchartype = gfc_get_pchar_type (code->expr1->ts.kind);
3449
3450 /* The jump table types are stored in static variables to avoid
3451 constructing them from scratch every single time. */
3452 static tree ss_string1[2], ss_string1_len[2];
3453 static tree ss_string2[2], ss_string2_len[2];
3454 static tree ss_target[2];
3455
3456 cp = code->block->ext.block.case_list;
3457 while (cp->left != NULL)
3458 cp = cp->left;
3459
3460 /* Generate the body */
3461 gfc_start_block (&block);
3462 gfc_init_se (&expr1se, NULL);
3463 gfc_conv_expr_reference (&expr1se, code->expr1);
3464
3465 gfc_add_block_to_block (&block, &expr1se.pre);
3466
3467 end_label = gfc_build_label_decl (NULL_TREE);
3468
3469 gfc_init_block (&body);
3470
3471 /* Attempt to optimize length 1 selects. */
3472 if (integer_onep (expr1se.string_length))
3473 {
3474 for (d = cp; d; d = d->right)
3475 {
3476 gfc_charlen_t i;
3477 if (d->low)
3478 {
3479 gcc_assert (d->low->expr_type == EXPR_CONSTANT
3480 && d->low->ts.type == BT_CHARACTER);
3481 if (d->low->value.character.length > 1)
3482 {
3483 for (i = 1; i < d->low->value.character.length; i++)
3484 if (d->low->value.character.string[i] != ' ')
3485 break;
3486 if (i != d->low->value.character.length)
3487 {
3488 if (optimize && d->high && i == 1)
3489 {
3490 gcc_assert (d->high->expr_type == EXPR_CONSTANT
3491 && d->high->ts.type == BT_CHARACTER);
3492 if (d->high->value.character.length > 1
3493 && (d->low->value.character.string[0]
3494 == d->high->value.character.string[0])
3495 && d->high->value.character.string[1] != ' '
3496 && ((d->low->value.character.string[1] < ' ')
3497 == (d->high->value.character.string[1]
3498 < ' ')))
3499 continue;
3500 }
3501 break;
3502 }
3503 }
3504 }
3505 if (d->high)
3506 {
3507 gcc_assert (d->high->expr_type == EXPR_CONSTANT
3508 && d->high->ts.type == BT_CHARACTER);
3509 if (d->high->value.character.length > 1)
3510 {
3511 for (i = 1; i < d->high->value.character.length; i++)
3512 if (d->high->value.character.string[i] != ' ')
3513 break;
3514 if (i != d->high->value.character.length)
3515 break;
3516 }
3517 }
3518 }
3519 if (d == NULL)
3520 {
3521 tree ctype = gfc_get_char_type (code->expr1->ts.kind);
3522
3523 for (c = code->block; c; c = c->block)
3524 {
3525 for (cp = c->ext.block.case_list; cp; cp = cp->next)
3526 {
3527 tree low, high;
3528 tree label;
3529 gfc_char_t r;
3530
3531 /* Assume it's the default case. */
3532 low = high = NULL_TREE;
3533
3534 if (cp->low)
3535 {
3536 /* CASE ('ab') or CASE ('ab':'az') will never match
3537 any length 1 character. */
3538 if (cp->low->value.character.length > 1
3539 && cp->low->value.character.string[1] != ' ')
3540 continue;
3541
3542 if (cp->low->value.character.length > 0)
3543 r = cp->low->value.character.string[0];
3544 else
3545 r = ' ';
3546 low = build_int_cst (ctype, r);
3547
3548 /* If there's only a lower bound, set the high bound
3549 to the maximum value of the case expression. */
3550 if (!cp->high)
3551 high = TYPE_MAX_VALUE (ctype);
3552 }
3553
3554 if (cp->high)
3555 {
3556 if (!cp->low
3557 || (cp->low->value.character.string[0]
3558 != cp->high->value.character.string[0]))
3559 {
3560 if (cp->high->value.character.length > 0)
3561 r = cp->high->value.character.string[0];
3562 else
3563 r = ' ';
3564 high = build_int_cst (ctype, r);
3565 }
3566
3567 /* Unbounded case. */
3568 if (!cp->low)
3569 low = TYPE_MIN_VALUE (ctype);
3570 }
3571
3572 /* Build a label. */
3573 label = gfc_build_label_decl (NULL_TREE);
3574
3575 /* Add this case label.
3576 Add parameter 'label', make it match GCC backend. */
3577 tmp = build_case_label (low, high, label);
3578 gfc_add_expr_to_block (&body, tmp);
3579 }
3580
3581 /* Add the statements for this case. */
3582 tmp = gfc_trans_code (c->next);
3583 gfc_add_expr_to_block (&body, tmp);
3584
3585 /* Break to the end of the construct. */
3586 tmp = build1_v (GOTO_EXPR, end_label);
3587 gfc_add_expr_to_block (&body, tmp);
3588 }
3589
3590 tmp = gfc_string_to_single_character (expr1se.string_length,
3591 expr1se.expr,
3592 code->expr1->ts.kind);
3593 case_num = gfc_create_var (ctype, "case_num");
3594 gfc_add_modify (&block, case_num, tmp);
3595
3596 gfc_add_block_to_block (&block, &expr1se.post);
3597
3598 tmp = gfc_finish_block (&body);
3599 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3600 case_num, tmp);
3601 gfc_add_expr_to_block (&block, tmp);
3602
3603 tmp = build1_v (LABEL_EXPR, end_label);
3604 gfc_add_expr_to_block (&block, tmp);
3605
3606 return gfc_finish_block (&block);
3607 }
3608 }
3609
3610 if (code->expr1->ts.kind == 1)
3611 k = 0;
3612 else if (code->expr1->ts.kind == 4)
3613 k = 1;
3614 else
3615 gcc_unreachable ();
3616
3617 if (select_struct[k] == NULL)
3618 {
3619 tree *chain = NULL;
3620 select_struct[k] = make_node (RECORD_TYPE);
3621
3622 if (code->expr1->ts.kind == 1)
3623 TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char1");
3624 else if (code->expr1->ts.kind == 4)
3625 TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char4");
3626 else
3627 gcc_unreachable ();
3628
3629 #undef ADD_FIELD
3630 #define ADD_FIELD(NAME, TYPE) \
3631 ss_##NAME[k] = gfc_add_field_to_struct (select_struct[k], \
3632 get_identifier (stringize(NAME)), \
3633 TYPE, \
3634 &chain)
3635
3636 ADD_FIELD (string1, pchartype);
3637 ADD_FIELD (string1_len, gfc_charlen_type_node);
3638
3639 ADD_FIELD (string2, pchartype);
3640 ADD_FIELD (string2_len, gfc_charlen_type_node);
3641
3642 ADD_FIELD (target, integer_type_node);
3643 #undef ADD_FIELD
3644
3645 gfc_finish_type (select_struct[k]);
3646 }
3647
3648 n = 0;
3649 for (d = cp; d; d = d->right)
3650 d->n = n++;
3651
3652 for (c = code->block; c; c = c->block)
3653 {
3654 for (d = c->ext.block.case_list; d; d = d->next)
3655 {
3656 label = gfc_build_label_decl (NULL_TREE);
3657 tmp = build_case_label ((d->low == NULL && d->high == NULL)
3658 ? NULL
3659 : build_int_cst (integer_type_node, d->n),
3660 NULL, label);
3661 gfc_add_expr_to_block (&body, tmp);
3662 }
3663
3664 tmp = gfc_trans_code (c->next);
3665 gfc_add_expr_to_block (&body, tmp);
3666
3667 tmp = build1_v (GOTO_EXPR, end_label);
3668 gfc_add_expr_to_block (&body, tmp);
3669 }
3670
3671 /* Generate the structure describing the branches */
3672 for (d = cp; d; d = d->right)
3673 {
3674 vec<constructor_elt, va_gc> *node = NULL;
3675
3676 gfc_init_se (&se, NULL);
3677
3678 if (d->low == NULL)
3679 {
3680 CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], null_pointer_node);
3681 CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], build_zero_cst (gfc_charlen_type_node));
3682 }
3683 else
3684 {
3685 gfc_conv_expr_reference (&se, d->low);
3686
3687 CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], se.expr);
3688 CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], se.string_length);
3689 }
3690
3691 if (d->high == NULL)
3692 {
3693 CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], null_pointer_node);
3694 CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], build_zero_cst (gfc_charlen_type_node));
3695 }
3696 else
3697 {
3698 gfc_init_se (&se, NULL);
3699 gfc_conv_expr_reference (&se, d->high);
3700
3701 CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], se.expr);
3702 CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], se.string_length);
3703 }
3704
3705 CONSTRUCTOR_APPEND_ELT (node, ss_target[k],
3706 build_int_cst (integer_type_node, d->n));
3707
3708 tmp = build_constructor (select_struct[k], node);
3709 CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, tmp);
3710 }
3711
3712 type = build_array_type (select_struct[k],
3713 build_index_type (size_int (n-1)));
3714
3715 init = build_constructor (type, inits);
3716 TREE_CONSTANT (init) = 1;
3717 TREE_STATIC (init) = 1;
3718 /* Create a static variable to hold the jump table. */
3719 tmp = gfc_create_var (type, "jumptable");
3720 TREE_CONSTANT (tmp) = 1;
3721 TREE_STATIC (tmp) = 1;
3722 TREE_READONLY (tmp) = 1;
3723 DECL_INITIAL (tmp) = init;
3724 init = tmp;
3725
3726 /* Build the library call */
3727 init = gfc_build_addr_expr (pvoid_type_node, init);
3728
3729 if (code->expr1->ts.kind == 1)
3730 fndecl = gfor_fndecl_select_string;
3731 else if (code->expr1->ts.kind == 4)
3732 fndecl = gfor_fndecl_select_string_char4;
3733 else
3734 gcc_unreachable ();
3735
3736 tmp = build_call_expr_loc (input_location,
3737 fndecl, 4, init,
3738 build_int_cst (gfc_charlen_type_node, n),
3739 expr1se.expr, expr1se.string_length);
3740 case_num = gfc_create_var (integer_type_node, "case_num");
3741 gfc_add_modify (&block, case_num, tmp);
3742
3743 gfc_add_block_to_block (&block, &expr1se.post);
3744
3745 tmp = gfc_finish_block (&body);
3746 tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3747 case_num, tmp);
3748 gfc_add_expr_to_block (&block, tmp);
3749
3750 tmp = build1_v (LABEL_EXPR, end_label);
3751 gfc_add_expr_to_block (&block, tmp);
3752
3753 return gfc_finish_block (&block);
3754 }
3755
3756
3757 /* Translate the three variants of the SELECT CASE construct.
3758
3759 SELECT CASEs with INTEGER case expressions can be translated to an
3760 equivalent GENERIC switch statement, and for LOGICAL case
3761 expressions we build one or two if-else compares.
3762
3763 SELECT CASEs with CHARACTER case expressions are a whole different
3764 story, because they don't exist in GENERIC. So we sort them and
3765 do a binary search at runtime.
3766
3767 Fortran has no BREAK statement, and it does not allow jumps from
3768 one case block to another. That makes things a lot easier for
3769 the optimizers. */
3770
3771 tree
3772 gfc_trans_select (gfc_code * code)
3773 {
3774 stmtblock_t block;
3775 tree body;
3776 tree exit_label;
3777
3778 gcc_assert (code && code->expr1);
3779 gfc_init_block (&block);
3780
3781 /* Build the exit label and hang it in. */
3782 exit_label = gfc_build_label_decl (NULL_TREE);
3783 code->exit_label = exit_label;
3784
3785 /* Empty SELECT constructs are legal. */
3786 if (code->block == NULL)
3787 body = build_empty_stmt (input_location);
3788
3789 /* Select the correct translation function. */
3790 else
3791 switch (code->expr1->ts.type)
3792 {
3793 case BT_LOGICAL:
3794 body = gfc_trans_logical_select (code);
3795 break;
3796
3797 case BT_INTEGER:
3798 case BT_UNSIGNED:
3799 body = gfc_trans_integer_select (code);
3800 break;
3801
3802 case BT_CHARACTER:
3803 body = gfc_trans_character_select (code);
3804 break;
3805
3806 default:
3807 gfc_internal_error ("gfc_trans_select(): Bad type for case expr.");
3808 /* Not reached */
3809 }
3810
3811 /* Build everything together. */
3812 gfc_add_expr_to_block (&block, body);
3813 gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3814
3815 return gfc_finish_block (&block);
3816 }
3817
3818 tree
3819 gfc_trans_select_type (gfc_code * code)
3820 {
3821 stmtblock_t block;
3822 tree body;
3823 tree exit_label;
3824
3825 gcc_assert (code && code->expr1);
3826 gfc_init_block (&block);
3827
3828 /* Build the exit label and hang it in. */
3829 exit_label = gfc_build_label_decl (NULL_TREE);
3830 code->exit_label = exit_label;
3831
3832 /* Empty SELECT constructs are legal. */
3833 if (code->block == NULL)
3834 body = build_empty_stmt (input_location);
3835 else
3836 body = gfc_trans_select_type_cases (code);
3837
3838 /* Build everything together. */
3839 gfc_add_expr_to_block (&block, body);
3840
3841 if (TREE_USED (exit_label))
3842 gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3843
3844 return gfc_finish_block (&block);
3845 }
3846
3847
3848 static tree
3849 gfc_trans_select_rank_cases (gfc_code * code)
3850 {
3851 gfc_code *c;
3852 gfc_case *cp;
3853 tree tmp;
3854 tree cond;
3855 tree low;
3856 tree rank;
3857 gfc_se se;
3858 gfc_se cse;
3859 stmtblock_t block;
3860 stmtblock_t body;
3861 bool def = false;
3862
3863 gfc_start_block (&block);
3864
3865 /* Calculate the switch expression. */
3866 gfc_init_se (&se, NULL);
3867 gfc_conv_expr_descriptor (&se, code->expr1);
3868 rank = gfc_conv_descriptor_rank (se.expr);
3869 rank = gfc_evaluate_now (rank, &block);
3870 symbol_attribute attr = gfc_expr_attr (code->expr1);
3871 if (!attr.pointer && !attr.allocatable)
3872 {
3873 /* Special case for assumed-rank ('rank(*)', internally -1):
3874 rank = (rank == 0 || ubound[rank-1] != -1) ? rank : -1. */
3875 cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3876 rank, build_int_cst (TREE_TYPE (rank), 0));
3877 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
3878 fold_convert (gfc_array_index_type, rank),
3879 gfc_index_one_node);
3880 tmp = gfc_conv_descriptor_ubound_get (se.expr, tmp);
3881 tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
3882 tmp, build_int_cst (TREE_TYPE (tmp), -1));
3883 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3884 logical_type_node, cond, tmp);
3885 tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (rank),
3886 cond, rank, build_int_cst (TREE_TYPE (rank), -1));
3887 rank = gfc_evaluate_now (tmp, &block);
3888 }
3889 TREE_USED (code->exit_label) = 0;
3890
3891 repeat:
3892 for (c = code->block; c; c = c->block)
3893 {
3894 cp = c->ext.block.case_list;
3895
3896 /* Assume it's the default case. */
3897 low = NULL_TREE;
3898 tmp = NULL_TREE;
3899
3900 /* Put the default case at the end. */
3901 if ((!def && !cp->low) || (def && cp->low))
3902 continue;
3903
3904 if (cp->low)
3905 {
3906 gfc_init_se (&cse, NULL);
3907 gfc_conv_expr_val (&cse, cp->low);
3908 gfc_add_block_to_block (&block, &cse.pre);
3909 low = cse.expr;
3910 }
3911
3912 gfc_init_block (&body);
3913
3914 /* Add the statements for this case. */
3915 tmp = gfc_trans_code (c->next);
3916 gfc_add_expr_to_block (&body, tmp);
3917
3918 /* Break to the end of the SELECT RANK construct. The default
3919 case just falls through. */
3920 if (!def)
3921 {
3922 TREE_USED (code->exit_label) = 1;
3923 tmp = build1_v (GOTO_EXPR, code->exit_label);
3924 gfc_add_expr_to_block (&body, tmp);
3925 }
3926
3927 tmp = gfc_finish_block (&body);
3928
3929 if (low != NULL_TREE)
3930 {
3931 cond = fold_build2_loc (input_location, EQ_EXPR,
3932 TREE_TYPE (rank), rank,
3933 fold_convert (TREE_TYPE (rank), low));
3934 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3935 cond, tmp,
3936 build_empty_stmt (input_location));
3937 }
3938
3939 gfc_add_expr_to_block (&block, tmp);
3940 }
3941
3942 if (!def)
3943 {
3944 def = true;
3945 goto repeat;
3946 }
3947
3948 return gfc_finish_block (&block);
3949 }
3950
3951
3952 tree
3953 gfc_trans_select_rank (gfc_code * code)
3954 {
3955 stmtblock_t block;
3956 tree body;
3957 tree exit_label;
3958
3959 gcc_assert (code && code->expr1);
3960 gfc_init_block (&block);
3961
3962 /* Build the exit label and hang it in. */
3963 exit_label = gfc_build_label_decl (NULL_TREE);
3964 code->exit_label = exit_label;
3965
3966 /* Empty SELECT constructs are legal. */
3967 if (code->block == NULL)
3968 body = build_empty_stmt (input_location);
3969 else
3970 body = gfc_trans_select_rank_cases (code);
3971
3972 /* Build everything together. */
3973 gfc_add_expr_to_block (&block, body);
3974
3975 if (TREE_USED (exit_label))
3976 gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3977
3978 return gfc_finish_block (&block);
3979 }
3980
3981
3982 /* Traversal function to substitute a replacement symtree if the symbol
3983 in the expression is the same as that passed. f == 2 signals that
3984 that variable itself is not to be checked - only the references.
3985 This group of functions is used when the variable expression in a
3986 FORALL assignment has internal references. For example:
3987 FORALL (i = 1:4) p(p(i)) = i
3988 The only recourse here is to store a copy of 'p' for the index
3989 expression. */
3990
3991 static gfc_symtree *new_symtree;
3992 static gfc_symtree *old_symtree;
3993
3994 static bool
3995 forall_replace (gfc_expr *expr, gfc_symbol *sym, int *f)
3996 {
3997 if (expr->expr_type != EXPR_VARIABLE)
3998 return false;
3999
4000 if (*f == 2)
4001 *f = 1;
4002 else if (expr->symtree->n.sym == sym)
4003 expr->symtree = new_symtree;
4004
4005 return false;
4006 }
4007
4008 static void
4009 forall_replace_symtree (gfc_expr *e, gfc_symbol *sym, int f)
4010 {
4011 gfc_traverse_expr (e, sym, forall_replace, f);
4012 }
4013
4014 static bool
4015 forall_restore (gfc_expr *expr,
4016 gfc_symbol *sym ATTRIBUTE_UNUSED,
4017 int *f ATTRIBUTE_UNUSED)
4018 {
4019 if (expr->expr_type != EXPR_VARIABLE)
4020 return false;
4021
4022 if (expr->symtree == new_symtree)
4023 expr->symtree = old_symtree;
4024
4025 return false;
4026 }
4027
4028 static void
4029 forall_restore_symtree (gfc_expr *e)
4030 {
4031 gfc_traverse_expr (e, NULL, forall_restore, 0);
4032 }
4033
4034 static void
4035 forall_make_variable_temp (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4036 {
4037 gfc_se tse;
4038 gfc_se rse;
4039 gfc_expr *e;
4040 gfc_symbol *new_sym;
4041 gfc_symbol *old_sym;
4042 gfc_symtree *root;
4043 tree tmp;
4044
4045 /* Build a copy of the lvalue. */
4046 old_symtree = c->expr1->symtree;
4047 old_sym = old_symtree->n.sym;
4048 e = gfc_lval_expr_from_sym (old_sym);
4049 if (old_sym->attr.dimension)
4050 {
4051 gfc_init_se (&tse, NULL);
4052 gfc_conv_subref_array_arg (&tse, e, 0, INTENT_IN, false);
4053 gfc_add_block_to_block (pre, &tse.pre);
4054 gfc_add_block_to_block (post, &tse.post);
4055 tse.expr = build_fold_indirect_ref_loc (input_location, tse.expr);
4056
4057 if (c->expr1->ref->u.ar.type != AR_SECTION)
4058 {
4059 /* Use the variable offset for the temporary. */
4060 tmp = gfc_conv_array_offset (old_sym->backend_decl);
4061 gfc_conv_descriptor_offset_set (pre, tse.expr, tmp);
4062 }
4063 }
4064 else
4065 {
4066 gfc_init_se (&tse, NULL);
4067 gfc_init_se (&rse, NULL);
4068 gfc_conv_expr (&rse, e);
4069 if (e->ts.type == BT_CHARACTER)
4070 {
4071 tse.string_length = rse.string_length;
4072 tmp = gfc_get_character_type_len (gfc_default_character_kind,
4073 tse.string_length);
4074 tse.expr = gfc_conv_string_tmp (&tse, build_pointer_type (tmp),
4075 rse.string_length);
4076 gfc_add_block_to_block (pre, &tse.pre);
4077 gfc_add_block_to_block (post, &tse.post);
4078 }
4079 else
4080 {
4081 tmp = gfc_typenode_for_spec (&e->ts);
4082 tse.expr = gfc_create_var (tmp, "temp");
4083 }
4084
4085 tmp = gfc_trans_scalar_assign (&tse, &rse, e->ts,
4086 e->expr_type == EXPR_VARIABLE, false);
4087 gfc_add_expr_to_block (pre, tmp);
4088 }
4089 gfc_free_expr (e);
4090
4091 /* Create a new symbol to represent the lvalue. */
4092 new_sym = gfc_new_symbol (old_sym->name, NULL);
4093 new_sym->ts = old_sym->ts;
4094 new_sym->attr.referenced = 1;
4095 new_sym->attr.temporary = 1;
4096 new_sym->attr.dimension = old_sym->attr.dimension;
4097 new_sym->attr.flavor = old_sym->attr.flavor;
4098
4099 /* Use the temporary as the backend_decl. */
4100 new_sym->backend_decl = tse.expr;
4101
4102 /* Create a fake symtree for it. */
4103 root = NULL;
4104 new_symtree = gfc_new_symtree (&root, old_sym->name);
4105 new_symtree->n.sym = new_sym;
4106 gcc_assert (new_symtree == root);
4107
4108 /* Go through the expression reference replacing the old_symtree
4109 with the new. */
4110 forall_replace_symtree (c->expr1, old_sym, 2);
4111
4112 /* Now we have made this temporary, we might as well use it for
4113 the right hand side. */
4114 forall_replace_symtree (c->expr2, old_sym, 1);
4115 }
4116
4117
4118 /* Handles dependencies in forall assignments. */
4119 static int
4120 check_forall_dependencies (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4121 {
4122 gfc_ref *lref;
4123 gfc_ref *rref;
4124 int need_temp;
4125 gfc_symbol *lsym;
4126
4127 lsym = c->expr1->symtree->n.sym;
4128 need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
4129
4130 /* Now check for dependencies within the 'variable'
4131 expression itself. These are treated by making a complete
4132 copy of variable and changing all the references to it
4133 point to the copy instead. Note that the shallow copy of
4134 the variable will not suffice for derived types with
4135 pointer components. We therefore leave these to their
4136 own devices. Likewise for allocatable components. */
4137 if (lsym->ts.type == BT_DERIVED
4138 && (lsym->ts.u.derived->attr.pointer_comp
4139 || lsym->ts.u.derived->attr.alloc_comp))
4140 return need_temp;
4141
4142 new_symtree = NULL;
4143 if (find_forall_index (c->expr1, lsym, 2))
4144 {
4145 forall_make_variable_temp (c, pre, post);
4146 need_temp = 0;
4147 }
4148
4149 /* Substrings with dependencies are treated in the same
4150 way. */
4151 if (c->expr1->ts.type == BT_CHARACTER
4152 && c->expr1->ref
4153 && c->expr2->expr_type == EXPR_VARIABLE
4154 && lsym == c->expr2->symtree->n.sym)
4155 {
4156 for (lref = c->expr1->ref; lref; lref = lref->next)
4157 if (lref->type == REF_SUBSTRING)
4158 break;
4159 for (rref = c->expr2->ref; rref; rref = rref->next)
4160 if (rref->type == REF_SUBSTRING)
4161 break;
4162
4163 if (rref && lref
4164 && gfc_dep_compare_expr (rref->u.ss.start, lref->u.ss.start) < 0)
4165 {
4166 forall_make_variable_temp (c, pre, post);
4167 need_temp = 0;
4168 }
4169 }
4170 return need_temp;
4171 }
4172
4173
4174 static void
4175 cleanup_forall_symtrees (gfc_code *c)
4176 {
4177 forall_restore_symtree (c->expr1);
4178 forall_restore_symtree (c->expr2);
4179 free (new_symtree->n.sym);
4180 free (new_symtree);
4181 }
4182
4183
4184 /* Generate the loops for a FORALL block, specified by FORALL_TMP. BODY
4185 is the contents of the FORALL block/stmt to be iterated. MASK_FLAG
4186 indicates whether we should generate code to test the FORALLs mask
4187 array. OUTER is the loop header to be used for initializing mask
4188 indices.
4189
4190 The generated loop format is:
4191 count = (end - start + step) / step
4192 loopvar = start
4193 while (1)
4194 {
4195 if (count <=0 )
4196 goto end_of_loop
4197 <body>
4198 loopvar += step
4199 count --
4200 }
4201 end_of_loop: */
4202
4203 static tree
4204 gfc_trans_forall_loop (forall_info *forall_tmp, tree body,
4205 int mask_flag, stmtblock_t *outer)
4206 {
4207 int n, nvar;
4208 tree tmp;
4209 tree cond;
4210 stmtblock_t block;
4211 tree exit_label;
4212 tree count;
4213 tree var, start, end, step;
4214 iter_info *iter;
4215
4216 /* Initialize the mask index outside the FORALL nest. */
4217 if (mask_flag && forall_tmp->mask)
4218 gfc_add_modify (outer, forall_tmp->maskindex, gfc_index_zero_node);
4219
4220 iter = forall_tmp->this_loop;
4221 nvar = forall_tmp->nvar;
4222 for (n = 0; n < nvar; n++)
4223 {
4224 var = iter->var;
4225 start = iter->start;
4226 end = iter->end;
4227 step = iter->step;
4228
4229 exit_label = gfc_build_label_decl (NULL_TREE);
4230 TREE_USED (exit_label) = 1;
4231
4232 /* The loop counter. */
4233 count = gfc_create_var (TREE_TYPE (var), "count");
4234
4235 /* The body of the loop. */
4236 gfc_init_block (&block);
4237
4238 /* The exit condition. */
4239 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
4240 count, build_int_cst (TREE_TYPE (count), 0));
4241
4242 /* PR 83064 means that we cannot use annot_expr_parallel_kind until
4243 the autoparallelizer can handle this. */
4244 if (forall_tmp->do_concurrent || iter->annot.ivdep)
4245 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4246 build_int_cst (integer_type_node,
4247 annot_expr_ivdep_kind),
4248 integer_zero_node);
4249
4250 if (iter->annot.unroll && cond != error_mark_node)
4251 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4252 build_int_cst (integer_type_node,
4253 annot_expr_unroll_kind),
4254 build_int_cst (integer_type_node, iter->annot.unroll));
4255
4256 if (iter->annot.vector && cond != error_mark_node)
4257 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4258 build_int_cst (integer_type_node,
4259 annot_expr_vector_kind),
4260 integer_zero_node);
4261
4262 if (iter->annot.novector && cond != error_mark_node)
4263 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4264 build_int_cst (integer_type_node,
4265 annot_expr_no_vector_kind),
4266 integer_zero_node);
4267
4268 tmp = build1_v (GOTO_EXPR, exit_label);
4269 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4270 cond, tmp, build_empty_stmt (input_location));
4271 gfc_add_expr_to_block (&block, tmp);
4272
4273 /* The main loop body. */
4274 gfc_add_expr_to_block (&block, body);
4275
4276 /* Increment the loop variable. */
4277 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var,
4278 step);
4279 gfc_add_modify (&block, var, tmp);
4280
4281 /* Advance to the next mask element. Only do this for the
4282 innermost loop. */
4283 if (n == 0 && mask_flag && forall_tmp->mask)
4284 {
4285 tree maskindex = forall_tmp->maskindex;
4286 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4287 maskindex, gfc_index_one_node);
4288 gfc_add_modify (&block, maskindex, tmp);
4289 }
4290
4291 /* Decrement the loop counter. */
4292 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), count,
4293 build_int_cst (TREE_TYPE (var), 1));
4294 gfc_add_modify (&block, count, tmp);
4295
4296 body = gfc_finish_block (&block);
4297
4298 /* Loop var initialization. */
4299 gfc_init_block (&block);
4300 gfc_add_modify (&block, var, start);
4301
4302
4303 /* Initialize the loop counter. */
4304 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), step,
4305 start);
4306 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), end,
4307 tmp);
4308 tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (var),
4309 tmp, step);
4310 gfc_add_modify (&block, count, tmp);
4311
4312 /* The loop expression. */
4313 tmp = build1_v (LOOP_EXPR, body);
4314 gfc_add_expr_to_block (&block, tmp);
4315
4316 /* The exit label. */
4317 tmp = build1_v (LABEL_EXPR, exit_label);
4318 gfc_add_expr_to_block (&block, tmp);
4319
4320 body = gfc_finish_block (&block);
4321 iter = iter->next;
4322 }
4323 return body;
4324 }
4325
4326
4327 /* Generate the body and loops according to MASK_FLAG. If MASK_FLAG
4328 is nonzero, the body is controlled by all masks in the forall nest.
4329 Otherwise, the innermost loop is not controlled by it's mask. This
4330 is used for initializing that mask. */
4331
4332 static tree
4333 gfc_trans_nested_forall_loop (forall_info * nested_forall_info, tree body,
4334 int mask_flag)
4335 {
4336 tree tmp;
4337 stmtblock_t header;
4338 forall_info *forall_tmp;
4339 tree mask, maskindex;
4340
4341 gfc_start_block (&header);
4342
4343 forall_tmp = nested_forall_info;
4344 while (forall_tmp != NULL)
4345 {
4346 /* Generate body with masks' control. */
4347 if (mask_flag)
4348 {
4349 mask = forall_tmp->mask;
4350 maskindex = forall_tmp->maskindex;
4351
4352 /* If a mask was specified make the assignment conditional. */
4353 if (mask)
4354 {
4355 tmp = gfc_build_array_ref (mask, maskindex, NULL);
4356 body = build3_v (COND_EXPR, tmp, body,
4357 build_empty_stmt (input_location));
4358 }
4359 }
4360 body = gfc_trans_forall_loop (forall_tmp, body, mask_flag, &header);
4361 forall_tmp = forall_tmp->prev_nest;
4362 mask_flag = 1;
4363 }
4364
4365 gfc_add_expr_to_block (&header, body);
4366 return gfc_finish_block (&header);
4367 }
4368
4369
4370 /* Allocate data for holding a temporary array. Returns either a local
4371 temporary array or a pointer variable. */
4372
4373 static tree
4374 gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
4375 tree elem_type)
4376 {
4377 tree tmpvar;
4378 tree type;
4379 tree tmp;
4380
4381 if (INTEGER_CST_P (size))
4382 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4383 size, gfc_index_one_node);
4384 else
4385 tmp = NULL_TREE;
4386
4387 type = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
4388 type = build_array_type (elem_type, type);
4389 if (gfc_can_put_var_on_stack (bytesize) && INTEGER_CST_P (size))
4390 {
4391 tmpvar = gfc_create_var (type, "temp");
4392 *pdata = NULL_TREE;
4393 }
4394 else
4395 {
4396 tmpvar = gfc_create_var (build_pointer_type (type), "temp");
4397 *pdata = convert (pvoid_type_node, tmpvar);
4398
4399 tmp = gfc_call_malloc (pblock, TREE_TYPE (tmpvar), bytesize);
4400 gfc_add_modify (pblock, tmpvar, tmp);
4401 }
4402 return tmpvar;
4403 }
4404
4405
4406 /* Generate codes to copy the temporary to the actual lhs. */
4407
4408 static tree
4409 generate_loop_for_temp_to_lhs (gfc_expr *expr, tree tmp1, tree count3,
4410 tree count1,
4411 gfc_ss *lss, gfc_ss *rss,
4412 tree wheremask, bool invert)
4413 {
4414 stmtblock_t block, body1;
4415 gfc_loopinfo loop;
4416 gfc_se lse;
4417 gfc_se rse;
4418 tree tmp;
4419 tree wheremaskexpr;
4420
4421 (void) rss; /* TODO: unused. */
4422
4423 gfc_start_block (&block);
4424
4425 gfc_init_se (&rse, NULL);
4426 gfc_init_se (&lse, NULL);
4427
4428 if (lss == gfc_ss_terminator)
4429 {
4430 gfc_init_block (&body1);
4431 gfc_conv_expr (&lse, expr);
4432 rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4433 }
4434 else
4435 {
4436 /* Initialize the loop. */
4437 gfc_init_loopinfo (&loop);
4438
4439 /* We may need LSS to determine the shape of the expression. */
4440 gfc_add_ss_to_loop (&loop, lss);
4441
4442 gfc_conv_ss_startstride (&loop);
4443 gfc_conv_loop_setup (&loop, &expr->where);
4444
4445 gfc_mark_ss_chain_used (lss, 1);
4446 /* Start the loop body. */
4447 gfc_start_scalarized_body (&loop, &body1);
4448
4449 /* Translate the expression. */
4450 gfc_copy_loopinfo_to_se (&lse, &loop);
4451 lse.ss = lss;
4452 gfc_conv_expr (&lse, expr);
4453
4454 /* Form the expression of the temporary. */
4455 rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4456 }
4457
4458 /* Use the scalar assignment. */
4459 rse.string_length = lse.string_length;
4460 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
4461 expr->expr_type == EXPR_VARIABLE, false);
4462
4463 /* Form the mask expression according to the mask tree list. */
4464 if (wheremask)
4465 {
4466 wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4467 if (invert)
4468 wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4469 TREE_TYPE (wheremaskexpr),
4470 wheremaskexpr);
4471 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4472 wheremaskexpr, tmp,
4473 build_empty_stmt (input_location));
4474 }
4475
4476 gfc_add_expr_to_block (&body1, tmp);
4477
4478 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4479 count1, gfc_index_one_node);
4480 gfc_add_modify (&body1, count1, tmp);
4481
4482 if (lss == gfc_ss_terminator)
4483 gfc_add_block_to_block (&block, &body1);
4484 else
4485 {
4486 /* Increment count3. */
4487 if (count3)
4488 {
4489 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4490 gfc_array_index_type,
4491 count3, gfc_index_one_node);
4492 gfc_add_modify (&body1, count3, tmp);
4493 }
4494
4495 /* Generate the copying loops. */
4496 gfc_trans_scalarizing_loops (&loop, &body1);
4497
4498 gfc_add_block_to_block (&block, &loop.pre);
4499 gfc_add_block_to_block (&block, &loop.post);
4500
4501 gfc_cleanup_loop (&loop);
4502 /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
4503 as tree nodes in SS may not be valid in different scope. */
4504 }
4505
4506 tmp = gfc_finish_block (&block);
4507 return tmp;
4508 }
4509
4510
4511 /* Generate codes to copy rhs to the temporary. TMP1 is the address of
4512 temporary, LSS and RSS are formed in function compute_inner_temp_size(),
4513 and should not be freed. WHEREMASK is the conditional execution mask
4514 whose sense may be inverted by INVERT. */
4515
4516 static tree
4517 generate_loop_for_rhs_to_temp (gfc_expr *expr2, tree tmp1, tree count3,
4518 tree count1, gfc_ss *lss, gfc_ss *rss,
4519 tree wheremask, bool invert)
4520 {
4521 stmtblock_t block, body1;
4522 gfc_loopinfo loop;
4523 gfc_se lse;
4524 gfc_se rse;
4525 tree tmp;
4526 tree wheremaskexpr;
4527
4528 gfc_start_block (&block);
4529
4530 gfc_init_se (&rse, NULL);
4531 gfc_init_se (&lse, NULL);
4532
4533 if (lss == gfc_ss_terminator)
4534 {
4535 gfc_init_block (&body1);
4536 gfc_conv_expr (&rse, expr2);
4537 lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4538 }
4539 else
4540 {
4541 /* Initialize the loop. */
4542 gfc_init_loopinfo (&loop);
4543
4544 /* We may need LSS to determine the shape of the expression. */
4545 gfc_add_ss_to_loop (&loop, lss);
4546 gfc_add_ss_to_loop (&loop, rss);
4547
4548 gfc_conv_ss_startstride (&loop);
4549 gfc_conv_loop_setup (&loop, &expr2->where);
4550
4551 gfc_mark_ss_chain_used (rss, 1);
4552 /* Start the loop body. */
4553 gfc_start_scalarized_body (&loop, &body1);
4554
4555 /* Translate the expression. */
4556 gfc_copy_loopinfo_to_se (&rse, &loop);
4557 rse.ss = rss;
4558 gfc_conv_expr (&rse, expr2);
4559
4560 /* Form the expression of the temporary. */
4561 lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4562 }
4563
4564 /* Use the scalar assignment. */
4565 lse.string_length = rse.string_length;
4566 tmp = gfc_trans_scalar_assign (&lse, &rse, expr2->ts,
4567 expr2->expr_type == EXPR_VARIABLE, false);
4568
4569 /* Form the mask expression according to the mask tree list. */
4570 if (wheremask)
4571 {
4572 wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4573 if (invert)
4574 wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4575 TREE_TYPE (wheremaskexpr),
4576 wheremaskexpr);
4577 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4578 wheremaskexpr, tmp,
4579 build_empty_stmt (input_location));
4580 }
4581
4582 gfc_add_expr_to_block (&body1, tmp);
4583
4584 if (lss == gfc_ss_terminator)
4585 {
4586 gfc_add_block_to_block (&block, &body1);
4587
4588 /* Increment count1. */
4589 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4590 count1, gfc_index_one_node);
4591 gfc_add_modify (&block, count1, tmp);
4592 }
4593 else
4594 {
4595 /* Increment count1. */
4596 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4597 count1, gfc_index_one_node);
4598 gfc_add_modify (&body1, count1, tmp);
4599
4600 /* Increment count3. */
4601 if (count3)
4602 {
4603 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4604 gfc_array_index_type,
4605 count3, gfc_index_one_node);
4606 gfc_add_modify (&body1, count3, tmp);
4607 }
4608
4609 /* Generate the copying loops. */
4610 gfc_trans_scalarizing_loops (&loop, &body1);
4611
4612 gfc_add_block_to_block (&block, &loop.pre);
4613 gfc_add_block_to_block (&block, &loop.post);
4614
4615 gfc_cleanup_loop (&loop);
4616 /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
4617 as tree nodes in SS may not be valid in different scope. */
4618 }
4619
4620 tmp = gfc_finish_block (&block);
4621 return tmp;
4622 }
4623
4624
4625 /* Calculate the size of temporary needed in the assignment inside forall.
4626 LSS and RSS are filled in this function. */
4627
4628 static tree
4629 compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
4630 stmtblock_t * pblock,
4631 gfc_ss **lss, gfc_ss **rss)
4632 {
4633 gfc_loopinfo loop;
4634 tree size;
4635 int i;
4636 int save_flag;
4637 tree tmp;
4638
4639 *lss = gfc_walk_expr (expr1);
4640 *rss = NULL;
4641
4642 size = gfc_index_one_node;
4643 if (*lss != gfc_ss_terminator)
4644 {
4645 gfc_init_loopinfo (&loop);
4646
4647 /* Walk the RHS of the expression. */
4648 *rss = gfc_walk_expr (expr2);
4649 if (*rss == gfc_ss_terminator)
4650 /* The rhs is scalar. Add a ss for the expression. */
4651 *rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
4652
4653 /* Associate the SS with the loop. */
4654 gfc_add_ss_to_loop (&loop, *lss);
4655 /* We don't actually need to add the rhs at this point, but it might
4656 make guessing the loop bounds a bit easier. */
4657 gfc_add_ss_to_loop (&loop, *rss);
4658
4659 /* We only want the shape of the expression, not rest of the junk
4660 generated by the scalarizer. */
4661 loop.array_parameter = 1;
4662
4663 /* Calculate the bounds of the scalarization. */
4664 save_flag = gfc_option.rtcheck;
4665 gfc_option.rtcheck &= ~GFC_RTCHECK_BOUNDS;
4666 gfc_conv_ss_startstride (&loop);
4667 gfc_option.rtcheck = save_flag;
4668 gfc_conv_loop_setup (&loop, &expr2->where);
4669
4670 /* Figure out how many elements we need. */
4671 for (i = 0; i < loop.dimen; i++)
4672 {
4673 tmp = fold_build2_loc (input_location, MINUS_EXPR,
4674 gfc_array_index_type,
4675 gfc_index_one_node, loop.from[i]);
4676 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4677 gfc_array_index_type, tmp, loop.to[i]);
4678 size = fold_build2_loc (input_location, MULT_EXPR,
4679 gfc_array_index_type, size, tmp);
4680 }
4681 gfc_add_block_to_block (pblock, &loop.pre);
4682 size = gfc_evaluate_now (size, pblock);
4683 gfc_add_block_to_block (pblock, &loop.post);
4684
4685 /* TODO: write a function that cleans up a loopinfo without freeing
4686 the SS chains. Currently a NOP. */
4687 }
4688
4689 return size;
4690 }
4691
4692
4693 /* Calculate the overall iterator number of the nested forall construct.
4694 This routine actually calculates the number of times the body of the
4695 nested forall specified by NESTED_FORALL_INFO is executed and multiplies
4696 that by the expression INNER_SIZE. The BLOCK argument specifies the
4697 block in which to calculate the result, and the optional INNER_SIZE_BODY
4698 argument contains any statements that need to executed (inside the loop)
4699 to initialize or calculate INNER_SIZE. */
4700
4701 static tree
4702 compute_overall_iter_number (forall_info *nested_forall_info, tree inner_size,
4703 stmtblock_t *inner_size_body, stmtblock_t *block)
4704 {
4705 forall_info *forall_tmp = nested_forall_info;
4706 tree tmp, number;
4707 stmtblock_t body;
4708
4709 /* We can eliminate the innermost unconditional loops with constant
4710 array bounds. */
4711 if (INTEGER_CST_P (inner_size))
4712 {
4713 while (forall_tmp
4714 && !forall_tmp->mask
4715 && INTEGER_CST_P (forall_tmp->size))
4716 {
4717 inner_size = fold_build2_loc (input_location, MULT_EXPR,
4718 gfc_array_index_type,
4719 inner_size, forall_tmp->size);
4720 forall_tmp = forall_tmp->prev_nest;
4721 }
4722
4723 /* If there are no loops left, we have our constant result. */
4724 if (!forall_tmp)
4725 return inner_size;
4726 }
4727
4728 /* Otherwise, create a temporary variable to compute the result. */
4729 number = gfc_create_var (gfc_array_index_type, "num");
4730 gfc_add_modify (block, number, gfc_index_zero_node);
4731
4732 gfc_start_block (&body);
4733 if (inner_size_body)
4734 gfc_add_block_to_block (&body, inner_size_body);
4735 if (forall_tmp)
4736 tmp = fold_build2_loc (input_location, PLUS_EXPR,
4737 gfc_array_index_type, number, inner_size);
4738 else
4739 tmp = inner_size;
4740 gfc_add_modify (&body, number, tmp);
4741 tmp = gfc_finish_block (&body);
4742
4743 /* Generate loops. */
4744 if (forall_tmp != NULL)
4745 tmp = gfc_trans_nested_forall_loop (forall_tmp, tmp, 1);
4746
4747 gfc_add_expr_to_block (block, tmp);
4748
4749 return number;
4750 }
4751
4752
4753 /* Allocate temporary for forall construct. SIZE is the size of temporary
4754 needed. PTEMP1 is returned for space free. */
4755
4756 static tree
4757 allocate_temp_for_forall_nest_1 (tree type, tree size, stmtblock_t * block,
4758 tree * ptemp1)
4759 {
4760 tree bytesize;
4761 tree unit;
4762 tree tmp;
4763
4764 unit = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (type));
4765 if (!integer_onep (unit))
4766 bytesize = fold_build2_loc (input_location, MULT_EXPR,
4767 gfc_array_index_type, size, unit);
4768 else
4769 bytesize = size;
4770
4771 *ptemp1 = NULL;
4772 tmp = gfc_do_allocate (bytesize, size, ptemp1, block, type);
4773
4774 if (*ptemp1)
4775 tmp = build_fold_indirect_ref_loc (input_location, tmp);
4776 return tmp;
4777 }
4778
4779
4780 /* Allocate temporary for forall construct according to the information in
4781 nested_forall_info. INNER_SIZE is the size of temporary needed in the
4782 assignment inside forall. PTEMP1 is returned for space free. */
4783
4784 static tree
4785 allocate_temp_for_forall_nest (forall_info * nested_forall_info, tree type,
4786 tree inner_size, stmtblock_t * inner_size_body,
4787 stmtblock_t * block, tree * ptemp1)
4788 {
4789 tree size;
4790
4791 /* Calculate the total size of temporary needed in forall construct. */
4792 size = compute_overall_iter_number (nested_forall_info, inner_size,
4793 inner_size_body, block);
4794
4795 return allocate_temp_for_forall_nest_1 (type, size, block, ptemp1);
4796 }
4797
4798
4799 /* Handle assignments inside forall which need temporary.
4800
4801 forall (i=start:end:stride; maskexpr)
4802 e<i> = f<i>
4803 end forall
4804 (where e,f<i> are arbitrary expressions possibly involving i
4805 and there is a dependency between e<i> and f<i>)
4806 Translates to:
4807 masktmp(:) = maskexpr(:)
4808
4809 maskindex = 0;
4810 count1 = 0;
4811 num = 0;
4812 for (i = start; i <= end; i += stride)
4813 num += SIZE (f<i>)
4814 count1 = 0;
4815 ALLOCATE (tmp(num))
4816 for (i = start; i <= end; i += stride)
4817 {
4818 if (masktmp[maskindex++])
4819 tmp[count1++] = f<i>
4820 }
4821 maskindex = 0;
4822 count1 = 0;
4823 for (i = start; i <= end; i += stride)
4824 {
4825 if (masktmp[maskindex++])
4826 e<i> = tmp[count1++]
4827 }
4828 DEALLOCATE (tmp)
4829 */
4830 static void
4831 gfc_trans_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
4832 tree wheremask, bool invert,
4833 forall_info * nested_forall_info,
4834 stmtblock_t * block)
4835 {
4836 tree type;
4837 tree inner_size;
4838 gfc_ss *lss, *rss;
4839 tree count, count1;
4840 tree tmp, tmp1;
4841 tree ptemp1;
4842 stmtblock_t inner_size_body;
4843
4844 /* Create vars. count1 is the current iterator number of the nested
4845 forall. */
4846 count1 = gfc_create_var (gfc_array_index_type, "count1");
4847
4848 /* Count is the wheremask index. */
4849 if (wheremask)
4850 {
4851 count = gfc_create_var (gfc_array_index_type, "count");
4852 gfc_add_modify (block, count, gfc_index_zero_node);
4853 }
4854 else
4855 count = NULL;
4856
4857 /* Initialize count1. */
4858 gfc_add_modify (block, count1, gfc_index_zero_node);
4859
4860 /* Calculate the size of temporary needed in the assignment. Return loop, lss
4861 and rss which are used in function generate_loop_for_rhs_to_temp(). */
4862 /* The type of LHS. Used in function allocate_temp_for_forall_nest */
4863 if (expr1->ts.type == BT_CHARACTER)
4864 {
4865 type = NULL;
4866 if (expr1->ref && expr1->ref->type == REF_SUBSTRING)
4867 {
4868 gfc_se ssse;
4869 gfc_init_se (&ssse, NULL);
4870 gfc_conv_expr (&ssse, expr1);
4871 type = gfc_get_character_type_len (gfc_default_character_kind,
4872 ssse.string_length);
4873 }
4874 else
4875 {
4876 if (!expr1->ts.u.cl->backend_decl)
4877 {
4878 gfc_se tse;
4879 gcc_assert (expr1->ts.u.cl->length);
4880 gfc_init_se (&tse, NULL);
4881 gfc_conv_expr (&tse, expr1->ts.u.cl->length);
4882 expr1->ts.u.cl->backend_decl = tse.expr;
4883 }
4884 type = gfc_get_character_type_len (gfc_default_character_kind,
4885 expr1->ts.u.cl->backend_decl);
4886 }
4887 }
4888 else
4889 type = gfc_typenode_for_spec (&expr1->ts);
4890
4891 gfc_init_block (&inner_size_body);
4892 inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
4893 &lss, &rss);
4894
4895 /* Allocate temporary for nested forall construct according to the
4896 information in nested_forall_info and inner_size. */
4897 tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type, inner_size,
4898 &inner_size_body, block, &ptemp1);
4899
4900 /* Generate codes to copy rhs to the temporary . */
4901 tmp = generate_loop_for_rhs_to_temp (expr2, tmp1, count, count1, lss, rss,
4902 wheremask, invert);
4903
4904 /* Generate body and loops according to the information in
4905 nested_forall_info. */
4906 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4907 gfc_add_expr_to_block (block, tmp);
4908
4909 /* Reset count1. */
4910 gfc_add_modify (block, count1, gfc_index_zero_node);
4911
4912 /* Reset count. */
4913 if (wheremask)
4914 gfc_add_modify (block, count, gfc_index_zero_node);
4915
4916 /* TODO: Second call to compute_inner_temp_size to initialize lss and
4917 rss; there must be a better way. */
4918 inner_size = compute_inner_temp_size (expr1, expr2, &inner_size_body,
4919 &lss, &rss);
4920
4921 /* Generate codes to copy the temporary to lhs. */
4922 tmp = generate_loop_for_temp_to_lhs (expr1, tmp1, count, count1,
4923 lss, rss,
4924 wheremask, invert);
4925
4926 /* Generate body and loops according to the information in
4927 nested_forall_info. */
4928 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4929 gfc_add_expr_to_block (block, tmp);
4930
4931 if (ptemp1)
4932 {
4933 /* Free the temporary. */
4934 tmp = gfc_call_free (ptemp1);
4935 gfc_add_expr_to_block (block, tmp);
4936 }
4937 }
4938
4939
4940 /* Translate pointer assignment inside FORALL which need temporary. */
4941
4942 static void
4943 gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
4944 forall_info * nested_forall_info,
4945 stmtblock_t * block)
4946 {
4947 tree type;
4948 tree inner_size;
4949 gfc_ss *lss, *rss;
4950 gfc_se lse;
4951 gfc_se rse;
4952 gfc_array_info *info;
4953 gfc_loopinfo loop;
4954 tree desc;
4955 tree parm;
4956 tree parmtype;
4957 stmtblock_t body;
4958 tree count;
4959 tree tmp, tmp1, ptemp1;
4960
4961 count = gfc_create_var (gfc_array_index_type, "count");
4962 gfc_add_modify (block, count, gfc_index_zero_node);
4963
4964 inner_size = gfc_index_one_node;
4965 lss = gfc_walk_expr (expr1);
4966 rss = gfc_walk_expr (expr2);
4967 if (lss == gfc_ss_terminator)
4968 {
4969 type = gfc_typenode_for_spec (&expr1->ts);
4970 type = build_pointer_type (type);
4971
4972 /* Allocate temporary for nested forall construct according to the
4973 information in nested_forall_info and inner_size. */
4974 tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type,
4975 inner_size, NULL, block, &ptemp1);
4976 gfc_start_block (&body);
4977 gfc_init_se (&lse, NULL);
4978 lse.expr = gfc_build_array_ref (tmp1, count, NULL);
4979 gfc_init_se (&rse, NULL);
4980 rse.want_pointer = 1;
4981 gfc_conv_expr (&rse, expr2);
4982 gfc_add_block_to_block (&body, &rse.pre);
4983 gfc_add_modify (&body, lse.expr,
4984 fold_convert (TREE_TYPE (lse.expr), rse.expr));
4985 gfc_add_block_to_block (&body, &rse.post);
4986
4987 /* Increment count. */
4988 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4989 count, gfc_index_one_node);
4990 gfc_add_modify (&body, count, tmp);
4991
4992 tmp = gfc_finish_block (&body);
4993
4994 /* Generate body and loops according to the information in
4995 nested_forall_info. */
4996 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4997 gfc_add_expr_to_block (block, tmp);
4998
4999 /* Reset count. */
5000 gfc_add_modify (block, count, gfc_index_zero_node);
5001
5002 gfc_start_block (&body);
5003 gfc_init_se (&lse, NULL);
5004 gfc_init_se (&rse, NULL);
5005 rse.expr = gfc_build_array_ref (tmp1, count, NULL);
5006 lse.want_pointer = 1;
5007 gfc_conv_expr (&lse, expr1);
5008 gfc_add_block_to_block (&body, &lse.pre);
5009 gfc_add_modify (&body, lse.expr, rse.expr);
5010 gfc_add_block_to_block (&body, &lse.post);
5011 /* Increment count. */
5012 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5013 count, gfc_index_one_node);
5014 gfc_add_modify (&body, count, tmp);
5015 tmp = gfc_finish_block (&body);
5016
5017 /* Generate body and loops according to the information in
5018 nested_forall_info. */
5019 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5020 gfc_add_expr_to_block (block, tmp);
5021 }
5022 else
5023 {
5024 gfc_init_loopinfo (&loop);
5025
5026 /* Associate the SS with the loop. */
5027 gfc_add_ss_to_loop (&loop, rss);
5028
5029 /* Setup the scalarizing loops and bounds. */
5030 gfc_conv_ss_startstride (&loop);
5031
5032 gfc_conv_loop_setup (&loop, &expr2->where);
5033
5034 info = &rss->info->data.array;
5035 desc = info->descriptor;
5036
5037 /* Make a new descriptor. */
5038 parmtype = gfc_get_element_type (TREE_TYPE (desc));
5039 parmtype = gfc_get_array_type_bounds (parmtype, loop.dimen, 0,
5040 loop.from, loop.to, 1,
5041 GFC_ARRAY_UNKNOWN, true);
5042
5043 /* Allocate temporary for nested forall construct. */
5044 tmp1 = allocate_temp_for_forall_nest (nested_forall_info, parmtype,
5045 inner_size, NULL, block, &ptemp1);
5046 gfc_start_block (&body);
5047 gfc_init_se (&lse, NULL);
5048 lse.expr = gfc_build_array_ref (tmp1, count, NULL);
5049 lse.direct_byref = 1;
5050 gfc_conv_expr_descriptor (&lse, expr2);
5051
5052 gfc_add_block_to_block (&body, &lse.pre);
5053 gfc_add_block_to_block (&body, &lse.post);
5054
5055 /* Increment count. */
5056 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5057 count, gfc_index_one_node);
5058 gfc_add_modify (&body, count, tmp);
5059
5060 tmp = gfc_finish_block (&body);
5061
5062 /* Generate body and loops according to the information in
5063 nested_forall_info. */
5064 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5065 gfc_add_expr_to_block (block, tmp);
5066
5067 /* Reset count. */
5068 gfc_add_modify (block, count, gfc_index_zero_node);
5069
5070 parm = gfc_build_array_ref (tmp1, count, NULL);
5071 gfc_init_se (&lse, NULL);
5072 gfc_conv_expr_descriptor (&lse, expr1);
5073 gfc_add_modify (&lse.pre, lse.expr, parm);
5074 gfc_start_block (&body);
5075 gfc_add_block_to_block (&body, &lse.pre);
5076 gfc_add_block_to_block (&body, &lse.post);
5077
5078 /* Increment count. */
5079 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5080 count, gfc_index_one_node);
5081 gfc_add_modify (&body, count, tmp);
5082
5083 tmp = gfc_finish_block (&body);
5084
5085 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5086 gfc_add_expr_to_block (block, tmp);
5087 }
5088 /* Free the temporary. */
5089 if (ptemp1)
5090 {
5091 tmp = gfc_call_free (ptemp1);
5092 gfc_add_expr_to_block (block, tmp);
5093 }
5094 }
5095
5096 /* For saving the outer-variable data when doing
5097 LOCAL and LOCAL_INIT substitution. */
5098 struct symbol_and_tree_t
5099 {
5100 gfc_symbol *sym;
5101 gfc_expr *value;
5102 tree decl;
5103 symbol_attribute attr;
5104 };
5105
5106 /* Handle the LOCAL and LOCAL_INIT locality specifiers. This has to be
5107 called twice, once with after_body=false - and then after the loop
5108 body has been processed with after_body=true.
5109
5110 Creates a copy of the variables that appear in the LOCAL and LOCAL_INIT
5111 locality specifiers of 'do concurrent' - and use it in the original
5112 gfc_symbol. The declaration is then reset by after_body=true.
5113
5114 Variables in LOCAL_INIT are set in every loop iteration. */
5115
5116 void
5117 gfc_trans_concurrent_locality_spec (bool after_body, stmtblock_t *body,
5118 std::vector<symbol_and_tree_t> *saved_decls,
5119 gfc_expr_list **locality_list)
5120 {
5121 if (!locality_list[LOCALITY_LOCAL] && !locality_list[LOCALITY_LOCAL_INIT])
5122 return;
5123
5124 if (after_body)
5125 {
5126 for (unsigned i = 0; i < saved_decls->size (); i++)
5127 {
5128 (*saved_decls)[i].sym->backend_decl = (*saved_decls)[i].decl;
5129 (*saved_decls)[i].sym->attr = (*saved_decls)[i].attr;
5130 (*saved_decls)[i].sym->value = (*saved_decls)[i].value;
5131 }
5132 return;
5133 }
5134
5135 gfc_expr_list *el;
5136 int cnt = 0;
5137 for (int i = 0; i <= 1; i++)
5138 for (el = locality_list[i == 0 ? LOCALITY_LOCAL : LOCALITY_LOCAL_INIT];
5139 el; el = el->next)
5140 {
5141 gfc_symbol *outer_sym = el->expr->symtree->n.sym;
5142 if (!outer_sym->backend_decl)
5143 outer_sym->backend_decl = gfc_get_symbol_decl (outer_sym);
5144 cnt++;
5145 }
5146 saved_decls->resize (cnt);
5147
5148 /* The variables have to be created in the scope of the loop body. */
5149 if (!body->has_scope)
5150 {
5151 gcc_checking_assert (body->head == NULL_TREE);
5152 gfc_start_block (body);
5153 }
5154 gfc_start_saved_local_decls ();
5155
5156 cnt = 0;
5157 static_assert (LOCALITY_LOCAL_INIT - LOCALITY_LOCAL == 1, "locality_type");
5158 for (int type = LOCALITY_LOCAL;
5159 type <= LOCALITY_LOCAL_INIT; type++)
5160 for (el = locality_list[type]; el; el = el->next)
5161 {
5162 gfc_symbol *sym = el->expr->symtree->n.sym;
5163 (*saved_decls)[cnt].sym = sym;
5164 (*saved_decls)[cnt].attr = sym->attr;
5165 (*saved_decls)[cnt].value = sym->value;
5166 (*saved_decls)[cnt].decl = sym->backend_decl;
5167
5168 if (sym->attr.dimension && sym->as->type == AS_ASSUMED_SHAPE)
5169 {
5170 gfc_error ("Sorry, %s specifier at %L for assumed-size array %qs "
5171 "is not yet supported",
5172 type == LOCALITY_LOCAL ? "LOCAL" : "LOCAL_INIT",
5173 &el->expr->where, sym->name);
5174 continue;
5175 }
5176
5177 gfc_symbol outer_sym = *sym;
5178
5179 /* Create the inner local variable. */
5180 sym->backend_decl = NULL;
5181 sym->value = NULL;
5182 sym->attr.save = SAVE_NONE;
5183 sym->attr.value = 0;
5184 sym->attr.dummy = 0;
5185 sym->attr.optional = 0;
5186
5187 {
5188 /* Slightly ugly hack for adding the decl via add_decl_as_local. */
5189 gfc_symbol dummy_block_sym;
5190 dummy_block_sym.attr.flavor = FL_LABEL;
5191 gfc_symbol *saved_proc_name = sym->ns->proc_name;
5192 sym->ns->proc_name = &dummy_block_sym;
5193
5194 gfc_get_symbol_decl (sym);
5195 DECL_SOURCE_LOCATION (sym->backend_decl)
5196 = gfc_get_location (&el->expr->where);
5197
5198 sym->ns->proc_name = saved_proc_name;
5199 }
5200
5201 symbol_attribute attr = gfc_expr_attr (el->expr);
5202 if (type == LOCALITY_LOCAL
5203 && !attr.pointer
5204 && sym->ts.type == BT_DERIVED
5205 && gfc_has_default_initializer (sym->ts.u.derived))
5206 /* Cf. PR fortran/ */
5207 gfc_error ("Sorry, LOCAL specifier at %L for %qs of derived type with"
5208 " default initializer is not yet supported",
5209 &el->expr->where, sym->name);
5210 if (type == LOCALITY_LOCAL_INIT)
5211 {
5212 /* LOCAL_INIT: local_var = outer_var. */
5213 gfc_symtree st = *el->expr->symtree;
5214 st.n.sym = &outer_sym;
5215 gfc_expr expr = *el->expr;
5216 expr.symtree = &st;
5217 tree t = (attr.pointer
5218 ? gfc_trans_pointer_assignment (el->expr, &expr)
5219 : gfc_trans_assignment (el->expr, &expr, false, false,
5220 false, false));
5221 gfc_add_expr_to_block (body, t);
5222 }
5223 cnt++;
5224 }
5225 gfc_stop_saved_local_decls ();
5226 }
5227
5228
5229 /* FORALL and WHERE statements are really nasty, especially when you nest
5230 them. All the rhs of a forall assignment must be evaluated before the
5231 actual assignments are performed. Presumably this also applies to all the
5232 assignments in an inner where statement. */
5233
5234 /* Generate code for a FORALL statement. Any temporaries are allocated as a
5235 linear array, relying on the fact that we process in the same order in all
5236 loops.
5237
5238 forall (i=start:end:stride; maskexpr)
5239 e<i> = f<i>
5240 g<i> = h<i>
5241 end forall
5242 (where e,f,g,h<i> are arbitrary expressions possibly involving i)
5243 Translates to:
5244 count = ((end + 1 - start) / stride)
5245 masktmp(:) = maskexpr(:)
5246
5247 maskindex = 0;
5248 for (i = start; i <= end; i += stride)
5249 {
5250 if (masktmp[maskindex++])
5251 e<i> = f<i>
5252 }
5253 maskindex = 0;
5254 for (i = start; i <= end; i += stride)
5255 {
5256 if (masktmp[maskindex++])
5257 g<i> = h<i>
5258 }
5259
5260 Note that this code only works when there are no dependencies.
5261 Forall loop with array assignments and data dependencies are a real pain,
5262 because the size of the temporary cannot always be determined before the
5263 loop is executed. This problem is compounded by the presence of nested
5264 FORALL constructs.
5265 */
5266
5267 static tree
5268 gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
5269 {
5270 stmtblock_t pre;
5271 stmtblock_t post;
5272 stmtblock_t block;
5273 stmtblock_t body;
5274 tree *var;
5275 tree *start;
5276 tree *end;
5277 tree *step;
5278 gfc_expr **varexpr;
5279 tree tmp;
5280 tree assign;
5281 tree size;
5282 tree maskindex;
5283 tree mask;
5284 tree pmask;
5285 tree cycle_label = NULL_TREE;
5286 int n;
5287 int nvar;
5288 int need_temp;
5289 gfc_forall_iterator *fa;
5290 gfc_se se;
5291 gfc_code *c;
5292 gfc_saved_var *saved_vars;
5293 iter_info *this_forall;
5294 forall_info *info;
5295 bool need_mask;
5296
5297 /* Do nothing if the mask is false. */
5298 if (code->expr1
5299 && code->expr1->expr_type == EXPR_CONSTANT
5300 && !code->expr1->value.logical)
5301 return build_empty_stmt (input_location);
5302
5303 n = 0;
5304 /* Count the FORALL index number. */
5305 for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5306 n++;
5307 nvar = n;
5308
5309 /* Allocate the space for var, start, end, step, varexpr. */
5310 var = XCNEWVEC (tree, nvar);
5311 start = XCNEWVEC (tree, nvar);
5312 end = XCNEWVEC (tree, nvar);
5313 step = XCNEWVEC (tree, nvar);
5314 varexpr = XCNEWVEC (gfc_expr *, nvar);
5315 saved_vars = XCNEWVEC (gfc_saved_var, nvar);
5316
5317 /* Allocate the space for info. */
5318 info = XCNEW (forall_info);
5319
5320 gfc_start_block (&pre);
5321 gfc_init_block (&post);
5322 gfc_init_block (&block);
5323
5324 n = 0;
5325 for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5326 {
5327 gfc_symbol *sym = fa->var->symtree->n.sym;
5328
5329 /* Allocate space for this_forall. */
5330 this_forall = XCNEW (iter_info);
5331
5332 /* Create a temporary variable for the FORALL index. */
5333 tmp = gfc_typenode_for_spec (&sym->ts);
5334 var[n] = gfc_create_var (tmp, sym->name);
5335 gfc_shadow_sym (sym, var[n], &saved_vars[n]);
5336
5337 /* Record it in this_forall. */
5338 this_forall->var = var[n];
5339
5340 /* Replace the index symbol's backend_decl with the temporary decl. */
5341 sym->backend_decl = var[n];
5342
5343 /* Work out the start, end and stride for the loop. */
5344 gfc_init_se (&se, NULL);
5345 gfc_conv_expr_val (&se, fa->start);
5346 /* Record it in this_forall. */
5347 this_forall->start = se.expr;
5348 gfc_add_block_to_block (&block, &se.pre);
5349 start[n] = se.expr;
5350
5351 gfc_init_se (&se, NULL);
5352 gfc_conv_expr_val (&se, fa->end);
5353 /* Record it in this_forall. */
5354 this_forall->end = se.expr;
5355 gfc_make_safe_expr (&se);
5356 gfc_add_block_to_block (&block, &se.pre);
5357 end[n] = se.expr;
5358
5359 gfc_init_se (&se, NULL);
5360 gfc_conv_expr_val (&se, fa->stride);
5361 /* Record it in this_forall. */
5362 this_forall->step = se.expr;
5363 gfc_make_safe_expr (&se);
5364 gfc_add_block_to_block (&block, &se.pre);
5365 step[n] = se.expr;
5366
5367 /* Copy loop annotations. */
5368 this_forall->annot = fa->annot;
5369
5370 /* Set the NEXT field of this_forall to NULL. */
5371 this_forall->next = NULL;
5372 /* Link this_forall to the info construct. */
5373 if (info->this_loop)
5374 {
5375 iter_info *iter_tmp = info->this_loop;
5376 while (iter_tmp->next != NULL)
5377 iter_tmp = iter_tmp->next;
5378 iter_tmp->next = this_forall;
5379 }
5380 else
5381 info->this_loop = this_forall;
5382
5383 n++;
5384 }
5385 nvar = n;
5386
5387 /* Calculate the size needed for the current forall level. */
5388 size = gfc_index_one_node;
5389 for (n = 0; n < nvar; n++)
5390 {
5391 /* size = (end + step - start) / step. */
5392 tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (start[n]),
5393 step[n], start[n]);
5394 tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (end[n]),
5395 end[n], tmp);
5396 tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, TREE_TYPE (tmp),
5397 tmp, step[n]);
5398 tmp = convert (gfc_array_index_type, tmp);
5399
5400 size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5401 size, tmp);
5402 }
5403
5404 /* Record the nvar and size of current forall level. */
5405 info->nvar = nvar;
5406 info->size = size;
5407
5408 if (code->expr1)
5409 {
5410 /* If the mask is .true., consider the FORALL unconditional. */
5411 if (code->expr1->expr_type == EXPR_CONSTANT
5412 && code->expr1->value.logical)
5413 need_mask = false;
5414 else
5415 need_mask = true;
5416 }
5417 else
5418 need_mask = false;
5419
5420 /* First we need to allocate the mask. */
5421 if (need_mask)
5422 {
5423 /* As the mask array can be very big, prefer compact boolean types. */
5424 tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5425 mask = allocate_temp_for_forall_nest (nested_forall_info, mask_type,
5426 size, NULL, &block, &pmask);
5427 maskindex = gfc_create_var_np (gfc_array_index_type, "mi");
5428
5429 /* Record them in the info structure. */
5430 info->maskindex = maskindex;
5431 info->mask = mask;
5432 }
5433 else
5434 {
5435 /* No mask was specified. */
5436 maskindex = NULL_TREE;
5437 mask = pmask = NULL_TREE;
5438 }
5439
5440 /* Link the current forall level to nested_forall_info. */
5441 info->prev_nest = nested_forall_info;
5442 nested_forall_info = info;
5443
5444 /* Copy the mask into a temporary variable if required.
5445 For now we assume a mask temporary is needed. */
5446 if (need_mask)
5447 {
5448 /* As the mask array can be very big, prefer compact boolean types. */
5449 tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5450
5451 gfc_add_modify (&block, maskindex, gfc_index_zero_node);
5452
5453 /* Start of mask assignment loop body. */
5454 gfc_start_block (&body);
5455
5456 /* Evaluate the mask expression. */
5457 gfc_init_se (&se, NULL);
5458 gfc_conv_expr_val (&se, code->expr1);
5459 gfc_add_block_to_block (&body, &se.pre);
5460
5461 /* Store the mask. */
5462 se.expr = convert (mask_type, se.expr);
5463
5464 tmp = gfc_build_array_ref (mask, maskindex, NULL);
5465 gfc_add_modify (&body, tmp, se.expr);
5466
5467 /* Advance to the next mask element. */
5468 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5469 maskindex, gfc_index_one_node);
5470 gfc_add_modify (&body, maskindex, tmp);
5471
5472 /* Generate the loops. */
5473 tmp = gfc_finish_block (&body);
5474 tmp = gfc_trans_nested_forall_loop (info, tmp, 0);
5475 gfc_add_expr_to_block (&block, tmp);
5476 }
5477
5478 if (code->op == EXEC_DO_CONCURRENT)
5479 {
5480 gfc_init_block (&body);
5481 cycle_label = gfc_build_label_decl (NULL_TREE);
5482 code->cycle_label = cycle_label;
5483
5484 /* Handle LOCAL and LOCAL_INIT. */
5485 std::vector<symbol_and_tree_t> saved_decls;
5486 gfc_trans_concurrent_locality_spec (false, &body, &saved_decls,
5487 code->ext.concur.locality);
5488
5489 /* Translate the body. */
5490 tmp = gfc_trans_code (code->block->next);
5491 gfc_add_expr_to_block (&body, tmp);
5492
5493 /* Reset locality variables. */
5494 gfc_trans_concurrent_locality_spec (true, &body, &saved_decls,
5495 code->ext.concur.locality);
5496 if (TREE_USED (cycle_label))
5497 {
5498 tmp = build1_v (LABEL_EXPR, cycle_label);
5499 gfc_add_expr_to_block (&body, tmp);
5500 }
5501
5502 tmp = gfc_finish_block (&body);
5503 nested_forall_info->do_concurrent = true;
5504 tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5505 gfc_add_expr_to_block (&block, tmp);
5506 goto done;
5507 }
5508
5509 c = code->block->next;
5510
5511 /* TODO: loop merging in FORALL statements. */
5512 /* Now that we've got a copy of the mask, generate the assignment loops. */
5513 while (c)
5514 {
5515 switch (c->op)
5516 {
5517 case EXEC_ASSIGN:
5518 /* A scalar or array assignment. DO the simple check for
5519 lhs to rhs dependencies. These make a temporary for the
5520 rhs and form a second forall block to copy to variable. */
5521 need_temp = check_forall_dependencies(c, &pre, &post);
5522
5523 /* Temporaries due to array assignment data dependencies introduce
5524 no end of problems. */
5525 if (need_temp || flag_test_forall_temp)
5526 gfc_trans_assign_need_temp (c->expr1, c->expr2, NULL, false,
5527 nested_forall_info, &block);
5528 else
5529 {
5530 /* Use the normal assignment copying routines. */
5531 assign = gfc_trans_assignment (c->expr1, c->expr2, false, true);
5532
5533 /* Generate body and loops. */
5534 tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5535 assign, 1);
5536 gfc_add_expr_to_block (&block, tmp);
5537 }
5538
5539 /* Cleanup any temporary symtrees that have been made to deal
5540 with dependencies. */
5541 if (new_symtree)
5542 cleanup_forall_symtrees (c);
5543
5544 break;
5545
5546 case EXEC_WHERE:
5547 /* Translate WHERE or WHERE construct nested in FORALL. */
5548 gfc_trans_where_2 (c, NULL, false, nested_forall_info, &block);
5549 break;
5550
5551 /* Pointer assignment inside FORALL. */
5552 case EXEC_POINTER_ASSIGN:
5553 need_temp = gfc_check_dependency (c->expr1, c->expr2, 0);
5554 /* Avoid cases where a temporary would never be needed and where
5555 the temp code is guaranteed to fail. */
5556 if (need_temp
5557 || (flag_test_forall_temp
5558 && c->expr2->expr_type != EXPR_CONSTANT
5559 && c->expr2->expr_type != EXPR_NULL))
5560 gfc_trans_pointer_assign_need_temp (c->expr1, c->expr2,
5561 nested_forall_info, &block);
5562 else
5563 {
5564 /* Use the normal assignment copying routines. */
5565 assign = gfc_trans_pointer_assignment (c->expr1, c->expr2);
5566
5567 /* Generate body and loops. */
5568 tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5569 assign, 1);
5570 gfc_add_expr_to_block (&block, tmp);
5571 }
5572 break;
5573
5574 case EXEC_FORALL:
5575 tmp = gfc_trans_forall_1 (c, nested_forall_info);
5576 gfc_add_expr_to_block (&block, tmp);
5577 break;
5578
5579 /* Explicit subroutine calls are prevented by the frontend but interface
5580 assignments can legitimately produce them. */
5581 case EXEC_ASSIGN_CALL:
5582 assign = gfc_trans_call (c, true, NULL_TREE, NULL_TREE, false);
5583 tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign, 1);
5584 gfc_add_expr_to_block (&block, tmp);
5585 break;
5586
5587 default:
5588 gcc_unreachable ();
5589 }
5590
5591 c = c->next;
5592 }
5593
5594 done:
5595 /* Restore the original index variables. */
5596 for (fa = code->ext.concur.forall_iterator, n = 0; fa; fa = fa->next, n++)
5597 gfc_restore_sym (fa->var->symtree->n.sym, &saved_vars[n]);
5598
5599 /* Free the space for var, start, end, step, varexpr. */
5600 free (var);
5601 free (start);
5602 free (end);
5603 free (step);
5604 free (varexpr);
5605 free (saved_vars);
5606
5607 for (this_forall = info->this_loop; this_forall;)
5608 {
5609 iter_info *next = this_forall->next;
5610 free (this_forall);
5611 this_forall = next;
5612 }
5613
5614 /* Free the space for this forall_info. */
5615 free (info);
5616
5617 if (pmask)
5618 {
5619 /* Free the temporary for the mask. */
5620 tmp = gfc_call_free (pmask);
5621 gfc_add_expr_to_block (&block, tmp);
5622 }
5623 if (maskindex)
5624 pushdecl (maskindex);
5625
5626 gfc_add_block_to_block (&pre, &block);
5627 gfc_add_block_to_block (&pre, &post);
5628
5629 return gfc_finish_block (&pre);
5630 }
5631
5632
5633 /* Translate the FORALL statement or construct. */
5634
5635 tree gfc_trans_forall (gfc_code * code)
5636 {
5637 return gfc_trans_forall_1 (code, NULL);
5638 }
5639
5640
5641 /* Translate the DO CONCURRENT construct. */
5642
5643 tree gfc_trans_do_concurrent (gfc_code * code)
5644 {
5645 return gfc_trans_forall_1 (code, NULL);
5646 }
5647
5648
5649 /* Evaluate the WHERE mask expression, copy its value to a temporary.
5650 If the WHERE construct is nested in FORALL, compute the overall temporary
5651 needed by the WHERE mask expression multiplied by the iterator number of
5652 the nested forall.
5653 ME is the WHERE mask expression.
5654 MASK is the current execution mask upon input, whose sense may or may
5655 not be inverted as specified by the INVERT argument.
5656 CMASK is the updated execution mask on output, or NULL if not required.
5657 PMASK is the pending execution mask on output, or NULL if not required.
5658 BLOCK is the block in which to place the condition evaluation loops. */
5659
5660 static void
5661 gfc_evaluate_where_mask (gfc_expr * me, forall_info * nested_forall_info,
5662 tree mask, bool invert, tree cmask, tree pmask,
5663 tree mask_type, stmtblock_t * block)
5664 {
5665 tree tmp, tmp1;
5666 gfc_ss *lss, *rss;
5667 gfc_loopinfo loop;
5668 stmtblock_t body, body1;
5669 tree count, cond, mtmp;
5670 gfc_se lse, rse;
5671
5672 gfc_init_loopinfo (&loop);
5673
5674 lss = gfc_walk_expr (me);
5675 rss = gfc_walk_expr (me);
5676
5677 /* Variable to index the temporary. */
5678 count = gfc_create_var (gfc_array_index_type, "count");
5679 /* Initialize count. */
5680 gfc_add_modify (block, count, gfc_index_zero_node);
5681
5682 gfc_start_block (&body);
5683
5684 gfc_init_se (&rse, NULL);
5685 gfc_init_se (&lse, NULL);
5686
5687 if (lss == gfc_ss_terminator)
5688 {
5689 gfc_init_block (&body1);
5690 }
5691 else
5692 {
5693 /* Initialize the loop. */
5694 gfc_init_loopinfo (&loop);
5695
5696 /* We may need LSS to determine the shape of the expression. */
5697 gfc_add_ss_to_loop (&loop, lss);
5698 gfc_add_ss_to_loop (&loop, rss);
5699
5700 gfc_conv_ss_startstride (&loop);
5701 gfc_conv_loop_setup (&loop, &me->where);
5702
5703 gfc_mark_ss_chain_used (rss, 1);
5704 /* Start the loop body. */
5705 gfc_start_scalarized_body (&loop, &body1);
5706
5707 /* Translate the expression. */
5708 gfc_copy_loopinfo_to_se (&rse, &loop);
5709 rse.ss = rss;
5710 gfc_conv_expr (&rse, me);
5711 }
5712
5713 /* Variable to evaluate mask condition. */
5714 cond = gfc_create_var (mask_type, "cond");
5715 if (mask && (cmask || pmask))
5716 mtmp = gfc_create_var (mask_type, "mask");
5717 else mtmp = NULL_TREE;
5718
5719 gfc_add_block_to_block (&body1, &lse.pre);
5720 gfc_add_block_to_block (&body1, &rse.pre);
5721
5722 gfc_add_modify (&body1, cond, fold_convert (mask_type, rse.expr));
5723
5724 if (mask && (cmask || pmask))
5725 {
5726 tmp = gfc_build_array_ref (mask, count, NULL);
5727 if (invert)
5728 tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, tmp);
5729 gfc_add_modify (&body1, mtmp, tmp);
5730 }
5731
5732 if (cmask)
5733 {
5734 tmp1 = gfc_build_array_ref (cmask, count, NULL);
5735 tmp = cond;
5736 if (mask)
5737 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type,
5738 mtmp, tmp);
5739 gfc_add_modify (&body1, tmp1, tmp);
5740 }
5741
5742 if (pmask)
5743 {
5744 tmp1 = gfc_build_array_ref (pmask, count, NULL);
5745 tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, cond);
5746 if (mask)
5747 tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type, mtmp,
5748 tmp);
5749 gfc_add_modify (&body1, tmp1, tmp);
5750 }
5751
5752 gfc_add_block_to_block (&body1, &lse.post);
5753 gfc_add_block_to_block (&body1, &rse.post);
5754
5755 if (lss == gfc_ss_terminator)
5756 {
5757 gfc_add_block_to_block (&body, &body1);
5758 }
5759 else
5760 {
5761 /* Increment count. */
5762 tmp1 = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5763 count, gfc_index_one_node);
5764 gfc_add_modify (&body1, count, tmp1);
5765
5766 /* Generate the copying loops. */
5767 gfc_trans_scalarizing_loops (&loop, &body1);
5768
5769 gfc_add_block_to_block (&body, &loop.pre);
5770 gfc_add_block_to_block (&body, &loop.post);
5771
5772 gfc_cleanup_loop (&loop);
5773 /* TODO: Reuse lss and rss when copying temp->lhs. Need to be careful
5774 as tree nodes in SS may not be valid in different scope. */
5775 }
5776
5777 tmp1 = gfc_finish_block (&body);
5778 /* If the WHERE construct is inside FORALL, fill the full temporary. */
5779 if (nested_forall_info != NULL)
5780 tmp1 = gfc_trans_nested_forall_loop (nested_forall_info, tmp1, 1);
5781
5782 gfc_add_expr_to_block (block, tmp1);
5783 }
5784
5785
5786 /* Translate an assignment statement in a WHERE statement or construct
5787 statement. The MASK expression is used to control which elements
5788 of EXPR1 shall be assigned. The sense of MASK is specified by
5789 INVERT. */
5790
5791 static tree
5792 gfc_trans_where_assign (gfc_expr *expr1, gfc_expr *expr2,
5793 tree mask, bool invert,
5794 tree count1, tree count2,
5795 gfc_code *cnext)
5796 {
5797 gfc_se lse;
5798 gfc_se rse;
5799 gfc_ss *lss;
5800 gfc_ss *lss_section;
5801 gfc_ss *rss;
5802
5803 gfc_loopinfo loop;
5804 tree tmp;
5805 stmtblock_t block;
5806 stmtblock_t body;
5807 tree index, maskexpr;
5808
5809 /* A defined assignment. */
5810 if (cnext && cnext->resolved_sym)
5811 return gfc_trans_call (cnext, true, mask, count1, invert);
5812
5813 #if 0
5814 /* TODO: handle this special case.
5815 Special case a single function returning an array. */
5816 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
5817 {
5818 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
5819 if (tmp)
5820 return tmp;
5821 }
5822 #endif
5823
5824 /* Assignment of the form lhs = rhs. */
5825 gfc_start_block (&block);
5826
5827 gfc_init_se (&lse, NULL);
5828 gfc_init_se (&rse, NULL);
5829
5830 /* Walk the lhs. */
5831 lss = gfc_walk_expr (expr1);
5832 rss = NULL;
5833
5834 /* In each where-assign-stmt, the mask-expr and the variable being
5835 defined shall be arrays of the same shape. */
5836 gcc_assert (lss != gfc_ss_terminator);
5837
5838 /* The assignment needs scalarization. */
5839 lss_section = lss;
5840
5841 /* Find a non-scalar SS from the lhs. */
5842 while (lss_section != gfc_ss_terminator
5843 && lss_section->info->type != GFC_SS_SECTION)
5844 lss_section = lss_section->next;
5845
5846 gcc_assert (lss_section != gfc_ss_terminator);
5847
5848 /* Initialize the scalarizer. */
5849 gfc_init_loopinfo (&loop);
5850
5851 /* Walk the rhs. */
5852 rss = gfc_walk_expr (expr2);
5853 if (rss == gfc_ss_terminator)
5854 {
5855 /* The rhs is scalar. Add a ss for the expression. */
5856 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
5857 rss->info->where = 1;
5858 }
5859
5860 /* Associate the SS with the loop. */
5861 gfc_add_ss_to_loop (&loop, lss);
5862 gfc_add_ss_to_loop (&loop, rss);
5863
5864 /* Calculate the bounds of the scalarization. */
5865 gfc_conv_ss_startstride (&loop);
5866
5867 /* Resolve any data dependencies in the statement. */
5868 gfc_conv_resolve_dependencies (&loop, lss_section, rss);
5869
5870 /* Setup the scalarizing loops. */
5871 gfc_conv_loop_setup (&loop, &expr2->where);
5872
5873 /* Setup the gfc_se structures. */
5874 gfc_copy_loopinfo_to_se (&lse, &loop);
5875 gfc_copy_loopinfo_to_se (&rse, &loop);
5876
5877 rse.ss = rss;
5878 gfc_mark_ss_chain_used (rss, 1);
5879 if (loop.temp_ss == NULL)
5880 {
5881 lse.ss = lss;
5882 gfc_mark_ss_chain_used (lss, 1);
5883 }
5884 else
5885 {
5886 lse.ss = loop.temp_ss;
5887 gfc_mark_ss_chain_used (lss, 3);
5888 gfc_mark_ss_chain_used (loop.temp_ss, 3);
5889 }
5890
5891 /* Start the scalarized loop body. */
5892 gfc_start_scalarized_body (&loop, &body);
5893
5894 /* Translate the expression. */
5895 gfc_conv_expr (&rse, expr2);
5896 if (lss != gfc_ss_terminator && loop.temp_ss != NULL)
5897 gfc_conv_tmp_array_ref (&lse);
5898 else
5899 gfc_conv_expr (&lse, expr1);
5900
5901 /* Form the mask expression according to the mask. */
5902 index = count1;
5903 maskexpr = gfc_build_array_ref (mask, index, NULL);
5904 if (invert)
5905 maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
5906 TREE_TYPE (maskexpr), maskexpr);
5907
5908 /* Use the scalar assignment as is. */
5909 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
5910 false, loop.temp_ss == NULL);
5911
5912 tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt (input_location));
5913
5914 gfc_add_expr_to_block (&body, tmp);
5915
5916 if (lss == gfc_ss_terminator)
5917 {
5918 /* Increment count1. */
5919 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5920 count1, gfc_index_one_node);
5921 gfc_add_modify (&body, count1, tmp);
5922
5923 /* Use the scalar assignment as is. */
5924 gfc_add_block_to_block (&block, &body);
5925 }
5926 else
5927 {
5928 gcc_assert (lse.ss == gfc_ss_terminator
5929 && rse.ss == gfc_ss_terminator);
5930
5931 if (loop.temp_ss != NULL)
5932 {
5933 /* Increment count1 before finish the main body of a scalarized
5934 expression. */
5935 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5936 gfc_array_index_type, count1, gfc_index_one_node);
5937 gfc_add_modify (&body, count1, tmp);
5938 gfc_trans_scalarized_loop_boundary (&loop, &body);
5939
5940 /* We need to copy the temporary to the actual lhs. */
5941 gfc_init_se (&lse, NULL);
5942 gfc_init_se (&rse, NULL);
5943 gfc_copy_loopinfo_to_se (&lse, &loop);
5944 gfc_copy_loopinfo_to_se (&rse, &loop);
5945
5946 rse.ss = loop.temp_ss;
5947 lse.ss = lss;
5948
5949 gfc_conv_tmp_array_ref (&rse);
5950 gfc_conv_expr (&lse, expr1);
5951
5952 gcc_assert (lse.ss == gfc_ss_terminator
5953 && rse.ss == gfc_ss_terminator);
5954
5955 /* Form the mask expression according to the mask tree list. */
5956 index = count2;
5957 maskexpr = gfc_build_array_ref (mask, index, NULL);
5958 if (invert)
5959 maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
5960 TREE_TYPE (maskexpr), maskexpr);
5961
5962 /* Use the scalar assignment as is. */
5963 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts, false, true);
5964 tmp = build3_v (COND_EXPR, maskexpr, tmp,
5965 build_empty_stmt (input_location));
5966 gfc_add_expr_to_block (&body, tmp);
5967
5968 /* Increment count2. */
5969 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5970 gfc_array_index_type, count2,
5971 gfc_index_one_node);
5972 gfc_add_modify (&body, count2, tmp);
5973 }
5974 else
5975 {
5976 /* Increment count1. */
5977 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5978 gfc_array_index_type, count1,
5979 gfc_index_one_node);
5980 gfc_add_modify (&body, count1, tmp);
5981 }
5982
5983 /* Generate the copying loops. */
5984 gfc_trans_scalarizing_loops (&loop, &body);
5985
5986 /* Wrap the whole thing up. */
5987 gfc_add_block_to_block (&block, &loop.pre);
5988 gfc_add_block_to_block (&block, &loop.post);
5989 gfc_cleanup_loop (&loop);
5990 }
5991
5992 return gfc_finish_block (&block);
5993 }
5994
5995
5996 /* Translate the WHERE construct or statement.
5997 This function can be called iteratively to translate the nested WHERE
5998 construct or statement.
5999 MASK is the control mask. */
6000
6001 static void
6002 gfc_trans_where_2 (gfc_code * code, tree mask, bool invert,
6003 forall_info * nested_forall_info, stmtblock_t * block)
6004 {
6005 stmtblock_t inner_size_body;
6006 tree inner_size, size;
6007 gfc_ss *lss, *rss;
6008 tree mask_type;
6009 gfc_expr *expr1;
6010 gfc_expr *expr2;
6011 gfc_code *cblock;
6012 gfc_code *cnext;
6013 tree tmp;
6014 tree cond;
6015 tree count1, count2;
6016 bool need_cmask;
6017 bool need_pmask;
6018 int need_temp;
6019 tree pcmask = NULL_TREE;
6020 tree ppmask = NULL_TREE;
6021 tree cmask = NULL_TREE;
6022 tree pmask = NULL_TREE;
6023 gfc_actual_arglist *arg;
6024
6025 /* the WHERE statement or the WHERE construct statement. */
6026 cblock = code->block;
6027
6028 /* As the mask array can be very big, prefer compact boolean types. */
6029 mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
6030
6031 /* Determine which temporary masks are needed. */
6032 if (!cblock->block)
6033 {
6034 /* One clause: No ELSEWHEREs. */
6035 need_cmask = (cblock->next != 0);
6036 need_pmask = false;
6037 }
6038 else if (cblock->block->block)
6039 {
6040 /* Three or more clauses: Conditional ELSEWHEREs. */
6041 need_cmask = true;
6042 need_pmask = true;
6043 }
6044 else if (cblock->next)
6045 {
6046 /* Two clauses, the first non-empty. */
6047 need_cmask = true;
6048 need_pmask = (mask != NULL_TREE
6049 && cblock->block->next != 0);
6050 }
6051 else if (!cblock->block->next)
6052 {
6053 /* Two clauses, both empty. */
6054 need_cmask = false;
6055 need_pmask = false;
6056 }
6057 /* Two clauses, the first empty, the second non-empty. */
6058 else if (mask)
6059 {
6060 need_cmask = (cblock->block->expr1 != 0);
6061 need_pmask = true;
6062 }
6063 else
6064 {
6065 need_cmask = true;
6066 need_pmask = false;
6067 }
6068
6069 if (need_cmask || need_pmask)
6070 {
6071 /* Calculate the size of temporary needed by the mask-expr. */
6072 gfc_init_block (&inner_size_body);
6073 inner_size = compute_inner_temp_size (cblock->expr1, cblock->expr1,
6074 &inner_size_body, &lss, &rss);
6075
6076 gfc_free_ss_chain (lss);
6077 gfc_free_ss_chain (rss);
6078
6079 /* Calculate the total size of temporary needed. */
6080 size = compute_overall_iter_number (nested_forall_info, inner_size,
6081 &inner_size_body, block);
6082
6083 /* Check whether the size is negative. */
6084 cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, size,
6085 gfc_index_zero_node);
6086 size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
6087 cond, gfc_index_zero_node, size);
6088 size = gfc_evaluate_now (size, block);
6089
6090 /* Allocate temporary for WHERE mask if needed. */
6091 if (need_cmask)
6092 cmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6093 &pcmask);
6094
6095 /* Allocate temporary for !mask if needed. */
6096 if (need_pmask)
6097 pmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6098 &ppmask);
6099 }
6100
6101 while (cblock)
6102 {
6103 /* Each time around this loop, the where clause is conditional
6104 on the value of mask and invert, which are updated at the
6105 bottom of the loop. */
6106
6107 /* Has mask-expr. */
6108 if (cblock->expr1)
6109 {
6110 /* Ensure that the WHERE mask will be evaluated exactly once.
6111 If there are no statements in this WHERE/ELSEWHERE clause,
6112 then we don't need to update the control mask (cmask).
6113 If this is the last clause of the WHERE construct, then
6114 we don't need to update the pending control mask (pmask). */
6115 if (mask)
6116 gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6117 mask, invert,
6118 cblock->next ? cmask : NULL_TREE,
6119 cblock->block ? pmask : NULL_TREE,
6120 mask_type, block);
6121 else
6122 gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6123 NULL_TREE, false,
6124 (cblock->next || cblock->block)
6125 ? cmask : NULL_TREE,
6126 NULL_TREE, mask_type, block);
6127
6128 invert = false;
6129 }
6130 /* It's a final elsewhere-stmt. No mask-expr is present. */
6131 else
6132 cmask = mask;
6133
6134 /* The body of this where clause are controlled by cmask with
6135 sense specified by invert. */
6136
6137 /* Get the assignment statement of a WHERE statement, or the first
6138 statement in where-body-construct of a WHERE construct. */
6139 cnext = cblock->next;
6140 while (cnext)
6141 {
6142 switch (cnext->op)
6143 {
6144 /* WHERE assignment statement. */
6145 case EXEC_ASSIGN_CALL:
6146
6147 arg = cnext->ext.actual;
6148 expr1 = expr2 = NULL;
6149 for (; arg; arg = arg->next)
6150 {
6151 if (!arg->expr)
6152 continue;
6153 if (expr1 == NULL)
6154 expr1 = arg->expr;
6155 else
6156 expr2 = arg->expr;
6157 }
6158 goto evaluate;
6159
6160 case EXEC_ASSIGN:
6161 expr1 = cnext->expr1;
6162 expr2 = cnext->expr2;
6163 evaluate:
6164 if (nested_forall_info != NULL)
6165 {
6166 need_temp = gfc_check_dependency (expr1, expr2, 0);
6167 if ((need_temp || flag_test_forall_temp)
6168 && cnext->op != EXEC_ASSIGN_CALL)
6169 gfc_trans_assign_need_temp (expr1, expr2,
6170 cmask, invert,
6171 nested_forall_info, block);
6172 else
6173 {
6174 /* Variables to control maskexpr. */
6175 count1 = gfc_create_var (gfc_array_index_type, "count1");
6176 count2 = gfc_create_var (gfc_array_index_type, "count2");
6177 gfc_add_modify (block, count1, gfc_index_zero_node);
6178 gfc_add_modify (block, count2, gfc_index_zero_node);
6179
6180 tmp = gfc_trans_where_assign (expr1, expr2,
6181 cmask, invert,
6182 count1, count2,
6183 cnext);
6184
6185 tmp = gfc_trans_nested_forall_loop (nested_forall_info,
6186 tmp, 1);
6187 gfc_add_expr_to_block (block, tmp);
6188 }
6189 }
6190 else
6191 {
6192 /* Variables to control maskexpr. */
6193 count1 = gfc_create_var (gfc_array_index_type, "count1");
6194 count2 = gfc_create_var (gfc_array_index_type, "count2");
6195 gfc_add_modify (block, count1, gfc_index_zero_node);
6196 gfc_add_modify (block, count2, gfc_index_zero_node);
6197
6198 tmp = gfc_trans_where_assign (expr1, expr2,
6199 cmask, invert,
6200 count1, count2,
6201 cnext);
6202 gfc_add_expr_to_block (block, tmp);
6203
6204 }
6205 break;
6206
6207 /* WHERE or WHERE construct is part of a where-body-construct. */
6208 case EXEC_WHERE:
6209 gfc_trans_where_2 (cnext, cmask, invert,
6210 nested_forall_info, block);
6211 break;
6212
6213 default:
6214 gcc_unreachable ();
6215 }
6216
6217 /* The next statement within the same where-body-construct. */
6218 cnext = cnext->next;
6219 }
6220 /* The next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt. */
6221 cblock = cblock->block;
6222 if (mask == NULL_TREE)
6223 {
6224 /* If we're the initial WHERE, we can simply invert the sense
6225 of the current mask to obtain the "mask" for the remaining
6226 ELSEWHEREs. */
6227 invert = true;
6228 mask = cmask;
6229 }
6230 else
6231 {
6232 /* Otherwise, for nested WHERE's we need to use the pending mask. */
6233 invert = false;
6234 mask = pmask;
6235 }
6236 }
6237
6238 /* If we allocated a pending mask array, deallocate it now. */
6239 if (ppmask)
6240 {
6241 tmp = gfc_call_free (ppmask);
6242 gfc_add_expr_to_block (block, tmp);
6243 }
6244
6245 /* If we allocated a current mask array, deallocate it now. */
6246 if (pcmask)
6247 {
6248 tmp = gfc_call_free (pcmask);
6249 gfc_add_expr_to_block (block, tmp);
6250 }
6251 }
6252
6253 /* Translate a simple WHERE construct or statement without dependencies.
6254 CBLOCK is the "then" clause of the WHERE statement, where CBLOCK->EXPR
6255 is the mask condition, and EBLOCK if non-NULL is the "else" clause.
6256 Currently both CBLOCK and EBLOCK are restricted to single assignments. */
6257
6258 static tree
6259 gfc_trans_where_3 (gfc_code * cblock, gfc_code * eblock)
6260 {
6261 stmtblock_t block, body;
6262 gfc_expr *cond, *tdst, *tsrc, *edst, *esrc;
6263 tree tmp, cexpr, tstmt, estmt;
6264 gfc_ss *css, *tdss, *tsss;
6265 gfc_se cse, tdse, tsse, edse, esse;
6266 gfc_loopinfo loop;
6267 gfc_ss *edss = 0;
6268 gfc_ss *esss = 0;
6269 bool maybe_workshare = false;
6270
6271 /* Allow the scalarizer to workshare simple where loops. */
6272 if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
6273 == OMPWS_WORKSHARE_FLAG)
6274 {
6275 maybe_workshare = true;
6276 ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
6277 }
6278
6279 cond = cblock->expr1;
6280 tdst = cblock->next->expr1;
6281 tsrc = cblock->next->expr2;
6282 edst = eblock ? eblock->next->expr1 : NULL;
6283 esrc = eblock ? eblock->next->expr2 : NULL;
6284
6285 gfc_start_block (&block);
6286 gfc_init_loopinfo (&loop);
6287
6288 /* Handle the condition. */
6289 gfc_init_se (&cse, NULL);
6290 css = gfc_walk_expr (cond);
6291 gfc_add_ss_to_loop (&loop, css);
6292
6293 /* Handle the then-clause. */
6294 gfc_init_se (&tdse, NULL);
6295 gfc_init_se (&tsse, NULL);
6296 tdss = gfc_walk_expr (tdst);
6297 tsss = gfc_walk_expr (tsrc);
6298 if (tsss == gfc_ss_terminator)
6299 {
6300 tsss = gfc_get_scalar_ss (gfc_ss_terminator, tsrc);
6301 tsss->info->where = 1;
6302 }
6303 gfc_add_ss_to_loop (&loop, tdss);
6304 gfc_add_ss_to_loop (&loop, tsss);
6305
6306 if (eblock)
6307 {
6308 /* Handle the else clause. */
6309 gfc_init_se (&edse, NULL);
6310 gfc_init_se (&esse, NULL);
6311 edss = gfc_walk_expr (edst);
6312 esss = gfc_walk_expr (esrc);
6313 if (esss == gfc_ss_terminator)
6314 {
6315 esss = gfc_get_scalar_ss (gfc_ss_terminator, esrc);
6316 esss->info->where = 1;
6317 }
6318 gfc_add_ss_to_loop (&loop, edss);
6319 gfc_add_ss_to_loop (&loop, esss);
6320 }
6321
6322 gfc_conv_ss_startstride (&loop);
6323 gfc_conv_loop_setup (&loop, &tdst->where);
6324
6325 gfc_mark_ss_chain_used (css, 1);
6326 gfc_mark_ss_chain_used (tdss, 1);
6327 gfc_mark_ss_chain_used (tsss, 1);
6328 if (eblock)
6329 {
6330 gfc_mark_ss_chain_used (edss, 1);
6331 gfc_mark_ss_chain_used (esss, 1);
6332 }
6333
6334 gfc_start_scalarized_body (&loop, &body);
6335
6336 gfc_copy_loopinfo_to_se (&cse, &loop);
6337 gfc_copy_loopinfo_to_se (&tdse, &loop);
6338 gfc_copy_loopinfo_to_se (&tsse, &loop);
6339 cse.ss = css;
6340 tdse.ss = tdss;
6341 tsse.ss = tsss;
6342 if (eblock)
6343 {
6344 gfc_copy_loopinfo_to_se (&edse, &loop);
6345 gfc_copy_loopinfo_to_se (&esse, &loop);
6346 edse.ss = edss;
6347 esse.ss = esss;
6348 }
6349
6350 gfc_conv_expr (&cse, cond);
6351 gfc_add_block_to_block (&body, &cse.pre);
6352 cexpr = cse.expr;
6353
6354 gfc_conv_expr (&tsse, tsrc);
6355 if (tdss != gfc_ss_terminator && loop.temp_ss != NULL)
6356 gfc_conv_tmp_array_ref (&tdse);
6357 else
6358 gfc_conv_expr (&tdse, tdst);
6359
6360 if (eblock)
6361 {
6362 gfc_conv_expr (&esse, esrc);
6363 if (edss != gfc_ss_terminator && loop.temp_ss != NULL)
6364 gfc_conv_tmp_array_ref (&edse);
6365 else
6366 gfc_conv_expr (&edse, edst);
6367 }
6368
6369 tstmt = gfc_trans_scalar_assign (&tdse, &tsse, tdst->ts, false, true);
6370 estmt = eblock ? gfc_trans_scalar_assign (&edse, &esse, edst->ts,
6371 false, true)
6372 : build_empty_stmt (input_location);
6373 tmp = build3_v (COND_EXPR, cexpr, tstmt, estmt);
6374 gfc_add_expr_to_block (&body, tmp);
6375 gfc_add_block_to_block (&body, &cse.post);
6376
6377 if (maybe_workshare)
6378 ompws_flags &= ~OMPWS_SCALARIZER_BODY;
6379 gfc_trans_scalarizing_loops (&loop, &body);
6380 gfc_add_block_to_block (&block, &loop.pre);
6381 gfc_add_block_to_block (&block, &loop.post);
6382 gfc_cleanup_loop (&loop);
6383
6384 return gfc_finish_block (&block);
6385 }
6386
6387 /* As the WHERE or WHERE construct statement can be nested, we call
6388 gfc_trans_where_2 to do the translation, and pass the initial
6389 NULL values for both the control mask and the pending control mask. */
6390
6391 tree
6392 gfc_trans_where (gfc_code * code)
6393 {
6394 stmtblock_t block;
6395 gfc_code *cblock;
6396 gfc_code *eblock;
6397
6398 cblock = code->block;
6399 if (cblock->next
6400 && cblock->next->op == EXEC_ASSIGN
6401 && !cblock->next->next)
6402 {
6403 eblock = cblock->block;
6404 if (!eblock)
6405 {
6406 /* A simple "WHERE (cond) x = y" statement or block is
6407 dependence free if cond is not dependent upon writing x,
6408 and the source y is unaffected by the destination x. */
6409 if (!gfc_check_dependency (cblock->next->expr1,
6410 cblock->expr1, 0)
6411 && !gfc_check_dependency (cblock->next->expr1,
6412 cblock->next->expr2, 0))
6413 return gfc_trans_where_3 (cblock, NULL);
6414 }
6415 else if (!eblock->expr1
6416 && !eblock->block
6417 && eblock->next
6418 && eblock->next->op == EXEC_ASSIGN
6419 && !eblock->next->next)
6420 {
6421 /* A simple "WHERE (cond) x1 = y1 ELSEWHERE x2 = y2 ENDWHERE"
6422 block is dependence free if cond is not dependent on writes
6423 to x1 and x2, y1 is not dependent on writes to x2, and y2
6424 is not dependent on writes to x1, and both y's are not
6425 dependent upon their own x's. In addition to this, the
6426 final two dependency checks below exclude all but the same
6427 array reference if the where and elswhere destinations
6428 are the same. In short, this is VERY conservative and this
6429 is needed because the two loops, required by the standard
6430 are coalesced in gfc_trans_where_3. */
6431 if (!gfc_check_dependency (cblock->next->expr1,
6432 cblock->expr1, 0)
6433 && !gfc_check_dependency (eblock->next->expr1,
6434 cblock->expr1, 0)
6435 && !gfc_check_dependency (cblock->next->expr1,
6436 eblock->next->expr2, 1)
6437 && !gfc_check_dependency (eblock->next->expr1,
6438 cblock->next->expr2, 1)
6439 && !gfc_check_dependency (cblock->next->expr1,
6440 cblock->next->expr2, 1)
6441 && !gfc_check_dependency (eblock->next->expr1,
6442 eblock->next->expr2, 1)
6443 && !gfc_check_dependency (cblock->next->expr1,
6444 eblock->next->expr1, 0)
6445 && !gfc_check_dependency (eblock->next->expr1,
6446 cblock->next->expr1, 0))
6447 return gfc_trans_where_3 (cblock, eblock);
6448 }
6449 }
6450
6451 gfc_start_block (&block);
6452
6453 gfc_trans_where_2 (code, NULL, false, NULL, &block);
6454
6455 return gfc_finish_block (&block);
6456 }
6457
6458
6459 /* CYCLE a DO loop. The label decl has already been created by
6460 gfc_trans_do(), it's in TREE_PURPOSE (backend_decl) of the gfc_code
6461 node at the head of the loop. We must mark the label as used. */
6462
6463 tree
6464 gfc_trans_cycle (gfc_code * code)
6465 {
6466 tree cycle_label;
6467
6468 cycle_label = code->ext.which_construct->cycle_label;
6469 gcc_assert (cycle_label);
6470
6471 TREE_USED (cycle_label) = 1;
6472 return build1_v (GOTO_EXPR, cycle_label);
6473 }
6474
6475
6476 /* EXIT a DO loop. Similar to CYCLE, but now the label is in
6477 TREE_VALUE (backend_decl) of the gfc_code node at the head of the
6478 loop. */
6479
6480 tree
6481 gfc_trans_exit (gfc_code * code)
6482 {
6483 tree exit_label;
6484
6485 exit_label = code->ext.which_construct->exit_label;
6486 gcc_assert (exit_label);
6487
6488 TREE_USED (exit_label) = 1;
6489 return build1_v (GOTO_EXPR, exit_label);
6490 }
6491
6492
6493 /* Get the initializer expression for the code and expr of an allocate.
6494 When no initializer is needed return NULL. */
6495
6496 static gfc_expr *
6497 allocate_get_initializer (gfc_code * code, gfc_expr * expr)
6498 {
6499 if (!gfc_bt_struct (expr->ts.type) && expr->ts.type != BT_CLASS)
6500 return NULL;
6501
6502 /* An explicit type was given in allocate ( T:: object). */
6503 if (code->ext.alloc.ts.type == BT_DERIVED
6504 && (code->ext.alloc.ts.u.derived->attr.alloc_comp
6505 || gfc_has_default_initializer (code->ext.alloc.ts.u.derived)))
6506 return gfc_default_initializer (&code->ext.alloc.ts);
6507
6508 if (gfc_bt_struct (expr->ts.type)
6509 && (expr->ts.u.derived->attr.alloc_comp
6510 || gfc_has_default_initializer (expr->ts.u.derived)))
6511 return gfc_default_initializer (&expr->ts);
6512
6513 if (expr->ts.type == BT_CLASS
6514 && (CLASS_DATA (expr)->ts.u.derived->attr.alloc_comp
6515 || gfc_has_default_initializer (CLASS_DATA (expr)->ts.u.derived)))
6516 return gfc_default_initializer (&CLASS_DATA (expr)->ts);
6517
6518 return NULL;
6519 }
6520
6521 /* Translate the ALLOCATE statement. */
6522
6523 tree
6524 gfc_trans_allocate (gfc_code * code, gfc_omp_namelist *omp_allocate)
6525 {
6526 gfc_alloc *al;
6527 gfc_expr *expr, *e3rhs = NULL, *init_expr;
6528 gfc_se se, se_sz;
6529 tree tmp;
6530 tree parm;
6531 tree stat;
6532 tree errmsg;
6533 tree errlen;
6534 tree label_errmsg;
6535 tree label_finish;
6536 tree memsz;
6537 tree al_vptr, al_len;
6538 /* If an expr3 is present, then store the tree for accessing its
6539 _vptr, and _len components in the variables, respectively. The
6540 element size, i.e. _vptr%size, is stored in expr3_esize. Any of
6541 the trees may be the NULL_TREE indicating that this is not
6542 available for expr3's type. */
6543 tree expr3, expr3_vptr, expr3_len, expr3_esize;
6544 /* Classify what expr3 stores. */
6545 enum { E3_UNSET = 0, E3_SOURCE, E3_MOLD, E3_DESC } e3_is;
6546 stmtblock_t block;
6547 stmtblock_t post;
6548 stmtblock_t final_block;
6549 tree nelems;
6550 bool upoly_expr, tmp_expr3_len_flag = false, al_len_needs_set, is_coarray;
6551 bool needs_caf_sync, caf_refs_comp;
6552 bool e3_has_nodescriptor = false;
6553 gfc_symtree *newsym = NULL;
6554 symbol_attribute caf_attr;
6555 gfc_actual_arglist *param_list;
6556 tree ts_string_length = NULL_TREE;
6557
6558 if (!code->ext.alloc.list)
6559 return NULL_TREE;
6560
6561 stat = tmp = memsz = al_vptr = al_len = NULL_TREE;
6562 expr3 = expr3_vptr = expr3_len = expr3_esize = NULL_TREE;
6563 label_errmsg = label_finish = errmsg = errlen = NULL_TREE;
6564 e3_is = E3_UNSET;
6565 is_coarray = needs_caf_sync = false;
6566
6567 gfc_init_block (&block);
6568 gfc_init_block (&post);
6569 gfc_init_block (&final_block);
6570
6571 /* STAT= (and maybe ERRMSG=) is present. */
6572 if (code->expr1)
6573 {
6574 /* STAT=. */
6575 tree gfc_int4_type_node = gfc_get_int_type (4);
6576 stat = gfc_create_var (gfc_int4_type_node, "stat");
6577
6578 /* ERRMSG= only makes sense with STAT=. */
6579 if (code->expr2)
6580 {
6581 gfc_init_se (&se, NULL);
6582 se.want_pointer = 1;
6583 gfc_conv_expr_lhs (&se, code->expr2);
6584 errmsg = se.expr;
6585 errlen = se.string_length;
6586 }
6587 else
6588 {
6589 errmsg = null_pointer_node;
6590 errlen = build_int_cst (gfc_charlen_type_node, 0);
6591 }
6592
6593 /* GOTO destinations. */
6594 label_errmsg = gfc_build_label_decl (NULL_TREE);
6595 label_finish = gfc_build_label_decl (NULL_TREE);
6596 TREE_USED (label_finish) = 0;
6597 }
6598
6599 /* When an expr3 is present evaluate it only once. The standards prevent a
6600 dependency of expr3 on the objects in the allocate list. An expr3 can
6601 be pre-evaluated in all cases. One just has to make sure, to use the
6602 correct way, i.e., to get the descriptor or to get a reference
6603 expression. */
6604 if (code->expr3)
6605 {
6606 bool vtab_needed = false, temp_var_needed = false,
6607 temp_obj_created = false;
6608
6609 is_coarray = gfc_is_coarray (code->expr3);
6610
6611 if (code->expr3->expr_type == EXPR_FUNCTION && !code->expr3->mold
6612 && (gfc_is_class_array_function (code->expr3)
6613 || gfc_is_alloc_class_scalar_function (code->expr3)))
6614 code->expr3->must_finalize = 1;
6615
6616 /* Figure whether we need the vtab from expr3. */
6617 for (al = code->ext.alloc.list; !vtab_needed && al != NULL;
6618 al = al->next)
6619 vtab_needed = (al->expr->ts.type == BT_CLASS);
6620
6621 gfc_init_se (&se, NULL);
6622 /* When expr3 is a variable, i.e., a very simple expression, then
6623 convert it once here. If one has a source expression that has
6624 substring references, part-refs, or %re/%im inquiries, wrap the
6625 entity in parentheses to force evaluation of the expression. */
6626 if (code->expr3->expr_type == EXPR_VARIABLE
6627 && is_subref_array (code->expr3))
6628 code->expr3 = gfc_get_parentheses (code->expr3);
6629
6630 if (code->expr3->expr_type == EXPR_VARIABLE
6631 || code->expr3->expr_type == EXPR_ARRAY
6632 || code->expr3->expr_type == EXPR_CONSTANT)
6633 {
6634 if (!code->expr3->mold
6635 || code->expr3->ts.type == BT_CHARACTER
6636 || vtab_needed
6637 || code->ext.alloc.arr_spec_from_expr3)
6638 {
6639 /* Convert expr3 to a tree. For all "simple" expression just
6640 get the descriptor or the reference, respectively, depending
6641 on the rank of the expr. */
6642 if (code->ext.alloc.arr_spec_from_expr3 || code->expr3->rank != 0)
6643 gfc_conv_expr_descriptor (&se, code->expr3);
6644 else
6645 {
6646 gfc_conv_expr_reference (&se, code->expr3);
6647
6648 /* gfc_conv_expr_reference wraps POINTER_PLUS_EXPR in a
6649 NOP_EXPR, which prevents gfortran from getting the vptr
6650 from the source=-expression. Remove the NOP_EXPR and go
6651 with the POINTER_PLUS_EXPR in this case. */
6652 if (code->expr3->ts.type == BT_CLASS
6653 && TREE_CODE (se.expr) == NOP_EXPR
6654 && (TREE_CODE (TREE_OPERAND (se.expr, 0))
6655 == POINTER_PLUS_EXPR
6656 || is_coarray))
6657 se.expr = TREE_OPERAND (se.expr, 0);
6658 }
6659 /* Create a temp variable only for component refs to prevent
6660 having to go through the full deref-chain each time and to
6661 simplify computation of array properties. */
6662 temp_var_needed = TREE_CODE (se.expr) == COMPONENT_REF;
6663 }
6664 }
6665 else
6666 {
6667 /* In all other cases evaluate the expr3. */
6668 symbol_attribute attr;
6669 /* Get the descriptor for all arrays, that are not allocatable or
6670 pointer, because the latter are descriptors already.
6671 The exception are function calls returning a class object:
6672 The descriptor is stored in their results _data component, which
6673 is easier to access, when first a temporary variable for the
6674 result is created and the descriptor retrieved from there. */
6675 attr = gfc_expr_attr (code->expr3);
6676 if (code->expr3->rank != 0
6677 && ((!attr.allocatable && !attr.pointer)
6678 || (code->expr3->expr_type == EXPR_FUNCTION
6679 && (code->expr3->ts.type != BT_CLASS
6680 || (code->expr3->value.function.isym
6681 && code->expr3->value.function.isym
6682 ->transformational)))))
6683 gfc_conv_expr_descriptor (&se, code->expr3);
6684 else
6685 gfc_conv_expr_reference (&se, code->expr3);
6686 if (code->expr3->ts.type == BT_CLASS)
6687 gfc_conv_class_to_class (&se, code->expr3,
6688 code->expr3->ts,
6689 false, true,
6690 false, false);
6691 temp_obj_created = temp_var_needed = !VAR_P (se.expr);
6692 }
6693 gfc_add_block_to_block (&block, &se.pre);
6694 if (code->expr3->must_finalize)
6695 {
6696 gfc_add_block_to_block (&final_block, &se.finalblock);
6697 gfc_add_block_to_block (&final_block, &se.post);
6698 }
6699 else
6700 gfc_add_block_to_block (&post, &se.post);
6701
6702 /* Special case when string in expr3 is scalar and has length zero. */
6703 if (code->expr3->ts.type == BT_CHARACTER
6704 && code->expr3->rank == 0
6705 && integer_zerop (se.string_length))
6706 {
6707 gfc_init_se (&se, NULL);
6708 temp_var_needed = false;
6709 expr3_len = build_zero_cst (gfc_charlen_type_node);
6710 e3_is = E3_MOLD;
6711 }
6712 /* Prevent aliasing, i.e., se.expr may be already a
6713 variable declaration. */
6714 else if (se.expr != NULL_TREE && temp_var_needed)
6715 {
6716 tree var, desc;
6717 tmp = (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
6718 || is_coarray
6719 || (code->expr3->ts.type == BT_CHARACTER
6720 && code->expr3->rank == 0)) ?
6721 se.expr
6722 : build_fold_indirect_ref_loc (input_location, se.expr);
6723
6724 /* Get the array descriptor and prepare it to be assigned to the
6725 temporary variable var. For classes the array descriptor is
6726 in the _data component and the object goes into the
6727 GFC_DECL_SAVED_DESCRIPTOR. */
6728 if (code->expr3->ts.type == BT_CLASS
6729 && code->expr3->rank != 0)
6730 {
6731 /* When an array_ref was in expr3, then the descriptor is the
6732 first operand. */
6733 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6734 {
6735 desc = TREE_OPERAND (tmp, 0);
6736 }
6737 else
6738 {
6739 desc = tmp;
6740 tmp = gfc_class_data_get (tmp);
6741 }
6742 if (code->ext.alloc.arr_spec_from_expr3)
6743 e3_is = E3_DESC;
6744 }
6745 else
6746 desc = !is_coarray ? se.expr
6747 : TREE_OPERAND (TREE_OPERAND (se.expr, 0), 0);
6748 /* We need a regular (non-UID) symbol here, therefore give a
6749 prefix. */
6750 var = gfc_create_var (TREE_TYPE (tmp), "source");
6751 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6752 {
6753 gfc_allocate_lang_decl (var);
6754 GFC_DECL_SAVED_DESCRIPTOR (var) = desc;
6755 }
6756 gfc_add_modify_loc (input_location, &block, var, tmp);
6757
6758 expr3 = var;
6759 if (se.string_length)
6760 /* Evaluate it assuming that it also is complicated like expr3. */
6761 expr3_len = gfc_evaluate_now (se.string_length, &block);
6762 }
6763 else
6764 {
6765 expr3 = se.expr;
6766 expr3_len = se.string_length;
6767 }
6768
6769 /* Deallocate any allocatable components in expressions that use a
6770 temporary object, i.e. are not a simple alias of to an EXPR_VARIABLE.
6771 E.g. temporaries of a function call need freeing of their components
6772 here. Explicit derived type allocation of class entities uses expr3
6773 to carry the default initializer. This must not be deallocated or
6774 finalized. */
6775 if ((code->expr3->ts.type == BT_DERIVED
6776 || code->expr3->ts.type == BT_CLASS)
6777 && (code->expr3->expr_type != EXPR_VARIABLE || temp_obj_created)
6778 && code->expr3->ts.u.derived->attr.alloc_comp
6779 && !code->expr3->must_finalize
6780 && !code->ext.alloc.expr3_not_explicit)
6781 {
6782 tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived,
6783 expr3, code->expr3->rank);
6784 gfc_prepend_expr_to_block (&post, tmp);
6785 }
6786
6787 /* Store what the expr3 is to be used for. */
6788 if (e3_is == E3_UNSET)
6789 e3_is = expr3 != NULL_TREE ?
6790 (code->ext.alloc.arr_spec_from_expr3 ?
6791 E3_DESC
6792 : (code->expr3->mold ? E3_MOLD : E3_SOURCE))
6793 : E3_UNSET;
6794
6795 /* Figure how to get the _vtab entry. This also obtains the tree
6796 expression for accessing the _len component, because only
6797 unlimited polymorphic objects, which are a subcategory of class
6798 types, have a _len component. */
6799 if (code->expr3->ts.type == BT_CLASS)
6800 {
6801 gfc_expr *rhs;
6802 tmp = expr3 != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (expr3)) ?
6803 build_fold_indirect_ref (expr3): expr3;
6804 /* Polymorphic SOURCE: VPTR must be determined at run time.
6805 expr3 may be a temporary array declaration, therefore check for
6806 GFC_CLASS_TYPE_P before trying to get the _vptr component. */
6807 if (tmp != NULL_TREE
6808 && (e3_is == E3_DESC
6809 || (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
6810 && (VAR_P (tmp) || !code->expr3->ref))
6811 || (VAR_P (tmp) && DECL_LANG_SPECIFIC (tmp))))
6812 tmp = gfc_class_vptr_get (expr3);
6813 else
6814 {
6815 rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6816 gfc_add_vptr_component (rhs);
6817 gfc_init_se (&se, NULL);
6818 se.want_pointer = 1;
6819 gfc_conv_expr (&se, rhs);
6820 tmp = se.expr;
6821 gfc_free_expr (rhs);
6822 }
6823 /* Set the element size. */
6824 expr3_esize = gfc_vptr_size_get (tmp);
6825 if (vtab_needed)
6826 expr3_vptr = tmp;
6827 /* Initialize the ref to the _len component. */
6828 if (expr3_len == NULL_TREE && UNLIMITED_POLY (code->expr3))
6829 {
6830 /* Same like for retrieving the _vptr. */
6831 if (expr3 != NULL_TREE && !code->expr3->ref)
6832 expr3_len = gfc_class_len_get (expr3);
6833 else
6834 {
6835 rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6836 gfc_add_len_component (rhs);
6837 gfc_init_se (&se, NULL);
6838 gfc_conv_expr (&se, rhs);
6839 expr3_len = se.expr;
6840 gfc_free_expr (rhs);
6841 }
6842 }
6843 }
6844 else
6845 {
6846 /* When the object to allocate is polymorphic type, then it
6847 needs its vtab set correctly, so deduce the required _vtab
6848 and _len from the source expression. */
6849 if (vtab_needed)
6850 {
6851 /* VPTR is fixed at compile time. */
6852 gfc_symbol *vtab;
6853
6854 vtab = gfc_find_vtab (&code->expr3->ts);
6855 gcc_assert (vtab);
6856 expr3_vptr = gfc_get_symbol_decl (vtab);
6857 expr3_vptr = gfc_build_addr_expr (NULL_TREE,
6858 expr3_vptr);
6859 }
6860 /* _len component needs to be set, when ts is a character
6861 array. */
6862 if (expr3_len == NULL_TREE
6863 && code->expr3->ts.type == BT_CHARACTER)
6864 {
6865 if (code->expr3->ts.u.cl
6866 && code->expr3->ts.u.cl->length)
6867 {
6868 gfc_init_se (&se, NULL);
6869 gfc_conv_expr (&se, code->expr3->ts.u.cl->length);
6870 gfc_add_block_to_block (&block, &se.pre);
6871 expr3_len = gfc_evaluate_now (se.expr, &block);
6872 }
6873 gcc_assert (expr3_len);
6874 }
6875 /* For character arrays only the kind's size is needed, because
6876 the array mem_size is _len * (elem_size = kind_size).
6877 For all other get the element size in the normal way. */
6878 if (code->expr3->ts.type == BT_CHARACTER)
6879 expr3_esize = TYPE_SIZE_UNIT (
6880 gfc_get_char_type (code->expr3->ts.kind));
6881 else
6882 expr3_esize = TYPE_SIZE_UNIT (
6883 gfc_typenode_for_spec (&code->expr3->ts));
6884 }
6885 gcc_assert (expr3_esize);
6886 expr3_esize = fold_convert (sizetype, expr3_esize);
6887 if (e3_is == E3_MOLD)
6888 /* The expr3 is no longer valid after this point. */
6889 expr3 = NULL_TREE;
6890 }
6891 else if (code->ext.alloc.ts.type != BT_UNKNOWN)
6892 {
6893 /* Compute the explicit typespec given only once for all objects
6894 to allocate. */
6895 if (code->ext.alloc.ts.type != BT_CHARACTER)
6896 expr3_esize = TYPE_SIZE_UNIT (
6897 gfc_typenode_for_spec (&code->ext.alloc.ts));
6898 else if (code->ext.alloc.ts.u.cl->length != NULL)
6899 {
6900 gfc_expr *sz;
6901 sz = gfc_copy_expr (code->ext.alloc.ts.u.cl->length);
6902 gfc_init_se (&se_sz, NULL);
6903 gfc_conv_expr (&se_sz, sz);
6904 gfc_free_expr (sz);
6905 ts_string_length = fold_convert (gfc_charlen_type_node, se_sz.expr);
6906 tmp = gfc_get_char_type (code->ext.alloc.ts.kind);
6907 tmp = TYPE_SIZE_UNIT (tmp);
6908 tmp = fold_convert (TREE_TYPE (se_sz.expr), tmp);
6909 gfc_add_block_to_block (&block, &se_sz.pre);
6910 expr3_esize = fold_build2_loc (input_location, MULT_EXPR,
6911 TREE_TYPE (se_sz.expr),
6912 tmp, se_sz.expr);
6913 expr3_esize = gfc_evaluate_now (expr3_esize, &block);
6914 }
6915 else
6916 expr3_esize = NULL_TREE;
6917 }
6918
6919 /* The routine gfc_trans_assignment () already implements all
6920 techniques needed. Unfortunately we may have a temporary
6921 variable for the source= expression here. When that is the
6922 case convert this variable into a temporary gfc_expr of type
6923 EXPR_VARIABLE and used it as rhs for the assignment. The
6924 advantage is, that we get scalarizer support for free,
6925 don't have to take care about scalar to array treatment and
6926 will benefit of every enhancements gfc_trans_assignment ()
6927 gets.
6928 No need to check whether e3_is is E3_UNSET, because that is
6929 done by expr3 != NULL_TREE.
6930 Exclude variables since the following block does not handle
6931 array sections. In any case, there is no harm in sending
6932 variables to gfc_trans_assignment because there is no
6933 evaluation of variables. */
6934 if (code->expr3)
6935 {
6936 if (code->expr3->expr_type != EXPR_VARIABLE
6937 && e3_is != E3_MOLD && expr3 != NULL_TREE
6938 && DECL_P (expr3) && DECL_ARTIFICIAL (expr3))
6939 {
6940 /* Build a temporary symtree and symbol. Do not add it to the current
6941 namespace to prevent accidentaly modifying a colliding
6942 symbol's as. */
6943 newsym = XCNEW (gfc_symtree);
6944 /* The name of the symtree should be unique, because gfc_create_var ()
6945 took care about generating the identifier. */
6946 newsym->name
6947 = gfc_get_string ("%s", IDENTIFIER_POINTER (DECL_NAME (expr3)));
6948 newsym->n.sym = gfc_new_symbol (newsym->name, NULL);
6949 /* The backend_decl is known. It is expr3, which is inserted
6950 here. */
6951 newsym->n.sym->backend_decl = expr3;
6952 e3rhs = gfc_get_expr ();
6953 e3rhs->rank = code->expr3->rank;
6954 e3rhs->corank = code->expr3->corank;
6955 e3rhs->symtree = newsym;
6956 /* Mark the symbol referenced or gfc_trans_assignment will bug. */
6957 newsym->n.sym->attr.referenced = 1;
6958 e3rhs->expr_type = EXPR_VARIABLE;
6959 e3rhs->where = code->expr3->where;
6960 /* Set the symbols type, upto it was BT_UNKNOWN. */
6961 if (IS_CLASS_ARRAY (code->expr3)
6962 && code->expr3->expr_type == EXPR_FUNCTION
6963 && code->expr3->value.function.isym
6964 && code->expr3->value.function.isym->transformational)
6965 {
6966 e3rhs->ts = CLASS_DATA (code->expr3)->ts;
6967 }
6968 else if (code->expr3->ts.type == BT_CLASS
6969 && !GFC_CLASS_TYPE_P (TREE_TYPE (expr3)))
6970 e3rhs->ts = CLASS_DATA (code->expr3)->ts;
6971 else
6972 e3rhs->ts = code->expr3->ts;
6973 newsym->n.sym->ts = e3rhs->ts;
6974 /* Check whether the expr3 is array valued. */
6975 if (e3rhs->rank)
6976 {
6977 gfc_array_spec *arr;
6978 arr = gfc_get_array_spec ();
6979 arr->rank = e3rhs->rank;
6980 arr->corank = e3rhs->corank;
6981 arr->type = AS_DEFERRED;
6982 /* Set the dimension and pointer attribute for arrays
6983 to be on the safe side. */
6984 newsym->n.sym->attr.dimension = 1;
6985 newsym->n.sym->attr.pointer = 1;
6986 newsym->n.sym->as = arr;
6987 if (IS_CLASS_ARRAY (code->expr3)
6988 && code->expr3->expr_type == EXPR_FUNCTION
6989 && code->expr3->value.function.isym
6990 && code->expr3->value.function.isym->transformational)
6991 {
6992 gfc_array_spec *tarr;
6993 tarr = gfc_get_array_spec ();
6994 *tarr = *arr;
6995 e3rhs->ts.u.derived->as = tarr;
6996 }
6997 gfc_add_full_array_ref (e3rhs, arr);
6998 }
6999 else if (POINTER_TYPE_P (TREE_TYPE (expr3)))
7000 newsym->n.sym->attr.pointer = 1;
7001 /* The string length is known, too. Set it for char arrays. */
7002 if (e3rhs->ts.type == BT_CHARACTER)
7003 newsym->n.sym->ts.u.cl->backend_decl = expr3_len;
7004 gfc_commit_symbol (newsym->n.sym);
7005 }
7006 else
7007 e3rhs = gfc_copy_expr (code->expr3);
7008
7009 // We need to propagate the bounds of the expr3 for source=/mold=.
7010 // However, for non-named arrays, the lbound has to be 1 and neither the
7011 // bound used inside the called function even when returning an
7012 // allocatable/pointer nor the zero used internally.
7013 if (e3_is == E3_DESC
7014 && code->expr3->expr_type != EXPR_VARIABLE)
7015 e3_has_nodescriptor = true;
7016 }
7017
7018 /* Loop over all objects to allocate. */
7019 for (al = code->ext.alloc.list; al != NULL; al = al->next)
7020 {
7021 expr = gfc_copy_expr (al->expr);
7022 /* UNLIMITED_POLY () needs the _data component to be set, when
7023 expr is a unlimited polymorphic object. But the _data component
7024 has not been set yet, so check the derived type's attr for the
7025 unlimited polymorphic flag to be safe. */
7026 upoly_expr = UNLIMITED_POLY (expr)
7027 || (expr->ts.type == BT_DERIVED
7028 && expr->ts.u.derived->attr.unlimited_polymorphic);
7029 gfc_init_se (&se, NULL);
7030
7031 /* For class types prepare the expressions to ref the _vptr
7032 and the _len component. The latter for unlimited polymorphic
7033 types only. */
7034 if (expr->ts.type == BT_CLASS)
7035 {
7036 gfc_expr *expr_ref_vptr, *expr_ref_len;
7037 gfc_add_data_component (expr);
7038 /* Prep the vptr handle. */
7039 expr_ref_vptr = gfc_copy_expr (al->expr);
7040 gfc_add_vptr_component (expr_ref_vptr);
7041 se.want_pointer = 1;
7042 gfc_conv_expr (&se, expr_ref_vptr);
7043 al_vptr = se.expr;
7044 se.want_pointer = 0;
7045 gfc_free_expr (expr_ref_vptr);
7046 /* Allocated unlimited polymorphic objects always have a _len
7047 component. */
7048 if (upoly_expr)
7049 {
7050 expr_ref_len = gfc_copy_expr (al->expr);
7051 gfc_add_len_component (expr_ref_len);
7052 gfc_conv_expr (&se, expr_ref_len);
7053 al_len = se.expr;
7054 gfc_free_expr (expr_ref_len);
7055 }
7056 else
7057 /* In a loop ensure that all loop variable dependent variables
7058 are initialized at the same spot in all execution paths. */
7059 al_len = NULL_TREE;
7060 }
7061 else
7062 al_vptr = al_len = NULL_TREE;
7063
7064 se.want_pointer = 1;
7065 se.descriptor_only = 1;
7066
7067 gfc_conv_expr (&se, expr);
7068 if (expr->ts.type == BT_CHARACTER && expr->ts.deferred)
7069 /* se.string_length now stores the .string_length variable of expr
7070 needed to allocate character(len=:) arrays. */
7071 al_len = se.string_length;
7072
7073 al_len_needs_set = al_len != NULL_TREE;
7074 /* When allocating an array one cannot use much of the
7075 pre-evaluated expr3 expressions, because for most of them the
7076 scalarizer is needed which is not available in the pre-evaluation
7077 step. Therefore gfc_array_allocate () is responsible (and able)
7078 to handle the complete array allocation. Only the element size
7079 needs to be provided, which is done most of the time by the
7080 pre-evaluation step. */
7081 nelems = NULL_TREE;
7082 if (expr3_len && (code->expr3->ts.type == BT_CHARACTER
7083 || code->expr3->ts.type == BT_CLASS))
7084 {
7085 /* When al is an array, then the element size for each element
7086 in the array is needed, which is the product of the len and
7087 esize for char arrays. For unlimited polymorphics len can be
7088 zero, therefore take the maximum of len and one. */
7089 tree lhs_len;
7090
7091 /* If an allocatable character variable has fixed length, use it.
7092 Otherwise use source length. As different lengths are not
7093 allowed by the standard, generate a runtime check. */
7094 if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7095 {
7096 gfc_trans_same_strlen_check ("ALLOCATE with SOURCE= or MOLD=",
7097 &code->expr3->where,
7098 se.string_length, expr3_len,
7099 &block);
7100 lhs_len = fold_convert (TREE_TYPE (expr3_len), se.string_length);
7101 }
7102 else
7103 lhs_len = expr3_len;
7104
7105 tmp = fold_build2_loc (input_location, MAX_EXPR,
7106 TREE_TYPE (expr3_len),
7107 lhs_len, fold_convert (TREE_TYPE (expr3_len),
7108 integer_one_node));
7109 tmp = fold_build2_loc (input_location, MULT_EXPR,
7110 TREE_TYPE (expr3_esize), expr3_esize,
7111 fold_convert (TREE_TYPE (expr3_esize), tmp));
7112 }
7113 else
7114 tmp = expr3_esize;
7115
7116 /* Create runtime check for ALLOCATE of character with type-spec. */
7117 if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred
7118 && ts_string_length
7119 && se.string_length)
7120 gfc_trans_same_strlen_check ("ALLOCATE with type-spec",
7121 &al->expr->where,
7122 ts_string_length, se.string_length,
7123 &block);
7124
7125 gfc_omp_namelist *omp_alloc_item = NULL;
7126 if (omp_allocate)
7127 {
7128 gfc_omp_namelist *n = NULL;
7129 gfc_omp_namelist *n_null = NULL;
7130 for (n = omp_allocate; n; n = n->next)
7131 {
7132 if (n->sym == NULL)
7133 {
7134 n_null = n;
7135 continue;
7136 }
7137 if (expr->expr_type == EXPR_VARIABLE
7138 && expr->symtree->n.sym == n->sym)
7139 {
7140 gfc_ref *ref;
7141 for (ref = expr->ref; ref; ref = ref->next)
7142 if (ref->type == REF_COMPONENT)
7143 break;
7144 if (ref == NULL)
7145 break;
7146 }
7147 }
7148 omp_alloc_item = n ? n : n_null;
7149
7150 }
7151
7152 if (!gfc_array_allocate (&se, expr, stat, errmsg, errlen,
7153 label_finish, tmp, &nelems,
7154 e3rhs ? e3rhs : code->expr3,
7155 e3_is == E3_DESC ? expr3 : NULL_TREE,
7156 e3_has_nodescriptor, omp_alloc_item,
7157 code->ext.alloc.ts.type != BT_UNKNOWN))
7158 {
7159 /* A scalar or derived type. First compute the size to
7160 allocate.
7161
7162 expr3_len is set when expr3 is an unlimited polymorphic
7163 object or a deferred length string.
7164
7165 If an allocatable character variable has fixed length, use it.
7166 Otherwise use source length. As different lengths are not
7167 allowed by the standard, a runtime check was inserted
7168 above. */
7169 if (expr3_len != NULL_TREE)
7170 {
7171 tree lhs_len;
7172 if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7173 lhs_len = fold_convert (TREE_TYPE (expr3_len),
7174 se.string_length);
7175 else
7176 lhs_len = expr3_len;
7177
7178 tmp = fold_convert (TREE_TYPE (expr3_esize), lhs_len);
7179 tmp = fold_build2_loc (input_location, MULT_EXPR,
7180 TREE_TYPE (expr3_esize),
7181 expr3_esize, tmp);
7182 if (code->expr3->ts.type != BT_CLASS)
7183 /* expr3 is a deferred length string, i.e., we are
7184 done. */
7185 memsz = tmp;
7186 else
7187 {
7188 /* For unlimited polymorphic enties build
7189 (len > 0) ? element_size * len : element_size
7190 to compute the number of bytes to allocate.
7191 This allows the allocation of unlimited polymorphic
7192 objects from an expr3 that is also unlimited
7193 polymorphic and stores a _len dependent object,
7194 e.g., a string. */
7195 memsz = fold_build2_loc (input_location, GT_EXPR,
7196 logical_type_node, expr3_len,
7197 build_zero_cst
7198 (TREE_TYPE (expr3_len)));
7199 memsz = fold_build3_loc (input_location, COND_EXPR,
7200 TREE_TYPE (expr3_esize),
7201 memsz, tmp, expr3_esize);
7202 }
7203 }
7204 else if (expr3_esize != NULL_TREE)
7205 /* Any other object in expr3 just needs element size in
7206 bytes. */
7207 memsz = expr3_esize;
7208 else if ((expr->ts.type == BT_CHARACTER && expr->ts.deferred)
7209 || (upoly_expr
7210 && code->ext.alloc.ts.type == BT_CHARACTER))
7211 {
7212 /* Allocating deferred length char arrays need the length
7213 to allocate in the alloc_type_spec. But also unlimited
7214 polymorphic objects may be allocated as char arrays.
7215 Both are handled here. */
7216 gfc_init_se (&se_sz, NULL);
7217 gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7218 gfc_add_block_to_block (&se.pre, &se_sz.pre);
7219 se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
7220 gfc_add_block_to_block (&se.pre, &se_sz.post);
7221 expr3_len = se_sz.expr;
7222 tmp_expr3_len_flag = true;
7223 tmp = TYPE_SIZE_UNIT (
7224 gfc_get_char_type (code->ext.alloc.ts.kind));
7225 memsz = fold_build2_loc (input_location, MULT_EXPR,
7226 TREE_TYPE (tmp),
7227 fold_convert (TREE_TYPE (tmp),
7228 expr3_len),
7229 tmp);
7230 }
7231 else if (expr->ts.type == BT_CHARACTER)
7232 {
7233 /* Compute the number of bytes needed to allocate a fixed
7234 length char array. */
7235 gcc_assert (se.string_length != NULL_TREE);
7236 tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind));
7237 memsz = fold_build2_loc (input_location, MULT_EXPR,
7238 TREE_TYPE (tmp), tmp,
7239 fold_convert (TREE_TYPE (tmp),
7240 se.string_length));
7241 }
7242 else if (code->ext.alloc.ts.type != BT_UNKNOWN)
7243 /* Handle all types, where the alloc_type_spec is set. */
7244 memsz = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&code->ext.alloc.ts));
7245 else
7246 /* Handle size computation of the type declared to alloc. */
7247 memsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (se.expr)));
7248
7249 bool use_coarray_alloc
7250 = (flag_coarray == GFC_FCOARRAY_LIB
7251 && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7252 .codimension);
7253 tree omp_cond = NULL_TREE;
7254 tree omp_alt_alloc = NULL_TREE;
7255 tree succ_add_expr = NULL_TREE;
7256 if (!use_coarray_alloc && omp_alloc_item)
7257 {
7258 tree align, alloc, sz;
7259 gfc_se se2;
7260
7261 omp_cond = boolean_true_node;
7262 if (omp_alloc_item->u2.allocator)
7263 {
7264 gfc_init_se (&se2, NULL);
7265 gfc_conv_expr (&se2, omp_alloc_item->u2.allocator);
7266 gfc_add_block_to_block (&se.pre, &se2.pre);
7267 alloc = gfc_evaluate_now (se2.expr, &se.pre);
7268 gfc_add_block_to_block (&se.pre, &se2.post);
7269 }
7270 else
7271 alloc = build_zero_cst (ptr_type_node);
7272 tmp = TREE_TYPE (TREE_TYPE (se.expr));
7273 if (tmp == void_type_node)
7274 tmp = gfc_typenode_for_spec (&expr->ts, 0);
7275 if (omp_alloc_item->u.align)
7276 {
7277 gfc_init_se (&se2, NULL);
7278 gfc_conv_expr (&se2, omp_alloc_item->u.align);
7279 gcc_assert (CONSTANT_CLASS_P (se2.expr)
7280 && se2.pre.head == NULL
7281 && se2.post.head == NULL);
7282 align = build_int_cst (size_type_node,
7283 MAX (tree_to_uhwi (se2.expr),
7284 TYPE_ALIGN_UNIT (tmp)));
7285 }
7286 else
7287 align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
7288 sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7289 fold_convert (size_type_node, memsz),
7290 build_int_cst (size_type_node, 1));
7291 omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
7292 DECL_ATTRIBUTES (omp_alt_alloc)
7293 = tree_cons (get_identifier ("omp allocator"),
7294 build_tree_list (NULL_TREE, alloc),
7295 DECL_ATTRIBUTES (omp_alt_alloc));
7296 omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
7297 succ_add_expr = gfc_omp_call_add_alloc (se.expr);
7298 }
7299
7300 /* Store the caf-attributes for latter use. */
7301 if (use_coarray_alloc)
7302 {
7303 /* Scalar allocatable components in coarray'ed derived types make
7304 it here and are treated now. */
7305 tree caf_decl, token;
7306 gfc_se caf_se;
7307
7308 is_coarray = true;
7309 /* Set flag, to add synchronize after the allocate. */
7310 needs_caf_sync = needs_caf_sync
7311 || caf_attr.coarray_comp || !caf_refs_comp;
7312
7313 gfc_init_se (&caf_se, NULL);
7314
7315 caf_decl = gfc_get_tree_for_caf_expr (expr);
7316 gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl,
7317 NULL_TREE, NULL);
7318 gfc_add_block_to_block (&se.pre, &caf_se.pre);
7319 gfc_allocate_allocatable (&se.pre, se.expr, memsz,
7320 gfc_build_addr_expr (NULL_TREE, token),
7321 NULL_TREE, NULL_TREE, NULL_TREE,
7322 label_finish, expr, 1);
7323 }
7324 /* Allocate - for non-pointers with re-alloc checking. */
7325 else if (gfc_expr_attr (expr).allocatable)
7326 gfc_allocate_allocatable (&se.pre, se.expr, memsz,
7327 NULL_TREE, stat, errmsg, errlen,
7328 label_finish, expr, 0,
7329 omp_cond, omp_alt_alloc, succ_add_expr);
7330 else
7331 gfc_allocate_using_malloc (&se.pre, se.expr, memsz, stat,
7332 omp_cond, omp_alt_alloc, succ_add_expr);
7333 }
7334 else
7335 {
7336 /* Allocating coarrays needs a sync after the allocate executed.
7337 Set the flag to add the sync after all objects are allocated. */
7338 if (flag_coarray == GFC_FCOARRAY_LIB
7339 && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7340 .codimension)
7341 {
7342 is_coarray = true;
7343 needs_caf_sync = needs_caf_sync
7344 || caf_attr.coarray_comp || !caf_refs_comp;
7345 }
7346
7347 if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7348 && expr3_len != NULL_TREE)
7349 {
7350 /* Arrays need to have a _len set before the array
7351 descriptor is filled. */
7352 gfc_add_modify (&block, al_len,
7353 fold_convert (TREE_TYPE (al_len), expr3_len));
7354 /* Prevent setting the length twice. */
7355 al_len_needs_set = false;
7356 }
7357 else if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7358 && code->ext.alloc.ts.u.cl->length)
7359 {
7360 /* Cover the cases where a string length is explicitly
7361 specified by a type spec for deferred length character
7362 arrays or unlimited polymorphic objects without a
7363 source= or mold= expression. */
7364 gfc_init_se (&se_sz, NULL);
7365 gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7366 gfc_add_block_to_block (&block, &se_sz.pre);
7367 gfc_add_modify (&block, al_len,
7368 fold_convert (TREE_TYPE (al_len),
7369 se_sz.expr));
7370 al_len_needs_set = false;
7371 }
7372 }
7373
7374 gfc_add_block_to_block (&block, &se.pre);
7375
7376 /* Error checking -- Note: ERRMSG only makes sense with STAT. */
7377 if (code->expr1)
7378 {
7379 tmp = build1_v (GOTO_EXPR, label_errmsg);
7380 parm = fold_build2_loc (input_location, NE_EXPR,
7381 logical_type_node, stat,
7382 build_int_cst (TREE_TYPE (stat), 0));
7383 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
7384 gfc_unlikely (parm, PRED_FORTRAN_FAIL_ALLOC),
7385 tmp, build_empty_stmt (input_location));
7386 gfc_add_expr_to_block (&block, tmp);
7387 }
7388
7389 /* Set the vptr only when no source= is set. When source= is set, then
7390 the trans_assignment below will set the vptr. */
7391 if (al_vptr != NULL_TREE && (!code->expr3 || code->expr3->mold))
7392 {
7393 if (expr3_vptr != NULL_TREE)
7394 /* The vtab is already known, so just assign it. */
7395 gfc_add_modify (&block, al_vptr,
7396 fold_convert (TREE_TYPE (al_vptr), expr3_vptr));
7397 else
7398 {
7399 /* VPTR is fixed at compile time. */
7400 gfc_symbol *vtab;
7401 gfc_typespec *ts;
7402
7403 if (code->expr3)
7404 /* Although expr3 is pre-evaluated above, it may happen,
7405 that for arrays or in mold= cases the pre-evaluation
7406 was not successful. In these rare cases take the vtab
7407 from the typespec of expr3 here. */
7408 ts = &code->expr3->ts;
7409 else if (code->ext.alloc.ts.type == BT_DERIVED || upoly_expr)
7410 /* The alloc_type_spec gives the type to allocate or the
7411 al is unlimited polymorphic, which enforces the use of
7412 an alloc_type_spec that is not necessarily a BT_DERIVED. */
7413 ts = &code->ext.alloc.ts;
7414 else
7415 /* Prepare for setting the vtab as declared. */
7416 ts = &expr->ts;
7417
7418 vtab = gfc_find_vtab (ts);
7419 gcc_assert (vtab);
7420 tmp = gfc_build_addr_expr (NULL_TREE,
7421 gfc_get_symbol_decl (vtab));
7422 gfc_add_modify (&block, al_vptr,
7423 fold_convert (TREE_TYPE (al_vptr), tmp));
7424 }
7425 }
7426
7427 /* Add assignment for string length. */
7428 if (al_len != NULL_TREE && al_len_needs_set)
7429 {
7430 if (expr3_len != NULL_TREE)
7431 {
7432 gfc_add_modify (&block, al_len,
7433 fold_convert (TREE_TYPE (al_len),
7434 expr3_len));
7435 /* When tmp_expr3_len_flag is set, then expr3_len is
7436 abused to carry the length information from the
7437 alloc_type. Clear it to prevent setting incorrect len
7438 information in future loop iterations. */
7439 if (tmp_expr3_len_flag)
7440 /* No need to reset tmp_expr3_len_flag, because the
7441 presence of an expr3 cannot change within in the
7442 loop. */
7443 expr3_len = NULL_TREE;
7444 }
7445 else if (code->ext.alloc.ts.type == BT_CHARACTER
7446 && code->ext.alloc.ts.u.cl->length)
7447 {
7448 /* Cover the cases where a string length is explicitly
7449 specified by a type spec for deferred length character
7450 arrays or unlimited polymorphic objects without a
7451 source= or mold= expression. */
7452 if (expr3_esize == NULL_TREE || code->ext.alloc.ts.kind != 1)
7453 {
7454 gfc_init_se (&se_sz, NULL);
7455 gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7456 gfc_add_block_to_block (&block, &se_sz.pre);
7457 gfc_add_modify (&block, al_len,
7458 fold_convert (TREE_TYPE (al_len),
7459 se_sz.expr));
7460 }
7461 else
7462 gfc_add_modify (&block, al_len,
7463 fold_convert (TREE_TYPE (al_len),
7464 expr3_esize));
7465 }
7466 else
7467 /* No length information needed, because type to allocate
7468 has no length. Set _len to 0. */
7469 gfc_add_modify (&block, al_len,
7470 fold_convert (TREE_TYPE (al_len),
7471 integer_zero_node));
7472 }
7473
7474 init_expr = NULL;
7475 if (code->expr3 && !code->expr3->mold && e3_is != E3_MOLD)
7476 {
7477 /* Initialization via SOURCE block (or static default initializer).
7478 Switch off automatic reallocation since we have just done the
7479 ALLOCATE. */
7480 int realloc_lhs = flag_realloc_lhs;
7481 gfc_expr *init_expr = gfc_expr_to_initialize (expr);
7482 gfc_expr *rhs = e3rhs ? e3rhs : gfc_copy_expr (code->expr3);
7483 flag_realloc_lhs = 0;
7484
7485 /* The handling of code->expr3 above produces a derived type of
7486 type "STAR", whose size defaults to size(void*). In order to
7487 have the right type information for the assignment, we must
7488 reconstruct an unlimited polymorphic rhs. */
7489 if (UNLIMITED_POLY (code->expr3)
7490 && e3rhs && e3rhs->ts.type == BT_DERIVED
7491 && !strcmp (e3rhs->ts.u.derived->name, "STAR"))
7492 {
7493 gfc_ref *ref;
7494 gcc_assert (TREE_CODE (expr3_vptr) == COMPONENT_REF);
7495 tmp = gfc_create_var (gfc_typenode_for_spec (&code->expr3->ts),
7496 "e3");
7497 gfc_add_modify (&block, tmp,
7498 gfc_get_class_from_expr (expr3_vptr));
7499 rhs->symtree->n.sym->backend_decl = tmp;
7500 rhs->ts = code->expr3->ts;
7501 rhs->symtree->n.sym->ts = rhs->ts;
7502 for (ref = init_expr->ref; ref; ref = ref->next)
7503 {
7504 /* Copy over the lhs _data component ref followed by the
7505 full array reference for source expressions with rank.
7506 Otherwise, just copy the _data component ref. */
7507 if (code->expr3->rank
7508 && ref && ref->next && !ref->next->next)
7509 {
7510 rhs->ref = gfc_copy_ref (ref);
7511 break;
7512 }
7513 else if ((init_expr->rank && !code->expr3->rank
7514 && ref && ref->next && !ref->next->next)
7515 || (ref && !ref->next))
7516 {
7517 rhs->ref = gfc_copy_ref (ref);
7518 gfc_free_ref_list (rhs->ref->next);
7519 rhs->ref->next = NULL;
7520 break;
7521 }
7522 }
7523 }
7524
7525 /* Set the symbol to be artificial so that the result is not finalized. */
7526 init_expr->symtree->n.sym->attr.artificial = 1;
7527 tmp = gfc_trans_assignment (init_expr, rhs, true, false, true,
7528 false);
7529 init_expr->symtree->n.sym->attr.artificial = 0;
7530
7531 flag_realloc_lhs = realloc_lhs;
7532 /* Free the expression allocated for init_expr. */
7533 gfc_free_expr (init_expr);
7534 if (rhs != e3rhs)
7535 gfc_free_expr (rhs);
7536 gfc_add_expr_to_block (&block, tmp);
7537 }
7538 /* Set KIND and LEN PDT components and allocate those that are
7539 parameterized. */
7540 else if (expr->ts.type == BT_DERIVED
7541 && expr->ts.u.derived->attr.pdt_type)
7542 {
7543 if (code->expr3 && code->expr3->param_list)
7544 param_list = code->expr3->param_list;
7545 else if (expr->param_list)
7546 param_list = expr->param_list;
7547 else
7548 param_list = expr->symtree->n.sym->param_list;
7549 tmp = gfc_allocate_pdt_comp (expr->ts.u.derived, se.expr,
7550 expr->rank, param_list);
7551 gfc_add_expr_to_block (&block, tmp);
7552 }
7553 /* Ditto for CLASS expressions. */
7554 else if (expr->ts.type == BT_CLASS
7555 && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type)
7556 {
7557 if (code->expr3 && code->expr3->param_list)
7558 param_list = code->expr3->param_list;
7559 else if (expr->param_list)
7560 param_list = expr->param_list;
7561 else
7562 param_list = expr->symtree->n.sym->param_list;
7563 tmp = gfc_allocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7564 se.expr, expr->rank, param_list);
7565 gfc_add_expr_to_block (&block, tmp);
7566 }
7567 else if (code->expr3 && code->expr3->mold
7568 && code->expr3->ts.type == BT_CLASS)
7569 {
7570 /* Use class_init_assign to initialize expr. */
7571 gfc_code *ini;
7572 ini = gfc_get_code (EXEC_ALLOCATE);
7573 ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr, true);
7574 tmp = gfc_trans_class_init_assign (ini);
7575 gfc_free_statements (ini);
7576 if (tmp != NULL_TREE)
7577 gfc_add_expr_to_block (&block, tmp);
7578 }
7579 else if ((init_expr = allocate_get_initializer (code, expr)))
7580 {
7581 /* Use class_init_assign to initialize expr. */
7582 gfc_code *ini;
7583 int realloc_lhs = flag_realloc_lhs;
7584 ini = gfc_get_code (EXEC_INIT_ASSIGN);
7585 ini->expr1 = gfc_expr_to_initialize (expr);
7586 ini->expr2 = init_expr;
7587 flag_realloc_lhs = 0;
7588 tmp= gfc_trans_init_assign (ini);
7589 flag_realloc_lhs = realloc_lhs;
7590 gfc_free_statements (ini);
7591 /* Init_expr is freeed by above free_statements, just need to null
7592 it here. */
7593 init_expr = NULL;
7594 gfc_add_expr_to_block (&block, tmp);
7595 }
7596
7597 /* Nullify all pointers in derived type coarrays. This registers a
7598 token for them which allows their allocation. */
7599 if (is_coarray)
7600 {
7601 gfc_symbol *type = NULL;
7602 symbol_attribute caf_attr;
7603 int rank = 0;
7604 if (code->ext.alloc.ts.type == BT_DERIVED
7605 && code->ext.alloc.ts.u.derived->attr.pointer_comp)
7606 {
7607 type = code->ext.alloc.ts.u.derived;
7608 rank = type->attr.dimension ? type->as->rank : 0;
7609 gfc_clear_attr (&caf_attr);
7610 }
7611 else if (expr->ts.type == BT_DERIVED
7612 && expr->ts.u.derived->attr.pointer_comp)
7613 {
7614 type = expr->ts.u.derived;
7615 rank = expr->rank;
7616 caf_attr = gfc_caf_attr (expr, true);
7617 }
7618
7619 /* Initialize the tokens of pointer components in derived type
7620 coarrays. */
7621 if (type)
7622 {
7623 tmp = (caf_attr.codimension && !caf_attr.dimension)
7624 ? gfc_conv_descriptor_data_get (se.expr) : se.expr;
7625 tmp = gfc_nullify_alloc_comp (type, tmp, rank,
7626 GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
7627 gfc_add_expr_to_block (&block, tmp);
7628 }
7629 }
7630
7631 gfc_free_expr (expr);
7632 } // for-loop
7633
7634 if (e3rhs)
7635 {
7636 if (newsym)
7637 {
7638 gfc_free_symbol (newsym->n.sym);
7639 XDELETE (newsym);
7640 }
7641 gfc_free_expr (e3rhs);
7642 }
7643 /* STAT. */
7644 if (code->expr1)
7645 {
7646 tmp = build1_v (LABEL_EXPR, label_errmsg);
7647 gfc_add_expr_to_block (&block, tmp);
7648 }
7649
7650 /* ERRMSG - only useful if STAT is present. */
7651 if (code->expr1 && code->expr2)
7652 {
7653 const char *msg = "Attempt to allocate an allocated object";
7654 const char *oommsg = "Insufficient virtual memory";
7655 tree slen, dlen, errmsg_str, oom_str, oom_loc;
7656 stmtblock_t errmsg_block;
7657
7658 gfc_init_block (&errmsg_block);
7659
7660 errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
7661 gfc_add_modify (&errmsg_block, errmsg_str,
7662 gfc_build_addr_expr (pchar_type_node,
7663 gfc_build_localized_cstring_const (msg)));
7664
7665 slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
7666 dlen = gfc_get_expr_charlen (code->expr2);
7667 slen = fold_build2_loc (input_location, MIN_EXPR,
7668 TREE_TYPE (slen), dlen, slen);
7669
7670 gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7671 code->expr2->ts.kind,
7672 slen, errmsg_str,
7673 gfc_default_character_kind);
7674 dlen = gfc_finish_block (&errmsg_block);
7675
7676 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7677 stat, build_int_cst (TREE_TYPE (stat),
7678 LIBERROR_ALLOCATION));
7679
7680 tmp = build3_v (COND_EXPR, tmp,
7681 dlen, build_empty_stmt (input_location));
7682
7683 gfc_add_expr_to_block (&block, tmp);
7684
7685 oom_str = gfc_create_var (pchar_type_node, "OOMMSG");
7686 oom_loc = gfc_build_localized_cstring_const (oommsg);
7687 gfc_add_modify (&errmsg_block, oom_str,
7688 gfc_build_addr_expr (pchar_type_node, oom_loc));
7689
7690 slen = build_int_cst (gfc_charlen_type_node, strlen (oommsg));
7691 dlen = gfc_get_expr_charlen (code->expr2);
7692 slen = fold_build2_loc (input_location, MIN_EXPR,
7693 TREE_TYPE (slen), dlen, slen);
7694
7695 gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7696 code->expr2->ts.kind,
7697 slen, oom_str,
7698 gfc_default_character_kind);
7699 dlen = gfc_finish_block (&errmsg_block);
7700
7701 tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7702 stat, build_int_cst (TREE_TYPE (stat),
7703 LIBERROR_NO_MEMORY));
7704
7705 tmp = build3_v (COND_EXPR, tmp,
7706 dlen, build_empty_stmt (input_location));
7707
7708 gfc_add_expr_to_block (&block, tmp);
7709 }
7710
7711 /* STAT block. */
7712 if (code->expr1)
7713 {
7714 if (TREE_USED (label_finish))
7715 {
7716 tmp = build1_v (LABEL_EXPR, label_finish);
7717 gfc_add_expr_to_block (&block, tmp);
7718 }
7719
7720 gfc_init_se (&se, NULL);
7721 gfc_conv_expr_lhs (&se, code->expr1);
7722 tmp = convert (TREE_TYPE (se.expr), stat);
7723 gfc_add_modify (&block, se.expr, tmp);
7724 }
7725
7726 if (needs_caf_sync)
7727 {
7728 /* Add a sync all after the allocation has been executed. */
7729 tree zero_size = build_zero_cst (size_type_node);
7730 tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
7731 3, null_pointer_node, null_pointer_node,
7732 zero_size);
7733 gfc_add_expr_to_block (&post, tmp);
7734 }
7735
7736 gfc_add_block_to_block (&block, &se.post);
7737 gfc_add_block_to_block (&block, &post);
7738 if (code->expr3 && code->expr3->must_finalize)
7739 gfc_add_block_to_block (&block, &final_block);
7740
7741 return gfc_finish_block (&block);
7742 }
7743
7744
7745 /* Translate a DEALLOCATE statement. */
7746
7747 tree
7748 gfc_trans_deallocate (gfc_code *code)
7749 {
7750 gfc_se se;
7751 gfc_alloc *al;
7752 tree apstat, pstat, stat, errmsg, errlen, tmp;
7753 tree label_finish, label_errmsg;
7754 stmtblock_t block;
7755
7756 pstat = apstat = stat = errmsg = errlen = tmp = NULL_TREE;
7757 label_finish = label_errmsg = NULL_TREE;
7758
7759 gfc_start_block (&block);
7760
7761 /* Count the number of failed deallocations. If deallocate() was
7762 called with STAT= , then set STAT to the count. If deallocate
7763 was called with ERRMSG, then set ERRMG to a string. */
7764 if (code->expr1)
7765 {
7766 tree gfc_int4_type_node = gfc_get_int_type (4);
7767
7768 stat = gfc_create_var (gfc_int4_type_node, "stat");
7769 pstat = gfc_build_addr_expr (NULL_TREE, stat);
7770
7771 /* GOTO destinations. */
7772 label_errmsg = gfc_build_label_decl (NULL_TREE);
7773 label_finish = gfc_build_label_decl (NULL_TREE);
7774 TREE_USED (label_finish) = 0;
7775 }
7776
7777 /* Set ERRMSG - only needed if STAT is available. */
7778 if (code->expr1 && code->expr2)
7779 {
7780 gfc_init_se (&se, NULL);
7781 se.want_pointer = 1;
7782 gfc_conv_expr_lhs (&se, code->expr2);
7783 errmsg = se.expr;
7784 errlen = se.string_length;
7785 }
7786
7787 for (al = code->ext.alloc.list; al != NULL; al = al->next)
7788 {
7789 gfc_expr *expr = gfc_copy_expr (al->expr);
7790 bool is_coarray = false, is_coarray_array = false;
7791 int caf_mode = 0;
7792
7793 gcc_assert (expr->expr_type == EXPR_VARIABLE);
7794
7795 if (expr->ts.type == BT_CLASS)
7796 gfc_add_data_component (expr);
7797
7798 gfc_init_se (&se, NULL);
7799 gfc_start_block (&se.pre);
7800
7801 se.want_pointer = 1;
7802 se.descriptor_only = 1;
7803 gfc_conv_expr (&se, expr);
7804
7805 /* Deallocate PDT components that are parameterized. */
7806 tmp = NULL;
7807 if (expr->ts.type == BT_DERIVED
7808 && expr->ts.u.derived->attr.pdt_type
7809 && expr->symtree->n.sym->param_list)
7810 tmp = gfc_deallocate_pdt_comp (expr->ts.u.derived, se.expr, expr->rank);
7811 else if (expr->ts.type == BT_CLASS
7812 && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type
7813 && expr->symtree->n.sym->param_list)
7814 tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7815 se.expr, expr->rank);
7816
7817 if (tmp)
7818 gfc_add_expr_to_block (&block, tmp);
7819
7820 if (flag_coarray == GFC_FCOARRAY_LIB
7821 || flag_coarray == GFC_FCOARRAY_SINGLE)
7822 {
7823 bool comp_ref;
7824 symbol_attribute caf_attr = gfc_caf_attr (expr, false, &comp_ref);
7825 if (caf_attr.codimension)
7826 {
7827 is_coarray = true;
7828 is_coarray_array = caf_attr.dimension || !comp_ref
7829 || caf_attr.coarray_comp;
7830
7831 if (flag_coarray == GFC_FCOARRAY_LIB)
7832 /* When the expression to deallocate is referencing a
7833 component, then only deallocate it, but do not
7834 deregister. */
7835 caf_mode = GFC_STRUCTURE_CAF_MODE_IN_COARRAY
7836 | (comp_ref && !caf_attr.coarray_comp
7837 ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0);
7838 }
7839 }
7840
7841 if (expr->rank || is_coarray_array)
7842 {
7843 gfc_ref *ref;
7844
7845 if (gfc_bt_struct (expr->ts.type)
7846 && expr->ts.u.derived->attr.alloc_comp
7847 && !gfc_is_finalizable (expr->ts.u.derived, NULL))
7848 {
7849 gfc_ref *last = NULL;
7850
7851 for (ref = expr->ref; ref; ref = ref->next)
7852 if (ref->type == REF_COMPONENT)
7853 last = ref;
7854
7855 /* Do not deallocate the components of a derived type
7856 ultimate pointer component. */
7857 if (!(last && last->u.c.component->attr.pointer)
7858 && !(!last && expr->symtree->n.sym->attr.pointer))
7859 {
7860 if (is_coarray && expr->rank == 0
7861 && (!last || !last->u.c.component->attr.dimension)
7862 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
7863 {
7864 /* Add the ref to the data member only, when this is not
7865 a regular array or deallocate_alloc_comp will try to
7866 add another one. */
7867 tmp = gfc_conv_descriptor_data_get (se.expr);
7868 }
7869 else
7870 tmp = se.expr;
7871 tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp,
7872 expr->rank, caf_mode);
7873 gfc_add_expr_to_block (&se.pre, tmp);
7874 }
7875 }
7876
7877 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
7878 {
7879 gfc_coarray_deregtype caf_dtype;
7880
7881 if (is_coarray)
7882 caf_dtype = gfc_caf_is_dealloc_only (caf_mode)
7883 ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
7884 : GFC_CAF_COARRAY_DEREGISTER;
7885 else
7886 caf_dtype = GFC_CAF_COARRAY_NOCOARRAY;
7887 tmp = gfc_deallocate_with_status (se.expr, pstat, errmsg, errlen,
7888 label_finish, false, expr,
7889 caf_dtype);
7890 gfc_add_expr_to_block (&se.pre, tmp);
7891 }
7892 else if (TREE_CODE (se.expr) == COMPONENT_REF
7893 && TREE_CODE (TREE_TYPE (se.expr)) == ARRAY_TYPE
7894 && TREE_CODE (TREE_TYPE (TREE_TYPE (se.expr)))
7895 == RECORD_TYPE)
7896 {
7897 /* class.cc(finalize_component) generates these, when a
7898 finalizable entity has a non-allocatable derived type array
7899 component, which has allocatable components. Obtain the
7900 derived type of the array and deallocate the allocatable
7901 components. */
7902 for (ref = expr->ref; ref; ref = ref->next)
7903 {
7904 if (ref->u.c.component->attr.dimension
7905 && ref->u.c.component->ts.type == BT_DERIVED)
7906 break;
7907 }
7908
7909 if (ref && ref->u.c.component->ts.u.derived->attr.alloc_comp
7910 && !gfc_is_finalizable (ref->u.c.component->ts.u.derived,
7911 NULL))
7912 {
7913 tmp = gfc_deallocate_alloc_comp
7914 (ref->u.c.component->ts.u.derived,
7915 se.expr, expr->rank);
7916 gfc_add_expr_to_block (&se.pre, tmp);
7917 }
7918 }
7919
7920 if (al->expr->ts.type == BT_CLASS)
7921 {
7922 gfc_reset_vptr (&se.pre, al->expr);
7923 if (UNLIMITED_POLY (al->expr)
7924 || (al->expr->ts.type == BT_DERIVED
7925 && al->expr->ts.u.derived->attr.unlimited_polymorphic))
7926 /* Clear _len, too. */
7927 gfc_reset_len (&se.pre, al->expr);
7928 }
7929 }
7930 else
7931 {
7932 tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish,
7933 false, al->expr,
7934 al->expr->ts, NULL_TREE,
7935 is_coarray);
7936 gfc_add_expr_to_block (&se.pre, tmp);
7937
7938 /* Set to zero after deallocation. */
7939 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7940 se.expr,
7941 build_int_cst (TREE_TYPE (se.expr), 0));
7942 gfc_add_expr_to_block (&se.pre, tmp);
7943
7944 if (al->expr->ts.type == BT_CLASS)
7945 {
7946 gfc_reset_vptr (&se.pre, al->expr);
7947 if (UNLIMITED_POLY (al->expr)
7948 || (al->expr->ts.type == BT_DERIVED
7949 && al->expr->ts.u.derived->attr.unlimited_polymorphic))
7950 /* Clear _len, too. */
7951 gfc_reset_len (&se.pre, al->expr);
7952 }
7953 }
7954
7955 if (code->expr1)
7956 {
7957 tree cond;
7958
7959 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
7960 build_int_cst (TREE_TYPE (stat), 0));
7961 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
7962 gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC),
7963 build1_v (GOTO_EXPR, label_errmsg),
7964 build_empty_stmt (input_location));
7965 gfc_add_expr_to_block (&se.pre, tmp);
7966 }
7967
7968 tmp = gfc_finish_block (&se.pre);
7969 gfc_add_expr_to_block (&block, tmp);
7970 gfc_free_expr (expr);
7971 }
7972
7973 if (code->expr1)
7974 {
7975 tmp = build1_v (LABEL_EXPR, label_errmsg);
7976 gfc_add_expr_to_block (&block, tmp);
7977 }
7978
7979 /* Set ERRMSG - only needed if STAT is available. */
7980 if (code->expr1 && code->expr2)
7981 {
7982 const char *msg = "Attempt to deallocate an unallocated object";
7983 stmtblock_t errmsg_block;
7984 tree errmsg_str, slen, dlen, cond;
7985
7986 gfc_init_block (&errmsg_block);
7987
7988 errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
7989 gfc_add_modify (&errmsg_block, errmsg_str,
7990 gfc_build_addr_expr (pchar_type_node,
7991 gfc_build_localized_cstring_const (msg)));
7992 slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
7993 dlen = gfc_get_expr_charlen (code->expr2);
7994
7995 gfc_trans_string_copy (&errmsg_block, dlen, errmsg, code->expr2->ts.kind,
7996 slen, errmsg_str, gfc_default_character_kind);
7997 tmp = gfc_finish_block (&errmsg_block);
7998
7999 cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
8000 build_int_cst (TREE_TYPE (stat), 0));
8001 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
8002 gfc_unlikely (cond, PRED_FORTRAN_FAIL_ALLOC), tmp,
8003 build_empty_stmt (input_location));
8004
8005 gfc_add_expr_to_block (&block, tmp);
8006 }
8007
8008 if (code->expr1 && TREE_USED (label_finish))
8009 {
8010 tmp = build1_v (LABEL_EXPR, label_finish);
8011 gfc_add_expr_to_block (&block, tmp);
8012 }
8013
8014 /* Set STAT. */
8015 if (code->expr1)
8016 {
8017 gfc_init_se (&se, NULL);
8018 gfc_conv_expr_lhs (&se, code->expr1);
8019 tmp = convert (TREE_TYPE (se.expr), stat);
8020 gfc_add_modify (&block, se.expr, tmp);
8021 }
8022
8023 return gfc_finish_block (&block);
8024 }
8025
8026 #include "gt-fortran-trans-stmt.h"