Branch data Line data Source code
1 : : /* Statement translation -- generate GCC trees from gfc_code.
2 : : Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 : : Contributed by Paul Brook <paul@nowt.org>
4 : : and Steven Bosscher <s.bosscher@student.tudelft.nl>
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 : 3502 : gfc_trans_label_here (gfc_code * code)
68 : : {
69 : 3502 : 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 : 187 : gfc_conv_label_variable (gfc_se * se, gfc_expr * expr)
79 : : {
80 : 187 : gcc_assert (expr->symtree->n.sym->attr.assign == 1);
81 : 187 : gfc_conv_expr (se, expr);
82 : : /* Deals with variable in common block. Get the field declaration. */
83 : 187 : if (TREE_CODE (se->expr) == COMPONENT_REF)
84 : 0 : se->expr = TREE_OPERAND (se->expr, 1);
85 : : /* Deals with dummy argument. Get the parameter declaration. */
86 : 187 : else if (INDIRECT_REF_P (se->expr))
87 : 12 : se->expr = TREE_OPERAND (se->expr, 0);
88 : 187 : }
89 : :
90 : : /* Translate a label assignment statement. */
91 : :
92 : : tree
93 : 116 : gfc_trans_label_assign (gfc_code * code)
94 : : {
95 : 116 : tree label_tree;
96 : 116 : gfc_se se;
97 : 116 : tree len;
98 : 116 : tree addr;
99 : 116 : tree len_tree;
100 : 116 : int label_len;
101 : :
102 : : /* Start a new block. */
103 : 116 : gfc_init_se (&se, NULL);
104 : 116 : gfc_start_block (&se.pre);
105 : 116 : gfc_conv_label_variable (&se, code->expr1);
106 : :
107 : 116 : len = GFC_DECL_STRING_LEN (se.expr);
108 : 116 : addr = GFC_DECL_ASSIGN_ADDR (se.expr);
109 : :
110 : 116 : label_tree = gfc_get_label_decl (code->label1);
111 : :
112 : 116 : if (code->label1->defined == ST_LABEL_TARGET
113 : 116 : || code->label1->defined == ST_LABEL_DO_TARGET)
114 : : {
115 : 115 : label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
116 : 115 : len_tree = build_int_cst (gfc_charlen_type_node, -1);
117 : : }
118 : : else
119 : : {
120 : 1 : gfc_expr *format = code->label1->format;
121 : :
122 : 1 : label_len = format->value.character.length;
123 : 1 : len_tree = build_int_cst (gfc_charlen_type_node, label_len);
124 : 2 : label_tree = gfc_build_wide_string_const (format->ts.kind, label_len + 1,
125 : 1 : format->value.character.string);
126 : 1 : label_tree = gfc_build_addr_expr (pvoid_type_node, label_tree);
127 : : }
128 : :
129 : 116 : gfc_add_modify (&se.pre, len, fold_convert (TREE_TYPE (len), len_tree));
130 : 116 : gfc_add_modify (&se.pre, addr, label_tree);
131 : :
132 : 116 : return gfc_finish_block (&se.pre);
133 : : }
134 : :
135 : : /* Translate a GOTO statement. */
136 : :
137 : : tree
138 : 1187 : gfc_trans_goto (gfc_code * code)
139 : : {
140 : 1187 : locus loc = code->loc;
141 : 1187 : tree assigned_goto;
142 : 1187 : tree target;
143 : 1187 : tree tmp;
144 : 1187 : gfc_se se;
145 : :
146 : 1187 : if (code->label1 != NULL)
147 : 1117 : return build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
148 : :
149 : : /* ASSIGNED GOTO. */
150 : 70 : gfc_init_se (&se, NULL);
151 : 70 : gfc_start_block (&se.pre);
152 : 70 : gfc_conv_label_variable (&se, code->expr1);
153 : 70 : tmp = GFC_DECL_STRING_LEN (se.expr);
154 : 70 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
155 : 70 : build_int_cst (TREE_TYPE (tmp), -1));
156 : 70 : gfc_trans_runtime_check (true, false, tmp, &se.pre, &loc,
157 : : "Assigned label is not a target label");
158 : :
159 : 70 : 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 : 70 : target = fold_build1_loc (input_location, GOTO_EXPR, void_type_node,
168 : : assigned_goto);
169 : 70 : gfc_add_expr_to_block (&se.pre, target);
170 : 70 : 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 : 1341 : gfc_trans_entry (gfc_code * code)
177 : : {
178 : 1341 : 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 : 343 : replace_ss (gfc_se *se, gfc_ss *old_ss, gfc_ss *new_ss)
188 : : {
189 : 343 : gfc_ss **sess, **loopss;
190 : :
191 : : /* The old_ss is a ss for a single variable. */
192 : 343 : gcc_assert (old_ss->info->type == GFC_SS_SECTION);
193 : :
194 : 490 : for (sess = &(se->ss); *sess != gfc_ss_terminator; sess = &((*sess)->next))
195 : 490 : if (*sess == old_ss)
196 : : break;
197 : 343 : gcc_assert (*sess != gfc_ss_terminator);
198 : :
199 : 343 : *sess = new_ss;
200 : 343 : new_ss->next = old_ss->next;
201 : :
202 : : /* Make sure that trailing references are not lost. */
203 : 343 : if (old_ss->info
204 : 343 : && old_ss->info->data.array.ref
205 : 343 : && old_ss->info->data.array.ref->next
206 : 25 : && !(new_ss->info->data.array.ref
207 : 0 : && new_ss->info->data.array.ref->next))
208 : 25 : new_ss->info->data.array.ref = old_ss->info->data.array.ref;
209 : :
210 : 490 : for (loopss = &(se->loop->ss); *loopss != gfc_ss_terminator;
211 : 147 : loopss = &((*loopss)->loop_chain))
212 : 490 : if (*loopss == old_ss)
213 : : break;
214 : 343 : gcc_assert (*loopss != gfc_ss_terminator);
215 : :
216 : 343 : *loopss = new_ss;
217 : 343 : new_ss->loop_chain = old_ss->loop_chain;
218 : 343 : new_ss->loop = old_ss->loop;
219 : :
220 : 343 : gfc_free_ss (old_ss);
221 : 343 : }
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 : 2210 : 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 : 2210 : gfc_actual_arglist *arg0;
234 : 2210 : gfc_expr *e;
235 : 2210 : gfc_formal_arglist *formal;
236 : 2210 : gfc_se parmse;
237 : 2210 : gfc_ss *ss;
238 : 2210 : gfc_symbol *fsym;
239 : 2210 : tree data;
240 : 2210 : tree size;
241 : 2210 : tree tmp;
242 : :
243 : 2210 : if (loopse->ss == NULL)
244 : 0 : return;
245 : :
246 : 2210 : ss = loopse->ss;
247 : 2210 : arg0 = arg;
248 : 2210 : formal = gfc_sym_get_dummy_args (sym);
249 : :
250 : : /* Loop over all the arguments testing for dependencies. */
251 : 11348 : for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
252 : : {
253 : 4569 : e = arg->expr;
254 : 4569 : if (e == NULL)
255 : 12 : continue;
256 : :
257 : : /* Obtain the info structure for the current argument. */
258 : 7891 : for (ss = loopse->ss; ss && ss != gfc_ss_terminator; ss = ss->next)
259 : 7625 : 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 : 4557 : fsym = formal ? formal->sym : NULL;
265 : 4557 : if (e->expr_type == EXPR_VARIABLE
266 : 3388 : && e->rank && fsym
267 : 2879 : && fsym->attr.intent != INTENT_IN
268 : 1479 : && !fsym->attr.value
269 : 5976 : && gfc_check_fncall_dependency (e, fsym->attr.intent,
270 : : sym, arg0, check_variable))
271 : : {
272 : 343 : tree initial, temptype;
273 : 343 : stmtblock_t temp_post;
274 : 343 : gfc_ss *tmp_ss;
275 : :
276 : 343 : tmp_ss = gfc_get_array_ss (gfc_ss_terminator, NULL, ss->dimen,
277 : : GFC_SS_SECTION);
278 : 343 : gfc_mark_ss_chain_used (tmp_ss, 1);
279 : 343 : tmp_ss->info->expr = ss->info->expr;
280 : 343 : replace_ss (loopse, ss, tmp_ss);
281 : :
282 : : /* Obtain the argument descriptor for unpacking. */
283 : 343 : gfc_init_se (&parmse, NULL);
284 : 343 : parmse.want_pointer = 1;
285 : 343 : gfc_conv_expr_descriptor (&parmse, e);
286 : 343 : 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 : 343 : if (fsym->attr.intent == INTENT_INOUT
291 : 299 : || (fsym->ts.type ==BT_DERIVED
292 : 51 : && fsym->attr.intent == INTENT_OUT))
293 : 95 : initial = parmse.expr;
294 : : /* For class expressions, we always initialize with the copy of
295 : : the values. */
296 : 248 : else if (e->ts.type == BT_CLASS)
297 : 6 : initial = parmse.expr;
298 : : else
299 : : initial = NULL_TREE;
300 : :
301 : 343 : 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 : 337 : temptype = TREE_TYPE (parmse.expr); /* Pointer to descriptor. */
310 : 337 : gcc_assert (TREE_CODE (temptype) == POINTER_TYPE);
311 : 337 : temptype = TREE_TYPE (temptype);
312 : 337 : 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 : 343 : size = gfc_create_var (gfc_array_index_type, NULL);
324 : 343 : data = gfc_create_var (pvoid_type_node, NULL);
325 : 343 : gfc_init_block (&temp_post);
326 : 686 : tmp = gfc_trans_create_temp_array (&se->pre, &temp_post, tmp_ss,
327 : : temptype, initial, false, true,
328 : 343 : false, &arg->expr->where);
329 : 343 : gfc_add_modify (&se->pre, size, tmp);
330 : 343 : tmp = fold_convert (pvoid_type_node, tmp_ss->info->data.array.data);
331 : 343 : gfc_add_modify (&se->pre, data, tmp);
332 : :
333 : : /* Update other ss' delta. */
334 : 343 : gfc_set_delta (loopse->loop);
335 : :
336 : : /* Copy the result back using unpack..... */
337 : 343 : if (e->ts.type != BT_CLASS)
338 : 337 : 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 : 6 : tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
345 : 6 : tmp = gfc_conv_descriptor_data_get (tmp);
346 : 6 : 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 : 343 : gfc_add_expr_to_block (&se->post, tmp);
352 : :
353 : : /* parmse.pre is already added above. */
354 : 343 : gfc_add_block_to_block (&se->post, &parmse.post);
355 : 343 : 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 : 6106 : get_intrinsic_for_code (gfc_code *code)
366 : : {
367 : 6106 : if (code->op == EXEC_CALL)
368 : : {
369 : 5763 : gfc_intrinsic_sym * const isym = code->resolved_isym;
370 : 5763 : if (isym)
371 : : return isym;
372 : : else
373 : 5566 : 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 : 76563 : gfc_trans_call (gfc_code * code, bool dependency_check,
384 : : tree mask, tree count1, bool invert)
385 : : {
386 : 76563 : gfc_se se;
387 : 76563 : gfc_ss * ss;
388 : 76563 : int has_alternate_specifier;
389 : 76563 : gfc_dep_check check_variable;
390 : 76563 : tree index = NULL_TREE;
391 : 76563 : tree maskexpr = NULL_TREE;
392 : 76563 : tree tmp;
393 : 76563 : bool is_intrinsic_mvbits;
394 : :
395 : : /* A CALL starts a new block because the actual arguments may have to
396 : : be evaluated first. */
397 : 76563 : gfc_init_se (&se, NULL);
398 : 76563 : gfc_start_block (&se.pre);
399 : :
400 : 76563 : gcc_assert (code->resolved_sym);
401 : :
402 : 76563 : ss = gfc_ss_terminator;
403 : 76563 : if (code->resolved_sym->attr.elemental)
404 : 6106 : 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 : 153126 : is_intrinsic_mvbits = code->resolved_isym
410 : 76563 : && code->resolved_isym->id == GFC_ISYM_MVBITS;
411 : :
412 : : /* Is not an elemental subroutine call with array valued arguments. */
413 : 76563 : if (ss == gfc_ss_terminator)
414 : : {
415 : :
416 : 74353 : if (is_intrinsic_mvbits)
417 : : {
418 : 130 : has_alternate_specifier = 0;
419 : 130 : gfc_conv_intrinsic_mvbits (&se, code->ext.actual, NULL);
420 : : }
421 : : else
422 : : {
423 : : /* Translate the call. */
424 : 74223 : has_alternate_specifier =
425 : 74223 : 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 : 74223 : TREE_SIDE_EFFECTS (se.expr) = 1;
430 : : }
431 : :
432 : : /* Chain the pieces together and return the block. */
433 : 74353 : if (has_alternate_specifier)
434 : : {
435 : 140 : gfc_code *select_code;
436 : 140 : gfc_symbol *sym;
437 : 140 : select_code = code->next;
438 : 140 : gcc_assert(select_code->op == EXEC_SELECT);
439 : 140 : sym = select_code->expr1->symtree->n.sym;
440 : 140 : se.expr = convert (gfc_typenode_for_spec (&sym->ts), se.expr);
441 : 140 : if (sym->backend_decl == NULL)
442 : 1 : sym->backend_decl = gfc_get_symbol_decl (sym);
443 : 140 : gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
444 : : }
445 : : else
446 : 74213 : gfc_add_expr_to_block (&se.pre, se.expr);
447 : :
448 : 74353 : gfc_add_block_to_block (&se.finalblock, &se.post);
449 : 74353 : 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 : 2210 : gfc_loopinfo loop;
457 : 2210 : stmtblock_t body;
458 : 2210 : stmtblock_t block;
459 : 2210 : gfc_se loopse;
460 : 2210 : 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 : 2210 : ss = gfc_reverse_ss (ss);
465 : :
466 : : /* Initialize the loop. */
467 : 2210 : gfc_init_se (&loopse, NULL);
468 : 2210 : gfc_init_loopinfo (&loop);
469 : 2210 : gfc_add_ss_to_loop (&loop, ss);
470 : :
471 : 2210 : 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 : 2210 : if (code->expr1)
477 : 237 : gfc_conv_loop_setup (&loop, &code->expr1->where);
478 : : else
479 : 1973 : gfc_conv_loop_setup (&loop, &code->loc);
480 : :
481 : 2210 : gfc_mark_ss_chain_used (ss, 1);
482 : :
483 : : /* Convert the arguments, checking for dependencies. */
484 : 2210 : gfc_copy_loopinfo_to_se (&loopse, &loop);
485 : 2210 : loopse.ss = ss;
486 : :
487 : : /* For operator assignment, do dependency checking. */
488 : 2210 : if (dependency_check)
489 : : check_variable = ELEM_CHECK_VARIABLE;
490 : : else
491 : 1886 : check_variable = ELEM_DONT_CHECK_VARIABLE;
492 : :
493 : 2210 : gfc_init_se (&depse, NULL);
494 : 2210 : gfc_conv_elemental_dependencies (&depse, &loopse, code->resolved_sym,
495 : : code->ext.actual, check_variable);
496 : :
497 : 2210 : gfc_add_block_to_block (&loop.pre, &depse.pre);
498 : 2210 : gfc_add_block_to_block (&loop.post, &depse.post);
499 : :
500 : : /* Generate the loop body. */
501 : 2210 : gfc_start_scalarized_body (&loop, &body);
502 : 2210 : gfc_init_block (&block);
503 : :
504 : 2210 : if (mask && count1)
505 : : {
506 : : /* Form the mask expression according to the mask. */
507 : 44 : index = count1;
508 : 44 : maskexpr = gfc_build_array_ref (mask, index, NULL);
509 : 44 : if (invert)
510 : 11 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
511 : 11 : TREE_TYPE (maskexpr), maskexpr);
512 : : }
513 : :
514 : 2210 : if (is_intrinsic_mvbits)
515 : : {
516 : 67 : has_alternate_specifier = 0;
517 : 67 : gfc_conv_intrinsic_mvbits (&loopse, code->ext.actual, &loop);
518 : : }
519 : : else
520 : : {
521 : : /* Add the subroutine call to the block. */
522 : 2143 : gfc_conv_procedure_call (&loopse, code->resolved_sym,
523 : : code->ext.actual, code->expr1,
524 : : NULL);
525 : : }
526 : :
527 : 2210 : if (mask && count1)
528 : : {
529 : 44 : tmp = build3_v (COND_EXPR, maskexpr, loopse.expr,
530 : : build_empty_stmt (input_location));
531 : 44 : gfc_add_expr_to_block (&loopse.pre, tmp);
532 : 44 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
533 : : gfc_array_index_type,
534 : : count1, gfc_index_one_node);
535 : 44 : gfc_add_modify (&loopse.pre, count1, tmp);
536 : : }
537 : : else
538 : 2166 : gfc_add_expr_to_block (&loopse.pre, loopse.expr);
539 : :
540 : 2210 : gfc_add_block_to_block (&block, &loopse.pre);
541 : 2210 : gfc_add_block_to_block (&block, &loopse.post);
542 : :
543 : : /* Finish up the loop block and the loop. */
544 : 2210 : gfc_add_expr_to_block (&body, gfc_finish_block (&block));
545 : 2210 : gfc_trans_scalarizing_loops (&loop, &body);
546 : 2210 : gfc_add_block_to_block (&se.pre, &loop.pre);
547 : 2210 : gfc_add_block_to_block (&se.pre, &loop.post);
548 : 2210 : gfc_add_block_to_block (&se.pre, &loopse.finalblock);
549 : 2210 : gfc_add_block_to_block (&se.pre, &se.post);
550 : 2210 : gfc_cleanup_loop (&loop);
551 : : }
552 : :
553 : 76563 : return gfc_finish_block (&se.pre);
554 : : }
555 : :
556 : :
557 : : /* Translate the RETURN statement. */
558 : :
559 : : tree
560 : 3035 : gfc_trans_return (gfc_code * code)
561 : : {
562 : 3035 : if (code->expr1)
563 : : {
564 : 50 : gfc_se se;
565 : 50 : tree tmp;
566 : 50 : 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 : 50 : result = gfc_get_fake_result_decl (NULL, 0);
573 : 50 : if (!result)
574 : : {
575 : 0 : gfc_warning (0,
576 : : "An alternate return at %L without a * dummy argument",
577 : 0 : &code->expr1->where);
578 : 0 : return gfc_generate_return ();
579 : : }
580 : :
581 : : /* Start a new block for this statement. */
582 : 50 : gfc_init_se (&se, NULL);
583 : 50 : gfc_start_block (&se.pre);
584 : :
585 : 50 : 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 : 50 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (result),
591 : 50 : result, fold_convert (TREE_TYPE (result),
592 : : se.expr));
593 : 50 : gfc_add_expr_to_block (&se.pre, tmp);
594 : 50 : gfc_add_block_to_block (&se.pre, &se.post);
595 : :
596 : 50 : tmp = gfc_generate_return ();
597 : 50 : gfc_add_expr_to_block (&se.pre, tmp);
598 : 50 : return gfc_finish_block (&se.pre);
599 : : }
600 : :
601 : 2985 : 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 : 28 : gfc_trans_pause (gfc_code * code)
610 : : {
611 : 28 : tree gfc_int8_type_node = gfc_get_int_type (8);
612 : 28 : gfc_se se;
613 : 28 : tree tmp;
614 : :
615 : : /* Start a new block for this statement. */
616 : 28 : gfc_init_se (&se, NULL);
617 : 28 : gfc_start_block (&se.pre);
618 : :
619 : :
620 : 28 : if (code->expr1 == NULL)
621 : : {
622 : 10 : tmp = build_int_cst (size_type_node, 0);
623 : 10 : tmp = build_call_expr_loc (input_location,
624 : : gfor_fndecl_pause_string, 2,
625 : 10 : build_int_cst (pchar_type_node, 0), tmp);
626 : : }
627 : 18 : else if (code->expr1->ts.type == BT_INTEGER)
628 : : {
629 : 9 : gfc_conv_expr (&se, code->expr1);
630 : 9 : 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 : 9 : gfc_conv_expr_reference (&se, code->expr1);
637 : 9 : 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 : 28 : gfc_add_expr_to_block (&se.pre, tmp);
644 : :
645 : 28 : gfc_add_block_to_block (&se.pre, &se.post);
646 : :
647 : 28 : 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 : 206298 : gfc_trans_stop (gfc_code *code, bool error_stop)
656 : : {
657 : 206298 : gfc_se se;
658 : 206298 : tree tmp;
659 : 206298 : tree quiet;
660 : :
661 : : /* Start a new block for this statement. */
662 : 206298 : gfc_init_se (&se, NULL);
663 : 206298 : gfc_start_block (&se.pre);
664 : :
665 : 206298 : if (code->expr2)
666 : : {
667 : 25 : gfc_conv_expr_val (&se, code->expr2);
668 : 25 : quiet = fold_convert (boolean_type_node, se.expr);
669 : : }
670 : : else
671 : 206273 : quiet = boolean_false_node;
672 : :
673 : 206298 : if (code->expr1 == NULL)
674 : : {
675 : 20178 : tmp = build_int_cst (size_type_node, 0);
676 : 40356 : tmp = build_call_expr_loc (input_location,
677 : : error_stop
678 : 19250 : ? (flag_coarray == GFC_FCOARRAY_LIB
679 : 19250 : ? gfor_fndecl_caf_error_stop_str
680 : : : gfor_fndecl_error_stop_string)
681 : 928 : : (flag_coarray == GFC_FCOARRAY_LIB
682 : 928 : ? gfor_fndecl_caf_stop_str
683 : : : gfor_fndecl_stop_string),
684 : 20178 : 3, build_int_cst (pchar_type_node, 0), tmp,
685 : : quiet);
686 : : }
687 : 186120 : else if (code->expr1->ts.type == BT_INTEGER)
688 : : {
689 : 185721 : gfc_conv_expr (&se, code->expr1);
690 : 371442 : tmp = build_call_expr_loc (input_location,
691 : : error_stop
692 : 19004 : ? (flag_coarray == GFC_FCOARRAY_LIB
693 : 19004 : ? gfor_fndecl_caf_error_stop
694 : : : gfor_fndecl_error_stop_numeric)
695 : 166717 : : (flag_coarray == GFC_FCOARRAY_LIB
696 : 166717 : ? 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 : 399 : gfc_conv_expr_reference (&se, code->expr1);
704 : 798 : tmp = build_call_expr_loc (input_location,
705 : : error_stop
706 : 291 : ? (flag_coarray == GFC_FCOARRAY_LIB
707 : 291 : ? gfor_fndecl_caf_error_stop_str
708 : : : gfor_fndecl_error_stop_string)
709 : 108 : : (flag_coarray == GFC_FCOARRAY_LIB
710 : 108 : ? 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 : 206298 : gfc_add_expr_to_block (&se.pre, tmp);
718 : :
719 : 206298 : gfc_add_block_to_block (&se.pre, &se.post);
720 : :
721 : 206298 : return gfc_finish_block (&se.pre);
722 : : }
723 : :
724 : : /* Translate the FAIL IMAGE statement. */
725 : :
726 : : tree
727 : 3 : gfc_trans_fail_image (gfc_code *code ATTRIBUTE_UNUSED)
728 : : {
729 : 3 : if (flag_coarray == GFC_FCOARRAY_LIB)
730 : 2 : return build_call_expr_loc (input_location,
731 : 2 : gfor_fndecl_caf_fail_image, 0);
732 : : else
733 : : {
734 : 1 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
735 : 1 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
736 : 1 : tree tmp = gfc_get_symbol_decl (exsym);
737 : 1 : 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 : 25 : gfc_trans_form_team (gfc_code *code)
745 : : {
746 : 25 : if (flag_coarray == GFC_FCOARRAY_LIB)
747 : : {
748 : 1 : gfc_se se;
749 : 1 : gfc_se argse1, argse2;
750 : 1 : tree team_id, team_type, tmp;
751 : :
752 : 1 : gfc_init_se (&se, NULL);
753 : 1 : gfc_init_se (&argse1, NULL);
754 : 1 : gfc_init_se (&argse2, NULL);
755 : 1 : gfc_start_block (&se.pre);
756 : :
757 : 1 : gfc_conv_expr_val (&argse1, code->expr1);
758 : 1 : gfc_conv_expr_val (&argse2, code->expr2);
759 : 1 : team_id = fold_convert (integer_type_node, argse1.expr);
760 : 1 : team_type = gfc_build_addr_expr (ppvoid_type_node, argse2.expr);
761 : :
762 : 1 : gfc_add_block_to_block (&se.pre, &argse1.pre);
763 : 1 : gfc_add_block_to_block (&se.pre, &argse2.pre);
764 : 1 : tmp = build_call_expr_loc (input_location,
765 : : gfor_fndecl_caf_form_team, 3,
766 : : team_id, team_type,
767 : : integer_zero_node);
768 : 1 : gfc_add_expr_to_block (&se.pre, tmp);
769 : 1 : gfc_add_block_to_block (&se.pre, &argse1.post);
770 : 1 : gfc_add_block_to_block (&se.pre, &argse2.post);
771 : 1 : return gfc_finish_block (&se.pre);
772 : : }
773 : : else
774 : : {
775 : 24 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
776 : 24 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
777 : 24 : tree tmp = gfc_get_symbol_decl (exsym);
778 : 24 : 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 : 19 : gfc_trans_change_team (gfc_code *code)
786 : : {
787 : 19 : if (flag_coarray == GFC_FCOARRAY_LIB)
788 : : {
789 : 1 : gfc_se argse;
790 : 1 : tree team_type, tmp;
791 : :
792 : 1 : gfc_init_se (&argse, NULL);
793 : 1 : gfc_conv_expr_val (&argse, code->expr1);
794 : 1 : team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
795 : :
796 : 1 : tmp = build_call_expr_loc (input_location,
797 : : gfor_fndecl_caf_change_team, 2, team_type,
798 : : integer_zero_node);
799 : 1 : gfc_add_expr_to_block (&argse.pre, tmp);
800 : 1 : gfc_add_block_to_block (&argse.pre, &argse.post);
801 : 1 : return gfc_finish_block (&argse.pre);
802 : : }
803 : : else
804 : : {
805 : 18 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
806 : 18 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
807 : 18 : tree tmp = gfc_get_symbol_decl (exsym);
808 : 18 : 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 : 19 : gfc_trans_end_team (gfc_code *code ATTRIBUTE_UNUSED)
816 : : {
817 : 19 : if (flag_coarray == GFC_FCOARRAY_LIB)
818 : : {
819 : 1 : return build_call_expr_loc (input_location,
820 : : gfor_fndecl_caf_end_team, 1,
821 : 1 : build_int_cst (pchar_type_node, 0));
822 : : }
823 : : else
824 : : {
825 : 18 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
826 : 18 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
827 : 18 : tree tmp = gfc_get_symbol_decl (exsym);
828 : 18 : 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 : 0 : gfc_trans_sync_team (gfc_code *code)
836 : : {
837 : 0 : if (flag_coarray == GFC_FCOARRAY_LIB)
838 : : {
839 : 0 : gfc_se argse;
840 : 0 : tree team_type, tmp;
841 : :
842 : 0 : gfc_init_se (&argse, NULL);
843 : 0 : gfc_conv_expr_val (&argse, code->expr1);
844 : 0 : team_type = gfc_build_addr_expr (ppvoid_type_node, argse.expr);
845 : :
846 : 0 : tmp = build_call_expr_loc (input_location,
847 : : gfor_fndecl_caf_sync_team, 2,
848 : : team_type,
849 : : integer_zero_node);
850 : 0 : gfc_add_expr_to_block (&argse.pre, tmp);
851 : 0 : gfc_add_block_to_block (&argse.pre, &argse.post);
852 : 0 : return gfc_finish_block (&argse.pre);
853 : : }
854 : : else
855 : : {
856 : 0 : const char *name = gfc_get_string (PREFIX ("exit_i%d"), 4);
857 : 0 : gfc_symbol *exsym = gfc_get_intrinsic_sub_symbol (name);
858 : 0 : tree tmp = gfc_get_symbol_decl (exsym);
859 : 0 : return build_call_expr_loc (input_location, tmp, 1, integer_zero_node);
860 : : }
861 : : }
862 : :
863 : : tree
864 : 84 : gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
865 : : {
866 : 84 : gfc_se se, argse;
867 : 84 : tree stat = NULL_TREE, stat2 = NULL_TREE;
868 : 84 : 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 : 84 : if (!code->expr2 && !code->expr4 && flag_coarray != GFC_FCOARRAY_LIB)
873 : : return NULL_TREE;
874 : :
875 : 66 : if (code->expr2)
876 : : {
877 : 28 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
878 : 28 : gfc_init_se (&argse, NULL);
879 : 28 : gfc_conv_expr_val (&argse, code->expr2);
880 : 28 : stat = argse.expr;
881 : : }
882 : 38 : else if (flag_coarray == GFC_FCOARRAY_LIB)
883 : 32 : stat = null_pointer_node;
884 : :
885 : 66 : if (code->expr4)
886 : : {
887 : 14 : gcc_assert (code->expr4->expr_type == EXPR_VARIABLE);
888 : 14 : gfc_init_se (&argse, NULL);
889 : 14 : gfc_conv_expr_val (&argse, code->expr4);
890 : 14 : lock_acquired = argse.expr;
891 : : }
892 : 52 : else if (flag_coarray == GFC_FCOARRAY_LIB)
893 : 40 : lock_acquired = null_pointer_node;
894 : :
895 : 66 : gfc_start_block (&se.pre);
896 : 66 : if (flag_coarray == GFC_FCOARRAY_LIB)
897 : : {
898 : 48 : tree tmp, token, image_index, errmsg, errmsg_len;
899 : 48 : tree index = build_zero_cst (gfc_array_index_type);
900 : 48 : tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
901 : :
902 : 48 : if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
903 : 48 : || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
904 : : != INTMOD_ISO_FORTRAN_ENV
905 : 48 : || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
906 : : != ISOFORTRAN_LOCK_TYPE)
907 : : {
908 : 4 : gfc_error ("Sorry, the lock component of derived type at %L is not "
909 : : "yet supported", &code->expr1->where);
910 : 4 : return NULL_TREE;
911 : : }
912 : :
913 : 44 : gfc_get_caf_token_offset (&se, &token, NULL, caf_decl, NULL_TREE,
914 : : code->expr1);
915 : :
916 : 44 : if (gfc_is_coindexed (code->expr1))
917 : 16 : image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
918 : : else
919 : 28 : image_index = integer_zero_node;
920 : :
921 : : /* For arrays, obtain the array index. */
922 : 44 : if (gfc_expr_attr (code->expr1).dimension)
923 : : {
924 : 28 : tree desc, tmp, extent, lbound, ubound;
925 : 28 : gfc_array_ref *ar, ar2;
926 : 28 : int i, rank;
927 : :
928 : : /* TODO: Extend this, once DT components are supported. */
929 : 28 : ar = &code->expr1->ref->u.ar;
930 : 28 : ar2 = *ar;
931 : 28 : memset (ar, '\0', sizeof (*ar));
932 : 28 : ar->as = ar2.as;
933 : 28 : ar->type = AR_FULL;
934 : 28 : rank = code->expr1->rank;
935 : 28 : code->expr1->rank = ar->as->rank;
936 : :
937 : 28 : gfc_init_se (&argse, NULL);
938 : 28 : argse.descriptor_only = 1;
939 : 28 : gfc_conv_expr_descriptor (&argse, code->expr1);
940 : 28 : gfc_add_block_to_block (&se.pre, &argse.pre);
941 : 28 : desc = argse.expr;
942 : 28 : *ar = ar2;
943 : 28 : code->expr1->rank = rank;
944 : :
945 : 28 : extent = build_one_cst (gfc_array_index_type);
946 : 98 : for (i = 0; i < ar->dimen; i++)
947 : : {
948 : 42 : gfc_init_se (&argse, NULL);
949 : 42 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
950 : 42 : gfc_add_block_to_block (&argse.pre, &argse.pre);
951 : 42 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
952 : 42 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
953 : 42 : TREE_TYPE (lbound), argse.expr, lbound);
954 : 42 : tmp = fold_build2_loc (input_location, MULT_EXPR,
955 : 42 : TREE_TYPE (tmp), extent, tmp);
956 : 42 : index = fold_build2_loc (input_location, PLUS_EXPR,
957 : 42 : TREE_TYPE (tmp), index, tmp);
958 : 42 : if (i < ar->dimen - 1)
959 : : {
960 : 14 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
961 : 14 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
962 : 14 : extent = fold_build2_loc (input_location, MULT_EXPR,
963 : 14 : TREE_TYPE (tmp), extent, tmp);
964 : : }
965 : : }
966 : : }
967 : :
968 : : /* errmsg. */
969 : 44 : if (code->expr3)
970 : : {
971 : 0 : gfc_init_se (&argse, NULL);
972 : 0 : argse.want_pointer = 1;
973 : 0 : gfc_conv_expr (&argse, code->expr3);
974 : 0 : gfc_add_block_to_block (&se.pre, &argse.pre);
975 : 0 : errmsg = argse.expr;
976 : 0 : errmsg_len = fold_convert (size_type_node, argse.string_length);
977 : : }
978 : : else
979 : : {
980 : 44 : errmsg = null_pointer_node;
981 : 44 : errmsg_len = build_zero_cst (size_type_node);
982 : : }
983 : :
984 : 44 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
985 : : {
986 : 0 : stat2 = stat;
987 : 0 : stat = gfc_create_var (integer_type_node, "stat");
988 : : }
989 : :
990 : 44 : if (lock_acquired != null_pointer_node
991 : 44 : && TREE_TYPE (lock_acquired) != integer_type_node)
992 : : {
993 : 8 : lock_acquired2 = lock_acquired;
994 : 8 : lock_acquired = gfc_create_var (integer_type_node, "acquired");
995 : : }
996 : :
997 : 44 : index = fold_convert (size_type_node, index);
998 : 44 : if (op == EXEC_LOCK)
999 : 22 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_lock, 7,
1000 : : token, index, image_index,
1001 : 22 : lock_acquired != null_pointer_node
1002 : 8 : ? gfc_build_addr_expr (NULL, lock_acquired)
1003 : : : lock_acquired,
1004 : 22 : stat != null_pointer_node
1005 : 8 : ? gfc_build_addr_expr (NULL, stat) : stat,
1006 : : errmsg, errmsg_len);
1007 : : else
1008 : 22 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_unlock, 6,
1009 : : token, index, image_index,
1010 : 22 : stat != null_pointer_node
1011 : 8 : ? gfc_build_addr_expr (NULL, stat) : stat,
1012 : : errmsg, errmsg_len);
1013 : 44 : gfc_add_expr_to_block (&se.pre, tmp);
1014 : :
1015 : : /* It guarantees memory consistency within the same segment */
1016 : 44 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1017 : 44 : 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 : 44 : ASM_VOLATILE_P (tmp) = 1;
1021 : :
1022 : 44 : gfc_add_expr_to_block (&se.pre, tmp);
1023 : :
1024 : 44 : if (stat2 != NULL_TREE)
1025 : 0 : gfc_add_modify (&se.pre, stat2,
1026 : 0 : fold_convert (TREE_TYPE (stat2), stat));
1027 : :
1028 : 44 : if (lock_acquired2 != NULL_TREE)
1029 : 8 : gfc_add_modify (&se.pre, lock_acquired2,
1030 : 8 : fold_convert (TREE_TYPE (lock_acquired2),
1031 : : lock_acquired));
1032 : :
1033 : 44 : return gfc_finish_block (&se.pre);
1034 : : }
1035 : :
1036 : 18 : if (stat != NULL_TREE)
1037 : 12 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1038 : :
1039 : 18 : if (lock_acquired != NULL_TREE)
1040 : 6 : gfc_add_modify (&se.pre, lock_acquired,
1041 : 6 : fold_convert (TREE_TYPE (lock_acquired),
1042 : : boolean_true_node));
1043 : :
1044 : 18 : return gfc_finish_block (&se.pre);
1045 : : }
1046 : :
1047 : : tree
1048 : 39 : gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
1049 : : {
1050 : 39 : gfc_se se, argse;
1051 : 39 : tree stat = NULL_TREE, stat2 = NULL_TREE;
1052 : 39 : tree until_count = NULL_TREE;
1053 : :
1054 : 39 : if (code->expr2)
1055 : : {
1056 : 8 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE);
1057 : 8 : gfc_init_se (&argse, NULL);
1058 : 8 : gfc_conv_expr_val (&argse, code->expr2);
1059 : 8 : stat = argse.expr;
1060 : : }
1061 : 31 : else if (flag_coarray == GFC_FCOARRAY_LIB)
1062 : 16 : stat = null_pointer_node;
1063 : :
1064 : 39 : if (code->expr4)
1065 : : {
1066 : 12 : gfc_init_se (&argse, NULL);
1067 : 12 : gfc_conv_expr_val (&argse, code->expr4);
1068 : 12 : until_count = fold_convert (integer_type_node, argse.expr);
1069 : : }
1070 : : else
1071 : 27 : until_count = integer_one_node;
1072 : :
1073 : 39 : if (flag_coarray != GFC_FCOARRAY_LIB)
1074 : : {
1075 : 19 : gfc_start_block (&se.pre);
1076 : 19 : gfc_init_se (&argse, NULL);
1077 : 19 : gfc_conv_expr_val (&argse, code->expr1);
1078 : :
1079 : 19 : if (op == EXEC_EVENT_POST)
1080 : 22 : gfc_add_modify (&se.pre, argse.expr,
1081 : : fold_build2_loc (input_location, PLUS_EXPR,
1082 : 11 : TREE_TYPE (argse.expr), argse.expr,
1083 : 11 : build_int_cst (TREE_TYPE (argse.expr), 1)));
1084 : : else
1085 : 16 : gfc_add_modify (&se.pre, argse.expr,
1086 : : fold_build2_loc (input_location, MINUS_EXPR,
1087 : 8 : TREE_TYPE (argse.expr), argse.expr,
1088 : 8 : fold_convert (TREE_TYPE (argse.expr),
1089 : : until_count)));
1090 : 19 : if (stat != NULL_TREE)
1091 : 4 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1092 : :
1093 : 19 : return gfc_finish_block (&se.pre);
1094 : : }
1095 : :
1096 : 20 : gfc_start_block (&se.pre);
1097 : 20 : tree tmp, token, image_index, errmsg, errmsg_len;
1098 : 20 : tree index = build_zero_cst (gfc_array_index_type);
1099 : 20 : tree caf_decl = gfc_get_tree_for_caf_expr (code->expr1);
1100 : :
1101 : 20 : if (code->expr1->symtree->n.sym->ts.type != BT_DERIVED
1102 : 20 : || code->expr1->symtree->n.sym->ts.u.derived->from_intmod
1103 : : != INTMOD_ISO_FORTRAN_ENV
1104 : 20 : || code->expr1->symtree->n.sym->ts.u.derived->intmod_sym_id
1105 : : != ISOFORTRAN_EVENT_TYPE)
1106 : : {
1107 : 0 : gfc_error ("Sorry, the event component of derived type at %L is not "
1108 : : "yet supported", &code->expr1->where);
1109 : 0 : return NULL_TREE;
1110 : : }
1111 : :
1112 : 20 : gfc_init_se (&argse, NULL);
1113 : 20 : gfc_get_caf_token_offset (&argse, &token, NULL, caf_decl, NULL_TREE,
1114 : : code->expr1);
1115 : 20 : gfc_add_block_to_block (&se.pre, &argse.pre);
1116 : :
1117 : 20 : if (gfc_is_coindexed (code->expr1))
1118 : 6 : image_index = gfc_caf_get_image_index (&se.pre, code->expr1, caf_decl);
1119 : : else
1120 : 14 : image_index = integer_zero_node;
1121 : :
1122 : : /* For arrays, obtain the array index. */
1123 : 20 : if (gfc_expr_attr (code->expr1).dimension)
1124 : : {
1125 : 7 : tree desc, tmp, extent, lbound, ubound;
1126 : 7 : gfc_array_ref *ar, ar2;
1127 : 7 : int i;
1128 : :
1129 : : /* TODO: Extend this, once DT components are supported. */
1130 : 7 : ar = &code->expr1->ref->u.ar;
1131 : 7 : ar2 = *ar;
1132 : 7 : memset (ar, '\0', sizeof (*ar));
1133 : 7 : ar->as = ar2.as;
1134 : 7 : ar->type = AR_FULL;
1135 : :
1136 : 7 : gfc_init_se (&argse, NULL);
1137 : 7 : argse.descriptor_only = 1;
1138 : 7 : gfc_conv_expr_descriptor (&argse, code->expr1);
1139 : 7 : gfc_add_block_to_block (&se.pre, &argse.pre);
1140 : 7 : desc = argse.expr;
1141 : 7 : *ar = ar2;
1142 : :
1143 : 7 : extent = build_one_cst (gfc_array_index_type);
1144 : 21 : for (i = 0; i < ar->dimen; i++)
1145 : : {
1146 : 7 : gfc_init_se (&argse, NULL);
1147 : 7 : gfc_conv_expr_type (&argse, ar->start[i], gfc_array_index_type);
1148 : 7 : gfc_add_block_to_block (&argse.pre, &argse.pre);
1149 : 7 : lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
1150 : 7 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
1151 : 7 : TREE_TYPE (lbound), argse.expr, lbound);
1152 : 7 : tmp = fold_build2_loc (input_location, MULT_EXPR,
1153 : 7 : TREE_TYPE (tmp), extent, tmp);
1154 : 7 : index = fold_build2_loc (input_location, PLUS_EXPR,
1155 : 7 : TREE_TYPE (tmp), index, tmp);
1156 : 7 : if (i < ar->dimen - 1)
1157 : : {
1158 : 0 : ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
1159 : 0 : tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
1160 : 0 : extent = fold_build2_loc (input_location, MULT_EXPR,
1161 : 0 : TREE_TYPE (tmp), extent, tmp);
1162 : : }
1163 : : }
1164 : : }
1165 : :
1166 : : /* errmsg. */
1167 : 20 : if (code->expr3)
1168 : : {
1169 : 0 : gfc_init_se (&argse, NULL);
1170 : 0 : argse.want_pointer = 1;
1171 : 0 : gfc_conv_expr (&argse, code->expr3);
1172 : 0 : gfc_add_block_to_block (&se.pre, &argse.pre);
1173 : 0 : errmsg = argse.expr;
1174 : 0 : errmsg_len = fold_convert (size_type_node, argse.string_length);
1175 : : }
1176 : : else
1177 : : {
1178 : 20 : errmsg = null_pointer_node;
1179 : 20 : errmsg_len = build_zero_cst (size_type_node);
1180 : : }
1181 : :
1182 : 20 : if (stat != null_pointer_node && TREE_TYPE (stat) != integer_type_node)
1183 : : {
1184 : 0 : stat2 = stat;
1185 : 0 : stat = gfc_create_var (integer_type_node, "stat");
1186 : : }
1187 : :
1188 : 20 : index = fold_convert (size_type_node, index);
1189 : 20 : if (op == EXEC_EVENT_POST)
1190 : 12 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_post, 6,
1191 : : token, index, image_index,
1192 : 12 : stat != null_pointer_node
1193 : 2 : ? gfc_build_addr_expr (NULL, stat) : stat,
1194 : : errmsg, errmsg_len);
1195 : : else
1196 : 8 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_event_wait, 6,
1197 : : token, index, until_count,
1198 : 8 : stat != null_pointer_node
1199 : 2 : ? gfc_build_addr_expr (NULL, stat) : stat,
1200 : : errmsg, errmsg_len);
1201 : 20 : gfc_add_expr_to_block (&se.pre, tmp);
1202 : :
1203 : : /* It guarantees memory consistency within the same segment */
1204 : 20 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1205 : 20 : 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 : 20 : ASM_VOLATILE_P (tmp) = 1;
1209 : 20 : gfc_add_expr_to_block (&se.pre, tmp);
1210 : :
1211 : 20 : if (stat2 != NULL_TREE)
1212 : 0 : gfc_add_modify (&se.pre, stat2, fold_convert (TREE_TYPE (stat2), stat));
1213 : :
1214 : 20 : return gfc_finish_block (&se.pre);
1215 : : }
1216 : :
1217 : : tree
1218 : 718 : gfc_trans_sync (gfc_code *code, gfc_exec_op type)
1219 : : {
1220 : 718 : gfc_se se, argse;
1221 : 718 : tree tmp;
1222 : 718 : tree images = NULL_TREE, stat = NULL_TREE,
1223 : 718 : 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 : 718 : if (!code->expr2 && !(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1228 : 620 : && flag_coarray != GFC_FCOARRAY_LIB)
1229 : : return NULL_TREE;
1230 : :
1231 : 392 : gfc_init_se (&se, NULL);
1232 : 392 : gfc_start_block (&se.pre);
1233 : :
1234 : 392 : if (code->expr1 && code->expr1->rank == 0)
1235 : : {
1236 : 13 : gfc_init_se (&argse, NULL);
1237 : 13 : gfc_conv_expr_val (&argse, code->expr1);
1238 : 13 : images = argse.expr;
1239 : : }
1240 : :
1241 : 392 : if (code->expr2)
1242 : : {
1243 : 71 : gcc_assert (code->expr2->expr_type == EXPR_VARIABLE
1244 : : || code->expr2->expr_type == EXPR_FUNCTION);
1245 : 71 : gfc_init_se (&argse, NULL);
1246 : 71 : gfc_conv_expr_val (&argse, code->expr2);
1247 : 71 : stat = argse.expr;
1248 : : }
1249 : : else
1250 : 321 : stat = null_pointer_node;
1251 : :
1252 : 392 : if (code->expr3 && flag_coarray == GFC_FCOARRAY_LIB)
1253 : : {
1254 : 14 : gcc_assert (code->expr3->expr_type == EXPR_VARIABLE
1255 : : || code->expr3->expr_type == EXPR_FUNCTION);
1256 : 14 : gfc_init_se (&argse, NULL);
1257 : 14 : argse.want_pointer = 1;
1258 : 14 : gfc_conv_expr (&argse, code->expr3);
1259 : 14 : gfc_conv_string_parameter (&argse);
1260 : 14 : errmsg = gfc_build_addr_expr (NULL, argse.expr);
1261 : 14 : errmsglen = fold_convert (size_type_node, argse.string_length);
1262 : : }
1263 : 378 : else if (flag_coarray == GFC_FCOARRAY_LIB)
1264 : : {
1265 : 318 : errmsg = null_pointer_node;
1266 : 318 : 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 : 392 : if (code->expr1 && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
1272 : 13 : && code->expr1->rank == 0)
1273 : : {
1274 : 11 : tree images2 = fold_convert (integer_type_node, images);
1275 : 11 : tree cond;
1276 : 11 : if (flag_coarray != GFC_FCOARRAY_LIB)
1277 : 4 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1278 : 4 : images, build_int_cst (TREE_TYPE (images), 1));
1279 : : else
1280 : : {
1281 : 7 : tree cond2;
1282 : 7 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_num_images,
1283 : : 2, integer_zero_node,
1284 : 7 : build_int_cst (integer_type_node, -1));
1285 : 7 : cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
1286 : : images2, tmp);
1287 : 7 : cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1288 : : images,
1289 : 7 : build_int_cst (TREE_TYPE (images), 1));
1290 : 7 : cond = fold_build2_loc (input_location, TRUTH_OR_EXPR,
1291 : : logical_type_node, cond, cond2);
1292 : : }
1293 : 11 : gfc_trans_runtime_check (true, false, cond, &se.pre,
1294 : 11 : &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 : 392 : if (flag_coarray == GFC_FCOARRAY_LIB)
1301 : : {
1302 : 332 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1303 : 332 : 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 : 332 : ASM_VOLATILE_P (tmp) = 1;
1307 : 332 : gfc_add_expr_to_block (&se.pre, tmp);
1308 : : }
1309 : :
1310 : 392 : if (flag_coarray != GFC_FCOARRAY_LIB)
1311 : : {
1312 : : /* Set STAT to zero. */
1313 : 60 : if (code->expr2)
1314 : 48 : gfc_add_modify (&se.pre, stat, build_int_cst (TREE_TYPE (stat), 0));
1315 : : }
1316 : 332 : 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 : 315 : if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1324 : : {
1325 : 315 : if (TREE_TYPE (stat) == integer_type_node)
1326 : 19 : stat = gfc_build_addr_expr (NULL, stat);
1327 : :
1328 : 315 : if(type == EXEC_SYNC_MEMORY)
1329 : 14 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_memory,
1330 : : 3, stat, errmsg, errmsglen);
1331 : : else
1332 : 301 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
1333 : : 3, stat, errmsg, errmsglen);
1334 : :
1335 : 315 : gfc_add_expr_to_block (&se.pre, tmp);
1336 : : }
1337 : : else
1338 : : {
1339 : 0 : tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1340 : :
1341 : 0 : 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 : 0 : gfc_add_expr_to_block (&se.pre, tmp);
1345 : :
1346 : 0 : gfc_add_modify (&se.pre, stat,
1347 : 0 : fold_convert (TREE_TYPE (stat), tmp_stat));
1348 : : }
1349 : : }
1350 : : else
1351 : : {
1352 : 17 : tree len;
1353 : :
1354 : 17 : gcc_assert (type == EXEC_SYNC_IMAGES);
1355 : :
1356 : 17 : if (!code->expr1)
1357 : : {
1358 : 6 : len = build_int_cst (integer_type_node, -1);
1359 : 6 : images = null_pointer_node;
1360 : : }
1361 : 11 : else if (code->expr1->rank == 0)
1362 : : {
1363 : 9 : len = integer_one_node;
1364 : 9 : images = gfc_build_addr_expr (NULL_TREE, images);
1365 : : }
1366 : : else
1367 : : {
1368 : : /* FIXME. */
1369 : 2 : if (code->expr1->ts.kind != gfc_c_int_kind)
1370 : 0 : 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 : 2 : gfc_conv_array_parameter (&se, code->expr1, true, NULL, NULL, &len);
1375 : 2 : images = se.expr;
1376 : :
1377 : 2 : tmp = gfc_typenode_for_spec (&code->expr1->ts);
1378 : 2 : if (GFC_ARRAY_TYPE_P (tmp) || GFC_DESCRIPTOR_TYPE_P (tmp))
1379 : 0 : tmp = gfc_get_element_type (tmp);
1380 : :
1381 : 4 : len = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
1382 : 2 : TREE_TYPE (len), len,
1383 : 2 : fold_convert (TREE_TYPE (len),
1384 : : TYPE_SIZE_UNIT (tmp)));
1385 : 2 : 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 : 17 : if (stat == null_pointer_node || TREE_TYPE (stat) == integer_type_node)
1394 : : {
1395 : 17 : if (TREE_TYPE (stat) == integer_type_node)
1396 : 4 : stat = gfc_build_addr_expr (NULL, stat);
1397 : :
1398 : 17 : 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 : 17 : gfc_add_expr_to_block (&se.pre, tmp);
1402 : : }
1403 : : else
1404 : : {
1405 : 0 : tree tmp_stat = gfc_create_var (integer_type_node, "stat");
1406 : :
1407 : 0 : 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 : 0 : gfc_add_expr_to_block (&se.pre, tmp);
1412 : :
1413 : 0 : gfc_add_modify (&se.pre, stat,
1414 : 0 : fold_convert (TREE_TYPE (stat), tmp_stat));
1415 : : }
1416 : : }
1417 : :
1418 : 392 : 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 : 232860 : gfc_trans_if_1 (gfc_code * code)
1464 : : {
1465 : 232860 : gfc_se if_se;
1466 : 232860 : tree stmt, elsestmt;
1467 : 232860 : location_t loc, saved_loc = UNKNOWN_LOCATION;
1468 : :
1469 : : /* Check for an unconditional ELSE clause. */
1470 : 232860 : if (!code->expr1)
1471 : 6586 : return gfc_trans_code (code->next);
1472 : :
1473 : : /* Initialize a statement builder for each block. Puts in NULL_TREEs. */
1474 : 226274 : gfc_init_se (&if_se, NULL);
1475 : 226274 : gfc_start_block (&if_se.pre);
1476 : :
1477 : : /* Calculate the IF condition expression. */
1478 : 226274 : if (GFC_LOCUS_IS_SET (code->expr1->where))
1479 : : {
1480 : 226274 : saved_loc = input_location;
1481 : 226274 : input_location = gfc_get_location (&code->expr1->where);
1482 : : }
1483 : :
1484 : 226274 : gfc_conv_expr_val (&if_se, code->expr1);
1485 : :
1486 : 226274 : if (saved_loc != UNKNOWN_LOCATION)
1487 : 226274 : input_location = saved_loc;
1488 : :
1489 : : /* Translate the THEN clause. */
1490 : 226274 : stmt = gfc_trans_code (code->next);
1491 : :
1492 : : /* Translate the ELSE clause. */
1493 : 226274 : if (code->block)
1494 : 6828 : elsestmt = gfc_trans_if_1 (code->block);
1495 : : else
1496 : 219446 : elsestmt = build_empty_stmt (input_location);
1497 : :
1498 : : /* Build the condition expression and add it to the condition block. */
1499 : 225726 : loc = (GFC_LOCUS_IS_SET (code->expr1->where)
1500 : 452000 : ? gfc_get_location (&code->expr1->where) : input_location);
1501 : 226274 : stmt = fold_build3_loc (loc, COND_EXPR, void_type_node, if_se.expr, stmt,
1502 : : elsestmt);
1503 : :
1504 : 226274 : gfc_add_expr_to_block (&if_se.pre, stmt);
1505 : :
1506 : : /* Finish off this statement. */
1507 : 226274 : return gfc_finish_block (&if_se.pre);
1508 : : }
1509 : :
1510 : : tree
1511 : 226032 : gfc_trans_if (gfc_code * code)
1512 : : {
1513 : 226032 : stmtblock_t body;
1514 : 226032 : tree exit_label;
1515 : :
1516 : : /* Create exit label so it is available for trans'ing the body code. */
1517 : 226032 : exit_label = gfc_build_label_decl (NULL_TREE);
1518 : 226032 : code->exit_label = exit_label;
1519 : :
1520 : : /* Translate the actual code in code->block. */
1521 : 226032 : gfc_init_block (&body);
1522 : 226032 : gfc_add_expr_to_block (&body, gfc_trans_if_1 (code->block));
1523 : :
1524 : : /* Add exit label. */
1525 : 226032 : gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
1526 : :
1527 : 226032 : 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 : 64 : gfc_trans_arithmetic_if (gfc_code * code)
1556 : : {
1557 : 64 : gfc_se se;
1558 : 64 : tree tmp;
1559 : 64 : tree branch1;
1560 : 64 : tree branch2;
1561 : 64 : tree zero;
1562 : :
1563 : : /* Start a new block. */
1564 : 64 : gfc_init_se (&se, NULL);
1565 : 64 : gfc_start_block (&se.pre);
1566 : :
1567 : : /* Pre-evaluate COND. */
1568 : 64 : gfc_conv_expr_val (&se, code->expr1);
1569 : 64 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
1570 : :
1571 : : /* Build something to compare with. */
1572 : 64 : zero = gfc_build_const (TREE_TYPE (se.expr), integer_zero_node);
1573 : :
1574 : 64 : 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 : 49 : branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1579 : 49 : branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label2));
1580 : :
1581 : 49 : if (code->label1->value != code->label3->value)
1582 : 36 : tmp = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
1583 : : se.expr, zero);
1584 : : else
1585 : 13 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
1586 : : se.expr, zero);
1587 : :
1588 : 49 : branch1 = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1589 : : tmp, branch1, branch2);
1590 : : }
1591 : : else
1592 : 15 : branch1 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label1));
1593 : :
1594 : 64 : if (code->label1->value != code->label3->value
1595 : 45 : && code->label2->value != code->label3->value)
1596 : : {
1597 : : /* if (cond <= 0) take branch1 else take branch2. */
1598 : 37 : branch2 = build1_v (GOTO_EXPR, gfc_get_label_decl (code->label3));
1599 : 37 : tmp = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
1600 : : se.expr, zero);
1601 : 37 : 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 : 64 : gfc_add_expr_to_block (&se.pre, branch1);
1607 : 64 : return gfc_finish_block (&se.pre);
1608 : : }
1609 : :
1610 : :
1611 : : /* Translate a CRITICAL block. */
1612 : : tree
1613 : 21 : gfc_trans_critical (gfc_code *code)
1614 : : {
1615 : 21 : stmtblock_t block;
1616 : 21 : tree tmp, token = NULL_TREE;
1617 : :
1618 : 21 : gfc_start_block (&block);
1619 : :
1620 : 21 : if (flag_coarray == GFC_FCOARRAY_LIB)
1621 : : {
1622 : 5 : tree zero_size = build_zero_cst (size_type_node);
1623 : 5 : token = gfc_get_symbol_decl (code->resolved_sym);
1624 : 5 : token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (token));
1625 : 5 : 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 : 5 : gfc_add_expr_to_block (&block, tmp);
1630 : :
1631 : : /* It guarantees memory consistency within the same segment */
1632 : 5 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1633 : 5 : 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 : 5 : ASM_VOLATILE_P (tmp) = 1;
1639 : :
1640 : 5 : gfc_add_expr_to_block (&block, tmp);
1641 : : }
1642 : :
1643 : 21 : tmp = gfc_trans_code (code->block->next);
1644 : 21 : gfc_add_expr_to_block (&block, tmp);
1645 : :
1646 : 21 : if (flag_coarray == GFC_FCOARRAY_LIB)
1647 : : {
1648 : 5 : tree zero_size = build_zero_cst (size_type_node);
1649 : 5 : 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 : 5 : gfc_add_expr_to_block (&block, tmp);
1654 : :
1655 : : /* It guarantees memory consistency within the same segment */
1656 : 5 : tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
1657 : 5 : 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 : 5 : ASM_VOLATILE_P (tmp) = 1;
1663 : :
1664 : 5 : gfc_add_expr_to_block (&block, tmp);
1665 : : }
1666 : :
1667 : 21 : return gfc_finish_block (&block);
1668 : : }
1669 : :
1670 : :
1671 : : /* Return true, when the class has a _len component. */
1672 : :
1673 : : static bool
1674 : 711 : class_has_len_component (gfc_symbol *sym)
1675 : : {
1676 : 711 : gfc_component *comp = sym->ts.u.derived->components;
1677 : 2040 : while (comp)
1678 : : {
1679 : 1746 : if (strcmp (comp->name, "_len") == 0)
1680 : : return true;
1681 : 1329 : comp = comp->next;
1682 : : }
1683 : : return false;
1684 : : }
1685 : :
1686 : :
1687 : : static void
1688 : 1720 : copy_descriptor (stmtblock_t *block, tree dst, tree src, int rank)
1689 : : {
1690 : 1720 : int n;
1691 : 1720 : tree dim;
1692 : 1720 : tree tmp;
1693 : 1720 : tree tmp2;
1694 : 1720 : tree size;
1695 : 1720 : tree offset;
1696 : :
1697 : 1720 : 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 : 1720 : tmp = TYPE_SIZE_UNIT (TREE_TYPE (src));
1702 : 1720 : tmp2 = TYPE_SIZE_UNIT (TREE_TYPE (dst));
1703 : 1720 : size = fold_build2_loc (input_location, MIN_EXPR,
1704 : 1720 : TREE_TYPE (tmp), tmp, tmp2);
1705 : 1720 : tmp = builtin_decl_explicit (BUILT_IN_MEMCPY);
1706 : 1720 : 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 : 1720 : gfc_add_expr_to_block (block, tmp);
1711 : :
1712 : : /* Set the offset correctly. */
1713 : 8592 : for (n = 0; n < rank; n++)
1714 : : {
1715 : 5152 : dim = gfc_rank_cst[n];
1716 : 5152 : tmp = gfc_conv_descriptor_lbound_get (src, dim);
1717 : 5152 : tmp2 = gfc_conv_descriptor_stride_get (src, dim);
1718 : 5152 : tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
1719 : : tmp, tmp2);
1720 : 5152 : offset = fold_build2_loc (input_location, MINUS_EXPR,
1721 : 5152 : TREE_TYPE (offset), offset, tmp);
1722 : 5152 : offset = gfc_evaluate_now (offset, block);
1723 : : }
1724 : :
1725 : 1720 : gfc_conv_descriptor_offset_set (block, dst, offset);
1726 : 1720 : }
1727 : :
1728 : :
1729 : : /* Do proper initialization for ASSOCIATE names. */
1730 : :
1731 : : static void
1732 : 6860 : trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
1733 : : {
1734 : 6860 : gfc_expr *e;
1735 : 6860 : tree tmp;
1736 : 6860 : bool class_target;
1737 : 6860 : bool unlimited;
1738 : 6860 : tree desc;
1739 : 6860 : tree offset;
1740 : 6860 : tree dim;
1741 : 6860 : int n;
1742 : 6860 : tree charlen;
1743 : 6860 : bool need_len_assign;
1744 : 6860 : bool whole_array = true;
1745 : 6860 : bool same_class;
1746 : 6860 : gfc_ref *ref;
1747 : 6860 : gfc_symbol *sym2;
1748 : :
1749 : 6860 : gcc_assert (sym->assoc);
1750 : 6860 : e = sym->assoc->target;
1751 : :
1752 : 16107 : class_target = (e->expr_type == EXPR_VARIABLE)
1753 : 6231 : && e->ts.type == BT_CLASS
1754 : 9327 : && (gfc_is_class_scalar_expr (e)
1755 : 1963 : || gfc_is_class_array_ref (e, NULL));
1756 : 2387 : same_class = class_target && sym->ts.type == BT_CLASS
1757 : 1066 : && strcmp (sym->ts.u.derived->name, e->ts.u.derived->name) == 0;
1758 : :
1759 : 6860 : unlimited = UNLIMITED_POLY (e);
1760 : :
1761 : 15131 : for (ref = e->ref; ref; ref = ref->next)
1762 : 8319 : if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
1763 : 3301 : && 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 : 5598 : need_len_assign = ((unlimited || (e->ts.type == BT_DERIVED
1779 : 2256 : && e->ts.u.derived->attr.unlimited_polymorphic))
1780 : 2069 : && (sym->ts.type == BT_CHARACTER
1781 : 1339 : || ((sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED)
1782 : 711 : && class_has_len_component (sym)))
1783 : 8007 : && !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 : 6860 : if (sym->attr.select_rank_temporary)
1794 : : {
1795 : 1362 : gfc_se se;
1796 : 1362 : tree class_decl = NULL_TREE;
1797 : 1362 : int rank = 0;
1798 : 1362 : bool class_ptr;
1799 : :
1800 : 1362 : sym2 = e->symtree->n.sym;
1801 : 1362 : gfc_init_se (&se, NULL);
1802 : 1362 : if (e->ts.type == BT_CLASS)
1803 : : {
1804 : : /* Go straight to the class data. */
1805 : 145 : if (sym2->attr.dummy && !sym2->attr.optional)
1806 : : {
1807 : 121 : class_decl = sym2->backend_decl;
1808 : 121 : if (DECL_LANG_SPECIFIC (class_decl)
1809 : 121 : && GFC_DECL_SAVED_DESCRIPTOR (class_decl))
1810 : 0 : class_decl = GFC_DECL_SAVED_DESCRIPTOR (class_decl);
1811 : 121 : if (POINTER_TYPE_P (TREE_TYPE (class_decl)))
1812 : 121 : class_decl = build_fold_indirect_ref_loc (input_location,
1813 : : class_decl);
1814 : 121 : gcc_assert (GFC_CLASS_TYPE_P (TREE_TYPE (class_decl)));
1815 : 121 : se.expr = gfc_class_data_get (class_decl);
1816 : : }
1817 : : else
1818 : : {
1819 : 24 : class_decl = sym2->backend_decl;
1820 : 24 : gfc_conv_expr_descriptor (&se, e);
1821 : 24 : if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1822 : 0 : se.expr = build_fold_indirect_ref_loc (input_location,
1823 : : se.expr);
1824 : : }
1825 : :
1826 : 145 : if (CLASS_DATA (sym)->as && CLASS_DATA (sym)->as->rank > 0)
1827 : 145 : rank = CLASS_DATA (sym)->as->rank;
1828 : : }
1829 : : else
1830 : : {
1831 : 1217 : gfc_conv_expr_descriptor (&se, e);
1832 : 1217 : if (sym->as && sym->as->rank > 0)
1833 : 1168 : rank = sym->as->rank;
1834 : : }
1835 : :
1836 : 1362 : desc = sym->backend_decl;
1837 : :
1838 : : /* The SELECT TYPE mechanisms turn class temporaries into pointers, which
1839 : : point to the selector. */
1840 : 1362 : class_ptr = class_decl != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (desc));
1841 : 145 : if (class_ptr)
1842 : : {
1843 : 145 : tmp = gfc_create_var (TREE_TYPE (TREE_TYPE (desc)), "class");
1844 : 145 : tmp = gfc_build_addr_expr (NULL, tmp);
1845 : 145 : gfc_add_modify (&se.pre, desc, tmp);
1846 : :
1847 : 145 : tmp = gfc_class_vptr_get (class_decl);
1848 : 145 : gfc_add_modify (&se.pre, gfc_class_vptr_get (desc), tmp);
1849 : 145 : if (UNLIMITED_POLY (sym))
1850 : 101 : gfc_add_modify (&se.pre, gfc_class_len_get (desc),
1851 : : gfc_class_len_get (class_decl));
1852 : :
1853 : 145 : 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 : 1362 : if (rank > 0)
1860 : 1282 : copy_descriptor (&se.pre, desc, se.expr, rank);
1861 : : else
1862 : : {
1863 : 80 : tmp = gfc_conv_descriptor_data_get (se.expr);
1864 : 80 : gfc_add_modify (&se.pre, desc,
1865 : 80 : 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 : 1362 : sym2 = sym->assoc->target->symtree->n.sym;
1871 : 1362 : if (sym2->assoc && sym->assoc->target && sym2->ts.type != BT_CLASS)
1872 : : {
1873 : 54 : sym2 = sym2->assoc->target->symtree->n.sym;
1874 : 54 : se.expr = sym2->backend_decl;
1875 : :
1876 : 54 : if (POINTER_TYPE_P (TREE_TYPE (se.expr)))
1877 : 54 : 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 : 1362 : if (sym->attr.allocatable || sym->attr.pointer
1884 : 996 : || (sym->ts.type == BT_CLASS
1885 : 127 : && (CLASS_DATA (sym)->attr.allocatable
1886 : 127 : || CLASS_DATA (sym)->attr.pointer)))
1887 : : {
1888 : 493 : if (rank > 0)
1889 : 438 : copy_descriptor (&se.post, se.expr, desc, rank);
1890 : : else
1891 : 55 : gfc_conv_descriptor_data_set (&se.post, se.expr, desc);
1892 : :
1893 : : /* The dynamic type could have changed too. */
1894 : 493 : if (sym->ts.type == BT_CLASS)
1895 : : {
1896 : 145 : tmp = sym->backend_decl;
1897 : 145 : if (class_ptr)
1898 : 145 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
1899 : 145 : gfc_add_modify (&se.post, gfc_class_vptr_get (class_decl),
1900 : : gfc_class_vptr_get (tmp));
1901 : 145 : if (UNLIMITED_POLY (sym))
1902 : 101 : gfc_add_modify (&se.post, gfc_class_len_get (class_decl),
1903 : : gfc_class_len_get (tmp));
1904 : : }
1905 : : }
1906 : :
1907 : 1362 : tmp = gfc_finish_block (&se.post);
1908 : :
1909 : 1362 : 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 : 5498 : else if (e->rank == -1 && sym->assoc->ar)
1919 : : {
1920 : 24 : gfc_array_ref *ar;
1921 : 24 : gfc_expr *expr1 = gfc_lval_expr_from_sym (sym);
1922 : 24 : stmtblock_t init;
1923 : 24 : gfc_init_block (&init);
1924 : :
1925 : : /* Build the array reference and add to expr1. */
1926 : 24 : gfc_free_ref_list (expr1->ref);
1927 : 24 : expr1->ref = gfc_get_ref();
1928 : 24 : expr1->ref->type = REF_ARRAY;
1929 : 24 : ar = gfc_copy_array_ref (sym->assoc->ar);
1930 : 24 : expr1->ref->u.ar = *ar;
1931 : 24 : 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 : 24 : if (expr1->ts.type == BT_CLASS)
1938 : : {
1939 : 12 : need_len_assign = false;
1940 : 12 : gfc_ref *ref;
1941 : 12 : gfc_find_component (expr1->ts.u.derived, "_data", true, true, &ref);
1942 : 12 : ref->next = expr1->ref;
1943 : 12 : expr1->ref = ref;
1944 : 12 : expr1->rank = CLASS_DATA (sym)->as->rank;
1945 : 12 : tmp = gfc_create_var (gfc_typenode_for_spec (&sym->ts), "class");
1946 : 12 : tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1947 : 12 : gfc_add_modify (&init, sym->backend_decl, tmp);
1948 : : }
1949 : :
1950 : : /* Do the pointer assignment and clean up. */
1951 : 24 : gfc_expr *expr2 = gfc_copy_expr (e);
1952 : 24 : gfc_add_expr_to_block (&init,
1953 : : gfc_trans_pointer_assignment (expr1, expr2));
1954 : 24 : gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL);
1955 : 24 : gfc_free_expr (expr1);
1956 : 24 : gfc_free_expr (expr2);
1957 : 24 : }
1958 : 5474 : else if ((sym->attr.dimension || sym->attr.codimension) && !class_target
1959 : 612 : && (sym->as->type == AS_DEFERRED || sym->assoc->variable))
1960 : : {
1961 : 612 : gfc_se se;
1962 : 612 : tree desc;
1963 : 612 : bool cst_array_ctor;
1964 : 612 : stmtblock_t init;
1965 : 612 : gfc_init_block (&init);
1966 : :
1967 : 612 : desc = sym->backend_decl;
1968 : 1224 : cst_array_ctor = e->expr_type == EXPR_ARRAY
1969 : 97 : && gfc_constant_array_constructor_p (e->value.constructor)
1970 : 630 : && 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 : 612 : gfc_init_se (&se, NULL);
1975 : :
1976 : 612 : if (sym->assoc->variable || cst_array_ctor)
1977 : : {
1978 : 387 : se.direct_byref = 1;
1979 : 387 : se.use_offset = 1;
1980 : 387 : se.expr = desc;
1981 : 387 : GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
1982 : : }
1983 : :
1984 : 612 : if (sym->attr.codimension && !sym->attr.dimension)
1985 : 8 : se.want_coarray = 1;
1986 : :
1987 : 612 : gfc_conv_expr_descriptor (&se, e);
1988 : :
1989 : 612 : if (sym->ts.type == BT_CHARACTER
1990 : 266 : && !sym->attr.select_type_temporary
1991 : 266 : && sym->ts.u.cl->backend_decl
1992 : 266 : && VAR_P (sym->ts.u.cl->backend_decl)
1993 : 222 : && se.string_length
1994 : 222 : && se.string_length != sym->ts.u.cl->backend_decl)
1995 : : {
1996 : : /* When the target is a variable, its length is already known. */
1997 : 222 : tree len = fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
1998 : : se.string_length);
1999 : 222 : if (e->expr_type == EXPR_VARIABLE)
2000 : 130 : gfc_add_modify (&init, sym->ts.u.cl->backend_decl, len);
2001 : : else
2002 : 92 : 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 : 612 : if ((!sym->assoc->variable && !cst_array_ctor)
2008 : 387 : || !whole_array)
2009 : : {
2010 : 225 : int dim;
2011 : :
2012 : 225 : if (whole_array)
2013 : 225 : 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 : 569 : for (dim = 0; dim < e->rank; ++dim)
2018 : 296 : 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 : 612 : if (sym->attr.subref_array_pointer && !se.direct_byref)
2025 : : {
2026 : 6 : gcc_assert (e->expr_type == EXPR_VARIABLE);
2027 : 6 : tmp = gfc_get_array_span (se.expr, e);
2028 : :
2029 : 6 : gfc_conv_descriptor_span_set (&se.pre, desc, tmp);
2030 : : }
2031 : :
2032 : 612 : if (e->expr_type == EXPR_FUNCTION
2033 : 90 : && sym->ts.type == BT_DERIVED
2034 : 43 : && sym->ts.u.derived
2035 : 43 : && sym->ts.u.derived->attr.pdt_type)
2036 : : {
2037 : 0 : tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, se.expr,
2038 : 0 : sym->as->rank);
2039 : 0 : gfc_add_expr_to_block (&se.post, tmp);
2040 : : }
2041 : :
2042 : : /* Done, register stuff as init / cleanup code. */
2043 : 612 : gfc_add_block_to_block (&init, &se.pre);
2044 : 612 : gfc_add_init_cleanup (block, gfc_finish_block (&init),
2045 : : gfc_finish_block (&se.post));
2046 : 612 : }
2047 : :
2048 : : /* Temporaries, arising from TYPE IS, just need the descriptor of class
2049 : : arrays to be assigned directly. */
2050 : 4862 : else if (class_target && (sym->attr.dimension || sym->attr.codimension)
2051 : 1321 : && (sym->ts.type == BT_DERIVED || unlimited))
2052 : : {
2053 : 1321 : gfc_se se;
2054 : :
2055 : 1321 : gfc_init_se (&se, NULL);
2056 : 1321 : 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 : 1321 : gfc_conv_expr_descriptor (&se, e);
2063 : :
2064 : 1321 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
2065 : : || GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)));
2066 : 1321 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (sym->backend_decl)));
2067 : :
2068 : 1321 : 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 : 302 : tree arraytype;
2073 : 302 : tmp = TREE_TYPE (sym->backend_decl);
2074 : 302 : arraytype = TREE_TYPE (GFC_TYPE_ARRAY_DATAPTR_TYPE (tmp));
2075 : 302 : if (! TYPE_NAME (arraytype))
2076 : 51 : TYPE_NAME (arraytype) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
2077 : : NULL_TREE, arraytype);
2078 : 302 : gfc_add_expr_to_block (&se.pre, build1 (DECL_EXPR,
2079 : 302 : arraytype, TYPE_NAME (arraytype)));
2080 : : }
2081 : :
2082 : 1321 : if (GFC_ARRAY_TYPE_P (TREE_TYPE (se.expr)))
2083 : : {
2084 : 0 : if (INDIRECT_REF_P (se.expr))
2085 : 0 : tmp = TREE_OPERAND (se.expr, 0);
2086 : : else
2087 : : tmp = se.expr;
2088 : :
2089 : 0 : gfc_add_modify (&se.pre, sym->backend_decl,
2090 : 0 : gfc_class_data_get (GFC_DECL_SAVED_DESCRIPTOR (tmp)));
2091 : : }
2092 : : else
2093 : 1321 : gfc_add_modify (&se.pre, sym->backend_decl, se.expr);
2094 : :
2095 : 1321 : if (unlimited)
2096 : : {
2097 : : /* Recover the dtype, which has been overwritten by the
2098 : : assignment from an unlimited polymorphic object. */
2099 : 797 : tmp = gfc_conv_descriptor_dtype (sym->backend_decl);
2100 : 797 : gfc_add_modify (&se.pre, tmp,
2101 : 797 : gfc_get_dtype (TREE_TYPE (sym->backend_decl)));
2102 : : }
2103 : :
2104 : 1321 : gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2105 : : gfc_finish_block (&se.post));
2106 : 1321 : }
2107 : :
2108 : : /* Do a scalar pointer assignment; this is for scalar variable targets. */
2109 : 3541 : else if (gfc_is_associate_pointer (sym))
2110 : : {
2111 : 3224 : gfc_se se;
2112 : :
2113 : 3224 : gcc_assert (!sym->attr.dimension && !sym->attr.codimension);
2114 : :
2115 : 3224 : 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 : 3224 : if (sym->ts.type == BT_CLASS && e->expr_type == EXPR_FUNCTION)
2120 : : {
2121 : 99 : gfc_conv_expr (&se, e);
2122 : 99 : se.expr = gfc_evaluate_now (se.expr, &se.pre);
2123 : : /* Finalize the expression and free if it is allocatable. */
2124 : 99 : gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (e), e->rank);
2125 : 99 : gfc_add_block_to_block (&se.post, &se.finalblock);
2126 : 99 : need_len_assign = false;
2127 : : }
2128 : 3125 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.dimension)
2129 : : {
2130 : 431 : tree target_expr;
2131 : : /* For a class array we need a descriptor for the selector. */
2132 : 431 : gfc_conv_expr_descriptor (&se, e);
2133 : : /* Needed to get/set the _len component below. */
2134 : 431 : target_expr = se.expr;
2135 : :
2136 : : /* Obtain a temporary class container for the result. */
2137 : 431 : gfc_conv_class_to_class (&se, e, sym->ts, false, true, false, false);
2138 : 431 : se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
2139 : :
2140 : 431 : desc = gfc_class_data_get (se.expr);
2141 : :
2142 : : /* Set the offset. */
2143 : 431 : offset = gfc_index_zero_node;
2144 : 912 : for (n = 0; n < e->rank; n++)
2145 : : {
2146 : 481 : dim = gfc_rank_cst[n];
2147 : 481 : 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 : 481 : offset = fold_build2_loc (input_location, MINUS_EXPR,
2152 : : gfc_array_index_type,
2153 : : offset, tmp);
2154 : : }
2155 : 431 : gfc_conv_descriptor_offset_set (&se.pre, desc, offset);
2156 : :
2157 : 431 : if (need_len_assign)
2158 : : {
2159 : 175 : if (e->symtree
2160 : 175 : && DECL_LANG_SPECIFIC (e->symtree->n.sym->backend_decl)
2161 : 84 : && GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl)
2162 : 205 : && TREE_CODE (target_expr) != COMPONENT_REF)
2163 : : /* Use the original class descriptor stored in the saved
2164 : : descriptor to get the target_expr. */
2165 : 36 : target_expr =
2166 : 18 : GFC_DECL_SAVED_DESCRIPTOR (e->symtree->n.sym->backend_decl);
2167 : : else
2168 : : /* Strip the _data component from the target_expr. */
2169 : 157 : target_expr = TREE_OPERAND (target_expr, 0);
2170 : : /* Add a reference to the _len comp to the target expr. */
2171 : 175 : tmp = gfc_class_len_get (target_expr);
2172 : : /* Get the component-ref for the temp structure's _len comp. */
2173 : 175 : charlen = gfc_class_len_get (se.expr);
2174 : : /* Add the assign to the beginning of the block... */
2175 : 175 : gfc_add_modify (&se.pre, charlen,
2176 : 175 : 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 : 175 : gfc_add_modify (&se.post, tmp,
2180 : 175 : fold_convert (TREE_TYPE (tmp), charlen));
2181 : : /* Length assignment done, prevent adding it again below. */
2182 : 175 : need_len_assign = false;
2183 : : }
2184 : : }
2185 : 2694 : else if (sym->ts.type == BT_CLASS && e->ts.type == BT_CLASS
2186 : 570 : && CLASS_DATA (e)->attr.dimension)
2187 : : {
2188 : : /* This is bound to be a class array element. */
2189 : 80 : gfc_conv_expr_reference (&se, e);
2190 : : /* Obtain a temporary class container for the result. */
2191 : 80 : gfc_conv_derived_to_class (&se, e, sym, se.expr, false, false,
2192 : 80 : e->symtree->name);
2193 : 80 : need_len_assign = false;
2194 : : }
2195 : 2614 : else if (whole_array && (same_class || unlimited)
2196 : 249 : && e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.codimension)
2197 : : {
2198 : 9 : gfc_expr *class_e = gfc_find_and_cut_at_last_class_ref (e);
2199 : 9 : gfc_conv_expr (&se, class_e);
2200 : 9 : gfc_free_expr (class_e);
2201 : 9 : need_len_assign = false;
2202 : 9 : }
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 : 2605 : gfc_conv_expr (&se, e);
2209 : 2605 : need_len_assign = need_len_assign && sym->ts.type == BT_CHARACTER;
2210 : : }
2211 : :
2212 : 3224 : if (sym->ts.type == BT_CHARACTER
2213 : 519 : && !sym->attr.select_type_temporary
2214 : 91 : && VAR_P (sym->ts.u.cl->backend_decl)
2215 : 55 : && se.string_length != sym->ts.u.cl->backend_decl)
2216 : : {
2217 : 55 : gfc_add_modify (&se.pre, sym->ts.u.cl->backend_decl,
2218 : 55 : fold_convert (TREE_TYPE (sym->ts.u.cl->backend_decl),
2219 : : se.string_length));
2220 : 55 : if (e->expr_type == EXPR_FUNCTION)
2221 : : {
2222 : 6 : tmp = gfc_call_free (sym->backend_decl);
2223 : 6 : gfc_add_expr_to_block (&se.post, tmp);
2224 : : }
2225 : : }
2226 : :
2227 : 519 : if (sym->ts.type == BT_CHARACTER && e->ts.type == BT_CHARACTER
2228 : 3315 : && POINTER_TYPE_P (TREE_TYPE (se.expr)))
2229 : : {
2230 : : /* These are pointer types already. */
2231 : 73 : tmp = fold_convert (TREE_TYPE (sym->backend_decl), se.expr);
2232 : : }
2233 : : else
2234 : : {
2235 : 3151 : tree ctree = gfc_get_class_from_expr (se.expr);
2236 : 3151 : tmp = TREE_TYPE (sym->backend_decl);
2237 : :
2238 : 3151 : 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 : 615 : if (e->rank == 0 && ctree
2245 : 1703 : && (!GFC_CLASS_TYPE_P (TREE_TYPE (se.expr))
2246 : 533 : || CLASS_DATA (e)->attr.class_pointer))
2247 : : {
2248 : 284 : tree stmp;
2249 : 284 : tree dtmp;
2250 : 284 : tree ctmp;
2251 : :
2252 : 284 : ctmp = ctree;
2253 : 284 : dtmp = TREE_TYPE (TREE_TYPE (sym->backend_decl));
2254 : 284 : ctree = gfc_create_var (dtmp, "class");
2255 : :
2256 : 284 : if (IS_INFERRED_TYPE (e)
2257 : 6 : && !GFC_CLASS_TYPE_P (TREE_TYPE (se.expr)))
2258 : : stmp = se.expr;
2259 : : else
2260 : 284 : stmp = gfc_class_data_get (ctmp);
2261 : :
2262 : 284 : if (!CLASS_DATA (sym)->attr.codimension
2263 : 284 : && !POINTER_TYPE_P (TREE_TYPE (stmp)))
2264 : 0 : stmp = gfc_build_addr_expr (NULL, stmp);
2265 : :
2266 : 284 : dtmp = gfc_class_data_get (ctree);
2267 : 284 : stmp = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (dtmp), stmp);
2268 : 284 : gfc_add_modify (&se.pre, dtmp, stmp);
2269 : 284 : stmp = gfc_class_vptr_get (ctmp);
2270 : 284 : dtmp = gfc_class_vptr_get (ctree);
2271 : 284 : stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2272 : 284 : gfc_add_modify (&se.pre, dtmp, stmp);
2273 : 284 : if (UNLIMITED_POLY (sym))
2274 : : {
2275 : 66 : stmp = gfc_class_len_get (ctmp);
2276 : 66 : dtmp = gfc_class_len_get (ctree);
2277 : 66 : stmp = fold_convert (TREE_TYPE (dtmp), stmp);
2278 : 66 : gfc_add_modify (&se.pre, dtmp, stmp);
2279 : 66 : need_len_assign = false;
2280 : : }
2281 : 284 : se.expr = ctree;
2282 : : }
2283 : 804 : else if (CLASS_DATA (sym)->attr.codimension)
2284 : : {
2285 : 27 : gfc_conv_class_to_class (&se, e, sym->ts, false, false, false,
2286 : : false);
2287 : 27 : 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 : 6274 : if (!POINTER_TYPE_P (TREE_TYPE (se.expr))
2294 : 3151 : || TREE_TYPE (TREE_TYPE (se.expr))
2295 : 28 : != TREE_TYPE (TREE_TYPE (sym->backend_decl)))
2296 : 3124 : tmp = gfc_build_addr_expr (tmp, se.expr);
2297 : : }
2298 : :
2299 : 3224 : gfc_add_modify (&se.pre, sym->backend_decl, tmp);
2300 : :
2301 : 3224 : 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 : 317 : gfc_expr *lhs;
2310 : 317 : tree res;
2311 : 317 : gfc_se se;
2312 : 317 : stmtblock_t final_block;
2313 : :
2314 : 317 : 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 : 317 : if (sym->ts.type == BT_DERIVED
2321 : 73 : && sym->ts.u.derived->attr.alloc_comp)
2322 : : {
2323 : 12 : tmp = sym->backend_decl;
2324 : 12 : tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, tmp,
2325 : 12 : sym->attr.dimension ? sym->as->rank : 0);
2326 : 12 : gfc_add_expr_to_block (&se.pre, tmp);
2327 : : }
2328 : :
2329 : 317 : if (sym->attr.allocatable)
2330 : : {
2331 : 12 : tmp = sym->backend_decl;
2332 : 12 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2333 : 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2334 : 12 : gfc_add_modify (&se.pre, tmp, fold_convert (TREE_TYPE (tmp),
2335 : : null_pointer_node));
2336 : : }
2337 : :
2338 : 317 : lhs = gfc_lval_expr_from_sym (sym);
2339 : 317 : lhs->must_finalize = 0;
2340 : 317 : res = gfc_trans_assignment (lhs, e, false, true);
2341 : 317 : gfc_add_expr_to_block (&se.pre, res);
2342 : :
2343 : 317 : gfc_init_block (&final_block);
2344 : :
2345 : 317 : if (sym->attr.associate_var
2346 : 317 : && sym->ts.type == BT_DERIVED
2347 : 73 : && sym->ts.u.derived->attr.defined_assign_comp
2348 : 0 : && gfc_may_be_finalized (sym->ts)
2349 : 317 : && e->expr_type == EXPR_FUNCTION)
2350 : : {
2351 : 0 : gfc_expr *ef;
2352 : 0 : ef = gfc_lval_expr_from_sym (sym);
2353 : 0 : gfc_add_finalizer_call (&final_block, ef);
2354 : 0 : gfc_free_expr (ef);
2355 : : }
2356 : :
2357 : 317 : if (sym->ts.type == BT_DERIVED
2358 : 73 : && sym->ts.u.derived->attr.alloc_comp)
2359 : : {
2360 : 12 : tmp = sym->backend_decl;
2361 : 12 : tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived,
2362 : : tmp, 0);
2363 : 12 : gfc_add_expr_to_block (&final_block, tmp);
2364 : : }
2365 : :
2366 : 317 : tmp = sym->backend_decl;
2367 : 317 : if (e->expr_type == EXPR_FUNCTION
2368 : 70 : && sym->ts.type == BT_DERIVED
2369 : 39 : && sym->ts.u.derived
2370 : 39 : && sym->ts.u.derived->attr.pdt_type)
2371 : : {
2372 : 6 : tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived, tmp,
2373 : : 0);
2374 : : }
2375 : 311 : else if (e->expr_type == EXPR_FUNCTION
2376 : 64 : && sym->ts.type == BT_CLASS
2377 : 0 : && CLASS_DATA (sym)->ts.u.derived
2378 : 0 : && CLASS_DATA (sym)->ts.u.derived->attr.pdt_type)
2379 : : {
2380 : 0 : tmp = gfc_class_data_get (tmp);
2381 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (sym)->ts.u.derived,
2382 : : tmp, 0);
2383 : : }
2384 : 311 : else if (sym->attr.allocatable)
2385 : : {
2386 : 12 : tmp = sym->backend_decl;
2387 : :
2388 : 12 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
2389 : 0 : tmp = gfc_conv_descriptor_data_get (tmp);
2390 : :
2391 : : /* A simple call to free suffices here. */
2392 : 12 : tmp = gfc_call_free (tmp);
2393 : :
2394 : : /* Make sure that reallocation on assignment cannot occur. */
2395 : 12 : sym->attr.allocatable = 0;
2396 : : }
2397 : : else
2398 : : tmp = NULL_TREE;
2399 : :
2400 : 317 : gfc_add_expr_to_block (&final_block, tmp);
2401 : 317 : tmp = gfc_finish_block (&final_block);
2402 : 317 : res = gfc_finish_block (&se.pre);
2403 : 317 : gfc_add_init_cleanup (block, res, tmp);
2404 : 317 : gfc_free_expr (lhs);
2405 : : }
2406 : :
2407 : : /* Set the stringlength, when needed. */
2408 : 6860 : if (need_len_assign)
2409 : : {
2410 : 730 : gfc_se se;
2411 : 730 : gfc_init_se (&se, NULL);
2412 : 730 : if (e->symtree->n.sym->ts.type == BT_CHARACTER)
2413 : : {
2414 : : /* Deferred strings are dealt with in the preceding. */
2415 : 0 : gcc_assert (!e->symtree->n.sym->ts.deferred);
2416 : 0 : tmp = e->symtree->n.sym->ts.u.cl->backend_decl;
2417 : : }
2418 : 730 : else if (e->symtree->n.sym->attr.function
2419 : 12 : && e->symtree->n.sym == e->symtree->n.sym->result)
2420 : : {
2421 : 12 : tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
2422 : 12 : tmp = gfc_class_len_get (tmp);
2423 : : }
2424 : : else
2425 : 718 : tmp = gfc_class_len_get (gfc_get_symbol_decl (e->symtree->n.sym));
2426 : 730 : gfc_get_symbol_decl (sym);
2427 : 730 : charlen = sym->ts.type == BT_CHARACTER ? sym->ts.u.cl->backend_decl
2428 : 0 : : gfc_class_len_get (sym->backend_decl);
2429 : : /* Prevent adding a noop len= len. */
2430 : 730 : if (tmp != charlen)
2431 : : {
2432 : 730 : gfc_add_modify (&se.pre, charlen,
2433 : 730 : fold_convert (TREE_TYPE (charlen), tmp));
2434 : 730 : gfc_add_init_cleanup (block, gfc_finish_block (&se.pre),
2435 : : gfc_finish_block (&se.post));
2436 : : }
2437 : : }
2438 : 6860 : }
2439 : :
2440 : :
2441 : : /* Translate a BLOCK construct. This is basically what we would do for a
2442 : : procedure body. */
2443 : :
2444 : : tree
2445 : 13232 : gfc_trans_block_construct (gfc_code* code)
2446 : : {
2447 : 13232 : gfc_namespace* ns;
2448 : 13232 : gfc_symbol* sym;
2449 : 13232 : gfc_wrapped_block block;
2450 : 13232 : tree exit_label;
2451 : 13232 : stmtblock_t body;
2452 : 13232 : gfc_association_list *ass;
2453 : 13232 : tree translated_body;
2454 : :
2455 : 13232 : ns = code->ext.block.ns;
2456 : 13232 : gcc_assert (ns);
2457 : 13232 : sym = ns->proc_name;
2458 : 13232 : gcc_assert (sym);
2459 : :
2460 : : /* Process local variables. */
2461 : 13232 : gcc_assert (!sym->tlink);
2462 : 13232 : sym->tlink = sym;
2463 : 13232 : gfc_process_block_locals (ns);
2464 : :
2465 : : /* Generate code including exit-label. */
2466 : 13232 : gfc_init_block (&body);
2467 : 13232 : exit_label = gfc_build_label_decl (NULL_TREE);
2468 : 13232 : code->exit_label = exit_label;
2469 : :
2470 : 13232 : finish_oacc_declare (ns, sym, true);
2471 : :
2472 : 13232 : translated_body = gfc_trans_code (ns->code);
2473 : 13232 : if (ns->omp_structured_block)
2474 : 452 : translated_body = build1 (OMP_STRUCTURED_BLOCK, void_type_node,
2475 : : translated_body);
2476 : 13232 : gfc_add_expr_to_block (&body, translated_body);
2477 : 13232 : gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
2478 : :
2479 : : /* Finish everything. */
2480 : 13232 : gfc_start_wrapped_block (&block, gfc_finish_block (&body));
2481 : 13232 : gfc_trans_deferred_vars (sym, &block);
2482 : 20092 : for (ass = code->ext.block.assoc; ass; ass = ass->next)
2483 : 6860 : trans_associate_var (ass->st->n.sym, &block);
2484 : :
2485 : 13232 : 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 : 24910 : gfc_trans_simple_do (gfc_code * code, stmtblock_t *pblock, tree dovar,
2519 : : tree from, tree to, tree step, tree exit_cond)
2520 : : {
2521 : 24910 : stmtblock_t body;
2522 : 24910 : tree type;
2523 : 24910 : tree cond;
2524 : 24910 : tree tmp;
2525 : 24910 : tree saved_dovar = NULL;
2526 : 24910 : tree cycle_label;
2527 : 24910 : tree exit_label;
2528 : 24910 : location_t loc;
2529 : 24910 : type = TREE_TYPE (dovar);
2530 : 24910 : bool is_step_positive = tree_int_cst_sgn (step) > 0;
2531 : :
2532 : 24910 : loc = gfc_get_location (&code->ext.iterator->start->where);
2533 : :
2534 : : /* Initialize the DO variable: dovar = from. */
2535 : 24910 : gfc_add_modify_loc (loc, pblock, dovar,
2536 : 24910 : fold_convert (TREE_TYPE (dovar), from));
2537 : :
2538 : : /* Save value for do-tinkering checking. */
2539 : 24910 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2540 : : {
2541 : 191 : saved_dovar = gfc_create_var (type, ".saved_dovar");
2542 : 191 : gfc_add_modify_loc (loc, pblock, saved_dovar, dovar);
2543 : : }
2544 : :
2545 : : /* Cycle and exit statements are implemented with gotos. */
2546 : 24910 : cycle_label = gfc_build_label_decl (NULL_TREE);
2547 : 24910 : exit_label = gfc_build_label_decl (NULL_TREE);
2548 : :
2549 : : /* Put the labels where they can be found later. See gfc_trans_do(). */
2550 : 24910 : code->cycle_label = cycle_label;
2551 : 24910 : code->exit_label = exit_label;
2552 : :
2553 : : /* Loop body. */
2554 : 24910 : gfc_start_block (&body);
2555 : :
2556 : : /* Exit the loop if there is an I/O result condition or error. */
2557 : 24910 : if (exit_cond)
2558 : : {
2559 : 320 : tmp = build1_v (GOTO_EXPR, exit_label);
2560 : 320 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2561 : : exit_cond, tmp,
2562 : : build_empty_stmt (loc));
2563 : 320 : gfc_add_expr_to_block (&body, tmp);
2564 : : }
2565 : :
2566 : : /* Evaluate the loop condition. */
2567 : 24910 : if (is_step_positive)
2568 : 24795 : cond = fold_build2_loc (loc, GT_EXPR, logical_type_node, dovar,
2569 : : fold_convert (type, to));
2570 : : else
2571 : 115 : cond = fold_build2_loc (loc, LT_EXPR, logical_type_node, dovar,
2572 : : fold_convert (type, to));
2573 : :
2574 : 24910 : cond = gfc_evaluate_now_loc (loc, cond, &body);
2575 : 24910 : if (code->ext.iterator->annot.unroll && cond != error_mark_node)
2576 : 12 : cond
2577 : 12 : = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2578 : 12 : build_int_cst (integer_type_node, annot_expr_unroll_kind),
2579 : 12 : build_int_cst (integer_type_node,
2580 : 12 : code->ext.iterator->annot.unroll));
2581 : :
2582 : 24910 : if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
2583 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2584 : 2 : build_int_cst (integer_type_node, annot_expr_ivdep_kind),
2585 : : integer_zero_node);
2586 : 24910 : if (code->ext.iterator->annot.vector && cond != error_mark_node)
2587 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2588 : 2 : build_int_cst (integer_type_node, annot_expr_vector_kind),
2589 : : integer_zero_node);
2590 : 24910 : if (code->ext.iterator->annot.novector && cond != error_mark_node)
2591 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2592 : 2 : build_int_cst (integer_type_node, annot_expr_no_vector_kind),
2593 : : integer_zero_node);
2594 : :
2595 : : /* The loop exit. */
2596 : 24910 : tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
2597 : 24910 : TREE_USED (exit_label) = 1;
2598 : 24910 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2599 : : cond, tmp, build_empty_stmt (loc));
2600 : 24910 : 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 : 24910 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2605 : : {
2606 : 197 : tree boundary = is_step_positive ? TYPE_MAX_VALUE (type)
2607 : 6 : : TYPE_MIN_VALUE (type);
2608 : :
2609 : 191 : tmp = fold_build2_loc (loc, EQ_EXPR, logical_type_node,
2610 : : dovar, boundary);
2611 : 191 : gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2612 : : "Loop iterates infinitely");
2613 : : }
2614 : :
2615 : : /* Main loop body. */
2616 : 24910 : tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2617 : 24910 : gfc_add_expr_to_block (&body, tmp);
2618 : :
2619 : : /* Label for cycle statements (if needed). */
2620 : 24910 : if (TREE_USED (cycle_label))
2621 : : {
2622 : 24910 : tmp = build1_v (LABEL_EXPR, cycle_label);
2623 : 24910 : gfc_add_expr_to_block (&body, tmp);
2624 : : }
2625 : :
2626 : : /* Check whether someone has modified the loop variable. */
2627 : 24910 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2628 : : {
2629 : 191 : tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node,
2630 : : dovar, saved_dovar);
2631 : 191 : gfc_trans_runtime_check (true, false, tmp, &body, &code->loc,
2632 : : "Loop variable has been modified");
2633 : : }
2634 : :
2635 : : /* Increment the loop variable. */
2636 : 24910 : tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2637 : 24910 : gfc_add_modify_loc (loc, &body, dovar, tmp);
2638 : :
2639 : 24910 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2640 : 191 : gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
2641 : :
2642 : : /* Finish the loop body. */
2643 : 24910 : tmp = gfc_finish_block (&body);
2644 : 24910 : tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
2645 : :
2646 : 24910 : gfc_add_expr_to_block (pblock, tmp);
2647 : :
2648 : : /* Add the exit label. */
2649 : 24910 : tmp = build1_v (LABEL_EXPR, exit_label);
2650 : 24910 : gfc_add_expr_to_block (pblock, tmp);
2651 : :
2652 : 24910 : 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 : 25945 : gfc_trans_do (gfc_code * code, tree exit_cond)
2692 : : {
2693 : 25945 : gfc_se se;
2694 : 25945 : tree dovar;
2695 : 25945 : tree saved_dovar = NULL;
2696 : 25945 : tree from;
2697 : 25945 : tree to;
2698 : 25945 : tree step;
2699 : 25945 : tree countm1;
2700 : 25945 : tree type;
2701 : 25945 : tree utype;
2702 : 25945 : tree cond;
2703 : 25945 : tree cycle_label;
2704 : 25945 : tree exit_label;
2705 : 25945 : tree tmp;
2706 : 25945 : stmtblock_t block;
2707 : 25945 : stmtblock_t body;
2708 : 25945 : location_t loc;
2709 : :
2710 : 25945 : gfc_start_block (&block);
2711 : :
2712 : 25945 : loc = gfc_get_location (&code->ext.iterator->start->where);
2713 : :
2714 : : /* Evaluate all the expressions in the iterator. */
2715 : 25945 : gfc_init_se (&se, NULL);
2716 : 25945 : gfc_conv_expr_lhs (&se, code->ext.iterator->var);
2717 : 25945 : gfc_add_block_to_block (&block, &se.pre);
2718 : 25945 : dovar = se.expr;
2719 : 25945 : type = TREE_TYPE (dovar);
2720 : :
2721 : 25945 : gfc_init_se (&se, NULL);
2722 : 25945 : gfc_conv_expr_val (&se, code->ext.iterator->start);
2723 : 25945 : gfc_add_block_to_block (&block, &se.pre);
2724 : 25945 : from = gfc_evaluate_now (se.expr, &block);
2725 : :
2726 : 25945 : gfc_init_se (&se, NULL);
2727 : 25945 : gfc_conv_expr_val (&se, code->ext.iterator->end);
2728 : 25945 : gfc_add_block_to_block (&block, &se.pre);
2729 : 25945 : to = gfc_evaluate_now (se.expr, &block);
2730 : :
2731 : 25945 : gfc_init_se (&se, NULL);
2732 : 25945 : gfc_conv_expr_val (&se, code->ext.iterator->step);
2733 : 25945 : gfc_add_block_to_block (&block, &se.pre);
2734 : 25945 : step = gfc_evaluate_now (se.expr, &block);
2735 : :
2736 : 25945 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2737 : : {
2738 : 203 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, step,
2739 : : build_zero_cst (type));
2740 : 203 : gfc_trans_runtime_check (true, false, tmp, &block, &code->loc,
2741 : : "DO step value is zero");
2742 : : }
2743 : :
2744 : : /* Special case simple loops. */
2745 : 25945 : if (TREE_CODE (type) == INTEGER_TYPE
2746 : 25945 : && (integer_onep (step)
2747 : 1068 : || tree_int_cst_equal (step, integer_minus_one_node)))
2748 : 24910 : return gfc_trans_simple_do (code, &block, dovar, from, to, step,
2749 : 24910 : exit_cond);
2750 : :
2751 : 1035 : if (TREE_CODE (type) == INTEGER_TYPE)
2752 : 953 : utype = unsigned_type_for (type);
2753 : : else
2754 : 82 : utype = unsigned_type_for (gfc_array_index_type);
2755 : 1035 : countm1 = gfc_create_var (utype, "countm1");
2756 : :
2757 : : /* Cycle and exit statements are implemented with gotos. */
2758 : 1035 : cycle_label = gfc_build_label_decl (NULL_TREE);
2759 : 1035 : exit_label = gfc_build_label_decl (NULL_TREE);
2760 : 1035 : TREE_USED (exit_label) = 1;
2761 : :
2762 : : /* Put these labels where they can be found later. */
2763 : 1035 : code->cycle_label = cycle_label;
2764 : 1035 : code->exit_label = exit_label;
2765 : :
2766 : : /* Initialize the DO variable: dovar = from. */
2767 : 1035 : gfc_add_modify (&block, dovar, from);
2768 : :
2769 : : /* Save value for do-tinkering checking. */
2770 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2771 : : {
2772 : 12 : saved_dovar = gfc_create_var (type, ".saved_dovar");
2773 : 12 : 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 : 1035 : if (TREE_CODE (type) == INTEGER_TYPE)
2793 : : {
2794 : 953 : 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 : 953 : tou = fold_convert (utype, to);
2800 : 953 : fromu = fold_convert (utype, from);
2801 : 953 : 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 : 953 : tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, to, from);
2806 : 953 : tmp2 = fold_build2_loc (loc, TRUNC_DIV_EXPR, utype,
2807 : : fold_build2_loc (loc, MINUS_EXPR, utype,
2808 : : tou, fromu),
2809 : : stepu);
2810 : 953 : 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 : 953 : tmp = fold_build2_loc (loc, GT_EXPR, logical_type_node, to, from);
2821 : 953 : 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 : 953 : 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 : 953 : tmp = fold_build2_loc (loc, LT_EXPR, logical_type_node, step,
2834 : 953 : build_int_cst (TREE_TYPE (step), 0));
2835 : 953 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, tmp, neg, pos);
2836 : :
2837 : 953 : gfc_add_expr_to_block (&block, tmp);
2838 : : }
2839 : : else
2840 : : {
2841 : 82 : 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 : 82 : tmp = fold_build2_loc (loc, MINUS_EXPR, type, to, from);
2848 : 82 : tmp = fold_build2_loc (loc, RDIV_EXPR, type, tmp, step);
2849 : 82 : tmp = fold_build1_loc (loc, FIX_TRUNC_EXPR, utype, tmp);
2850 : 82 : 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 : 82 : pos_step = fold_build2_loc (loc, GT_EXPR, logical_type_node, step,
2855 : : build_zero_cst (type));
2856 : 82 : 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 : 82 : 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 : 82 : gfc_add_expr_to_block (&block, tmp);
2866 : : }
2867 : :
2868 : : /* Loop body. */
2869 : 1035 : gfc_start_block (&body);
2870 : :
2871 : : /* Main loop body. */
2872 : 1035 : tmp = gfc_trans_code_cond (code->block->next, exit_cond);
2873 : 1035 : gfc_add_expr_to_block (&body, tmp);
2874 : :
2875 : : /* Label for cycle statements (if needed). */
2876 : 1035 : if (TREE_USED (cycle_label))
2877 : : {
2878 : 1035 : tmp = build1_v (LABEL_EXPR, cycle_label);
2879 : 1035 : gfc_add_expr_to_block (&body, tmp);
2880 : : }
2881 : :
2882 : : /* Check whether someone has modified the loop variable. */
2883 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2884 : : {
2885 : 12 : tmp = fold_build2_loc (loc, NE_EXPR, logical_type_node, dovar,
2886 : : saved_dovar);
2887 : 12 : 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 : 1035 : if (exit_cond)
2893 : : {
2894 : 1 : tmp = build1_v (GOTO_EXPR, exit_label);
2895 : 1 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2896 : : exit_cond, tmp,
2897 : : build_empty_stmt (input_location));
2898 : 1 : gfc_add_expr_to_block (&body, tmp);
2899 : : }
2900 : :
2901 : : /* Increment the loop variable. */
2902 : 1035 : tmp = fold_build2_loc (loc, PLUS_EXPR, type, dovar, step);
2903 : 1035 : gfc_add_modify_loc (loc, &body, dovar, tmp);
2904 : :
2905 : 1035 : if (gfc_option.rtcheck & GFC_RTCHECK_DO)
2906 : 12 : gfc_add_modify_loc (loc, &body, saved_dovar, dovar);
2907 : :
2908 : : /* Initialize countm1t. */
2909 : 1035 : tree countm1t = gfc_create_var (utype, "countm1t");
2910 : 1035 : gfc_add_modify_loc (loc, &body, countm1t, countm1);
2911 : :
2912 : : /* Decrement the loop count. */
2913 : 1035 : tmp = fold_build2_loc (loc, MINUS_EXPR, utype, countm1,
2914 : 1035 : build_int_cst (utype, 1));
2915 : 1035 : gfc_add_modify_loc (loc, &body, countm1, tmp);
2916 : :
2917 : : /* End with the loop condition. Loop until countm1t == 0. */
2918 : 1035 : cond = fold_build2_loc (loc, EQ_EXPR, logical_type_node, countm1t,
2919 : 1035 : build_int_cst (utype, 0));
2920 : 1035 : if (code->ext.iterator->annot.unroll && cond != error_mark_node)
2921 : 3 : cond
2922 : 3 : = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2923 : 3 : build_int_cst (integer_type_node, annot_expr_unroll_kind),
2924 : 3 : build_int_cst (integer_type_node,
2925 : 3 : code->ext.iterator->annot.unroll));
2926 : :
2927 : 1035 : if (code->ext.iterator->annot.ivdep && cond != error_mark_node)
2928 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2929 : 0 : build_int_cst (integer_type_node, annot_expr_ivdep_kind),
2930 : : integer_zero_node);
2931 : 1035 : if (code->ext.iterator->annot.vector && cond != error_mark_node)
2932 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2933 : 0 : build_int_cst (integer_type_node, annot_expr_vector_kind),
2934 : : integer_zero_node);
2935 : 1035 : if (code->ext.iterator->annot.novector && cond != error_mark_node)
2936 : 0 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
2937 : 0 : build_int_cst (integer_type_node, annot_expr_no_vector_kind),
2938 : : integer_zero_node);
2939 : :
2940 : 1035 : tmp = fold_build1_loc (loc, GOTO_EXPR, void_type_node, exit_label);
2941 : 1035 : tmp = fold_build3_loc (loc, COND_EXPR, void_type_node,
2942 : : cond, tmp, build_empty_stmt (loc));
2943 : 1035 : gfc_add_expr_to_block (&body, tmp);
2944 : :
2945 : : /* End of loop body. */
2946 : 1035 : tmp = gfc_finish_block (&body);
2947 : :
2948 : : /* The for loop itself. */
2949 : 1035 : tmp = fold_build1_loc (loc, LOOP_EXPR, void_type_node, tmp);
2950 : 1035 : gfc_add_expr_to_block (&block, tmp);
2951 : :
2952 : : /* Add the exit label. */
2953 : 1035 : tmp = build1_v (LABEL_EXPR, exit_label);
2954 : 1035 : gfc_add_expr_to_block (&block, tmp);
2955 : :
2956 : 1035 : 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 : 495 : gfc_trans_do_while (gfc_code * code)
2985 : : {
2986 : 495 : gfc_se cond;
2987 : 495 : tree tmp;
2988 : 495 : tree cycle_label;
2989 : 495 : tree exit_label;
2990 : 495 : stmtblock_t block;
2991 : :
2992 : : /* Everything we build here is part of the loop body. */
2993 : 495 : gfc_start_block (&block);
2994 : :
2995 : : /* Cycle and exit statements are implemented with gotos. */
2996 : 495 : cycle_label = gfc_build_label_decl (NULL_TREE);
2997 : 495 : exit_label = gfc_build_label_decl (NULL_TREE);
2998 : :
2999 : : /* Put the labels where they can be found later. See gfc_trans_do(). */
3000 : 495 : code->cycle_label = cycle_label;
3001 : 495 : code->exit_label = exit_label;
3002 : :
3003 : : /* Create a GIMPLE version of the exit condition. */
3004 : 495 : gfc_init_se (&cond, NULL);
3005 : 495 : gfc_conv_expr_val (&cond, code->expr1);
3006 : 495 : gfc_add_block_to_block (&block, &cond.pre);
3007 : 495 : cond.expr = fold_build1_loc (gfc_get_location (&code->expr1->where),
3008 : 495 : TRUTH_NOT_EXPR, TREE_TYPE (cond.expr),
3009 : : cond.expr);
3010 : :
3011 : : /* Build "IF (! cond) GOTO exit_label". */
3012 : 495 : tmp = build1_v (GOTO_EXPR, exit_label);
3013 : 495 : TREE_USED (exit_label) = 1;
3014 : 495 : 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 : 495 : &code->expr1->where)));
3018 : 495 : gfc_add_expr_to_block (&block, tmp);
3019 : :
3020 : : /* The main body of the loop. */
3021 : 495 : tmp = gfc_trans_code (code->block->next);
3022 : 495 : gfc_add_expr_to_block (&block, tmp);
3023 : :
3024 : : /* Label for cycle statements (if needed). */
3025 : 495 : if (TREE_USED (cycle_label))
3026 : : {
3027 : 495 : tmp = build1_v (LABEL_EXPR, cycle_label);
3028 : 495 : gfc_add_expr_to_block (&block, tmp);
3029 : : }
3030 : :
3031 : : /* End of loop body. */
3032 : 495 : tmp = gfc_finish_block (&block);
3033 : :
3034 : 495 : gfc_init_block (&block);
3035 : : /* Build the loop. */
3036 : 495 : tmp = fold_build1_loc (gfc_get_location (&code->expr1->where), LOOP_EXPR,
3037 : : void_type_node, tmp);
3038 : 495 : gfc_add_expr_to_block (&block, tmp);
3039 : :
3040 : : /* Add the exit label. */
3041 : 495 : tmp = build1_v (LABEL_EXPR, exit_label);
3042 : 495 : gfc_add_expr_to_block (&block, tmp);
3043 : :
3044 : 495 : 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 : 2799 : gfc_trans_select_type_cases (gfc_code * code)
3054 : : {
3055 : 2799 : gfc_code *c;
3056 : 2799 : gfc_case *cp;
3057 : 2799 : tree tmp;
3058 : 2799 : tree cond;
3059 : 2799 : tree low;
3060 : 2799 : tree high;
3061 : 2799 : gfc_se se;
3062 : 2799 : gfc_se cse;
3063 : 2799 : stmtblock_t block;
3064 : 2799 : stmtblock_t body;
3065 : 2799 : bool def = false;
3066 : 2799 : gfc_expr *e;
3067 : 2799 : gfc_start_block (&block);
3068 : :
3069 : : /* Calculate the switch expression. */
3070 : 2799 : gfc_init_se (&se, NULL);
3071 : 2799 : gfc_conv_expr_val (&se, code->expr1);
3072 : 2799 : 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 : 2799 : e = gfc_copy_expr (code->expr1->value.function.actual->expr);
3077 : 2799 : gfc_add_hash_component (e);
3078 : :
3079 : 2799 : TREE_USED (code->exit_label) = 0;
3080 : :
3081 : 5598 : repeat:
3082 : 15504 : for (c = code->block; c; c = c->block)
3083 : : {
3084 : 9906 : cp = c->ext.block.case_list;
3085 : :
3086 : : /* Assume it's the default case. */
3087 : 9906 : low = NULL_TREE;
3088 : 9906 : high = NULL_TREE;
3089 : 9906 : tmp = NULL_TREE;
3090 : :
3091 : : /* Put the default case at the end. */
3092 : 9906 : if ((!def && !cp->low) || (def && cp->low))
3093 : 4953 : continue;
3094 : :
3095 : 4953 : if (cp->low && (cp->ts.type == BT_CLASS
3096 : 3229 : || cp->ts.type == BT_DERIVED))
3097 : : {
3098 : 1871 : gfc_init_se (&cse, NULL);
3099 : 1871 : gfc_conv_expr_val (&cse, cp->low);
3100 : 1871 : gfc_add_block_to_block (&block, &cse.pre);
3101 : 1871 : low = cse.expr;
3102 : : }
3103 : 3082 : else if (cp->ts.type != BT_UNKNOWN)
3104 : : {
3105 : 1358 : gcc_assert (cp->high);
3106 : 1358 : gfc_init_se (&cse, NULL);
3107 : 1358 : gfc_conv_expr_val (&cse, cp->high);
3108 : 1358 : gfc_add_block_to_block (&block, &cse.pre);
3109 : 1358 : high = cse.expr;
3110 : : }
3111 : :
3112 : 4953 : gfc_init_block (&body);
3113 : :
3114 : : /* Add the statements for this case. */
3115 : 4953 : tmp = gfc_trans_code (c->next);
3116 : 4953 : 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 : 4953 : if (!def)
3121 : : {
3122 : 3229 : TREE_USED (code->exit_label) = 1;
3123 : 3229 : tmp = build1_v (GOTO_EXPR, code->exit_label);
3124 : 3229 : gfc_add_expr_to_block (&body, tmp);
3125 : : }
3126 : :
3127 : 4953 : tmp = gfc_finish_block (&body);
3128 : :
3129 : 4953 : if (low != NULL_TREE)
3130 : : {
3131 : : /* Compare vtable pointers. */
3132 : 1871 : cond = fold_build2_loc (input_location, EQ_EXPR,
3133 : 1871 : TREE_TYPE (se.expr), se.expr, low);
3134 : 1871 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3135 : : cond, tmp,
3136 : : build_empty_stmt (input_location));
3137 : : }
3138 : 3082 : else if (high != NULL_TREE)
3139 : : {
3140 : : /* Compare hash values for character cases. */
3141 : 1358 : gfc_init_se (&cse, NULL);
3142 : 1358 : gfc_conv_expr_val (&cse, e);
3143 : 1358 : gfc_add_block_to_block (&block, &cse.pre);
3144 : :
3145 : 1358 : cond = fold_build2_loc (input_location, EQ_EXPR,
3146 : 1358 : TREE_TYPE (se.expr), high, cse.expr);
3147 : 1358 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3148 : : cond, tmp,
3149 : : build_empty_stmt (input_location));
3150 : : }
3151 : :
3152 : 4953 : gfc_add_expr_to_block (&block, tmp);
3153 : : }
3154 : :
3155 : 5598 : if (!def)
3156 : : {
3157 : 2799 : def = true;
3158 : 2799 : goto repeat;
3159 : : }
3160 : :
3161 : 2799 : gfc_free_expr (e);
3162 : :
3163 : 2799 : 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 : 872 : gfc_trans_integer_select (gfc_code * code)
3214 : : {
3215 : 872 : gfc_code *c;
3216 : 872 : gfc_case *cp;
3217 : 872 : tree end_label;
3218 : 872 : tree tmp;
3219 : 872 : gfc_se se;
3220 : 872 : stmtblock_t block;
3221 : 872 : stmtblock_t body;
3222 : :
3223 : 872 : gfc_start_block (&block);
3224 : :
3225 : : /* Calculate the switch expression. */
3226 : 872 : gfc_init_se (&se, NULL);
3227 : 872 : gfc_conv_expr_val (&se, code->expr1);
3228 : 872 : gfc_add_block_to_block (&block, &se.pre);
3229 : :
3230 : 872 : end_label = gfc_build_label_decl (NULL_TREE);
3231 : :
3232 : 872 : gfc_init_block (&body);
3233 : :
3234 : 2666 : for (c = code->block; c; c = c->block)
3235 : : {
3236 : 3660 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3237 : : {
3238 : 1866 : tree low, high;
3239 : 1866 : tree label;
3240 : :
3241 : : /* Assume it's the default case. */
3242 : 1866 : low = high = NULL_TREE;
3243 : :
3244 : 1866 : if (cp->low)
3245 : : {
3246 : 1478 : if (cp->low->ts.type == BT_INTEGER)
3247 : 1436 : low = gfc_conv_mpz_to_tree (cp->low->value.integer,
3248 : : cp->low->ts.kind);
3249 : : else
3250 : 42 : 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 : 1478 : if (!cp->high)
3256 : 45 : high = TYPE_MAX_VALUE (TREE_TYPE (se.expr));
3257 : : }
3258 : :
3259 : 1866 : 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 : 1477 : if (!cp->low
3278 : 1433 : || (mpz_cmp (cp->low->value.integer,
3279 : 1433 : cp->high->value.integer) != 0))
3280 : : {
3281 : 73 : if (cp->high->ts.type == BT_INTEGER)
3282 : 73 : high = gfc_conv_mpz_to_tree (cp->high->value.integer,
3283 : : cp->high->ts.kind);
3284 : : else
3285 : 0 : high
3286 : 0 : = gfc_conv_mpz_unsigned_to_tree (cp->high->value.integer,
3287 : : cp->high->ts.kind);
3288 : : }
3289 : :
3290 : : /* Unbounded case. */
3291 : 1477 : if (!cp->low)
3292 : 44 : low = TYPE_MIN_VALUE (TREE_TYPE (se.expr));
3293 : : }
3294 : :
3295 : : /* Build a label. */
3296 : 1866 : label = gfc_build_label_decl (NULL_TREE);
3297 : :
3298 : : /* Add this case label.
3299 : : Add parameter 'label', make it match GCC backend. */
3300 : 1866 : tmp = build_case_label (low, high, label);
3301 : 1866 : gfc_add_expr_to_block (&body, tmp);
3302 : : }
3303 : :
3304 : : /* Add the statements for this case. */
3305 : 1794 : tmp = gfc_trans_code (c->next);
3306 : 1794 : gfc_add_expr_to_block (&body, tmp);
3307 : :
3308 : : /* Break to the end of the construct. */
3309 : 1794 : tmp = build1_v (GOTO_EXPR, end_label);
3310 : 1794 : gfc_add_expr_to_block (&body, tmp);
3311 : : }
3312 : :
3313 : 872 : tmp = gfc_finish_block (&body);
3314 : 872 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, se.expr, tmp);
3315 : 872 : gfc_add_expr_to_block (&block, tmp);
3316 : :
3317 : 872 : tmp = build1_v (LABEL_EXPR, end_label);
3318 : 872 : gfc_add_expr_to_block (&block, tmp);
3319 : :
3320 : 872 : 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 : 54 : gfc_trans_logical_select (gfc_code * code)
3340 : : {
3341 : 54 : gfc_code *c;
3342 : 54 : gfc_code *t, *f, *d;
3343 : 54 : gfc_case *cp;
3344 : 54 : gfc_se se;
3345 : 54 : stmtblock_t block;
3346 : :
3347 : : /* Assume we don't have any cases at all. */
3348 : 54 : 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 : 171 : for (c = code->block; c; c = c->block)
3356 : : {
3357 : 243 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3358 : : {
3359 : 126 : if (cp->low)
3360 : : {
3361 : 72 : if (cp->low->value.logical == 0) /* .FALSE. */
3362 : : f = c;
3363 : : else /* if (cp->value.logical != 0), thus .TRUE. */
3364 : 36 : t = c;
3365 : : }
3366 : : else
3367 : : d = c;
3368 : : }
3369 : : }
3370 : :
3371 : : /* Start a new block. */
3372 : 54 : gfc_start_block (&block);
3373 : :
3374 : : /* Calculate the switch expression. We always need to do this
3375 : : because it may have side effects. */
3376 : 54 : gfc_init_se (&se, NULL);
3377 : 54 : gfc_conv_expr_val (&se, code->expr1);
3378 : 54 : gfc_add_block_to_block (&block, &se.pre);
3379 : :
3380 : 54 : 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 : 9 : gfc_add_expr_to_block (&block, gfc_trans_code (t->next));
3386 : : }
3387 : : else
3388 : : {
3389 : 45 : tree true_tree, false_tree, stmt;
3390 : :
3391 : 45 : true_tree = build_empty_stmt (input_location);
3392 : 45 : 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 : 45 : if (t != NULL && f != NULL)
3398 : 63 : d = NULL;
3399 : 36 : else if (d != NULL)
3400 : : {
3401 : 36 : 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 : 18 : if (t != NULL)
3410 : 45 : true_tree = gfc_trans_code (t->next);
3411 : :
3412 : 45 : if (f != NULL)
3413 : 45 : false_tree = gfc_trans_code (f->next);
3414 : :
3415 : 45 : stmt = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3416 : : se.expr, true_tree, false_tree);
3417 : 45 : gfc_add_expr_to_block (&block, stmt);
3418 : : }
3419 : :
3420 : 54 : 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 : 75 : gfc_trans_character_select (gfc_code *code)
3439 : : {
3440 : 75 : tree init, end_label, tmp, type, case_num, label, fndecl;
3441 : 75 : stmtblock_t block, body;
3442 : 75 : gfc_case *cp, *d;
3443 : 75 : gfc_code *c;
3444 : 75 : gfc_se se, expr1se;
3445 : 75 : int n, k;
3446 : 75 : vec<constructor_elt, va_gc> *inits = NULL;
3447 : :
3448 : 75 : 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 : 75 : static tree ss_string1[2], ss_string1_len[2];
3453 : 75 : static tree ss_string2[2], ss_string2_len[2];
3454 : 75 : static tree ss_target[2];
3455 : :
3456 : 75 : cp = code->block->ext.block.case_list;
3457 : 241 : while (cp->left != NULL)
3458 : : cp = cp->left;
3459 : :
3460 : : /* Generate the body */
3461 : 75 : gfc_start_block (&block);
3462 : 75 : gfc_init_se (&expr1se, NULL);
3463 : 75 : gfc_conv_expr_reference (&expr1se, code->expr1);
3464 : :
3465 : 75 : gfc_add_block_to_block (&block, &expr1se.pre);
3466 : :
3467 : 75 : end_label = gfc_build_label_decl (NULL_TREE);
3468 : :
3469 : 75 : gfc_init_block (&body);
3470 : :
3471 : : /* Attempt to optimize length 1 selects. */
3472 : 75 : if (integer_onep (expr1se.string_length))
3473 : : {
3474 : 126 : for (d = cp; d; d = d->right)
3475 : : {
3476 : 110 : gfc_charlen_t i;
3477 : 110 : if (d->low)
3478 : : {
3479 : 100 : gcc_assert (d->low->expr_type == EXPR_CONSTANT
3480 : : && d->low->ts.type == BT_CHARACTER);
3481 : 100 : if (d->low->value.character.length > 1)
3482 : : {
3483 : 2 : for (i = 1; i < d->low->value.character.length; i++)
3484 : 2 : if (d->low->value.character.string[i] != ' ')
3485 : : break;
3486 : 2 : if (i != d->low->value.character.length)
3487 : : {
3488 : 2 : if (optimize && d->high && i == 1)
3489 : : {
3490 : 2 : gcc_assert (d->high->expr_type == EXPR_CONSTANT
3491 : : && d->high->ts.type == BT_CHARACTER);
3492 : 2 : if (d->high->value.character.length > 1
3493 : 2 : && (d->low->value.character.string[0]
3494 : 2 : == d->high->value.character.string[0])
3495 : 2 : && d->high->value.character.string[1] != ' '
3496 : 4 : && ((d->low->value.character.string[1] < ' ')
3497 : : == (d->high->value.character.string[1]
3498 : 2 : < ' ')))
3499 : 2 : continue;
3500 : : }
3501 : : break;
3502 : : }
3503 : : }
3504 : : }
3505 : 108 : if (d->high)
3506 : : {
3507 : 98 : gcc_assert (d->high->expr_type == EXPR_CONSTANT
3508 : : && d->high->ts.type == BT_CHARACTER);
3509 : 98 : if (d->high->value.character.length > 1)
3510 : : {
3511 : 3 : for (i = 1; i < d->high->value.character.length; i++)
3512 : 2 : if (d->high->value.character.string[i] != ' ')
3513 : : break;
3514 : 1 : if (i != d->high->value.character.length)
3515 : : break;
3516 : : }
3517 : : }
3518 : : }
3519 : 16 : if (d == NULL)
3520 : : {
3521 : 16 : tree ctype = gfc_get_char_type (code->expr1->ts.kind);
3522 : :
3523 : 58 : for (c = code->block; c; c = c->block)
3524 : : {
3525 : 152 : for (cp = c->ext.block.case_list; cp; cp = cp->next)
3526 : : {
3527 : 110 : tree low, high;
3528 : 110 : tree label;
3529 : 110 : gfc_char_t r;
3530 : :
3531 : : /* Assume it's the default case. */
3532 : 110 : low = high = NULL_TREE;
3533 : :
3534 : 110 : if (cp->low)
3535 : : {
3536 : : /* CASE ('ab') or CASE ('ab':'az') will never match
3537 : : any length 1 character. */
3538 : 100 : if (cp->low->value.character.length > 1
3539 : 2 : && cp->low->value.character.string[1] != ' ')
3540 : 2 : continue;
3541 : :
3542 : 98 : if (cp->low->value.character.length > 0)
3543 : 97 : r = cp->low->value.character.string[0];
3544 : : else
3545 : : r = ' ';
3546 : 98 : 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 : 98 : if (!cp->high)
3551 : 0 : high = TYPE_MAX_VALUE (ctype);
3552 : : }
3553 : :
3554 : 108 : if (cp->high)
3555 : : {
3556 : 98 : if (!cp->low
3557 : 98 : || (cp->low->value.character.string[0]
3558 : 98 : != cp->high->value.character.string[0]))
3559 : : {
3560 : 2 : if (cp->high->value.character.length > 0)
3561 : 2 : r = cp->high->value.character.string[0];
3562 : : else
3563 : : r = ' ';
3564 : 2 : high = build_int_cst (ctype, r);
3565 : : }
3566 : :
3567 : : /* Unbounded case. */
3568 : 98 : if (!cp->low)
3569 : 0 : low = TYPE_MIN_VALUE (ctype);
3570 : : }
3571 : :
3572 : : /* Build a label. */
3573 : 108 : label = gfc_build_label_decl (NULL_TREE);
3574 : :
3575 : : /* Add this case label.
3576 : : Add parameter 'label', make it match GCC backend. */
3577 : 108 : tmp = build_case_label (low, high, label);
3578 : 108 : gfc_add_expr_to_block (&body, tmp);
3579 : : }
3580 : :
3581 : : /* Add the statements for this case. */
3582 : 42 : tmp = gfc_trans_code (c->next);
3583 : 42 : gfc_add_expr_to_block (&body, tmp);
3584 : :
3585 : : /* Break to the end of the construct. */
3586 : 42 : tmp = build1_v (GOTO_EXPR, end_label);
3587 : 42 : gfc_add_expr_to_block (&body, tmp);
3588 : : }
3589 : :
3590 : 32 : tmp = gfc_string_to_single_character (expr1se.string_length,
3591 : : expr1se.expr,
3592 : 16 : code->expr1->ts.kind);
3593 : 16 : case_num = gfc_create_var (ctype, "case_num");
3594 : 16 : gfc_add_modify (&block, case_num, tmp);
3595 : :
3596 : 16 : gfc_add_block_to_block (&block, &expr1se.post);
3597 : :
3598 : 16 : tmp = gfc_finish_block (&body);
3599 : 16 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3600 : : case_num, tmp);
3601 : 16 : gfc_add_expr_to_block (&block, tmp);
3602 : :
3603 : 16 : tmp = build1_v (LABEL_EXPR, end_label);
3604 : 16 : gfc_add_expr_to_block (&block, tmp);
3605 : :
3606 : 16 : return gfc_finish_block (&block);
3607 : : }
3608 : : }
3609 : :
3610 : 59 : if (code->expr1->ts.kind == 1)
3611 : : k = 0;
3612 : 6 : else if (code->expr1->ts.kind == 4)
3613 : : k = 1;
3614 : : else
3615 : 0 : gcc_unreachable ();
3616 : :
3617 : 59 : if (select_struct[k] == NULL)
3618 : : {
3619 : 53 : tree *chain = NULL;
3620 : 53 : select_struct[k] = make_node (RECORD_TYPE);
3621 : :
3622 : 53 : if (code->expr1->ts.kind == 1)
3623 : 47 : TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char1");
3624 : 6 : else if (code->expr1->ts.kind == 4)
3625 : 6 : TYPE_NAME (select_struct[k]) = get_identifier ("_jump_struct_char4");
3626 : : else
3627 : 0 : 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 : 53 : ADD_FIELD (string1, pchartype);
3637 : 53 : ADD_FIELD (string1_len, gfc_charlen_type_node);
3638 : :
3639 : 53 : ADD_FIELD (string2, pchartype);
3640 : 53 : ADD_FIELD (string2_len, gfc_charlen_type_node);
3641 : :
3642 : 53 : ADD_FIELD (target, integer_type_node);
3643 : : #undef ADD_FIELD
3644 : :
3645 : 53 : gfc_finish_type (select_struct[k]);
3646 : : }
3647 : :
3648 : : n = 0;
3649 : 311 : for (d = cp; d; d = d->right)
3650 : 252 : d->n = n++;
3651 : :
3652 : 263 : for (c = code->block; c; c = c->block)
3653 : : {
3654 : 456 : for (d = c->ext.block.case_list; d; d = d->next)
3655 : : {
3656 : 252 : label = gfc_build_label_decl (NULL_TREE);
3657 : 452 : tmp = build_case_label ((d->low == NULL && d->high == NULL)
3658 : : ? NULL
3659 : 200 : : build_int_cst (integer_type_node, d->n),
3660 : : NULL, label);
3661 : 252 : gfc_add_expr_to_block (&body, tmp);
3662 : : }
3663 : :
3664 : 204 : tmp = gfc_trans_code (c->next);
3665 : 204 : gfc_add_expr_to_block (&body, tmp);
3666 : :
3667 : 204 : tmp = build1_v (GOTO_EXPR, end_label);
3668 : 204 : gfc_add_expr_to_block (&body, tmp);
3669 : : }
3670 : :
3671 : : /* Generate the structure describing the branches */
3672 : 311 : for (d = cp; d; d = d->right)
3673 : : {
3674 : 252 : vec<constructor_elt, va_gc> *node = NULL;
3675 : :
3676 : 252 : gfc_init_se (&se, NULL);
3677 : :
3678 : 252 : if (d->low == NULL)
3679 : : {
3680 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], null_pointer_node);
3681 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], build_zero_cst (gfc_charlen_type_node));
3682 : : }
3683 : : else
3684 : : {
3685 : 200 : gfc_conv_expr_reference (&se, d->low);
3686 : :
3687 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string1[k], se.expr);
3688 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string1_len[k], se.string_length);
3689 : : }
3690 : :
3691 : 252 : if (d->high == NULL)
3692 : : {
3693 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], null_pointer_node);
3694 : 52 : CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], build_zero_cst (gfc_charlen_type_node));
3695 : : }
3696 : : else
3697 : : {
3698 : 200 : gfc_init_se (&se, NULL);
3699 : 200 : gfc_conv_expr_reference (&se, d->high);
3700 : :
3701 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string2[k], se.expr);
3702 : 200 : CONSTRUCTOR_APPEND_ELT (node, ss_string2_len[k], se.string_length);
3703 : : }
3704 : :
3705 : 252 : CONSTRUCTOR_APPEND_ELT (node, ss_target[k],
3706 : : build_int_cst (integer_type_node, d->n));
3707 : :
3708 : 252 : tmp = build_constructor (select_struct[k], node);
3709 : 252 : CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, tmp);
3710 : : }
3711 : :
3712 : 59 : type = build_array_type (select_struct[k],
3713 : 59 : build_index_type (size_int (n-1)));
3714 : :
3715 : 59 : init = build_constructor (type, inits);
3716 : 59 : TREE_CONSTANT (init) = 1;
3717 : 59 : TREE_STATIC (init) = 1;
3718 : : /* Create a static variable to hold the jump table. */
3719 : 59 : tmp = gfc_create_var (type, "jumptable");
3720 : 59 : TREE_CONSTANT (tmp) = 1;
3721 : 59 : TREE_STATIC (tmp) = 1;
3722 : 59 : TREE_READONLY (tmp) = 1;
3723 : 59 : DECL_INITIAL (tmp) = init;
3724 : 59 : init = tmp;
3725 : :
3726 : : /* Build the library call */
3727 : 59 : init = gfc_build_addr_expr (pvoid_type_node, init);
3728 : :
3729 : 59 : if (code->expr1->ts.kind == 1)
3730 : 53 : fndecl = gfor_fndecl_select_string;
3731 : 6 : else if (code->expr1->ts.kind == 4)
3732 : 6 : fndecl = gfor_fndecl_select_string_char4;
3733 : : else
3734 : 0 : gcc_unreachable ();
3735 : :
3736 : 59 : tmp = build_call_expr_loc (input_location,
3737 : : fndecl, 4, init,
3738 : 59 : build_int_cst (gfc_charlen_type_node, n),
3739 : : expr1se.expr, expr1se.string_length);
3740 : 59 : case_num = gfc_create_var (integer_type_node, "case_num");
3741 : 59 : gfc_add_modify (&block, case_num, tmp);
3742 : :
3743 : 59 : gfc_add_block_to_block (&block, &expr1se.post);
3744 : :
3745 : 59 : tmp = gfc_finish_block (&body);
3746 : 59 : tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE,
3747 : : case_num, tmp);
3748 : 59 : gfc_add_expr_to_block (&block, tmp);
3749 : :
3750 : 59 : tmp = build1_v (LABEL_EXPR, end_label);
3751 : 59 : gfc_add_expr_to_block (&block, tmp);
3752 : :
3753 : 59 : 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 : 1003 : gfc_trans_select (gfc_code * code)
3773 : : {
3774 : 1003 : stmtblock_t block;
3775 : 1003 : tree body;
3776 : 1003 : tree exit_label;
3777 : :
3778 : 1003 : gcc_assert (code && code->expr1);
3779 : 1003 : gfc_init_block (&block);
3780 : :
3781 : : /* Build the exit label and hang it in. */
3782 : 1003 : exit_label = gfc_build_label_decl (NULL_TREE);
3783 : 1003 : code->exit_label = exit_label;
3784 : :
3785 : : /* Empty SELECT constructs are legal. */
3786 : 1003 : if (code->block == NULL)
3787 : 2 : body = build_empty_stmt (input_location);
3788 : :
3789 : : /* Select the correct translation function. */
3790 : : else
3791 : 1001 : switch (code->expr1->ts.type)
3792 : : {
3793 : 54 : case BT_LOGICAL:
3794 : 54 : body = gfc_trans_logical_select (code);
3795 : 54 : break;
3796 : :
3797 : 872 : case BT_INTEGER:
3798 : 872 : case BT_UNSIGNED:
3799 : 872 : body = gfc_trans_integer_select (code);
3800 : 872 : break;
3801 : :
3802 : 75 : case BT_CHARACTER:
3803 : 75 : body = gfc_trans_character_select (code);
3804 : 75 : break;
3805 : :
3806 : 0 : default:
3807 : 0 : gfc_internal_error ("gfc_trans_select(): Bad type for case expr.");
3808 : : /* Not reached */
3809 : : }
3810 : :
3811 : : /* Build everything together. */
3812 : 1003 : gfc_add_expr_to_block (&block, body);
3813 : 1003 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3814 : :
3815 : 1003 : return gfc_finish_block (&block);
3816 : : }
3817 : :
3818 : : tree
3819 : 2799 : gfc_trans_select_type (gfc_code * code)
3820 : : {
3821 : 2799 : stmtblock_t block;
3822 : 2799 : tree body;
3823 : 2799 : tree exit_label;
3824 : :
3825 : 2799 : gcc_assert (code && code->expr1);
3826 : 2799 : gfc_init_block (&block);
3827 : :
3828 : : /* Build the exit label and hang it in. */
3829 : 2799 : exit_label = gfc_build_label_decl (NULL_TREE);
3830 : 2799 : code->exit_label = exit_label;
3831 : :
3832 : : /* Empty SELECT constructs are legal. */
3833 : 2799 : if (code->block == NULL)
3834 : 0 : body = build_empty_stmt (input_location);
3835 : : else
3836 : 2799 : body = gfc_trans_select_type_cases (code);
3837 : :
3838 : : /* Build everything together. */
3839 : 2799 : gfc_add_expr_to_block (&block, body);
3840 : :
3841 : 2799 : if (TREE_USED (exit_label))
3842 : 2612 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3843 : :
3844 : 2799 : return gfc_finish_block (&block);
3845 : : }
3846 : :
3847 : :
3848 : : static tree
3849 : 1000 : gfc_trans_select_rank_cases (gfc_code * code)
3850 : : {
3851 : 1000 : gfc_code *c;
3852 : 1000 : gfc_case *cp;
3853 : 1000 : tree tmp;
3854 : 1000 : tree cond;
3855 : 1000 : tree low;
3856 : 1000 : tree rank;
3857 : 1000 : gfc_se se;
3858 : 1000 : gfc_se cse;
3859 : 1000 : stmtblock_t block;
3860 : 1000 : stmtblock_t body;
3861 : 1000 : bool def = false;
3862 : :
3863 : 1000 : gfc_start_block (&block);
3864 : :
3865 : : /* Calculate the switch expression. */
3866 : 1000 : gfc_init_se (&se, NULL);
3867 : 1000 : gfc_conv_expr_descriptor (&se, code->expr1);
3868 : 1000 : rank = gfc_conv_descriptor_rank (se.expr);
3869 : 1000 : rank = gfc_evaluate_now (rank, &block);
3870 : 1000 : symbol_attribute attr = gfc_expr_attr (code->expr1);
3871 : 1000 : 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 : 766 : cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
3876 : 766 : rank, build_int_cst (TREE_TYPE (rank), 0));
3877 : 766 : 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 : 766 : tmp = gfc_conv_descriptor_ubound_get (se.expr, tmp);
3881 : 766 : tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
3882 : 766 : tmp, build_int_cst (TREE_TYPE (tmp), -1));
3883 : 766 : cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3884 : : logical_type_node, cond, tmp);
3885 : 766 : tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (rank),
3886 : 766 : cond, rank, build_int_cst (TREE_TYPE (rank), -1));
3887 : 766 : rank = gfc_evaluate_now (tmp, &block);
3888 : : }
3889 : 1000 : TREE_USED (code->exit_label) = 0;
3890 : :
3891 : 2000 : repeat:
3892 : 6554 : for (c = code->block; c; c = c->block)
3893 : : {
3894 : 4554 : cp = c->ext.block.case_list;
3895 : :
3896 : : /* Assume it's the default case. */
3897 : 4554 : low = NULL_TREE;
3898 : 4554 : tmp = NULL_TREE;
3899 : :
3900 : : /* Put the default case at the end. */
3901 : 4554 : if ((!def && !cp->low) || (def && cp->low))
3902 : 2277 : continue;
3903 : :
3904 : 2277 : if (cp->low)
3905 : : {
3906 : 1362 : gfc_init_se (&cse, NULL);
3907 : 1362 : gfc_conv_expr_val (&cse, cp->low);
3908 : 1362 : gfc_add_block_to_block (&block, &cse.pre);
3909 : 1362 : low = cse.expr;
3910 : : }
3911 : :
3912 : 2277 : gfc_init_block (&body);
3913 : :
3914 : : /* Add the statements for this case. */
3915 : 2277 : tmp = gfc_trans_code (c->next);
3916 : 2277 : 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 : 2277 : if (!def)
3921 : : {
3922 : 1362 : TREE_USED (code->exit_label) = 1;
3923 : 1362 : tmp = build1_v (GOTO_EXPR, code->exit_label);
3924 : 1362 : gfc_add_expr_to_block (&body, tmp);
3925 : : }
3926 : :
3927 : 2277 : tmp = gfc_finish_block (&body);
3928 : :
3929 : 2277 : if (low != NULL_TREE)
3930 : : {
3931 : 2724 : cond = fold_build2_loc (input_location, EQ_EXPR,
3932 : 1362 : TREE_TYPE (rank), rank,
3933 : 1362 : fold_convert (TREE_TYPE (rank), low));
3934 : 1362 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
3935 : : cond, tmp,
3936 : : build_empty_stmt (input_location));
3937 : : }
3938 : :
3939 : 2277 : gfc_add_expr_to_block (&block, tmp);
3940 : : }
3941 : :
3942 : 2000 : if (!def)
3943 : : {
3944 : 1000 : def = true;
3945 : 1000 : goto repeat;
3946 : : }
3947 : :
3948 : 1000 : return gfc_finish_block (&block);
3949 : : }
3950 : :
3951 : :
3952 : : tree
3953 : 1001 : gfc_trans_select_rank (gfc_code * code)
3954 : : {
3955 : 1001 : stmtblock_t block;
3956 : 1001 : tree body;
3957 : 1001 : tree exit_label;
3958 : :
3959 : 1001 : gcc_assert (code && code->expr1);
3960 : 1001 : gfc_init_block (&block);
3961 : :
3962 : : /* Build the exit label and hang it in. */
3963 : 1001 : exit_label = gfc_build_label_decl (NULL_TREE);
3964 : 1001 : code->exit_label = exit_label;
3965 : :
3966 : : /* Empty SELECT constructs are legal. */
3967 : 1001 : if (code->block == NULL)
3968 : 1 : body = build_empty_stmt (input_location);
3969 : : else
3970 : 1000 : body = gfc_trans_select_rank_cases (code);
3971 : :
3972 : : /* Build everything together. */
3973 : 1001 : gfc_add_expr_to_block (&block, body);
3974 : :
3975 : 1001 : if (TREE_USED (exit_label))
3976 : 1001 : gfc_add_expr_to_block (&block, build1_v (LABEL_EXPR, exit_label));
3977 : :
3978 : 1001 : 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 : 710 : forall_replace (gfc_expr *expr, gfc_symbol *sym, int *f)
3996 : : {
3997 : 710 : if (expr->expr_type != EXPR_VARIABLE)
3998 : : return false;
3999 : :
4000 : 406 : if (*f == 2)
4001 : 62 : *f = 1;
4002 : 344 : else if (expr->symtree->n.sym == sym)
4003 : 72 : expr->symtree = new_symtree;
4004 : :
4005 : : return false;
4006 : : }
4007 : :
4008 : : static void
4009 : 124 : forall_replace_symtree (gfc_expr *e, gfc_symbol *sym, int f)
4010 : : {
4011 : 0 : gfc_traverse_expr (e, sym, forall_replace, f);
4012 : 0 : }
4013 : :
4014 : : static bool
4015 : 710 : forall_restore (gfc_expr *expr,
4016 : : gfc_symbol *sym ATTRIBUTE_UNUSED,
4017 : : int *f ATTRIBUTE_UNUSED)
4018 : : {
4019 : 710 : if (expr->expr_type != EXPR_VARIABLE)
4020 : : return false;
4021 : :
4022 : 406 : if (expr->symtree == new_symtree)
4023 : 72 : expr->symtree = old_symtree;
4024 : :
4025 : : return false;
4026 : : }
4027 : :
4028 : : static void
4029 : 124 : forall_restore_symtree (gfc_expr *e)
4030 : : {
4031 : 0 : gfc_traverse_expr (e, NULL, forall_restore, 0);
4032 : 0 : }
4033 : :
4034 : : static void
4035 : 62 : forall_make_variable_temp (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4036 : : {
4037 : 62 : gfc_se tse;
4038 : 62 : gfc_se rse;
4039 : 62 : gfc_expr *e;
4040 : 62 : gfc_symbol *new_sym;
4041 : 62 : gfc_symbol *old_sym;
4042 : 62 : gfc_symtree *root;
4043 : 62 : tree tmp;
4044 : :
4045 : : /* Build a copy of the lvalue. */
4046 : 62 : old_symtree = c->expr1->symtree;
4047 : 62 : old_sym = old_symtree->n.sym;
4048 : 62 : e = gfc_lval_expr_from_sym (old_sym);
4049 : 62 : if (old_sym->attr.dimension)
4050 : : {
4051 : 30 : gfc_init_se (&tse, NULL);
4052 : 30 : gfc_conv_subref_array_arg (&tse, e, 0, INTENT_IN, false);
4053 : 30 : gfc_add_block_to_block (pre, &tse.pre);
4054 : 30 : gfc_add_block_to_block (post, &tse.post);
4055 : 30 : tse.expr = build_fold_indirect_ref_loc (input_location, tse.expr);
4056 : :
4057 : 30 : if (c->expr1->ref->u.ar.type != AR_SECTION)
4058 : : {
4059 : : /* Use the variable offset for the temporary. */
4060 : 24 : tmp = gfc_conv_array_offset (old_sym->backend_decl);
4061 : 24 : gfc_conv_descriptor_offset_set (pre, tse.expr, tmp);
4062 : : }
4063 : : }
4064 : : else
4065 : : {
4066 : 32 : gfc_init_se (&tse, NULL);
4067 : 32 : gfc_init_se (&rse, NULL);
4068 : 32 : gfc_conv_expr (&rse, e);
4069 : 32 : if (e->ts.type == BT_CHARACTER)
4070 : : {
4071 : 32 : tse.string_length = rse.string_length;
4072 : 32 : tmp = gfc_get_character_type_len (gfc_default_character_kind,
4073 : : tse.string_length);
4074 : 32 : tse.expr = gfc_conv_string_tmp (&tse, build_pointer_type (tmp),
4075 : : rse.string_length);
4076 : 32 : gfc_add_block_to_block (pre, &tse.pre);
4077 : 32 : gfc_add_block_to_block (post, &tse.post);
4078 : : }
4079 : : else
4080 : : {
4081 : 0 : tmp = gfc_typenode_for_spec (&e->ts);
4082 : 0 : tse.expr = gfc_create_var (tmp, "temp");
4083 : : }
4084 : :
4085 : 64 : tmp = gfc_trans_scalar_assign (&tse, &rse, e->ts,
4086 : 32 : e->expr_type == EXPR_VARIABLE, false);
4087 : 32 : gfc_add_expr_to_block (pre, tmp);
4088 : : }
4089 : 62 : gfc_free_expr (e);
4090 : :
4091 : : /* Create a new symbol to represent the lvalue. */
4092 : 62 : new_sym = gfc_new_symbol (old_sym->name, NULL);
4093 : 62 : new_sym->ts = old_sym->ts;
4094 : 62 : new_sym->attr.referenced = 1;
4095 : 62 : new_sym->attr.temporary = 1;
4096 : 62 : new_sym->attr.dimension = old_sym->attr.dimension;
4097 : 62 : new_sym->attr.flavor = old_sym->attr.flavor;
4098 : :
4099 : : /* Use the temporary as the backend_decl. */
4100 : 62 : new_sym->backend_decl = tse.expr;
4101 : :
4102 : : /* Create a fake symtree for it. */
4103 : 62 : root = NULL;
4104 : 62 : new_symtree = gfc_new_symtree (&root, old_sym->name);
4105 : 62 : new_symtree->n.sym = new_sym;
4106 : 62 : gcc_assert (new_symtree == root);
4107 : :
4108 : : /* Go through the expression reference replacing the old_symtree
4109 : : with the new. */
4110 : 62 : 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 : 62 : forall_replace_symtree (c->expr2, old_sym, 1);
4115 : 62 : }
4116 : :
4117 : :
4118 : : /* Handles dependencies in forall assignments. */
4119 : : static int
4120 : 1825 : check_forall_dependencies (gfc_code *c, stmtblock_t *pre, stmtblock_t *post)
4121 : : {
4122 : 1825 : gfc_ref *lref;
4123 : 1825 : gfc_ref *rref;
4124 : 1825 : int need_temp;
4125 : 1825 : gfc_symbol *lsym;
4126 : :
4127 : 1825 : lsym = c->expr1->symtree->n.sym;
4128 : 1825 : 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 : 1825 : if (lsym->ts.type == BT_DERIVED
4138 : 149 : && (lsym->ts.u.derived->attr.pointer_comp
4139 : 149 : || lsym->ts.u.derived->attr.alloc_comp))
4140 : : return need_temp;
4141 : :
4142 : 1730 : new_symtree = NULL;
4143 : 1730 : if (find_forall_index (c->expr1, lsym, 2))
4144 : : {
4145 : 12 : forall_make_variable_temp (c, pre, post);
4146 : 12 : need_temp = 0;
4147 : : }
4148 : :
4149 : : /* Substrings with dependencies are treated in the same
4150 : : way. */
4151 : 1730 : if (c->expr1->ts.type == BT_CHARACTER
4152 : 685 : && c->expr1->ref
4153 : 685 : && c->expr2->expr_type == EXPR_VARIABLE
4154 : 492 : && lsym == c->expr2->symtree->n.sym)
4155 : : {
4156 : 124 : for (lref = c->expr1->ref; lref; lref = lref->next)
4157 : 117 : if (lref->type == REF_SUBSTRING)
4158 : : break;
4159 : 124 : for (rref = c->expr2->ref; rref; rref = rref->next)
4160 : 117 : if (rref->type == REF_SUBSTRING)
4161 : : break;
4162 : :
4163 : 81 : if (rref && lref
4164 : 81 : && gfc_dep_compare_expr (rref->u.ss.start, lref->u.ss.start) < 0)
4165 : : {
4166 : 50 : forall_make_variable_temp (c, pre, post);
4167 : 50 : need_temp = 0;
4168 : : }
4169 : : }
4170 : : return need_temp;
4171 : : }
4172 : :
4173 : :
4174 : : static void
4175 : 62 : cleanup_forall_symtrees (gfc_code *c)
4176 : : {
4177 : 62 : forall_restore_symtree (c->expr1);
4178 : 62 : forall_restore_symtree (c->expr2);
4179 : 62 : free (new_symtree->n.sym);
4180 : 62 : free (new_symtree);
4181 : 62 : }
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 : 3495 : gfc_trans_forall_loop (forall_info *forall_tmp, tree body,
4205 : : int mask_flag, stmtblock_t *outer)
4206 : : {
4207 : 3495 : int n, nvar;
4208 : 3495 : tree tmp;
4209 : 3495 : tree cond;
4210 : 3495 : stmtblock_t block;
4211 : 3495 : tree exit_label;
4212 : 3495 : tree count;
4213 : 3495 : tree var, start, end, step;
4214 : 3495 : iter_info *iter;
4215 : :
4216 : : /* Initialize the mask index outside the FORALL nest. */
4217 : 3495 : if (mask_flag && forall_tmp->mask)
4218 : 1067 : gfc_add_modify (outer, forall_tmp->maskindex, gfc_index_zero_node);
4219 : :
4220 : 3495 : iter = forall_tmp->this_loop;
4221 : 3495 : nvar = forall_tmp->nvar;
4222 : 9659 : for (n = 0; n < nvar; n++)
4223 : : {
4224 : 6164 : var = iter->var;
4225 : 6164 : start = iter->start;
4226 : 6164 : end = iter->end;
4227 : 6164 : step = iter->step;
4228 : :
4229 : 6164 : exit_label = gfc_build_label_decl (NULL_TREE);
4230 : 6164 : TREE_USED (exit_label) = 1;
4231 : :
4232 : : /* The loop counter. */
4233 : 6164 : count = gfc_create_var (TREE_TYPE (var), "count");
4234 : :
4235 : : /* The body of the loop. */
4236 : 6164 : gfc_init_block (&block);
4237 : :
4238 : : /* The exit condition. */
4239 : 6164 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node,
4240 : 6164 : 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 : 6164 : if (forall_tmp->do_concurrent || iter->annot.ivdep)
4245 : 127 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4246 : 127 : build_int_cst (integer_type_node,
4247 : 127 : annot_expr_ivdep_kind),
4248 : : integer_zero_node);
4249 : :
4250 : 6164 : if (iter->annot.unroll && cond != error_mark_node)
4251 : 1 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4252 : 1 : build_int_cst (integer_type_node,
4253 : 1 : annot_expr_unroll_kind),
4254 : 1 : build_int_cst (integer_type_node, iter->annot.unroll));
4255 : :
4256 : 6164 : if (iter->annot.vector && cond != error_mark_node)
4257 : 1 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4258 : 1 : build_int_cst (integer_type_node,
4259 : 1 : annot_expr_vector_kind),
4260 : : integer_zero_node);
4261 : :
4262 : 6164 : if (iter->annot.novector && cond != error_mark_node)
4263 : 2 : cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
4264 : 2 : build_int_cst (integer_type_node,
4265 : 2 : annot_expr_no_vector_kind),
4266 : : integer_zero_node);
4267 : :
4268 : 6164 : tmp = build1_v (GOTO_EXPR, exit_label);
4269 : 6164 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4270 : : cond, tmp, build_empty_stmt (input_location));
4271 : 6164 : gfc_add_expr_to_block (&block, tmp);
4272 : :
4273 : : /* The main loop body. */
4274 : 6164 : gfc_add_expr_to_block (&block, body);
4275 : :
4276 : : /* Increment the loop variable. */
4277 : 6164 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var,
4278 : : step);
4279 : 6164 : gfc_add_modify (&block, var, tmp);
4280 : :
4281 : : /* Advance to the next mask element. Only do this for the
4282 : : innermost loop. */
4283 : 6164 : if (n == 0 && mask_flag && forall_tmp->mask)
4284 : : {
4285 : 1067 : tree maskindex = forall_tmp->maskindex;
4286 : 1067 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4287 : : maskindex, gfc_index_one_node);
4288 : 1067 : gfc_add_modify (&block, maskindex, tmp);
4289 : : }
4290 : :
4291 : : /* Decrement the loop counter. */
4292 : 6164 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), count,
4293 : 6164 : build_int_cst (TREE_TYPE (var), 1));
4294 : 6164 : gfc_add_modify (&block, count, tmp);
4295 : :
4296 : 6164 : body = gfc_finish_block (&block);
4297 : :
4298 : : /* Loop var initialization. */
4299 : 6164 : gfc_init_block (&block);
4300 : 6164 : gfc_add_modify (&block, var, start);
4301 : :
4302 : :
4303 : : /* Initialize the loop counter. */
4304 : 6164 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (var), step,
4305 : : start);
4306 : 6164 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), end,
4307 : : tmp);
4308 : 6164 : tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (var),
4309 : : tmp, step);
4310 : 6164 : gfc_add_modify (&block, count, tmp);
4311 : :
4312 : : /* The loop expression. */
4313 : 6164 : tmp = build1_v (LOOP_EXPR, body);
4314 : 6164 : gfc_add_expr_to_block (&block, tmp);
4315 : :
4316 : : /* The exit label. */
4317 : 6164 : tmp = build1_v (LABEL_EXPR, exit_label);
4318 : 6164 : gfc_add_expr_to_block (&block, tmp);
4319 : :
4320 : 6164 : body = gfc_finish_block (&block);
4321 : 6164 : iter = iter->next;
4322 : : }
4323 : 3495 : 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 : 3295 : gfc_trans_nested_forall_loop (forall_info * nested_forall_info, tree body,
4334 : : int mask_flag)
4335 : : {
4336 : 3295 : tree tmp;
4337 : 3295 : stmtblock_t header;
4338 : 3295 : forall_info *forall_tmp;
4339 : 3295 : tree mask, maskindex;
4340 : :
4341 : 3295 : gfc_start_block (&header);
4342 : :
4343 : 3295 : forall_tmp = nested_forall_info;
4344 : 10085 : while (forall_tmp != NULL)
4345 : : {
4346 : : /* Generate body with masks' control. */
4347 : 3495 : if (mask_flag)
4348 : : {
4349 : 2772 : mask = forall_tmp->mask;
4350 : 2772 : maskindex = forall_tmp->maskindex;
4351 : :
4352 : : /* If a mask was specified make the assignment conditional. */
4353 : 2772 : if (mask)
4354 : : {
4355 : 1067 : tmp = gfc_build_array_ref (mask, maskindex, NULL);
4356 : 1067 : body = build3_v (COND_EXPR, tmp, body,
4357 : : build_empty_stmt (input_location));
4358 : : }
4359 : : }
4360 : 3495 : body = gfc_trans_forall_loop (forall_tmp, body, mask_flag, &header);
4361 : 3495 : forall_tmp = forall_tmp->prev_nest;
4362 : 3495 : mask_flag = 1;
4363 : : }
4364 : :
4365 : 3295 : gfc_add_expr_to_block (&header, body);
4366 : 3295 : 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 : 1381 : gfc_do_allocate (tree bytesize, tree size, tree * pdata, stmtblock_t * pblock,
4375 : : tree elem_type)
4376 : : {
4377 : 1381 : tree tmpvar;
4378 : 1381 : tree type;
4379 : 1381 : tree tmp;
4380 : :
4381 : 1381 : if (INTEGER_CST_P (size))
4382 : 1139 : 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 : 1381 : type = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
4388 : 1381 : type = build_array_type (elem_type, type);
4389 : 1381 : if (gfc_can_put_var_on_stack (bytesize) && INTEGER_CST_P (size))
4390 : : {
4391 : 1139 : tmpvar = gfc_create_var (type, "temp");
4392 : 1139 : *pdata = NULL_TREE;
4393 : : }
4394 : : else
4395 : : {
4396 : 242 : tmpvar = gfc_create_var (build_pointer_type (type), "temp");
4397 : 242 : *pdata = convert (pvoid_type_node, tmpvar);
4398 : :
4399 : 242 : tmp = gfc_call_malloc (pblock, TREE_TYPE (tmpvar), bytesize);
4400 : 242 : gfc_add_modify (pblock, tmpvar, tmp);
4401 : : }
4402 : 1381 : return tmpvar;
4403 : : }
4404 : :
4405 : :
4406 : : /* Generate codes to copy the temporary to the actual lhs. */
4407 : :
4408 : : static tree
4409 : 224 : 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 : 224 : stmtblock_t block, body1;
4415 : 224 : gfc_loopinfo loop;
4416 : 224 : gfc_se lse;
4417 : 224 : gfc_se rse;
4418 : 224 : tree tmp;
4419 : 224 : tree wheremaskexpr;
4420 : :
4421 : 224 : (void) rss; /* TODO: unused. */
4422 : :
4423 : 224 : gfc_start_block (&block);
4424 : :
4425 : 224 : gfc_init_se (&rse, NULL);
4426 : 224 : gfc_init_se (&lse, NULL);
4427 : :
4428 : 224 : if (lss == gfc_ss_terminator)
4429 : : {
4430 : 149 : gfc_init_block (&body1);
4431 : 149 : gfc_conv_expr (&lse, expr);
4432 : 149 : rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4433 : : }
4434 : : else
4435 : : {
4436 : : /* Initialize the loop. */
4437 : 75 : gfc_init_loopinfo (&loop);
4438 : :
4439 : : /* We may need LSS to determine the shape of the expression. */
4440 : 75 : gfc_add_ss_to_loop (&loop, lss);
4441 : :
4442 : 75 : gfc_conv_ss_startstride (&loop);
4443 : 75 : gfc_conv_loop_setup (&loop, &expr->where);
4444 : :
4445 : 75 : gfc_mark_ss_chain_used (lss, 1);
4446 : : /* Start the loop body. */
4447 : 75 : gfc_start_scalarized_body (&loop, &body1);
4448 : :
4449 : : /* Translate the expression. */
4450 : 75 : gfc_copy_loopinfo_to_se (&lse, &loop);
4451 : 75 : lse.ss = lss;
4452 : 75 : gfc_conv_expr (&lse, expr);
4453 : :
4454 : : /* Form the expression of the temporary. */
4455 : 75 : rse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4456 : : }
4457 : :
4458 : : /* Use the scalar assignment. */
4459 : 224 : rse.string_length = lse.string_length;
4460 : 448 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts,
4461 : 224 : expr->expr_type == EXPR_VARIABLE, false);
4462 : :
4463 : : /* Form the mask expression according to the mask tree list. */
4464 : 224 : if (wheremask)
4465 : : {
4466 : 27 : wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4467 : 27 : if (invert)
4468 : 0 : wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4469 : 0 : TREE_TYPE (wheremaskexpr),
4470 : : wheremaskexpr);
4471 : 27 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4472 : : wheremaskexpr, tmp,
4473 : : build_empty_stmt (input_location));
4474 : : }
4475 : :
4476 : 224 : gfc_add_expr_to_block (&body1, tmp);
4477 : :
4478 : 224 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4479 : : count1, gfc_index_one_node);
4480 : 224 : gfc_add_modify (&body1, count1, tmp);
4481 : :
4482 : 224 : if (lss == gfc_ss_terminator)
4483 : 149 : gfc_add_block_to_block (&block, &body1);
4484 : : else
4485 : : {
4486 : : /* Increment count3. */
4487 : 75 : if (count3)
4488 : : {
4489 : 27 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4490 : : gfc_array_index_type,
4491 : : count3, gfc_index_one_node);
4492 : 27 : gfc_add_modify (&body1, count3, tmp);
4493 : : }
4494 : :
4495 : : /* Generate the copying loops. */
4496 : 75 : gfc_trans_scalarizing_loops (&loop, &body1);
4497 : :
4498 : 75 : gfc_add_block_to_block (&block, &loop.pre);
4499 : 75 : gfc_add_block_to_block (&block, &loop.post);
4500 : :
4501 : 75 : 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 : 224 : tmp = gfc_finish_block (&block);
4507 : 224 : 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 : 224 : 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 : 224 : stmtblock_t block, body1;
4522 : 224 : gfc_loopinfo loop;
4523 : 224 : gfc_se lse;
4524 : 224 : gfc_se rse;
4525 : 224 : tree tmp;
4526 : 224 : tree wheremaskexpr;
4527 : :
4528 : 224 : gfc_start_block (&block);
4529 : :
4530 : 224 : gfc_init_se (&rse, NULL);
4531 : 224 : gfc_init_se (&lse, NULL);
4532 : :
4533 : 224 : if (lss == gfc_ss_terminator)
4534 : : {
4535 : 149 : gfc_init_block (&body1);
4536 : 149 : gfc_conv_expr (&rse, expr2);
4537 : 149 : lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4538 : : }
4539 : : else
4540 : : {
4541 : : /* Initialize the loop. */
4542 : 75 : gfc_init_loopinfo (&loop);
4543 : :
4544 : : /* We may need LSS to determine the shape of the expression. */
4545 : 75 : gfc_add_ss_to_loop (&loop, lss);
4546 : 75 : gfc_add_ss_to_loop (&loop, rss);
4547 : :
4548 : 75 : gfc_conv_ss_startstride (&loop);
4549 : 75 : gfc_conv_loop_setup (&loop, &expr2->where);
4550 : :
4551 : 75 : gfc_mark_ss_chain_used (rss, 1);
4552 : : /* Start the loop body. */
4553 : 75 : gfc_start_scalarized_body (&loop, &body1);
4554 : :
4555 : : /* Translate the expression. */
4556 : 75 : gfc_copy_loopinfo_to_se (&rse, &loop);
4557 : 75 : rse.ss = rss;
4558 : 75 : gfc_conv_expr (&rse, expr2);
4559 : :
4560 : : /* Form the expression of the temporary. */
4561 : 75 : lse.expr = gfc_build_array_ref (tmp1, count1, NULL);
4562 : : }
4563 : :
4564 : : /* Use the scalar assignment. */
4565 : 224 : lse.string_length = rse.string_length;
4566 : 448 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr2->ts,
4567 : 224 : expr2->expr_type == EXPR_VARIABLE, false);
4568 : :
4569 : : /* Form the mask expression according to the mask tree list. */
4570 : 224 : if (wheremask)
4571 : : {
4572 : 27 : wheremaskexpr = gfc_build_array_ref (wheremask, count3, NULL);
4573 : 27 : if (invert)
4574 : 0 : wheremaskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
4575 : 0 : TREE_TYPE (wheremaskexpr),
4576 : : wheremaskexpr);
4577 : 27 : tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
4578 : : wheremaskexpr, tmp,
4579 : : build_empty_stmt (input_location));
4580 : : }
4581 : :
4582 : 224 : gfc_add_expr_to_block (&body1, tmp);
4583 : :
4584 : 224 : if (lss == gfc_ss_terminator)
4585 : : {
4586 : 149 : gfc_add_block_to_block (&block, &body1);
4587 : :
4588 : : /* Increment count1. */
4589 : 149 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (count1),
4590 : : count1, gfc_index_one_node);
4591 : 149 : gfc_add_modify (&block, count1, tmp);
4592 : : }
4593 : : else
4594 : : {
4595 : : /* Increment count1. */
4596 : 75 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4597 : : count1, gfc_index_one_node);
4598 : 75 : gfc_add_modify (&body1, count1, tmp);
4599 : :
4600 : : /* Increment count3. */
4601 : 75 : if (count3)
4602 : : {
4603 : 27 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4604 : : gfc_array_index_type,
4605 : : count3, gfc_index_one_node);
4606 : 27 : gfc_add_modify (&body1, count3, tmp);
4607 : : }
4608 : :
4609 : : /* Generate the copying loops. */
4610 : 75 : gfc_trans_scalarizing_loops (&loop, &body1);
4611 : :
4612 : 75 : gfc_add_block_to_block (&block, &loop.pre);
4613 : 75 : gfc_add_block_to_block (&block, &loop.post);
4614 : :
4615 : 75 : 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 : 224 : tmp = gfc_finish_block (&block);
4621 : 224 : 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 : 779 : compute_inner_temp_size (gfc_expr *expr1, gfc_expr *expr2,
4630 : : stmtblock_t * pblock,
4631 : : gfc_ss **lss, gfc_ss **rss)
4632 : : {
4633 : 779 : gfc_loopinfo loop;
4634 : 779 : tree size;
4635 : 779 : int i;
4636 : 779 : int save_flag;
4637 : 779 : tree tmp;
4638 : :
4639 : 779 : *lss = gfc_walk_expr (expr1);
4640 : 779 : *rss = NULL;
4641 : :
4642 : 779 : size = gfc_index_one_node;
4643 : 779 : if (*lss != gfc_ss_terminator)
4644 : : {
4645 : 481 : gfc_init_loopinfo (&loop);
4646 : :
4647 : : /* Walk the RHS of the expression. */
4648 : 481 : *rss = gfc_walk_expr (expr2);
4649 : 481 : if (*rss == gfc_ss_terminator)
4650 : : /* The rhs is scalar. Add a ss for the expression. */
4651 : 0 : *rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
4652 : :
4653 : : /* Associate the SS with the loop. */
4654 : 481 : 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 : 481 : 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 : 481 : loop.array_parameter = 1;
4662 : :
4663 : : /* Calculate the bounds of the scalarization. */
4664 : 481 : save_flag = gfc_option.rtcheck;
4665 : 481 : gfc_option.rtcheck &= ~GFC_RTCHECK_BOUNDS;
4666 : 481 : gfc_conv_ss_startstride (&loop);
4667 : 481 : gfc_option.rtcheck = save_flag;
4668 : 481 : gfc_conv_loop_setup (&loop, &expr2->where);
4669 : :
4670 : : /* Figure out how many elements we need. */
4671 : 1473 : for (i = 0; i < loop.dimen; i++)
4672 : : {
4673 : 511 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
4674 : : gfc_array_index_type,
4675 : : gfc_index_one_node, loop.from[i]);
4676 : 511 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4677 : : gfc_array_index_type, tmp, loop.to[i]);
4678 : 511 : size = fold_build2_loc (input_location, MULT_EXPR,
4679 : : gfc_array_index_type, size, tmp);
4680 : : }
4681 : 481 : gfc_add_block_to_block (pblock, &loop.pre);
4682 : 481 : size = gfc_evaluate_now (size, pblock);
4683 : 481 : 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 : 779 : 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 : 1298 : compute_overall_iter_number (forall_info *nested_forall_info, tree inner_size,
4703 : : stmtblock_t *inner_size_body, stmtblock_t *block)
4704 : : {
4705 : 1298 : forall_info *forall_tmp = nested_forall_info;
4706 : 1298 : tree tmp, number;
4707 : 1298 : stmtblock_t body;
4708 : :
4709 : : /* We can eliminate the innermost unconditional loops with constant
4710 : : array bounds. */
4711 : 1298 : if (INTEGER_CST_P (inner_size))
4712 : : {
4713 : : while (forall_tmp
4714 : 267 : && !forall_tmp->mask
4715 : 1506 : && INTEGER_CST_P (forall_tmp->size))
4716 : : {
4717 : 117 : inner_size = fold_build2_loc (input_location, MULT_EXPR,
4718 : : gfc_array_index_type,
4719 : : inner_size, forall_tmp->size);
4720 : 117 : forall_tmp = forall_tmp->prev_nest;
4721 : : }
4722 : :
4723 : : /* If there are no loops left, we have our constant result. */
4724 : 1207 : if (!forall_tmp)
4725 : : return inner_size;
4726 : : }
4727 : :
4728 : : /* Otherwise, create a temporary variable to compute the result. */
4729 : 241 : number = gfc_create_var (gfc_array_index_type, "num");
4730 : 241 : gfc_add_modify (block, number, gfc_index_zero_node);
4731 : :
4732 : 241 : gfc_start_block (&body);
4733 : 241 : if (inner_size_body)
4734 : 188 : gfc_add_block_to_block (&body, inner_size_body);
4735 : 241 : if (forall_tmp)
4736 : 225 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
4737 : : gfc_array_index_type, number, inner_size);
4738 : : else
4739 : : tmp = inner_size;
4740 : 241 : gfc_add_modify (&body, number, tmp);
4741 : 241 : tmp = gfc_finish_block (&body);
4742 : :
4743 : : /* Generate loops. */
4744 : 241 : if (forall_tmp != NULL)
4745 : 225 : tmp = gfc_trans_nested_forall_loop (forall_tmp, tmp, 1);
4746 : :
4747 : 241 : gfc_add_expr_to_block (block, tmp);
4748 : :
4749 : 241 : 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 : 1381 : allocate_temp_for_forall_nest_1 (tree type, tree size, stmtblock_t * block,
4758 : : tree * ptemp1)
4759 : : {
4760 : 1381 : tree bytesize;
4761 : 1381 : tree unit;
4762 : 1381 : tree tmp;
4763 : :
4764 : 1381 : unit = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (type));
4765 : 1381 : if (!integer_onep (unit))
4766 : 166 : bytesize = fold_build2_loc (input_location, MULT_EXPR,
4767 : : gfc_array_index_type, size, unit);
4768 : : else
4769 : : bytesize = size;
4770 : :
4771 : 1381 : *ptemp1 = NULL;
4772 : 1381 : tmp = gfc_do_allocate (bytesize, size, ptemp1, block, type);
4773 : :
4774 : 1381 : if (*ptemp1)
4775 : 242 : tmp = build_fold_indirect_ref_loc (input_location, tmp);
4776 : 1381 : 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 : 967 : 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 : 967 : tree size;
4790 : :
4791 : : /* Calculate the total size of temporary needed in forall construct. */
4792 : 967 : size = compute_overall_iter_number (nested_forall_info, inner_size,
4793 : : inner_size_body, block);
4794 : :
4795 : 967 : 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 : 224 : 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 : 224 : tree type;
4837 : 224 : tree inner_size;
4838 : 224 : gfc_ss *lss, *rss;
4839 : 224 : tree count, count1;
4840 : 224 : tree tmp, tmp1;
4841 : 224 : tree ptemp1;
4842 : 224 : stmtblock_t inner_size_body;
4843 : :
4844 : : /* Create vars. count1 is the current iterator number of the nested
4845 : : forall. */
4846 : 224 : count1 = gfc_create_var (gfc_array_index_type, "count1");
4847 : :
4848 : : /* Count is the wheremask index. */
4849 : 224 : if (wheremask)
4850 : : {
4851 : 27 : count = gfc_create_var (gfc_array_index_type, "count");
4852 : 27 : gfc_add_modify (block, count, gfc_index_zero_node);
4853 : : }
4854 : : else
4855 : : count = NULL;
4856 : :
4857 : : /* Initialize count1. */
4858 : 224 : 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 : 224 : if (expr1->ts.type == BT_CHARACTER)
4864 : : {
4865 : 103 : type = NULL;
4866 : 103 : if (expr1->ref && expr1->ref->type == REF_SUBSTRING)
4867 : : {
4868 : 72 : gfc_se ssse;
4869 : 72 : gfc_init_se (&ssse, NULL);
4870 : 72 : gfc_conv_expr (&ssse, expr1);
4871 : 72 : type = gfc_get_character_type_len (gfc_default_character_kind,
4872 : : ssse.string_length);
4873 : 72 : }
4874 : : else
4875 : : {
4876 : 31 : if (!expr1->ts.u.cl->backend_decl)
4877 : : {
4878 : 6 : gfc_se tse;
4879 : 6 : gcc_assert (expr1->ts.u.cl->length);
4880 : 6 : gfc_init_se (&tse, NULL);
4881 : 6 : gfc_conv_expr (&tse, expr1->ts.u.cl->length);
4882 : 6 : expr1->ts.u.cl->backend_decl = tse.expr;
4883 : : }
4884 : 31 : type = gfc_get_character_type_len (gfc_default_character_kind,
4885 : 31 : expr1->ts.u.cl->backend_decl);
4886 : : }
4887 : : }
4888 : : else
4889 : 121 : type = gfc_typenode_for_spec (&expr1->ts);
4890 : :
4891 : 224 : gfc_init_block (&inner_size_body);
4892 : 224 : 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 : 224 : 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 : 224 : 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 : 224 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4907 : 224 : gfc_add_expr_to_block (block, tmp);
4908 : :
4909 : : /* Reset count1. */
4910 : 224 : gfc_add_modify (block, count1, gfc_index_zero_node);
4911 : :
4912 : : /* Reset count. */
4913 : 224 : if (wheremask)
4914 : 27 : 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 : 224 : 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 : 224 : 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 : 224 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4929 : 224 : gfc_add_expr_to_block (block, tmp);
4930 : :
4931 : 224 : if (ptemp1)
4932 : : {
4933 : : /* Free the temporary. */
4934 : 145 : tmp = gfc_call_free (ptemp1);
4935 : 145 : gfc_add_expr_to_block (block, tmp);
4936 : : }
4937 : 224 : }
4938 : :
4939 : :
4940 : : /* Translate pointer assignment inside FORALL which need temporary. */
4941 : :
4942 : : static void
4943 : 20 : gfc_trans_pointer_assign_need_temp (gfc_expr * expr1, gfc_expr * expr2,
4944 : : forall_info * nested_forall_info,
4945 : : stmtblock_t * block)
4946 : : {
4947 : 20 : tree type;
4948 : 20 : tree inner_size;
4949 : 20 : gfc_ss *lss, *rss;
4950 : 20 : gfc_se lse;
4951 : 20 : gfc_se rse;
4952 : 20 : gfc_array_info *info;
4953 : 20 : gfc_loopinfo loop;
4954 : 20 : tree desc;
4955 : 20 : tree parm;
4956 : 20 : tree parmtype;
4957 : 20 : stmtblock_t body;
4958 : 20 : tree count;
4959 : 20 : tree tmp, tmp1, ptemp1;
4960 : :
4961 : 20 : count = gfc_create_var (gfc_array_index_type, "count");
4962 : 20 : gfc_add_modify (block, count, gfc_index_zero_node);
4963 : :
4964 : 20 : inner_size = gfc_index_one_node;
4965 : 20 : lss = gfc_walk_expr (expr1);
4966 : 20 : rss = gfc_walk_expr (expr2);
4967 : 20 : if (lss == gfc_ss_terminator)
4968 : : {
4969 : 11 : type = gfc_typenode_for_spec (&expr1->ts);
4970 : 11 : 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 : 11 : tmp1 = allocate_temp_for_forall_nest (nested_forall_info, type,
4975 : : inner_size, NULL, block, &ptemp1);
4976 : 11 : gfc_start_block (&body);
4977 : 11 : gfc_init_se (&lse, NULL);
4978 : 11 : lse.expr = gfc_build_array_ref (tmp1, count, NULL);
4979 : 11 : gfc_init_se (&rse, NULL);
4980 : 11 : rse.want_pointer = 1;
4981 : 11 : gfc_conv_expr (&rse, expr2);
4982 : 11 : gfc_add_block_to_block (&body, &rse.pre);
4983 : 11 : gfc_add_modify (&body, lse.expr,
4984 : 11 : fold_convert (TREE_TYPE (lse.expr), rse.expr));
4985 : 11 : gfc_add_block_to_block (&body, &rse.post);
4986 : :
4987 : : /* Increment count. */
4988 : 11 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4989 : : count, gfc_index_one_node);
4990 : 11 : gfc_add_modify (&body, count, tmp);
4991 : :
4992 : 11 : tmp = gfc_finish_block (&body);
4993 : :
4994 : : /* Generate body and loops according to the information in
4995 : : nested_forall_info. */
4996 : 11 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
4997 : 11 : gfc_add_expr_to_block (block, tmp);
4998 : :
4999 : : /* Reset count. */
5000 : 11 : gfc_add_modify (block, count, gfc_index_zero_node);
5001 : :
5002 : 11 : gfc_start_block (&body);
5003 : 11 : gfc_init_se (&lse, NULL);
5004 : 11 : gfc_init_se (&rse, NULL);
5005 : 11 : rse.expr = gfc_build_array_ref (tmp1, count, NULL);
5006 : 11 : lse.want_pointer = 1;
5007 : 11 : gfc_conv_expr (&lse, expr1);
5008 : 11 : gfc_add_block_to_block (&body, &lse.pre);
5009 : 11 : gfc_add_modify (&body, lse.expr, rse.expr);
5010 : 11 : gfc_add_block_to_block (&body, &lse.post);
5011 : : /* Increment count. */
5012 : 11 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5013 : : count, gfc_index_one_node);
5014 : 11 : gfc_add_modify (&body, count, tmp);
5015 : 11 : tmp = gfc_finish_block (&body);
5016 : :
5017 : : /* Generate body and loops according to the information in
5018 : : nested_forall_info. */
5019 : 11 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5020 : 11 : gfc_add_expr_to_block (block, tmp);
5021 : : }
5022 : : else
5023 : : {
5024 : 9 : gfc_init_loopinfo (&loop);
5025 : :
5026 : : /* Associate the SS with the loop. */
5027 : 9 : gfc_add_ss_to_loop (&loop, rss);
5028 : :
5029 : : /* Setup the scalarizing loops and bounds. */
5030 : 9 : gfc_conv_ss_startstride (&loop);
5031 : :
5032 : 9 : gfc_conv_loop_setup (&loop, &expr2->where);
5033 : :
5034 : 9 : info = &rss->info->data.array;
5035 : 9 : desc = info->descriptor;
5036 : :
5037 : : /* Make a new descriptor. */
5038 : 9 : parmtype = gfc_get_element_type (TREE_TYPE (desc));
5039 : 9 : 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 : 9 : tmp1 = allocate_temp_for_forall_nest (nested_forall_info, parmtype,
5045 : : inner_size, NULL, block, &ptemp1);
5046 : 9 : gfc_start_block (&body);
5047 : 9 : gfc_init_se (&lse, NULL);
5048 : 9 : lse.expr = gfc_build_array_ref (tmp1, count, NULL);
5049 : 9 : lse.direct_byref = 1;
5050 : 9 : gfc_conv_expr_descriptor (&lse, expr2);
5051 : :
5052 : 9 : gfc_add_block_to_block (&body, &lse.pre);
5053 : 9 : gfc_add_block_to_block (&body, &lse.post);
5054 : :
5055 : : /* Increment count. */
5056 : 9 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5057 : : count, gfc_index_one_node);
5058 : 9 : gfc_add_modify (&body, count, tmp);
5059 : :
5060 : 9 : tmp = gfc_finish_block (&body);
5061 : :
5062 : : /* Generate body and loops according to the information in
5063 : : nested_forall_info. */
5064 : 9 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5065 : 9 : gfc_add_expr_to_block (block, tmp);
5066 : :
5067 : : /* Reset count. */
5068 : 9 : gfc_add_modify (block, count, gfc_index_zero_node);
5069 : :
5070 : 9 : parm = gfc_build_array_ref (tmp1, count, NULL);
5071 : 9 : gfc_init_se (&lse, NULL);
5072 : 9 : gfc_conv_expr_descriptor (&lse, expr1);
5073 : 9 : gfc_add_modify (&lse.pre, lse.expr, parm);
5074 : 9 : gfc_start_block (&body);
5075 : 9 : gfc_add_block_to_block (&body, &lse.pre);
5076 : 9 : gfc_add_block_to_block (&body, &lse.post);
5077 : :
5078 : : /* Increment count. */
5079 : 9 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5080 : : count, gfc_index_one_node);
5081 : 9 : gfc_add_modify (&body, count, tmp);
5082 : :
5083 : 9 : tmp = gfc_finish_block (&body);
5084 : :
5085 : 9 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5086 : 9 : gfc_add_expr_to_block (block, tmp);
5087 : : }
5088 : : /* Free the temporary. */
5089 : 20 : if (ptemp1)
5090 : : {
5091 : 1 : tmp = gfc_call_free (ptemp1);
5092 : 1 : gfc_add_expr_to_block (block, tmp);
5093 : : }
5094 : 20 : }
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 : 232 : 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 : 232 : if (!locality_list[LOCALITY_LOCAL] && !locality_list[LOCALITY_LOCAL_INIT])
5122 : : return;
5123 : :
5124 : 96 : if (after_body)
5125 : : {
5126 : 205 : for (unsigned i = 0; i < saved_decls->size (); i++)
5127 : : {
5128 : 157 : (*saved_decls)[i].sym->backend_decl = (*saved_decls)[i].decl;
5129 : 157 : (*saved_decls)[i].sym->attr = (*saved_decls)[i].attr;
5130 : 157 : (*saved_decls)[i].sym->value = (*saved_decls)[i].value;
5131 : : }
5132 : : return;
5133 : : }
5134 : :
5135 : : gfc_expr_list *el;
5136 : : int cnt = 0;
5137 : 144 : for (int i = 0; i <= 1; i++)
5138 : 96 : for (el = locality_list[i == 0 ? LOCALITY_LOCAL : LOCALITY_LOCAL_INIT];
5139 : 253 : el; el = el->next)
5140 : : {
5141 : 157 : gfc_symbol *outer_sym = el->expr->symtree->n.sym;
5142 : 157 : if (!outer_sym->backend_decl)
5143 : 0 : outer_sym->backend_decl = gfc_get_symbol_decl (outer_sym);
5144 : 157 : cnt++;
5145 : : }
5146 : 48 : saved_decls->resize (cnt);
5147 : :
5148 : : /* The variables have to be created in the scope of the loop body. */
5149 : 48 : if (!body->has_scope)
5150 : : {
5151 : 48 : gcc_checking_assert (body->head == NULL_TREE);
5152 : 48 : gfc_start_block (body);
5153 : : }
5154 : 48 : gfc_start_saved_local_decls ();
5155 : :
5156 : 48 : cnt = 0;
5157 : 48 : static_assert (LOCALITY_LOCAL_INIT - LOCALITY_LOCAL == 1, "locality_type");
5158 : 144 : for (int type = LOCALITY_LOCAL;
5159 : 144 : type <= LOCALITY_LOCAL_INIT; type++)
5160 : 253 : for (el = locality_list[type]; el; el = el->next)
5161 : : {
5162 : 157 : gfc_symbol *sym = el->expr->symtree->n.sym;
5163 : 157 : (*saved_decls)[cnt].sym = sym;
5164 : 157 : (*saved_decls)[cnt].attr = sym->attr;
5165 : 157 : (*saved_decls)[cnt].value = sym->value;
5166 : 157 : (*saved_decls)[cnt].decl = sym->backend_decl;
5167 : :
5168 : 157 : if (sym->attr.dimension && sym->as->type == AS_ASSUMED_SHAPE)
5169 : : {
5170 : 3 : 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 : 2 : continue;
5175 : : }
5176 : :
5177 : 155 : gfc_symbol outer_sym = *sym;
5178 : :
5179 : : /* Create the inner local variable. */
5180 : 155 : sym->backend_decl = NULL;
5181 : 155 : sym->value = NULL;
5182 : 155 : sym->attr.save = SAVE_NONE;
5183 : 155 : sym->attr.value = 0;
5184 : 155 : sym->attr.dummy = 0;
5185 : 155 : sym->attr.optional = 0;
5186 : :
5187 : 155 : {
5188 : : /* Slightly ugly hack for adding the decl via add_decl_as_local. */
5189 : 155 : gfc_symbol dummy_block_sym;
5190 : 155 : dummy_block_sym.attr.flavor = FL_LABEL;
5191 : 155 : gfc_symbol *saved_proc_name = sym->ns->proc_name;
5192 : 155 : sym->ns->proc_name = &dummy_block_sym;
5193 : :
5194 : 155 : gfc_get_symbol_decl (sym);
5195 : 310 : DECL_SOURCE_LOCATION (sym->backend_decl)
5196 : 155 : = gfc_get_location (&el->expr->where);
5197 : :
5198 : 155 : sym->ns->proc_name = saved_proc_name;
5199 : : }
5200 : :
5201 : 155 : symbol_attribute attr = gfc_expr_attr (el->expr);
5202 : 155 : if (type == LOCALITY_LOCAL
5203 : 89 : && !attr.pointer
5204 : 41 : && sym->ts.type == BT_DERIVED
5205 : 183 : && gfc_has_default_initializer (sym->ts.u.derived))
5206 : : /* Cf. PR fortran/ */
5207 : 4 : gfc_error ("Sorry, LOCAL specifier at %L for %qs of derived type with"
5208 : : " default initializer is not yet supported",
5209 : 4 : &el->expr->where, sym->name);
5210 : 155 : if (type == LOCALITY_LOCAL_INIT)
5211 : : {
5212 : : /* LOCAL_INIT: local_var = outer_var. */
5213 : 66 : gfc_symtree st = *el->expr->symtree;
5214 : 66 : st.n.sym = &outer_sym;
5215 : 66 : gfc_expr expr = *el->expr;
5216 : 66 : expr.symtree = &st;
5217 : 66 : tree t = (attr.pointer
5218 : 66 : ? gfc_trans_pointer_assignment (el->expr, &expr)
5219 : 42 : : gfc_trans_assignment (el->expr, &expr, false, false,
5220 : 66 : false, false));
5221 : 66 : gfc_add_expr_to_block (body, t);
5222 : : }
5223 : 155 : cnt++;
5224 : : }
5225 : 48 : 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 : 2079 : gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
5269 : : {
5270 : 2079 : stmtblock_t pre;
5271 : 2079 : stmtblock_t post;
5272 : 2079 : stmtblock_t block;
5273 : 2079 : stmtblock_t body;
5274 : 2079 : tree *var;
5275 : 2079 : tree *start;
5276 : 2079 : tree *end;
5277 : 2079 : tree *step;
5278 : 2079 : gfc_expr **varexpr;
5279 : 2079 : tree tmp;
5280 : 2079 : tree assign;
5281 : 2079 : tree size;
5282 : 2079 : tree maskindex;
5283 : 2079 : tree mask;
5284 : 2079 : tree pmask;
5285 : 2079 : tree cycle_label = NULL_TREE;
5286 : 2079 : int n;
5287 : 2079 : int nvar;
5288 : 2079 : int need_temp;
5289 : 2079 : gfc_forall_iterator *fa;
5290 : 2079 : gfc_se se;
5291 : 2079 : gfc_code *c;
5292 : 2079 : gfc_saved_var *saved_vars;
5293 : 2079 : iter_info *this_forall;
5294 : 2079 : forall_info *info;
5295 : 2079 : bool need_mask;
5296 : :
5297 : : /* Do nothing if the mask is false. */
5298 : 2079 : if (code->expr1
5299 : 725 : && code->expr1->expr_type == EXPR_CONSTANT
5300 : 2 : && !code->expr1->value.logical)
5301 : 1 : return build_empty_stmt (input_location);
5302 : :
5303 : 2078 : n = 0;
5304 : : /* Count the FORALL index number. */
5305 : 6060 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5306 : 3982 : n++;
5307 : 2078 : nvar = n;
5308 : :
5309 : : /* Allocate the space for var, start, end, step, varexpr. */
5310 : 2078 : var = XCNEWVEC (tree, nvar);
5311 : 2078 : start = XCNEWVEC (tree, nvar);
5312 : 2078 : end = XCNEWVEC (tree, nvar);
5313 : 2078 : step = XCNEWVEC (tree, nvar);
5314 : 2078 : varexpr = XCNEWVEC (gfc_expr *, nvar);
5315 : 2078 : saved_vars = XCNEWVEC (gfc_saved_var, nvar);
5316 : :
5317 : : /* Allocate the space for info. */
5318 : 2078 : info = XCNEW (forall_info);
5319 : :
5320 : 2078 : gfc_start_block (&pre);
5321 : 2078 : gfc_init_block (&post);
5322 : 2078 : gfc_init_block (&block);
5323 : :
5324 : 2078 : n = 0;
5325 : 6060 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
5326 : : {
5327 : 3982 : gfc_symbol *sym = fa->var->symtree->n.sym;
5328 : :
5329 : : /* Allocate space for this_forall. */
5330 : 3982 : this_forall = XCNEW (iter_info);
5331 : :
5332 : : /* Create a temporary variable for the FORALL index. */
5333 : 3982 : tmp = gfc_typenode_for_spec (&sym->ts);
5334 : 3982 : var[n] = gfc_create_var (tmp, sym->name);
5335 : 3982 : gfc_shadow_sym (sym, var[n], &saved_vars[n]);
5336 : :
5337 : : /* Record it in this_forall. */
5338 : 3982 : this_forall->var = var[n];
5339 : :
5340 : : /* Replace the index symbol's backend_decl with the temporary decl. */
5341 : 3982 : sym->backend_decl = var[n];
5342 : :
5343 : : /* Work out the start, end and stride for the loop. */
5344 : 3982 : gfc_init_se (&se, NULL);
5345 : 3982 : gfc_conv_expr_val (&se, fa->start);
5346 : : /* Record it in this_forall. */
5347 : 3982 : this_forall->start = se.expr;
5348 : 3982 : gfc_add_block_to_block (&block, &se.pre);
5349 : 3982 : start[n] = se.expr;
5350 : :
5351 : 3982 : gfc_init_se (&se, NULL);
5352 : 3982 : gfc_conv_expr_val (&se, fa->end);
5353 : : /* Record it in this_forall. */
5354 : 3982 : this_forall->end = se.expr;
5355 : 3982 : gfc_make_safe_expr (&se);
5356 : 3982 : gfc_add_block_to_block (&block, &se.pre);
5357 : 3982 : end[n] = se.expr;
5358 : :
5359 : 3982 : gfc_init_se (&se, NULL);
5360 : 3982 : gfc_conv_expr_val (&se, fa->stride);
5361 : : /* Record it in this_forall. */
5362 : 3982 : this_forall->step = se.expr;
5363 : 3982 : gfc_make_safe_expr (&se);
5364 : 3982 : gfc_add_block_to_block (&block, &se.pre);
5365 : 3982 : step[n] = se.expr;
5366 : :
5367 : : /* Copy loop annotations. */
5368 : 3982 : this_forall->annot = fa->annot;
5369 : :
5370 : : /* Set the NEXT field of this_forall to NULL. */
5371 : 3982 : this_forall->next = NULL;
5372 : : /* Link this_forall to the info construct. */
5373 : 3982 : if (info->this_loop)
5374 : : {
5375 : : iter_info *iter_tmp = info->this_loop;
5376 : 2830 : while (iter_tmp->next != NULL)
5377 : : iter_tmp = iter_tmp->next;
5378 : 1904 : iter_tmp->next = this_forall;
5379 : : }
5380 : : else
5381 : 2078 : info->this_loop = this_forall;
5382 : :
5383 : 3982 : n++;
5384 : : }
5385 : 2078 : nvar = n;
5386 : :
5387 : : /* Calculate the size needed for the current forall level. */
5388 : 2078 : size = gfc_index_one_node;
5389 : 6060 : for (n = 0; n < nvar; n++)
5390 : : {
5391 : : /* size = (end + step - start) / step. */
5392 : 3982 : tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (start[n]),
5393 : 3982 : step[n], start[n]);
5394 : 3982 : tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (end[n]),
5395 : 3982 : end[n], tmp);
5396 : 3982 : tmp = fold_build2_loc (input_location, FLOOR_DIV_EXPR, TREE_TYPE (tmp),
5397 : : tmp, step[n]);
5398 : 3982 : tmp = convert (gfc_array_index_type, tmp);
5399 : :
5400 : 3982 : 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 : 2078 : info->nvar = nvar;
5406 : 2078 : info->size = size;
5407 : :
5408 : 2078 : if (code->expr1)
5409 : : {
5410 : : /* If the mask is .true., consider the FORALL unconditional. */
5411 : 724 : if (code->expr1->expr_type == EXPR_CONSTANT
5412 : 1 : && code->expr1->value.logical)
5413 : : need_mask = false;
5414 : : else
5415 : 723 : need_mask = true;
5416 : : }
5417 : : else
5418 : : need_mask = false;
5419 : :
5420 : : /* First we need to allocate the mask. */
5421 : 723 : if (need_mask)
5422 : : {
5423 : : /* As the mask array can be very big, prefer compact boolean types. */
5424 : 723 : tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5425 : 723 : mask = allocate_temp_for_forall_nest (nested_forall_info, mask_type,
5426 : : size, NULL, &block, &pmask);
5427 : 723 : maskindex = gfc_create_var_np (gfc_array_index_type, "mi");
5428 : :
5429 : : /* Record them in the info structure. */
5430 : 723 : info->maskindex = maskindex;
5431 : 723 : info->mask = mask;
5432 : : }
5433 : : else
5434 : : {
5435 : : /* No mask was specified. */
5436 : 1355 : maskindex = NULL_TREE;
5437 : 1355 : mask = pmask = NULL_TREE;
5438 : : }
5439 : :
5440 : : /* Link the current forall level to nested_forall_info. */
5441 : 2078 : info->prev_nest = nested_forall_info;
5442 : 2078 : 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 : 2078 : if (need_mask)
5447 : : {
5448 : : /* As the mask array can be very big, prefer compact boolean types. */
5449 : 723 : tree mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
5450 : :
5451 : 723 : gfc_add_modify (&block, maskindex, gfc_index_zero_node);
5452 : :
5453 : : /* Start of mask assignment loop body. */
5454 : 723 : gfc_start_block (&body);
5455 : :
5456 : : /* Evaluate the mask expression. */
5457 : 723 : gfc_init_se (&se, NULL);
5458 : 723 : gfc_conv_expr_val (&se, code->expr1);
5459 : 723 : gfc_add_block_to_block (&body, &se.pre);
5460 : :
5461 : : /* Store the mask. */
5462 : 723 : se.expr = convert (mask_type, se.expr);
5463 : :
5464 : 723 : tmp = gfc_build_array_ref (mask, maskindex, NULL);
5465 : 723 : gfc_add_modify (&body, tmp, se.expr);
5466 : :
5467 : : /* Advance to the next mask element. */
5468 : 723 : tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5469 : : maskindex, gfc_index_one_node);
5470 : 723 : gfc_add_modify (&body, maskindex, tmp);
5471 : :
5472 : : /* Generate the loops. */
5473 : 723 : tmp = gfc_finish_block (&body);
5474 : 723 : tmp = gfc_trans_nested_forall_loop (info, tmp, 0);
5475 : 723 : gfc_add_expr_to_block (&block, tmp);
5476 : : }
5477 : :
5478 : 2078 : if (code->op == EXEC_DO_CONCURRENT)
5479 : : {
5480 : 116 : gfc_init_block (&body);
5481 : 116 : cycle_label = gfc_build_label_decl (NULL_TREE);
5482 : 116 : code->cycle_label = cycle_label;
5483 : :
5484 : : /* Handle LOCAL and LOCAL_INIT. */
5485 : 116 : std::vector<symbol_and_tree_t> saved_decls;
5486 : 116 : gfc_trans_concurrent_locality_spec (false, &body, &saved_decls,
5487 : 116 : code->ext.concur.locality);
5488 : :
5489 : : /* Translate the body. */
5490 : 116 : tmp = gfc_trans_code (code->block->next);
5491 : 116 : gfc_add_expr_to_block (&body, tmp);
5492 : :
5493 : : /* Reset locality variables. */
5494 : 116 : gfc_trans_concurrent_locality_spec (true, &body, &saved_decls,
5495 : : code->ext.concur.locality);
5496 : 116 : if (TREE_USED (cycle_label))
5497 : : {
5498 : 116 : tmp = build1_v (LABEL_EXPR, cycle_label);
5499 : 116 : gfc_add_expr_to_block (&body, tmp);
5500 : : }
5501 : :
5502 : 116 : tmp = gfc_finish_block (&body);
5503 : 116 : nested_forall_info->do_concurrent = true;
5504 : 116 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
5505 : 116 : gfc_add_expr_to_block (&block, tmp);
5506 : 116 : goto done;
5507 : 116 : }
5508 : :
5509 : 1962 : 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 : 3942 : while (c)
5514 : : {
5515 : 1980 : switch (c->op)
5516 : : {
5517 : 1825 : 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 : 1825 : need_temp = check_forall_dependencies(c, &pre, &post);
5522 : :
5523 : : /* Temporaries due to array assignment data dependencies introduce
5524 : : no end of problems. */
5525 : 1825 : if (need_temp || flag_test_forall_temp)
5526 : 197 : 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 : 1628 : assign = gfc_trans_assignment (c->expr1, c->expr2, false, true);
5532 : :
5533 : : /* Generate body and loops. */
5534 : 1628 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5535 : : assign, 1);
5536 : 1628 : gfc_add_expr_to_block (&block, tmp);
5537 : : }
5538 : :
5539 : : /* Cleanup any temporary symtrees that have been made to deal
5540 : : with dependencies. */
5541 : 1825 : if (new_symtree)
5542 : 62 : cleanup_forall_symtrees (c);
5543 : :
5544 : : break;
5545 : :
5546 : 46 : case EXEC_WHERE:
5547 : : /* Translate WHERE or WHERE construct nested in FORALL. */
5548 : 46 : gfc_trans_where_2 (c, NULL, false, nested_forall_info, &block);
5549 : 46 : break;
5550 : :
5551 : : /* Pointer assignment inside FORALL. */
5552 : 27 : case EXEC_POINTER_ASSIGN:
5553 : 27 : 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 : 27 : if (need_temp
5557 : 7 : || (flag_test_forall_temp
5558 : 0 : && c->expr2->expr_type != EXPR_CONSTANT
5559 : 0 : && c->expr2->expr_type != EXPR_NULL))
5560 : 20 : 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 : 7 : assign = gfc_trans_pointer_assignment (c->expr1, c->expr2);
5566 : :
5567 : : /* Generate body and loops. */
5568 : 7 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
5569 : : assign, 1);
5570 : 7 : gfc_add_expr_to_block (&block, tmp);
5571 : : }
5572 : : break;
5573 : :
5574 : 76 : case EXEC_FORALL:
5575 : 76 : tmp = gfc_trans_forall_1 (c, nested_forall_info);
5576 : 76 : gfc_add_expr_to_block (&block, tmp);
5577 : 76 : break;
5578 : :
5579 : : /* Explicit subroutine calls are prevented by the frontend but interface
5580 : : assignments can legitimately produce them. */
5581 : 6 : case EXEC_ASSIGN_CALL:
5582 : 6 : assign = gfc_trans_call (c, true, NULL_TREE, NULL_TREE, false);
5583 : 6 : tmp = gfc_trans_nested_forall_loop (nested_forall_info, assign, 1);
5584 : 6 : gfc_add_expr_to_block (&block, tmp);
5585 : 6 : break;
5586 : :
5587 : 0 : default:
5588 : 0 : gcc_unreachable ();
5589 : : }
5590 : :
5591 : 1980 : c = c->next;
5592 : : }
5593 : :
5594 : 1962 : done:
5595 : : /* Restore the original index variables. */
5596 : 6060 : for (fa = code->ext.concur.forall_iterator, n = 0; fa; fa = fa->next, n++)
5597 : 3982 : gfc_restore_sym (fa->var->symtree->n.sym, &saved_vars[n]);
5598 : :
5599 : : /* Free the space for var, start, end, step, varexpr. */
5600 : 2078 : free (var);
5601 : 2078 : free (start);
5602 : 2078 : free (end);
5603 : 2078 : free (step);
5604 : 2078 : free (varexpr);
5605 : 2078 : free (saved_vars);
5606 : :
5607 : 6060 : for (this_forall = info->this_loop; this_forall;)
5608 : : {
5609 : 3982 : iter_info *next = this_forall->next;
5610 : 3982 : free (this_forall);
5611 : 3982 : this_forall = next;
5612 : : }
5613 : :
5614 : : /* Free the space for this forall_info. */
5615 : 2078 : free (info);
5616 : :
5617 : 2078 : if (pmask)
5618 : : {
5619 : : /* Free the temporary for the mask. */
5620 : 52 : tmp = gfc_call_free (pmask);
5621 : 52 : gfc_add_expr_to_block (&block, tmp);
5622 : : }
5623 : 2078 : if (maskindex)
5624 : 723 : pushdecl (maskindex);
5625 : :
5626 : 2078 : gfc_add_block_to_block (&pre, &block);
5627 : 2078 : gfc_add_block_to_block (&pre, &post);
5628 : :
5629 : 2078 : return gfc_finish_block (&pre);
5630 : : }
5631 : :
5632 : :
5633 : : /* Translate the FORALL statement or construct. */
5634 : :
5635 : 1887 : tree gfc_trans_forall (gfc_code * code)
5636 : : {
5637 : 1887 : return gfc_trans_forall_1 (code, NULL);
5638 : : }
5639 : :
5640 : :
5641 : : /* Translate the DO CONCURRENT construct. */
5642 : :
5643 : 116 : tree gfc_trans_do_concurrent (gfc_code * code)
5644 : : {
5645 : 116 : 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 : 527 : 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 : 527 : tree tmp, tmp1;
5666 : 527 : gfc_ss *lss, *rss;
5667 : 527 : gfc_loopinfo loop;
5668 : 527 : stmtblock_t body, body1;
5669 : 527 : tree count, cond, mtmp;
5670 : 527 : gfc_se lse, rse;
5671 : :
5672 : 527 : gfc_init_loopinfo (&loop);
5673 : :
5674 : 527 : lss = gfc_walk_expr (me);
5675 : 527 : rss = gfc_walk_expr (me);
5676 : :
5677 : : /* Variable to index the temporary. */
5678 : 527 : count = gfc_create_var (gfc_array_index_type, "count");
5679 : : /* Initialize count. */
5680 : 527 : gfc_add_modify (block, count, gfc_index_zero_node);
5681 : :
5682 : 527 : gfc_start_block (&body);
5683 : :
5684 : 527 : gfc_init_se (&rse, NULL);
5685 : 527 : gfc_init_se (&lse, NULL);
5686 : :
5687 : 527 : if (lss == gfc_ss_terminator)
5688 : : {
5689 : 0 : gfc_init_block (&body1);
5690 : : }
5691 : : else
5692 : : {
5693 : : /* Initialize the loop. */
5694 : 527 : gfc_init_loopinfo (&loop);
5695 : :
5696 : : /* We may need LSS to determine the shape of the expression. */
5697 : 527 : gfc_add_ss_to_loop (&loop, lss);
5698 : 527 : gfc_add_ss_to_loop (&loop, rss);
5699 : :
5700 : 527 : gfc_conv_ss_startstride (&loop);
5701 : 527 : gfc_conv_loop_setup (&loop, &me->where);
5702 : :
5703 : 527 : gfc_mark_ss_chain_used (rss, 1);
5704 : : /* Start the loop body. */
5705 : 527 : gfc_start_scalarized_body (&loop, &body1);
5706 : :
5707 : : /* Translate the expression. */
5708 : 527 : gfc_copy_loopinfo_to_se (&rse, &loop);
5709 : 527 : rse.ss = rss;
5710 : 527 : gfc_conv_expr (&rse, me);
5711 : : }
5712 : :
5713 : : /* Variable to evaluate mask condition. */
5714 : 527 : cond = gfc_create_var (mask_type, "cond");
5715 : 527 : if (mask && (cmask || pmask))
5716 : 234 : mtmp = gfc_create_var (mask_type, "mask");
5717 : : else mtmp = NULL_TREE;
5718 : :
5719 : 527 : gfc_add_block_to_block (&body1, &lse.pre);
5720 : 527 : gfc_add_block_to_block (&body1, &rse.pre);
5721 : :
5722 : 527 : gfc_add_modify (&body1, cond, fold_convert (mask_type, rse.expr));
5723 : :
5724 : 527 : if (mask && (cmask || pmask))
5725 : : {
5726 : 234 : tmp = gfc_build_array_ref (mask, count, NULL);
5727 : 234 : if (invert)
5728 : 99 : tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, tmp);
5729 : 234 : gfc_add_modify (&body1, mtmp, tmp);
5730 : : }
5731 : :
5732 : 527 : if (cmask)
5733 : : {
5734 : 509 : tmp1 = gfc_build_array_ref (cmask, count, NULL);
5735 : 509 : tmp = cond;
5736 : 509 : if (mask)
5737 : 234 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type,
5738 : : mtmp, tmp);
5739 : 509 : gfc_add_modify (&body1, tmp1, tmp);
5740 : : }
5741 : :
5742 : 527 : if (pmask)
5743 : : {
5744 : 146 : tmp1 = gfc_build_array_ref (pmask, count, NULL);
5745 : 146 : tmp = fold_build1_loc (input_location, TRUTH_NOT_EXPR, mask_type, cond);
5746 : 146 : if (mask)
5747 : 146 : tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR, mask_type, mtmp,
5748 : : tmp);
5749 : 146 : gfc_add_modify (&body1, tmp1, tmp);
5750 : : }
5751 : :
5752 : 527 : gfc_add_block_to_block (&body1, &lse.post);
5753 : 527 : gfc_add_block_to_block (&body1, &rse.post);
5754 : :
5755 : 527 : if (lss == gfc_ss_terminator)
5756 : : {
5757 : 0 : gfc_add_block_to_block (&body, &body1);
5758 : : }
5759 : : else
5760 : : {
5761 : : /* Increment count. */
5762 : 527 : tmp1 = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
5763 : : count, gfc_index_one_node);
5764 : 527 : gfc_add_modify (&body1, count, tmp1);
5765 : :
5766 : : /* Generate the copying loops. */
5767 : 527 : gfc_trans_scalarizing_loops (&loop, &body1);
5768 : :
5769 : 527 : gfc_add_block_to_block (&body, &loop.pre);
5770 : 527 : gfc_add_block_to_block (&body, &loop.post);
5771 : :
5772 : 527 : 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 : 527 : tmp1 = gfc_finish_block (&body);
5778 : : /* If the WHERE construct is inside FORALL, fill the full temporary. */
5779 : 527 : if (nested_forall_info != NULL)
5780 : 64 : tmp1 = gfc_trans_nested_forall_loop (nested_forall_info, tmp1, 1);
5781 : :
5782 : 527 : gfc_add_expr_to_block (block, tmp1);
5783 : 527 : }
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 : 544 : 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 : 544 : gfc_se lse;
5798 : 544 : gfc_se rse;
5799 : 544 : gfc_ss *lss;
5800 : 544 : gfc_ss *lss_section;
5801 : 544 : gfc_ss *rss;
5802 : :
5803 : 544 : gfc_loopinfo loop;
5804 : 544 : tree tmp;
5805 : 544 : stmtblock_t block;
5806 : 544 : stmtblock_t body;
5807 : 544 : tree index, maskexpr;
5808 : :
5809 : : /* A defined assignment. */
5810 : 544 : if (cnext && cnext->resolved_sym)
5811 : 44 : 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 : 500 : gfc_start_block (&block);
5826 : :
5827 : 500 : gfc_init_se (&lse, NULL);
5828 : 500 : gfc_init_se (&rse, NULL);
5829 : :
5830 : : /* Walk the lhs. */
5831 : 500 : lss = gfc_walk_expr (expr1);
5832 : 500 : 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 : 500 : 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 : 500 : && lss_section->info->type != GFC_SS_SECTION)
5844 : 0 : lss_section = lss_section->next;
5845 : :
5846 : 500 : gcc_assert (lss_section != gfc_ss_terminator);
5847 : :
5848 : : /* Initialize the scalarizer. */
5849 : 500 : gfc_init_loopinfo (&loop);
5850 : :
5851 : : /* Walk the rhs. */
5852 : 500 : rss = gfc_walk_expr (expr2);
5853 : 500 : if (rss == gfc_ss_terminator)
5854 : : {
5855 : : /* The rhs is scalar. Add a ss for the expression. */
5856 : 343 : rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
5857 : 343 : rss->info->where = 1;
5858 : : }
5859 : :
5860 : : /* Associate the SS with the loop. */
5861 : 500 : gfc_add_ss_to_loop (&loop, lss);
5862 : 500 : gfc_add_ss_to_loop (&loop, rss);
5863 : :
5864 : : /* Calculate the bounds of the scalarization. */
5865 : 500 : gfc_conv_ss_startstride (&loop);
5866 : :
5867 : : /* Resolve any data dependencies in the statement. */
5868 : 500 : gfc_conv_resolve_dependencies (&loop, lss_section, rss);
5869 : :
5870 : : /* Setup the scalarizing loops. */
5871 : 500 : gfc_conv_loop_setup (&loop, &expr2->where);
5872 : :
5873 : : /* Setup the gfc_se structures. */
5874 : 500 : gfc_copy_loopinfo_to_se (&lse, &loop);
5875 : 500 : gfc_copy_loopinfo_to_se (&rse, &loop);
5876 : :
5877 : 500 : rse.ss = rss;
5878 : 500 : gfc_mark_ss_chain_used (rss, 1);
5879 : 500 : if (loop.temp_ss == NULL)
5880 : : {
5881 : 422 : lse.ss = lss;
5882 : 422 : gfc_mark_ss_chain_used (lss, 1);
5883 : : }
5884 : : else
5885 : : {
5886 : 78 : lse.ss = loop.temp_ss;
5887 : 78 : gfc_mark_ss_chain_used (lss, 3);
5888 : 78 : gfc_mark_ss_chain_used (loop.temp_ss, 3);
5889 : : }
5890 : :
5891 : : /* Start the scalarized loop body. */
5892 : 500 : gfc_start_scalarized_body (&loop, &body);
5893 : :
5894 : : /* Translate the expression. */
5895 : 500 : gfc_conv_expr (&rse, expr2);
5896 : 500 : if (lss != gfc_ss_terminator && loop.temp_ss != NULL)
5897 : 78 : gfc_conv_tmp_array_ref (&lse);
5898 : : else
5899 : 422 : gfc_conv_expr (&lse, expr1);
5900 : :
5901 : : /* Form the mask expression according to the mask. */
5902 : 500 : index = count1;
5903 : 500 : maskexpr = gfc_build_array_ref (mask, index, NULL);
5904 : 500 : if (invert)
5905 : 24 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
5906 : 24 : TREE_TYPE (maskexpr), maskexpr);
5907 : :
5908 : : /* Use the scalar assignment as is. */
5909 : 1000 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
5910 : 500 : false, loop.temp_ss == NULL);
5911 : :
5912 : 500 : tmp = build3_v (COND_EXPR, maskexpr, tmp, build_empty_stmt (input_location));
5913 : :
5914 : 500 : gfc_add_expr_to_block (&body, tmp);
5915 : :
5916 : 500 : 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 : 500 : gcc_assert (lse.ss == gfc_ss_terminator
5929 : : && rse.ss == gfc_ss_terminator);
5930 : :
5931 : 500 : if (loop.temp_ss != NULL)
5932 : : {
5933 : : /* Increment count1 before finish the main body of a scalarized
5934 : : expression. */
5935 : 78 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5936 : : gfc_array_index_type, count1, gfc_index_one_node);
5937 : 78 : gfc_add_modify (&body, count1, tmp);
5938 : 78 : gfc_trans_scalarized_loop_boundary (&loop, &body);
5939 : :
5940 : : /* We need to copy the temporary to the actual lhs. */
5941 : 78 : gfc_init_se (&lse, NULL);
5942 : 78 : gfc_init_se (&rse, NULL);
5943 : 78 : gfc_copy_loopinfo_to_se (&lse, &loop);
5944 : 78 : gfc_copy_loopinfo_to_se (&rse, &loop);
5945 : :
5946 : 78 : rse.ss = loop.temp_ss;
5947 : 78 : lse.ss = lss;
5948 : :
5949 : 78 : gfc_conv_tmp_array_ref (&rse);
5950 : 78 : gfc_conv_expr (&lse, expr1);
5951 : :
5952 : 78 : 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 : 78 : index = count2;
5957 : 78 : maskexpr = gfc_build_array_ref (mask, index, NULL);
5958 : 78 : if (invert)
5959 : 0 : maskexpr = fold_build1_loc (input_location, TRUTH_NOT_EXPR,
5960 : 0 : TREE_TYPE (maskexpr), maskexpr);
5961 : :
5962 : : /* Use the scalar assignment as is. */
5963 : 78 : tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts, false, true);
5964 : 78 : tmp = build3_v (COND_EXPR, maskexpr, tmp,
5965 : : build_empty_stmt (input_location));
5966 : 78 : gfc_add_expr_to_block (&body, tmp);
5967 : :
5968 : : /* Increment count2. */
5969 : 78 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5970 : : gfc_array_index_type, count2,
5971 : : gfc_index_one_node);
5972 : 78 : gfc_add_modify (&body, count2, tmp);
5973 : : }
5974 : : else
5975 : : {
5976 : : /* Increment count1. */
5977 : 422 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
5978 : : gfc_array_index_type, count1,
5979 : : gfc_index_one_node);
5980 : 422 : gfc_add_modify (&body, count1, tmp);
5981 : : }
5982 : :
5983 : : /* Generate the copying loops. */
5984 : 500 : gfc_trans_scalarizing_loops (&loop, &body);
5985 : :
5986 : : /* Wrap the whole thing up. */
5987 : 500 : gfc_add_block_to_block (&block, &loop.pre);
5988 : 500 : gfc_add_block_to_block (&block, &loop.post);
5989 : 500 : gfc_cleanup_loop (&loop);
5990 : : }
5991 : :
5992 : 500 : 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 : 349 : gfc_trans_where_2 (gfc_code * code, tree mask, bool invert,
6003 : : forall_info * nested_forall_info, stmtblock_t * block)
6004 : : {
6005 : 349 : stmtblock_t inner_size_body;
6006 : 349 : tree inner_size, size;
6007 : 349 : gfc_ss *lss, *rss;
6008 : 349 : tree mask_type;
6009 : 349 : gfc_expr *expr1;
6010 : 349 : gfc_expr *expr2;
6011 : 349 : gfc_code *cblock;
6012 : 349 : gfc_code *cnext;
6013 : 349 : tree tmp;
6014 : 349 : tree cond;
6015 : 349 : tree count1, count2;
6016 : 349 : bool need_cmask;
6017 : 349 : bool need_pmask;
6018 : 349 : int need_temp;
6019 : 349 : tree pcmask = NULL_TREE;
6020 : 349 : tree ppmask = NULL_TREE;
6021 : 349 : tree cmask = NULL_TREE;
6022 : 349 : tree pmask = NULL_TREE;
6023 : 349 : gfc_actual_arglist *arg;
6024 : :
6025 : : /* the WHERE statement or the WHERE construct statement. */
6026 : 349 : cblock = code->block;
6027 : :
6028 : : /* As the mask array can be very big, prefer compact boolean types. */
6029 : 349 : mask_type = gfc_get_logical_type (gfc_logical_kinds[0].kind);
6030 : :
6031 : : /* Determine which temporary masks are needed. */
6032 : 349 : if (!cblock->block)
6033 : : {
6034 : : /* One clause: No ELSEWHEREs. */
6035 : 168 : need_cmask = (cblock->next != 0);
6036 : 168 : need_pmask = false;
6037 : : }
6038 : 181 : else if (cblock->block->block)
6039 : : {
6040 : : /* Three or more clauses: Conditional ELSEWHEREs. */
6041 : : need_cmask = true;
6042 : : need_pmask = true;
6043 : : }
6044 : 102 : else if (cblock->next)
6045 : : {
6046 : : /* Two clauses, the first non-empty. */
6047 : 84 : need_cmask = true;
6048 : 84 : need_pmask = (mask != NULL_TREE
6049 : 84 : && cblock->block->next != 0);
6050 : : }
6051 : 18 : 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 : 9 : else if (mask)
6059 : : {
6060 : 0 : need_cmask = (cblock->block->expr1 != 0);
6061 : 0 : need_pmask = true;
6062 : : }
6063 : : else
6064 : : {
6065 : : need_cmask = true;
6066 : : need_pmask = false;
6067 : : }
6068 : :
6069 : 168 : if (need_cmask || need_pmask)
6070 : : {
6071 : : /* Calculate the size of temporary needed by the mask-expr. */
6072 : 331 : gfc_init_block (&inner_size_body);
6073 : 331 : inner_size = compute_inner_temp_size (cblock->expr1, cblock->expr1,
6074 : : &inner_size_body, &lss, &rss);
6075 : :
6076 : 331 : gfc_free_ss_chain (lss);
6077 : 331 : gfc_free_ss_chain (rss);
6078 : :
6079 : : /* Calculate the total size of temporary needed. */
6080 : 331 : size = compute_overall_iter_number (nested_forall_info, inner_size,
6081 : : &inner_size_body, block);
6082 : :
6083 : : /* Check whether the size is negative. */
6084 : 331 : cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, size,
6085 : : gfc_index_zero_node);
6086 : 331 : size = fold_build3_loc (input_location, COND_EXPR, gfc_array_index_type,
6087 : : cond, gfc_index_zero_node, size);
6088 : 331 : size = gfc_evaluate_now (size, block);
6089 : :
6090 : : /* Allocate temporary for WHERE mask if needed. */
6091 : 331 : if (need_cmask)
6092 : 331 : cmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6093 : : &pcmask);
6094 : :
6095 : : /* Allocate temporary for !mask if needed. */
6096 : 331 : if (need_pmask)
6097 : 83 : pmask = allocate_temp_for_forall_nest_1 (mask_type, size, block,
6098 : : &ppmask);
6099 : : }
6100 : :
6101 : 994 : 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 : 645 : 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 : 527 : if (mask)
6116 : 234 : gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6117 : : mask, invert,
6118 : 234 : cblock->next ? cmask : NULL_TREE,
6119 : 234 : cblock->block ? pmask : NULL_TREE,
6120 : : mask_type, block);
6121 : : else
6122 : 293 : gfc_evaluate_where_mask (cblock->expr1, nested_forall_info,
6123 : : NULL_TREE, false,
6124 : 293 : (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 : 645 : cnext = cblock->next;
6140 : 1272 : while (cnext)
6141 : : {
6142 : 627 : switch (cnext->op)
6143 : : {
6144 : : /* WHERE assignment statement. */
6145 : 44 : case EXEC_ASSIGN_CALL:
6146 : :
6147 : 44 : arg = cnext->ext.actual;
6148 : 44 : expr1 = expr2 = NULL;
6149 : 132 : for (; arg; arg = arg->next)
6150 : : {
6151 : 88 : if (!arg->expr)
6152 : 0 : continue;
6153 : 88 : if (expr1 == NULL)
6154 : : expr1 = arg->expr;
6155 : : else
6156 : 44 : expr2 = arg->expr;
6157 : : }
6158 : 44 : goto evaluate;
6159 : :
6160 : 527 : case EXEC_ASSIGN:
6161 : 527 : expr1 = cnext->expr1;
6162 : 527 : expr2 = cnext->expr2;
6163 : 571 : evaluate:
6164 : 571 : if (nested_forall_info != NULL)
6165 : : {
6166 : 65 : need_temp = gfc_check_dependency (expr1, expr2, 0);
6167 : 65 : if ((need_temp || flag_test_forall_temp)
6168 : 28 : && cnext->op != EXEC_ASSIGN_CALL)
6169 : 27 : gfc_trans_assign_need_temp (expr1, expr2,
6170 : : cmask, invert,
6171 : : nested_forall_info, block);
6172 : : else
6173 : : {
6174 : : /* Variables to control maskexpr. */
6175 : 38 : count1 = gfc_create_var (gfc_array_index_type, "count1");
6176 : 38 : count2 = gfc_create_var (gfc_array_index_type, "count2");
6177 : 38 : gfc_add_modify (block, count1, gfc_index_zero_node);
6178 : 38 : gfc_add_modify (block, count2, gfc_index_zero_node);
6179 : :
6180 : 38 : tmp = gfc_trans_where_assign (expr1, expr2,
6181 : : cmask, invert,
6182 : : count1, count2,
6183 : : cnext);
6184 : :
6185 : 38 : tmp = gfc_trans_nested_forall_loop (nested_forall_info,
6186 : : tmp, 1);
6187 : 38 : gfc_add_expr_to_block (block, tmp);
6188 : : }
6189 : : }
6190 : : else
6191 : : {
6192 : : /* Variables to control maskexpr. */
6193 : 506 : count1 = gfc_create_var (gfc_array_index_type, "count1");
6194 : 506 : count2 = gfc_create_var (gfc_array_index_type, "count2");
6195 : 506 : gfc_add_modify (block, count1, gfc_index_zero_node);
6196 : 506 : gfc_add_modify (block, count2, gfc_index_zero_node);
6197 : :
6198 : 506 : tmp = gfc_trans_where_assign (expr1, expr2,
6199 : : cmask, invert,
6200 : : count1, count2,
6201 : : cnext);
6202 : 506 : 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 : 56 : case EXEC_WHERE:
6209 : 56 : gfc_trans_where_2 (cnext, cmask, invert,
6210 : : nested_forall_info, block);
6211 : 56 : break;
6212 : :
6213 : 0 : default:
6214 : 0 : gcc_unreachable ();
6215 : : }
6216 : :
6217 : : /* The next statement within the same where-body-construct. */
6218 : 627 : cnext = cnext->next;
6219 : : }
6220 : : /* The next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt. */
6221 : 645 : cblock = cblock->block;
6222 : 645 : 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 : 343 : invert = false;
6234 : 343 : mask = pmask;
6235 : : }
6236 : : }
6237 : :
6238 : : /* If we allocated a pending mask array, deallocate it now. */
6239 : 349 : if (ppmask)
6240 : : {
6241 : 1 : tmp = gfc_call_free (ppmask);
6242 : 1 : gfc_add_expr_to_block (block, tmp);
6243 : : }
6244 : :
6245 : : /* If we allocated a current mask array, deallocate it now. */
6246 : 349 : if (pcmask)
6247 : : {
6248 : 43 : tmp = gfc_call_free (pcmask);
6249 : 43 : gfc_add_expr_to_block (block, tmp);
6250 : : }
6251 : 349 : }
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 : 94 : gfc_trans_where_3 (gfc_code * cblock, gfc_code * eblock)
6260 : : {
6261 : 94 : stmtblock_t block, body;
6262 : 94 : gfc_expr *cond, *tdst, *tsrc, *edst, *esrc;
6263 : 94 : tree tmp, cexpr, tstmt, estmt;
6264 : 94 : gfc_ss *css, *tdss, *tsss;
6265 : 94 : gfc_se cse, tdse, tsse, edse, esse;
6266 : 94 : gfc_loopinfo loop;
6267 : 94 : gfc_ss *edss = 0;
6268 : 94 : gfc_ss *esss = 0;
6269 : 94 : bool maybe_workshare = false;
6270 : :
6271 : : /* Allow the scalarizer to workshare simple where loops. */
6272 : 94 : if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
6273 : : == OMPWS_WORKSHARE_FLAG)
6274 : : {
6275 : 13 : maybe_workshare = true;
6276 : 13 : ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
6277 : : }
6278 : :
6279 : 94 : cond = cblock->expr1;
6280 : 94 : tdst = cblock->next->expr1;
6281 : 94 : tsrc = cblock->next->expr2;
6282 : 94 : edst = eblock ? eblock->next->expr1 : NULL;
6283 : 13 : esrc = eblock ? eblock->next->expr2 : NULL;
6284 : :
6285 : 94 : gfc_start_block (&block);
6286 : 94 : gfc_init_loopinfo (&loop);
6287 : :
6288 : : /* Handle the condition. */
6289 : 94 : gfc_init_se (&cse, NULL);
6290 : 94 : css = gfc_walk_expr (cond);
6291 : 94 : gfc_add_ss_to_loop (&loop, css);
6292 : :
6293 : : /* Handle the then-clause. */
6294 : 94 : gfc_init_se (&tdse, NULL);
6295 : 94 : gfc_init_se (&tsse, NULL);
6296 : 94 : tdss = gfc_walk_expr (tdst);
6297 : 94 : tsss = gfc_walk_expr (tsrc);
6298 : 94 : if (tsss == gfc_ss_terminator)
6299 : : {
6300 : 58 : tsss = gfc_get_scalar_ss (gfc_ss_terminator, tsrc);
6301 : 58 : tsss->info->where = 1;
6302 : : }
6303 : 94 : gfc_add_ss_to_loop (&loop, tdss);
6304 : 94 : gfc_add_ss_to_loop (&loop, tsss);
6305 : :
6306 : 94 : if (eblock)
6307 : : {
6308 : : /* Handle the else clause. */
6309 : 13 : gfc_init_se (&edse, NULL);
6310 : 13 : gfc_init_se (&esse, NULL);
6311 : 13 : edss = gfc_walk_expr (edst);
6312 : 13 : esss = gfc_walk_expr (esrc);
6313 : 13 : if (esss == gfc_ss_terminator)
6314 : : {
6315 : 13 : esss = gfc_get_scalar_ss (gfc_ss_terminator, esrc);
6316 : 13 : esss->info->where = 1;
6317 : : }
6318 : 13 : gfc_add_ss_to_loop (&loop, edss);
6319 : 13 : gfc_add_ss_to_loop (&loop, esss);
6320 : : }
6321 : :
6322 : 94 : gfc_conv_ss_startstride (&loop);
6323 : 94 : gfc_conv_loop_setup (&loop, &tdst->where);
6324 : :
6325 : 94 : gfc_mark_ss_chain_used (css, 1);
6326 : 94 : gfc_mark_ss_chain_used (tdss, 1);
6327 : 94 : gfc_mark_ss_chain_used (tsss, 1);
6328 : 94 : if (eblock)
6329 : : {
6330 : 13 : gfc_mark_ss_chain_used (edss, 1);
6331 : 13 : gfc_mark_ss_chain_used (esss, 1);
6332 : : }
6333 : :
6334 : 94 : gfc_start_scalarized_body (&loop, &body);
6335 : :
6336 : 94 : gfc_copy_loopinfo_to_se (&cse, &loop);
6337 : 94 : gfc_copy_loopinfo_to_se (&tdse, &loop);
6338 : 94 : gfc_copy_loopinfo_to_se (&tsse, &loop);
6339 : 94 : cse.ss = css;
6340 : 94 : tdse.ss = tdss;
6341 : 94 : tsse.ss = tsss;
6342 : 94 : if (eblock)
6343 : : {
6344 : 13 : gfc_copy_loopinfo_to_se (&edse, &loop);
6345 : 13 : gfc_copy_loopinfo_to_se (&esse, &loop);
6346 : 13 : edse.ss = edss;
6347 : 13 : esse.ss = esss;
6348 : : }
6349 : :
6350 : 94 : gfc_conv_expr (&cse, cond);
6351 : 94 : gfc_add_block_to_block (&body, &cse.pre);
6352 : 94 : cexpr = cse.expr;
6353 : :
6354 : 94 : gfc_conv_expr (&tsse, tsrc);
6355 : 94 : if (tdss != gfc_ss_terminator && loop.temp_ss != NULL)
6356 : 0 : gfc_conv_tmp_array_ref (&tdse);
6357 : : else
6358 : 94 : gfc_conv_expr (&tdse, tdst);
6359 : :
6360 : 94 : if (eblock)
6361 : : {
6362 : 13 : gfc_conv_expr (&esse, esrc);
6363 : 13 : if (edss != gfc_ss_terminator && loop.temp_ss != NULL)
6364 : 0 : gfc_conv_tmp_array_ref (&edse);
6365 : : else
6366 : 13 : gfc_conv_expr (&edse, edst);
6367 : : }
6368 : :
6369 : 94 : tstmt = gfc_trans_scalar_assign (&tdse, &tsse, tdst->ts, false, true);
6370 : 94 : estmt = eblock ? gfc_trans_scalar_assign (&edse, &esse, edst->ts,
6371 : : false, true)
6372 : 81 : : build_empty_stmt (input_location);
6373 : 94 : tmp = build3_v (COND_EXPR, cexpr, tstmt, estmt);
6374 : 94 : gfc_add_expr_to_block (&body, tmp);
6375 : 94 : gfc_add_block_to_block (&body, &cse.post);
6376 : :
6377 : 94 : if (maybe_workshare)
6378 : 13 : ompws_flags &= ~OMPWS_SCALARIZER_BODY;
6379 : 94 : gfc_trans_scalarizing_loops (&loop, &body);
6380 : 94 : gfc_add_block_to_block (&block, &loop.pre);
6381 : 94 : gfc_add_block_to_block (&block, &loop.post);
6382 : 94 : gfc_cleanup_loop (&loop);
6383 : :
6384 : 94 : 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 : 341 : gfc_trans_where (gfc_code * code)
6393 : : {
6394 : 341 : stmtblock_t block;
6395 : 341 : gfc_code *cblock;
6396 : 341 : gfc_code *eblock;
6397 : :
6398 : 341 : cblock = code->block;
6399 : 341 : if (cblock->next
6400 : 314 : && cblock->next->op == EXEC_ASSIGN
6401 : 269 : && !cblock->next->next)
6402 : : {
6403 : 267 : eblock = cblock->block;
6404 : 267 : 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 : 162 : if (!gfc_check_dependency (cblock->next->expr1,
6410 : : cblock->expr1, 0)
6411 : 268 : && !gfc_check_dependency (cblock->next->expr1,
6412 : 106 : cblock->next->expr2, 0))
6413 : 81 : return gfc_trans_where_3 (cblock, NULL);
6414 : : }
6415 : 105 : else if (!eblock->expr1
6416 : 35 : && !eblock->block
6417 : 35 : && eblock->next
6418 : 26 : && eblock->next->op == EXEC_ASSIGN
6419 : 25 : && !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 : 25 : if (!gfc_check_dependency (cblock->next->expr1,
6432 : : cblock->expr1, 0)
6433 : 25 : && !gfc_check_dependency (eblock->next->expr1,
6434 : : cblock->expr1, 0)
6435 : 25 : && !gfc_check_dependency (cblock->next->expr1,
6436 : 25 : eblock->next->expr2, 1)
6437 : 19 : && !gfc_check_dependency (eblock->next->expr1,
6438 : 19 : cblock->next->expr2, 1)
6439 : 19 : && !gfc_check_dependency (cblock->next->expr1,
6440 : 19 : cblock->next->expr2, 1)
6441 : 19 : && !gfc_check_dependency (eblock->next->expr1,
6442 : 19 : eblock->next->expr2, 1)
6443 : 19 : && !gfc_check_dependency (cblock->next->expr1,
6444 : 19 : eblock->next->expr1, 0)
6445 : 44 : && !gfc_check_dependency (eblock->next->expr1,
6446 : 19 : cblock->next->expr1, 0))
6447 : 13 : return gfc_trans_where_3 (cblock, eblock);
6448 : : }
6449 : : }
6450 : :
6451 : 247 : gfc_start_block (&block);
6452 : :
6453 : 247 : gfc_trans_where_2 (code, NULL, false, NULL, &block);
6454 : :
6455 : 247 : 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 : 106 : gfc_trans_cycle (gfc_code * code)
6465 : : {
6466 : 106 : tree cycle_label;
6467 : :
6468 : 106 : cycle_label = code->ext.which_construct->cycle_label;
6469 : 106 : gcc_assert (cycle_label);
6470 : :
6471 : 106 : TREE_USED (cycle_label) = 1;
6472 : 106 : 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 : 679 : gfc_trans_exit (gfc_code * code)
6482 : : {
6483 : 679 : tree exit_label;
6484 : :
6485 : 679 : exit_label = code->ext.which_construct->exit_label;
6486 : 679 : gcc_assert (exit_label);
6487 : :
6488 : 679 : TREE_USED (exit_label) = 1;
6489 : 679 : 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 : 12835 : allocate_get_initializer (gfc_code * code, gfc_expr * expr)
6498 : : {
6499 : 12835 : 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 : 3629 : if (code->ext.alloc.ts.type == BT_DERIVED
6504 : 3629 : && (code->ext.alloc.ts.u.derived->attr.alloc_comp
6505 : 497 : || gfc_has_default_initializer (code->ext.alloc.ts.u.derived)))
6506 : 341 : return gfc_default_initializer (&code->ext.alloc.ts);
6507 : :
6508 : 547 : if (gfc_bt_struct (expr->ts.type)
6509 : 3288 : && (expr->ts.u.derived->attr.alloc_comp
6510 : 1868 : || gfc_has_default_initializer (expr->ts.u.derived)))
6511 : 1326 : return gfc_default_initializer (&expr->ts);
6512 : :
6513 : 1962 : if (expr->ts.type == BT_CLASS
6514 : 1962 : && (CLASS_DATA (expr)->ts.u.derived->attr.alloc_comp
6515 : 457 : || gfc_has_default_initializer (CLASS_DATA (expr)->ts.u.derived)))
6516 : 173 : return gfc_default_initializer (&CLASS_DATA (expr)->ts);
6517 : :
6518 : : return NULL;
6519 : : }
6520 : :
6521 : : /* Translate the ALLOCATE statement. */
6522 : :
6523 : : tree
6524 : 13551 : gfc_trans_allocate (gfc_code * code, gfc_omp_namelist *omp_allocate)
6525 : : {
6526 : 13551 : gfc_alloc *al;
6527 : 13551 : gfc_expr *expr, *e3rhs = NULL, *init_expr;
6528 : 13551 : gfc_se se, se_sz;
6529 : 13551 : tree tmp;
6530 : 13551 : tree parm;
6531 : 13551 : tree stat;
6532 : 13551 : tree errmsg;
6533 : 13551 : tree errlen;
6534 : 13551 : tree label_errmsg;
6535 : 13551 : tree label_finish;
6536 : 13551 : tree memsz;
6537 : 13551 : 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 : 13551 : tree expr3, expr3_vptr, expr3_len, expr3_esize;
6544 : : /* Classify what expr3 stores. */
6545 : 13551 : enum { E3_UNSET = 0, E3_SOURCE, E3_MOLD, E3_DESC } e3_is;
6546 : 13551 : stmtblock_t block;
6547 : 13551 : stmtblock_t post;
6548 : 13551 : stmtblock_t final_block;
6549 : 13551 : tree nelems;
6550 : 13551 : bool upoly_expr, tmp_expr3_len_flag = false, al_len_needs_set, is_coarray;
6551 : 13551 : bool needs_caf_sync, caf_refs_comp;
6552 : 13551 : bool e3_has_nodescriptor = false;
6553 : 13551 : gfc_symtree *newsym = NULL;
6554 : 13551 : symbol_attribute caf_attr;
6555 : 13551 : gfc_actual_arglist *param_list;
6556 : 13551 : tree ts_string_length = NULL_TREE;
6557 : :
6558 : 13551 : if (!code->ext.alloc.list)
6559 : : return NULL_TREE;
6560 : :
6561 : 13551 : stat = tmp = memsz = al_vptr = al_len = NULL_TREE;
6562 : 13551 : expr3 = expr3_vptr = expr3_len = expr3_esize = NULL_TREE;
6563 : 13551 : label_errmsg = label_finish = errmsg = errlen = NULL_TREE;
6564 : 13551 : e3_is = E3_UNSET;
6565 : 13551 : is_coarray = needs_caf_sync = false;
6566 : :
6567 : 13551 : gfc_init_block (&block);
6568 : 13551 : gfc_init_block (&post);
6569 : 13551 : gfc_init_block (&final_block);
6570 : :
6571 : : /* STAT= (and maybe ERRMSG=) is present. */
6572 : 13551 : if (code->expr1)
6573 : : {
6574 : : /* STAT=. */
6575 : 266 : tree gfc_int4_type_node = gfc_get_int_type (4);
6576 : 266 : stat = gfc_create_var (gfc_int4_type_node, "stat");
6577 : :
6578 : : /* ERRMSG= only makes sense with STAT=. */
6579 : 266 : if (code->expr2)
6580 : : {
6581 : 68 : gfc_init_se (&se, NULL);
6582 : 68 : se.want_pointer = 1;
6583 : 68 : gfc_conv_expr_lhs (&se, code->expr2);
6584 : 68 : errmsg = se.expr;
6585 : 68 : errlen = se.string_length;
6586 : : }
6587 : : else
6588 : : {
6589 : 198 : errmsg = null_pointer_node;
6590 : 198 : errlen = build_int_cst (gfc_charlen_type_node, 0);
6591 : : }
6592 : :
6593 : : /* GOTO destinations. */
6594 : 266 : label_errmsg = gfc_build_label_decl (NULL_TREE);
6595 : 266 : label_finish = gfc_build_label_decl (NULL_TREE);
6596 : 266 : 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 : 13551 : if (code->expr3)
6605 : : {
6606 : 3680 : bool vtab_needed = false, temp_var_needed = false,
6607 : 3680 : temp_obj_created = false;
6608 : :
6609 : 3680 : is_coarray = gfc_is_coarray (code->expr3);
6610 : :
6611 : 279 : if (code->expr3->expr_type == EXPR_FUNCTION && !code->expr3->mold
6612 : 3923 : && (gfc_is_class_array_function (code->expr3)
6613 : 213 : || gfc_is_alloc_class_scalar_function (code->expr3)))
6614 : 78 : code->expr3->must_finalize = 1;
6615 : :
6616 : : /* Figure whether we need the vtab from expr3. */
6617 : 7372 : for (al = code->ext.alloc.list; !vtab_needed && al != NULL;
6618 : 3692 : al = al->next)
6619 : 3692 : vtab_needed = (al->expr->ts.type == BT_CLASS);
6620 : :
6621 : 3680 : 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 : 3680 : if (code->expr3->expr_type == EXPR_VARIABLE
6627 : 3680 : && is_subref_array (code->expr3))
6628 : 60 : code->expr3 = gfc_get_parentheses (code->expr3);
6629 : :
6630 : 3680 : if (code->expr3->expr_type == EXPR_VARIABLE
6631 : 2426 : || code->expr3->expr_type == EXPR_ARRAY
6632 : 1450 : || code->expr3->expr_type == EXPR_CONSTANT)
6633 : : {
6634 : 2639 : if (!code->expr3->mold
6635 : 232 : || code->expr3->ts.type == BT_CHARACTER
6636 : 104 : || vtab_needed
6637 : 69 : || 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 : 2639 : if (code->ext.alloc.arr_spec_from_expr3 || code->expr3->rank != 0)
6643 : 1526 : gfc_conv_expr_descriptor (&se, code->expr3);
6644 : : else
6645 : : {
6646 : 1113 : 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 : 1113 : if (code->expr3->ts.type == BT_CLASS
6653 : 256 : && TREE_CODE (se.expr) == NOP_EXPR
6654 : 1269 : && (TREE_CODE (TREE_OPERAND (se.expr, 0))
6655 : : == POINTER_PLUS_EXPR
6656 : 138 : || is_coarray))
6657 : 30 : 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 : 2639 : temp_var_needed = TREE_CODE (se.expr) == COMPONENT_REF;
6663 : : }
6664 : : }
6665 : : else
6666 : : {
6667 : : /* In all other cases evaluate the expr3. */
6668 : 1041 : 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 : 1041 : attr = gfc_expr_attr (code->expr3);
6676 : 1041 : if (code->expr3->rank != 0
6677 : 248 : && ((!attr.allocatable && !attr.pointer)
6678 : 71 : || (code->expr3->expr_type == EXPR_FUNCTION
6679 : 71 : && (code->expr3->ts.type != BT_CLASS
6680 : 53 : || (code->expr3->value.function.isym
6681 : 12 : && code->expr3->value.function.isym
6682 : 12 : ->transformational)))))
6683 : 207 : gfc_conv_expr_descriptor (&se, code->expr3);
6684 : : else
6685 : 834 : gfc_conv_expr_reference (&se, code->expr3);
6686 : 1041 : if (code->expr3->ts.type == BT_CLASS)
6687 : 132 : gfc_conv_class_to_class (&se, code->expr3,
6688 : : code->expr3->ts,
6689 : : false, true,
6690 : : false, false);
6691 : 1041 : temp_obj_created = temp_var_needed = !VAR_P (se.expr);
6692 : : }
6693 : 3680 : gfc_add_block_to_block (&block, &se.pre);
6694 : 3680 : if (code->expr3->must_finalize)
6695 : : {
6696 : 78 : gfc_add_block_to_block (&final_block, &se.finalblock);
6697 : 78 : gfc_add_block_to_block (&final_block, &se.post);
6698 : : }
6699 : : else
6700 : 3602 : gfc_add_block_to_block (&post, &se.post);
6701 : :
6702 : : /* Special case when string in expr3 is scalar and has length zero. */
6703 : 3680 : if (code->expr3->ts.type == BT_CHARACTER
6704 : 787 : && code->expr3->rank == 0
6705 : 4174 : && integer_zerop (se.string_length))
6706 : : {
6707 : 6 : gfc_init_se (&se, NULL);
6708 : 6 : temp_var_needed = false;
6709 : 6 : expr3_len = build_zero_cst (gfc_charlen_type_node);
6710 : 6 : e3_is = E3_MOLD;
6711 : : }
6712 : : /* Prevent aliasing, i.e., se.expr may be already a
6713 : : variable declaration. */
6714 : 3674 : else if (se.expr != NULL_TREE && temp_var_needed)
6715 : : {
6716 : 883 : tree var, desc;
6717 : 883 : tmp = (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr))
6718 : 821 : || is_coarray
6719 : 809 : || (code->expr3->ts.type == BT_CHARACTER
6720 : 905 : && code->expr3->rank == 0)) ?
6721 : : se.expr
6722 : 787 : : 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 : 883 : if (code->expr3->ts.type == BT_CLASS
6729 : 187 : && code->expr3->rank != 0)
6730 : : {
6731 : : /* When an array_ref was in expr3, then the descriptor is the
6732 : : first operand. */
6733 : 96 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6734 : : {
6735 : 49 : desc = TREE_OPERAND (tmp, 0);
6736 : : }
6737 : : else
6738 : : {
6739 : 47 : desc = tmp;
6740 : 47 : tmp = gfc_class_data_get (tmp);
6741 : : }
6742 : 96 : if (code->ext.alloc.arr_spec_from_expr3)
6743 : 39 : e3_is = E3_DESC;
6744 : : }
6745 : : else
6746 : 799 : desc = !is_coarray ? se.expr
6747 : 12 : : TREE_OPERAND (TREE_OPERAND (se.expr, 0), 0);
6748 : : /* We need a regular (non-UID) symbol here, therefore give a
6749 : : prefix. */
6750 : 883 : var = gfc_create_var (TREE_TYPE (tmp), "source");
6751 : 883 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)) || is_coarray)
6752 : : {
6753 : 121 : gfc_allocate_lang_decl (var);
6754 : 121 : GFC_DECL_SAVED_DESCRIPTOR (var) = desc;
6755 : : }
6756 : 883 : gfc_add_modify_loc (input_location, &block, var, tmp);
6757 : :
6758 : 883 : expr3 = var;
6759 : 883 : if (se.string_length)
6760 : : /* Evaluate it assuming that it also is complicated like expr3. */
6761 : 29 : expr3_len = gfc_evaluate_now (se.string_length, &block);
6762 : : }
6763 : : else
6764 : : {
6765 : 2791 : expr3 = se.expr;
6766 : 2791 : 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 : 3680 : if ((code->expr3->ts.type == BT_DERIVED
6776 : 2412 : || code->expr3->ts.type == BT_CLASS)
6777 : 1789 : && (code->expr3->expr_type != EXPR_VARIABLE || temp_obj_created)
6778 : 1204 : && code->expr3->ts.u.derived->attr.alloc_comp
6779 : 302 : && !code->expr3->must_finalize
6780 : 296 : && !code->ext.alloc.expr3_not_explicit)
6781 : : {
6782 : 216 : tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived,
6783 : : expr3, code->expr3->rank);
6784 : 216 : gfc_prepend_expr_to_block (&post, tmp);
6785 : : }
6786 : :
6787 : : /* Store what the expr3 is to be used for. */
6788 : 3680 : if (e3_is == E3_UNSET)
6789 : 3635 : e3_is = expr3 != NULL_TREE ?
6790 : 3635 : (code->ext.alloc.arr_spec_from_expr3 ?
6791 : : E3_DESC
6792 : 2538 : : (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 : 3680 : if (code->expr3->ts.type == BT_CLASS)
6800 : : {
6801 : 521 : gfc_expr *rhs;
6802 : 521 : tmp = expr3 != NULL_TREE && POINTER_TYPE_P (TREE_TYPE (expr3)) ?
6803 : 256 : 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 : 521 : if (tmp != NULL_TREE
6808 : 521 : && (e3_is == E3_DESC
6809 : 440 : || (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
6810 : 280 : && (VAR_P (tmp) || !code->expr3->ref))
6811 : 186 : || (VAR_P (tmp) && DECL_LANG_SPECIFIC (tmp))))
6812 : 440 : tmp = gfc_class_vptr_get (expr3);
6813 : : else
6814 : : {
6815 : 81 : rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6816 : 81 : gfc_add_vptr_component (rhs);
6817 : 81 : gfc_init_se (&se, NULL);
6818 : 81 : se.want_pointer = 1;
6819 : 81 : gfc_conv_expr (&se, rhs);
6820 : 81 : tmp = se.expr;
6821 : 81 : gfc_free_expr (rhs);
6822 : : }
6823 : : /* Set the element size. */
6824 : 521 : expr3_esize = gfc_vptr_size_get (tmp);
6825 : 521 : if (vtab_needed)
6826 : 515 : expr3_vptr = tmp;
6827 : : /* Initialize the ref to the _len component. */
6828 : 521 : if (expr3_len == NULL_TREE && UNLIMITED_POLY (code->expr3))
6829 : : {
6830 : : /* Same like for retrieving the _vptr. */
6831 : 164 : if (expr3 != NULL_TREE && !code->expr3->ref)
6832 : 92 : expr3_len = gfc_class_len_get (expr3);
6833 : : else
6834 : : {
6835 : 72 : rhs = gfc_find_and_cut_at_last_class_ref (code->expr3);
6836 : 72 : gfc_add_len_component (rhs);
6837 : 72 : gfc_init_se (&se, NULL);
6838 : 72 : gfc_conv_expr (&se, rhs);
6839 : 72 : expr3_len = se.expr;
6840 : 72 : 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 : 3159 : if (vtab_needed)
6850 : : {
6851 : : /* VPTR is fixed at compile time. */
6852 : 1193 : gfc_symbol *vtab;
6853 : :
6854 : 1193 : vtab = gfc_find_vtab (&code->expr3->ts);
6855 : 1193 : gcc_assert (vtab);
6856 : 1193 : expr3_vptr = gfc_get_symbol_decl (vtab);
6857 : 1193 : 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 : 3159 : if (expr3_len == NULL_TREE
6863 : 2372 : && code->expr3->ts.type == BT_CHARACTER)
6864 : : {
6865 : 0 : if (code->expr3->ts.u.cl
6866 : 0 : && code->expr3->ts.u.cl->length)
6867 : : {
6868 : 0 : gfc_init_se (&se, NULL);
6869 : 0 : gfc_conv_expr (&se, code->expr3->ts.u.cl->length);
6870 : 0 : gfc_add_block_to_block (&block, &se.pre);
6871 : 0 : expr3_len = gfc_evaluate_now (se.expr, &block);
6872 : : }
6873 : 0 : 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 : 3159 : if (code->expr3->ts.type == BT_CHARACTER)
6879 : 787 : expr3_esize = TYPE_SIZE_UNIT (
6880 : : gfc_get_char_type (code->expr3->ts.kind));
6881 : : else
6882 : 2372 : expr3_esize = TYPE_SIZE_UNIT (
6883 : : gfc_typenode_for_spec (&code->expr3->ts));
6884 : : }
6885 : 3680 : gcc_assert (expr3_esize);
6886 : 3680 : expr3_esize = fold_convert (sizetype, expr3_esize);
6887 : 3680 : if (e3_is == E3_MOLD)
6888 : : /* The expr3 is no longer valid after this point. */
6889 : 179 : expr3 = NULL_TREE;
6890 : : }
6891 : 9871 : 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 : 1160 : if (code->ext.alloc.ts.type != BT_CHARACTER)
6896 : 750 : expr3_esize = TYPE_SIZE_UNIT (
6897 : : gfc_typenode_for_spec (&code->ext.alloc.ts));
6898 : 410 : else if (code->ext.alloc.ts.u.cl->length != NULL)
6899 : : {
6900 : 404 : gfc_expr *sz;
6901 : 404 : sz = gfc_copy_expr (code->ext.alloc.ts.u.cl->length);
6902 : 404 : gfc_init_se (&se_sz, NULL);
6903 : 404 : gfc_conv_expr (&se_sz, sz);
6904 : 404 : gfc_free_expr (sz);
6905 : 404 : ts_string_length = fold_convert (gfc_charlen_type_node, se_sz.expr);
6906 : 404 : tmp = gfc_get_char_type (code->ext.alloc.ts.kind);
6907 : 404 : tmp = TYPE_SIZE_UNIT (tmp);
6908 : 404 : tmp = fold_convert (TREE_TYPE (se_sz.expr), tmp);
6909 : 404 : gfc_add_block_to_block (&block, &se_sz.pre);
6910 : 404 : expr3_esize = fold_build2_loc (input_location, MULT_EXPR,
6911 : 404 : TREE_TYPE (se_sz.expr),
6912 : : tmp, se_sz.expr);
6913 : 404 : 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 : 13551 : if (code->expr3)
6935 : : {
6936 : 3680 : if (code->expr3->expr_type != EXPR_VARIABLE
6937 : 2426 : && e3_is != E3_MOLD && expr3 != NULL_TREE
6938 : 6038 : && 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 : 1983 : 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 : 1983 : newsym->name
6947 : 1983 : = gfc_get_string ("%s", IDENTIFIER_POINTER (DECL_NAME (expr3)));
6948 : 1983 : newsym->n.sym = gfc_new_symbol (newsym->name, NULL);
6949 : : /* The backend_decl is known. It is expr3, which is inserted
6950 : : here. */
6951 : 1983 : newsym->n.sym->backend_decl = expr3;
6952 : 1983 : e3rhs = gfc_get_expr ();
6953 : 1983 : e3rhs->rank = code->expr3->rank;
6954 : 1983 : e3rhs->corank = code->expr3->corank;
6955 : 1983 : e3rhs->symtree = newsym;
6956 : : /* Mark the symbol referenced or gfc_trans_assignment will bug. */
6957 : 1983 : newsym->n.sym->attr.referenced = 1;
6958 : 1983 : e3rhs->expr_type = EXPR_VARIABLE;
6959 : 1983 : e3rhs->where = code->expr3->where;
6960 : : /* Set the symbols type, upto it was BT_UNKNOWN. */
6961 : 1983 : if (IS_CLASS_ARRAY (code->expr3)
6962 : 54 : && code->expr3->expr_type == EXPR_FUNCTION
6963 : 42 : && code->expr3->value.function.isym
6964 : 12 : && code->expr3->value.function.isym->transformational)
6965 : : {
6966 : 12 : e3rhs->ts = CLASS_DATA (code->expr3)->ts;
6967 : : }
6968 : 1971 : else if (code->expr3->ts.type == BT_CLASS
6969 : 1971 : && !GFC_CLASS_TYPE_P (TREE_TYPE (expr3)))
6970 : 46 : e3rhs->ts = CLASS_DATA (code->expr3)->ts;
6971 : : else
6972 : 1925 : e3rhs->ts = code->expr3->ts;
6973 : 1983 : newsym->n.sym->ts = e3rhs->ts;
6974 : : /* Check whether the expr3 is array valued. */
6975 : 1983 : if (e3rhs->rank)
6976 : : {
6977 : 1217 : gfc_array_spec *arr;
6978 : 1217 : arr = gfc_get_array_spec ();
6979 : 1217 : arr->rank = e3rhs->rank;
6980 : 1217 : arr->corank = e3rhs->corank;
6981 : 1217 : arr->type = AS_DEFERRED;
6982 : : /* Set the dimension and pointer attribute for arrays
6983 : : to be on the safe side. */
6984 : 1217 : newsym->n.sym->attr.dimension = 1;
6985 : 1217 : newsym->n.sym->attr.pointer = 1;
6986 : 1217 : newsym->n.sym->as = arr;
6987 : 1217 : if (IS_CLASS_ARRAY (code->expr3)
6988 : 54 : && code->expr3->expr_type == EXPR_FUNCTION
6989 : 42 : && code->expr3->value.function.isym
6990 : 12 : && code->expr3->value.function.isym->transformational)
6991 : : {
6992 : 12 : gfc_array_spec *tarr;
6993 : 12 : tarr = gfc_get_array_spec ();
6994 : 12 : *tarr = *arr;
6995 : 12 : e3rhs->ts.u.derived->as = tarr;
6996 : : }
6997 : 1217 : gfc_add_full_array_ref (e3rhs, arr);
6998 : : }
6999 : 766 : else if (POINTER_TYPE_P (TREE_TYPE (expr3)))
7000 : 45 : newsym->n.sym->attr.pointer = 1;
7001 : : /* The string length is known, too. Set it for char arrays. */
7002 : 1983 : if (e3rhs->ts.type == BT_CHARACTER)
7003 : 281 : newsym->n.sym->ts.u.cl->backend_decl = expr3_len;
7004 : 1983 : gfc_commit_symbol (newsym->n.sym);
7005 : : }
7006 : : else
7007 : 1697 : 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 : 3680 : if (e3_is == E3_DESC
7014 : 1136 : && code->expr3->expr_type != EXPR_VARIABLE)
7015 : 13551 : e3_has_nodescriptor = true;
7016 : : }
7017 : :
7018 : : /* Loop over all objects to allocate. */
7019 : 30029 : for (al = code->ext.alloc.list; al != NULL; al = al->next)
7020 : : {
7021 : 16478 : 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 : 16478 : upoly_expr = UNLIMITED_POLY (expr)
7027 : 32363 : || (expr->ts.type == BT_DERIVED
7028 : 2414 : && expr->ts.u.derived->attr.unlimited_polymorphic);
7029 : 16478 : 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 : 16478 : if (expr->ts.type == BT_CLASS)
7035 : : {
7036 : 3422 : gfc_expr *expr_ref_vptr, *expr_ref_len;
7037 : 3422 : gfc_add_data_component (expr);
7038 : : /* Prep the vptr handle. */
7039 : 3422 : expr_ref_vptr = gfc_copy_expr (al->expr);
7040 : 3422 : gfc_add_vptr_component (expr_ref_vptr);
7041 : 3422 : se.want_pointer = 1;
7042 : 3422 : gfc_conv_expr (&se, expr_ref_vptr);
7043 : 3422 : al_vptr = se.expr;
7044 : 3422 : se.want_pointer = 0;
7045 : 3422 : gfc_free_expr (expr_ref_vptr);
7046 : : /* Allocated unlimited polymorphic objects always have a _len
7047 : : component. */
7048 : 3422 : if (upoly_expr)
7049 : : {
7050 : 593 : expr_ref_len = gfc_copy_expr (al->expr);
7051 : 593 : gfc_add_len_component (expr_ref_len);
7052 : 593 : gfc_conv_expr (&se, expr_ref_len);
7053 : 593 : al_len = se.expr;
7054 : 593 : 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 : 16478 : se.want_pointer = 1;
7065 : 16478 : se.descriptor_only = 1;
7066 : :
7067 : 16478 : gfc_conv_expr (&se, expr);
7068 : 16478 : 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 : 995 : al_len = se.string_length;
7072 : :
7073 : 16478 : 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 : 16478 : nelems = NULL_TREE;
7082 : 16478 : if (expr3_len && (code->expr3->ts.type == BT_CHARACTER
7083 : 957 : || 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 : 957 : 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 : 957 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7095 : : {
7096 : 111 : gfc_trans_same_strlen_check ("ALLOCATE with SOURCE= or MOLD=",
7097 : : &code->expr3->where,
7098 : : se.string_length, expr3_len,
7099 : : &block);
7100 : 111 : lhs_len = fold_convert (TREE_TYPE (expr3_len), se.string_length);
7101 : : }
7102 : : else
7103 : : lhs_len = expr3_len;
7104 : :
7105 : 1914 : tmp = fold_build2_loc (input_location, MAX_EXPR,
7106 : 957 : TREE_TYPE (expr3_len),
7107 : 957 : lhs_len, fold_convert (TREE_TYPE (expr3_len),
7108 : : integer_one_node));
7109 : 1914 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7110 : 957 : TREE_TYPE (expr3_esize), expr3_esize,
7111 : 957 : fold_convert (TREE_TYPE (expr3_esize), tmp));
7112 : 957 : }
7113 : : else
7114 : : tmp = expr3_esize;
7115 : :
7116 : : /* Create runtime check for ALLOCATE of character with type-spec. */
7117 : 16478 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred
7118 : 737 : && ts_string_length
7119 : 19 : && se.string_length)
7120 : 19 : gfc_trans_same_strlen_check ("ALLOCATE with type-spec",
7121 : 19 : &al->expr->where,
7122 : : ts_string_length, se.string_length,
7123 : : &block);
7124 : :
7125 : 16478 : gfc_omp_namelist *omp_alloc_item = NULL;
7126 : 16478 : if (omp_allocate)
7127 : : {
7128 : : gfc_omp_namelist *n = NULL;
7129 : : gfc_omp_namelist *n_null = NULL;
7130 : 127 : for (n = omp_allocate; n; n = n->next)
7131 : : {
7132 : 85 : if (n->sym == NULL)
7133 : : {
7134 : 41 : n_null = n;
7135 : 41 : continue;
7136 : : }
7137 : 44 : if (expr->expr_type == EXPR_VARIABLE
7138 : 44 : && expr->symtree->n.sym == n->sym)
7139 : : {
7140 : 22 : gfc_ref *ref;
7141 : 32 : for (ref = expr->ref; ref; ref = ref->next)
7142 : 13 : if (ref->type == REF_COMPONENT)
7143 : : break;
7144 : : if (ref == NULL)
7145 : : break;
7146 : : }
7147 : : }
7148 : 61 : omp_alloc_item = n ? n : n_null;
7149 : :
7150 : : }
7151 : :
7152 : 16478 : 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 : 16478 : 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 : 4962 : if (expr3_len != NULL_TREE)
7170 : : {
7171 : 537 : tree lhs_len;
7172 : 537 : if (expr->ts.type == BT_CHARACTER && !expr->ts.deferred)
7173 : 56 : lhs_len = fold_convert (TREE_TYPE (expr3_len),
7174 : : se.string_length);
7175 : : else
7176 : : lhs_len = expr3_len;
7177 : :
7178 : 537 : tmp = fold_convert (TREE_TYPE (expr3_esize), lhs_len);
7179 : 537 : tmp = fold_build2_loc (input_location, MULT_EXPR,
7180 : 537 : TREE_TYPE (expr3_esize),
7181 : : expr3_esize, tmp);
7182 : 537 : 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 : 98 : memsz = fold_build2_loc (input_location, GT_EXPR,
7196 : : logical_type_node, expr3_len,
7197 : : build_zero_cst
7198 : 98 : (TREE_TYPE (expr3_len)));
7199 : 98 : memsz = fold_build3_loc (input_location, COND_EXPR,
7200 : 98 : TREE_TYPE (expr3_esize),
7201 : : memsz, tmp, expr3_esize);
7202 : : }
7203 : : }
7204 : 4425 : else if (expr3_esize != NULL_TREE)
7205 : : /* Any other object in expr3 just needs element size in
7206 : : bytes. */
7207 : : memsz = expr3_esize;
7208 : 2740 : else if ((expr->ts.type == BT_CHARACTER && expr->ts.deferred)
7209 : 2740 : || (upoly_expr
7210 : 0 : && 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 : 0 : gfc_init_se (&se_sz, NULL);
7217 : 0 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7218 : 0 : gfc_add_block_to_block (&se.pre, &se_sz.pre);
7219 : 0 : se_sz.expr = gfc_evaluate_now (se_sz.expr, &se.pre);
7220 : 0 : gfc_add_block_to_block (&se.pre, &se_sz.post);
7221 : 0 : expr3_len = se_sz.expr;
7222 : 0 : tmp_expr3_len_flag = true;
7223 : 0 : tmp = TYPE_SIZE_UNIT (
7224 : : gfc_get_char_type (code->ext.alloc.ts.kind));
7225 : 0 : memsz = fold_build2_loc (input_location, MULT_EXPR,
7226 : 0 : TREE_TYPE (tmp),
7227 : 0 : fold_convert (TREE_TYPE (tmp),
7228 : : expr3_len),
7229 : : tmp);
7230 : : }
7231 : 2740 : else if (expr->ts.type == BT_CHARACTER)
7232 : : {
7233 : : /* Compute the number of bytes needed to allocate a fixed
7234 : : length char array. */
7235 : 164 : gcc_assert (se.string_length != NULL_TREE);
7236 : 164 : tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr->ts.kind));
7237 : 328 : memsz = fold_build2_loc (input_location, MULT_EXPR,
7238 : 164 : TREE_TYPE (tmp), tmp,
7239 : 164 : fold_convert (TREE_TYPE (tmp),
7240 : : se.string_length));
7241 : : }
7242 : 2576 : else if (code->ext.alloc.ts.type != BT_UNKNOWN)
7243 : : /* Handle all types, where the alloc_type_spec is set. */
7244 : 0 : 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 : 2576 : memsz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (se.expr)));
7248 : :
7249 : 4962 : bool use_coarray_alloc
7250 : 4962 : = (flag_coarray == GFC_FCOARRAY_LIB
7251 : 4962 : && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7252 : 70 : .codimension);
7253 : 4962 : tree omp_cond = NULL_TREE;
7254 : 4962 : tree omp_alt_alloc = NULL_TREE;
7255 : 4962 : tree succ_add_expr = NULL_TREE;
7256 : 4962 : if (!use_coarray_alloc && omp_alloc_item)
7257 : : {
7258 : 28 : tree align, alloc, sz;
7259 : 28 : gfc_se se2;
7260 : :
7261 : 28 : omp_cond = boolean_true_node;
7262 : 28 : if (omp_alloc_item->u2.allocator)
7263 : : {
7264 : 2 : gfc_init_se (&se2, NULL);
7265 : 2 : gfc_conv_expr (&se2, omp_alloc_item->u2.allocator);
7266 : 2 : gfc_add_block_to_block (&se.pre, &se2.pre);
7267 : 2 : alloc = gfc_evaluate_now (se2.expr, &se.pre);
7268 : 2 : gfc_add_block_to_block (&se.pre, &se2.post);
7269 : : }
7270 : : else
7271 : 26 : alloc = build_zero_cst (ptr_type_node);
7272 : 28 : tmp = TREE_TYPE (TREE_TYPE (se.expr));
7273 : 28 : if (tmp == void_type_node)
7274 : 3 : tmp = gfc_typenode_for_spec (&expr->ts, 0);
7275 : 28 : if (omp_alloc_item->u.align)
7276 : : {
7277 : 14 : gfc_init_se (&se2, NULL);
7278 : 14 : gfc_conv_expr (&se2, omp_alloc_item->u.align);
7279 : 14 : gcc_assert (CONSTANT_CLASS_P (se2.expr)
7280 : : && se2.pre.head == NULL
7281 : : && se2.post.head == NULL);
7282 : 14 : align = build_int_cst (size_type_node,
7283 : 14 : MAX (tree_to_uhwi (se2.expr),
7284 : : TYPE_ALIGN_UNIT (tmp)));
7285 : : }
7286 : : else
7287 : 14 : align = build_int_cst (size_type_node, TYPE_ALIGN_UNIT (tmp));
7288 : 28 : sz = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
7289 : : fold_convert (size_type_node, memsz),
7290 : 28 : build_int_cst (size_type_node, 1));
7291 : 28 : omp_alt_alloc = builtin_decl_explicit (BUILT_IN_GOMP_ALLOC);
7292 : 28 : DECL_ATTRIBUTES (omp_alt_alloc)
7293 : 28 : = tree_cons (get_identifier ("omp allocator"),
7294 : : build_tree_list (NULL_TREE, alloc),
7295 : 28 : DECL_ATTRIBUTES (omp_alt_alloc));
7296 : 28 : omp_alt_alloc = build_call_expr (omp_alt_alloc, 3, align, sz, alloc);
7297 : 28 : succ_add_expr = gfc_omp_call_add_alloc (se.expr);
7298 : : }
7299 : :
7300 : : /* Store the caf-attributes for latter use. */
7301 : 4962 : if (use_coarray_alloc)
7302 : : {
7303 : : /* Scalar allocatable components in coarray'ed derived types make
7304 : : it here and are treated now. */
7305 : 65 : tree caf_decl, token;
7306 : 65 : gfc_se caf_se;
7307 : :
7308 : 65 : is_coarray = true;
7309 : : /* Set flag, to add synchronize after the allocate. */
7310 : 130 : needs_caf_sync = needs_caf_sync
7311 : 65 : || caf_attr.coarray_comp || !caf_refs_comp;
7312 : :
7313 : 65 : gfc_init_se (&caf_se, NULL);
7314 : :
7315 : 65 : caf_decl = gfc_get_tree_for_caf_expr (expr);
7316 : 65 : gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl,
7317 : : NULL_TREE, NULL);
7318 : 65 : gfc_add_block_to_block (&se.pre, &caf_se.pre);
7319 : 65 : 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 : 4897 : else if (gfc_expr_attr (expr).allocatable)
7326 : 3352 : 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 : 1545 : 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 : 11516 : if (flag_coarray == GFC_FCOARRAY_LIB
7339 : 11516 : && (caf_attr = gfc_caf_attr (expr, true, &caf_refs_comp))
7340 : 332 : .codimension)
7341 : : {
7342 : 290 : is_coarray = true;
7343 : 290 : needs_caf_sync = needs_caf_sync
7344 : 290 : || caf_attr.coarray_comp || !caf_refs_comp;
7345 : : }
7346 : :
7347 : 11516 : if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7348 : 989 : && expr3_len != NULL_TREE)
7349 : : {
7350 : : /* Arrays need to have a _len set before the array
7351 : : descriptor is filled. */
7352 : 278 : gfc_add_modify (&block, al_len,
7353 : 278 : fold_convert (TREE_TYPE (al_len), expr3_len));
7354 : : /* Prevent setting the length twice. */
7355 : 278 : al_len_needs_set = false;
7356 : : }
7357 : 11238 : else if (expr->ts.type == BT_CHARACTER && al_len != NULL_TREE
7358 : 204 : && 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 : 204 : gfc_init_se (&se_sz, NULL);
7365 : 204 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7366 : 204 : gfc_add_block_to_block (&block, &se_sz.pre);
7367 : 204 : gfc_add_modify (&block, al_len,
7368 : 204 : fold_convert (TREE_TYPE (al_len),
7369 : : se_sz.expr));
7370 : 204 : al_len_needs_set = false;
7371 : : }
7372 : : }
7373 : :
7374 : 16478 : gfc_add_block_to_block (&block, &se.pre);
7375 : :
7376 : : /* Error checking -- Note: ERRMSG only makes sense with STAT. */
7377 : 16478 : if (code->expr1)
7378 : : {
7379 : 278 : tmp = build1_v (GOTO_EXPR, label_errmsg);
7380 : 278 : parm = fold_build2_loc (input_location, NE_EXPR,
7381 : : logical_type_node, stat,
7382 : 278 : build_int_cst (TREE_TYPE (stat), 0));
7383 : 278 : 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 : 278 : 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 : 16478 : if (al_vptr != NULL_TREE && (!code->expr3 || code->expr3->mold))
7392 : : {
7393 : 1661 : if (expr3_vptr != NULL_TREE)
7394 : : /* The vtab is already known, so just assign it. */
7395 : 73 : gfc_add_modify (&block, al_vptr,
7396 : 73 : fold_convert (TREE_TYPE (al_vptr), expr3_vptr));
7397 : : else
7398 : : {
7399 : : /* VPTR is fixed at compile time. */
7400 : 1588 : gfc_symbol *vtab;
7401 : 1588 : gfc_typespec *ts;
7402 : :
7403 : 1588 : 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 : 0 : ts = &code->expr3->ts;
7409 : 1588 : 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 : 705 : ts = &code->ext.alloc.ts;
7414 : : else
7415 : : /* Prepare for setting the vtab as declared. */
7416 : 883 : ts = &expr->ts;
7417 : :
7418 : 1588 : vtab = gfc_find_vtab (ts);
7419 : 1588 : gcc_assert (vtab);
7420 : 1588 : tmp = gfc_build_addr_expr (NULL_TREE,
7421 : : gfc_get_symbol_decl (vtab));
7422 : 1588 : gfc_add_modify (&block, al_vptr,
7423 : 1588 : fold_convert (TREE_TYPE (al_vptr), tmp));
7424 : : }
7425 : : }
7426 : :
7427 : : /* Add assignment for string length. */
7428 : 16478 : if (al_len != NULL_TREE && al_len_needs_set)
7429 : : {
7430 : 1106 : if (expr3_len != NULL_TREE)
7431 : : {
7432 : 568 : gfc_add_modify (&block, al_len,
7433 : 568 : 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 : 568 : 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 : 538 : expr3_len = NULL_TREE;
7444 : : }
7445 : 538 : else if (code->ext.alloc.ts.type == BT_CHARACTER
7446 : 203 : && 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 : 203 : if (expr3_esize == NULL_TREE || code->ext.alloc.ts.kind != 1)
7453 : : {
7454 : 66 : gfc_init_se (&se_sz, NULL);
7455 : 66 : gfc_conv_expr (&se_sz, code->ext.alloc.ts.u.cl->length);
7456 : 66 : gfc_add_block_to_block (&block, &se_sz.pre);
7457 : 66 : gfc_add_modify (&block, al_len,
7458 : 66 : fold_convert (TREE_TYPE (al_len),
7459 : : se_sz.expr));
7460 : : }
7461 : : else
7462 : 137 : gfc_add_modify (&block, al_len,
7463 : 137 : 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 : 335 : gfc_add_modify (&block, al_len,
7470 : 335 : fold_convert (TREE_TYPE (al_len),
7471 : : integer_zero_node));
7472 : : }
7473 : :
7474 : 16478 : init_expr = NULL;
7475 : 16478 : 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 : 3518 : int realloc_lhs = flag_realloc_lhs;
7481 : 3518 : gfc_expr *init_expr = gfc_expr_to_initialize (expr);
7482 : 3518 : gfc_expr *rhs = e3rhs ? e3rhs : gfc_copy_expr (code->expr3);
7483 : 3518 : 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 : 3518 : if (UNLIMITED_POLY (code->expr3)
7490 : 151 : && e3rhs && e3rhs->ts.type == BT_DERIVED
7491 : 6 : && !strcmp (e3rhs->ts.u.derived->name, "STAR"))
7492 : : {
7493 : 6 : gfc_ref *ref;
7494 : 6 : gcc_assert (TREE_CODE (expr3_vptr) == COMPONENT_REF);
7495 : 6 : tmp = gfc_create_var (gfc_typenode_for_spec (&code->expr3->ts),
7496 : : "e3");
7497 : 6 : gfc_add_modify (&block, tmp,
7498 : : gfc_get_class_from_expr (expr3_vptr));
7499 : 6 : rhs->symtree->n.sym->backend_decl = tmp;
7500 : 6 : rhs->ts = code->expr3->ts;
7501 : 6 : rhs->symtree->n.sym->ts = rhs->ts;
7502 : 6 : 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 : 6 : if (code->expr3->rank
7508 : 6 : && ref && ref->next && !ref->next->next)
7509 : : {
7510 : 6 : rhs->ref = gfc_copy_ref (ref);
7511 : 6 : break;
7512 : : }
7513 : 0 : else if ((init_expr->rank && !code->expr3->rank
7514 : 0 : && ref && ref->next && !ref->next->next)
7515 : 0 : || (ref && !ref->next))
7516 : : {
7517 : 0 : rhs->ref = gfc_copy_ref (ref);
7518 : 0 : gfc_free_ref_list (rhs->ref->next);
7519 : 0 : rhs->ref->next = NULL;
7520 : 0 : break;
7521 : : }
7522 : : }
7523 : : }
7524 : :
7525 : : /* Set the symbol to be artificial so that the result is not finalized. */
7526 : 3518 : init_expr->symtree->n.sym->attr.artificial = 1;
7527 : 3518 : tmp = gfc_trans_assignment (init_expr, rhs, true, false, true,
7528 : : false);
7529 : 3518 : init_expr->symtree->n.sym->attr.artificial = 0;
7530 : :
7531 : 3518 : flag_realloc_lhs = realloc_lhs;
7532 : : /* Free the expression allocated for init_expr. */
7533 : 3518 : gfc_free_expr (init_expr);
7534 : 3518 : if (rhs != e3rhs)
7535 : 0 : gfc_free_expr (rhs);
7536 : 3518 : gfc_add_expr_to_block (&block, tmp);
7537 : 3518 : }
7538 : : /* Set KIND and LEN PDT components and allocate those that are
7539 : : parameterized. */
7540 : 12960 : else if (expr->ts.type == BT_DERIVED
7541 : 3168 : && expr->ts.u.derived->attr.pdt_type)
7542 : : {
7543 : 64 : if (code->expr3 && code->expr3->param_list)
7544 : : param_list = code->expr3->param_list;
7545 : 64 : else if (expr->param_list)
7546 : : param_list = expr->param_list;
7547 : : else
7548 : 25 : param_list = expr->symtree->n.sym->param_list;
7549 : 64 : tmp = gfc_allocate_pdt_comp (expr->ts.u.derived, se.expr,
7550 : : expr->rank, param_list);
7551 : 64 : gfc_add_expr_to_block (&block, tmp);
7552 : : }
7553 : : /* Ditto for CLASS expressions. */
7554 : 12896 : else if (expr->ts.type == BT_CLASS
7555 : 586 : && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type)
7556 : : {
7557 : 0 : if (code->expr3 && code->expr3->param_list)
7558 : : param_list = code->expr3->param_list;
7559 : 0 : else if (expr->param_list)
7560 : : param_list = expr->param_list;
7561 : : else
7562 : 0 : param_list = expr->symtree->n.sym->param_list;
7563 : 0 : tmp = gfc_allocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7564 : : se.expr, expr->rank, param_list);
7565 : 0 : gfc_add_expr_to_block (&block, tmp);
7566 : : }
7567 : 12896 : else if (code->expr3 && code->expr3->mold
7568 : 294 : && code->expr3->ts.type == BT_CLASS)
7569 : : {
7570 : : /* Use class_init_assign to initialize expr. */
7571 : 61 : gfc_code *ini;
7572 : 61 : ini = gfc_get_code (EXEC_ALLOCATE);
7573 : 61 : ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr, true);
7574 : 61 : tmp = gfc_trans_class_init_assign (ini);
7575 : 61 : gfc_free_statements (ini);
7576 : 61 : if (tmp != NULL_TREE)
7577 : 61 : gfc_add_expr_to_block (&block, tmp);
7578 : : }
7579 : 12835 : else if ((init_expr = allocate_get_initializer (code, expr)))
7580 : : {
7581 : : /* Use class_init_assign to initialize expr. */
7582 : 1840 : gfc_code *ini;
7583 : 1840 : int realloc_lhs = flag_realloc_lhs;
7584 : 1840 : ini = gfc_get_code (EXEC_INIT_ASSIGN);
7585 : 1840 : ini->expr1 = gfc_expr_to_initialize (expr);
7586 : 1840 : ini->expr2 = init_expr;
7587 : 1840 : flag_realloc_lhs = 0;
7588 : 1840 : tmp= gfc_trans_init_assign (ini);
7589 : 1840 : flag_realloc_lhs = realloc_lhs;
7590 : 1840 : gfc_free_statements (ini);
7591 : : /* Init_expr is freeed by above free_statements, just need to null
7592 : : it here. */
7593 : 1840 : init_expr = NULL;
7594 : 1840 : 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 : 16478 : if (is_coarray)
7600 : : {
7601 : 394 : gfc_symbol *type = NULL;
7602 : 394 : symbol_attribute caf_attr;
7603 : 394 : int rank = 0;
7604 : 394 : if (code->ext.alloc.ts.type == BT_DERIVED
7605 : 9 : && code->ext.alloc.ts.u.derived->attr.pointer_comp)
7606 : : {
7607 : 0 : type = code->ext.alloc.ts.u.derived;
7608 : 0 : rank = type->attr.dimension ? type->as->rank : 0;
7609 : 0 : gfc_clear_attr (&caf_attr);
7610 : : }
7611 : 394 : else if (expr->ts.type == BT_DERIVED
7612 : 95 : && expr->ts.u.derived->attr.pointer_comp)
7613 : : {
7614 : 14 : type = expr->ts.u.derived;
7615 : 14 : rank = expr->rank;
7616 : 14 : caf_attr = gfc_caf_attr (expr, true);
7617 : : }
7618 : :
7619 : : /* Initialize the tokens of pointer components in derived type
7620 : : coarrays. */
7621 : 14 : if (type)
7622 : : {
7623 : 28 : tmp = (caf_attr.codimension && !caf_attr.dimension)
7624 : 14 : ? gfc_conv_descriptor_data_get (se.expr) : se.expr;
7625 : 14 : tmp = gfc_nullify_alloc_comp (type, tmp, rank,
7626 : : GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
7627 : 14 : gfc_add_expr_to_block (&block, tmp);
7628 : : }
7629 : : }
7630 : :
7631 : 16478 : gfc_free_expr (expr);
7632 : : } // for-loop
7633 : :
7634 : 13551 : if (e3rhs)
7635 : : {
7636 : 3680 : if (newsym)
7637 : : {
7638 : 1983 : gfc_free_symbol (newsym->n.sym);
7639 : 1983 : XDELETE (newsym);
7640 : : }
7641 : 3680 : gfc_free_expr (e3rhs);
7642 : : }
7643 : : /* STAT. */
7644 : 13551 : if (code->expr1)
7645 : : {
7646 : 266 : tmp = build1_v (LABEL_EXPR, label_errmsg);
7647 : 266 : gfc_add_expr_to_block (&block, tmp);
7648 : : }
7649 : :
7650 : : /* ERRMSG - only useful if STAT is present. */
7651 : 13551 : if (code->expr1 && code->expr2)
7652 : : {
7653 : 68 : const char *msg = "Attempt to allocate an allocated object";
7654 : 68 : const char *oommsg = "Insufficient virtual memory";
7655 : 68 : tree slen, dlen, errmsg_str, oom_str, oom_loc;
7656 : 68 : stmtblock_t errmsg_block;
7657 : :
7658 : 68 : gfc_init_block (&errmsg_block);
7659 : :
7660 : 68 : errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
7661 : 68 : gfc_add_modify (&errmsg_block, errmsg_str,
7662 : : gfc_build_addr_expr (pchar_type_node,
7663 : : gfc_build_localized_cstring_const (msg)));
7664 : :
7665 : 68 : slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
7666 : 68 : dlen = gfc_get_expr_charlen (code->expr2);
7667 : 68 : slen = fold_build2_loc (input_location, MIN_EXPR,
7668 : 68 : TREE_TYPE (slen), dlen, slen);
7669 : :
7670 : 68 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7671 : 68 : code->expr2->ts.kind,
7672 : : slen, errmsg_str,
7673 : : gfc_default_character_kind);
7674 : 68 : dlen = gfc_finish_block (&errmsg_block);
7675 : :
7676 : 68 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7677 : 68 : stat, build_int_cst (TREE_TYPE (stat),
7678 : 68 : LIBERROR_ALLOCATION));
7679 : :
7680 : 68 : tmp = build3_v (COND_EXPR, tmp,
7681 : : dlen, build_empty_stmt (input_location));
7682 : :
7683 : 68 : gfc_add_expr_to_block (&block, tmp);
7684 : :
7685 : 68 : oom_str = gfc_create_var (pchar_type_node, "OOMMSG");
7686 : 68 : oom_loc = gfc_build_localized_cstring_const (oommsg);
7687 : 68 : gfc_add_modify (&errmsg_block, oom_str,
7688 : : gfc_build_addr_expr (pchar_type_node, oom_loc));
7689 : :
7690 : 68 : slen = build_int_cst (gfc_charlen_type_node, strlen (oommsg));
7691 : 68 : dlen = gfc_get_expr_charlen (code->expr2);
7692 : 68 : slen = fold_build2_loc (input_location, MIN_EXPR,
7693 : 68 : TREE_TYPE (slen), dlen, slen);
7694 : :
7695 : 68 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg,
7696 : 68 : code->expr2->ts.kind,
7697 : : slen, oom_str,
7698 : : gfc_default_character_kind);
7699 : 68 : dlen = gfc_finish_block (&errmsg_block);
7700 : :
7701 : 68 : tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
7702 : 68 : stat, build_int_cst (TREE_TYPE (stat),
7703 : 68 : LIBERROR_NO_MEMORY));
7704 : :
7705 : 68 : tmp = build3_v (COND_EXPR, tmp,
7706 : : dlen, build_empty_stmt (input_location));
7707 : :
7708 : 68 : gfc_add_expr_to_block (&block, tmp);
7709 : : }
7710 : :
7711 : : /* STAT block. */
7712 : 13551 : if (code->expr1)
7713 : : {
7714 : 266 : if (TREE_USED (label_finish))
7715 : : {
7716 : 12 : tmp = build1_v (LABEL_EXPR, label_finish);
7717 : 12 : gfc_add_expr_to_block (&block, tmp);
7718 : : }
7719 : :
7720 : 266 : gfc_init_se (&se, NULL);
7721 : 266 : gfc_conv_expr_lhs (&se, code->expr1);
7722 : 266 : tmp = convert (TREE_TYPE (se.expr), stat);
7723 : 266 : gfc_add_modify (&block, se.expr, tmp);
7724 : : }
7725 : :
7726 : 13551 : if (needs_caf_sync)
7727 : : {
7728 : : /* Add a sync all after the allocation has been executed. */
7729 : 141 : tree zero_size = build_zero_cst (size_type_node);
7730 : 141 : tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_sync_all,
7731 : : 3, null_pointer_node, null_pointer_node,
7732 : : zero_size);
7733 : 141 : gfc_add_expr_to_block (&post, tmp);
7734 : : }
7735 : :
7736 : 13551 : gfc_add_block_to_block (&block, &se.post);
7737 : 13551 : gfc_add_block_to_block (&block, &post);
7738 : 13551 : if (code->expr3 && code->expr3->must_finalize)
7739 : 78 : gfc_add_block_to_block (&block, &final_block);
7740 : :
7741 : 13551 : return gfc_finish_block (&block);
7742 : : }
7743 : :
7744 : :
7745 : : /* Translate a DEALLOCATE statement. */
7746 : :
7747 : : tree
7748 : 7823 : gfc_trans_deallocate (gfc_code *code)
7749 : : {
7750 : 7823 : gfc_se se;
7751 : 7823 : gfc_alloc *al;
7752 : 7823 : tree apstat, pstat, stat, errmsg, errlen, tmp;
7753 : 7823 : tree label_finish, label_errmsg;
7754 : 7823 : stmtblock_t block;
7755 : :
7756 : 7823 : pstat = apstat = stat = errmsg = errlen = tmp = NULL_TREE;
7757 : 7823 : label_finish = label_errmsg = NULL_TREE;
7758 : :
7759 : 7823 : 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 : 7823 : if (code->expr1)
7765 : : {
7766 : 2127 : tree gfc_int4_type_node = gfc_get_int_type (4);
7767 : :
7768 : 2127 : stat = gfc_create_var (gfc_int4_type_node, "stat");
7769 : 2127 : pstat = gfc_build_addr_expr (NULL_TREE, stat);
7770 : :
7771 : : /* GOTO destinations. */
7772 : 2127 : label_errmsg = gfc_build_label_decl (NULL_TREE);
7773 : 2127 : label_finish = gfc_build_label_decl (NULL_TREE);
7774 : 2127 : TREE_USED (label_finish) = 0;
7775 : : }
7776 : :
7777 : : /* Set ERRMSG - only needed if STAT is available. */
7778 : 7823 : if (code->expr1 && code->expr2)
7779 : : {
7780 : 51 : gfc_init_se (&se, NULL);
7781 : 51 : se.want_pointer = 1;
7782 : 51 : gfc_conv_expr_lhs (&se, code->expr2);
7783 : 51 : errmsg = se.expr;
7784 : 51 : errlen = se.string_length;
7785 : : }
7786 : :
7787 : 17762 : for (al = code->ext.alloc.list; al != NULL; al = al->next)
7788 : : {
7789 : 9939 : gfc_expr *expr = gfc_copy_expr (al->expr);
7790 : 9939 : bool is_coarray = false, is_coarray_array = false;
7791 : 9939 : int caf_mode = 0;
7792 : :
7793 : 9939 : gcc_assert (expr->expr_type == EXPR_VARIABLE);
7794 : :
7795 : 9939 : if (expr->ts.type == BT_CLASS)
7796 : 1905 : gfc_add_data_component (expr);
7797 : :
7798 : 9939 : gfc_init_se (&se, NULL);
7799 : 9939 : gfc_start_block (&se.pre);
7800 : :
7801 : 9939 : se.want_pointer = 1;
7802 : 9939 : se.descriptor_only = 1;
7803 : 9939 : gfc_conv_expr (&se, expr);
7804 : :
7805 : : /* Deallocate PDT components that are parameterized. */
7806 : 9939 : tmp = NULL;
7807 : 9939 : if (expr->ts.type == BT_DERIVED
7808 : 2486 : && expr->ts.u.derived->attr.pdt_type
7809 : 59 : && expr->symtree->n.sym->param_list)
7810 : 39 : tmp = gfc_deallocate_pdt_comp (expr->ts.u.derived, se.expr, expr->rank);
7811 : 9900 : else if (expr->ts.type == BT_CLASS
7812 : 1063 : && CLASS_DATA (expr)->ts.u.derived->attr.pdt_type
7813 : 0 : && expr->symtree->n.sym->param_list)
7814 : 0 : tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr)->ts.u.derived,
7815 : : se.expr, expr->rank);
7816 : :
7817 : 39 : if (tmp)
7818 : 39 : gfc_add_expr_to_block (&block, tmp);
7819 : :
7820 : 9939 : if (flag_coarray == GFC_FCOARRAY_LIB
7821 : 9939 : || flag_coarray == GFC_FCOARRAY_SINGLE)
7822 : : {
7823 : 387 : bool comp_ref;
7824 : 387 : symbol_attribute caf_attr = gfc_caf_attr (expr, false, &comp_ref);
7825 : 387 : if (caf_attr.codimension)
7826 : : {
7827 : 274 : is_coarray = true;
7828 : 126 : is_coarray_array = caf_attr.dimension || !comp_ref
7829 : 326 : || caf_attr.coarray_comp;
7830 : :
7831 : 274 : 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 : 169 : caf_mode = GFC_STRUCTURE_CAF_MODE_IN_COARRAY
7836 : 169 : | (comp_ref && !caf_attr.coarray_comp
7837 : : ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0);
7838 : : }
7839 : : }
7840 : :
7841 : 9939 : if (expr->rank || is_coarray_array)
7842 : : {
7843 : 6754 : gfc_ref *ref;
7844 : :
7845 : 5696 : if (gfc_bt_struct (expr->ts.type)
7846 : 1058 : && expr->ts.u.derived->attr.alloc_comp
7847 : 7224 : && !gfc_is_finalizable (expr->ts.u.derived, NULL))
7848 : : {
7849 : 467 : gfc_ref *last = NULL;
7850 : :
7851 : 1267 : for (ref = expr->ref; ref; ref = ref->next)
7852 : 800 : if (ref->type == REF_COMPONENT)
7853 : 285 : last = ref;
7854 : :
7855 : : /* Do not deallocate the components of a derived type
7856 : : ultimate pointer component. */
7857 : 467 : if (!(last && last->u.c.component->attr.pointer)
7858 : 209 : && !(!last && expr->symtree->n.sym->attr.pointer))
7859 : : {
7860 : 26 : if (is_coarray && expr->rank == 0
7861 : 18 : && (!last || !last->u.c.component->attr.dimension)
7862 : 461 : && 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 : 18 : tmp = gfc_conv_descriptor_data_get (se.expr);
7868 : : }
7869 : : else
7870 : 425 : tmp = se.expr;
7871 : 443 : tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, tmp,
7872 : : expr->rank, caf_mode);
7873 : 443 : gfc_add_expr_to_block (&se.pre, tmp);
7874 : : }
7875 : : }
7876 : :
7877 : 6754 : if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
7878 : : {
7879 : 6715 : gfc_coarray_deregtype caf_dtype;
7880 : :
7881 : 6715 : if (is_coarray)
7882 : 234 : caf_dtype = gfc_caf_is_dealloc_only (caf_mode)
7883 : 234 : ? GFC_CAF_COARRAY_DEALLOCATE_ONLY
7884 : : : GFC_CAF_COARRAY_DEREGISTER;
7885 : : else
7886 : : caf_dtype = GFC_CAF_COARRAY_NOCOARRAY;
7887 : 6715 : tmp = gfc_deallocate_with_status (se.expr, pstat, errmsg, errlen,
7888 : : label_finish, false, expr,
7889 : : caf_dtype);
7890 : 6715 : gfc_add_expr_to_block (&se.pre, tmp);
7891 : : }
7892 : 39 : else if (TREE_CODE (se.expr) == COMPONENT_REF
7893 : 39 : && TREE_CODE (TREE_TYPE (se.expr)) == ARRAY_TYPE
7894 : 78 : && 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 : 45 : for (ref = expr->ref; ref; ref = ref->next)
7903 : : {
7904 : 45 : if (ref->u.c.component->attr.dimension
7905 : 39 : && ref->u.c.component->ts.type == BT_DERIVED)
7906 : : break;
7907 : : }
7908 : :
7909 : 39 : if (ref && ref->u.c.component->ts.u.derived->attr.alloc_comp
7910 : 78 : && !gfc_is_finalizable (ref->u.c.component->ts.u.derived,
7911 : : NULL))
7912 : : {
7913 : 39 : tmp = gfc_deallocate_alloc_comp
7914 : 39 : (ref->u.c.component->ts.u.derived,
7915 : : se.expr, expr->rank);
7916 : 39 : gfc_add_expr_to_block (&se.pre, tmp);
7917 : : }
7918 : : }
7919 : :
7920 : 6754 : if (al->expr->ts.type == BT_CLASS)
7921 : : {
7922 : 1072 : gfc_reset_vptr (&se.pre, al->expr);
7923 : 1072 : if (UNLIMITED_POLY (al->expr)
7924 : 731 : || (al->expr->ts.type == BT_DERIVED
7925 : 0 : && al->expr->ts.u.derived->attr.unlimited_polymorphic))
7926 : : /* Clear _len, too. */
7927 : 341 : gfc_reset_len (&se.pre, al->expr);
7928 : : }
7929 : : }
7930 : : else
7931 : : {
7932 : 6370 : tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish,
7933 : : false, al->expr,
7934 : 3185 : al->expr->ts, NULL_TREE,
7935 : : is_coarray);
7936 : 3185 : gfc_add_expr_to_block (&se.pre, tmp);
7937 : :
7938 : : /* Set to zero after deallocation. */
7939 : 3185 : tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
7940 : : se.expr,
7941 : 3185 : build_int_cst (TREE_TYPE (se.expr), 0));
7942 : 3185 : gfc_add_expr_to_block (&se.pre, tmp);
7943 : :
7944 : 3185 : if (al->expr->ts.type == BT_CLASS)
7945 : : {
7946 : 833 : gfc_reset_vptr (&se.pre, al->expr);
7947 : 833 : if (UNLIMITED_POLY (al->expr)
7948 : 613 : || (al->expr->ts.type == BT_DERIVED
7949 : 0 : && al->expr->ts.u.derived->attr.unlimited_polymorphic))
7950 : : /* Clear _len, too. */
7951 : 220 : gfc_reset_len (&se.pre, al->expr);
7952 : : }
7953 : : }
7954 : :
7955 : 9939 : if (code->expr1)
7956 : : {
7957 : 2166 : tree cond;
7958 : :
7959 : 2166 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
7960 : 2166 : build_int_cst (TREE_TYPE (stat), 0));
7961 : 2166 : 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 : 2166 : gfc_add_expr_to_block (&se.pre, tmp);
7966 : : }
7967 : :
7968 : 9939 : tmp = gfc_finish_block (&se.pre);
7969 : 9939 : gfc_add_expr_to_block (&block, tmp);
7970 : 9939 : gfc_free_expr (expr);
7971 : : }
7972 : :
7973 : 7823 : if (code->expr1)
7974 : : {
7975 : 2127 : tmp = build1_v (LABEL_EXPR, label_errmsg);
7976 : 2127 : gfc_add_expr_to_block (&block, tmp);
7977 : : }
7978 : :
7979 : : /* Set ERRMSG - only needed if STAT is available. */
7980 : 7823 : if (code->expr1 && code->expr2)
7981 : : {
7982 : 51 : const char *msg = "Attempt to deallocate an unallocated object";
7983 : 51 : stmtblock_t errmsg_block;
7984 : 51 : tree errmsg_str, slen, dlen, cond;
7985 : :
7986 : 51 : gfc_init_block (&errmsg_block);
7987 : :
7988 : 51 : errmsg_str = gfc_create_var (pchar_type_node, "ERRMSG");
7989 : 51 : gfc_add_modify (&errmsg_block, errmsg_str,
7990 : : gfc_build_addr_expr (pchar_type_node,
7991 : : gfc_build_localized_cstring_const (msg)));
7992 : 51 : slen = build_int_cst (gfc_charlen_type_node, strlen (msg));
7993 : 51 : dlen = gfc_get_expr_charlen (code->expr2);
7994 : :
7995 : 51 : gfc_trans_string_copy (&errmsg_block, dlen, errmsg, code->expr2->ts.kind,
7996 : : slen, errmsg_str, gfc_default_character_kind);
7997 : 51 : tmp = gfc_finish_block (&errmsg_block);
7998 : :
7999 : 51 : cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, stat,
8000 : 51 : build_int_cst (TREE_TYPE (stat), 0));
8001 : 51 : 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 : 51 : gfc_add_expr_to_block (&block, tmp);
8006 : : }
8007 : :
8008 : 7823 : if (code->expr1 && TREE_USED (label_finish))
8009 : : {
8010 : 8 : tmp = build1_v (LABEL_EXPR, label_finish);
8011 : 8 : gfc_add_expr_to_block (&block, tmp);
8012 : : }
8013 : :
8014 : : /* Set STAT. */
8015 : 7823 : if (code->expr1)
8016 : : {
8017 : 2127 : gfc_init_se (&se, NULL);
8018 : 2127 : gfc_conv_expr_lhs (&se, code->expr1);
8019 : 2127 : tmp = convert (TREE_TYPE (se.expr), stat);
8020 : 2127 : gfc_add_modify (&block, se.expr, tmp);
8021 : : }
8022 : :
8023 : 7823 : return gfc_finish_block (&block);
8024 : : }
8025 : :
8026 : : #include "gt-fortran-trans-stmt.h"
|