]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-expr.c
2005-05-31 Andrew pinski <pinskia@physics.uc.edu>
[thirdparty/gcc.git] / gcc / fortran / trans-expr.c
CommitLineData
4ee9c684 1/* Expression translation
0305ad9b 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
5
c84b470d 6This file is part of GCC.
4ee9c684 7
c84b470d 8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
4ee9c684 12
c84b470d 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
4ee9c684 17
18You should have received a copy of the GNU General Public License
c84b470d 19along with GCC; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
4ee9c684 22
23/* trans-expr.c-- generate GENERIC trees for gfc_expr. */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "tree.h"
29#include "convert.h"
4ee9c684 30#include "ggc.h"
31#include "toplev.h"
32#include "real.h"
88bce636 33#include "tree-gimple.h"
4ee9c684 34#include "flags.h"
4ee9c684 35#include "gfortran.h"
36#include "trans.h"
37#include "trans-const.h"
38#include "trans-types.h"
39#include "trans-array.h"
40/* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
41#include "trans-stmt.h"
42
9a0aec1d 43static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
4ee9c684 44
45/* Copy the scalarization loop variables. */
46
47static void
48gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
49{
50 dest->ss = src->ss;
51 dest->loop = src->loop;
52}
53
54
f888a3fb 55/* Initialize a simple expression holder.
4ee9c684 56
57 Care must be taken when multiple se are created with the same parent.
58 The child se must be kept in sync. The easiest way is to delay creation
59 of a child se until after after the previous se has been translated. */
60
61void
62gfc_init_se (gfc_se * se, gfc_se * parent)
63{
64 memset (se, 0, sizeof (gfc_se));
65 gfc_init_block (&se->pre);
66 gfc_init_block (&se->post);
67
68 se->parent = parent;
69
70 if (parent)
71 gfc_copy_se_loopvars (se, parent);
72}
73
74
75/* Advances to the next SS in the chain. Use this rather than setting
f888a3fb 76 se->ss = se->ss->next because all the parents needs to be kept in sync.
4ee9c684 77 See gfc_init_se. */
78
79void
80gfc_advance_se_ss_chain (gfc_se * se)
81{
82 gfc_se *p;
83
22d678e8 84 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
4ee9c684 85
86 p = se;
87 /* Walk down the parent chain. */
88 while (p != NULL)
89 {
f888a3fb 90 /* Simple consistency check. */
22d678e8 91 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
4ee9c684 92
93 p->ss = p->ss->next;
94
95 p = p->parent;
96 }
97}
98
99
100/* Ensures the result of the expression as either a temporary variable
101 or a constant so that it can be used repeatedly. */
102
103void
104gfc_make_safe_expr (gfc_se * se)
105{
106 tree var;
107
ce45a448 108 if (CONSTANT_CLASS_P (se->expr))
4ee9c684 109 return;
110
f888a3fb 111 /* We need a temporary for this result. */
4ee9c684 112 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
113 gfc_add_modify_expr (&se->pre, var, se->expr);
114 se->expr = var;
115}
116
117
5cb9d0d8 118/* Return an expression which determines if a dummy parameter is present.
119 Also used for arguments to procedures with multiple entry points. */
4ee9c684 120
121tree
122gfc_conv_expr_present (gfc_symbol * sym)
123{
124 tree decl;
125
5cb9d0d8 126 gcc_assert (sym->attr.dummy);
4ee9c684 127
128 decl = gfc_get_symbol_decl (sym);
129 if (TREE_CODE (decl) != PARM_DECL)
130 {
131 /* Array parameters use a temporary descriptor, we want the real
132 parameter. */
22d678e8 133 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
4ee9c684 134 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
135 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
136 }
ed52ef8b 137 return build2 (NE_EXPR, boolean_type_node, decl,
138 fold_convert (TREE_TYPE (decl), null_pointer_node));
4ee9c684 139}
140
141
6bf678b8 142/* Get the character length of an expression, looking through gfc_refs
143 if necessary. */
144
145tree
146gfc_get_expr_charlen (gfc_expr *e)
147{
148 gfc_ref *r;
149 tree length;
150
151 gcc_assert (e->expr_type == EXPR_VARIABLE
152 && e->ts.type == BT_CHARACTER);
153
154 length = NULL; /* To silence compiler warning. */
155
156 /* First candidate: if the variable is of type CHARACTER, the
157 expression's length could be the length of the character
b14e2757 158 variable. */
6bf678b8 159 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
160 length = e->symtree->n.sym->ts.cl->backend_decl;
161
162 /* Look through the reference chain for component references. */
163 for (r = e->ref; r; r = r->next)
164 {
165 switch (r->type)
166 {
167 case REF_COMPONENT:
168 if (r->u.c.component->ts.type == BT_CHARACTER)
169 length = r->u.c.component->ts.cl->backend_decl;
170 break;
171
172 case REF_ARRAY:
173 /* Do nothing. */
174 break;
175
176 default:
177 /* We should never got substring references here. These will be
178 broken down by the scalarizer. */
179 gcc_unreachable ();
180 }
181 }
182
183 gcc_assert (length != NULL);
184 return length;
185}
186
187
188
4ee9c684 189/* Generate code to initialize a string length variable. Returns the
190 value. */
191
192void
193gfc_trans_init_string_length (gfc_charlen * cl, stmtblock_t * pblock)
194{
195 gfc_se se;
196 tree tmp;
197
198 gfc_init_se (&se, NULL);
9ad09405 199 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
4ee9c684 200 gfc_add_block_to_block (pblock, &se.pre);
201
202 tmp = cl->backend_decl;
203 gfc_add_modify_expr (pblock, tmp, se.expr);
204}
205
f888a3fb 206
4ee9c684 207static void
208gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind)
209{
210 tree tmp;
211 tree type;
212 tree var;
213 gfc_se start;
214 gfc_se end;
215
216 type = gfc_get_character_type (kind, ref->u.ss.length);
217 type = build_pointer_type (type);
218
219 var = NULL_TREE;
220 gfc_init_se (&start, se);
9ad09405 221 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
4ee9c684 222 gfc_add_block_to_block (&se->pre, &start.pre);
223
224 if (integer_onep (start.expr))
260abd71 225 gfc_conv_string_parameter (se);
4ee9c684 226 else
227 {
228 /* Change the start of the string. */
229 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
230 tmp = se->expr;
231 else
232 tmp = gfc_build_indirect_ref (se->expr);
233 tmp = gfc_build_array_ref (tmp, start.expr);
234 se->expr = gfc_build_addr_expr (type, tmp);
235 }
236
237 /* Length = end + 1 - start. */
238 gfc_init_se (&end, se);
239 if (ref->u.ss.end == NULL)
240 end.expr = se->string_length;
241 else
242 {
9ad09405 243 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
4ee9c684 244 gfc_add_block_to_block (&se->pre, &end.pre);
245 }
246 tmp =
9ad09405 247 build2 (MINUS_EXPR, gfc_charlen_type_node,
248 fold_convert (gfc_charlen_type_node, integer_one_node),
ed52ef8b 249 start.expr);
9ad09405 250 tmp = build2 (PLUS_EXPR, gfc_charlen_type_node, end.expr, tmp);
4ee9c684 251 se->string_length = fold (tmp);
252}
253
254
255/* Convert a derived type component reference. */
256
257static void
258gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
259{
260 gfc_component *c;
261 tree tmp;
262 tree decl;
263 tree field;
264
265 c = ref->u.c.component;
266
22d678e8 267 gcc_assert (c->backend_decl);
4ee9c684 268
269 field = c->backend_decl;
22d678e8 270 gcc_assert (TREE_CODE (field) == FIELD_DECL);
4ee9c684 271 decl = se->expr;
ed52ef8b 272 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field, NULL_TREE);
4ee9c684 273
274 se->expr = tmp;
275
276 if (c->ts.type == BT_CHARACTER)
277 {
278 tmp = c->ts.cl->backend_decl;
7949cb07 279 /* Components must always be constant length. */
22d678e8 280 gcc_assert (tmp && INTEGER_CST_P (tmp));
4ee9c684 281 se->string_length = tmp;
282 }
283
284 if (c->pointer && c->dimension == 0)
285 se->expr = gfc_build_indirect_ref (se->expr);
286}
287
288
289/* Return the contents of a variable. Also handles reference/pointer
290 variables (all Fortran pointer references are implicit). */
291
292static void
293gfc_conv_variable (gfc_se * se, gfc_expr * expr)
294{
295 gfc_ref *ref;
296 gfc_symbol *sym;
297
298 sym = expr->symtree->n.sym;
299 if (se->ss != NULL)
300 {
301 /* Check that something hasn't gone horribly wrong. */
22d678e8 302 gcc_assert (se->ss != gfc_ss_terminator);
303 gcc_assert (se->ss->expr == expr);
4ee9c684 304
305 /* A scalarized term. We already know the descriptor. */
306 se->expr = se->ss->data.info.descriptor;
7949cb07 307 se->string_length = se->ss->string_length;
4ee9c684 308 ref = se->ss->data.info.ref;
309 }
310 else
311 {
c6871095 312 tree se_expr = NULL_TREE;
313
4ee9c684 314 se->expr = gfc_get_symbol_decl (sym);
315
c6871095 316 /* Special case for assigning the return value of a function.
317 Self recursive functions must have an explicit return value. */
318 if (se->expr == current_function_decl && sym->attr.function
319 && (sym->result == sym))
320 se_expr = gfc_get_fake_result_decl (sym);
321
322 /* Similarly for alternate entry points. */
323 else if (sym->attr.function && sym->attr.entry
324 && (sym->result == sym)
325 && sym->ns->proc_name->backend_decl == current_function_decl)
326 {
327 gfc_entry_list *el = NULL;
328
329 for (el = sym->ns->entries; el; el = el->next)
330 if (sym == el->sym)
331 {
332 se_expr = gfc_get_fake_result_decl (sym);
333 break;
334 }
335 }
336
337 else if (sym->attr.result
338 && sym->ns->proc_name->backend_decl == current_function_decl
339 && sym->ns->proc_name->attr.entry_master
340 && !gfc_return_by_reference (sym->ns->proc_name))
341 se_expr = gfc_get_fake_result_decl (sym);
342
343 if (se_expr)
344 se->expr = se_expr;
345
4ee9c684 346 /* Procedure actual arguments. */
c6871095 347 else if (sym->attr.flavor == FL_PROCEDURE
348 && se->expr != current_function_decl)
4ee9c684 349 {
22d678e8 350 gcc_assert (se->want_pointer);
4ee9c684 351 if (!sym->attr.dummy)
352 {
22d678e8 353 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
4ee9c684 354 se->expr = gfc_build_addr_expr (NULL, se->expr);
355 }
356 return;
544c333b 357 }
358
359
360 /* Dereference the expression, where needed. Since characters
361 are entirely different from other types, they are treated
362 separately. */
363 if (sym->ts.type == BT_CHARACTER)
364 {
365 /* Dereference character pointer dummy arguments
bf7e666b 366 or results. */
544c333b 367 if ((sym->attr.pointer || sym->attr.allocatable)
368 && ((sym->attr.dummy)
369 || (sym->attr.function
370 || sym->attr.result)))
371 se->expr = gfc_build_indirect_ref (se->expr);
372 }
373 else
374 {
747a9f62 375 /* Dereference non-character scalar dummy arguments. */
544c333b 376 if ((sym->attr.dummy) && (!sym->attr.dimension))
377 se->expr = gfc_build_indirect_ref (se->expr);
378
bf7e666b 379 /* Dereference scalar hidden result. */
544c333b 380 if ((gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX)
381 && (sym->attr.function || sym->attr.result)
382 && (!sym->attr.dimension))
383 se->expr = gfc_build_indirect_ref (se->expr);
384
385 /* Dereference non-character pointer variables.
747a9f62 386 These must be dummies, results, or scalars. */
544c333b 387 if ((sym->attr.pointer || sym->attr.allocatable)
388 && ((sym->attr.dummy)
389 || (sym->attr.function || sym->attr.result)
390 || (!sym->attr.dimension)))
391 se->expr = gfc_build_indirect_ref (se->expr);
392 }
393
4ee9c684 394 ref = expr->ref;
395 }
396
397 /* For character variables, also get the length. */
398 if (sym->ts.type == BT_CHARACTER)
399 {
400 se->string_length = sym->ts.cl->backend_decl;
22d678e8 401 gcc_assert (se->string_length);
4ee9c684 402 }
403
404 while (ref)
405 {
406 switch (ref->type)
407 {
408 case REF_ARRAY:
409 /* Return the descriptor if that's what we want and this is an array
410 section reference. */
411 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
412 return;
413/* TODO: Pointers to single elements of array sections, eg elemental subs. */
414 /* Return the descriptor for array pointers and allocations. */
415 if (se->want_pointer
416 && ref->next == NULL && (se->descriptor_only))
417 return;
418
419 gfc_conv_array_ref (se, &ref->u.ar);
420 /* Return a pointer to an element. */
421 break;
422
423 case REF_COMPONENT:
424 gfc_conv_component_ref (se, ref);
425 break;
426
427 case REF_SUBSTRING:
428 gfc_conv_substring (se, ref, expr->ts.kind);
429 break;
430
431 default:
22d678e8 432 gcc_unreachable ();
4ee9c684 433 break;
434 }
435 ref = ref->next;
436 }
437 /* Pointer assignment, allocation or pass by reference. Arrays are handled
f888a3fb 438 separately. */
4ee9c684 439 if (se->want_pointer)
440 {
441 if (expr->ts.type == BT_CHARACTER)
442 gfc_conv_string_parameter (se);
443 else
444 se->expr = gfc_build_addr_expr (NULL, se->expr);
445 }
446 if (se->ss != NULL)
447 gfc_advance_se_ss_chain (se);
448}
449
450
451/* Unary ops are easy... Or they would be if ! was a valid op. */
452
453static void
454gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
455{
456 gfc_se operand;
457 tree type;
458
22d678e8 459 gcc_assert (expr->ts.type != BT_CHARACTER);
4ee9c684 460 /* Initialize the operand. */
461 gfc_init_se (&operand, se);
9b773341 462 gfc_conv_expr_val (&operand, expr->value.op.op1);
4ee9c684 463 gfc_add_block_to_block (&se->pre, &operand.pre);
464
465 type = gfc_typenode_for_spec (&expr->ts);
466
467 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
468 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
f888a3fb 469 All other unary operators have an equivalent GIMPLE unary operator. */
4ee9c684 470 if (code == TRUTH_NOT_EXPR)
ed52ef8b 471 se->expr = build2 (EQ_EXPR, type, operand.expr,
472 convert (type, integer_zero_node));
4ee9c684 473 else
474 se->expr = build1 (code, type, operand.expr);
475
476}
477
76834664 478/* Expand power operator to optimal multiplications when a value is raised
f888a3fb 479 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
76834664 480 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
481 Programming", 3rd Edition, 1998. */
482
483/* This code is mostly duplicated from expand_powi in the backend.
484 We establish the "optimal power tree" lookup table with the defined size.
485 The items in the table are the exponents used to calculate the index
486 exponents. Any integer n less than the value can get an "addition chain",
487 with the first node being one. */
488#define POWI_TABLE_SIZE 256
489
f888a3fb 490/* The table is from builtins.c. */
76834664 491static const unsigned char powi_table[POWI_TABLE_SIZE] =
492 {
493 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
494 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
495 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
496 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
497 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
498 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
499 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
500 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
501 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
502 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
503 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
504 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
505 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
506 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
507 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
508 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
509 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
510 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
511 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
512 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
513 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
514 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
515 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
516 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
517 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
518 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
519 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
520 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
521 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
522 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
523 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
524 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
525 };
526
f888a3fb 527/* If n is larger than lookup table's max index, we use the "window
528 method". */
76834664 529#define POWI_WINDOW_SIZE 3
530
f888a3fb 531/* Recursive function to expand the power operator. The temporary
532 values are put in tmpvar. The function returns tmpvar[1] ** n. */
76834664 533static tree
534gfc_conv_powi (gfc_se * se, int n, tree * tmpvar)
4ee9c684 535{
76834664 536 tree op0;
537 tree op1;
4ee9c684 538 tree tmp;
76834664 539 int digit;
4ee9c684 540
76834664 541 if (n < POWI_TABLE_SIZE)
4ee9c684 542 {
76834664 543 if (tmpvar[n])
544 return tmpvar[n];
4ee9c684 545
76834664 546 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
547 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
548 }
549 else if (n & 1)
550 {
551 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
552 op0 = gfc_conv_powi (se, n - digit, tmpvar);
553 op1 = gfc_conv_powi (se, digit, tmpvar);
4ee9c684 554 }
555 else
556 {
76834664 557 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
558 op1 = op0;
4ee9c684 559 }
560
318c9b27 561 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (op0), op0, op1);
76834664 562 tmp = gfc_evaluate_now (tmp, &se->pre);
4ee9c684 563
76834664 564 if (n < POWI_TABLE_SIZE)
565 tmpvar[n] = tmp;
4ee9c684 566
76834664 567 return tmp;
568}
4ee9c684 569
f888a3fb 570
571/* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
572 return 1. Else return 0 and a call to runtime library functions
573 will have to be built. */
76834664 574static int
575gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
576{
577 tree cond;
578 tree tmp;
579 tree type;
580 tree vartmp[POWI_TABLE_SIZE];
581 int n;
582 int sgn;
4ee9c684 583
76834664 584 type = TREE_TYPE (lhs);
585 n = abs (TREE_INT_CST_LOW (rhs));
586 sgn = tree_int_cst_sgn (rhs);
4ee9c684 587
31ad0f07 588 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations) || optimize_size)
589 && (n > 2 || n < -1))
76834664 590 return 0;
4ee9c684 591
76834664 592 /* rhs == 0 */
593 if (sgn == 0)
594 {
595 se->expr = gfc_build_const (type, integer_one_node);
596 return 1;
597 }
598 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
599 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
600 {
ed52ef8b 601 tmp = build2 (EQ_EXPR, boolean_type_node, lhs,
602 fold_convert (TREE_TYPE (lhs), integer_minus_one_node));
603 cond = build2 (EQ_EXPR, boolean_type_node, lhs,
604 convert (TREE_TYPE (lhs), integer_one_node));
76834664 605
f888a3fb 606 /* If rhs is even,
260abd71 607 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
76834664 608 if ((n & 1) == 0)
609 {
ed52ef8b 610 tmp = build2 (TRUTH_OR_EXPR, boolean_type_node, tmp, cond);
611 se->expr = build3 (COND_EXPR, type, tmp,
612 convert (type, integer_one_node),
613 convert (type, integer_zero_node));
76834664 614 return 1;
615 }
f888a3fb 616 /* If rhs is odd,
76834664 617 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
ed52ef8b 618 tmp = build3 (COND_EXPR, type, tmp,
619 convert (type, integer_minus_one_node),
620 convert (type, integer_zero_node));
621 se->expr = build3 (COND_EXPR, type, cond,
622 convert (type, integer_one_node),
623 tmp);
76834664 624 return 1;
625 }
4ee9c684 626
76834664 627 memset (vartmp, 0, sizeof (vartmp));
628 vartmp[1] = lhs;
76834664 629 if (sgn == -1)
630 {
631 tmp = gfc_build_const (type, integer_one_node);
ed52ef8b 632 vartmp[1] = build2 (RDIV_EXPR, type, tmp, vartmp[1]);
76834664 633 }
f5efe504 634
635 se->expr = gfc_conv_powi (se, n, vartmp);
636
76834664 637 return 1;
4ee9c684 638}
639
640
76834664 641/* Power op (**). Constant integer exponent has special handling. */
4ee9c684 642
643static void
644gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
645{
90ba9145 646 tree gfc_int4_type_node;
4ee9c684 647 int kind;
76834664 648 int ikind;
4ee9c684 649 gfc_se lse;
650 gfc_se rse;
651 tree fndecl;
652 tree tmp;
4ee9c684 653
654 gfc_init_se (&lse, se);
9b773341 655 gfc_conv_expr_val (&lse, expr->value.op.op1);
4ee9c684 656 gfc_add_block_to_block (&se->pre, &lse.pre);
657
658 gfc_init_se (&rse, se);
9b773341 659 gfc_conv_expr_val (&rse, expr->value.op.op2);
4ee9c684 660 gfc_add_block_to_block (&se->pre, &rse.pre);
661
9b773341 662 if (expr->value.op.op2->ts.type == BT_INTEGER
663 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
76834664 664 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
665 return;
4ee9c684 666
90ba9145 667 gfc_int4_type_node = gfc_get_int_type (4);
668
9b773341 669 kind = expr->value.op.op1->ts.kind;
670 switch (expr->value.op.op2->ts.type)
4ee9c684 671 {
672 case BT_INTEGER:
9b773341 673 ikind = expr->value.op.op2->ts.kind;
76834664 674 switch (ikind)
675 {
676 case 1:
677 case 2:
678 rse.expr = convert (gfc_int4_type_node, rse.expr);
679 /* Fall through. */
680
681 case 4:
682 ikind = 0;
683 break;
684
685 case 8:
686 ikind = 1;
687 break;
688
689 default:
22d678e8 690 gcc_unreachable ();
76834664 691 }
692 switch (kind)
693 {
694 case 1:
695 case 2:
9b773341 696 if (expr->value.op.op1->ts.type == BT_INTEGER)
76834664 697 lse.expr = convert (gfc_int4_type_node, lse.expr);
698 else
22d678e8 699 gcc_unreachable ();
76834664 700 /* Fall through. */
701
702 case 4:
703 kind = 0;
704 break;
705
706 case 8:
707 kind = 1;
708 break;
709
710 default:
22d678e8 711 gcc_unreachable ();
76834664 712 }
713
9b773341 714 switch (expr->value.op.op1->ts.type)
76834664 715 {
716 case BT_INTEGER:
717 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
718 break;
719
720 case BT_REAL:
721 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
722 break;
723
724 case BT_COMPLEX:
725 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
726 break;
727
728 default:
22d678e8 729 gcc_unreachable ();
76834664 730 }
731 break;
4ee9c684 732
733 case BT_REAL:
734 switch (kind)
735 {
736 case 4:
76834664 737 fndecl = built_in_decls[BUILT_IN_POWF];
4ee9c684 738 break;
739 case 8:
76834664 740 fndecl = built_in_decls[BUILT_IN_POW];
4ee9c684 741 break;
742 default:
22d678e8 743 gcc_unreachable ();
4ee9c684 744 }
745 break;
746
747 case BT_COMPLEX:
748 switch (kind)
749 {
750 case 4:
751 fndecl = gfor_fndecl_math_cpowf;
752 break;
753 case 8:
754 fndecl = gfor_fndecl_math_cpow;
755 break;
756 default:
22d678e8 757 gcc_unreachable ();
4ee9c684 758 }
759 break;
760
761 default:
22d678e8 762 gcc_unreachable ();
4ee9c684 763 break;
764 }
765
766 tmp = gfc_chainon_list (NULL_TREE, lse.expr);
767 tmp = gfc_chainon_list (tmp, rse.expr);
76834664 768 se->expr = fold (gfc_build_function_call (fndecl, tmp));
4ee9c684 769}
770
771
772/* Generate code to allocate a string temporary. */
773
774tree
775gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
776{
777 tree var;
778 tree tmp;
779 tree args;
780
22d678e8 781 gcc_assert (TREE_TYPE (len) == gfc_charlen_type_node);
260abd71 782
4ee9c684 783 if (gfc_can_put_var_on_stack (len))
784 {
785 /* Create a temporary variable to hold the result. */
318c9b27 786 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
787 convert (gfc_charlen_type_node, integer_one_node));
260abd71 788 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
4ee9c684 789 tmp = build_array_type (gfc_character1_type_node, tmp);
790 var = gfc_create_var (tmp, "str");
791 var = gfc_build_addr_expr (type, var);
792 }
793 else
794 {
795 /* Allocate a temporary to hold the result. */
796 var = gfc_create_var (type, "pstr");
797 args = gfc_chainon_list (NULL_TREE, len);
798 tmp = gfc_build_function_call (gfor_fndecl_internal_malloc, args);
799 tmp = convert (type, tmp);
800 gfc_add_modify_expr (&se->pre, var, tmp);
801
802 /* Free the temporary afterwards. */
803 tmp = convert (pvoid_type_node, var);
804 args = gfc_chainon_list (NULL_TREE, tmp);
805 tmp = gfc_build_function_call (gfor_fndecl_internal_free, args);
806 gfc_add_expr_to_block (&se->post, tmp);
807 }
808
809 return var;
810}
811
812
813/* Handle a string concatenation operation. A temporary will be allocated to
814 hold the result. */
815
816static void
817gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
818{
819 gfc_se lse;
820 gfc_se rse;
821 tree len;
822 tree type;
823 tree var;
824 tree args;
825 tree tmp;
826
9b773341 827 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
828 && expr->value.op.op2->ts.type == BT_CHARACTER);
4ee9c684 829
830 gfc_init_se (&lse, se);
9b773341 831 gfc_conv_expr (&lse, expr->value.op.op1);
4ee9c684 832 gfc_conv_string_parameter (&lse);
833 gfc_init_se (&rse, se);
9b773341 834 gfc_conv_expr (&rse, expr->value.op.op2);
4ee9c684 835 gfc_conv_string_parameter (&rse);
836
837 gfc_add_block_to_block (&se->pre, &lse.pre);
838 gfc_add_block_to_block (&se->pre, &rse.pre);
839
840 type = gfc_get_character_type (expr->ts.kind, expr->ts.cl);
841 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
842 if (len == NULL_TREE)
843 {
318c9b27 844 len = fold_build2 (PLUS_EXPR, TREE_TYPE (lse.string_length),
845 lse.string_length, rse.string_length);
4ee9c684 846 }
847
848 type = build_pointer_type (type);
849
850 var = gfc_conv_string_tmp (se, type, len);
851
852 /* Do the actual concatenation. */
853 args = NULL_TREE;
854 args = gfc_chainon_list (args, len);
855 args = gfc_chainon_list (args, var);
856 args = gfc_chainon_list (args, lse.string_length);
857 args = gfc_chainon_list (args, lse.expr);
858 args = gfc_chainon_list (args, rse.string_length);
859 args = gfc_chainon_list (args, rse.expr);
860 tmp = gfc_build_function_call (gfor_fndecl_concat_string, args);
861 gfc_add_expr_to_block (&se->pre, tmp);
862
863 /* Add the cleanup for the operands. */
864 gfc_add_block_to_block (&se->pre, &rse.post);
865 gfc_add_block_to_block (&se->pre, &lse.post);
866
867 se->expr = var;
868 se->string_length = len;
869}
870
871
872/* Translates an op expression. Common (binary) cases are handled by this
873 function, others are passed on. Recursion is used in either case.
874 We use the fact that (op1.ts == op2.ts) (except for the power
f888a3fb 875 operator **).
4ee9c684 876 Operators need no special handling for scalarized expressions as long as
f888a3fb 877 they call gfc_conv_simple_val to get their operands.
4ee9c684 878 Character strings get special handling. */
879
880static void
881gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
882{
883 enum tree_code code;
884 gfc_se lse;
885 gfc_se rse;
886 tree type;
887 tree tmp;
888 int lop;
889 int checkstring;
890
891 checkstring = 0;
892 lop = 0;
9b773341 893 switch (expr->value.op.operator)
4ee9c684 894 {
895 case INTRINSIC_UPLUS:
9b773341 896 gfc_conv_expr (se, expr->value.op.op1);
4ee9c684 897 return;
898
899 case INTRINSIC_UMINUS:
900 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
901 return;
902
903 case INTRINSIC_NOT:
904 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
905 return;
906
907 case INTRINSIC_PLUS:
908 code = PLUS_EXPR;
909 break;
910
911 case INTRINSIC_MINUS:
912 code = MINUS_EXPR;
913 break;
914
915 case INTRINSIC_TIMES:
916 code = MULT_EXPR;
917 break;
918
919 case INTRINSIC_DIVIDE:
920 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
921 an integer, we must round towards zero, so we use a
922 TRUNC_DIV_EXPR. */
923 if (expr->ts.type == BT_INTEGER)
924 code = TRUNC_DIV_EXPR;
925 else
926 code = RDIV_EXPR;
927 break;
928
929 case INTRINSIC_POWER:
930 gfc_conv_power_op (se, expr);
931 return;
932
933 case INTRINSIC_CONCAT:
934 gfc_conv_concat_op (se, expr);
935 return;
936
937 case INTRINSIC_AND:
938 code = TRUTH_ANDIF_EXPR;
939 lop = 1;
940 break;
941
942 case INTRINSIC_OR:
943 code = TRUTH_ORIF_EXPR;
944 lop = 1;
945 break;
946
947 /* EQV and NEQV only work on logicals, but since we represent them
88bce636 948 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
4ee9c684 949 case INTRINSIC_EQ:
950 case INTRINSIC_EQV:
951 code = EQ_EXPR;
952 checkstring = 1;
953 lop = 1;
954 break;
955
956 case INTRINSIC_NE:
957 case INTRINSIC_NEQV:
958 code = NE_EXPR;
959 checkstring = 1;
960 lop = 1;
961 break;
962
963 case INTRINSIC_GT:
964 code = GT_EXPR;
965 checkstring = 1;
966 lop = 1;
967 break;
968
969 case INTRINSIC_GE:
970 code = GE_EXPR;
971 checkstring = 1;
972 lop = 1;
973 break;
974
975 case INTRINSIC_LT:
976 code = LT_EXPR;
977 checkstring = 1;
978 lop = 1;
979 break;
980
981 case INTRINSIC_LE:
982 code = LE_EXPR;
983 checkstring = 1;
984 lop = 1;
985 break;
986
987 case INTRINSIC_USER:
988 case INTRINSIC_ASSIGN:
989 /* These should be converted into function calls by the frontend. */
22d678e8 990 gcc_unreachable ();
4ee9c684 991
992 default:
993 fatal_error ("Unknown intrinsic op");
994 return;
995 }
996
f888a3fb 997 /* The only exception to this is **, which is handled separately anyway. */
9b773341 998 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
4ee9c684 999
9b773341 1000 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
4ee9c684 1001 checkstring = 0;
1002
1003 /* lhs */
1004 gfc_init_se (&lse, se);
9b773341 1005 gfc_conv_expr (&lse, expr->value.op.op1);
4ee9c684 1006 gfc_add_block_to_block (&se->pre, &lse.pre);
1007
1008 /* rhs */
1009 gfc_init_se (&rse, se);
9b773341 1010 gfc_conv_expr (&rse, expr->value.op.op2);
4ee9c684 1011 gfc_add_block_to_block (&se->pre, &rse.pre);
1012
1013 /* For string comparisons we generate a library call, and compare the return
1014 value with 0. */
1015 if (checkstring)
1016 {
1017 gfc_conv_string_parameter (&lse);
1018 gfc_conv_string_parameter (&rse);
1019 tmp = NULL_TREE;
1020 tmp = gfc_chainon_list (tmp, lse.string_length);
1021 tmp = gfc_chainon_list (tmp, lse.expr);
1022 tmp = gfc_chainon_list (tmp, rse.string_length);
1023 tmp = gfc_chainon_list (tmp, rse.expr);
1024
1025 /* Build a call for the comparison. */
1026 lse.expr = gfc_build_function_call (gfor_fndecl_compare_string, tmp);
1027 gfc_add_block_to_block (&lse.post, &rse.post);
1028
1029 rse.expr = integer_zero_node;
1030 }
1031
1032 type = gfc_typenode_for_spec (&expr->ts);
1033
1034 if (lop)
1035 {
1036 /* The result of logical ops is always boolean_type_node. */
318c9b27 1037 tmp = fold_build2 (code, type, lse.expr, rse.expr);
4ee9c684 1038 se->expr = convert (type, tmp);
1039 }
1040 else
318c9b27 1041 se->expr = fold_build2 (code, type, lse.expr, rse.expr);
4ee9c684 1042
4ee9c684 1043 /* Add the post blocks. */
1044 gfc_add_block_to_block (&se->post, &rse.post);
1045 gfc_add_block_to_block (&se->post, &lse.post);
1046}
1047
f888a3fb 1048
4ee9c684 1049static void
1050gfc_conv_function_val (gfc_se * se, gfc_symbol * sym)
1051{
1052 tree tmp;
1053
1054 if (sym->attr.dummy)
1055 {
1056 tmp = gfc_get_symbol_decl (sym);
22d678e8 1057 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
4ee9c684 1058 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1059
1060 se->expr = tmp;
1061 }
1062 else
1063 {
1064 if (!sym->backend_decl)
1065 sym->backend_decl = gfc_get_extern_function_decl (sym);
1066
1067 tmp = sym->backend_decl;
22d678e8 1068 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
4ee9c684 1069 se->expr = gfc_build_addr_expr (NULL, tmp);
1070 }
1071}
1072
1073
1074/* Generate code for a procedure call. Note can return se->post != NULL.
1075 If se->direct_byref is set then se->expr contains the return parameter. */
1076
1077void
1078gfc_conv_function_call (gfc_se * se, gfc_symbol * sym,
1079 gfc_actual_arglist * arg)
1080{
1081 tree arglist;
1082 tree tmp;
1083 tree fntype;
1084 gfc_se parmse;
1085 gfc_ss *argss;
1086 gfc_ss_info *info;
1087 int byref;
1088 tree type;
1089 tree var;
1090 tree len;
1091 tree stringargs;
1092 gfc_formal_arglist *formal;
1093
1094 arglist = NULL_TREE;
1095 stringargs = NULL_TREE;
1096 var = NULL_TREE;
1097 len = NULL_TREE;
1098
bf7e666b 1099 /* Obtain the string length now because it is needed often below. */
1100 if (sym->ts.type == BT_CHARACTER)
1101 {
1102 gcc_assert (sym->ts.cl && sym->ts.cl->length
1103 && sym->ts.cl->length->expr_type == EXPR_CONSTANT);
1104 len = gfc_conv_mpz_to_tree
1105 (sym->ts.cl->length->value.integer, sym->ts.cl->length->ts.kind);
1106 }
1107
4ee9c684 1108 if (se->ss != NULL)
1109 {
1110 if (!sym->attr.elemental)
1111 {
22d678e8 1112 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
4ee9c684 1113 if (se->ss->useflags)
1114 {
22d678e8 1115 gcc_assert (gfc_return_by_reference (sym)
4ee9c684 1116 && sym->result->attr.dimension);
22d678e8 1117 gcc_assert (se->loop != NULL);
4ee9c684 1118
1119 /* Access the previously obtained result. */
1120 gfc_conv_tmp_array_ref (se);
1121 gfc_advance_se_ss_chain (se);
bf7e666b 1122
1123 /* Bundle in the string length. */
1124 se->string_length=len;
4ee9c684 1125 return;
1126 }
1127 }
1128 info = &se->ss->data.info;
1129 }
1130 else
1131 info = NULL;
1132
1133 byref = gfc_return_by_reference (sym);
1134 if (byref)
1135 {
bf7e666b 1136 if (se->direct_byref)
1137 {
1138 arglist = gfc_chainon_list (arglist, se->expr);
544c333b 1139
1140 /* Add string length to argument list. */
bf7e666b 1141 if (sym->ts.type == BT_CHARACTER)
1142 {
1143 sym->ts.cl->backend_decl = len;
1144 arglist = gfc_chainon_list (arglist,
1145 convert (gfc_charlen_type_node, len));
1146 }
1147 }
4ee9c684 1148 else if (sym->result->attr.dimension)
1149 {
544c333b 1150 gcc_assert (se->loop && se->ss);
bf7e666b 1151
4ee9c684 1152 /* Set the type of the array. */
1153 tmp = gfc_typenode_for_spec (&sym->ts);
544c333b 1154 info->dimen = se->loop->dimen;
bf7e666b 1155
4ee9c684 1156 /* Allocate a temporary to store the result. */
7949cb07 1157 gfc_trans_allocate_temp_array (se->loop, info, tmp);
4ee9c684 1158
1159 /* Zero the first stride to indicate a temporary. */
1160 tmp =
1161 gfc_conv_descriptor_stride (info->descriptor, gfc_rank_cst[0]);
260abd71 1162 gfc_add_modify_expr (&se->pre, tmp,
1163 convert (TREE_TYPE (tmp), integer_zero_node));
544c333b 1164
4ee9c684 1165 /* Pass the temporary as the first argument. */
1166 tmp = info->descriptor;
1167 tmp = gfc_build_addr_expr (NULL, tmp);
1168 arglist = gfc_chainon_list (arglist, tmp);
bf7e666b 1169
1170 /* Add string length to argument list. */
1171 if (sym->ts.type == BT_CHARACTER)
1172 {
1173 sym->ts.cl->backend_decl = len;
1174 arglist = gfc_chainon_list (arglist,
1175 convert (gfc_charlen_type_node, len));
1176 }
1177
4ee9c684 1178 }
1179 else if (sym->ts.type == BT_CHARACTER)
1180 {
544c333b 1181
1182 /* Pass the string length. */
4ee9c684 1183 sym->ts.cl->backend_decl = len;
1184 type = gfc_get_character_type (sym->ts.kind, sym->ts.cl);
1185 type = build_pointer_type (type);
1186
bf7e666b 1187 /* Return an address to a char[4]* temporary for character pointers. */
1188 if (sym->attr.pointer || sym->attr.allocatable)
1189 {
1190 /* Build char[4] * pstr. */
544c333b 1191 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
1192 convert (gfc_charlen_type_node, integer_one_node));
1193 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1194 tmp = build_array_type (gfc_character1_type_node, tmp);
bf7e666b 1195 var = gfc_create_var (build_pointer_type (tmp), "pstr");
1196
544c333b 1197 /* Provide an address expression for the function arguments. */
1198 var = gfc_build_addr_expr (NULL, var);
bf7e666b 1199 }
1200 else
1201 {
544c333b 1202 var = gfc_conv_string_tmp (se, type, len);
bf7e666b 1203 }
4ee9c684 1204 arglist = gfc_chainon_list (arglist, var);
9ad09405 1205 arglist = gfc_chainon_list (arglist,
1206 convert (gfc_charlen_type_node, len));
4ee9c684 1207 }
e308bc81 1208 else
bdaed7d2 1209 {
1210 gcc_assert (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX);
1211
1212 type = gfc_get_complex_type (sym->ts.kind);
1213 var = gfc_build_addr_expr (NULL, gfc_create_var (type, "cmplx"));
1214 arglist = gfc_chainon_list (arglist, var);
1215 }
4ee9c684 1216 }
1217
1218 formal = sym->formal;
1219 /* Evaluate the arguments. */
1220 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
1221 {
1222 if (arg->expr == NULL)
1223 {
1224
1225 if (se->ignore_optional)
1226 {
1227 /* Some intrinsics have already been resolved to the correct
1228 parameters. */
1229 continue;
1230 }
1231 else if (arg->label)
1232 {
1233 has_alternate_specifier = 1;
1234 continue;
1235 }
1236 else
1237 {
1238 /* Pass a NULL pointer for an absent arg. */
1239 gfc_init_se (&parmse, NULL);
1240 parmse.expr = null_pointer_node;
0fe9e56f 1241 if (arg->missing_arg_type == BT_CHARACTER)
4ee9c684 1242 {
260abd71 1243 stringargs =
1244 gfc_chainon_list (stringargs,
9ad09405 1245 convert (gfc_charlen_type_node,
260abd71 1246 integer_zero_node));
4ee9c684 1247 }
1248 }
1249 }
1250 else if (se->ss && se->ss->useflags)
1251 {
1252 /* An elemental function inside a scalarized loop. */
1253 gfc_init_se (&parmse, se);
1254 gfc_conv_expr_reference (&parmse, arg->expr);
1255 }
1256 else
1257 {
1258 /* A scalar or transformational function. */
1259 gfc_init_se (&parmse, NULL);
1260 argss = gfc_walk_expr (arg->expr);
1261
1262 if (argss == gfc_ss_terminator)
1263 {
1264 gfc_conv_expr_reference (&parmse, arg->expr);
d6606878 1265 if (formal && formal->sym->attr.pointer
1266 && arg->expr->expr_type != EXPR_NULL)
4ee9c684 1267 {
1268 /* Scalar pointer dummy args require an extra level of
bf7e666b 1269 indirection. The null pointer already contains
1270 this level of indirection. */
4ee9c684 1271 parmse.expr = gfc_build_addr_expr (NULL, parmse.expr);
1272 }
1273 }
1274 else
1275 {
f888a3fb 1276 /* If the procedure requires an explicit interface, the
1277 actual argument is passed according to the
1278 corresponding formal argument. If the corresponding
1279 formal argument is a POINTER or assumed shape, we do
231e961a 1280 not use g77's calling convention, and pass the
f888a3fb 1281 address of the array descriptor instead. Otherwise we
1282 use g77's calling convention. */
4ee9c684 1283 int f;
1284 f = (formal != NULL)
1285 && !formal->sym->attr.pointer
1286 && formal->sym->as->type != AS_ASSUMED_SHAPE;
1287 f = f || !sym->attr.always_explicit;
1288 gfc_conv_array_parameter (&parmse, arg->expr, argss, f);
1289 }
1290 }
1291
1292 gfc_add_block_to_block (&se->pre, &parmse.pre);
1293 gfc_add_block_to_block (&se->post, &parmse.post);
1294
7b3423b9 1295 /* Character strings are passed as two parameters, a length and a
4ee9c684 1296 pointer. */
1297 if (parmse.string_length != NULL_TREE)
1298 stringargs = gfc_chainon_list (stringargs, parmse.string_length);
1299
1300 arglist = gfc_chainon_list (arglist, parmse.expr);
1301 }
1302
1303 /* Add the hidden string length parameters to the arguments. */
1304 arglist = chainon (arglist, stringargs);
1305
1306 /* Generate the actual call. */
1307 gfc_conv_function_val (se, sym);
1308 /* If there are alternate return labels, function type should be
1309 integer. */
1310 if (has_alternate_specifier)
1311 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
1312
1313 fntype = TREE_TYPE (TREE_TYPE (se->expr));
ed52ef8b 1314 se->expr = build3 (CALL_EXPR, TREE_TYPE (fntype), se->expr,
1315 arglist, NULL_TREE);
4ee9c684 1316
bdaed7d2 1317 if (sym->result)
1318 sym = sym->result;
1319
fa069004 1320 /* If we have a pointer function, but we don't want a pointer, e.g.
1321 something like
1322 x = f()
1323 where f is pointer valued, we have to dereference the result. */
bdaed7d2 1324 if (!se->want_pointer && !byref && sym->attr.pointer)
fa069004 1325 se->expr = gfc_build_indirect_ref (se->expr);
1326
bdaed7d2 1327 /* f2c calling conventions require a scalar default real function to
1328 return a double precision result. Convert this back to default
1329 real. We only care about the cases that can happen in Fortran 77.
1330 */
1331 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
1332 && sym->ts.kind == gfc_default_real_kind
1333 && !sym->attr.always_explicit)
1334 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
1335
f888a3fb 1336 /* A pure function may still have side-effects - it may modify its
1337 parameters. */
4ee9c684 1338 TREE_SIDE_EFFECTS (se->expr) = 1;
1339#if 0
1340 if (!sym->attr.pure)
1341 TREE_SIDE_EFFECTS (se->expr) = 1;
1342#endif
1343
4396343e 1344 if (byref)
4ee9c684 1345 {
4396343e 1346 /* Add the function call to the pre chain. There is no expression. */
4ee9c684 1347 gfc_add_expr_to_block (&se->pre, se->expr);
4396343e 1348 se->expr = NULL_TREE;
4ee9c684 1349
4396343e 1350 if (!se->direct_byref)
4ee9c684 1351 {
65cf6ae7 1352 if (sym->attr.dimension)
4ee9c684 1353 {
4396343e 1354 if (flag_bounds_check)
1355 {
1356 /* Check the data pointer hasn't been modified. This would
1357 happen in a function returning a pointer. */
1358 tmp = gfc_conv_descriptor_data (info->descriptor);
ed52ef8b 1359 tmp = build2 (NE_EXPR, boolean_type_node, tmp, info->data);
4396343e 1360 gfc_trans_runtime_check (tmp, gfc_strconst_fault, &se->pre);
1361 }
1362 se->expr = info->descriptor;
bf7e666b 1363 /* Bundle in the string length. */
1364 se->string_length = len;
4ee9c684 1365 }
4396343e 1366 else if (sym->ts.type == BT_CHARACTER)
544c333b 1367 {
bf7e666b 1368 /* Dereference for character pointer results. */
1369 if (sym->attr.pointer || sym->attr.allocatable)
544c333b 1370 se->expr = gfc_build_indirect_ref (var);
1371 else
bf7e666b 1372 se->expr = var;
1373
4396343e 1374 se->string_length = len;
1375 }
1376 else
bdaed7d2 1377 {
1378 gcc_assert (sym->ts.type == BT_COMPLEX && gfc_option.flag_f2c);
1379 se->expr = gfc_build_indirect_ref (var);
1380 }
4ee9c684 1381 }
4ee9c684 1382 }
1383}
1384
1385
dbe60343 1386/* Generate code to copy a string. */
1387
1388static void
1389gfc_trans_string_copy (stmtblock_t * block, tree dlen, tree dest,
1390 tree slen, tree src)
1391{
1392 tree tmp;
1393
1394 tmp = NULL_TREE;
1395 tmp = gfc_chainon_list (tmp, dlen);
1396 tmp = gfc_chainon_list (tmp, dest);
1397 tmp = gfc_chainon_list (tmp, slen);
1398 tmp = gfc_chainon_list (tmp, src);
1399 tmp = gfc_build_function_call (gfor_fndecl_copy_string, tmp);
1400 gfc_add_expr_to_block (block, tmp);
1401}
1402
1403
4ee9c684 1404/* Translate a statement function.
1405 The value of a statement function reference is obtained by evaluating the
1406 expression using the values of the actual arguments for the values of the
1407 corresponding dummy arguments. */
1408
1409static void
1410gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
1411{
1412 gfc_symbol *sym;
1413 gfc_symbol *fsym;
1414 gfc_formal_arglist *fargs;
1415 gfc_actual_arglist *args;
1416 gfc_se lse;
1417 gfc_se rse;
dbe60343 1418 gfc_saved_var *saved_vars;
1419 tree *temp_vars;
1420 tree type;
1421 tree tmp;
1422 int n;
4ee9c684 1423
1424 sym = expr->symtree->n.sym;
1425 args = expr->value.function.actual;
1426 gfc_init_se (&lse, NULL);
1427 gfc_init_se (&rse, NULL);
1428
dbe60343 1429 n = 0;
4ee9c684 1430 for (fargs = sym->formal; fargs; fargs = fargs->next)
dbe60343 1431 n++;
1432 saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
1433 temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
1434
1435 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4ee9c684 1436 {
1437 /* Each dummy shall be specified, explicitly or implicitly, to be
1438 scalar. */
22d678e8 1439 gcc_assert (fargs->sym->attr.dimension == 0);
4ee9c684 1440 fsym = fargs->sym;
4ee9c684 1441
dbe60343 1442 /* Create a temporary to hold the value. */
1443 type = gfc_typenode_for_spec (&fsym->ts);
1444 temp_vars[n] = gfc_create_var (type, fsym->name);
1445
1446 if (fsym->ts.type == BT_CHARACTER)
4ee9c684 1447 {
dbe60343 1448 /* Copy string arguments. */
1449 tree arglen;
4ee9c684 1450
22d678e8 1451 gcc_assert (fsym->ts.cl && fsym->ts.cl->length
4ee9c684 1452 && fsym->ts.cl->length->expr_type == EXPR_CONSTANT);
1453
dbe60343 1454 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1455 tmp = gfc_build_addr_expr (build_pointer_type (type),
1456 temp_vars[n]);
4ee9c684 1457
1458 gfc_conv_expr (&rse, args->expr);
1459 gfc_conv_string_parameter (&rse);
4ee9c684 1460 gfc_add_block_to_block (&se->pre, &lse.pre);
1461 gfc_add_block_to_block (&se->pre, &rse.pre);
1462
dbe60343 1463 gfc_trans_string_copy (&se->pre, arglen, tmp, rse.string_length,
1464 rse.expr);
4ee9c684 1465 gfc_add_block_to_block (&se->pre, &lse.post);
1466 gfc_add_block_to_block (&se->pre, &rse.post);
1467 }
1468 else
1469 {
1470 /* For everything else, just evaluate the expression. */
4ee9c684 1471 gfc_conv_expr (&lse, args->expr);
1472
1473 gfc_add_block_to_block (&se->pre, &lse.pre);
dbe60343 1474 gfc_add_modify_expr (&se->pre, temp_vars[n], lse.expr);
4ee9c684 1475 gfc_add_block_to_block (&se->pre, &lse.post);
1476 }
dbe60343 1477
4ee9c684 1478 args = args->next;
1479 }
dbe60343 1480
1481 /* Use the temporary variables in place of the real ones. */
1482 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
1483 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
1484
4ee9c684 1485 gfc_conv_expr (se, sym->value);
dbe60343 1486
1487 if (sym->ts.type == BT_CHARACTER)
1488 {
1489 gfc_conv_const_charlen (sym->ts.cl);
1490
1491 /* Force the expression to the correct length. */
1492 if (!INTEGER_CST_P (se->string_length)
1493 || tree_int_cst_lt (se->string_length,
1494 sym->ts.cl->backend_decl))
1495 {
1496 type = gfc_get_character_type (sym->ts.kind, sym->ts.cl);
1497 tmp = gfc_create_var (type, sym->name);
1498 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
1499 gfc_trans_string_copy (&se->pre, sym->ts.cl->backend_decl, tmp,
1500 se->string_length, se->expr);
1501 se->expr = tmp;
1502 }
1503 se->string_length = sym->ts.cl->backend_decl;
1504 }
1505
f888a3fb 1506 /* Restore the original variables. */
dbe60343 1507 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
1508 gfc_restore_sym (fargs->sym, &saved_vars[n]);
1509 gfc_free (saved_vars);
4ee9c684 1510}
1511
1512
1513/* Translate a function expression. */
1514
1515static void
1516gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
1517{
1518 gfc_symbol *sym;
1519
1520 if (expr->value.function.isym)
1521 {
1522 gfc_conv_intrinsic_function (se, expr);
1523 return;
1524 }
1525
f888a3fb 1526 /* We distinguish statement functions from general functions to improve
4ee9c684 1527 runtime performance. */
1528 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1529 {
1530 gfc_conv_statement_function (se, expr);
1531 return;
1532 }
1533
1534 /* expr.value.function.esym is the resolved (specific) function symbol for
1535 most functions. However this isn't set for dummy procedures. */
1536 sym = expr->value.function.esym;
1537 if (!sym)
1538 sym = expr->symtree->n.sym;
1539 gfc_conv_function_call (se, sym, expr->value.function.actual);
1540}
1541
f888a3fb 1542
4ee9c684 1543static void
1544gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
1545{
22d678e8 1546 gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
1547 gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
4ee9c684 1548
1549 gfc_conv_tmp_array_ref (se);
1550 gfc_advance_se_ss_chain (se);
1551}
1552
1553
bda1f152 1554/* Build a static initializer. EXPR is the expression for the initial value.
f888a3fb 1555 The other parameters describe the variable of the component being
1556 initialized. EXPR may be null. */
4ee9c684 1557
bda1f152 1558tree
1559gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
1560 bool array, bool pointer)
1561{
1562 gfc_se se;
1563
1564 if (!(expr || pointer))
1565 return NULL_TREE;
1566
1567 if (array)
1568 {
1569 /* Arrays need special handling. */
1570 if (pointer)
1571 return gfc_build_null_descriptor (type);
1572 else
1573 return gfc_conv_array_initializer (type, expr);
1574 }
1575 else if (pointer)
1576 return fold_convert (type, null_pointer_node);
1577 else
1578 {
1579 switch (ts->type)
1580 {
1581 case BT_DERIVED:
1582 gfc_init_se (&se, NULL);
1583 gfc_conv_structure (&se, expr, 1);
1584 return se.expr;
1585
1586 case BT_CHARACTER:
1587 return gfc_conv_string_init (ts->cl->backend_decl,expr);
1588
1589 default:
1590 gfc_init_se (&se, NULL);
1591 gfc_conv_constant (&se, expr);
1592 return se.expr;
1593 }
1594 }
1595}
1596
9a0aec1d 1597static tree
1598gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
1599{
1600 gfc_se rse;
1601 gfc_se lse;
1602 gfc_ss *rss;
1603 gfc_ss *lss;
1604 stmtblock_t body;
1605 stmtblock_t block;
1606 gfc_loopinfo loop;
1607 int n;
1608 tree tmp;
1609
1610 gfc_start_block (&block);
1611
1612 /* Initialize the scalarizer. */
1613 gfc_init_loopinfo (&loop);
1614
1615 gfc_init_se (&lse, NULL);
1616 gfc_init_se (&rse, NULL);
1617
1618 /* Walk the rhs. */
1619 rss = gfc_walk_expr (expr);
1620 if (rss == gfc_ss_terminator)
1621 {
1622 /* The rhs is scalar. Add a ss for the expression. */
1623 rss = gfc_get_ss ();
1624 rss->next = gfc_ss_terminator;
1625 rss->type = GFC_SS_SCALAR;
1626 rss->expr = expr;
1627 }
1628
1629 /* Create a SS for the destination. */
1630 lss = gfc_get_ss ();
1631 lss->type = GFC_SS_COMPONENT;
1632 lss->expr = NULL;
1633 lss->shape = gfc_get_shape (cm->as->rank);
1634 lss->next = gfc_ss_terminator;
1635 lss->data.info.dimen = cm->as->rank;
1636 lss->data.info.descriptor = dest;
1637 lss->data.info.data = gfc_conv_array_data (dest);
1638 lss->data.info.offset = gfc_conv_array_offset (dest);
1639 for (n = 0; n < cm->as->rank; n++)
1640 {
1641 lss->data.info.dim[n] = n;
1642 lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
1643 lss->data.info.stride[n] = gfc_index_one_node;
1644
1645 mpz_init (lss->shape[n]);
1646 mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
1647 cm->as->lower[n]->value.integer);
1648 mpz_add_ui (lss->shape[n], lss->shape[n], 1);
1649 }
1650
1651 /* Associate the SS with the loop. */
1652 gfc_add_ss_to_loop (&loop, lss);
1653 gfc_add_ss_to_loop (&loop, rss);
1654
1655 /* Calculate the bounds of the scalarization. */
1656 gfc_conv_ss_startstride (&loop);
1657
1658 /* Setup the scalarizing loops. */
1659 gfc_conv_loop_setup (&loop);
1660
1661 /* Setup the gfc_se structures. */
1662 gfc_copy_loopinfo_to_se (&lse, &loop);
1663 gfc_copy_loopinfo_to_se (&rse, &loop);
1664
1665 rse.ss = rss;
1666 gfc_mark_ss_chain_used (rss, 1);
1667 lse.ss = lss;
1668 gfc_mark_ss_chain_used (lss, 1);
1669
1670 /* Start the scalarized loop body. */
1671 gfc_start_scalarized_body (&loop, &body);
1672
1673 gfc_conv_tmp_array_ref (&lse);
1674 gfc_conv_expr (&rse, expr);
1675
1676 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts.type);
1677 gfc_add_expr_to_block (&body, tmp);
1678
22d678e8 1679 gcc_assert (rse.ss == gfc_ss_terminator);
9a0aec1d 1680
1681 /* Generate the copying loops. */
1682 gfc_trans_scalarizing_loops (&loop, &body);
1683
1684 /* Wrap the whole thing up. */
1685 gfc_add_block_to_block (&block, &loop.pre);
1686 gfc_add_block_to_block (&block, &loop.post);
1687
9a0aec1d 1688 for (n = 0; n < cm->as->rank; n++)
1689 mpz_clear (lss->shape[n]);
1690 gfc_free (lss->shape);
1691
6cf06ccd 1692 gfc_cleanup_loop (&loop);
1693
9a0aec1d 1694 return gfc_finish_block (&block);
1695}
1696
1697/* Assign a single component of a derived type constructor. */
1698
1699static tree
1700gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
1701{
1702 gfc_se se;
1703 gfc_ss *rss;
1704 stmtblock_t block;
1705 tree tmp;
1706
1707 gfc_start_block (&block);
1708 if (cm->pointer)
1709 {
1710 gfc_init_se (&se, NULL);
1711 /* Pointer component. */
1712 if (cm->dimension)
1713 {
1714 /* Array pointer. */
1715 if (expr->expr_type == EXPR_NULL)
1716 {
1717 dest = gfc_conv_descriptor_data (dest);
1718 tmp = fold_convert (TREE_TYPE (se.expr),
1719 null_pointer_node);
1720 gfc_add_modify_expr (&block, dest, tmp);
1721 }
1722 else
1723 {
1724 rss = gfc_walk_expr (expr);
1725 se.direct_byref = 1;
1726 se.expr = dest;
1727 gfc_conv_expr_descriptor (&se, expr, rss);
1728 gfc_add_block_to_block (&block, &se.pre);
1729 gfc_add_block_to_block (&block, &se.post);
1730 }
1731 }
1732 else
1733 {
1734 /* Scalar pointers. */
1735 se.want_pointer = 1;
1736 gfc_conv_expr (&se, expr);
1737 gfc_add_block_to_block (&block, &se.pre);
1738 gfc_add_modify_expr (&block, dest,
1739 fold_convert (TREE_TYPE (dest), se.expr));
1740 gfc_add_block_to_block (&block, &se.post);
1741 }
1742 }
1743 else if (cm->dimension)
1744 {
1745 tmp = gfc_trans_subarray_assign (dest, cm, expr);
1746 gfc_add_expr_to_block (&block, tmp);
1747 }
1748 else if (expr->ts.type == BT_DERIVED)
1749 {
39fca56b 1750 /* Nested derived type. */
9a0aec1d 1751 tmp = gfc_trans_structure_assign (dest, expr);
1752 gfc_add_expr_to_block (&block, tmp);
1753 }
1754 else
1755 {
1756 /* Scalar component. */
1757 gfc_se lse;
1758
1759 gfc_init_se (&se, NULL);
1760 gfc_init_se (&lse, NULL);
1761
1762 gfc_conv_expr (&se, expr);
1763 if (cm->ts.type == BT_CHARACTER)
1764 lse.string_length = cm->ts.cl->backend_decl;
1765 lse.expr = dest;
1766 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts.type);
1767 gfc_add_expr_to_block (&block, tmp);
1768 }
1769 return gfc_finish_block (&block);
1770}
1771
39fca56b 1772/* Assign a derived type constructor to a variable. */
9a0aec1d 1773
1774static tree
1775gfc_trans_structure_assign (tree dest, gfc_expr * expr)
1776{
1777 gfc_constructor *c;
1778 gfc_component *cm;
1779 stmtblock_t block;
1780 tree field;
1781 tree tmp;
1782
1783 gfc_start_block (&block);
1784 cm = expr->ts.derived->components;
1785 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
1786 {
1787 /* Skip absent members in default initializers. */
1788 if (!c->expr)
1789 continue;
1790
1791 field = cm->backend_decl;
ed52ef8b 1792 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), dest, field, NULL_TREE);
9a0aec1d 1793 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
1794 gfc_add_expr_to_block (&block, tmp);
1795 }
1796 return gfc_finish_block (&block);
1797}
1798
4ee9c684 1799/* Build an expression for a constructor. If init is nonzero then
1800 this is part of a static variable initializer. */
1801
1802void
1803gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
1804{
1805 gfc_constructor *c;
1806 gfc_component *cm;
1807 tree head;
1808 tree tail;
1809 tree val;
4ee9c684 1810 tree type;
9a0aec1d 1811 tree tmp;
4ee9c684 1812
22d678e8 1813 gcc_assert (se->ss == NULL);
1814 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
4ee9c684 1815 type = gfc_typenode_for_spec (&expr->ts);
9a0aec1d 1816
1817 if (!init)
1818 {
1819 /* Create a temporary variable and fill it in. */
1820 se->expr = gfc_create_var (type, expr->ts.derived->name);
1821 tmp = gfc_trans_structure_assign (se->expr, expr);
1822 gfc_add_expr_to_block (&se->pre, tmp);
1823 return;
1824 }
1825
4ee9c684 1826 head = build1 (CONSTRUCTOR, type, NULL_TREE);
1827 tail = NULL_TREE;
1828
1829 cm = expr->ts.derived->components;
1830 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
1831 {
1832 /* Skip absent members in default initializers. */
1833 if (!c->expr)
1834 continue;
1835
9a0aec1d 1836 val = gfc_conv_initializer (c->expr, &cm->ts,
1837 TREE_TYPE (cm->backend_decl), cm->dimension, cm->pointer);
4ee9c684 1838
1839 /* Build a TREE_CHAIN to hold it. */
9a0aec1d 1840 val = tree_cons (cm->backend_decl, val, NULL_TREE);
4ee9c684 1841
1842 /* Add it to the list. */
1843 if (tail == NULL_TREE)
1844 TREE_OPERAND(head, 0) = tail = val;
1845 else
1846 {
1847 TREE_CHAIN (tail) = val;
1848 tail = val;
1849 }
1850 }
1851 se->expr = head;
1852}
1853
1854
f888a3fb 1855/* Translate a substring expression. */
4ee9c684 1856
1857static void
1858gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
1859{
1860 gfc_ref *ref;
1861
1862 ref = expr->ref;
1863
22d678e8 1864 gcc_assert (ref->type == REF_SUBSTRING);
4ee9c684 1865
1866 se->expr = gfc_build_string_const(expr->value.character.length,
1867 expr->value.character.string);
1868 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
1869 TYPE_STRING_FLAG (TREE_TYPE (se->expr))=1;
1870
1871 gfc_conv_substring(se,ref,expr->ts.kind);
1872}
1873
1874
1875/* Entry point for expression translation. */
1876
1877void
1878gfc_conv_expr (gfc_se * se, gfc_expr * expr)
1879{
1880 if (se->ss && se->ss->expr == expr
1881 && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
1882 {
9a0aec1d 1883 /* Substitute a scalar expression evaluated outside the scalarization
4ee9c684 1884 loop. */
1885 se->expr = se->ss->data.scalar.expr;
7949cb07 1886 se->string_length = se->ss->string_length;
4ee9c684 1887 gfc_advance_se_ss_chain (se);
1888 return;
1889 }
1890
1891 switch (expr->expr_type)
1892 {
1893 case EXPR_OP:
1894 gfc_conv_expr_op (se, expr);
1895 break;
1896
1897 case EXPR_FUNCTION:
1898 gfc_conv_function_expr (se, expr);
1899 break;
1900
1901 case EXPR_CONSTANT:
1902 gfc_conv_constant (se, expr);
1903 break;
1904
1905 case EXPR_VARIABLE:
1906 gfc_conv_variable (se, expr);
1907 break;
1908
1909 case EXPR_NULL:
1910 se->expr = null_pointer_node;
1911 break;
1912
1913 case EXPR_SUBSTRING:
1914 gfc_conv_substring_expr (se, expr);
1915 break;
1916
1917 case EXPR_STRUCTURE:
1918 gfc_conv_structure (se, expr, 0);
1919 break;
1920
1921 case EXPR_ARRAY:
1922 gfc_conv_array_constructor_expr (se, expr);
1923 break;
1924
1925 default:
22d678e8 1926 gcc_unreachable ();
4ee9c684 1927 break;
1928 }
1929}
1930
1931void
1932gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
1933{
1934 gfc_conv_expr (se, expr);
1935 /* AFAICS all numeric lvalues have empty post chains. If not we need to
1936 figure out a way of rewriting an lvalue so that it has no post chain. */
22d678e8 1937 gcc_assert (expr->ts.type != BT_CHARACTER || !se->post.head);
4ee9c684 1938}
1939
1940void
1941gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
1942{
1943 tree val;
1944
22d678e8 1945 gcc_assert (expr->ts.type != BT_CHARACTER);
4ee9c684 1946 gfc_conv_expr (se, expr);
1947 if (se->post.head)
1948 {
1949 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
1950 gfc_add_modify_expr (&se->pre, val, se->expr);
1951 }
1952}
1953
1954void
1955gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
1956{
1957 gfc_conv_expr_val (se, expr);
1958 se->expr = convert (type, se->expr);
1959}
1960
1961
f888a3fb 1962/* Converts an expression so that it can be passed by reference. Scalar
4ee9c684 1963 values only. */
1964
1965void
1966gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
1967{
1968 tree var;
1969
1970 if (se->ss && se->ss->expr == expr
1971 && se->ss->type == GFC_SS_REFERENCE)
1972 {
1973 se->expr = se->ss->data.scalar.expr;
7949cb07 1974 se->string_length = se->ss->string_length;
4ee9c684 1975 gfc_advance_se_ss_chain (se);
1976 return;
1977 }
1978
1979 if (expr->ts.type == BT_CHARACTER)
1980 {
1981 gfc_conv_expr (se, expr);
1982 gfc_conv_string_parameter (se);
1983 return;
1984 }
1985
1986 if (expr->expr_type == EXPR_VARIABLE)
1987 {
1988 se->want_pointer = 1;
1989 gfc_conv_expr (se, expr);
1990 if (se->post.head)
1991 {
1992 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
1993 gfc_add_modify_expr (&se->pre, var, se->expr);
1994 gfc_add_block_to_block (&se->pre, &se->post);
1995 se->expr = var;
1996 }
1997 return;
1998 }
1999
2000 gfc_conv_expr (se, expr);
2001
2002 /* Create a temporary var to hold the value. */
e67e5e1f 2003 if (TREE_CONSTANT (se->expr))
2004 {
2005 var = build_decl (CONST_DECL, NULL, TREE_TYPE (se->expr));
2006 DECL_INITIAL (var) = se->expr;
2007 pushdecl (var);
2008 }
2009 else
2010 {
2011 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
2012 gfc_add_modify_expr (&se->pre, var, se->expr);
2013 }
4ee9c684 2014 gfc_add_block_to_block (&se->pre, &se->post);
2015
2016 /* Take the address of that value. */
2017 se->expr = gfc_build_addr_expr (NULL, var);
2018}
2019
2020
2021tree
2022gfc_trans_pointer_assign (gfc_code * code)
2023{
2024 return gfc_trans_pointer_assignment (code->expr, code->expr2);
2025}
2026
2027
4396343e 2028/* Generate code for a pointer assignment. */
2029
4ee9c684 2030tree
2031gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
2032{
2033 gfc_se lse;
2034 gfc_se rse;
2035 gfc_ss *lss;
2036 gfc_ss *rss;
2037 stmtblock_t block;
4ee9c684 2038
2039 gfc_start_block (&block);
2040
2041 gfc_init_se (&lse, NULL);
2042
2043 lss = gfc_walk_expr (expr1);
2044 rss = gfc_walk_expr (expr2);
2045 if (lss == gfc_ss_terminator)
2046 {
4396343e 2047 /* Scalar pointers. */
4ee9c684 2048 lse.want_pointer = 1;
2049 gfc_conv_expr (&lse, expr1);
22d678e8 2050 gcc_assert (rss == gfc_ss_terminator);
4ee9c684 2051 gfc_init_se (&rse, NULL);
2052 rse.want_pointer = 1;
2053 gfc_conv_expr (&rse, expr2);
2054 gfc_add_block_to_block (&block, &lse.pre);
2055 gfc_add_block_to_block (&block, &rse.pre);
260abd71 2056 gfc_add_modify_expr (&block, lse.expr,
2057 fold_convert (TREE_TYPE (lse.expr), rse.expr));
4ee9c684 2058 gfc_add_block_to_block (&block, &rse.post);
2059 gfc_add_block_to_block (&block, &lse.post);
2060 }
2061 else
2062 {
4396343e 2063 /* Array pointer. */
4ee9c684 2064 gfc_conv_expr_descriptor (&lse, expr1, lss);
2065 /* Implement Nullify. */
2066 if (expr2->expr_type == EXPR_NULL)
2067 {
2068 lse.expr = gfc_conv_descriptor_data (lse.expr);
260abd71 2069 rse.expr = fold_convert (TREE_TYPE (lse.expr), null_pointer_node);
2070 gfc_add_modify_expr (&block, lse.expr, rse.expr);
4ee9c684 2071 }
2072 else
2073 {
2074 lse.direct_byref = 1;
2075 gfc_conv_expr_descriptor (&lse, expr2, rss);
2076 }
2077 gfc_add_block_to_block (&block, &lse.pre);
2078 gfc_add_block_to_block (&block, &lse.post);
2079 }
2080 return gfc_finish_block (&block);
2081}
2082
2083
2084/* Makes sure se is suitable for passing as a function string parameter. */
2085/* TODO: Need to check all callers fo this function. It may be abused. */
2086
2087void
2088gfc_conv_string_parameter (gfc_se * se)
2089{
2090 tree type;
2091
2092 if (TREE_CODE (se->expr) == STRING_CST)
2093 {
2094 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
2095 return;
2096 }
2097
2098 type = TREE_TYPE (se->expr);
2099 if (TYPE_STRING_FLAG (type))
2100 {
22d678e8 2101 gcc_assert (TREE_CODE (se->expr) != INDIRECT_REF);
4ee9c684 2102 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
2103 }
2104
22d678e8 2105 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
2106 gcc_assert (se->string_length
4ee9c684 2107 && TREE_CODE (TREE_TYPE (se->string_length)) == INTEGER_TYPE);
2108}
2109
2110
2111/* Generate code for assignment of scalar variables. Includes character
2112 strings. */
2113
2114tree
2115gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, bt type)
2116{
4ee9c684 2117 stmtblock_t block;
2118
2119 gfc_init_block (&block);
2120
4ee9c684 2121 if (type == BT_CHARACTER)
2122 {
22d678e8 2123 gcc_assert (lse->string_length != NULL_TREE
4ee9c684 2124 && rse->string_length != NULL_TREE);
2125
2126 gfc_conv_string_parameter (lse);
2127 gfc_conv_string_parameter (rse);
2128
2129 gfc_add_block_to_block (&block, &lse->pre);
2130 gfc_add_block_to_block (&block, &rse->pre);
2131
dbe60343 2132 gfc_trans_string_copy (&block, lse->string_length, lse->expr,
2133 rse->string_length, rse->expr);
4ee9c684 2134 }
2135 else
2136 {
2137 gfc_add_block_to_block (&block, &lse->pre);
2138 gfc_add_block_to_block (&block, &rse->pre);
2139
260abd71 2140 gfc_add_modify_expr (&block, lse->expr,
2141 fold_convert (TREE_TYPE (lse->expr), rse->expr));
4ee9c684 2142 }
2143
2144 gfc_add_block_to_block (&block, &lse->post);
2145 gfc_add_block_to_block (&block, &rse->post);
2146
2147 return gfc_finish_block (&block);
2148}
2149
2150
2151/* Try to translate array(:) = func (...), where func is a transformational
2152 array function, without using a temporary. Returns NULL is this isn't the
2153 case. */
2154
2155static tree
2156gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
2157{
2158 gfc_se se;
2159 gfc_ss *ss;
2160
2161 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
2162 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
2163 return NULL;
2164
2165 /* Elemental functions don't need a temporary anyway. */
2166 if (expr2->symtree->n.sym->attr.elemental)
2167 return NULL;
2168
2169 /* Check for a dependency. */
2170 if (gfc_check_fncall_dependency (expr1, expr2))
2171 return NULL;
2172
2173 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
2174 functions. */
22d678e8 2175 gcc_assert (expr2->value.function.isym
e2293887 2176 || (gfc_return_by_reference (expr2->value.function.esym)
2177 && expr2->value.function.esym->result->attr.dimension));
4ee9c684 2178
2179 ss = gfc_walk_expr (expr1);
22d678e8 2180 gcc_assert (ss != gfc_ss_terminator);
4ee9c684 2181 gfc_init_se (&se, NULL);
2182 gfc_start_block (&se.pre);
2183 se.want_pointer = 1;
2184
2185 gfc_conv_array_parameter (&se, expr1, ss, 0);
2186
2187 se.direct_byref = 1;
2188 se.ss = gfc_walk_expr (expr2);
22d678e8 2189 gcc_assert (se.ss != gfc_ss_terminator);
4ee9c684 2190 gfc_conv_function_expr (&se, expr2);
4ee9c684 2191 gfc_add_block_to_block (&se.pre, &se.post);
2192
2193 return gfc_finish_block (&se.pre);
2194}
2195
2196
2197/* Translate an assignment. Most of the code is concerned with
2198 setting up the scalarizer. */
2199
2200tree
2201gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2)
2202{
2203 gfc_se lse;
2204 gfc_se rse;
2205 gfc_ss *lss;
2206 gfc_ss *lss_section;
2207 gfc_ss *rss;
2208 gfc_loopinfo loop;
2209 tree tmp;
2210 stmtblock_t block;
2211 stmtblock_t body;
2212
2213 /* Special case a single function returning an array. */
2214 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
2215 {
2216 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
2217 if (tmp)
2218 return tmp;
2219 }
2220
2221 /* Assignment of the form lhs = rhs. */
2222 gfc_start_block (&block);
2223
2224 gfc_init_se (&lse, NULL);
2225 gfc_init_se (&rse, NULL);
2226
2227 /* Walk the lhs. */
2228 lss = gfc_walk_expr (expr1);
2229 rss = NULL;
2230 if (lss != gfc_ss_terminator)
2231 {
2232 /* The assignment needs scalarization. */
2233 lss_section = lss;
2234
2235 /* Find a non-scalar SS from the lhs. */
2236 while (lss_section != gfc_ss_terminator
2237 && lss_section->type != GFC_SS_SECTION)
2238 lss_section = lss_section->next;
2239
22d678e8 2240 gcc_assert (lss_section != gfc_ss_terminator);
4ee9c684 2241
2242 /* Initialize the scalarizer. */
2243 gfc_init_loopinfo (&loop);
2244
2245 /* Walk the rhs. */
2246 rss = gfc_walk_expr (expr2);
2247 if (rss == gfc_ss_terminator)
2248 {
2249 /* The rhs is scalar. Add a ss for the expression. */
2250 rss = gfc_get_ss ();
2251 rss->next = gfc_ss_terminator;
2252 rss->type = GFC_SS_SCALAR;
2253 rss->expr = expr2;
2254 }
2255 /* Associate the SS with the loop. */
2256 gfc_add_ss_to_loop (&loop, lss);
2257 gfc_add_ss_to_loop (&loop, rss);
2258
2259 /* Calculate the bounds of the scalarization. */
2260 gfc_conv_ss_startstride (&loop);
2261 /* Resolve any data dependencies in the statement. */
2262 gfc_conv_resolve_dependencies (&loop, lss_section, rss);
2263 /* Setup the scalarizing loops. */
2264 gfc_conv_loop_setup (&loop);
2265
2266 /* Setup the gfc_se structures. */
2267 gfc_copy_loopinfo_to_se (&lse, &loop);
2268 gfc_copy_loopinfo_to_se (&rse, &loop);
2269
2270 rse.ss = rss;
2271 gfc_mark_ss_chain_used (rss, 1);
2272 if (loop.temp_ss == NULL)
2273 {
2274 lse.ss = lss;
2275 gfc_mark_ss_chain_used (lss, 1);
2276 }
2277 else
2278 {
2279 lse.ss = loop.temp_ss;
2280 gfc_mark_ss_chain_used (lss, 3);
2281 gfc_mark_ss_chain_used (loop.temp_ss, 3);
2282 }
2283
2284 /* Start the scalarized loop body. */
2285 gfc_start_scalarized_body (&loop, &body);
2286 }
2287 else
2288 gfc_init_block (&body);
2289
2290 /* Translate the expression. */
2291 gfc_conv_expr (&rse, expr2);
2292
2293 if (lss != gfc_ss_terminator && loop.temp_ss != NULL)
2294 {
2295 gfc_conv_tmp_array_ref (&lse);
2296 gfc_advance_se_ss_chain (&lse);
2297 }
2298 else
2299 gfc_conv_expr (&lse, expr1);
544c333b 2300
4ee9c684 2301 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts.type);
2302 gfc_add_expr_to_block (&body, tmp);
2303
2304 if (lss == gfc_ss_terminator)
2305 {
2306 /* Use the scalar assignment as is. */
2307 gfc_add_block_to_block (&block, &body);
2308 }
2309 else
2310 {
22d678e8 2311 gcc_assert (lse.ss == gfc_ss_terminator
2312 && rse.ss == gfc_ss_terminator);
4ee9c684 2313
2314 if (loop.temp_ss != NULL)
2315 {
2316 gfc_trans_scalarized_loop_boundary (&loop, &body);
2317
2318 /* We need to copy the temporary to the actual lhs. */
2319 gfc_init_se (&lse, NULL);
2320 gfc_init_se (&rse, NULL);
2321 gfc_copy_loopinfo_to_se (&lse, &loop);
2322 gfc_copy_loopinfo_to_se (&rse, &loop);
2323
2324 rse.ss = loop.temp_ss;
2325 lse.ss = lss;
2326
2327 gfc_conv_tmp_array_ref (&rse);
2328 gfc_advance_se_ss_chain (&rse);
2329 gfc_conv_expr (&lse, expr1);
2330
22d678e8 2331 gcc_assert (lse.ss == gfc_ss_terminator
2332 && rse.ss == gfc_ss_terminator);
4ee9c684 2333
2334 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts.type);
2335 gfc_add_expr_to_block (&body, tmp);
2336 }
2337 /* Generate the copying loops. */
2338 gfc_trans_scalarizing_loops (&loop, &body);
2339
2340 /* Wrap the whole thing up. */
2341 gfc_add_block_to_block (&block, &loop.pre);
2342 gfc_add_block_to_block (&block, &loop.post);
2343
2344 gfc_cleanup_loop (&loop);
2345 }
2346
2347 return gfc_finish_block (&block);
2348}
2349
2350tree
2351gfc_trans_assign (gfc_code * code)
2352{
2353 return gfc_trans_assignment (code->expr, code->expr2);
2354}