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