OSDN Git Service

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