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>
6 This file is part of GCC.
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
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
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
23 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
27 #include "coretypes.h"
33 #include "tree-gimple.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"
43 static tree
gfc_trans_structure_assign (tree dest
, gfc_expr
* expr
);
45 /* Copy the scalarization loop variables. */
48 gfc_copy_se_loopvars (gfc_se
* dest
, gfc_se
* src
)
51 dest
->loop
= src
->loop
;
55 /* Initialize a simple expression holder.
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. */
62 gfc_init_se (gfc_se
* se
, gfc_se
* parent
)
64 memset (se
, 0, sizeof (gfc_se
));
65 gfc_init_block (&se
->pre
);
66 gfc_init_block (&se
->post
);
71 gfc_copy_se_loopvars (se
, parent
);
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.
80 gfc_advance_se_ss_chain (gfc_se
* se
)
84 gcc_assert (se
!= NULL
&& se
->ss
!= NULL
&& se
->ss
!= gfc_ss_terminator
);
87 /* Walk down the parent chain. */
90 /* Simple consistency check. */
91 gcc_assert (p
->parent
== NULL
|| p
->parent
->ss
== p
->ss
);
100 /* Ensures the result of the expression as either a temporary variable
101 or a constant so that it can be used repeatedly. */
104 gfc_make_safe_expr (gfc_se
* se
)
108 if (CONSTANT_CLASS_P (se
->expr
))
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
);
118 /* Return an expression which determines if a dummy parameter is present. */
121 gfc_conv_expr_present (gfc_symbol
* sym
)
125 gcc_assert (sym
->attr
.dummy
&& sym
->attr
.optional
);
127 decl
= gfc_get_symbol_decl (sym
);
128 if (TREE_CODE (decl
) != PARM_DECL
)
130 /* Array parameters use a temporary descriptor, we want the real
132 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl
))
133 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl
)));
134 decl
= GFC_DECL_SAVED_DESCRIPTOR (decl
);
136 return build2 (NE_EXPR
, boolean_type_node
, decl
,
137 fold_convert (TREE_TYPE (decl
), null_pointer_node
));
141 /* Get the character length of an expression, looking through gfc_refs
145 gfc_get_expr_charlen (gfc_expr
*e
)
150 gcc_assert (e
->expr_type
== EXPR_VARIABLE
151 && e
->ts
.type
== BT_CHARACTER
);
153 length
= NULL
; /* To silence compiler warning. */
155 /* First candidate: if the variable is of type CHARACTER, the
156 expression's length could be the length of the character
158 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
)
159 length
= e
->symtree
->n
.sym
->ts
.cl
->backend_decl
;
161 /* Look through the reference chain for component references. */
162 for (r
= e
->ref
; r
; r
= r
->next
)
167 if (r
->u
.c
.component
->ts
.type
== BT_CHARACTER
)
168 length
= r
->u
.c
.component
->ts
.cl
->backend_decl
;
176 /* We should never got substring references here. These will be
177 broken down by the scalarizer. */
182 gcc_assert (length
!= NULL
);
188 /* Generate code to initialize a string length variable. Returns the
192 gfc_trans_init_string_length (gfc_charlen
* cl
, stmtblock_t
* pblock
)
197 gfc_init_se (&se
, NULL
);
198 gfc_conv_expr_type (&se
, cl
->length
, gfc_charlen_type_node
);
199 gfc_add_block_to_block (pblock
, &se
.pre
);
201 tmp
= cl
->backend_decl
;
202 gfc_add_modify_expr (pblock
, tmp
, se
.expr
);
207 gfc_conv_substring (gfc_se
* se
, gfc_ref
* ref
, int kind
)
215 type
= gfc_get_character_type (kind
, ref
->u
.ss
.length
);
216 type
= build_pointer_type (type
);
219 gfc_init_se (&start
, se
);
220 gfc_conv_expr_type (&start
, ref
->u
.ss
.start
, gfc_charlen_type_node
);
221 gfc_add_block_to_block (&se
->pre
, &start
.pre
);
223 if (integer_onep (start
.expr
))
224 gfc_conv_string_parameter (se
);
227 /* Change the start of the string. */
228 if (TYPE_STRING_FLAG (TREE_TYPE (se
->expr
)))
231 tmp
= gfc_build_indirect_ref (se
->expr
);
232 tmp
= gfc_build_array_ref (tmp
, start
.expr
);
233 se
->expr
= gfc_build_addr_expr (type
, tmp
);
236 /* Length = end + 1 - start. */
237 gfc_init_se (&end
, se
);
238 if (ref
->u
.ss
.end
== NULL
)
239 end
.expr
= se
->string_length
;
242 gfc_conv_expr_type (&end
, ref
->u
.ss
.end
, gfc_charlen_type_node
);
243 gfc_add_block_to_block (&se
->pre
, &end
.pre
);
246 build2 (MINUS_EXPR
, gfc_charlen_type_node
,
247 fold_convert (gfc_charlen_type_node
, integer_one_node
),
249 tmp
= build2 (PLUS_EXPR
, gfc_charlen_type_node
, end
.expr
, tmp
);
250 se
->string_length
= fold (tmp
);
254 /* Convert a derived type component reference. */
257 gfc_conv_component_ref (gfc_se
* se
, gfc_ref
* ref
)
264 c
= ref
->u
.c
.component
;
266 gcc_assert (c
->backend_decl
);
268 field
= c
->backend_decl
;
269 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
271 tmp
= build3 (COMPONENT_REF
, TREE_TYPE (field
), decl
, field
, NULL_TREE
);
275 if (c
->ts
.type
== BT_CHARACTER
)
277 tmp
= c
->ts
.cl
->backend_decl
;
278 /* Components must always be constant length. */
279 gcc_assert (tmp
&& INTEGER_CST_P (tmp
));
280 se
->string_length
= tmp
;
283 if (c
->pointer
&& c
->dimension
== 0)
284 se
->expr
= gfc_build_indirect_ref (se
->expr
);
288 /* Return the contents of a variable. Also handles reference/pointer
289 variables (all Fortran pointer references are implicit). */
292 gfc_conv_variable (gfc_se
* se
, gfc_expr
* expr
)
297 sym
= expr
->symtree
->n
.sym
;
300 /* Check that something hasn't gone horribly wrong. */
301 gcc_assert (se
->ss
!= gfc_ss_terminator
);
302 gcc_assert (se
->ss
->expr
== expr
);
304 /* A scalarized term. We already know the descriptor. */
305 se
->expr
= se
->ss
->data
.info
.descriptor
;
306 se
->string_length
= se
->ss
->string_length
;
307 ref
= se
->ss
->data
.info
.ref
;
311 se
->expr
= gfc_get_symbol_decl (sym
);
313 /* Procedure actual arguments. */
314 if (sym
->attr
.flavor
== FL_PROCEDURE
315 && se
->expr
!= current_function_decl
)
317 gcc_assert (se
->want_pointer
);
318 if (!sym
->attr
.dummy
)
320 gcc_assert (TREE_CODE (se
->expr
) == FUNCTION_DECL
);
321 se
->expr
= gfc_build_addr_expr (NULL
, se
->expr
);
326 /* Special case for assigning the return value of a function.
327 Self recursive functions must have an explicit return value. */
328 if (se
->expr
== current_function_decl
&& sym
->attr
.function
329 && (sym
->result
== sym
))
331 se
->expr
= gfc_get_fake_result_decl (sym
);
334 /* Dereference scalar dummy variables. */
336 && sym
->ts
.type
!= BT_CHARACTER
337 && !sym
->attr
.dimension
)
338 se
->expr
= gfc_build_indirect_ref (se
->expr
);
340 /* Dereference pointer variables. */
341 if ((sym
->attr
.pointer
|| sym
->attr
.allocatable
)
344 || sym
->attr
.function
345 || !sym
->attr
.dimension
)
346 && sym
->ts
.type
!= BT_CHARACTER
)
347 se
->expr
= gfc_build_indirect_ref (se
->expr
);
352 /* For character variables, also get the length. */
353 if (sym
->ts
.type
== BT_CHARACTER
)
355 se
->string_length
= sym
->ts
.cl
->backend_decl
;
356 gcc_assert (se
->string_length
);
364 /* Return the descriptor if that's what we want and this is an array
365 section reference. */
366 if (se
->descriptor_only
&& ref
->u
.ar
.type
!= AR_ELEMENT
)
368 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
369 /* Return the descriptor for array pointers and allocations. */
371 && ref
->next
== NULL
&& (se
->descriptor_only
))
374 gfc_conv_array_ref (se
, &ref
->u
.ar
);
375 /* Return a pointer to an element. */
379 gfc_conv_component_ref (se
, ref
);
383 gfc_conv_substring (se
, ref
, expr
->ts
.kind
);
392 /* Pointer assignment, allocation or pass by reference. Arrays are handled
394 if (se
->want_pointer
)
396 if (expr
->ts
.type
== BT_CHARACTER
)
397 gfc_conv_string_parameter (se
);
399 se
->expr
= gfc_build_addr_expr (NULL
, se
->expr
);
402 gfc_advance_se_ss_chain (se
);
406 /* Unary ops are easy... Or they would be if ! was a valid op. */
409 gfc_conv_unary_op (enum tree_code code
, gfc_se
* se
, gfc_expr
* expr
)
414 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
415 /* Initialize the operand. */
416 gfc_init_se (&operand
, se
);
417 gfc_conv_expr_val (&operand
, expr
->value
.op
.op1
);
418 gfc_add_block_to_block (&se
->pre
, &operand
.pre
);
420 type
= gfc_typenode_for_spec (&expr
->ts
);
422 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
423 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
424 All other unary operators have an equivalent GIMPLE unary operator. */
425 if (code
== TRUTH_NOT_EXPR
)
426 se
->expr
= build2 (EQ_EXPR
, type
, operand
.expr
,
427 convert (type
, integer_zero_node
));
429 se
->expr
= build1 (code
, type
, operand
.expr
);
433 /* Expand power operator to optimal multiplications when a value is raised
434 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
435 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
436 Programming", 3rd Edition, 1998. */
438 /* This code is mostly duplicated from expand_powi in the backend.
439 We establish the "optimal power tree" lookup table with the defined size.
440 The items in the table are the exponents used to calculate the index
441 exponents. Any integer n less than the value can get an "addition chain",
442 with the first node being one. */
443 #define POWI_TABLE_SIZE 256
445 /* The table is from builtins.c. */
446 static const unsigned char powi_table
[POWI_TABLE_SIZE
] =
448 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
449 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
450 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
451 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
452 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
453 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
454 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
455 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
456 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
457 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
458 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
459 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
460 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
461 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
462 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
463 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
464 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
465 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
466 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
467 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
468 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
469 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
470 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
471 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
472 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
473 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
474 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
475 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
476 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
477 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
478 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
479 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
482 /* If n is larger than lookup table's max index, we use the "window
484 #define POWI_WINDOW_SIZE 3
486 /* Recursive function to expand the power operator. The temporary
487 values are put in tmpvar. The function returns tmpvar[1] ** n. */
489 gfc_conv_powi (gfc_se
* se
, int n
, tree
* tmpvar
)
496 if (n
< POWI_TABLE_SIZE
)
501 op0
= gfc_conv_powi (se
, n
- powi_table
[n
], tmpvar
);
502 op1
= gfc_conv_powi (se
, powi_table
[n
], tmpvar
);
506 digit
= n
& ((1 << POWI_WINDOW_SIZE
) - 1);
507 op0
= gfc_conv_powi (se
, n
- digit
, tmpvar
);
508 op1
= gfc_conv_powi (se
, digit
, tmpvar
);
512 op0
= gfc_conv_powi (se
, n
>> 1, tmpvar
);
516 tmp
= fold (build2 (MULT_EXPR
, TREE_TYPE (op0
), op0
, op1
));
517 tmp
= gfc_evaluate_now (tmp
, &se
->pre
);
519 if (n
< POWI_TABLE_SIZE
)
526 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
527 return 1. Else return 0 and a call to runtime library functions
528 will have to be built. */
530 gfc_conv_cst_int_power (gfc_se
* se
, tree lhs
, tree rhs
)
535 tree vartmp
[POWI_TABLE_SIZE
];
539 type
= TREE_TYPE (lhs
);
540 n
= abs (TREE_INT_CST_LOW (rhs
));
541 sgn
= tree_int_cst_sgn (rhs
);
543 if (((FLOAT_TYPE_P (type
) && !flag_unsafe_math_optimizations
) || optimize_size
)
544 && (n
> 2 || n
< -1))
550 se
->expr
= gfc_build_const (type
, integer_one_node
);
553 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
554 if ((sgn
== -1) && (TREE_CODE (type
) == INTEGER_TYPE
))
556 tmp
= build2 (EQ_EXPR
, boolean_type_node
, lhs
,
557 fold_convert (TREE_TYPE (lhs
), integer_minus_one_node
));
558 cond
= build2 (EQ_EXPR
, boolean_type_node
, lhs
,
559 convert (TREE_TYPE (lhs
), integer_one_node
));
562 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
565 tmp
= build2 (TRUTH_OR_EXPR
, boolean_type_node
, tmp
, cond
);
566 se
->expr
= build3 (COND_EXPR
, type
, tmp
,
567 convert (type
, integer_one_node
),
568 convert (type
, integer_zero_node
));
572 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
573 tmp
= build3 (COND_EXPR
, type
, tmp
,
574 convert (type
, integer_minus_one_node
),
575 convert (type
, integer_zero_node
));
576 se
->expr
= build3 (COND_EXPR
, type
, cond
,
577 convert (type
, integer_one_node
),
582 memset (vartmp
, 0, sizeof (vartmp
));
586 tmp
= gfc_build_const (type
, integer_one_node
);
587 vartmp
[1] = build2 (RDIV_EXPR
, type
, tmp
, vartmp
[1]);
590 se
->expr
= gfc_conv_powi (se
, n
, vartmp
);
596 /* Power op (**). Constant integer exponent has special handling. */
599 gfc_conv_power_op (gfc_se
* se
, gfc_expr
* expr
)
601 tree gfc_int4_type_node
;
609 gfc_init_se (&lse
, se
);
610 gfc_conv_expr_val (&lse
, expr
->value
.op
.op1
);
611 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
613 gfc_init_se (&rse
, se
);
614 gfc_conv_expr_val (&rse
, expr
->value
.op
.op2
);
615 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
617 if (expr
->value
.op
.op2
->ts
.type
== BT_INTEGER
618 && expr
->value
.op
.op2
->expr_type
== EXPR_CONSTANT
)
619 if (gfc_conv_cst_int_power (se
, lse
.expr
, rse
.expr
))
622 gfc_int4_type_node
= gfc_get_int_type (4);
624 kind
= expr
->value
.op
.op1
->ts
.kind
;
625 switch (expr
->value
.op
.op2
->ts
.type
)
628 ikind
= expr
->value
.op
.op2
->ts
.kind
;
633 rse
.expr
= convert (gfc_int4_type_node
, rse
.expr
);
651 if (expr
->value
.op
.op1
->ts
.type
== BT_INTEGER
)
652 lse
.expr
= convert (gfc_int4_type_node
, lse
.expr
);
669 switch (expr
->value
.op
.op1
->ts
.type
)
672 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].integer
;
676 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].real
;
680 fndecl
= gfor_fndecl_math_powi
[kind
][ikind
].cmplx
;
692 fndecl
= built_in_decls
[BUILT_IN_POWF
];
695 fndecl
= built_in_decls
[BUILT_IN_POW
];
706 fndecl
= gfor_fndecl_math_cpowf
;
709 fndecl
= gfor_fndecl_math_cpow
;
721 tmp
= gfc_chainon_list (NULL_TREE
, lse
.expr
);
722 tmp
= gfc_chainon_list (tmp
, rse
.expr
);
723 se
->expr
= fold (gfc_build_function_call (fndecl
, tmp
));
727 /* Generate code to allocate a string temporary. */
730 gfc_conv_string_tmp (gfc_se
* se
, tree type
, tree len
)
736 gcc_assert (TREE_TYPE (len
) == gfc_charlen_type_node
);
738 if (gfc_can_put_var_on_stack (len
))
740 /* Create a temporary variable to hold the result. */
741 tmp
= fold (build2 (MINUS_EXPR
, gfc_charlen_type_node
, len
,
742 convert (gfc_charlen_type_node
,
744 tmp
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
, tmp
);
745 tmp
= build_array_type (gfc_character1_type_node
, tmp
);
746 var
= gfc_create_var (tmp
, "str");
747 var
= gfc_build_addr_expr (type
, var
);
751 /* Allocate a temporary to hold the result. */
752 var
= gfc_create_var (type
, "pstr");
753 args
= gfc_chainon_list (NULL_TREE
, len
);
754 tmp
= gfc_build_function_call (gfor_fndecl_internal_malloc
, args
);
755 tmp
= convert (type
, tmp
);
756 gfc_add_modify_expr (&se
->pre
, var
, tmp
);
758 /* Free the temporary afterwards. */
759 tmp
= convert (pvoid_type_node
, var
);
760 args
= gfc_chainon_list (NULL_TREE
, tmp
);
761 tmp
= gfc_build_function_call (gfor_fndecl_internal_free
, args
);
762 gfc_add_expr_to_block (&se
->post
, tmp
);
769 /* Handle a string concatenation operation. A temporary will be allocated to
773 gfc_conv_concat_op (gfc_se
* se
, gfc_expr
* expr
)
783 gcc_assert (expr
->value
.op
.op1
->ts
.type
== BT_CHARACTER
784 && expr
->value
.op
.op2
->ts
.type
== BT_CHARACTER
);
786 gfc_init_se (&lse
, se
);
787 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
788 gfc_conv_string_parameter (&lse
);
789 gfc_init_se (&rse
, se
);
790 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
791 gfc_conv_string_parameter (&rse
);
793 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
794 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
796 type
= gfc_get_character_type (expr
->ts
.kind
, expr
->ts
.cl
);
797 len
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
798 if (len
== NULL_TREE
)
800 len
= fold (build2 (PLUS_EXPR
, TREE_TYPE (lse
.string_length
),
801 lse
.string_length
, rse
.string_length
));
804 type
= build_pointer_type (type
);
806 var
= gfc_conv_string_tmp (se
, type
, len
);
808 /* Do the actual concatenation. */
810 args
= gfc_chainon_list (args
, len
);
811 args
= gfc_chainon_list (args
, var
);
812 args
= gfc_chainon_list (args
, lse
.string_length
);
813 args
= gfc_chainon_list (args
, lse
.expr
);
814 args
= gfc_chainon_list (args
, rse
.string_length
);
815 args
= gfc_chainon_list (args
, rse
.expr
);
816 tmp
= gfc_build_function_call (gfor_fndecl_concat_string
, args
);
817 gfc_add_expr_to_block (&se
->pre
, tmp
);
819 /* Add the cleanup for the operands. */
820 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
821 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
824 se
->string_length
= len
;
828 /* Translates an op expression. Common (binary) cases are handled by this
829 function, others are passed on. Recursion is used in either case.
830 We use the fact that (op1.ts == op2.ts) (except for the power
832 Operators need no special handling for scalarized expressions as long as
833 they call gfc_conv_simple_val to get their operands.
834 Character strings get special handling. */
837 gfc_conv_expr_op (gfc_se
* se
, gfc_expr
* expr
)
849 switch (expr
->value
.op
.operator)
851 case INTRINSIC_UPLUS
:
852 gfc_conv_expr (se
, expr
->value
.op
.op1
);
855 case INTRINSIC_UMINUS
:
856 gfc_conv_unary_op (NEGATE_EXPR
, se
, expr
);
860 gfc_conv_unary_op (TRUTH_NOT_EXPR
, se
, expr
);
867 case INTRINSIC_MINUS
:
871 case INTRINSIC_TIMES
:
875 case INTRINSIC_DIVIDE
:
876 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
877 an integer, we must round towards zero, so we use a
879 if (expr
->ts
.type
== BT_INTEGER
)
880 code
= TRUNC_DIV_EXPR
;
885 case INTRINSIC_POWER
:
886 gfc_conv_power_op (se
, expr
);
889 case INTRINSIC_CONCAT
:
890 gfc_conv_concat_op (se
, expr
);
894 code
= TRUTH_ANDIF_EXPR
;
899 code
= TRUTH_ORIF_EXPR
;
903 /* EQV and NEQV only work on logicals, but since we represent them
904 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
944 case INTRINSIC_ASSIGN
:
945 /* These should be converted into function calls by the frontend. */
949 fatal_error ("Unknown intrinsic op");
953 /* The only exception to this is **, which is handled separately anyway. */
954 gcc_assert (expr
->value
.op
.op1
->ts
.type
== expr
->value
.op
.op2
->ts
.type
);
956 if (checkstring
&& expr
->value
.op
.op1
->ts
.type
!= BT_CHARACTER
)
960 gfc_init_se (&lse
, se
);
961 gfc_conv_expr (&lse
, expr
->value
.op
.op1
);
962 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
965 gfc_init_se (&rse
, se
);
966 gfc_conv_expr (&rse
, expr
->value
.op
.op2
);
967 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
969 /* For string comparisons we generate a library call, and compare the return
973 gfc_conv_string_parameter (&lse
);
974 gfc_conv_string_parameter (&rse
);
976 tmp
= gfc_chainon_list (tmp
, lse
.string_length
);
977 tmp
= gfc_chainon_list (tmp
, lse
.expr
);
978 tmp
= gfc_chainon_list (tmp
, rse
.string_length
);
979 tmp
= gfc_chainon_list (tmp
, rse
.expr
);
981 /* Build a call for the comparison. */
982 lse
.expr
= gfc_build_function_call (gfor_fndecl_compare_string
, tmp
);
983 gfc_add_block_to_block (&lse
.post
, &rse
.post
);
985 rse
.expr
= integer_zero_node
;
988 type
= gfc_typenode_for_spec (&expr
->ts
);
992 /* The result of logical ops is always boolean_type_node. */
993 tmp
= fold (build2 (code
, type
, lse
.expr
, rse
.expr
));
994 se
->expr
= convert (type
, tmp
);
997 se
->expr
= fold (build2 (code
, type
, lse
.expr
, rse
.expr
));
999 /* Add the post blocks. */
1000 gfc_add_block_to_block (&se
->post
, &rse
.post
);
1001 gfc_add_block_to_block (&se
->post
, &lse
.post
);
1006 gfc_conv_function_val (gfc_se
* se
, gfc_symbol
* sym
)
1010 if (sym
->attr
.dummy
)
1012 tmp
= gfc_get_symbol_decl (sym
);
1013 gcc_assert (TREE_CODE (TREE_TYPE (tmp
)) == POINTER_TYPE
1014 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp
))) == FUNCTION_TYPE
);
1020 if (!sym
->backend_decl
)
1021 sym
->backend_decl
= gfc_get_extern_function_decl (sym
);
1023 tmp
= sym
->backend_decl
;
1024 gcc_assert (TREE_CODE (tmp
) == FUNCTION_DECL
);
1025 se
->expr
= gfc_build_addr_expr (NULL
, tmp
);
1030 /* Generate code for a procedure call. Note can return se->post != NULL.
1031 If se->direct_byref is set then se->expr contains the return parameter. */
1034 gfc_conv_function_call (gfc_se
* se
, gfc_symbol
* sym
,
1035 gfc_actual_arglist
* arg
)
1048 gfc_formal_arglist
*formal
;
1050 arglist
= NULL_TREE
;
1051 stringargs
= NULL_TREE
;
1057 if (!sym
->attr
.elemental
)
1059 gcc_assert (se
->ss
->type
== GFC_SS_FUNCTION
);
1060 if (se
->ss
->useflags
)
1062 gcc_assert (gfc_return_by_reference (sym
)
1063 && sym
->result
->attr
.dimension
);
1064 gcc_assert (se
->loop
!= NULL
);
1066 /* Access the previously obtained result. */
1067 gfc_conv_tmp_array_ref (se
);
1068 gfc_advance_se_ss_chain (se
);
1072 info
= &se
->ss
->data
.info
;
1077 byref
= gfc_return_by_reference (sym
);
1080 if (se
->direct_byref
)
1081 arglist
= gfc_chainon_list (arglist
, se
->expr
);
1082 else if (sym
->result
->attr
.dimension
)
1084 gcc_assert (se
->loop
&& se
->ss
);
1085 /* Set the type of the array. */
1086 tmp
= gfc_typenode_for_spec (&sym
->ts
);
1087 info
->dimen
= se
->loop
->dimen
;
1088 /* Allocate a temporary to store the result. */
1089 gfc_trans_allocate_temp_array (se
->loop
, info
, tmp
);
1091 /* Zero the first stride to indicate a temporary. */
1093 gfc_conv_descriptor_stride (info
->descriptor
, gfc_rank_cst
[0]);
1094 gfc_add_modify_expr (&se
->pre
, tmp
,
1095 convert (TREE_TYPE (tmp
), integer_zero_node
));
1096 /* Pass the temporary as the first argument. */
1097 tmp
= info
->descriptor
;
1098 tmp
= gfc_build_addr_expr (NULL
, tmp
);
1099 arglist
= gfc_chainon_list (arglist
, tmp
);
1101 else if (sym
->ts
.type
== BT_CHARACTER
)
1103 gcc_assert (sym
->ts
.cl
&& sym
->ts
.cl
->length
1104 && sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1105 len
= gfc_conv_mpz_to_tree
1106 (sym
->ts
.cl
->length
->value
.integer
, sym
->ts
.cl
->length
->ts
.kind
);
1107 sym
->ts
.cl
->backend_decl
= len
;
1108 type
= gfc_get_character_type (sym
->ts
.kind
, sym
->ts
.cl
);
1109 type
= build_pointer_type (type
);
1111 var
= gfc_conv_string_tmp (se
, type
, len
);
1112 arglist
= gfc_chainon_list (arglist
, var
);
1113 arglist
= gfc_chainon_list (arglist
,
1114 convert (gfc_charlen_type_node
, len
));
1120 formal
= sym
->formal
;
1121 /* Evaluate the arguments. */
1122 for (; arg
!= NULL
; arg
= arg
->next
, formal
= formal
? formal
->next
: NULL
)
1124 if (arg
->expr
== NULL
)
1127 if (se
->ignore_optional
)
1129 /* Some intrinsics have already been resolved to the correct
1133 else if (arg
->label
)
1135 has_alternate_specifier
= 1;
1140 /* Pass a NULL pointer for an absent arg. */
1141 gfc_init_se (&parmse
, NULL
);
1142 parmse
.expr
= null_pointer_node
;
1143 if (arg
->missing_arg_type
== BT_CHARACTER
)
1146 gfc_chainon_list (stringargs
,
1147 convert (gfc_charlen_type_node
,
1148 integer_zero_node
));
1152 else if (se
->ss
&& se
->ss
->useflags
)
1154 /* An elemental function inside a scalarized loop. */
1155 gfc_init_se (&parmse
, se
);
1156 gfc_conv_expr_reference (&parmse
, arg
->expr
);
1160 /* A scalar or transformational function. */
1161 gfc_init_se (&parmse
, NULL
);
1162 argss
= gfc_walk_expr (arg
->expr
);
1164 if (argss
== gfc_ss_terminator
)
1166 gfc_conv_expr_reference (&parmse
, arg
->expr
);
1167 if (formal
&& formal
->sym
->attr
.pointer
1168 && arg
->expr
->expr_type
!= EXPR_NULL
)
1170 /* Scalar pointer dummy args require an extra level of
1171 indirection. The null pointer already contains
1172 this level of indirection. */
1173 parmse
.expr
= gfc_build_addr_expr (NULL
, parmse
.expr
);
1178 /* If the procedure requires an explicit interface, the
1179 actual argument is passed according to the
1180 corresponding formal argument. If the corresponding
1181 formal argument is a POINTER or assumed shape, we do
1182 not use g77's calling convention, and pass the
1183 address of the array descriptor instead. Otherwise we
1184 use g77's calling convention. */
1186 f
= (formal
!= NULL
)
1187 && !formal
->sym
->attr
.pointer
1188 && formal
->sym
->as
->type
!= AS_ASSUMED_SHAPE
;
1189 f
= f
|| !sym
->attr
.always_explicit
;
1190 gfc_conv_array_parameter (&parmse
, arg
->expr
, argss
, f
);
1194 gfc_add_block_to_block (&se
->pre
, &parmse
.pre
);
1195 gfc_add_block_to_block (&se
->post
, &parmse
.post
);
1197 /* Character strings are passed as two parameters, a length and a
1199 if (parmse
.string_length
!= NULL_TREE
)
1200 stringargs
= gfc_chainon_list (stringargs
, parmse
.string_length
);
1202 arglist
= gfc_chainon_list (arglist
, parmse
.expr
);
1205 /* Add the hidden string length parameters to the arguments. */
1206 arglist
= chainon (arglist
, stringargs
);
1208 /* Generate the actual call. */
1209 gfc_conv_function_val (se
, sym
);
1210 /* If there are alternate return labels, function type should be
1212 if (has_alternate_specifier
)
1213 TREE_TYPE (TREE_TYPE (TREE_TYPE (se
->expr
))) = integer_type_node
;
1215 fntype
= TREE_TYPE (TREE_TYPE (se
->expr
));
1216 se
->expr
= build3 (CALL_EXPR
, TREE_TYPE (fntype
), se
->expr
,
1217 arglist
, NULL_TREE
);
1219 /* If we have a pointer function, but we don't want a pointer, e.g.
1222 where f is pointer valued, we have to dereference the result. */
1223 if (!se
->want_pointer
&& !byref
1224 && (sym
->attr
.pointer
|| (sym
->result
&& sym
->result
->attr
.pointer
)))
1225 se
->expr
= gfc_build_indirect_ref (se
->expr
);
1227 /* A pure function may still have side-effects - it may modify its
1229 TREE_SIDE_EFFECTS (se
->expr
) = 1;
1231 if (!sym
->attr
.pure
)
1232 TREE_SIDE_EFFECTS (se
->expr
) = 1;
1237 /* Add the function call to the pre chain. There is no expression. */
1238 gfc_add_expr_to_block (&se
->pre
, se
->expr
);
1239 se
->expr
= NULL_TREE
;
1241 if (!se
->direct_byref
)
1243 if (sym
->result
->attr
.dimension
)
1245 if (flag_bounds_check
)
1247 /* Check the data pointer hasn't been modified. This would
1248 happen in a function returning a pointer. */
1249 tmp
= gfc_conv_descriptor_data (info
->descriptor
);
1250 tmp
= build2 (NE_EXPR
, boolean_type_node
, tmp
, info
->data
);
1251 gfc_trans_runtime_check (tmp
, gfc_strconst_fault
, &se
->pre
);
1253 se
->expr
= info
->descriptor
;
1255 else if (sym
->ts
.type
== BT_CHARACTER
)
1258 se
->string_length
= len
;
1267 /* Generate code to copy a string. */
1270 gfc_trans_string_copy (stmtblock_t
* block
, tree dlen
, tree dest
,
1271 tree slen
, tree src
)
1276 tmp
= gfc_chainon_list (tmp
, dlen
);
1277 tmp
= gfc_chainon_list (tmp
, dest
);
1278 tmp
= gfc_chainon_list (tmp
, slen
);
1279 tmp
= gfc_chainon_list (tmp
, src
);
1280 tmp
= gfc_build_function_call (gfor_fndecl_copy_string
, tmp
);
1281 gfc_add_expr_to_block (block
, tmp
);
1285 /* Translate a statement function.
1286 The value of a statement function reference is obtained by evaluating the
1287 expression using the values of the actual arguments for the values of the
1288 corresponding dummy arguments. */
1291 gfc_conv_statement_function (gfc_se
* se
, gfc_expr
* expr
)
1295 gfc_formal_arglist
*fargs
;
1296 gfc_actual_arglist
*args
;
1299 gfc_saved_var
*saved_vars
;
1305 sym
= expr
->symtree
->n
.sym
;
1306 args
= expr
->value
.function
.actual
;
1307 gfc_init_se (&lse
, NULL
);
1308 gfc_init_se (&rse
, NULL
);
1311 for (fargs
= sym
->formal
; fargs
; fargs
= fargs
->next
)
1313 saved_vars
= (gfc_saved_var
*)gfc_getmem (n
* sizeof (gfc_saved_var
));
1314 temp_vars
= (tree
*)gfc_getmem (n
* sizeof (tree
));
1316 for (fargs
= sym
->formal
, n
= 0; fargs
; fargs
= fargs
->next
, n
++)
1318 /* Each dummy shall be specified, explicitly or implicitly, to be
1320 gcc_assert (fargs
->sym
->attr
.dimension
== 0);
1323 /* Create a temporary to hold the value. */
1324 type
= gfc_typenode_for_spec (&fsym
->ts
);
1325 temp_vars
[n
] = gfc_create_var (type
, fsym
->name
);
1327 if (fsym
->ts
.type
== BT_CHARACTER
)
1329 /* Copy string arguments. */
1332 gcc_assert (fsym
->ts
.cl
&& fsym
->ts
.cl
->length
1333 && fsym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1335 arglen
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
1336 tmp
= gfc_build_addr_expr (build_pointer_type (type
),
1339 gfc_conv_expr (&rse
, args
->expr
);
1340 gfc_conv_string_parameter (&rse
);
1341 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
1342 gfc_add_block_to_block (&se
->pre
, &rse
.pre
);
1344 gfc_trans_string_copy (&se
->pre
, arglen
, tmp
, rse
.string_length
,
1346 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
1347 gfc_add_block_to_block (&se
->pre
, &rse
.post
);
1351 /* For everything else, just evaluate the expression. */
1352 gfc_conv_expr (&lse
, args
->expr
);
1354 gfc_add_block_to_block (&se
->pre
, &lse
.pre
);
1355 gfc_add_modify_expr (&se
->pre
, temp_vars
[n
], lse
.expr
);
1356 gfc_add_block_to_block (&se
->pre
, &lse
.post
);
1362 /* Use the temporary variables in place of the real ones. */
1363 for (fargs
= sym
->formal
, n
= 0; fargs
; fargs
= fargs
->next
, n
++)
1364 gfc_shadow_sym (fargs
->sym
, temp_vars
[n
], &saved_vars
[n
]);
1366 gfc_conv_expr (se
, sym
->value
);
1368 if (sym
->ts
.type
== BT_CHARACTER
)
1370 gfc_conv_const_charlen (sym
->ts
.cl
);
1372 /* Force the expression to the correct length. */
1373 if (!INTEGER_CST_P (se
->string_length
)
1374 || tree_int_cst_lt (se
->string_length
,
1375 sym
->ts
.cl
->backend_decl
))
1377 type
= gfc_get_character_type (sym
->ts
.kind
, sym
->ts
.cl
);
1378 tmp
= gfc_create_var (type
, sym
->name
);
1379 tmp
= gfc_build_addr_expr (build_pointer_type (type
), tmp
);
1380 gfc_trans_string_copy (&se
->pre
, sym
->ts
.cl
->backend_decl
, tmp
,
1381 se
->string_length
, se
->expr
);
1384 se
->string_length
= sym
->ts
.cl
->backend_decl
;
1387 /* Restore the original variables. */
1388 for (fargs
= sym
->formal
, n
= 0; fargs
; fargs
= fargs
->next
, n
++)
1389 gfc_restore_sym (fargs
->sym
, &saved_vars
[n
]);
1390 gfc_free (saved_vars
);
1394 /* Translate a function expression. */
1397 gfc_conv_function_expr (gfc_se
* se
, gfc_expr
* expr
)
1401 if (expr
->value
.function
.isym
)
1403 gfc_conv_intrinsic_function (se
, expr
);
1407 /* We distinguish statement functions from general functions to improve
1408 runtime performance. */
1409 if (expr
->symtree
->n
.sym
->attr
.proc
== PROC_ST_FUNCTION
)
1411 gfc_conv_statement_function (se
, expr
);
1415 /* expr.value.function.esym is the resolved (specific) function symbol for
1416 most functions. However this isn't set for dummy procedures. */
1417 sym
= expr
->value
.function
.esym
;
1419 sym
= expr
->symtree
->n
.sym
;
1420 gfc_conv_function_call (se
, sym
, expr
->value
.function
.actual
);
1425 gfc_conv_array_constructor_expr (gfc_se
* se
, gfc_expr
* expr
)
1427 gcc_assert (se
->ss
!= NULL
&& se
->ss
!= gfc_ss_terminator
);
1428 gcc_assert (se
->ss
->expr
== expr
&& se
->ss
->type
== GFC_SS_CONSTRUCTOR
);
1430 gfc_conv_tmp_array_ref (se
);
1431 gfc_advance_se_ss_chain (se
);
1435 /* Build a static initializer. EXPR is the expression for the initial value.
1436 The other parameters describe the variable of the component being
1437 initialized. EXPR may be null. */
1440 gfc_conv_initializer (gfc_expr
* expr
, gfc_typespec
* ts
, tree type
,
1441 bool array
, bool pointer
)
1445 if (!(expr
|| pointer
))
1450 /* Arrays need special handling. */
1452 return gfc_build_null_descriptor (type
);
1454 return gfc_conv_array_initializer (type
, expr
);
1457 return fold_convert (type
, null_pointer_node
);
1463 gfc_init_se (&se
, NULL
);
1464 gfc_conv_structure (&se
, expr
, 1);
1468 return gfc_conv_string_init (ts
->cl
->backend_decl
,expr
);
1471 gfc_init_se (&se
, NULL
);
1472 gfc_conv_constant (&se
, expr
);
1479 gfc_trans_subarray_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
)
1491 gfc_start_block (&block
);
1493 /* Initialize the scalarizer. */
1494 gfc_init_loopinfo (&loop
);
1496 gfc_init_se (&lse
, NULL
);
1497 gfc_init_se (&rse
, NULL
);
1500 rss
= gfc_walk_expr (expr
);
1501 if (rss
== gfc_ss_terminator
)
1503 /* The rhs is scalar. Add a ss for the expression. */
1504 rss
= gfc_get_ss ();
1505 rss
->next
= gfc_ss_terminator
;
1506 rss
->type
= GFC_SS_SCALAR
;
1510 /* Create a SS for the destination. */
1511 lss
= gfc_get_ss ();
1512 lss
->type
= GFC_SS_COMPONENT
;
1514 lss
->shape
= gfc_get_shape (cm
->as
->rank
);
1515 lss
->next
= gfc_ss_terminator
;
1516 lss
->data
.info
.dimen
= cm
->as
->rank
;
1517 lss
->data
.info
.descriptor
= dest
;
1518 lss
->data
.info
.data
= gfc_conv_array_data (dest
);
1519 lss
->data
.info
.offset
= gfc_conv_array_offset (dest
);
1520 for (n
= 0; n
< cm
->as
->rank
; n
++)
1522 lss
->data
.info
.dim
[n
] = n
;
1523 lss
->data
.info
.start
[n
] = gfc_conv_array_lbound (dest
, n
);
1524 lss
->data
.info
.stride
[n
] = gfc_index_one_node
;
1526 mpz_init (lss
->shape
[n
]);
1527 mpz_sub (lss
->shape
[n
], cm
->as
->upper
[n
]->value
.integer
,
1528 cm
->as
->lower
[n
]->value
.integer
);
1529 mpz_add_ui (lss
->shape
[n
], lss
->shape
[n
], 1);
1532 /* Associate the SS with the loop. */
1533 gfc_add_ss_to_loop (&loop
, lss
);
1534 gfc_add_ss_to_loop (&loop
, rss
);
1536 /* Calculate the bounds of the scalarization. */
1537 gfc_conv_ss_startstride (&loop
);
1539 /* Setup the scalarizing loops. */
1540 gfc_conv_loop_setup (&loop
);
1542 /* Setup the gfc_se structures. */
1543 gfc_copy_loopinfo_to_se (&lse
, &loop
);
1544 gfc_copy_loopinfo_to_se (&rse
, &loop
);
1547 gfc_mark_ss_chain_used (rss
, 1);
1549 gfc_mark_ss_chain_used (lss
, 1);
1551 /* Start the scalarized loop body. */
1552 gfc_start_scalarized_body (&loop
, &body
);
1554 gfc_conv_tmp_array_ref (&lse
);
1555 gfc_conv_expr (&rse
, expr
);
1557 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, cm
->ts
.type
);
1558 gfc_add_expr_to_block (&body
, tmp
);
1560 gcc_assert (rse
.ss
== gfc_ss_terminator
);
1562 /* Generate the copying loops. */
1563 gfc_trans_scalarizing_loops (&loop
, &body
);
1565 /* Wrap the whole thing up. */
1566 gfc_add_block_to_block (&block
, &loop
.pre
);
1567 gfc_add_block_to_block (&block
, &loop
.post
);
1569 for (n
= 0; n
< cm
->as
->rank
; n
++)
1570 mpz_clear (lss
->shape
[n
]);
1571 gfc_free (lss
->shape
);
1573 gfc_cleanup_loop (&loop
);
1575 return gfc_finish_block (&block
);
1578 /* Assign a single component of a derived type constructor. */
1581 gfc_trans_subcomponent_assign (tree dest
, gfc_component
* cm
, gfc_expr
* expr
)
1588 gfc_start_block (&block
);
1591 gfc_init_se (&se
, NULL
);
1592 /* Pointer component. */
1595 /* Array pointer. */
1596 if (expr
->expr_type
== EXPR_NULL
)
1598 dest
= gfc_conv_descriptor_data (dest
);
1599 tmp
= fold_convert (TREE_TYPE (se
.expr
),
1601 gfc_add_modify_expr (&block
, dest
, tmp
);
1605 rss
= gfc_walk_expr (expr
);
1606 se
.direct_byref
= 1;
1608 gfc_conv_expr_descriptor (&se
, expr
, rss
);
1609 gfc_add_block_to_block (&block
, &se
.pre
);
1610 gfc_add_block_to_block (&block
, &se
.post
);
1615 /* Scalar pointers. */
1616 se
.want_pointer
= 1;
1617 gfc_conv_expr (&se
, expr
);
1618 gfc_add_block_to_block (&block
, &se
.pre
);
1619 gfc_add_modify_expr (&block
, dest
,
1620 fold_convert (TREE_TYPE (dest
), se
.expr
));
1621 gfc_add_block_to_block (&block
, &se
.post
);
1624 else if (cm
->dimension
)
1626 tmp
= gfc_trans_subarray_assign (dest
, cm
, expr
);
1627 gfc_add_expr_to_block (&block
, tmp
);
1629 else if (expr
->ts
.type
== BT_DERIVED
)
1631 /* Nested derived type. */
1632 tmp
= gfc_trans_structure_assign (dest
, expr
);
1633 gfc_add_expr_to_block (&block
, tmp
);
1637 /* Scalar component. */
1640 gfc_init_se (&se
, NULL
);
1641 gfc_init_se (&lse
, NULL
);
1643 gfc_conv_expr (&se
, expr
);
1644 if (cm
->ts
.type
== BT_CHARACTER
)
1645 lse
.string_length
= cm
->ts
.cl
->backend_decl
;
1647 tmp
= gfc_trans_scalar_assign (&lse
, &se
, cm
->ts
.type
);
1648 gfc_add_expr_to_block (&block
, tmp
);
1650 return gfc_finish_block (&block
);
1653 /* Assign a derived type constructor to a variable. */
1656 gfc_trans_structure_assign (tree dest
, gfc_expr
* expr
)
1664 gfc_start_block (&block
);
1665 cm
= expr
->ts
.derived
->components
;
1666 for (c
= expr
->value
.constructor
; c
; c
= c
->next
, cm
= cm
->next
)
1668 /* Skip absent members in default initializers. */
1672 field
= cm
->backend_decl
;
1673 tmp
= build3 (COMPONENT_REF
, TREE_TYPE (field
), dest
, field
, NULL_TREE
);
1674 tmp
= gfc_trans_subcomponent_assign (tmp
, cm
, c
->expr
);
1675 gfc_add_expr_to_block (&block
, tmp
);
1677 return gfc_finish_block (&block
);
1680 /* Build an expression for a constructor. If init is nonzero then
1681 this is part of a static variable initializer. */
1684 gfc_conv_structure (gfc_se
* se
, gfc_expr
* expr
, int init
)
1694 gcc_assert (se
->ss
== NULL
);
1695 gcc_assert (expr
->expr_type
== EXPR_STRUCTURE
);
1696 type
= gfc_typenode_for_spec (&expr
->ts
);
1700 /* Create a temporary variable and fill it in. */
1701 se
->expr
= gfc_create_var (type
, expr
->ts
.derived
->name
);
1702 tmp
= gfc_trans_structure_assign (se
->expr
, expr
);
1703 gfc_add_expr_to_block (&se
->pre
, tmp
);
1707 head
= build1 (CONSTRUCTOR
, type
, NULL_TREE
);
1710 cm
= expr
->ts
.derived
->components
;
1711 for (c
= expr
->value
.constructor
; c
; c
= c
->next
, cm
= cm
->next
)
1713 /* Skip absent members in default initializers. */
1717 val
= gfc_conv_initializer (c
->expr
, &cm
->ts
,
1718 TREE_TYPE (cm
->backend_decl
), cm
->dimension
, cm
->pointer
);
1720 /* Build a TREE_CHAIN to hold it. */
1721 val
= tree_cons (cm
->backend_decl
, val
, NULL_TREE
);
1723 /* Add it to the list. */
1724 if (tail
== NULL_TREE
)
1725 TREE_OPERAND(head
, 0) = tail
= val
;
1728 TREE_CHAIN (tail
) = val
;
1736 /* Translate a substring expression. */
1739 gfc_conv_substring_expr (gfc_se
* se
, gfc_expr
* expr
)
1745 gcc_assert (ref
->type
== REF_SUBSTRING
);
1747 se
->expr
= gfc_build_string_const(expr
->value
.character
.length
,
1748 expr
->value
.character
.string
);
1749 se
->string_length
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se
->expr
)));
1750 TYPE_STRING_FLAG (TREE_TYPE (se
->expr
))=1;
1752 gfc_conv_substring(se
,ref
,expr
->ts
.kind
);
1756 /* Entry point for expression translation. */
1759 gfc_conv_expr (gfc_se
* se
, gfc_expr
* expr
)
1761 if (se
->ss
&& se
->ss
->expr
== expr
1762 && (se
->ss
->type
== GFC_SS_SCALAR
|| se
->ss
->type
== GFC_SS_REFERENCE
))
1764 /* Substitute a scalar expression evaluated outside the scalarization
1766 se
->expr
= se
->ss
->data
.scalar
.expr
;
1767 se
->string_length
= se
->ss
->string_length
;
1768 gfc_advance_se_ss_chain (se
);
1772 switch (expr
->expr_type
)
1775 gfc_conv_expr_op (se
, expr
);
1779 gfc_conv_function_expr (se
, expr
);
1783 gfc_conv_constant (se
, expr
);
1787 gfc_conv_variable (se
, expr
);
1791 se
->expr
= null_pointer_node
;
1794 case EXPR_SUBSTRING
:
1795 gfc_conv_substring_expr (se
, expr
);
1798 case EXPR_STRUCTURE
:
1799 gfc_conv_structure (se
, expr
, 0);
1803 gfc_conv_array_constructor_expr (se
, expr
);
1813 gfc_conv_expr_lhs (gfc_se
* se
, gfc_expr
* expr
)
1815 gfc_conv_expr (se
, expr
);
1816 /* AFAICS all numeric lvalues have empty post chains. If not we need to
1817 figure out a way of rewriting an lvalue so that it has no post chain. */
1818 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
|| !se
->post
.head
);
1822 gfc_conv_expr_val (gfc_se
* se
, gfc_expr
* expr
)
1826 gcc_assert (expr
->ts
.type
!= BT_CHARACTER
);
1827 gfc_conv_expr (se
, expr
);
1830 val
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
1831 gfc_add_modify_expr (&se
->pre
, val
, se
->expr
);
1836 gfc_conv_expr_type (gfc_se
* se
, gfc_expr
* expr
, tree type
)
1838 gfc_conv_expr_val (se
, expr
);
1839 se
->expr
= convert (type
, se
->expr
);
1843 /* Converts an expression so that it can be passed by reference. Scalar
1847 gfc_conv_expr_reference (gfc_se
* se
, gfc_expr
* expr
)
1851 if (se
->ss
&& se
->ss
->expr
== expr
1852 && se
->ss
->type
== GFC_SS_REFERENCE
)
1854 se
->expr
= se
->ss
->data
.scalar
.expr
;
1855 se
->string_length
= se
->ss
->string_length
;
1856 gfc_advance_se_ss_chain (se
);
1860 if (expr
->ts
.type
== BT_CHARACTER
)
1862 gfc_conv_expr (se
, expr
);
1863 gfc_conv_string_parameter (se
);
1867 if (expr
->expr_type
== EXPR_VARIABLE
)
1869 se
->want_pointer
= 1;
1870 gfc_conv_expr (se
, expr
);
1873 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
1874 gfc_add_modify_expr (&se
->pre
, var
, se
->expr
);
1875 gfc_add_block_to_block (&se
->pre
, &se
->post
);
1881 gfc_conv_expr (se
, expr
);
1883 /* Create a temporary var to hold the value. */
1884 if (TREE_CONSTANT (se
->expr
))
1886 var
= build_decl (CONST_DECL
, NULL
, TREE_TYPE (se
->expr
));
1887 DECL_INITIAL (var
) = se
->expr
;
1892 var
= gfc_create_var (TREE_TYPE (se
->expr
), NULL
);
1893 gfc_add_modify_expr (&se
->pre
, var
, se
->expr
);
1895 gfc_add_block_to_block (&se
->pre
, &se
->post
);
1897 /* Take the address of that value. */
1898 se
->expr
= gfc_build_addr_expr (NULL
, var
);
1903 gfc_trans_pointer_assign (gfc_code
* code
)
1905 return gfc_trans_pointer_assignment (code
->expr
, code
->expr2
);
1909 /* Generate code for a pointer assignment. */
1912 gfc_trans_pointer_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
)
1920 gfc_start_block (&block
);
1922 gfc_init_se (&lse
, NULL
);
1924 lss
= gfc_walk_expr (expr1
);
1925 rss
= gfc_walk_expr (expr2
);
1926 if (lss
== gfc_ss_terminator
)
1928 /* Scalar pointers. */
1929 lse
.want_pointer
= 1;
1930 gfc_conv_expr (&lse
, expr1
);
1931 gcc_assert (rss
== gfc_ss_terminator
);
1932 gfc_init_se (&rse
, NULL
);
1933 rse
.want_pointer
= 1;
1934 gfc_conv_expr (&rse
, expr2
);
1935 gfc_add_block_to_block (&block
, &lse
.pre
);
1936 gfc_add_block_to_block (&block
, &rse
.pre
);
1937 gfc_add_modify_expr (&block
, lse
.expr
,
1938 fold_convert (TREE_TYPE (lse
.expr
), rse
.expr
));
1939 gfc_add_block_to_block (&block
, &rse
.post
);
1940 gfc_add_block_to_block (&block
, &lse
.post
);
1944 /* Array pointer. */
1945 gfc_conv_expr_descriptor (&lse
, expr1
, lss
);
1946 /* Implement Nullify. */
1947 if (expr2
->expr_type
== EXPR_NULL
)
1949 lse
.expr
= gfc_conv_descriptor_data (lse
.expr
);
1950 rse
.expr
= fold_convert (TREE_TYPE (lse
.expr
), null_pointer_node
);
1951 gfc_add_modify_expr (&block
, lse
.expr
, rse
.expr
);
1955 lse
.direct_byref
= 1;
1956 gfc_conv_expr_descriptor (&lse
, expr2
, rss
);
1958 gfc_add_block_to_block (&block
, &lse
.pre
);
1959 gfc_add_block_to_block (&block
, &lse
.post
);
1961 return gfc_finish_block (&block
);
1965 /* Makes sure se is suitable for passing as a function string parameter. */
1966 /* TODO: Need to check all callers fo this function. It may be abused. */
1969 gfc_conv_string_parameter (gfc_se
* se
)
1973 if (TREE_CODE (se
->expr
) == STRING_CST
)
1975 se
->expr
= gfc_build_addr_expr (pchar_type_node
, se
->expr
);
1979 type
= TREE_TYPE (se
->expr
);
1980 if (TYPE_STRING_FLAG (type
))
1982 gcc_assert (TREE_CODE (se
->expr
) != INDIRECT_REF
);
1983 se
->expr
= gfc_build_addr_expr (pchar_type_node
, se
->expr
);
1986 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se
->expr
)));
1987 gcc_assert (se
->string_length
1988 && TREE_CODE (TREE_TYPE (se
->string_length
)) == INTEGER_TYPE
);
1992 /* Generate code for assignment of scalar variables. Includes character
1996 gfc_trans_scalar_assign (gfc_se
* lse
, gfc_se
* rse
, bt type
)
2000 gfc_init_block (&block
);
2002 if (type
== BT_CHARACTER
)
2004 gcc_assert (lse
->string_length
!= NULL_TREE
2005 && rse
->string_length
!= NULL_TREE
);
2007 gfc_conv_string_parameter (lse
);
2008 gfc_conv_string_parameter (rse
);
2010 gfc_add_block_to_block (&block
, &lse
->pre
);
2011 gfc_add_block_to_block (&block
, &rse
->pre
);
2013 gfc_trans_string_copy (&block
, lse
->string_length
, lse
->expr
,
2014 rse
->string_length
, rse
->expr
);
2018 gfc_add_block_to_block (&block
, &lse
->pre
);
2019 gfc_add_block_to_block (&block
, &rse
->pre
);
2021 gfc_add_modify_expr (&block
, lse
->expr
,
2022 fold_convert (TREE_TYPE (lse
->expr
), rse
->expr
));
2025 gfc_add_block_to_block (&block
, &lse
->post
);
2026 gfc_add_block_to_block (&block
, &rse
->post
);
2028 return gfc_finish_block (&block
);
2032 /* Try to translate array(:) = func (...), where func is a transformational
2033 array function, without using a temporary. Returns NULL is this isn't the
2037 gfc_trans_arrayfunc_assign (gfc_expr
* expr1
, gfc_expr
* expr2
)
2042 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
2043 if (expr2
->value
.function
.isym
&& !gfc_is_intrinsic_libcall (expr2
))
2046 /* Elemental functions don't need a temporary anyway. */
2047 if (expr2
->symtree
->n
.sym
->attr
.elemental
)
2050 /* Check for a dependency. */
2051 if (gfc_check_fncall_dependency (expr1
, expr2
))
2054 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
2056 gcc_assert (expr2
->value
.function
.isym
2057 || (gfc_return_by_reference (expr2
->value
.function
.esym
)
2058 && expr2
->value
.function
.esym
->result
->attr
.dimension
));
2060 ss
= gfc_walk_expr (expr1
);
2061 gcc_assert (ss
!= gfc_ss_terminator
);
2062 gfc_init_se (&se
, NULL
);
2063 gfc_start_block (&se
.pre
);
2064 se
.want_pointer
= 1;
2066 gfc_conv_array_parameter (&se
, expr1
, ss
, 0);
2068 se
.direct_byref
= 1;
2069 se
.ss
= gfc_walk_expr (expr2
);
2070 gcc_assert (se
.ss
!= gfc_ss_terminator
);
2071 gfc_conv_function_expr (&se
, expr2
);
2072 gfc_add_block_to_block (&se
.pre
, &se
.post
);
2074 return gfc_finish_block (&se
.pre
);
2078 /* Translate an assignment. Most of the code is concerned with
2079 setting up the scalarizer. */
2082 gfc_trans_assignment (gfc_expr
* expr1
, gfc_expr
* expr2
)
2087 gfc_ss
*lss_section
;
2094 /* Special case a single function returning an array. */
2095 if (expr2
->expr_type
== EXPR_FUNCTION
&& expr2
->rank
> 0)
2097 tmp
= gfc_trans_arrayfunc_assign (expr1
, expr2
);
2102 /* Assignment of the form lhs = rhs. */
2103 gfc_start_block (&block
);
2105 gfc_init_se (&lse
, NULL
);
2106 gfc_init_se (&rse
, NULL
);
2109 lss
= gfc_walk_expr (expr1
);
2111 if (lss
!= gfc_ss_terminator
)
2113 /* The assignment needs scalarization. */
2116 /* Find a non-scalar SS from the lhs. */
2117 while (lss_section
!= gfc_ss_terminator
2118 && lss_section
->type
!= GFC_SS_SECTION
)
2119 lss_section
= lss_section
->next
;
2121 gcc_assert (lss_section
!= gfc_ss_terminator
);
2123 /* Initialize the scalarizer. */
2124 gfc_init_loopinfo (&loop
);
2127 rss
= gfc_walk_expr (expr2
);
2128 if (rss
== gfc_ss_terminator
)
2130 /* The rhs is scalar. Add a ss for the expression. */
2131 rss
= gfc_get_ss ();
2132 rss
->next
= gfc_ss_terminator
;
2133 rss
->type
= GFC_SS_SCALAR
;
2136 /* Associate the SS with the loop. */
2137 gfc_add_ss_to_loop (&loop
, lss
);
2138 gfc_add_ss_to_loop (&loop
, rss
);
2140 /* Calculate the bounds of the scalarization. */
2141 gfc_conv_ss_startstride (&loop
);
2142 /* Resolve any data dependencies in the statement. */
2143 gfc_conv_resolve_dependencies (&loop
, lss_section
, rss
);
2144 /* Setup the scalarizing loops. */
2145 gfc_conv_loop_setup (&loop
);
2147 /* Setup the gfc_se structures. */
2148 gfc_copy_loopinfo_to_se (&lse
, &loop
);
2149 gfc_copy_loopinfo_to_se (&rse
, &loop
);
2152 gfc_mark_ss_chain_used (rss
, 1);
2153 if (loop
.temp_ss
== NULL
)
2156 gfc_mark_ss_chain_used (lss
, 1);
2160 lse
.ss
= loop
.temp_ss
;
2161 gfc_mark_ss_chain_used (lss
, 3);
2162 gfc_mark_ss_chain_used (loop
.temp_ss
, 3);
2165 /* Start the scalarized loop body. */
2166 gfc_start_scalarized_body (&loop
, &body
);
2169 gfc_init_block (&body
);
2171 /* Translate the expression. */
2172 gfc_conv_expr (&rse
, expr2
);
2174 if (lss
!= gfc_ss_terminator
&& loop
.temp_ss
!= NULL
)
2176 gfc_conv_tmp_array_ref (&lse
);
2177 gfc_advance_se_ss_chain (&lse
);
2180 gfc_conv_expr (&lse
, expr1
);
2182 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
.type
);
2183 gfc_add_expr_to_block (&body
, tmp
);
2185 if (lss
== gfc_ss_terminator
)
2187 /* Use the scalar assignment as is. */
2188 gfc_add_block_to_block (&block
, &body
);
2192 gcc_assert (lse
.ss
== gfc_ss_terminator
2193 && rse
.ss
== gfc_ss_terminator
);
2195 if (loop
.temp_ss
!= NULL
)
2197 gfc_trans_scalarized_loop_boundary (&loop
, &body
);
2199 /* We need to copy the temporary to the actual lhs. */
2200 gfc_init_se (&lse
, NULL
);
2201 gfc_init_se (&rse
, NULL
);
2202 gfc_copy_loopinfo_to_se (&lse
, &loop
);
2203 gfc_copy_loopinfo_to_se (&rse
, &loop
);
2205 rse
.ss
= loop
.temp_ss
;
2208 gfc_conv_tmp_array_ref (&rse
);
2209 gfc_advance_se_ss_chain (&rse
);
2210 gfc_conv_expr (&lse
, expr1
);
2212 gcc_assert (lse
.ss
== gfc_ss_terminator
2213 && rse
.ss
== gfc_ss_terminator
);
2215 tmp
= gfc_trans_scalar_assign (&lse
, &rse
, expr1
->ts
.type
);
2216 gfc_add_expr_to_block (&body
, tmp
);
2218 /* Generate the copying loops. */
2219 gfc_trans_scalarizing_loops (&loop
, &body
);
2221 /* Wrap the whole thing up. */
2222 gfc_add_block_to_block (&block
, &loop
.pre
);
2223 gfc_add_block_to_block (&block
, &loop
.post
);
2225 gfc_cleanup_loop (&loop
);
2228 return gfc_finish_block (&block
);
2232 gfc_trans_assign (gfc_code
* code
)
2234 return gfc_trans_assignment (code
->expr
, code
->expr2
);