Merged with mainline at revision 128810.
[official-gcc.git] / gcc / fortran / trans-expr.c
blobdff1fd8fcc148727e43f0336b9360e270d13cac2
1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
3 Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "convert.h"
30 #include "ggc.h"
31 #include "toplev.h"
32 #include "real.h"
33 #include "tree-gimple.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "gfortran.h"
37 #include "trans.h"
38 #include "trans-const.h"
39 #include "trans-types.h"
40 #include "trans-array.h"
41 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
42 #include "trans-stmt.h"
43 #include "dependency.h"
45 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
46 static int gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
47 gfc_expr *);
49 /* Copy the scalarization loop variables. */
51 static void
52 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
54 dest->ss = src->ss;
55 dest->loop = src->loop;
59 /* Initialize a simple expression holder.
61 Care must be taken when multiple se are created with the same parent.
62 The child se must be kept in sync. The easiest way is to delay creation
63 of a child se until after after the previous se has been translated. */
65 void
66 gfc_init_se (gfc_se * se, gfc_se * parent)
68 memset (se, 0, sizeof (gfc_se));
69 gfc_init_block (&se->pre);
70 gfc_init_block (&se->post);
72 se->parent = parent;
74 if (parent)
75 gfc_copy_se_loopvars (se, parent);
79 /* Advances to the next SS in the chain. Use this rather than setting
80 se->ss = se->ss->next because all the parents needs to be kept in sync.
81 See gfc_init_se. */
83 void
84 gfc_advance_se_ss_chain (gfc_se * se)
86 gfc_se *p;
88 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
90 p = se;
91 /* Walk down the parent chain. */
92 while (p != NULL)
94 /* Simple consistency check. */
95 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
97 p->ss = p->ss->next;
99 p = p->parent;
104 /* Ensures the result of the expression as either a temporary variable
105 or a constant so that it can be used repeatedly. */
107 void
108 gfc_make_safe_expr (gfc_se * se)
110 tree var;
112 if (CONSTANT_CLASS_P (se->expr))
113 return;
115 /* We need a temporary for this result. */
116 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
117 gfc_add_modify_expr (&se->pre, var, se->expr);
118 se->expr = var;
122 /* Return an expression which determines if a dummy parameter is present.
123 Also used for arguments to procedures with multiple entry points. */
125 tree
126 gfc_conv_expr_present (gfc_symbol * sym)
128 tree decl;
130 gcc_assert (sym->attr.dummy);
132 decl = gfc_get_symbol_decl (sym);
133 if (TREE_CODE (decl) != PARM_DECL)
135 /* Array parameters use a temporary descriptor, we want the real
136 parameter. */
137 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
138 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
139 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
141 return build2 (NE_EXPR, boolean_type_node, decl,
142 fold_convert (TREE_TYPE (decl), null_pointer_node));
146 /* Converts a missing, dummy argument into a null or zero. */
148 void
149 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts)
151 tree present;
152 tree tmp;
154 present = gfc_conv_expr_present (arg->symtree->n.sym);
155 tmp = build3 (COND_EXPR, TREE_TYPE (se->expr), present, se->expr,
156 fold_convert (TREE_TYPE (se->expr), integer_zero_node));
158 tmp = gfc_evaluate_now (tmp, &se->pre);
159 se->expr = tmp;
160 if (ts.type == BT_CHARACTER)
162 tmp = build_int_cst (gfc_charlen_type_node, 0);
163 tmp = build3 (COND_EXPR, gfc_charlen_type_node, present,
164 se->string_length, tmp);
165 tmp = gfc_evaluate_now (tmp, &se->pre);
166 se->string_length = tmp;
168 return;
172 /* Get the character length of an expression, looking through gfc_refs
173 if necessary. */
175 tree
176 gfc_get_expr_charlen (gfc_expr *e)
178 gfc_ref *r;
179 tree length;
181 gcc_assert (e->expr_type == EXPR_VARIABLE
182 && e->ts.type == BT_CHARACTER);
184 length = NULL; /* To silence compiler warning. */
186 if (is_subref_array (e) && e->ts.cl->length)
188 gfc_se tmpse;
189 gfc_init_se (&tmpse, NULL);
190 gfc_conv_expr_type (&tmpse, e->ts.cl->length, gfc_charlen_type_node);
191 e->ts.cl->backend_decl = tmpse.expr;
192 return tmpse.expr;
195 /* First candidate: if the variable is of type CHARACTER, the
196 expression's length could be the length of the character
197 variable. */
198 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
199 length = e->symtree->n.sym->ts.cl->backend_decl;
201 /* Look through the reference chain for component references. */
202 for (r = e->ref; r; r = r->next)
204 switch (r->type)
206 case REF_COMPONENT:
207 if (r->u.c.component->ts.type == BT_CHARACTER)
208 length = r->u.c.component->ts.cl->backend_decl;
209 break;
211 case REF_ARRAY:
212 /* Do nothing. */
213 break;
215 default:
216 /* We should never got substring references here. These will be
217 broken down by the scalarizer. */
218 gcc_unreachable ();
219 break;
223 gcc_assert (length != NULL);
224 return length;
229 /* Generate code to initialize a string length variable. Returns the
230 value. */
232 void
233 gfc_conv_string_length (gfc_charlen * cl, stmtblock_t * pblock)
235 gfc_se se;
237 gfc_init_se (&se, NULL);
238 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
239 se.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, se.expr,
240 build_int_cst (gfc_charlen_type_node, 0));
241 gfc_add_block_to_block (pblock, &se.pre);
243 if (cl->backend_decl)
244 gfc_add_modify_expr (pblock, cl->backend_decl, se.expr);
245 else
246 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
250 static void
251 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
252 const char *name, locus *where)
254 tree tmp;
255 tree type;
256 tree var;
257 tree fault;
258 gfc_se start;
259 gfc_se end;
260 char *msg;
262 type = gfc_get_character_type (kind, ref->u.ss.length);
263 type = build_pointer_type (type);
265 var = NULL_TREE;
266 gfc_init_se (&start, se);
267 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
268 gfc_add_block_to_block (&se->pre, &start.pre);
270 if (integer_onep (start.expr))
271 gfc_conv_string_parameter (se);
272 else
274 /* Avoid multiple evaluation of substring start. */
275 if (!CONSTANT_CLASS_P (start.expr) && !DECL_P (start.expr))
276 start.expr = gfc_evaluate_now (start.expr, &se->pre);
278 /* Change the start of the string. */
279 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
280 tmp = se->expr;
281 else
282 tmp = build_fold_indirect_ref (se->expr);
283 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
284 se->expr = gfc_build_addr_expr (type, tmp);
287 /* Length = end + 1 - start. */
288 gfc_init_se (&end, se);
289 if (ref->u.ss.end == NULL)
290 end.expr = se->string_length;
291 else
293 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
294 gfc_add_block_to_block (&se->pre, &end.pre);
296 if (!CONSTANT_CLASS_P (end.expr) && !DECL_P (end.expr))
297 end.expr = gfc_evaluate_now (end.expr, &se->pre);
299 if (flag_bounds_check)
301 tree nonempty = fold_build2 (LE_EXPR, boolean_type_node,
302 start.expr, end.expr);
304 /* Check lower bound. */
305 fault = fold_build2 (LT_EXPR, boolean_type_node, start.expr,
306 build_int_cst (gfc_charlen_type_node, 1));
307 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
308 nonempty, fault);
309 if (name)
310 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
311 "is less than one", name);
312 else
313 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
314 "is less than one");
315 gfc_trans_runtime_check (fault, &se->pre, where, msg,
316 fold_convert (long_integer_type_node,
317 start.expr));
318 gfc_free (msg);
320 /* Check upper bound. */
321 fault = fold_build2 (GT_EXPR, boolean_type_node, end.expr,
322 se->string_length);
323 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
324 nonempty, fault);
325 if (name)
326 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
327 "exceeds string length (%%ld)", name);
328 else
329 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
330 "exceeds string length (%%ld)");
331 gfc_trans_runtime_check (fault, &se->pre, where, msg,
332 fold_convert (long_integer_type_node, end.expr),
333 fold_convert (long_integer_type_node,
334 se->string_length));
335 gfc_free (msg);
338 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node,
339 build_int_cst (gfc_charlen_type_node, 1),
340 start.expr);
341 tmp = fold_build2 (PLUS_EXPR, gfc_charlen_type_node, end.expr, tmp);
342 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
343 build_int_cst (gfc_charlen_type_node, 0));
344 se->string_length = tmp;
348 /* Convert a derived type component reference. */
350 static void
351 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
353 gfc_component *c;
354 tree tmp;
355 tree decl;
356 tree field;
358 c = ref->u.c.component;
360 gcc_assert (c->backend_decl);
362 field = c->backend_decl;
363 gcc_assert (TREE_CODE (field) == FIELD_DECL);
364 decl = se->expr;
365 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field, NULL_TREE);
367 se->expr = tmp;
369 if (c->ts.type == BT_CHARACTER)
371 tmp = c->ts.cl->backend_decl;
372 /* Components must always be constant length. */
373 gcc_assert (tmp && INTEGER_CST_P (tmp));
374 se->string_length = tmp;
377 if (c->pointer && c->dimension == 0 && c->ts.type != BT_CHARACTER)
378 se->expr = build_fold_indirect_ref (se->expr);
382 /* Return the contents of a variable. Also handles reference/pointer
383 variables (all Fortran pointer references are implicit). */
385 static void
386 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
388 gfc_ref *ref;
389 gfc_symbol *sym;
390 tree parent_decl;
391 int parent_flag;
392 bool return_value;
393 bool alternate_entry;
394 bool entry_master;
396 sym = expr->symtree->n.sym;
397 if (se->ss != NULL)
399 /* Check that something hasn't gone horribly wrong. */
400 gcc_assert (se->ss != gfc_ss_terminator);
401 gcc_assert (se->ss->expr == expr);
403 /* A scalarized term. We already know the descriptor. */
404 se->expr = se->ss->data.info.descriptor;
405 se->string_length = se->ss->string_length;
406 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
407 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
408 break;
410 else
412 tree se_expr = NULL_TREE;
414 se->expr = gfc_get_symbol_decl (sym);
416 /* Deal with references to a parent results or entries by storing
417 the current_function_decl and moving to the parent_decl. */
418 return_value = sym->attr.function && sym->result == sym;
419 alternate_entry = sym->attr.function && sym->attr.entry
420 && sym->result == sym;
421 entry_master = sym->attr.result
422 && sym->ns->proc_name->attr.entry_master
423 && !gfc_return_by_reference (sym->ns->proc_name);
424 parent_decl = DECL_CONTEXT (current_function_decl);
426 if ((se->expr == parent_decl && return_value)
427 || (sym->ns && sym->ns->proc_name
428 && parent_decl
429 && sym->ns->proc_name->backend_decl == parent_decl
430 && (alternate_entry || entry_master)))
431 parent_flag = 1;
432 else
433 parent_flag = 0;
435 /* Special case for assigning the return value of a function.
436 Self recursive functions must have an explicit return value. */
437 if (return_value && (se->expr == current_function_decl || parent_flag))
438 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
440 /* Similarly for alternate entry points. */
441 else if (alternate_entry
442 && (sym->ns->proc_name->backend_decl == current_function_decl
443 || parent_flag))
445 gfc_entry_list *el = NULL;
447 for (el = sym->ns->entries; el; el = el->next)
448 if (sym == el->sym)
450 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
451 break;
455 else if (entry_master
456 && (sym->ns->proc_name->backend_decl == current_function_decl
457 || parent_flag))
458 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
460 if (se_expr)
461 se->expr = se_expr;
463 /* Procedure actual arguments. */
464 else if (sym->attr.flavor == FL_PROCEDURE
465 && se->expr != current_function_decl)
467 gcc_assert (se->want_pointer);
468 if (!sym->attr.dummy)
470 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
471 se->expr = build_fold_addr_expr (se->expr);
473 return;
477 /* Dereference the expression, where needed. Since characters
478 are entirely different from other types, they are treated
479 separately. */
480 if (sym->ts.type == BT_CHARACTER)
482 /* Dereference character pointer dummy arguments
483 or results. */
484 if ((sym->attr.pointer || sym->attr.allocatable)
485 && (sym->attr.dummy
486 || sym->attr.function
487 || sym->attr.result))
488 se->expr = build_fold_indirect_ref (se->expr);
491 else if (!sym->attr.value)
493 /* Dereference non-character scalar dummy arguments. */
494 if (sym->attr.dummy && !sym->attr.dimension)
495 se->expr = build_fold_indirect_ref (se->expr);
497 /* Dereference scalar hidden result. */
498 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
499 && (sym->attr.function || sym->attr.result)
500 && !sym->attr.dimension && !sym->attr.pointer)
501 se->expr = build_fold_indirect_ref (se->expr);
503 /* Dereference non-character pointer variables.
504 These must be dummies, results, or scalars. */
505 if ((sym->attr.pointer || sym->attr.allocatable)
506 && (sym->attr.dummy
507 || sym->attr.function
508 || sym->attr.result
509 || !sym->attr.dimension))
510 se->expr = build_fold_indirect_ref (se->expr);
513 ref = expr->ref;
516 /* For character variables, also get the length. */
517 if (sym->ts.type == BT_CHARACTER)
519 /* If the character length of an entry isn't set, get the length from
520 the master function instead. */
521 if (sym->attr.entry && !sym->ts.cl->backend_decl)
522 se->string_length = sym->ns->proc_name->ts.cl->backend_decl;
523 else
524 se->string_length = sym->ts.cl->backend_decl;
525 gcc_assert (se->string_length);
528 while (ref)
530 switch (ref->type)
532 case REF_ARRAY:
533 /* Return the descriptor if that's what we want and this is an array
534 section reference. */
535 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
536 return;
537 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
538 /* Return the descriptor for array pointers and allocations. */
539 if (se->want_pointer
540 && ref->next == NULL && (se->descriptor_only))
541 return;
543 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
544 /* Return a pointer to an element. */
545 break;
547 case REF_COMPONENT:
548 gfc_conv_component_ref (se, ref);
549 break;
551 case REF_SUBSTRING:
552 gfc_conv_substring (se, ref, expr->ts.kind,
553 expr->symtree->name, &expr->where);
554 break;
556 default:
557 gcc_unreachable ();
558 break;
560 ref = ref->next;
562 /* Pointer assignment, allocation or pass by reference. Arrays are handled
563 separately. */
564 if (se->want_pointer)
566 if (expr->ts.type == BT_CHARACTER)
567 gfc_conv_string_parameter (se);
568 else
569 se->expr = build_fold_addr_expr (se->expr);
574 /* Unary ops are easy... Or they would be if ! was a valid op. */
576 static void
577 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
579 gfc_se operand;
580 tree type;
582 gcc_assert (expr->ts.type != BT_CHARACTER);
583 /* Initialize the operand. */
584 gfc_init_se (&operand, se);
585 gfc_conv_expr_val (&operand, expr->value.op.op1);
586 gfc_add_block_to_block (&se->pre, &operand.pre);
588 type = gfc_typenode_for_spec (&expr->ts);
590 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
591 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
592 All other unary operators have an equivalent GIMPLE unary operator. */
593 if (code == TRUTH_NOT_EXPR)
594 se->expr = build2 (EQ_EXPR, type, operand.expr,
595 build_int_cst (type, 0));
596 else
597 se->expr = build1 (code, type, operand.expr);
601 /* Expand power operator to optimal multiplications when a value is raised
602 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
603 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
604 Programming", 3rd Edition, 1998. */
606 /* This code is mostly duplicated from expand_powi in the backend.
607 We establish the "optimal power tree" lookup table with the defined size.
608 The items in the table are the exponents used to calculate the index
609 exponents. Any integer n less than the value can get an "addition chain",
610 with the first node being one. */
611 #define POWI_TABLE_SIZE 256
613 /* The table is from builtins.c. */
614 static const unsigned char powi_table[POWI_TABLE_SIZE] =
616 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
617 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
618 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
619 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
620 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
621 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
622 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
623 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
624 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
625 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
626 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
627 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
628 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
629 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
630 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
631 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
632 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
633 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
634 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
635 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
636 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
637 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
638 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
639 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
640 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
641 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
642 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
643 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
644 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
645 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
646 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
647 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
650 /* If n is larger than lookup table's max index, we use the "window
651 method". */
652 #define POWI_WINDOW_SIZE 3
654 /* Recursive function to expand the power operator. The temporary
655 values are put in tmpvar. The function returns tmpvar[1] ** n. */
656 static tree
657 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
659 tree op0;
660 tree op1;
661 tree tmp;
662 int digit;
664 if (n < POWI_TABLE_SIZE)
666 if (tmpvar[n])
667 return tmpvar[n];
669 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
670 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
672 else if (n & 1)
674 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
675 op0 = gfc_conv_powi (se, n - digit, tmpvar);
676 op1 = gfc_conv_powi (se, digit, tmpvar);
678 else
680 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
681 op1 = op0;
684 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (op0), op0, op1);
685 tmp = gfc_evaluate_now (tmp, &se->pre);
687 if (n < POWI_TABLE_SIZE)
688 tmpvar[n] = tmp;
690 return tmp;
694 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
695 return 1. Else return 0 and a call to runtime library functions
696 will have to be built. */
697 static int
698 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
700 tree cond;
701 tree tmp;
702 tree type;
703 tree vartmp[POWI_TABLE_SIZE];
704 HOST_WIDE_INT m;
705 unsigned HOST_WIDE_INT n;
706 int sgn;
708 /* If exponent is too large, we won't expand it anyway, so don't bother
709 with large integer values. */
710 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
711 return 0;
713 m = double_int_to_shwi (TREE_INT_CST (rhs));
714 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
715 of the asymmetric range of the integer type. */
716 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
718 type = TREE_TYPE (lhs);
719 sgn = tree_int_cst_sgn (rhs);
721 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
722 || optimize_size) && (m > 2 || m < -1))
723 return 0;
725 /* rhs == 0 */
726 if (sgn == 0)
728 se->expr = gfc_build_const (type, integer_one_node);
729 return 1;
732 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
733 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
735 tmp = build2 (EQ_EXPR, boolean_type_node, lhs,
736 build_int_cst (TREE_TYPE (lhs), -1));
737 cond = build2 (EQ_EXPR, boolean_type_node, lhs,
738 build_int_cst (TREE_TYPE (lhs), 1));
740 /* If rhs is even,
741 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
742 if ((n & 1) == 0)
744 tmp = build2 (TRUTH_OR_EXPR, boolean_type_node, tmp, cond);
745 se->expr = build3 (COND_EXPR, type, tmp, build_int_cst (type, 1),
746 build_int_cst (type, 0));
747 return 1;
749 /* If rhs is odd,
750 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
751 tmp = build3 (COND_EXPR, type, tmp, build_int_cst (type, -1),
752 build_int_cst (type, 0));
753 se->expr = build3 (COND_EXPR, type, cond, build_int_cst (type, 1), tmp);
754 return 1;
757 memset (vartmp, 0, sizeof (vartmp));
758 vartmp[1] = lhs;
759 if (sgn == -1)
761 tmp = gfc_build_const (type, integer_one_node);
762 vartmp[1] = build2 (RDIV_EXPR, type, tmp, vartmp[1]);
765 se->expr = gfc_conv_powi (se, n, vartmp);
767 return 1;
771 /* Power op (**). Constant integer exponent has special handling. */
773 static void
774 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
776 tree gfc_int4_type_node;
777 int kind;
778 int ikind;
779 gfc_se lse;
780 gfc_se rse;
781 tree fndecl;
783 gfc_init_se (&lse, se);
784 gfc_conv_expr_val (&lse, expr->value.op.op1);
785 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
786 gfc_add_block_to_block (&se->pre, &lse.pre);
788 gfc_init_se (&rse, se);
789 gfc_conv_expr_val (&rse, expr->value.op.op2);
790 gfc_add_block_to_block (&se->pre, &rse.pre);
792 if (expr->value.op.op2->ts.type == BT_INTEGER
793 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
794 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
795 return;
797 gfc_int4_type_node = gfc_get_int_type (4);
799 kind = expr->value.op.op1->ts.kind;
800 switch (expr->value.op.op2->ts.type)
802 case BT_INTEGER:
803 ikind = expr->value.op.op2->ts.kind;
804 switch (ikind)
806 case 1:
807 case 2:
808 rse.expr = convert (gfc_int4_type_node, rse.expr);
809 /* Fall through. */
811 case 4:
812 ikind = 0;
813 break;
815 case 8:
816 ikind = 1;
817 break;
819 case 16:
820 ikind = 2;
821 break;
823 default:
824 gcc_unreachable ();
826 switch (kind)
828 case 1:
829 case 2:
830 if (expr->value.op.op1->ts.type == BT_INTEGER)
831 lse.expr = convert (gfc_int4_type_node, lse.expr);
832 else
833 gcc_unreachable ();
834 /* Fall through. */
836 case 4:
837 kind = 0;
838 break;
840 case 8:
841 kind = 1;
842 break;
844 case 10:
845 kind = 2;
846 break;
848 case 16:
849 kind = 3;
850 break;
852 default:
853 gcc_unreachable ();
856 switch (expr->value.op.op1->ts.type)
858 case BT_INTEGER:
859 if (kind == 3) /* Case 16 was not handled properly above. */
860 kind = 2;
861 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
862 break;
864 case BT_REAL:
865 /* Use builtins for real ** int4. */
866 if (ikind == 0)
868 switch (kind)
870 case 0:
871 fndecl = built_in_decls[BUILT_IN_POWIF];
872 break;
874 case 1:
875 fndecl = built_in_decls[BUILT_IN_POWI];
876 break;
878 case 2:
879 case 3:
880 fndecl = built_in_decls[BUILT_IN_POWIL];
881 break;
883 default:
884 gcc_unreachable ();
887 else
888 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
889 break;
891 case BT_COMPLEX:
892 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
893 break;
895 default:
896 gcc_unreachable ();
898 break;
900 case BT_REAL:
901 switch (kind)
903 case 4:
904 fndecl = built_in_decls[BUILT_IN_POWF];
905 break;
906 case 8:
907 fndecl = built_in_decls[BUILT_IN_POW];
908 break;
909 case 10:
910 case 16:
911 fndecl = built_in_decls[BUILT_IN_POWL];
912 break;
913 default:
914 gcc_unreachable ();
916 break;
918 case BT_COMPLEX:
919 switch (kind)
921 case 4:
922 fndecl = gfor_fndecl_math_cpowf;
923 break;
924 case 8:
925 fndecl = gfor_fndecl_math_cpow;
926 break;
927 case 10:
928 fndecl = gfor_fndecl_math_cpowl10;
929 break;
930 case 16:
931 fndecl = gfor_fndecl_math_cpowl16;
932 break;
933 default:
934 gcc_unreachable ();
936 break;
938 default:
939 gcc_unreachable ();
940 break;
943 se->expr = build_call_expr (fndecl, 2, lse.expr, rse.expr);
947 /* Generate code to allocate a string temporary. */
949 tree
950 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
952 tree var;
953 tree tmp;
955 gcc_assert (TREE_TYPE (len) == gfc_charlen_type_node);
957 if (gfc_can_put_var_on_stack (len))
959 /* Create a temporary variable to hold the result. */
960 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
961 build_int_cst (gfc_charlen_type_node, 1));
962 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
963 tmp = build_array_type (gfc_character1_type_node, tmp);
964 var = gfc_create_var (tmp, "str");
965 var = gfc_build_addr_expr (type, var);
967 else
969 /* Allocate a temporary to hold the result. */
970 var = gfc_create_var (type, "pstr");
971 tmp = gfc_call_malloc (&se->pre, type, len);
972 gfc_add_modify_expr (&se->pre, var, tmp);
974 /* Free the temporary afterwards. */
975 tmp = gfc_call_free (convert (pvoid_type_node, var));
976 gfc_add_expr_to_block (&se->post, tmp);
979 return var;
983 /* Handle a string concatenation operation. A temporary will be allocated to
984 hold the result. */
986 static void
987 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
989 gfc_se lse;
990 gfc_se rse;
991 tree len;
992 tree type;
993 tree var;
994 tree tmp;
996 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
997 && expr->value.op.op2->ts.type == BT_CHARACTER);
999 gfc_init_se (&lse, se);
1000 gfc_conv_expr (&lse, expr->value.op.op1);
1001 gfc_conv_string_parameter (&lse);
1002 gfc_init_se (&rse, se);
1003 gfc_conv_expr (&rse, expr->value.op.op2);
1004 gfc_conv_string_parameter (&rse);
1006 gfc_add_block_to_block (&se->pre, &lse.pre);
1007 gfc_add_block_to_block (&se->pre, &rse.pre);
1009 type = gfc_get_character_type (expr->ts.kind, expr->ts.cl);
1010 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1011 if (len == NULL_TREE)
1013 len = fold_build2 (PLUS_EXPR, TREE_TYPE (lse.string_length),
1014 lse.string_length, rse.string_length);
1017 type = build_pointer_type (type);
1019 var = gfc_conv_string_tmp (se, type, len);
1021 /* Do the actual concatenation. */
1022 tmp = build_call_expr (gfor_fndecl_concat_string, 6,
1023 len, var,
1024 lse.string_length, lse.expr,
1025 rse.string_length, rse.expr);
1026 gfc_add_expr_to_block (&se->pre, tmp);
1028 /* Add the cleanup for the operands. */
1029 gfc_add_block_to_block (&se->pre, &rse.post);
1030 gfc_add_block_to_block (&se->pre, &lse.post);
1032 se->expr = var;
1033 se->string_length = len;
1036 /* Translates an op expression. Common (binary) cases are handled by this
1037 function, others are passed on. Recursion is used in either case.
1038 We use the fact that (op1.ts == op2.ts) (except for the power
1039 operator **).
1040 Operators need no special handling for scalarized expressions as long as
1041 they call gfc_conv_simple_val to get their operands.
1042 Character strings get special handling. */
1044 static void
1045 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1047 enum tree_code code;
1048 gfc_se lse;
1049 gfc_se rse;
1050 tree tmp, type;
1051 int lop;
1052 int checkstring;
1054 checkstring = 0;
1055 lop = 0;
1056 switch (expr->value.op.operator)
1058 case INTRINSIC_UPLUS:
1059 case INTRINSIC_PARENTHESES:
1060 gfc_conv_expr (se, expr->value.op.op1);
1061 return;
1063 case INTRINSIC_UMINUS:
1064 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1065 return;
1067 case INTRINSIC_NOT:
1068 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1069 return;
1071 case INTRINSIC_PLUS:
1072 code = PLUS_EXPR;
1073 break;
1075 case INTRINSIC_MINUS:
1076 code = MINUS_EXPR;
1077 break;
1079 case INTRINSIC_TIMES:
1080 code = MULT_EXPR;
1081 break;
1083 case INTRINSIC_DIVIDE:
1084 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1085 an integer, we must round towards zero, so we use a
1086 TRUNC_DIV_EXPR. */
1087 if (expr->ts.type == BT_INTEGER)
1088 code = TRUNC_DIV_EXPR;
1089 else
1090 code = RDIV_EXPR;
1091 break;
1093 case INTRINSIC_POWER:
1094 gfc_conv_power_op (se, expr);
1095 return;
1097 case INTRINSIC_CONCAT:
1098 gfc_conv_concat_op (se, expr);
1099 return;
1101 case INTRINSIC_AND:
1102 code = TRUTH_ANDIF_EXPR;
1103 lop = 1;
1104 break;
1106 case INTRINSIC_OR:
1107 code = TRUTH_ORIF_EXPR;
1108 lop = 1;
1109 break;
1111 /* EQV and NEQV only work on logicals, but since we represent them
1112 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1113 case INTRINSIC_EQ:
1114 case INTRINSIC_EQ_OS:
1115 case INTRINSIC_EQV:
1116 code = EQ_EXPR;
1117 checkstring = 1;
1118 lop = 1;
1119 break;
1121 case INTRINSIC_NE:
1122 case INTRINSIC_NE_OS:
1123 case INTRINSIC_NEQV:
1124 code = NE_EXPR;
1125 checkstring = 1;
1126 lop = 1;
1127 break;
1129 case INTRINSIC_GT:
1130 case INTRINSIC_GT_OS:
1131 code = GT_EXPR;
1132 checkstring = 1;
1133 lop = 1;
1134 break;
1136 case INTRINSIC_GE:
1137 case INTRINSIC_GE_OS:
1138 code = GE_EXPR;
1139 checkstring = 1;
1140 lop = 1;
1141 break;
1143 case INTRINSIC_LT:
1144 case INTRINSIC_LT_OS:
1145 code = LT_EXPR;
1146 checkstring = 1;
1147 lop = 1;
1148 break;
1150 case INTRINSIC_LE:
1151 case INTRINSIC_LE_OS:
1152 code = LE_EXPR;
1153 checkstring = 1;
1154 lop = 1;
1155 break;
1157 case INTRINSIC_USER:
1158 case INTRINSIC_ASSIGN:
1159 /* These should be converted into function calls by the frontend. */
1160 gcc_unreachable ();
1162 default:
1163 fatal_error ("Unknown intrinsic op");
1164 return;
1167 /* The only exception to this is **, which is handled separately anyway. */
1168 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1170 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1171 checkstring = 0;
1173 /* lhs */
1174 gfc_init_se (&lse, se);
1175 gfc_conv_expr (&lse, expr->value.op.op1);
1176 gfc_add_block_to_block (&se->pre, &lse.pre);
1178 /* rhs */
1179 gfc_init_se (&rse, se);
1180 gfc_conv_expr (&rse, expr->value.op.op2);
1181 gfc_add_block_to_block (&se->pre, &rse.pre);
1183 if (checkstring)
1185 gfc_conv_string_parameter (&lse);
1186 gfc_conv_string_parameter (&rse);
1188 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1189 rse.string_length, rse.expr);
1190 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1191 gfc_add_block_to_block (&lse.post, &rse.post);
1194 type = gfc_typenode_for_spec (&expr->ts);
1196 if (lop)
1198 /* The result of logical ops is always boolean_type_node. */
1199 tmp = fold_build2 (code, boolean_type_node, lse.expr, rse.expr);
1200 se->expr = convert (type, tmp);
1202 else
1203 se->expr = fold_build2 (code, type, lse.expr, rse.expr);
1205 /* Add the post blocks. */
1206 gfc_add_block_to_block (&se->post, &rse.post);
1207 gfc_add_block_to_block (&se->post, &lse.post);
1210 /* If a string's length is one, we convert it to a single character. */
1212 static tree
1213 gfc_to_single_character (tree len, tree str)
1215 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str)));
1217 if (INTEGER_CST_P (len) && TREE_INT_CST_LOW (len) == 1
1218 && TREE_INT_CST_HIGH (len) == 0)
1220 str = fold_convert (pchar_type_node, str);
1221 return build_fold_indirect_ref (str);
1224 return NULL_TREE;
1228 void
1229 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1232 if (sym->backend_decl)
1234 /* This becomes the nominal_type in
1235 function.c:assign_parm_find_data_types. */
1236 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1237 /* This becomes the passed_type in
1238 function.c:assign_parm_find_data_types. C promotes char to
1239 integer for argument passing. */
1240 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1242 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1245 if (expr != NULL)
1247 /* If we have a constant character expression, make it into an
1248 integer. */
1249 if ((*expr)->expr_type == EXPR_CONSTANT)
1251 gfc_typespec ts;
1253 *expr = gfc_int_expr ((int)(*expr)->value.character.string[0]);
1254 if ((*expr)->ts.kind != gfc_c_int_kind)
1256 /* The expr needs to be compatible with a C int. If the
1257 conversion fails, then the 2 causes an ICE. */
1258 ts.type = BT_INTEGER;
1259 ts.kind = gfc_c_int_kind;
1260 gfc_convert_type (*expr, &ts, 2);
1263 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1265 if ((*expr)->ref == NULL)
1267 se->expr = gfc_to_single_character
1268 (build_int_cst (integer_type_node, 1),
1269 gfc_build_addr_expr (pchar_type_node,
1270 gfc_get_symbol_decl
1271 ((*expr)->symtree->n.sym)));
1273 else
1275 gfc_conv_variable (se, *expr);
1276 se->expr = gfc_to_single_character
1277 (build_int_cst (integer_type_node, 1),
1278 gfc_build_addr_expr (pchar_type_node, se->expr));
1285 /* Compare two strings. If they are all single characters, the result is the
1286 subtraction of them. Otherwise, we build a library call. */
1288 tree
1289 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2)
1291 tree sc1;
1292 tree sc2;
1293 tree tmp;
1295 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1296 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1298 sc1 = gfc_to_single_character (len1, str1);
1299 sc2 = gfc_to_single_character (len2, str2);
1301 /* Deal with single character specially. */
1302 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1304 sc1 = fold_convert (integer_type_node, sc1);
1305 sc2 = fold_convert (integer_type_node, sc2);
1306 tmp = fold_build2 (MINUS_EXPR, integer_type_node, sc1, sc2);
1308 else
1309 /* Build a call for the comparison. */
1310 tmp = build_call_expr (gfor_fndecl_compare_string, 4,
1311 len1, str1, len2, str2);
1312 return tmp;
1315 static void
1316 gfc_conv_function_val (gfc_se * se, gfc_symbol * sym)
1318 tree tmp;
1320 if (sym->attr.dummy)
1322 tmp = gfc_get_symbol_decl (sym);
1323 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1324 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1326 else
1328 if (!sym->backend_decl)
1329 sym->backend_decl = gfc_get_extern_function_decl (sym);
1331 tmp = sym->backend_decl;
1332 if (sym->attr.cray_pointee)
1333 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1334 gfc_get_symbol_decl (sym->cp_pointer));
1335 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1337 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1338 tmp = build_fold_addr_expr (tmp);
1341 se->expr = tmp;
1345 /* Translate the call for an elemental subroutine call used in an operator
1346 assignment. This is a simplified version of gfc_conv_function_call. */
1348 tree
1349 gfc_conv_operator_assign (gfc_se *lse, gfc_se *rse, gfc_symbol *sym)
1351 tree args;
1352 tree tmp;
1353 gfc_se se;
1354 stmtblock_t block;
1356 /* Only elemental subroutines with two arguments. */
1357 gcc_assert (sym->attr.elemental && sym->attr.subroutine);
1358 gcc_assert (sym->formal->next->next == NULL);
1360 gfc_init_block (&block);
1362 gfc_add_block_to_block (&block, &lse->pre);
1363 gfc_add_block_to_block (&block, &rse->pre);
1365 /* Build the argument list for the call, including hidden string lengths. */
1366 args = gfc_chainon_list (NULL_TREE, build_fold_addr_expr (lse->expr));
1367 args = gfc_chainon_list (args, build_fold_addr_expr (rse->expr));
1368 if (lse->string_length != NULL_TREE)
1369 args = gfc_chainon_list (args, lse->string_length);
1370 if (rse->string_length != NULL_TREE)
1371 args = gfc_chainon_list (args, rse->string_length);
1373 /* Build the function call. */
1374 gfc_init_se (&se, NULL);
1375 gfc_conv_function_val (&se, sym);
1376 tmp = TREE_TYPE (TREE_TYPE (TREE_TYPE (se.expr)));
1377 tmp = build_call_list (tmp, se.expr, args);
1378 gfc_add_expr_to_block (&block, tmp);
1380 gfc_add_block_to_block (&block, &lse->post);
1381 gfc_add_block_to_block (&block, &rse->post);
1383 return gfc_finish_block (&block);
1387 /* Initialize MAPPING. */
1389 void
1390 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1392 mapping->syms = NULL;
1393 mapping->charlens = NULL;
1397 /* Free all memory held by MAPPING (but not MAPPING itself). */
1399 void
1400 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1402 gfc_interface_sym_mapping *sym;
1403 gfc_interface_sym_mapping *nextsym;
1404 gfc_charlen *cl;
1405 gfc_charlen *nextcl;
1407 for (sym = mapping->syms; sym; sym = nextsym)
1409 nextsym = sym->next;
1410 gfc_free_symbol (sym->new->n.sym);
1411 gfc_free (sym->new);
1412 gfc_free (sym);
1414 for (cl = mapping->charlens; cl; cl = nextcl)
1416 nextcl = cl->next;
1417 gfc_free_expr (cl->length);
1418 gfc_free (cl);
1423 /* Return a copy of gfc_charlen CL. Add the returned structure to
1424 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1426 static gfc_charlen *
1427 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1428 gfc_charlen * cl)
1430 gfc_charlen *new;
1432 new = gfc_get_charlen ();
1433 new->next = mapping->charlens;
1434 new->length = gfc_copy_expr (cl->length);
1436 mapping->charlens = new;
1437 return new;
1441 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1442 array variable that can be used as the actual argument for dummy
1443 argument SYM. Add any initialization code to BLOCK. PACKED is as
1444 for gfc_get_nodesc_array_type and DATA points to the first element
1445 in the passed array. */
1447 static tree
1448 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1449 gfc_packed packed, tree data)
1451 tree type;
1452 tree var;
1454 type = gfc_typenode_for_spec (&sym->ts);
1455 type = gfc_get_nodesc_array_type (type, sym->as, packed);
1457 var = gfc_create_var (type, "ifm");
1458 gfc_add_modify_expr (block, var, fold_convert (type, data));
1460 return var;
1464 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1465 and offset of descriptorless array type TYPE given that it has the same
1466 size as DESC. Add any set-up code to BLOCK. */
1468 static void
1469 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1471 int n;
1472 tree dim;
1473 tree offset;
1474 tree tmp;
1476 offset = gfc_index_zero_node;
1477 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1479 dim = gfc_rank_cst[n];
1480 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1481 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1483 GFC_TYPE_ARRAY_LBOUND (type, n)
1484 = gfc_conv_descriptor_lbound (desc, dim);
1485 GFC_TYPE_ARRAY_UBOUND (type, n)
1486 = gfc_conv_descriptor_ubound (desc, dim);
1488 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1490 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1491 gfc_conv_descriptor_ubound (desc, dim),
1492 gfc_conv_descriptor_lbound (desc, dim));
1493 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1494 GFC_TYPE_ARRAY_LBOUND (type, n),
1495 tmp);
1496 tmp = gfc_evaluate_now (tmp, block);
1497 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1499 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
1500 GFC_TYPE_ARRAY_LBOUND (type, n),
1501 GFC_TYPE_ARRAY_STRIDE (type, n));
1502 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
1504 offset = gfc_evaluate_now (offset, block);
1505 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1509 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1510 in SE. The caller may still use se->expr and se->string_length after
1511 calling this function. */
1513 void
1514 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1515 gfc_symbol * sym, gfc_se * se)
1517 gfc_interface_sym_mapping *sm;
1518 tree desc;
1519 tree tmp;
1520 tree value;
1521 gfc_symbol *new_sym;
1522 gfc_symtree *root;
1523 gfc_symtree *new_symtree;
1525 /* Create a new symbol to represent the actual argument. */
1526 new_sym = gfc_new_symbol (sym->name, NULL);
1527 new_sym->ts = sym->ts;
1528 new_sym->attr.referenced = 1;
1529 new_sym->attr.dimension = sym->attr.dimension;
1530 new_sym->attr.pointer = sym->attr.pointer;
1531 new_sym->attr.allocatable = sym->attr.allocatable;
1532 new_sym->attr.flavor = sym->attr.flavor;
1534 /* Create a fake symtree for it. */
1535 root = NULL;
1536 new_symtree = gfc_new_symtree (&root, sym->name);
1537 new_symtree->n.sym = new_sym;
1538 gcc_assert (new_symtree == root);
1540 /* Create a dummy->actual mapping. */
1541 sm = gfc_getmem (sizeof (*sm));
1542 sm->next = mapping->syms;
1543 sm->old = sym;
1544 sm->new = new_symtree;
1545 mapping->syms = sm;
1547 /* Stabilize the argument's value. */
1548 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1550 if (sym->ts.type == BT_CHARACTER)
1552 /* Create a copy of the dummy argument's length. */
1553 new_sym->ts.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.cl);
1555 /* If the length is specified as "*", record the length that
1556 the caller is passing. We should use the callee's length
1557 in all other cases. */
1558 if (!new_sym->ts.cl->length)
1560 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1561 new_sym->ts.cl->backend_decl = se->string_length;
1565 /* Use the passed value as-is if the argument is a function. */
1566 if (sym->attr.flavor == FL_PROCEDURE)
1567 value = se->expr;
1569 /* If the argument is either a string or a pointer to a string,
1570 convert it to a boundless character type. */
1571 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1573 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1574 tmp = build_pointer_type (tmp);
1575 if (sym->attr.pointer)
1576 value = build_fold_indirect_ref (se->expr);
1577 else
1578 value = se->expr;
1579 value = fold_convert (tmp, value);
1582 /* If the argument is a scalar, a pointer to an array or an allocatable,
1583 dereference it. */
1584 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1585 value = build_fold_indirect_ref (se->expr);
1587 /* For character(*), use the actual argument's descriptor. */
1588 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.cl->length)
1589 value = build_fold_indirect_ref (se->expr);
1591 /* If the argument is an array descriptor, use it to determine
1592 information about the actual argument's shape. */
1593 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1594 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1596 /* Get the actual argument's descriptor. */
1597 desc = build_fold_indirect_ref (se->expr);
1599 /* Create the replacement variable. */
1600 tmp = gfc_conv_descriptor_data_get (desc);
1601 value = gfc_get_interface_mapping_array (&se->pre, sym,
1602 PACKED_NO, tmp);
1604 /* Use DESC to work out the upper bounds, strides and offset. */
1605 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1607 else
1608 /* Otherwise we have a packed array. */
1609 value = gfc_get_interface_mapping_array (&se->pre, sym,
1610 PACKED_FULL, se->expr);
1612 new_sym->backend_decl = value;
1616 /* Called once all dummy argument mappings have been added to MAPPING,
1617 but before the mapping is used to evaluate expressions. Pre-evaluate
1618 the length of each argument, adding any initialization code to PRE and
1619 any finalization code to POST. */
1621 void
1622 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1623 stmtblock_t * pre, stmtblock_t * post)
1625 gfc_interface_sym_mapping *sym;
1626 gfc_expr *expr;
1627 gfc_se se;
1629 for (sym = mapping->syms; sym; sym = sym->next)
1630 if (sym->new->n.sym->ts.type == BT_CHARACTER
1631 && !sym->new->n.sym->ts.cl->backend_decl)
1633 expr = sym->new->n.sym->ts.cl->length;
1634 gfc_apply_interface_mapping_to_expr (mapping, expr);
1635 gfc_init_se (&se, NULL);
1636 gfc_conv_expr (&se, expr);
1638 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1639 gfc_add_block_to_block (pre, &se.pre);
1640 gfc_add_block_to_block (post, &se.post);
1642 sym->new->n.sym->ts.cl->backend_decl = se.expr;
1647 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1648 constructor C. */
1650 static void
1651 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1652 gfc_constructor * c)
1654 for (; c; c = c->next)
1656 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
1657 if (c->iterator)
1659 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
1660 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
1661 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
1667 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1668 reference REF. */
1670 static void
1671 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
1672 gfc_ref * ref)
1674 int n;
1676 for (; ref; ref = ref->next)
1677 switch (ref->type)
1679 case REF_ARRAY:
1680 for (n = 0; n < ref->u.ar.dimen; n++)
1682 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
1683 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
1684 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
1686 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
1687 break;
1689 case REF_COMPONENT:
1690 break;
1692 case REF_SUBSTRING:
1693 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
1694 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
1695 break;
1700 /* EXPR is a copy of an expression that appeared in the interface
1701 associated with MAPPING. Walk it recursively looking for references to
1702 dummy arguments that MAPPING maps to actual arguments. Replace each such
1703 reference with a reference to the associated actual argument. */
1705 static int
1706 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
1707 gfc_expr * expr)
1709 gfc_interface_sym_mapping *sym;
1710 gfc_actual_arglist *actual;
1711 int seen_result = 0;
1713 if (!expr)
1714 return 0;
1716 /* Copying an expression does not copy its length, so do that here. */
1717 if (expr->ts.type == BT_CHARACTER && expr->ts.cl)
1719 expr->ts.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.cl);
1720 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.cl->length);
1723 /* Apply the mapping to any references. */
1724 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
1726 /* ...and to the expression's symbol, if it has one. */
1727 if (expr->symtree)
1728 for (sym = mapping->syms; sym; sym = sym->next)
1729 if (sym->old == expr->symtree->n.sym)
1730 expr->symtree = sym->new;
1732 /* ...and to subexpressions in expr->value. */
1733 switch (expr->expr_type)
1735 case EXPR_VARIABLE:
1736 if (expr->symtree->n.sym->attr.result)
1737 seen_result = 1;
1738 case EXPR_CONSTANT:
1739 case EXPR_NULL:
1740 case EXPR_SUBSTRING:
1741 break;
1743 case EXPR_OP:
1744 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
1745 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
1746 break;
1748 case EXPR_FUNCTION:
1749 if (expr->value.function.esym == NULL
1750 && expr->value.function.isym != NULL
1751 && expr->value.function.isym->id == GFC_ISYM_LEN
1752 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE
1753 && gfc_apply_interface_mapping_to_expr (mapping,
1754 expr->value.function.actual->expr))
1756 gfc_expr *new_expr;
1757 new_expr = gfc_copy_expr (expr->value.function.actual->expr->ts.cl->length);
1758 *expr = *new_expr;
1759 gfc_free (new_expr);
1760 gfc_apply_interface_mapping_to_expr (mapping, expr);
1761 break;
1764 for (sym = mapping->syms; sym; sym = sym->next)
1765 if (sym->old == expr->value.function.esym)
1766 expr->value.function.esym = sym->new->n.sym;
1768 for (actual = expr->value.function.actual; actual; actual = actual->next)
1769 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
1770 break;
1772 case EXPR_ARRAY:
1773 case EXPR_STRUCTURE:
1774 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
1775 break;
1777 return seen_result;
1781 /* Evaluate interface expression EXPR using MAPPING. Store the result
1782 in SE. */
1784 void
1785 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
1786 gfc_se * se, gfc_expr * expr)
1788 expr = gfc_copy_expr (expr);
1789 gfc_apply_interface_mapping_to_expr (mapping, expr);
1790 gfc_conv_expr (se, expr);
1791 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1792 gfc_free_expr (expr);
1796 /* Returns a reference to a temporary array into which a component of
1797 an actual argument derived type array is copied and then returned
1798 after the function call. */
1799 void
1800 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr,
1801 int g77, sym_intent intent)
1803 gfc_se lse;
1804 gfc_se rse;
1805 gfc_ss *lss;
1806 gfc_ss *rss;
1807 gfc_loopinfo loop;
1808 gfc_loopinfo loop2;
1809 gfc_ss_info *info;
1810 tree offset;
1811 tree tmp_index;
1812 tree tmp;
1813 tree base_type;
1814 stmtblock_t body;
1815 int n;
1817 gcc_assert (expr->expr_type == EXPR_VARIABLE);
1819 gfc_init_se (&lse, NULL);
1820 gfc_init_se (&rse, NULL);
1822 /* Walk the argument expression. */
1823 rss = gfc_walk_expr (expr);
1825 gcc_assert (rss != gfc_ss_terminator);
1827 /* Initialize the scalarizer. */
1828 gfc_init_loopinfo (&loop);
1829 gfc_add_ss_to_loop (&loop, rss);
1831 /* Calculate the bounds of the scalarization. */
1832 gfc_conv_ss_startstride (&loop);
1834 /* Build an ss for the temporary. */
1835 if (expr->ts.type == BT_CHARACTER && !expr->ts.cl->backend_decl)
1836 gfc_conv_string_length (expr->ts.cl, &parmse->pre);
1838 base_type = gfc_typenode_for_spec (&expr->ts);
1839 if (GFC_ARRAY_TYPE_P (base_type)
1840 || GFC_DESCRIPTOR_TYPE_P (base_type))
1841 base_type = gfc_get_element_type (base_type);
1843 loop.temp_ss = gfc_get_ss ();;
1844 loop.temp_ss->type = GFC_SS_TEMP;
1845 loop.temp_ss->data.temp.type = base_type;
1847 if (expr->ts.type == BT_CHARACTER)
1848 loop.temp_ss->string_length = expr->ts.cl->backend_decl;
1849 else
1850 loop.temp_ss->string_length = NULL;
1852 parmse->string_length = loop.temp_ss->string_length;
1853 loop.temp_ss->data.temp.dimen = loop.dimen;
1854 loop.temp_ss->next = gfc_ss_terminator;
1856 /* Associate the SS with the loop. */
1857 gfc_add_ss_to_loop (&loop, loop.temp_ss);
1859 /* Setup the scalarizing loops. */
1860 gfc_conv_loop_setup (&loop);
1862 /* Pass the temporary descriptor back to the caller. */
1863 info = &loop.temp_ss->data.info;
1864 parmse->expr = info->descriptor;
1866 /* Setup the gfc_se structures. */
1867 gfc_copy_loopinfo_to_se (&lse, &loop);
1868 gfc_copy_loopinfo_to_se (&rse, &loop);
1870 rse.ss = rss;
1871 lse.ss = loop.temp_ss;
1872 gfc_mark_ss_chain_used (rss, 1);
1873 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1875 /* Start the scalarized loop body. */
1876 gfc_start_scalarized_body (&loop, &body);
1878 /* Translate the expression. */
1879 gfc_conv_expr (&rse, expr);
1881 gfc_conv_tmp_array_ref (&lse);
1882 gfc_advance_se_ss_chain (&lse);
1884 if (intent != INTENT_OUT)
1886 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false);
1887 gfc_add_expr_to_block (&body, tmp);
1888 gcc_assert (rse.ss == gfc_ss_terminator);
1889 gfc_trans_scalarizing_loops (&loop, &body);
1891 else
1893 /* Make sure that the temporary declaration survives by merging
1894 all the loop declarations into the current context. */
1895 for (n = 0; n < loop.dimen; n++)
1897 gfc_merge_block_scope (&body);
1898 body = loop.code[loop.order[n]];
1900 gfc_merge_block_scope (&body);
1903 /* Add the post block after the second loop, so that any
1904 freeing of allocated memory is done at the right time. */
1905 gfc_add_block_to_block (&parmse->pre, &loop.pre);
1907 /**********Copy the temporary back again.*********/
1909 gfc_init_se (&lse, NULL);
1910 gfc_init_se (&rse, NULL);
1912 /* Walk the argument expression. */
1913 lss = gfc_walk_expr (expr);
1914 rse.ss = loop.temp_ss;
1915 lse.ss = lss;
1917 /* Initialize the scalarizer. */
1918 gfc_init_loopinfo (&loop2);
1919 gfc_add_ss_to_loop (&loop2, lss);
1921 /* Calculate the bounds of the scalarization. */
1922 gfc_conv_ss_startstride (&loop2);
1924 /* Setup the scalarizing loops. */
1925 gfc_conv_loop_setup (&loop2);
1927 gfc_copy_loopinfo_to_se (&lse, &loop2);
1928 gfc_copy_loopinfo_to_se (&rse, &loop2);
1930 gfc_mark_ss_chain_used (lss, 1);
1931 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1933 /* Declare the variable to hold the temporary offset and start the
1934 scalarized loop body. */
1935 offset = gfc_create_var (gfc_array_index_type, NULL);
1936 gfc_start_scalarized_body (&loop2, &body);
1938 /* Build the offsets for the temporary from the loop variables. The
1939 temporary array has lbounds of zero and strides of one in all
1940 dimensions, so this is very simple. The offset is only computed
1941 outside the innermost loop, so the overall transfer could be
1942 optimized further. */
1943 info = &rse.ss->data.info;
1945 tmp_index = gfc_index_zero_node;
1946 for (n = info->dimen - 1; n > 0; n--)
1948 tree tmp_str;
1949 tmp = rse.loop->loopvar[n];
1950 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1951 tmp, rse.loop->from[n]);
1952 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1953 tmp, tmp_index);
1955 tmp_str = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1956 rse.loop->to[n-1], rse.loop->from[n-1]);
1957 tmp_str = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1958 tmp_str, gfc_index_one_node);
1960 tmp_index = fold_build2 (MULT_EXPR, gfc_array_index_type,
1961 tmp, tmp_str);
1964 tmp_index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1965 tmp_index, rse.loop->from[0]);
1966 gfc_add_modify_expr (&rse.loop->code[0], offset, tmp_index);
1968 tmp_index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1969 rse.loop->loopvar[0], offset);
1971 /* Now use the offset for the reference. */
1972 tmp = build_fold_indirect_ref (info->data);
1973 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
1975 if (expr->ts.type == BT_CHARACTER)
1976 rse.string_length = expr->ts.cl->backend_decl;
1978 gfc_conv_expr (&lse, expr);
1980 gcc_assert (lse.ss == gfc_ss_terminator);
1982 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
1983 gfc_add_expr_to_block (&body, tmp);
1985 /* Generate the copying loops. */
1986 gfc_trans_scalarizing_loops (&loop2, &body);
1988 /* Wrap the whole thing up by adding the second loop to the post-block
1989 and following it by the post-block of the first loop. In this way,
1990 if the temporary needs freeing, it is done after use! */
1991 if (intent != INTENT_IN)
1993 gfc_add_block_to_block (&parmse->post, &loop2.pre);
1994 gfc_add_block_to_block (&parmse->post, &loop2.post);
1997 gfc_add_block_to_block (&parmse->post, &loop.post);
1999 gfc_cleanup_loop (&loop);
2000 gfc_cleanup_loop (&loop2);
2002 /* Pass the string length to the argument expression. */
2003 if (expr->ts.type == BT_CHARACTER)
2004 parmse->string_length = expr->ts.cl->backend_decl;
2006 /* We want either the address for the data or the address of the descriptor,
2007 depending on the mode of passing array arguments. */
2008 if (g77)
2009 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2010 else
2011 parmse->expr = build_fold_addr_expr (parmse->expr);
2013 return;
2017 /* Generate the code for argument list functions. */
2019 static void
2020 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2022 /* Pass by value for g77 %VAL(arg), pass the address
2023 indirectly for %LOC, else by reference. Thus %REF
2024 is a "do-nothing" and %LOC is the same as an F95
2025 pointer. */
2026 if (strncmp (name, "%VAL", 4) == 0)
2027 gfc_conv_expr (se, expr);
2028 else if (strncmp (name, "%LOC", 4) == 0)
2030 gfc_conv_expr_reference (se, expr);
2031 se->expr = gfc_build_addr_expr (NULL, se->expr);
2033 else if (strncmp (name, "%REF", 4) == 0)
2034 gfc_conv_expr_reference (se, expr);
2035 else
2036 gfc_error ("Unknown argument list function at %L", &expr->where);
2040 /* Generate code for a procedure call. Note can return se->post != NULL.
2041 If se->direct_byref is set then se->expr contains the return parameter.
2042 Return nonzero, if the call has alternate specifiers. */
2045 gfc_conv_function_call (gfc_se * se, gfc_symbol * sym,
2046 gfc_actual_arglist * arg, tree append_args)
2048 gfc_interface_mapping mapping;
2049 tree arglist;
2050 tree retargs;
2051 tree tmp;
2052 tree fntype;
2053 gfc_se parmse;
2054 gfc_ss *argss;
2055 gfc_ss_info *info;
2056 int byref;
2057 int parm_kind;
2058 tree type;
2059 tree var;
2060 tree len;
2061 tree stringargs;
2062 gfc_formal_arglist *formal;
2063 int has_alternate_specifier = 0;
2064 bool need_interface_mapping;
2065 bool callee_alloc;
2066 gfc_typespec ts;
2067 gfc_charlen cl;
2068 gfc_expr *e;
2069 gfc_symbol *fsym;
2070 stmtblock_t post;
2071 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2073 arglist = NULL_TREE;
2074 retargs = NULL_TREE;
2075 stringargs = NULL_TREE;
2076 var = NULL_TREE;
2077 len = NULL_TREE;
2079 if (sym->from_intmod == INTMOD_ISO_C_BINDING)
2081 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2083 if (arg->expr->rank == 0)
2084 gfc_conv_expr_reference (se, arg->expr);
2085 else
2087 int f;
2088 /* This is really the actual arg because no formal arglist is
2089 created for C_LOC. */
2090 fsym = arg->expr->symtree->n.sym;
2092 /* We should want it to do g77 calling convention. */
2093 f = (fsym != NULL)
2094 && !(fsym->attr.pointer || fsym->attr.allocatable)
2095 && fsym->as->type != AS_ASSUMED_SHAPE;
2096 f = f || !sym->attr.always_explicit;
2098 argss = gfc_walk_expr (arg->expr);
2099 gfc_conv_array_parameter (se, arg->expr, argss, f);
2102 return 0;
2104 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2106 arg->expr->ts.type = sym->ts.derived->ts.type;
2107 arg->expr->ts.f90_type = sym->ts.derived->ts.f90_type;
2108 arg->expr->ts.kind = sym->ts.derived->ts.kind;
2109 gfc_conv_expr_reference (se, arg->expr);
2111 return 0;
2115 if (se->ss != NULL)
2117 if (!sym->attr.elemental)
2119 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2120 if (se->ss->useflags)
2122 gcc_assert (gfc_return_by_reference (sym)
2123 && sym->result->attr.dimension);
2124 gcc_assert (se->loop != NULL);
2126 /* Access the previously obtained result. */
2127 gfc_conv_tmp_array_ref (se);
2128 gfc_advance_se_ss_chain (se);
2129 return 0;
2132 info = &se->ss->data.info;
2134 else
2135 info = NULL;
2137 gfc_init_block (&post);
2138 gfc_init_interface_mapping (&mapping);
2139 need_interface_mapping = ((sym->ts.type == BT_CHARACTER
2140 && sym->ts.cl->length
2141 && sym->ts.cl->length->expr_type
2142 != EXPR_CONSTANT)
2143 || sym->attr.dimension);
2144 formal = sym->formal;
2145 /* Evaluate the arguments. */
2146 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
2148 e = arg->expr;
2149 fsym = formal ? formal->sym : NULL;
2150 parm_kind = MISSING;
2151 if (e == NULL)
2154 if (se->ignore_optional)
2156 /* Some intrinsics have already been resolved to the correct
2157 parameters. */
2158 continue;
2160 else if (arg->label)
2162 has_alternate_specifier = 1;
2163 continue;
2165 else
2167 /* Pass a NULL pointer for an absent arg. */
2168 gfc_init_se (&parmse, NULL);
2169 parmse.expr = null_pointer_node;
2170 if (arg->missing_arg_type == BT_CHARACTER)
2171 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2174 else if (se->ss && se->ss->useflags)
2176 /* An elemental function inside a scalarized loop. */
2177 gfc_init_se (&parmse, se);
2178 gfc_conv_expr_reference (&parmse, e);
2179 parm_kind = ELEMENTAL;
2181 else
2183 /* A scalar or transformational function. */
2184 gfc_init_se (&parmse, NULL);
2185 argss = gfc_walk_expr (e);
2187 if (argss == gfc_ss_terminator)
2189 if (fsym && fsym->attr.value)
2191 if (fsym->ts.type == BT_CHARACTER
2192 && fsym->ts.is_c_interop
2193 && fsym->ns->proc_name != NULL
2194 && fsym->ns->proc_name->attr.is_bind_c)
2196 parmse.expr = NULL;
2197 gfc_conv_scalar_char_value (fsym, &parmse, &e);
2198 if (parmse.expr == NULL)
2199 gfc_conv_expr (&parmse, e);
2201 else
2202 gfc_conv_expr (&parmse, e);
2204 else if (arg->name && arg->name[0] == '%')
2205 /* Argument list functions %VAL, %LOC and %REF are signalled
2206 through arg->name. */
2207 conv_arglist_function (&parmse, arg->expr, arg->name);
2208 else if ((e->expr_type == EXPR_FUNCTION)
2209 && e->symtree->n.sym->attr.pointer
2210 && fsym && fsym->attr.target)
2212 gfc_conv_expr (&parmse, e);
2213 parmse.expr = build_fold_addr_expr (parmse.expr);
2215 else
2217 gfc_conv_expr_reference (&parmse, e);
2218 if (fsym && fsym->attr.pointer
2219 && fsym->attr.flavor != FL_PROCEDURE
2220 && e->expr_type != EXPR_NULL)
2222 /* Scalar pointer dummy args require an extra level of
2223 indirection. The null pointer already contains
2224 this level of indirection. */
2225 parm_kind = SCALAR_POINTER;
2226 parmse.expr = build_fold_addr_expr (parmse.expr);
2230 else
2232 /* If the procedure requires an explicit interface, the actual
2233 argument is passed according to the corresponding formal
2234 argument. If the corresponding formal argument is a POINTER,
2235 ALLOCATABLE or assumed shape, we do not use g77's calling
2236 convention, and pass the address of the array descriptor
2237 instead. Otherwise we use g77's calling convention. */
2238 int f;
2239 f = (fsym != NULL)
2240 && !(fsym->attr.pointer || fsym->attr.allocatable)
2241 && fsym->as->type != AS_ASSUMED_SHAPE;
2242 f = f || !sym->attr.always_explicit;
2244 if (e->expr_type == EXPR_VARIABLE
2245 && is_subref_array (e))
2246 /* The actual argument is a component reference to an
2247 array of derived types. In this case, the argument
2248 is converted to a temporary, which is passed and then
2249 written back after the procedure call. */
2250 gfc_conv_subref_array_arg (&parmse, e, f,
2251 fsym ? fsym->attr.intent : INTENT_INOUT);
2252 else
2253 gfc_conv_array_parameter (&parmse, e, argss, f);
2255 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2256 allocated on entry, it must be deallocated. */
2257 if (fsym && fsym->attr.allocatable
2258 && fsym->attr.intent == INTENT_OUT)
2260 tmp = build_fold_indirect_ref (parmse.expr);
2261 tmp = gfc_trans_dealloc_allocated (tmp);
2262 gfc_add_expr_to_block (&se->pre, tmp);
2268 /* The case with fsym->attr.optional is that of a user subroutine
2269 with an interface indicating an optional argument. When we call
2270 an intrinsic subroutine, however, fsym is NULL, but we might still
2271 have an optional argument, so we proceed to the substitution
2272 just in case. */
2273 if (e && (fsym == NULL || fsym->attr.optional))
2275 /* If an optional argument is itself an optional dummy argument,
2276 check its presence and substitute a null if absent. */
2277 if (e->expr_type == EXPR_VARIABLE
2278 && e->symtree->n.sym->attr.optional)
2279 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts);
2282 if (fsym && e)
2284 /* Obtain the character length of an assumed character length
2285 length procedure from the typespec. */
2286 if (fsym->ts.type == BT_CHARACTER
2287 && parmse.string_length == NULL_TREE
2288 && e->ts.type == BT_PROCEDURE
2289 && e->symtree->n.sym->ts.type == BT_CHARACTER
2290 && e->symtree->n.sym->ts.cl->length != NULL)
2292 gfc_conv_const_charlen (e->symtree->n.sym->ts.cl);
2293 parmse.string_length = e->symtree->n.sym->ts.cl->backend_decl;
2297 if (fsym && need_interface_mapping)
2298 gfc_add_interface_mapping (&mapping, fsym, &parmse);
2300 gfc_add_block_to_block (&se->pre, &parmse.pre);
2301 gfc_add_block_to_block (&post, &parmse.post);
2303 /* Allocated allocatable components of derived types must be
2304 deallocated for INTENT(OUT) dummy arguments and non-variable
2305 scalars. Non-variable arrays are dealt with in trans-array.c
2306 (gfc_conv_array_parameter). */
2307 if (e && e->ts.type == BT_DERIVED
2308 && e->ts.derived->attr.alloc_comp
2309 && ((formal && formal->sym->attr.intent == INTENT_OUT)
2311 (e->expr_type != EXPR_VARIABLE && !e->rank)))
2313 int parm_rank;
2314 tmp = build_fold_indirect_ref (parmse.expr);
2315 parm_rank = e->rank;
2316 switch (parm_kind)
2318 case (ELEMENTAL):
2319 case (SCALAR):
2320 parm_rank = 0;
2321 break;
2323 case (SCALAR_POINTER):
2324 tmp = build_fold_indirect_ref (tmp);
2325 break;
2326 case (ARRAY):
2327 tmp = parmse.expr;
2328 break;
2331 tmp = gfc_deallocate_alloc_comp (e->ts.derived, tmp, parm_rank);
2332 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
2333 tmp = build3_v (COND_EXPR, gfc_conv_expr_present (e->symtree->n.sym),
2334 tmp, build_empty_stmt ());
2336 if (e->expr_type != EXPR_VARIABLE)
2337 /* Don't deallocate non-variables until they have been used. */
2338 gfc_add_expr_to_block (&se->post, tmp);
2339 else
2341 gcc_assert (formal && formal->sym->attr.intent == INTENT_OUT);
2342 gfc_add_expr_to_block (&se->pre, tmp);
2346 /* Character strings are passed as two parameters, a length and a
2347 pointer. */
2348 if (parmse.string_length != NULL_TREE)
2349 stringargs = gfc_chainon_list (stringargs, parmse.string_length);
2351 arglist = gfc_chainon_list (arglist, parmse.expr);
2353 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
2355 ts = sym->ts;
2356 if (ts.type == BT_CHARACTER)
2358 if (sym->ts.cl->length == NULL)
2360 /* Assumed character length results are not allowed by 5.1.1.5 of the
2361 standard and are trapped in resolve.c; except in the case of SPREAD
2362 (and other intrinsics?) and dummy functions. In the case of SPREAD,
2363 we take the character length of the first argument for the result.
2364 For dummies, we have to look through the formal argument list for
2365 this function and use the character length found there.*/
2366 if (!sym->attr.dummy)
2367 cl.backend_decl = TREE_VALUE (stringargs);
2368 else
2370 formal = sym->ns->proc_name->formal;
2371 for (; formal; formal = formal->next)
2372 if (strcmp (formal->sym->name, sym->name) == 0)
2373 cl.backend_decl = formal->sym->ts.cl->backend_decl;
2376 else
2378 tree tmp;
2380 /* Calculate the length of the returned string. */
2381 gfc_init_se (&parmse, NULL);
2382 if (need_interface_mapping)
2383 gfc_apply_interface_mapping (&mapping, &parmse, sym->ts.cl->length);
2384 else
2385 gfc_conv_expr (&parmse, sym->ts.cl->length);
2386 gfc_add_block_to_block (&se->pre, &parmse.pre);
2387 gfc_add_block_to_block (&se->post, &parmse.post);
2389 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
2390 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
2391 build_int_cst (gfc_charlen_type_node, 0));
2392 cl.backend_decl = tmp;
2395 /* Set up a charlen structure for it. */
2396 cl.next = NULL;
2397 cl.length = NULL;
2398 ts.cl = &cl;
2400 len = cl.backend_decl;
2403 byref = gfc_return_by_reference (sym);
2404 if (byref)
2406 if (se->direct_byref)
2408 /* Sometimes, too much indirection can be applied; eg. for
2409 function_result = array_valued_recursive_function. */
2410 if (TREE_TYPE (TREE_TYPE (se->expr))
2411 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
2412 && GFC_DESCRIPTOR_TYPE_P
2413 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
2414 se->expr = build_fold_indirect_ref (se->expr);
2416 retargs = gfc_chainon_list (retargs, se->expr);
2418 else if (sym->result->attr.dimension)
2420 gcc_assert (se->loop && info);
2422 /* Set the type of the array. */
2423 tmp = gfc_typenode_for_spec (&ts);
2424 info->dimen = se->loop->dimen;
2426 /* Evaluate the bounds of the result, if known. */
2427 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
2429 /* Create a temporary to store the result. In case the function
2430 returns a pointer, the temporary will be a shallow copy and
2431 mustn't be deallocated. */
2432 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
2433 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
2434 false, !sym->attr.pointer, callee_alloc);
2436 /* Pass the temporary as the first argument. */
2437 tmp = info->descriptor;
2438 tmp = build_fold_addr_expr (tmp);
2439 retargs = gfc_chainon_list (retargs, tmp);
2441 else if (ts.type == BT_CHARACTER)
2443 /* Pass the string length. */
2444 type = gfc_get_character_type (ts.kind, ts.cl);
2445 type = build_pointer_type (type);
2447 /* Return an address to a char[0:len-1]* temporary for
2448 character pointers. */
2449 if (sym->attr.pointer || sym->attr.allocatable)
2451 /* Build char[0:len-1] * pstr. */
2452 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
2453 build_int_cst (gfc_charlen_type_node, 1));
2454 tmp = build_range_type (gfc_array_index_type,
2455 gfc_index_zero_node, tmp);
2456 tmp = build_array_type (gfc_character1_type_node, tmp);
2457 var = gfc_create_var (build_pointer_type (tmp), "pstr");
2459 /* Provide an address expression for the function arguments. */
2460 var = build_fold_addr_expr (var);
2462 else
2463 var = gfc_conv_string_tmp (se, type, len);
2465 retargs = gfc_chainon_list (retargs, var);
2467 else
2469 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
2471 type = gfc_get_complex_type (ts.kind);
2472 var = build_fold_addr_expr (gfc_create_var (type, "cmplx"));
2473 retargs = gfc_chainon_list (retargs, var);
2476 /* Add the string length to the argument list. */
2477 if (ts.type == BT_CHARACTER)
2478 retargs = gfc_chainon_list (retargs, len);
2480 gfc_free_interface_mapping (&mapping);
2482 /* Add the return arguments. */
2483 arglist = chainon (retargs, arglist);
2485 /* Add the hidden string length parameters to the arguments. */
2486 arglist = chainon (arglist, stringargs);
2488 /* We may want to append extra arguments here. This is used e.g. for
2489 calls to libgfortran_matmul_??, which need extra information. */
2490 if (append_args != NULL_TREE)
2491 arglist = chainon (arglist, append_args);
2493 /* Generate the actual call. */
2494 gfc_conv_function_val (se, sym);
2496 /* If there are alternate return labels, function type should be
2497 integer. Can't modify the type in place though, since it can be shared
2498 with other functions. For dummy arguments, the typing is done to
2499 to this result, even if it has to be repeated for each call. */
2500 if (has_alternate_specifier
2501 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
2503 if (!sym->attr.dummy)
2505 TREE_TYPE (sym->backend_decl)
2506 = build_function_type (integer_type_node,
2507 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
2508 se->expr = build_fold_addr_expr (sym->backend_decl);
2510 else
2511 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
2514 fntype = TREE_TYPE (TREE_TYPE (se->expr));
2515 se->expr = build_call_list (TREE_TYPE (fntype), se->expr, arglist);
2517 /* If we have a pointer function, but we don't want a pointer, e.g.
2518 something like
2519 x = f()
2520 where f is pointer valued, we have to dereference the result. */
2521 if (!se->want_pointer && !byref && sym->attr.pointer)
2522 se->expr = build_fold_indirect_ref (se->expr);
2524 /* f2c calling conventions require a scalar default real function to
2525 return a double precision result. Convert this back to default
2526 real. We only care about the cases that can happen in Fortran 77.
2528 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
2529 && sym->ts.kind == gfc_default_real_kind
2530 && !sym->attr.always_explicit)
2531 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
2533 /* A pure function may still have side-effects - it may modify its
2534 parameters. */
2535 TREE_SIDE_EFFECTS (se->expr) = 1;
2536 #if 0
2537 if (!sym->attr.pure)
2538 TREE_SIDE_EFFECTS (se->expr) = 1;
2539 #endif
2541 if (byref)
2543 /* Add the function call to the pre chain. There is no expression. */
2544 gfc_add_expr_to_block (&se->pre, se->expr);
2545 se->expr = NULL_TREE;
2547 if (!se->direct_byref)
2549 if (sym->attr.dimension)
2551 if (flag_bounds_check)
2553 /* Check the data pointer hasn't been modified. This would
2554 happen in a function returning a pointer. */
2555 tmp = gfc_conv_descriptor_data_get (info->descriptor);
2556 tmp = fold_build2 (NE_EXPR, boolean_type_node,
2557 tmp, info->data);
2558 gfc_trans_runtime_check (tmp, &se->pre, NULL, gfc_msg_fault);
2560 se->expr = info->descriptor;
2561 /* Bundle in the string length. */
2562 se->string_length = len;
2564 else if (sym->ts.type == BT_CHARACTER)
2566 /* Dereference for character pointer results. */
2567 if (sym->attr.pointer || sym->attr.allocatable)
2568 se->expr = build_fold_indirect_ref (var);
2569 else
2570 se->expr = var;
2572 se->string_length = len;
2574 else
2576 gcc_assert (sym->ts.type == BT_COMPLEX && gfc_option.flag_f2c);
2577 se->expr = build_fold_indirect_ref (var);
2582 /* Follow the function call with the argument post block. */
2583 if (byref)
2584 gfc_add_block_to_block (&se->pre, &post);
2585 else
2586 gfc_add_block_to_block (&se->post, &post);
2588 return has_alternate_specifier;
2592 /* Generate code to copy a string. */
2594 static void
2595 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
2596 tree slength, tree src)
2598 tree tmp, dlen, slen;
2599 tree dsc;
2600 tree ssc;
2601 tree cond;
2602 tree cond2;
2603 tree tmp2;
2604 tree tmp3;
2605 tree tmp4;
2606 stmtblock_t tempblock;
2608 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
2609 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
2611 /* Deal with single character specially. */
2612 dsc = gfc_to_single_character (dlen, dest);
2613 ssc = gfc_to_single_character (slen, src);
2614 if (dsc != NULL_TREE && ssc != NULL_TREE)
2616 gfc_add_modify_expr (block, dsc, ssc);
2617 return;
2620 /* Do nothing if the destination length is zero. */
2621 cond = fold_build2 (GT_EXPR, boolean_type_node, dlen,
2622 build_int_cst (size_type_node, 0));
2624 /* The following code was previously in _gfortran_copy_string:
2626 // The two strings may overlap so we use memmove.
2627 void
2628 copy_string (GFC_INTEGER_4 destlen, char * dest,
2629 GFC_INTEGER_4 srclen, const char * src)
2631 if (srclen >= destlen)
2633 // This will truncate if too long.
2634 memmove (dest, src, destlen);
2636 else
2638 memmove (dest, src, srclen);
2639 // Pad with spaces.
2640 memset (&dest[srclen], ' ', destlen - srclen);
2644 We're now doing it here for better optimization, but the logic
2645 is the same. */
2647 /* Truncate string if source is too long. */
2648 cond2 = fold_build2 (GE_EXPR, boolean_type_node, slen, dlen);
2649 tmp2 = build_call_expr (built_in_decls[BUILT_IN_MEMMOVE],
2650 3, dest, src, dlen);
2652 /* Else copy and pad with spaces. */
2653 tmp3 = build_call_expr (built_in_decls[BUILT_IN_MEMMOVE],
2654 3, dest, src, slen);
2656 tmp4 = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, dest,
2657 fold_convert (sizetype, slen));
2658 tmp4 = build_call_expr (built_in_decls[BUILT_IN_MEMSET], 3,
2659 tmp4,
2660 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
2661 lang_hooks.to_target_charset (' ')),
2662 fold_build2 (MINUS_EXPR, TREE_TYPE(dlen),
2663 dlen, slen));
2665 gfc_init_block (&tempblock);
2666 gfc_add_expr_to_block (&tempblock, tmp3);
2667 gfc_add_expr_to_block (&tempblock, tmp4);
2668 tmp3 = gfc_finish_block (&tempblock);
2670 /* The whole copy_string function is there. */
2671 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp2, tmp3);
2672 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp, build_empty_stmt ());
2673 gfc_add_expr_to_block (block, tmp);
2677 /* Translate a statement function.
2678 The value of a statement function reference is obtained by evaluating the
2679 expression using the values of the actual arguments for the values of the
2680 corresponding dummy arguments. */
2682 static void
2683 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
2685 gfc_symbol *sym;
2686 gfc_symbol *fsym;
2687 gfc_formal_arglist *fargs;
2688 gfc_actual_arglist *args;
2689 gfc_se lse;
2690 gfc_se rse;
2691 gfc_saved_var *saved_vars;
2692 tree *temp_vars;
2693 tree type;
2694 tree tmp;
2695 int n;
2697 sym = expr->symtree->n.sym;
2698 args = expr->value.function.actual;
2699 gfc_init_se (&lse, NULL);
2700 gfc_init_se (&rse, NULL);
2702 n = 0;
2703 for (fargs = sym->formal; fargs; fargs = fargs->next)
2704 n++;
2705 saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
2706 temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
2708 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2710 /* Each dummy shall be specified, explicitly or implicitly, to be
2711 scalar. */
2712 gcc_assert (fargs->sym->attr.dimension == 0);
2713 fsym = fargs->sym;
2715 /* Create a temporary to hold the value. */
2716 type = gfc_typenode_for_spec (&fsym->ts);
2717 temp_vars[n] = gfc_create_var (type, fsym->name);
2719 if (fsym->ts.type == BT_CHARACTER)
2721 /* Copy string arguments. */
2722 tree arglen;
2724 gcc_assert (fsym->ts.cl && fsym->ts.cl->length
2725 && fsym->ts.cl->length->expr_type == EXPR_CONSTANT);
2727 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2728 tmp = gfc_build_addr_expr (build_pointer_type (type),
2729 temp_vars[n]);
2731 gfc_conv_expr (&rse, args->expr);
2732 gfc_conv_string_parameter (&rse);
2733 gfc_add_block_to_block (&se->pre, &lse.pre);
2734 gfc_add_block_to_block (&se->pre, &rse.pre);
2736 gfc_trans_string_copy (&se->pre, arglen, tmp, rse.string_length,
2737 rse.expr);
2738 gfc_add_block_to_block (&se->pre, &lse.post);
2739 gfc_add_block_to_block (&se->pre, &rse.post);
2741 else
2743 /* For everything else, just evaluate the expression. */
2744 gfc_conv_expr (&lse, args->expr);
2746 gfc_add_block_to_block (&se->pre, &lse.pre);
2747 gfc_add_modify_expr (&se->pre, temp_vars[n], lse.expr);
2748 gfc_add_block_to_block (&se->pre, &lse.post);
2751 args = args->next;
2754 /* Use the temporary variables in place of the real ones. */
2755 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2756 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
2758 gfc_conv_expr (se, sym->value);
2760 if (sym->ts.type == BT_CHARACTER)
2762 gfc_conv_const_charlen (sym->ts.cl);
2764 /* Force the expression to the correct length. */
2765 if (!INTEGER_CST_P (se->string_length)
2766 || tree_int_cst_lt (se->string_length,
2767 sym->ts.cl->backend_decl))
2769 type = gfc_get_character_type (sym->ts.kind, sym->ts.cl);
2770 tmp = gfc_create_var (type, sym->name);
2771 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
2772 gfc_trans_string_copy (&se->pre, sym->ts.cl->backend_decl, tmp,
2773 se->string_length, se->expr);
2774 se->expr = tmp;
2776 se->string_length = sym->ts.cl->backend_decl;
2779 /* Restore the original variables. */
2780 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2781 gfc_restore_sym (fargs->sym, &saved_vars[n]);
2782 gfc_free (saved_vars);
2786 /* Translate a function expression. */
2788 static void
2789 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
2791 gfc_symbol *sym;
2793 if (expr->value.function.isym)
2795 gfc_conv_intrinsic_function (se, expr);
2796 return;
2799 /* We distinguish statement functions from general functions to improve
2800 runtime performance. */
2801 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2803 gfc_conv_statement_function (se, expr);
2804 return;
2807 /* expr.value.function.esym is the resolved (specific) function symbol for
2808 most functions. However this isn't set for dummy procedures. */
2809 sym = expr->value.function.esym;
2810 if (!sym)
2811 sym = expr->symtree->n.sym;
2812 gfc_conv_function_call (se, sym, expr->value.function.actual, NULL_TREE);
2816 static void
2817 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
2819 gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
2820 gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
2822 gfc_conv_tmp_array_ref (se);
2823 gfc_advance_se_ss_chain (se);
2827 /* Build a static initializer. EXPR is the expression for the initial value.
2828 The other parameters describe the variable of the component being
2829 initialized. EXPR may be null. */
2831 tree
2832 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
2833 bool array, bool pointer)
2835 gfc_se se;
2837 if (!(expr || pointer))
2838 return NULL_TREE;
2840 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
2841 (these are the only two iso_c_binding derived types that can be
2842 used as initialization expressions). If so, we need to modify
2843 the 'expr' to be that for a (void *). */
2844 if (expr != NULL && expr->ts.type == BT_DERIVED
2845 && expr->ts.is_iso_c && expr->ts.derived)
2847 gfc_symbol *derived = expr->ts.derived;
2849 expr = gfc_int_expr (0);
2851 /* The derived symbol has already been converted to a (void *). Use
2852 its kind. */
2853 expr->ts.f90_type = derived->ts.f90_type;
2854 expr->ts.kind = derived->ts.kind;
2857 if (array)
2859 /* Arrays need special handling. */
2860 if (pointer)
2861 return gfc_build_null_descriptor (type);
2862 else
2863 return gfc_conv_array_initializer (type, expr);
2865 else if (pointer)
2866 return fold_convert (type, null_pointer_node);
2867 else
2869 switch (ts->type)
2871 case BT_DERIVED:
2872 gfc_init_se (&se, NULL);
2873 gfc_conv_structure (&se, expr, 1);
2874 return se.expr;
2876 case BT_CHARACTER:
2877 return gfc_conv_string_init (ts->cl->backend_decl,expr);
2879 default:
2880 gfc_init_se (&se, NULL);
2881 gfc_conv_constant (&se, expr);
2882 return se.expr;
2887 static tree
2888 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
2890 gfc_se rse;
2891 gfc_se lse;
2892 gfc_ss *rss;
2893 gfc_ss *lss;
2894 stmtblock_t body;
2895 stmtblock_t block;
2896 gfc_loopinfo loop;
2897 int n;
2898 tree tmp;
2900 gfc_start_block (&block);
2902 /* Initialize the scalarizer. */
2903 gfc_init_loopinfo (&loop);
2905 gfc_init_se (&lse, NULL);
2906 gfc_init_se (&rse, NULL);
2908 /* Walk the rhs. */
2909 rss = gfc_walk_expr (expr);
2910 if (rss == gfc_ss_terminator)
2912 /* The rhs is scalar. Add a ss for the expression. */
2913 rss = gfc_get_ss ();
2914 rss->next = gfc_ss_terminator;
2915 rss->type = GFC_SS_SCALAR;
2916 rss->expr = expr;
2919 /* Create a SS for the destination. */
2920 lss = gfc_get_ss ();
2921 lss->type = GFC_SS_COMPONENT;
2922 lss->expr = NULL;
2923 lss->shape = gfc_get_shape (cm->as->rank);
2924 lss->next = gfc_ss_terminator;
2925 lss->data.info.dimen = cm->as->rank;
2926 lss->data.info.descriptor = dest;
2927 lss->data.info.data = gfc_conv_array_data (dest);
2928 lss->data.info.offset = gfc_conv_array_offset (dest);
2929 for (n = 0; n < cm->as->rank; n++)
2931 lss->data.info.dim[n] = n;
2932 lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
2933 lss->data.info.stride[n] = gfc_index_one_node;
2935 mpz_init (lss->shape[n]);
2936 mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
2937 cm->as->lower[n]->value.integer);
2938 mpz_add_ui (lss->shape[n], lss->shape[n], 1);
2941 /* Associate the SS with the loop. */
2942 gfc_add_ss_to_loop (&loop, lss);
2943 gfc_add_ss_to_loop (&loop, rss);
2945 /* Calculate the bounds of the scalarization. */
2946 gfc_conv_ss_startstride (&loop);
2948 /* Setup the scalarizing loops. */
2949 gfc_conv_loop_setup (&loop);
2951 /* Setup the gfc_se structures. */
2952 gfc_copy_loopinfo_to_se (&lse, &loop);
2953 gfc_copy_loopinfo_to_se (&rse, &loop);
2955 rse.ss = rss;
2956 gfc_mark_ss_chain_used (rss, 1);
2957 lse.ss = lss;
2958 gfc_mark_ss_chain_used (lss, 1);
2960 /* Start the scalarized loop body. */
2961 gfc_start_scalarized_body (&loop, &body);
2963 gfc_conv_tmp_array_ref (&lse);
2964 if (cm->ts.type == BT_CHARACTER)
2965 lse.string_length = cm->ts.cl->backend_decl;
2967 gfc_conv_expr (&rse, expr);
2969 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
2970 gfc_add_expr_to_block (&body, tmp);
2972 gcc_assert (rse.ss == gfc_ss_terminator);
2974 /* Generate the copying loops. */
2975 gfc_trans_scalarizing_loops (&loop, &body);
2977 /* Wrap the whole thing up. */
2978 gfc_add_block_to_block (&block, &loop.pre);
2979 gfc_add_block_to_block (&block, &loop.post);
2981 for (n = 0; n < cm->as->rank; n++)
2982 mpz_clear (lss->shape[n]);
2983 gfc_free (lss->shape);
2985 gfc_cleanup_loop (&loop);
2987 return gfc_finish_block (&block);
2991 /* Assign a single component of a derived type constructor. */
2993 static tree
2994 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
2996 gfc_se se;
2997 gfc_se lse;
2998 gfc_ss *rss;
2999 stmtblock_t block;
3000 tree tmp;
3001 tree offset;
3002 int n;
3004 gfc_start_block (&block);
3006 if (cm->pointer)
3008 gfc_init_se (&se, NULL);
3009 /* Pointer component. */
3010 if (cm->dimension)
3012 /* Array pointer. */
3013 if (expr->expr_type == EXPR_NULL)
3014 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
3015 else
3017 rss = gfc_walk_expr (expr);
3018 se.direct_byref = 1;
3019 se.expr = dest;
3020 gfc_conv_expr_descriptor (&se, expr, rss);
3021 gfc_add_block_to_block (&block, &se.pre);
3022 gfc_add_block_to_block (&block, &se.post);
3025 else
3027 /* Scalar pointers. */
3028 se.want_pointer = 1;
3029 gfc_conv_expr (&se, expr);
3030 gfc_add_block_to_block (&block, &se.pre);
3031 gfc_add_modify_expr (&block, dest,
3032 fold_convert (TREE_TYPE (dest), se.expr));
3033 gfc_add_block_to_block (&block, &se.post);
3036 else if (cm->dimension)
3038 if (cm->allocatable && expr->expr_type == EXPR_NULL)
3039 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
3040 else if (cm->allocatable)
3042 tree tmp2;
3044 gfc_init_se (&se, NULL);
3046 rss = gfc_walk_expr (expr);
3047 se.want_pointer = 0;
3048 gfc_conv_expr_descriptor (&se, expr, rss);
3049 gfc_add_block_to_block (&block, &se.pre);
3051 tmp = fold_convert (TREE_TYPE (dest), se.expr);
3052 gfc_add_modify_expr (&block, dest, tmp);
3054 if (cm->ts.type == BT_DERIVED && cm->ts.derived->attr.alloc_comp)
3055 tmp = gfc_copy_alloc_comp (cm->ts.derived, se.expr, dest,
3056 cm->as->rank);
3057 else
3058 tmp = gfc_duplicate_allocatable (dest, se.expr,
3059 TREE_TYPE(cm->backend_decl),
3060 cm->as->rank);
3062 gfc_add_expr_to_block (&block, tmp);
3064 gfc_add_block_to_block (&block, &se.post);
3065 gfc_conv_descriptor_data_set (&block, se.expr, null_pointer_node);
3067 /* Shift the lbound and ubound of temporaries to being unity, rather
3068 than zero, based. Calculate the offset for all cases. */
3069 offset = gfc_conv_descriptor_offset (dest);
3070 gfc_add_modify_expr (&block, offset, gfc_index_zero_node);
3071 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
3072 for (n = 0; n < expr->rank; n++)
3074 if (expr->expr_type != EXPR_VARIABLE
3075 && expr->expr_type != EXPR_CONSTANT)
3077 tree span;
3078 tmp = gfc_conv_descriptor_ubound (dest, gfc_rank_cst[n]);
3079 span = fold_build2 (MINUS_EXPR, gfc_array_index_type, tmp,
3080 gfc_conv_descriptor_lbound (dest, gfc_rank_cst[n]));
3081 gfc_add_modify_expr (&block, tmp,
3082 fold_build2 (PLUS_EXPR,
3083 gfc_array_index_type,
3084 span, gfc_index_one_node));
3085 tmp = gfc_conv_descriptor_lbound (dest, gfc_rank_cst[n]);
3086 gfc_add_modify_expr (&block, tmp, gfc_index_one_node);
3088 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3089 gfc_conv_descriptor_lbound (dest,
3090 gfc_rank_cst[n]),
3091 gfc_conv_descriptor_stride (dest,
3092 gfc_rank_cst[n]));
3093 gfc_add_modify_expr (&block, tmp2, tmp);
3094 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp2);
3095 gfc_add_modify_expr (&block, offset, tmp);
3098 else
3100 tmp = gfc_trans_subarray_assign (dest, cm, expr);
3101 gfc_add_expr_to_block (&block, tmp);
3104 else if (expr->ts.type == BT_DERIVED)
3106 if (expr->expr_type != EXPR_STRUCTURE)
3108 gfc_init_se (&se, NULL);
3109 gfc_conv_expr (&se, expr);
3110 gfc_add_modify_expr (&block, dest,
3111 fold_convert (TREE_TYPE (dest), se.expr));
3113 else
3115 /* Nested constructors. */
3116 tmp = gfc_trans_structure_assign (dest, expr);
3117 gfc_add_expr_to_block (&block, tmp);
3120 else
3122 /* Scalar component. */
3123 gfc_init_se (&se, NULL);
3124 gfc_init_se (&lse, NULL);
3126 gfc_conv_expr (&se, expr);
3127 if (cm->ts.type == BT_CHARACTER)
3128 lse.string_length = cm->ts.cl->backend_decl;
3129 lse.expr = dest;
3130 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false);
3131 gfc_add_expr_to_block (&block, tmp);
3133 return gfc_finish_block (&block);
3136 /* Assign a derived type constructor to a variable. */
3138 static tree
3139 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
3141 gfc_constructor *c;
3142 gfc_component *cm;
3143 stmtblock_t block;
3144 tree field;
3145 tree tmp;
3147 gfc_start_block (&block);
3148 cm = expr->ts.derived->components;
3149 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
3151 /* Skip absent members in default initializers. */
3152 if (!c->expr)
3153 continue;
3155 /* Update the type/kind of the expression if it represents either
3156 C_NULL_PTR or C_NULL_FUNPTR. This is done here because this may
3157 be the first place reached for initializing output variables that
3158 have components of type C_PTR/C_FUNPTR that are initialized. */
3159 if (c->expr->ts.type == BT_DERIVED && c->expr->ts.derived
3160 && c->expr->ts.derived->attr.is_iso_c)
3162 c->expr->expr_type = EXPR_NULL;
3163 c->expr->ts.type = c->expr->ts.derived->ts.type;
3164 c->expr->ts.f90_type = c->expr->ts.derived->ts.f90_type;
3165 c->expr->ts.kind = c->expr->ts.derived->ts.kind;
3168 field = cm->backend_decl;
3169 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), dest, field, NULL_TREE);
3170 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
3171 gfc_add_expr_to_block (&block, tmp);
3173 return gfc_finish_block (&block);
3176 /* Build an expression for a constructor. If init is nonzero then
3177 this is part of a static variable initializer. */
3179 void
3180 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
3182 gfc_constructor *c;
3183 gfc_component *cm;
3184 tree val;
3185 tree type;
3186 tree tmp;
3187 VEC(constructor_elt,gc) *v = NULL;
3189 gcc_assert (se->ss == NULL);
3190 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
3191 type = gfc_typenode_for_spec (&expr->ts);
3193 if (!init)
3195 /* Create a temporary variable and fill it in. */
3196 se->expr = gfc_create_var (type, expr->ts.derived->name);
3197 tmp = gfc_trans_structure_assign (se->expr, expr);
3198 gfc_add_expr_to_block (&se->pre, tmp);
3199 return;
3202 cm = expr->ts.derived->components;
3204 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
3206 /* Skip absent members in default initializers and allocatable
3207 components. Although the latter have a default initializer
3208 of EXPR_NULL,... by default, the static nullify is not needed
3209 since this is done every time we come into scope. */
3210 if (!c->expr || cm->allocatable)
3211 continue;
3213 val = gfc_conv_initializer (c->expr, &cm->ts,
3214 TREE_TYPE (cm->backend_decl), cm->dimension, cm->pointer);
3216 /* Append it to the constructor list. */
3217 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
3219 se->expr = build_constructor (type, v);
3223 /* Translate a substring expression. */
3225 static void
3226 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
3228 gfc_ref *ref;
3230 ref = expr->ref;
3232 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
3234 se->expr = gfc_build_string_const (expr->value.character.length,
3235 expr->value.character.string);
3236 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
3237 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
3239 if (ref)
3240 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
3244 /* Entry point for expression translation. Evaluates a scalar quantity.
3245 EXPR is the expression to be translated, and SE is the state structure if
3246 called from within the scalarized. */
3248 void
3249 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
3251 if (se->ss && se->ss->expr == expr
3252 && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
3254 /* Substitute a scalar expression evaluated outside the scalarization
3255 loop. */
3256 se->expr = se->ss->data.scalar.expr;
3257 se->string_length = se->ss->string_length;
3258 gfc_advance_se_ss_chain (se);
3259 return;
3262 /* We need to convert the expressions for the iso_c_binding derived types.
3263 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
3264 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
3265 typespec for the C_PTR and C_FUNPTR symbols, which has already been
3266 updated to be an integer with a kind equal to the size of a (void *). */
3267 if (expr->ts.type == BT_DERIVED && expr->ts.derived
3268 && expr->ts.derived->attr.is_iso_c)
3270 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
3271 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
3273 /* Set expr_type to EXPR_NULL, which will result in
3274 null_pointer_node being used below. */
3275 expr->expr_type = EXPR_NULL;
3277 else
3279 /* Update the type/kind of the expression to be what the new
3280 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
3281 expr->ts.type = expr->ts.derived->ts.type;
3282 expr->ts.f90_type = expr->ts.derived->ts.f90_type;
3283 expr->ts.kind = expr->ts.derived->ts.kind;
3287 switch (expr->expr_type)
3289 case EXPR_OP:
3290 gfc_conv_expr_op (se, expr);
3291 break;
3293 case EXPR_FUNCTION:
3294 gfc_conv_function_expr (se, expr);
3295 break;
3297 case EXPR_CONSTANT:
3298 gfc_conv_constant (se, expr);
3299 break;
3301 case EXPR_VARIABLE:
3302 gfc_conv_variable (se, expr);
3303 break;
3305 case EXPR_NULL:
3306 se->expr = null_pointer_node;
3307 break;
3309 case EXPR_SUBSTRING:
3310 gfc_conv_substring_expr (se, expr);
3311 break;
3313 case EXPR_STRUCTURE:
3314 gfc_conv_structure (se, expr, 0);
3315 break;
3317 case EXPR_ARRAY:
3318 gfc_conv_array_constructor_expr (se, expr);
3319 break;
3321 default:
3322 gcc_unreachable ();
3323 break;
3327 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
3328 of an assignment. */
3329 void
3330 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
3332 gfc_conv_expr (se, expr);
3333 /* All numeric lvalues should have empty post chains. If not we need to
3334 figure out a way of rewriting an lvalue so that it has no post chain. */
3335 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
3338 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
3339 numeric expressions. Used for scalar values where inserting cleanup code
3340 is inconvenient. */
3341 void
3342 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
3344 tree val;
3346 gcc_assert (expr->ts.type != BT_CHARACTER);
3347 gfc_conv_expr (se, expr);
3348 if (se->post.head)
3350 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
3351 gfc_add_modify_expr (&se->pre, val, se->expr);
3352 se->expr = val;
3353 gfc_add_block_to_block (&se->pre, &se->post);
3357 /* Helper to translate and expression and convert it to a particular type. */
3358 void
3359 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
3361 gfc_conv_expr_val (se, expr);
3362 se->expr = convert (type, se->expr);
3366 /* Converts an expression so that it can be passed by reference. Scalar
3367 values only. */
3369 void
3370 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
3372 tree var;
3374 if (se->ss && se->ss->expr == expr
3375 && se->ss->type == GFC_SS_REFERENCE)
3377 se->expr = se->ss->data.scalar.expr;
3378 se->string_length = se->ss->string_length;
3379 gfc_advance_se_ss_chain (se);
3380 return;
3383 if (expr->ts.type == BT_CHARACTER)
3385 gfc_conv_expr (se, expr);
3386 gfc_conv_string_parameter (se);
3387 return;
3390 if (expr->expr_type == EXPR_VARIABLE)
3392 se->want_pointer = 1;
3393 gfc_conv_expr (se, expr);
3394 if (se->post.head)
3396 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3397 gfc_add_modify_expr (&se->pre, var, se->expr);
3398 gfc_add_block_to_block (&se->pre, &se->post);
3399 se->expr = var;
3401 return;
3404 if (expr->expr_type == EXPR_FUNCTION
3405 && expr->symtree->n.sym->attr.pointer
3406 && !expr->symtree->n.sym->attr.dimension)
3408 se->want_pointer = 1;
3409 gfc_conv_expr (se, expr);
3410 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3411 gfc_add_modify_expr (&se->pre, var, se->expr);
3412 se->expr = var;
3413 return;
3417 gfc_conv_expr (se, expr);
3419 /* Create a temporary var to hold the value. */
3420 if (TREE_CONSTANT (se->expr))
3422 tree tmp = se->expr;
3423 STRIP_TYPE_NOPS (tmp);
3424 var = build_decl (CONST_DECL, NULL, TREE_TYPE (tmp));
3425 DECL_INITIAL (var) = tmp;
3426 TREE_STATIC (var) = 1;
3427 pushdecl (var);
3429 else
3431 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3432 gfc_add_modify_expr (&se->pre, var, se->expr);
3434 gfc_add_block_to_block (&se->pre, &se->post);
3436 /* Take the address of that value. */
3437 se->expr = build_fold_addr_expr (var);
3441 tree
3442 gfc_trans_pointer_assign (gfc_code * code)
3444 return gfc_trans_pointer_assignment (code->expr, code->expr2);
3448 /* Generate code for a pointer assignment. */
3450 tree
3451 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
3453 gfc_se lse;
3454 gfc_se rse;
3455 gfc_ss *lss;
3456 gfc_ss *rss;
3457 stmtblock_t block;
3458 tree desc;
3459 tree tmp;
3460 tree decl;
3463 gfc_start_block (&block);
3465 gfc_init_se (&lse, NULL);
3467 lss = gfc_walk_expr (expr1);
3468 rss = gfc_walk_expr (expr2);
3469 if (lss == gfc_ss_terminator)
3471 /* Scalar pointers. */
3472 lse.want_pointer = 1;
3473 gfc_conv_expr (&lse, expr1);
3474 gcc_assert (rss == gfc_ss_terminator);
3475 gfc_init_se (&rse, NULL);
3476 rse.want_pointer = 1;
3477 gfc_conv_expr (&rse, expr2);
3478 gfc_add_block_to_block (&block, &lse.pre);
3479 gfc_add_block_to_block (&block, &rse.pre);
3480 gfc_add_modify_expr (&block, lse.expr,
3481 fold_convert (TREE_TYPE (lse.expr), rse.expr));
3482 gfc_add_block_to_block (&block, &rse.post);
3483 gfc_add_block_to_block (&block, &lse.post);
3485 else
3487 /* Array pointer. */
3488 gfc_conv_expr_descriptor (&lse, expr1, lss);
3489 switch (expr2->expr_type)
3491 case EXPR_NULL:
3492 /* Just set the data pointer to null. */
3493 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
3494 break;
3496 case EXPR_VARIABLE:
3497 /* Assign directly to the pointer's descriptor. */
3498 lse.direct_byref = 1;
3499 gfc_conv_expr_descriptor (&lse, expr2, rss);
3501 /* If this is a subreference array pointer assignment, use the rhs
3502 descriptor element size for the lhs span. */
3503 if (expr1->symtree->n.sym->attr.subref_array_pointer)
3505 decl = expr1->symtree->n.sym->backend_decl;
3506 gfc_init_se (&rse, NULL);
3507 rse.descriptor_only = 1;
3508 gfc_conv_expr (&rse, expr2);
3509 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
3510 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
3511 if (!INTEGER_CST_P (tmp))
3512 gfc_add_block_to_block (&lse.post, &rse.pre);
3513 gfc_add_modify_expr (&lse.post, GFC_DECL_SPAN(decl), tmp);
3516 break;
3518 default:
3519 /* Assign to a temporary descriptor and then copy that
3520 temporary to the pointer. */
3521 desc = lse.expr;
3522 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
3524 lse.expr = tmp;
3525 lse.direct_byref = 1;
3526 gfc_conv_expr_descriptor (&lse, expr2, rss);
3527 gfc_add_modify_expr (&lse.pre, desc, tmp);
3528 break;
3530 gfc_add_block_to_block (&block, &lse.pre);
3531 gfc_add_block_to_block (&block, &lse.post);
3533 return gfc_finish_block (&block);
3537 /* Makes sure se is suitable for passing as a function string parameter. */
3538 /* TODO: Need to check all callers fo this function. It may be abused. */
3540 void
3541 gfc_conv_string_parameter (gfc_se * se)
3543 tree type;
3545 if (TREE_CODE (se->expr) == STRING_CST)
3547 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3548 return;
3551 type = TREE_TYPE (se->expr);
3552 if (TYPE_STRING_FLAG (type))
3554 gcc_assert (TREE_CODE (se->expr) != INDIRECT_REF);
3555 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3558 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
3559 gcc_assert (se->string_length
3560 && TREE_CODE (TREE_TYPE (se->string_length)) == INTEGER_TYPE);
3564 /* Generate code for assignment of scalar variables. Includes character
3565 strings and derived types with allocatable components. */
3567 tree
3568 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
3569 bool l_is_temp, bool r_is_var)
3571 stmtblock_t block;
3572 tree tmp;
3573 tree cond;
3575 gfc_init_block (&block);
3577 if (ts.type == BT_CHARACTER)
3579 gcc_assert (lse->string_length != NULL_TREE
3580 && rse->string_length != NULL_TREE);
3582 gfc_conv_string_parameter (lse);
3583 gfc_conv_string_parameter (rse);
3585 gfc_add_block_to_block (&block, &lse->pre);
3586 gfc_add_block_to_block (&block, &rse->pre);
3588 gfc_trans_string_copy (&block, lse->string_length, lse->expr,
3589 rse->string_length, rse->expr);
3591 else if (ts.type == BT_DERIVED && ts.derived->attr.alloc_comp)
3593 cond = NULL_TREE;
3595 /* Are the rhs and the lhs the same? */
3596 if (r_is_var)
3598 cond = fold_build2 (EQ_EXPR, boolean_type_node,
3599 build_fold_addr_expr (lse->expr),
3600 build_fold_addr_expr (rse->expr));
3601 cond = gfc_evaluate_now (cond, &lse->pre);
3604 /* Deallocate the lhs allocated components as long as it is not
3605 the same as the rhs. This must be done following the assignment
3606 to prevent deallocating data that could be used in the rhs
3607 expression. */
3608 if (!l_is_temp)
3610 tmp = gfc_evaluate_now (lse->expr, &lse->pre);
3611 tmp = gfc_deallocate_alloc_comp (ts.derived, tmp, 0);
3612 if (r_is_var)
3613 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3614 gfc_add_expr_to_block (&lse->post, tmp);
3617 gfc_add_block_to_block (&block, &rse->pre);
3618 gfc_add_block_to_block (&block, &lse->pre);
3620 gfc_add_modify_expr (&block, lse->expr,
3621 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3623 /* Do a deep copy if the rhs is a variable, if it is not the
3624 same as the lhs. */
3625 if (r_is_var)
3627 tmp = gfc_copy_alloc_comp (ts.derived, rse->expr, lse->expr, 0);
3628 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3629 gfc_add_expr_to_block (&block, tmp);
3632 else
3634 gfc_add_block_to_block (&block, &lse->pre);
3635 gfc_add_block_to_block (&block, &rse->pre);
3637 gfc_add_modify_expr (&block, lse->expr,
3638 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3641 gfc_add_block_to_block (&block, &lse->post);
3642 gfc_add_block_to_block (&block, &rse->post);
3644 return gfc_finish_block (&block);
3648 /* Try to translate array(:) = func (...), where func is a transformational
3649 array function, without using a temporary. Returns NULL is this isn't the
3650 case. */
3652 static tree
3653 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
3655 gfc_se se;
3656 gfc_ss *ss;
3657 gfc_ref * ref;
3658 bool seen_array_ref;
3660 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
3661 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
3662 return NULL;
3664 /* Elemental functions don't need a temporary anyway. */
3665 if (expr2->value.function.esym != NULL
3666 && expr2->value.function.esym->attr.elemental)
3667 return NULL;
3669 /* Fail if EXPR1 can't be expressed as a descriptor. */
3670 if (gfc_ref_needs_temporary_p (expr1->ref))
3671 return NULL;
3673 /* Functions returning pointers need temporaries. */
3674 if (expr2->symtree->n.sym->attr.pointer
3675 || expr2->symtree->n.sym->attr.allocatable)
3676 return NULL;
3678 /* Character array functions need temporaries unless the
3679 character lengths are the same. */
3680 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
3682 if (expr1->ts.cl->length == NULL
3683 || expr1->ts.cl->length->expr_type != EXPR_CONSTANT)
3684 return NULL;
3686 if (expr2->ts.cl->length == NULL
3687 || expr2->ts.cl->length->expr_type != EXPR_CONSTANT)
3688 return NULL;
3690 if (mpz_cmp (expr1->ts.cl->length->value.integer,
3691 expr2->ts.cl->length->value.integer) != 0)
3692 return NULL;
3695 /* Check that no LHS component references appear during an array
3696 reference. This is needed because we do not have the means to
3697 span any arbitrary stride with an array descriptor. This check
3698 is not needed for the rhs because the function result has to be
3699 a complete type. */
3700 seen_array_ref = false;
3701 for (ref = expr1->ref; ref; ref = ref->next)
3703 if (ref->type == REF_ARRAY)
3704 seen_array_ref= true;
3705 else if (ref->type == REF_COMPONENT && seen_array_ref)
3706 return NULL;
3709 /* Check for a dependency. */
3710 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
3711 expr2->value.function.esym,
3712 expr2->value.function.actual))
3713 return NULL;
3715 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
3716 functions. */
3717 gcc_assert (expr2->value.function.isym
3718 || (gfc_return_by_reference (expr2->value.function.esym)
3719 && expr2->value.function.esym->result->attr.dimension));
3721 ss = gfc_walk_expr (expr1);
3722 gcc_assert (ss != gfc_ss_terminator);
3723 gfc_init_se (&se, NULL);
3724 gfc_start_block (&se.pre);
3725 se.want_pointer = 1;
3727 gfc_conv_array_parameter (&se, expr1, ss, 0);
3729 se.direct_byref = 1;
3730 se.ss = gfc_walk_expr (expr2);
3731 gcc_assert (se.ss != gfc_ss_terminator);
3732 gfc_conv_function_expr (&se, expr2);
3733 gfc_add_block_to_block (&se.pre, &se.post);
3735 return gfc_finish_block (&se.pre);
3738 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
3740 static bool
3741 is_zero_initializer_p (gfc_expr * expr)
3743 if (expr->expr_type != EXPR_CONSTANT)
3744 return false;
3746 /* We ignore constants with prescribed memory representations for now. */
3747 if (expr->representation.string)
3748 return false;
3750 switch (expr->ts.type)
3752 case BT_INTEGER:
3753 return mpz_cmp_si (expr->value.integer, 0) == 0;
3755 case BT_REAL:
3756 return mpfr_zero_p (expr->value.real)
3757 && MPFR_SIGN (expr->value.real) >= 0;
3759 case BT_LOGICAL:
3760 return expr->value.logical == 0;
3762 case BT_COMPLEX:
3763 return mpfr_zero_p (expr->value.complex.r)
3764 && MPFR_SIGN (expr->value.complex.r) >= 0
3765 && mpfr_zero_p (expr->value.complex.i)
3766 && MPFR_SIGN (expr->value.complex.i) >= 0;
3768 default:
3769 break;
3771 return false;
3774 /* Try to efficiently translate array(:) = 0. Return NULL if this
3775 can't be done. */
3777 static tree
3778 gfc_trans_zero_assign (gfc_expr * expr)
3780 tree dest, len, type;
3781 tree tmp;
3782 gfc_symbol *sym;
3784 sym = expr->symtree->n.sym;
3785 dest = gfc_get_symbol_decl (sym);
3787 type = TREE_TYPE (dest);
3788 if (POINTER_TYPE_P (type))
3789 type = TREE_TYPE (type);
3790 if (!GFC_ARRAY_TYPE_P (type))
3791 return NULL_TREE;
3793 /* Determine the length of the array. */
3794 len = GFC_TYPE_ARRAY_SIZE (type);
3795 if (!len || TREE_CODE (len) != INTEGER_CST)
3796 return NULL_TREE;
3798 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3799 len = fold_build2 (MULT_EXPR, gfc_array_index_type, len,
3800 fold_convert (gfc_array_index_type, tmp));
3802 /* Convert arguments to the correct types. */
3803 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
3804 dest = gfc_build_addr_expr (pvoid_type_node, dest);
3805 else
3806 dest = fold_convert (pvoid_type_node, dest);
3807 len = fold_convert (size_type_node, len);
3809 /* Construct call to __builtin_memset. */
3810 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMSET],
3811 3, dest, integer_zero_node, len);
3812 return fold_convert (void_type_node, tmp);
3816 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
3817 that constructs the call to __builtin_memcpy. */
3819 static tree
3820 gfc_build_memcpy_call (tree dst, tree src, tree len)
3822 tree tmp;
3824 /* Convert arguments to the correct types. */
3825 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
3826 dst = gfc_build_addr_expr (pvoid_type_node, dst);
3827 else
3828 dst = fold_convert (pvoid_type_node, dst);
3830 if (!POINTER_TYPE_P (TREE_TYPE (src)))
3831 src = gfc_build_addr_expr (pvoid_type_node, src);
3832 else
3833 src = fold_convert (pvoid_type_node, src);
3835 len = fold_convert (size_type_node, len);
3837 /* Construct call to __builtin_memcpy. */
3838 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMCPY], 3, dst, src, len);
3839 return fold_convert (void_type_node, tmp);
3843 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
3844 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
3845 source/rhs, both are gfc_full_array_ref_p which have been checked for
3846 dependencies. */
3848 static tree
3849 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
3851 tree dst, dlen, dtype;
3852 tree src, slen, stype;
3853 tree tmp;
3855 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
3856 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
3858 dtype = TREE_TYPE (dst);
3859 if (POINTER_TYPE_P (dtype))
3860 dtype = TREE_TYPE (dtype);
3861 stype = TREE_TYPE (src);
3862 if (POINTER_TYPE_P (stype))
3863 stype = TREE_TYPE (stype);
3865 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
3866 return NULL_TREE;
3868 /* Determine the lengths of the arrays. */
3869 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
3870 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
3871 return NULL_TREE;
3872 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
3873 dlen = fold_build2 (MULT_EXPR, gfc_array_index_type, dlen,
3874 fold_convert (gfc_array_index_type, tmp));
3876 slen = GFC_TYPE_ARRAY_SIZE (stype);
3877 if (!slen || TREE_CODE (slen) != INTEGER_CST)
3878 return NULL_TREE;
3879 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
3880 slen = fold_build2 (MULT_EXPR, gfc_array_index_type, slen,
3881 fold_convert (gfc_array_index_type, tmp));
3883 /* Sanity check that they are the same. This should always be
3884 the case, as we should already have checked for conformance. */
3885 if (!tree_int_cst_equal (slen, dlen))
3886 return NULL_TREE;
3888 return gfc_build_memcpy_call (dst, src, dlen);
3892 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
3893 this can't be done. EXPR1 is the destination/lhs for which
3894 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
3896 static tree
3897 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
3899 unsigned HOST_WIDE_INT nelem;
3900 tree dst, dtype;
3901 tree src, stype;
3902 tree len;
3903 tree tmp;
3905 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
3906 if (nelem == 0)
3907 return NULL_TREE;
3909 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
3910 dtype = TREE_TYPE (dst);
3911 if (POINTER_TYPE_P (dtype))
3912 dtype = TREE_TYPE (dtype);
3913 if (!GFC_ARRAY_TYPE_P (dtype))
3914 return NULL_TREE;
3916 /* Determine the lengths of the array. */
3917 len = GFC_TYPE_ARRAY_SIZE (dtype);
3918 if (!len || TREE_CODE (len) != INTEGER_CST)
3919 return NULL_TREE;
3921 /* Confirm that the constructor is the same size. */
3922 if (compare_tree_int (len, nelem) != 0)
3923 return NULL_TREE;
3925 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
3926 len = fold_build2 (MULT_EXPR, gfc_array_index_type, len,
3927 fold_convert (gfc_array_index_type, tmp));
3929 stype = gfc_typenode_for_spec (&expr2->ts);
3930 src = gfc_build_constant_array_constructor (expr2, stype);
3932 stype = TREE_TYPE (src);
3933 if (POINTER_TYPE_P (stype))
3934 stype = TREE_TYPE (stype);
3936 return gfc_build_memcpy_call (dst, src, len);
3940 /* Subroutine of gfc_trans_assignment that actually scalarizes the
3941 assignment. EXPR1 is the destination/RHS and EXPR2 is the source/LHS. */
3943 static tree
3944 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag)
3946 gfc_se lse;
3947 gfc_se rse;
3948 gfc_ss *lss;
3949 gfc_ss *lss_section;
3950 gfc_ss *rss;
3951 gfc_loopinfo loop;
3952 tree tmp;
3953 stmtblock_t block;
3954 stmtblock_t body;
3955 bool l_is_temp;
3957 /* Assignment of the form lhs = rhs. */
3958 gfc_start_block (&block);
3960 gfc_init_se (&lse, NULL);
3961 gfc_init_se (&rse, NULL);
3963 /* Walk the lhs. */
3964 lss = gfc_walk_expr (expr1);
3965 rss = NULL;
3966 if (lss != gfc_ss_terminator)
3968 /* The assignment needs scalarization. */
3969 lss_section = lss;
3971 /* Find a non-scalar SS from the lhs. */
3972 while (lss_section != gfc_ss_terminator
3973 && lss_section->type != GFC_SS_SECTION)
3974 lss_section = lss_section->next;
3976 gcc_assert (lss_section != gfc_ss_terminator);
3978 /* Initialize the scalarizer. */
3979 gfc_init_loopinfo (&loop);
3981 /* Walk the rhs. */
3982 rss = gfc_walk_expr (expr2);
3983 if (rss == gfc_ss_terminator)
3985 /* The rhs is scalar. Add a ss for the expression. */
3986 rss = gfc_get_ss ();
3987 rss->next = gfc_ss_terminator;
3988 rss->type = GFC_SS_SCALAR;
3989 rss->expr = expr2;
3991 /* Associate the SS with the loop. */
3992 gfc_add_ss_to_loop (&loop, lss);
3993 gfc_add_ss_to_loop (&loop, rss);
3995 /* Calculate the bounds of the scalarization. */
3996 gfc_conv_ss_startstride (&loop);
3997 /* Resolve any data dependencies in the statement. */
3998 gfc_conv_resolve_dependencies (&loop, lss, rss);
3999 /* Setup the scalarizing loops. */
4000 gfc_conv_loop_setup (&loop);
4002 /* Setup the gfc_se structures. */
4003 gfc_copy_loopinfo_to_se (&lse, &loop);
4004 gfc_copy_loopinfo_to_se (&rse, &loop);
4006 rse.ss = rss;
4007 gfc_mark_ss_chain_used (rss, 1);
4008 if (loop.temp_ss == NULL)
4010 lse.ss = lss;
4011 gfc_mark_ss_chain_used (lss, 1);
4013 else
4015 lse.ss = loop.temp_ss;
4016 gfc_mark_ss_chain_used (lss, 3);
4017 gfc_mark_ss_chain_used (loop.temp_ss, 3);
4020 /* Start the scalarized loop body. */
4021 gfc_start_scalarized_body (&loop, &body);
4023 else
4024 gfc_init_block (&body);
4026 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
4028 /* Translate the expression. */
4029 gfc_conv_expr (&rse, expr2);
4031 if (l_is_temp)
4033 gfc_conv_tmp_array_ref (&lse);
4034 gfc_advance_se_ss_chain (&lse);
4036 else
4037 gfc_conv_expr (&lse, expr1);
4039 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
4040 l_is_temp || init_flag,
4041 expr2->expr_type == EXPR_VARIABLE);
4042 gfc_add_expr_to_block (&body, tmp);
4044 if (lss == gfc_ss_terminator)
4046 /* Use the scalar assignment as is. */
4047 gfc_add_block_to_block (&block, &body);
4049 else
4051 gcc_assert (lse.ss == gfc_ss_terminator
4052 && rse.ss == gfc_ss_terminator);
4054 if (l_is_temp)
4056 gfc_trans_scalarized_loop_boundary (&loop, &body);
4058 /* We need to copy the temporary to the actual lhs. */
4059 gfc_init_se (&lse, NULL);
4060 gfc_init_se (&rse, NULL);
4061 gfc_copy_loopinfo_to_se (&lse, &loop);
4062 gfc_copy_loopinfo_to_se (&rse, &loop);
4064 rse.ss = loop.temp_ss;
4065 lse.ss = lss;
4067 gfc_conv_tmp_array_ref (&rse);
4068 gfc_advance_se_ss_chain (&rse);
4069 gfc_conv_expr (&lse, expr1);
4071 gcc_assert (lse.ss == gfc_ss_terminator
4072 && rse.ss == gfc_ss_terminator);
4074 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
4075 false, false);
4076 gfc_add_expr_to_block (&body, tmp);
4079 /* Generate the copying loops. */
4080 gfc_trans_scalarizing_loops (&loop, &body);
4082 /* Wrap the whole thing up. */
4083 gfc_add_block_to_block (&block, &loop.pre);
4084 gfc_add_block_to_block (&block, &loop.post);
4086 gfc_cleanup_loop (&loop);
4089 return gfc_finish_block (&block);
4093 /* Check whether EXPR is a copyable array. */
4095 static bool
4096 copyable_array_p (gfc_expr * expr)
4098 if (expr->expr_type != EXPR_VARIABLE)
4099 return false;
4101 /* First check it's an array. */
4102 if (expr->rank < 1 || !expr->ref || expr->ref->next)
4103 return false;
4105 if (!gfc_full_array_ref_p (expr->ref))
4106 return false;
4108 /* Next check that it's of a simple enough type. */
4109 switch (expr->ts.type)
4111 case BT_INTEGER:
4112 case BT_REAL:
4113 case BT_COMPLEX:
4114 case BT_LOGICAL:
4115 return true;
4117 case BT_CHARACTER:
4118 return false;
4120 case BT_DERIVED:
4121 return !expr->ts.derived->attr.alloc_comp;
4123 default:
4124 break;
4127 return false;
4130 /* Translate an assignment. */
4132 tree
4133 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag)
4135 tree tmp;
4137 /* Special case a single function returning an array. */
4138 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
4140 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
4141 if (tmp)
4142 return tmp;
4145 /* Special case assigning an array to zero. */
4146 if (copyable_array_p (expr1)
4147 && is_zero_initializer_p (expr2))
4149 tmp = gfc_trans_zero_assign (expr1);
4150 if (tmp)
4151 return tmp;
4154 /* Special case copying one array to another. */
4155 if (copyable_array_p (expr1)
4156 && copyable_array_p (expr2)
4157 && gfc_compare_types (&expr1->ts, &expr2->ts)
4158 && !gfc_check_dependency (expr1, expr2, 0))
4160 tmp = gfc_trans_array_copy (expr1, expr2);
4161 if (tmp)
4162 return tmp;
4165 /* Special case initializing an array from a constant array constructor. */
4166 if (copyable_array_p (expr1)
4167 && expr2->expr_type == EXPR_ARRAY
4168 && gfc_compare_types (&expr1->ts, &expr2->ts))
4170 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
4171 if (tmp)
4172 return tmp;
4175 /* Fallback to the scalarizer to generate explicit loops. */
4176 return gfc_trans_assignment_1 (expr1, expr2, init_flag);
4179 tree
4180 gfc_trans_init_assign (gfc_code * code)
4182 return gfc_trans_assignment (code->expr, code->expr2, true);
4185 tree
4186 gfc_trans_assign (gfc_code * code)
4188 return gfc_trans_assignment (code->expr, code->expr2, false);