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