1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Contributed by Paul Brook
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-decl.c -- Handling of backend function and variable decls, etc */
26 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-gimple.h"
40 #include "trans-types.h"
41 #include "trans-array.h"
42 #include "trans-const.h"
43 /* Only for gfc_trans_code. Shouldn't need to include this. */
44 #include "trans-stmt.h"
46 #define MAX_LABEL_VALUE 99999
49 /* Holds the result of the function if no result variable specified. */
51 static GTY(()) tree current_fake_result_decl;
52 static GTY(()) tree parent_fake_result_decl;
54 static GTY(()) tree current_function_return_label;
57 /* Holds the variable DECLs for the current function. */
59 static GTY(()) tree saved_function_decls;
60 static GTY(()) tree saved_parent_function_decls;
63 /* The namespace of the module we're currently generating. Only used while
64 outputting decls for module variables. Do not rely on this being set. */
66 static gfc_namespace *module_namespace;
69 /* List of static constructor functions. */
71 tree gfc_static_ctors;
74 /* Function declarations for builtin library functions. */
76 tree gfor_fndecl_pause_numeric;
77 tree gfor_fndecl_pause_string;
78 tree gfor_fndecl_stop_numeric;
79 tree gfor_fndecl_stop_string;
80 tree gfor_fndecl_select_string;
81 tree gfor_fndecl_runtime_error;
82 tree gfor_fndecl_runtime_error_at;
83 tree gfor_fndecl_os_error;
84 tree gfor_fndecl_generate_error;
85 tree gfor_fndecl_set_fpe;
86 tree gfor_fndecl_set_options;
87 tree gfor_fndecl_set_convert;
88 tree gfor_fndecl_set_record_marker;
89 tree gfor_fndecl_set_max_subrecord_length;
90 tree gfor_fndecl_ctime;
91 tree gfor_fndecl_fdate;
92 tree gfor_fndecl_ttynam;
93 tree gfor_fndecl_in_pack;
94 tree gfor_fndecl_in_unpack;
95 tree gfor_fndecl_associated;
98 /* Math functions. Many other math functions are handled in
101 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
102 tree gfor_fndecl_math_cpowf;
103 tree gfor_fndecl_math_cpow;
104 tree gfor_fndecl_math_cpowl10;
105 tree gfor_fndecl_math_cpowl16;
106 tree gfor_fndecl_math_ishftc4;
107 tree gfor_fndecl_math_ishftc8;
108 tree gfor_fndecl_math_ishftc16;
109 tree gfor_fndecl_math_exponent4;
110 tree gfor_fndecl_math_exponent8;
111 tree gfor_fndecl_math_exponent10;
112 tree gfor_fndecl_math_exponent16;
115 /* String functions. */
117 tree gfor_fndecl_compare_string;
118 tree gfor_fndecl_concat_string;
119 tree gfor_fndecl_string_len_trim;
120 tree gfor_fndecl_string_index;
121 tree gfor_fndecl_string_scan;
122 tree gfor_fndecl_string_verify;
123 tree gfor_fndecl_string_trim;
124 tree gfor_fndecl_string_minmax;
125 tree gfor_fndecl_adjustl;
126 tree gfor_fndecl_adjustr;
129 /* Other misc. runtime library functions. */
131 tree gfor_fndecl_size0;
132 tree gfor_fndecl_size1;
133 tree gfor_fndecl_iargc;
135 /* Intrinsic functions implemented in FORTRAN. */
136 tree gfor_fndecl_si_kind;
137 tree gfor_fndecl_sr_kind;
139 /* BLAS gemm functions. */
140 tree gfor_fndecl_sgemm;
141 tree gfor_fndecl_dgemm;
142 tree gfor_fndecl_cgemm;
143 tree gfor_fndecl_zgemm;
147 gfc_add_decl_to_parent_function (tree decl)
150 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
151 DECL_NONLOCAL (decl) = 1;
152 TREE_CHAIN (decl) = saved_parent_function_decls;
153 saved_parent_function_decls = decl;
157 gfc_add_decl_to_function (tree decl)
160 TREE_USED (decl) = 1;
161 DECL_CONTEXT (decl) = current_function_decl;
162 TREE_CHAIN (decl) = saved_function_decls;
163 saved_function_decls = decl;
167 /* Build a backend label declaration. Set TREE_USED for named labels.
168 The context of the label is always the current_function_decl. All
169 labels are marked artificial. */
172 gfc_build_label_decl (tree label_id)
174 /* 2^32 temporaries should be enough. */
175 static unsigned int tmp_num = 1;
179 if (label_id == NULL_TREE)
181 /* Build an internal label name. */
182 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
183 label_id = get_identifier (label_name);
188 /* Build the LABEL_DECL node. Labels have no type. */
189 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
190 DECL_CONTEXT (label_decl) = current_function_decl;
191 DECL_MODE (label_decl) = VOIDmode;
193 /* We always define the label as used, even if the original source
194 file never references the label. We don't want all kinds of
195 spurious warnings for old-style Fortran code with too many
197 TREE_USED (label_decl) = 1;
199 DECL_ARTIFICIAL (label_decl) = 1;
204 /* Returns the return label for the current function. */
207 gfc_get_return_label (void)
209 char name[GFC_MAX_SYMBOL_LEN + 10];
211 if (current_function_return_label)
212 return current_function_return_label;
214 sprintf (name, "__return_%s",
215 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
217 current_function_return_label =
218 gfc_build_label_decl (get_identifier (name));
220 DECL_ARTIFICIAL (current_function_return_label) = 1;
222 return current_function_return_label;
226 /* Set the backend source location of a decl. */
229 gfc_set_decl_location (tree decl, locus * loc)
231 #ifdef USE_MAPPED_LOCATION
232 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
234 DECL_SOURCE_LINE (decl) = loc->lb->linenum;
235 DECL_SOURCE_FILE (decl) = loc->lb->file->filename;
240 /* Return the backend label declaration for a given label structure,
241 or create it if it doesn't exist yet. */
244 gfc_get_label_decl (gfc_st_label * lp)
246 if (lp->backend_decl)
247 return lp->backend_decl;
250 char label_name[GFC_MAX_SYMBOL_LEN + 1];
253 /* Validate the label declaration from the front end. */
254 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
256 /* Build a mangled name for the label. */
257 sprintf (label_name, "__label_%.6d", lp->value);
259 /* Build the LABEL_DECL node. */
260 label_decl = gfc_build_label_decl (get_identifier (label_name));
262 /* Tell the debugger where the label came from. */
263 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
264 gfc_set_decl_location (label_decl, &lp->where);
266 DECL_ARTIFICIAL (label_decl) = 1;
268 /* Store the label in the label list and return the LABEL_DECL. */
269 lp->backend_decl = label_decl;
275 /* Convert a gfc_symbol to an identifier of the same name. */
278 gfc_sym_identifier (gfc_symbol * sym)
280 return (get_identifier (sym->name));
284 /* Construct mangled name from symbol name. */
287 gfc_sym_mangled_identifier (gfc_symbol * sym)
289 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
291 /* Prevent the mangling of identifiers that have an assigned
292 binding label (mainly those that are bind(c)). */
293 if (sym->attr.is_bind_c == 1
294 && sym->binding_label[0] != '\0')
295 return get_identifier(sym->binding_label);
297 if (sym->module == NULL)
298 return gfc_sym_identifier (sym);
301 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
302 return get_identifier (name);
307 /* Construct mangled function name from symbol name. */
310 gfc_sym_mangled_function_id (gfc_symbol * sym)
313 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
315 /* It may be possible to simply use the binding label if it's
316 provided, and remove the other checks. Then we could use it
317 for other things if we wished. */
318 if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
319 sym->binding_label[0] != '\0')
320 /* use the binding label rather than the mangled name */
321 return get_identifier (sym->binding_label);
323 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
324 || (sym->module != NULL && (sym->attr.external
325 || sym->attr.if_source == IFSRC_IFBODY)))
327 /* Main program is mangled into MAIN__. */
328 if (sym->attr.is_main_program)
329 return get_identifier ("MAIN__");
331 /* Intrinsic procedures are never mangled. */
332 if (sym->attr.proc == PROC_INTRINSIC)
333 return get_identifier (sym->name);
335 if (gfc_option.flag_underscoring)
337 has_underscore = strchr (sym->name, '_') != 0;
338 if (gfc_option.flag_second_underscore && has_underscore)
339 snprintf (name, sizeof name, "%s__", sym->name);
341 snprintf (name, sizeof name, "%s_", sym->name);
342 return get_identifier (name);
345 return get_identifier (sym->name);
349 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
350 return get_identifier (name);
355 /* Returns true if a variable of specified size should go on the stack. */
358 gfc_can_put_var_on_stack (tree size)
360 unsigned HOST_WIDE_INT low;
362 if (!INTEGER_CST_P (size))
365 if (gfc_option.flag_max_stack_var_size < 0)
368 if (TREE_INT_CST_HIGH (size) != 0)
371 low = TREE_INT_CST_LOW (size);
372 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
375 /* TODO: Set a per-function stack size limit. */
381 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
382 an expression involving its corresponding pointer. There are
383 2 cases; one for variable size arrays, and one for everything else,
384 because variable-sized arrays require one fewer level of
388 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
390 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
393 /* Parameters need to be dereferenced. */
394 if (sym->cp_pointer->attr.dummy)
395 ptr_decl = build_fold_indirect_ref (ptr_decl);
397 /* Check to see if we're dealing with a variable-sized array. */
398 if (sym->attr.dimension
399 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
401 /* These decls will be dereferenced later, so we don't dereference
403 value = convert (TREE_TYPE (decl), ptr_decl);
407 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
409 value = build_fold_indirect_ref (ptr_decl);
412 SET_DECL_VALUE_EXPR (decl, value);
413 DECL_HAS_VALUE_EXPR_P (decl) = 1;
414 GFC_DECL_CRAY_POINTEE (decl) = 1;
415 /* This is a fake variable just for debugging purposes. */
416 TREE_ASM_WRITTEN (decl) = 1;
420 /* Finish processing of a declaration without an initial value. */
423 gfc_finish_decl (tree decl)
425 gcc_assert (TREE_CODE (decl) == PARM_DECL
426 || DECL_INITIAL (decl) == NULL_TREE);
428 if (TREE_CODE (decl) != VAR_DECL)
431 if (DECL_SIZE (decl) == NULL_TREE
432 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
433 layout_decl (decl, 0);
435 /* A few consistency checks. */
436 /* A static variable with an incomplete type is an error if it is
437 initialized. Also if it is not file scope. Otherwise, let it
438 through, but if it is not `extern' then it may cause an error
440 /* An automatic variable with an incomplete type is an error. */
442 /* We should know the storage size. */
443 gcc_assert (DECL_SIZE (decl) != NULL_TREE
444 || (TREE_STATIC (decl)
445 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
446 : DECL_EXTERNAL (decl)));
448 /* The storage size should be constant. */
449 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
451 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
455 /* Apply symbol attributes to a variable, and add it to the function scope. */
458 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
461 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
462 This is the equivalent of the TARGET variables.
463 We also need to set this if the variable is passed by reference in a
466 /* Set DECL_VALUE_EXPR for Cray Pointees. */
467 if (sym->attr.cray_pointee)
468 gfc_finish_cray_pointee (decl, sym);
470 if (sym->attr.target)
471 TREE_ADDRESSABLE (decl) = 1;
472 /* If it wasn't used we wouldn't be getting it. */
473 TREE_USED (decl) = 1;
475 /* Chain this decl to the pending declarations. Don't do pushdecl()
476 because this would add them to the current scope rather than the
478 if (current_function_decl != NULL_TREE)
480 if (sym->ns->proc_name->backend_decl == current_function_decl
481 || sym->result == sym)
482 gfc_add_decl_to_function (decl);
484 gfc_add_decl_to_parent_function (decl);
487 if (sym->attr.cray_pointee)
490 if(sym->attr.is_bind_c == 1)
492 /* We need to put variables that are bind(c) into the common
493 segment of the object file, because this is what C would do.
494 gfortran would typically put them in either the BSS or
495 initialized data segments, and only mark them as common if
496 they were part of common blocks. However, if they are not put
497 into common space, then C cannot initialize global fortran
498 variables that it interoperates with and the draft says that
499 either Fortran or C should be able to initialize it (but not
500 both, of course.) (J3/04-007, section 15.3). */
501 TREE_PUBLIC(decl) = 1;
502 DECL_COMMON(decl) = 1;
505 /* If a variable is USE associated, it's always external. */
506 if (sym->attr.use_assoc)
508 DECL_EXTERNAL (decl) = 1;
509 TREE_PUBLIC (decl) = 1;
511 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
513 /* TODO: Don't set sym->module for result or dummy variables. */
514 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
515 /* This is the declaration of a module variable. */
516 TREE_PUBLIC (decl) = 1;
517 TREE_STATIC (decl) = 1;
520 if ((sym->attr.save || sym->attr.data || sym->value)
521 && !sym->attr.use_assoc)
522 TREE_STATIC (decl) = 1;
524 if (sym->attr.volatile_)
526 TREE_THIS_VOLATILE (decl) = 1;
527 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
528 TREE_TYPE (decl) = new;
531 /* Keep variables larger than max-stack-var-size off stack. */
532 if (!sym->ns->proc_name->attr.recursive
533 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
534 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
535 /* Put variable length auto array pointers always into stack. */
536 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
537 || sym->attr.dimension == 0
538 || sym->as->type != AS_EXPLICIT
540 || sym->attr.allocatable)
541 && !DECL_ARTIFICIAL (decl))
542 TREE_STATIC (decl) = 1;
544 /* Handle threadprivate variables. */
545 if (sym->attr.threadprivate
546 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
547 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
551 /* Allocate the lang-specific part of a decl. */
554 gfc_allocate_lang_decl (tree decl)
556 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
557 ggc_alloc_cleared (sizeof (struct lang_decl));
560 /* Remember a symbol to generate initialization/cleanup code at function
564 gfc_defer_symbol_init (gfc_symbol * sym)
570 /* Don't add a symbol twice. */
574 last = head = sym->ns->proc_name;
577 /* Make sure that setup code for dummy variables which are used in the
578 setup of other variables is generated first. */
581 /* Find the first dummy arg seen after us, or the first non-dummy arg.
582 This is a circular list, so don't go past the head. */
584 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
590 /* Insert in between last and p. */
596 /* Create an array index type variable with function scope. */
599 create_index_var (const char * pfx, int nest)
603 decl = gfc_create_var_np (gfc_array_index_type, pfx);
605 gfc_add_decl_to_parent_function (decl);
607 gfc_add_decl_to_function (decl);
612 /* Create variables to hold all the non-constant bits of info for a
613 descriptorless array. Remember these in the lang-specific part of the
617 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
623 type = TREE_TYPE (decl);
625 /* We just use the descriptor, if there is one. */
626 if (GFC_DESCRIPTOR_TYPE_P (type))
629 gcc_assert (GFC_ARRAY_TYPE_P (type));
630 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
631 && !sym->attr.contained;
633 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
635 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
637 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
638 TREE_NO_WARNING (GFC_TYPE_ARRAY_LBOUND (type, dim)) = 1;
640 /* Don't try to use the unknown bound for assumed shape arrays. */
641 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
642 && (sym->as->type != AS_ASSUMED_SIZE
643 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
645 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
646 TREE_NO_WARNING (GFC_TYPE_ARRAY_UBOUND (type, dim)) = 1;
649 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
651 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
652 TREE_NO_WARNING (GFC_TYPE_ARRAY_STRIDE (type, dim)) = 1;
655 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
657 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
659 TREE_NO_WARNING (GFC_TYPE_ARRAY_OFFSET (type)) = 1;
662 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
664 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
667 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
668 && sym->as->type != AS_ASSUMED_SIZE)
670 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
671 TREE_NO_WARNING (GFC_TYPE_ARRAY_SIZE (type)) = 1;
674 if (POINTER_TYPE_P (type))
676 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
677 gcc_assert (TYPE_LANG_SPECIFIC (type)
678 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
679 type = TREE_TYPE (type);
682 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
686 size = build2 (MINUS_EXPR, gfc_array_index_type,
687 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
688 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
690 TYPE_DOMAIN (type) = range;
696 /* For some dummy arguments we don't use the actual argument directly.
697 Instead we create a local decl and use that. This allows us to perform
698 initialization, and construct full type information. */
701 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
711 if (sym->attr.pointer || sym->attr.allocatable)
714 /* Add to list of variables if not a fake result variable. */
715 if (sym->attr.result || sym->attr.dummy)
716 gfc_defer_symbol_init (sym);
718 type = TREE_TYPE (dummy);
719 gcc_assert (TREE_CODE (dummy) == PARM_DECL
720 && POINTER_TYPE_P (type));
722 /* Do we know the element size? */
723 known_size = sym->ts.type != BT_CHARACTER
724 || INTEGER_CST_P (sym->ts.cl->backend_decl);
726 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
728 /* For descriptorless arrays with known element size the actual
729 argument is sufficient. */
730 gcc_assert (GFC_ARRAY_TYPE_P (type));
731 gfc_build_qualified_array (dummy, sym);
735 type = TREE_TYPE (type);
736 if (GFC_DESCRIPTOR_TYPE_P (type))
738 /* Create a descriptorless array pointer. */
741 if (!gfc_option.flag_repack_arrays)
743 if (as->type == AS_ASSUMED_SIZE)
744 packed = PACKED_FULL;
748 if (as->type == AS_EXPLICIT)
750 packed = PACKED_FULL;
751 for (n = 0; n < as->rank; n++)
755 && as->upper[n]->expr_type == EXPR_CONSTANT
756 && as->lower[n]->expr_type == EXPR_CONSTANT))
757 packed = PACKED_PARTIAL;
761 packed = PACKED_PARTIAL;
764 type = gfc_typenode_for_spec (&sym->ts);
765 type = gfc_get_nodesc_array_type (type, sym->as, packed);
769 /* We now have an expression for the element size, so create a fully
770 qualified type. Reset sym->backend decl or this will just return the
772 DECL_ARTIFICIAL (sym->backend_decl) = 1;
773 sym->backend_decl = NULL_TREE;
774 type = gfc_sym_type (sym);
775 packed = PACKED_FULL;
778 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
779 decl = build_decl (VAR_DECL, get_identifier (name), type);
781 DECL_ARTIFICIAL (decl) = 1;
782 TREE_PUBLIC (decl) = 0;
783 TREE_STATIC (decl) = 0;
784 DECL_EXTERNAL (decl) = 0;
786 /* We should never get deferred shape arrays here. We used to because of
788 gcc_assert (sym->as->type != AS_DEFERRED);
790 if (packed == PACKED_PARTIAL)
791 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
792 else if (packed == PACKED_FULL)
793 GFC_DECL_PACKED_ARRAY (decl) = 1;
795 gfc_build_qualified_array (decl, sym);
797 if (DECL_LANG_SPECIFIC (dummy))
798 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
800 gfc_allocate_lang_decl (decl);
802 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
804 if (sym->ns->proc_name->backend_decl == current_function_decl
805 || sym->attr.contained)
806 gfc_add_decl_to_function (decl);
808 gfc_add_decl_to_parent_function (decl);
814 /* Return a constant or a variable to use as a string length. Does not
815 add the decl to the current scope. */
818 gfc_create_string_length (gfc_symbol * sym)
822 gcc_assert (sym->ts.cl);
823 gfc_conv_const_charlen (sym->ts.cl);
825 if (sym->ts.cl->backend_decl == NULL_TREE)
827 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
829 /* Also prefix the mangled name. */
830 strcpy (&name[1], sym->name);
832 length = build_decl (VAR_DECL, get_identifier (name),
833 gfc_charlen_type_node);
834 DECL_ARTIFICIAL (length) = 1;
835 TREE_USED (length) = 1;
836 if (sym->ns->proc_name->tlink != NULL)
837 gfc_defer_symbol_init (sym);
838 sym->ts.cl->backend_decl = length;
841 return sym->ts.cl->backend_decl;
844 /* If a variable is assigned a label, we add another two auxiliary
848 gfc_add_assign_aux_vars (gfc_symbol * sym)
854 gcc_assert (sym->backend_decl);
856 decl = sym->backend_decl;
857 gfc_allocate_lang_decl (decl);
858 GFC_DECL_ASSIGN (decl) = 1;
859 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
860 gfc_charlen_type_node);
861 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
863 gfc_finish_var_decl (length, sym);
864 gfc_finish_var_decl (addr, sym);
865 /* STRING_LENGTH is also used as flag. Less than -1 means that
866 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
867 target label's address. Otherwise, value is the length of a format string
868 and ASSIGN_ADDR is its address. */
869 if (TREE_STATIC (length))
870 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
872 gfc_defer_symbol_init (sym);
874 GFC_DECL_STRING_LEN (decl) = length;
875 GFC_DECL_ASSIGN_ADDR (decl) = addr;
878 /* Return the decl for a gfc_symbol, create it if it doesn't already
882 gfc_get_symbol_decl (gfc_symbol * sym)
885 tree length = NULL_TREE;
888 gcc_assert (sym->attr.referenced
889 || sym->attr.use_assoc
890 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
892 if (sym->ns && sym->ns->proc_name->attr.function)
893 byref = gfc_return_by_reference (sym->ns->proc_name);
897 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
899 /* Return via extra parameter. */
900 if (sym->attr.result && byref
901 && !sym->backend_decl)
904 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
905 /* For entry master function skip over the __entry
907 if (sym->ns->proc_name->attr.entry_master)
908 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
911 /* Dummy variables should already have been created. */
912 gcc_assert (sym->backend_decl);
914 /* Create a character length variable. */
915 if (sym->ts.type == BT_CHARACTER)
917 if (sym->ts.cl->backend_decl == NULL_TREE)
918 length = gfc_create_string_length (sym);
920 length = sym->ts.cl->backend_decl;
921 if (TREE_CODE (length) == VAR_DECL
922 && DECL_CONTEXT (length) == NULL_TREE)
924 /* Add the string length to the same context as the symbol. */
925 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
926 gfc_add_decl_to_function (length);
928 gfc_add_decl_to_parent_function (length);
930 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
931 DECL_CONTEXT (length));
933 gfc_defer_symbol_init (sym);
937 /* Use a copy of the descriptor for dummy arrays. */
938 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
940 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
941 /* Prevent the dummy from being detected as unused if it is copied. */
942 if (sym->backend_decl != NULL && decl != sym->backend_decl)
943 DECL_ARTIFICIAL (sym->backend_decl) = 1;
944 sym->backend_decl = decl;
947 TREE_USED (sym->backend_decl) = 1;
948 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
950 gfc_add_assign_aux_vars (sym);
952 return sym->backend_decl;
955 if (sym->backend_decl)
956 return sym->backend_decl;
958 /* Catch function declarations. Only used for actual parameters. */
959 if (sym->attr.flavor == FL_PROCEDURE)
961 decl = gfc_get_extern_function_decl (sym);
965 if (sym->attr.intrinsic)
966 internal_error ("intrinsic variable which isn't a procedure");
968 /* Create string length decl first so that they can be used in the
970 if (sym->ts.type == BT_CHARACTER)
971 length = gfc_create_string_length (sym);
973 /* Create the decl for the variable. */
974 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
976 gfc_set_decl_location (decl, &sym->declared_at);
978 /* Symbols from modules should have their assembler names mangled.
979 This is done here rather than in gfc_finish_var_decl because it
980 is different for string length variables. */
982 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
984 if (sym->attr.dimension)
986 /* Create variables to hold the non-constant bits of array info. */
987 gfc_build_qualified_array (decl, sym);
989 /* Remember this variable for allocation/cleanup. */
990 gfc_defer_symbol_init (sym);
992 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
993 GFC_DECL_PACKED_ARRAY (decl) = 1;
996 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
997 gfc_defer_symbol_init (sym);
999 gfc_finish_var_decl (decl, sym);
1001 if (sym->ts.type == BT_CHARACTER)
1003 /* Character variables need special handling. */
1004 gfc_allocate_lang_decl (decl);
1006 if (TREE_CODE (length) != INTEGER_CST)
1008 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
1012 /* Also prefix the mangled name for symbols from modules. */
1013 strcpy (&name[1], sym->name);
1016 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
1017 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
1019 gfc_finish_var_decl (length, sym);
1020 gcc_assert (!sym->value);
1023 else if (sym->attr.subref_array_pointer)
1025 /* We need the span for these beasts. */
1026 gfc_allocate_lang_decl (decl);
1029 if (sym->attr.subref_array_pointer)
1032 GFC_DECL_SUBREF_ARRAY_P (decl) = 1;
1033 span = build_decl (VAR_DECL, create_tmp_var_name ("span"),
1034 gfc_array_index_type);
1035 gfc_finish_var_decl (span, sym);
1036 TREE_STATIC (span) = 1;
1037 DECL_INITIAL (span) = build_int_cst (NULL_TREE, 0);
1039 GFC_DECL_SPAN (decl) = span;
1042 sym->backend_decl = decl;
1044 if (sym->attr.assign)
1045 gfc_add_assign_aux_vars (sym);
1047 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
1049 /* Add static initializer. */
1050 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
1051 TREE_TYPE (decl), sym->attr.dimension,
1052 sym->attr.pointer || sym->attr.allocatable);
1059 /* Substitute a temporary variable in place of the real one. */
1062 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1064 save->attr = sym->attr;
1065 save->decl = sym->backend_decl;
1067 gfc_clear_attr (&sym->attr);
1068 sym->attr.referenced = 1;
1069 sym->attr.flavor = FL_VARIABLE;
1071 sym->backend_decl = decl;
1075 /* Restore the original variable. */
1078 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1080 sym->attr = save->attr;
1081 sym->backend_decl = save->decl;
1085 /* Get a basic decl for an external function. */
1088 gfc_get_extern_function_decl (gfc_symbol * sym)
1093 gfc_intrinsic_sym *isym;
1095 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1099 if (sym->backend_decl)
1100 return sym->backend_decl;
1102 /* We should never be creating external decls for alternate entry points.
1103 The procedure may be an alternate entry point, but we don't want/need
1105 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1107 if (sym->attr.intrinsic)
1109 /* Call the resolution function to get the actual name. This is
1110 a nasty hack which relies on the resolution functions only looking
1111 at the first argument. We pass NULL for the second argument
1112 otherwise things like AINT get confused. */
1113 isym = gfc_find_function (sym->name);
1114 gcc_assert (isym->resolve.f0 != NULL);
1116 memset (&e, 0, sizeof (e));
1117 e.expr_type = EXPR_FUNCTION;
1119 memset (&argexpr, 0, sizeof (argexpr));
1120 gcc_assert (isym->formal);
1121 argexpr.ts = isym->formal->ts;
1123 if (isym->formal->next == NULL)
1124 isym->resolve.f1 (&e, &argexpr);
1127 if (isym->formal->next->next == NULL)
1128 isym->resolve.f2 (&e, &argexpr, NULL);
1131 if (isym->formal->next->next->next == NULL)
1132 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1135 /* All specific intrinsics take less than 5 arguments. */
1136 gcc_assert (isym->formal->next->next->next->next == NULL);
1137 isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
1142 if (gfc_option.flag_f2c
1143 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1144 || e.ts.type == BT_COMPLEX))
1146 /* Specific which needs a different implementation if f2c
1147 calling conventions are used. */
1148 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1151 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1153 name = get_identifier (s);
1154 mangled_name = name;
1158 name = gfc_sym_identifier (sym);
1159 mangled_name = gfc_sym_mangled_function_id (sym);
1162 type = gfc_get_function_type (sym);
1163 fndecl = build_decl (FUNCTION_DECL, name, type);
1165 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1166 /* If the return type is a pointer, avoid alias issues by setting
1167 DECL_IS_MALLOC to nonzero. This means that the function should be
1168 treated as if it were a malloc, meaning it returns a pointer that
1170 if (POINTER_TYPE_P (type))
1171 DECL_IS_MALLOC (fndecl) = 1;
1173 /* Set the context of this decl. */
1174 if (0 && sym->ns && sym->ns->proc_name)
1176 /* TODO: Add external decls to the appropriate scope. */
1177 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1181 /* Global declaration, e.g. intrinsic subroutine. */
1182 DECL_CONTEXT (fndecl) = NULL_TREE;
1185 DECL_EXTERNAL (fndecl) = 1;
1187 /* This specifies if a function is globally addressable, i.e. it is
1188 the opposite of declaring static in C. */
1189 TREE_PUBLIC (fndecl) = 1;
1191 /* Set attributes for PURE functions. A call to PURE function in the
1192 Fortran 95 sense is both pure and without side effects in the C
1194 if (sym->attr.pure || sym->attr.elemental)
1196 if (sym->attr.function && !gfc_return_by_reference (sym))
1197 DECL_IS_PURE (fndecl) = 1;
1198 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1199 parameters and don't use alternate returns (is this
1200 allowed?). In that case, calls to them are meaningless, and
1201 can be optimized away. See also in build_function_decl(). */
1202 TREE_SIDE_EFFECTS (fndecl) = 0;
1205 /* Mark non-returning functions. */
1206 if (sym->attr.noreturn)
1207 TREE_THIS_VOLATILE(fndecl) = 1;
1209 sym->backend_decl = fndecl;
1211 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1212 pushdecl_top_level (fndecl);
1218 /* Create a declaration for a procedure. For external functions (in the C
1219 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1220 a master function with alternate entry points. */
1223 build_function_decl (gfc_symbol * sym)
1226 symbol_attribute attr;
1228 gfc_formal_arglist *f;
1230 gcc_assert (!sym->backend_decl);
1231 gcc_assert (!sym->attr.external);
1233 /* Set the line and filename. sym->declared_at seems to point to the
1234 last statement for subroutines, but it'll do for now. */
1235 gfc_set_backend_locus (&sym->declared_at);
1237 /* Allow only one nesting level. Allow public declarations. */
1238 gcc_assert (current_function_decl == NULL_TREE
1239 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1241 type = gfc_get_function_type (sym);
1242 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1244 /* Perform name mangling if this is a top level or module procedure. */
1245 if (current_function_decl == NULL_TREE)
1246 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1248 /* Figure out the return type of the declared function, and build a
1249 RESULT_DECL for it. If this is a subroutine with alternate
1250 returns, build a RESULT_DECL for it. */
1253 result_decl = NULL_TREE;
1254 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1257 if (gfc_return_by_reference (sym))
1258 type = void_type_node;
1261 if (sym->result != sym)
1262 result_decl = gfc_sym_identifier (sym->result);
1264 type = TREE_TYPE (TREE_TYPE (fndecl));
1269 /* Look for alternate return placeholders. */
1270 int has_alternate_returns = 0;
1271 for (f = sym->formal; f; f = f->next)
1275 has_alternate_returns = 1;
1280 if (has_alternate_returns)
1281 type = integer_type_node;
1283 type = void_type_node;
1286 result_decl = build_decl (RESULT_DECL, result_decl, type);
1287 DECL_ARTIFICIAL (result_decl) = 1;
1288 DECL_IGNORED_P (result_decl) = 1;
1289 DECL_CONTEXT (result_decl) = fndecl;
1290 DECL_RESULT (fndecl) = result_decl;
1292 /* Don't call layout_decl for a RESULT_DECL.
1293 layout_decl (result_decl, 0); */
1295 /* If the return type is a pointer, avoid alias issues by setting
1296 DECL_IS_MALLOC to nonzero. This means that the function should be
1297 treated as if it were a malloc, meaning it returns a pointer that
1299 if (POINTER_TYPE_P (type))
1300 DECL_IS_MALLOC (fndecl) = 1;
1302 /* Set up all attributes for the function. */
1303 DECL_CONTEXT (fndecl) = current_function_decl;
1304 DECL_EXTERNAL (fndecl) = 0;
1306 /* This specifies if a function is globally visible, i.e. it is
1307 the opposite of declaring static in C. */
1308 if (DECL_CONTEXT (fndecl) == NULL_TREE
1309 && !sym->attr.entry_master)
1310 TREE_PUBLIC (fndecl) = 1;
1312 /* TREE_STATIC means the function body is defined here. */
1313 TREE_STATIC (fndecl) = 1;
1315 /* Set attributes for PURE functions. A call to a PURE function in the
1316 Fortran 95 sense is both pure and without side effects in the C
1318 if (attr.pure || attr.elemental)
1320 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1321 including an alternate return. In that case it can also be
1322 marked as PURE. See also in gfc_get_extern_function_decl(). */
1323 if (attr.function && !gfc_return_by_reference (sym))
1324 DECL_IS_PURE (fndecl) = 1;
1325 TREE_SIDE_EFFECTS (fndecl) = 0;
1328 /* For -fwhole-program to work well, the main program needs to have the
1329 "externally_visible" attribute. */
1330 if (attr.is_main_program)
1331 DECL_ATTRIBUTES (fndecl)
1332 = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
1334 /* Layout the function declaration and put it in the binding level
1335 of the current function. */
1338 sym->backend_decl = fndecl;
1342 /* Create the DECL_ARGUMENTS for a procedure. */
1345 create_function_arglist (gfc_symbol * sym)
1348 gfc_formal_arglist *f;
1349 tree typelist, hidden_typelist;
1350 tree arglist, hidden_arglist;
1354 fndecl = sym->backend_decl;
1356 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1357 the new FUNCTION_DECL node. */
1358 arglist = NULL_TREE;
1359 hidden_arglist = NULL_TREE;
1360 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1362 if (sym->attr.entry_master)
1364 type = TREE_VALUE (typelist);
1365 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1367 DECL_CONTEXT (parm) = fndecl;
1368 DECL_ARG_TYPE (parm) = type;
1369 TREE_READONLY (parm) = 1;
1370 gfc_finish_decl (parm);
1371 DECL_ARTIFICIAL (parm) = 1;
1373 arglist = chainon (arglist, parm);
1374 typelist = TREE_CHAIN (typelist);
1377 if (gfc_return_by_reference (sym))
1379 tree type = TREE_VALUE (typelist), length = NULL;
1381 if (sym->ts.type == BT_CHARACTER)
1383 /* Length of character result. */
1384 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1385 gcc_assert (len_type == gfc_charlen_type_node);
1387 length = build_decl (PARM_DECL,
1388 get_identifier (".__result"),
1390 if (!sym->ts.cl->length)
1392 sym->ts.cl->backend_decl = length;
1393 TREE_USED (length) = 1;
1395 gcc_assert (TREE_CODE (length) == PARM_DECL);
1396 DECL_CONTEXT (length) = fndecl;
1397 DECL_ARG_TYPE (length) = len_type;
1398 TREE_READONLY (length) = 1;
1399 DECL_ARTIFICIAL (length) = 1;
1400 gfc_finish_decl (length);
1401 if (sym->ts.cl->backend_decl == NULL
1402 || sym->ts.cl->backend_decl == length)
1407 if (sym->ts.cl->backend_decl == NULL)
1409 tree len = build_decl (VAR_DECL,
1410 get_identifier ("..__result"),
1411 gfc_charlen_type_node);
1412 DECL_ARTIFICIAL (len) = 1;
1413 TREE_USED (len) = 1;
1414 sym->ts.cl->backend_decl = len;
1417 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1418 arg = sym->result ? sym->result : sym;
1419 backend_decl = arg->backend_decl;
1420 /* Temporary clear it, so that gfc_sym_type creates complete
1422 arg->backend_decl = NULL;
1423 type = gfc_sym_type (arg);
1424 arg->backend_decl = backend_decl;
1425 type = build_reference_type (type);
1429 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1431 DECL_CONTEXT (parm) = fndecl;
1432 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1433 TREE_READONLY (parm) = 1;
1434 DECL_ARTIFICIAL (parm) = 1;
1435 gfc_finish_decl (parm);
1437 arglist = chainon (arglist, parm);
1438 typelist = TREE_CHAIN (typelist);
1440 if (sym->ts.type == BT_CHARACTER)
1442 gfc_allocate_lang_decl (parm);
1443 arglist = chainon (arglist, length);
1444 typelist = TREE_CHAIN (typelist);
1448 hidden_typelist = typelist;
1449 for (f = sym->formal; f; f = f->next)
1450 if (f->sym != NULL) /* Ignore alternate returns. */
1451 hidden_typelist = TREE_CHAIN (hidden_typelist);
1453 for (f = sym->formal; f; f = f->next)
1455 char name[GFC_MAX_SYMBOL_LEN + 2];
1457 /* Ignore alternate returns. */
1461 type = TREE_VALUE (typelist);
1463 if (f->sym->ts.type == BT_CHARACTER)
1465 tree len_type = TREE_VALUE (hidden_typelist);
1466 tree length = NULL_TREE;
1467 gcc_assert (len_type == gfc_charlen_type_node);
1469 strcpy (&name[1], f->sym->name);
1471 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1473 hidden_arglist = chainon (hidden_arglist, length);
1474 DECL_CONTEXT (length) = fndecl;
1475 DECL_ARTIFICIAL (length) = 1;
1476 DECL_ARG_TYPE (length) = len_type;
1477 TREE_READONLY (length) = 1;
1478 gfc_finish_decl (length);
1480 /* TODO: Check string lengths when -fbounds-check. */
1482 /* Use the passed value for assumed length variables. */
1483 if (!f->sym->ts.cl->length)
1485 TREE_USED (length) = 1;
1486 gcc_assert (!f->sym->ts.cl->backend_decl);
1487 f->sym->ts.cl->backend_decl = length;
1490 hidden_typelist = TREE_CHAIN (hidden_typelist);
1492 if (f->sym->ts.cl->backend_decl == NULL
1493 || f->sym->ts.cl->backend_decl == length)
1495 if (f->sym->ts.cl->backend_decl == NULL)
1496 gfc_create_string_length (f->sym);
1498 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1499 if (f->sym->attr.flavor == FL_PROCEDURE)
1500 type = build_pointer_type (gfc_get_function_type (f->sym));
1502 type = gfc_sym_type (f->sym);
1506 /* For non-constant length array arguments, make sure they use
1507 a different type node from TYPE_ARG_TYPES type. */
1508 if (f->sym->attr.dimension
1509 && type == TREE_VALUE (typelist)
1510 && TREE_CODE (type) == POINTER_TYPE
1511 && GFC_ARRAY_TYPE_P (type)
1512 && f->sym->as->type != AS_ASSUMED_SIZE
1513 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1515 if (f->sym->attr.flavor == FL_PROCEDURE)
1516 type = build_pointer_type (gfc_get_function_type (f->sym));
1518 type = gfc_sym_type (f->sym);
1521 /* Build a the argument declaration. */
1522 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1524 /* Fill in arg stuff. */
1525 DECL_CONTEXT (parm) = fndecl;
1526 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1527 /* All implementation args are read-only. */
1528 TREE_READONLY (parm) = 1;
1530 gfc_finish_decl (parm);
1532 f->sym->backend_decl = parm;
1534 arglist = chainon (arglist, parm);
1535 typelist = TREE_CHAIN (typelist);
1538 /* Add the hidden string length parameters, unless the procedure
1540 if (!sym->attr.is_bind_c)
1541 arglist = chainon (arglist, hidden_arglist);
1543 gcc_assert (hidden_typelist == NULL_TREE
1544 || TREE_VALUE (hidden_typelist) == void_type_node);
1545 DECL_ARGUMENTS (fndecl) = arglist;
1548 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1551 gfc_gimplify_function (tree fndecl)
1553 struct cgraph_node *cgn;
1555 gimplify_function_tree (fndecl);
1556 dump_function (TDI_generic, fndecl);
1558 /* Generate errors for structured block violations. */
1559 /* ??? Could be done as part of resolve_labels. */
1561 diagnose_omp_structured_block_errors (fndecl);
1563 /* Convert all nested functions to GIMPLE now. We do things in this order
1564 so that items like VLA sizes are expanded properly in the context of the
1565 correct function. */
1566 cgn = cgraph_node (fndecl);
1567 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1568 gfc_gimplify_function (cgn->decl);
1572 /* Do the setup necessary before generating the body of a function. */
1575 trans_function_start (gfc_symbol * sym)
1579 fndecl = sym->backend_decl;
1581 /* Let GCC know the current scope is this function. */
1582 current_function_decl = fndecl;
1584 /* Let the world know what we're about to do. */
1585 announce_function (fndecl);
1587 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1589 /* Create RTL for function declaration. */
1590 rest_of_decl_compilation (fndecl, 1, 0);
1593 /* Create RTL for function definition. */
1594 make_decl_rtl (fndecl);
1596 init_function_start (fndecl);
1598 /* Even though we're inside a function body, we still don't want to
1599 call expand_expr to calculate the size of a variable-sized array.
1600 We haven't necessarily assigned RTL to all variables yet, so it's
1601 not safe to try to expand expressions involving them. */
1602 cfun->x_dont_save_pending_sizes_p = 1;
1604 /* function.c requires a push at the start of the function. */
1608 /* Create thunks for alternate entry points. */
1611 build_entry_thunks (gfc_namespace * ns)
1613 gfc_formal_arglist *formal;
1614 gfc_formal_arglist *thunk_formal;
1616 gfc_symbol *thunk_sym;
1624 /* This should always be a toplevel function. */
1625 gcc_assert (current_function_decl == NULL_TREE);
1627 gfc_get_backend_locus (&old_loc);
1628 for (el = ns->entries; el; el = el->next)
1630 thunk_sym = el->sym;
1632 build_function_decl (thunk_sym);
1633 create_function_arglist (thunk_sym);
1635 trans_function_start (thunk_sym);
1637 thunk_fndecl = thunk_sym->backend_decl;
1639 gfc_start_block (&body);
1641 /* Pass extra parameter identifying this entry point. */
1642 tmp = build_int_cst (gfc_array_index_type, el->id);
1643 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1644 string_args = NULL_TREE;
1646 if (thunk_sym->attr.function)
1648 if (gfc_return_by_reference (ns->proc_name))
1650 tree ref = DECL_ARGUMENTS (current_function_decl);
1651 args = tree_cons (NULL_TREE, ref, args);
1652 if (ns->proc_name->ts.type == BT_CHARACTER)
1653 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1658 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1660 /* Ignore alternate returns. */
1661 if (formal->sym == NULL)
1664 /* We don't have a clever way of identifying arguments, so resort to
1665 a brute-force search. */
1666 for (thunk_formal = thunk_sym->formal;
1668 thunk_formal = thunk_formal->next)
1670 if (thunk_formal->sym == formal->sym)
1676 /* Pass the argument. */
1677 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1678 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1680 if (formal->sym->ts.type == BT_CHARACTER)
1682 tmp = thunk_formal->sym->ts.cl->backend_decl;
1683 string_args = tree_cons (NULL_TREE, tmp, string_args);
1688 /* Pass NULL for a missing argument. */
1689 args = tree_cons (NULL_TREE, null_pointer_node, args);
1690 if (formal->sym->ts.type == BT_CHARACTER)
1692 tmp = build_int_cst (gfc_charlen_type_node, 0);
1693 string_args = tree_cons (NULL_TREE, tmp, string_args);
1698 /* Call the master function. */
1699 args = nreverse (args);
1700 args = chainon (args, nreverse (string_args));
1701 tmp = ns->proc_name->backend_decl;
1702 tmp = build_function_call_expr (tmp, args);
1703 if (ns->proc_name->attr.mixed_entry_master)
1705 tree union_decl, field;
1706 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1708 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1709 TREE_TYPE (master_type));
1710 DECL_ARTIFICIAL (union_decl) = 1;
1711 DECL_EXTERNAL (union_decl) = 0;
1712 TREE_PUBLIC (union_decl) = 0;
1713 TREE_USED (union_decl) = 1;
1714 layout_decl (union_decl, 0);
1715 pushdecl (union_decl);
1717 DECL_CONTEXT (union_decl) = current_function_decl;
1718 tmp = build2 (MODIFY_EXPR,
1719 TREE_TYPE (union_decl),
1721 gfc_add_expr_to_block (&body, tmp);
1723 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1724 field; field = TREE_CHAIN (field))
1725 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1726 thunk_sym->result->name) == 0)
1728 gcc_assert (field != NULL_TREE);
1729 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), union_decl, field,
1731 tmp = build2 (MODIFY_EXPR,
1732 TREE_TYPE (DECL_RESULT (current_function_decl)),
1733 DECL_RESULT (current_function_decl), tmp);
1734 tmp = build1_v (RETURN_EXPR, tmp);
1736 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1739 tmp = build2 (MODIFY_EXPR,
1740 TREE_TYPE (DECL_RESULT (current_function_decl)),
1741 DECL_RESULT (current_function_decl), tmp);
1742 tmp = build1_v (RETURN_EXPR, tmp);
1744 gfc_add_expr_to_block (&body, tmp);
1746 /* Finish off this function and send it for code generation. */
1747 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1749 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1751 /* Output the GENERIC tree. */
1752 dump_function (TDI_original, thunk_fndecl);
1754 /* Store the end of the function, so that we get good line number
1755 info for the epilogue. */
1756 cfun->function_end_locus = input_location;
1758 /* We're leaving the context of this function, so zap cfun.
1759 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1760 tree_rest_of_compilation. */
1763 current_function_decl = NULL_TREE;
1765 gfc_gimplify_function (thunk_fndecl);
1766 cgraph_finalize_function (thunk_fndecl, false);
1768 /* We share the symbols in the formal argument list with other entry
1769 points and the master function. Clear them so that they are
1770 recreated for each function. */
1771 for (formal = thunk_sym->formal; formal; formal = formal->next)
1772 if (formal->sym != NULL) /* Ignore alternate returns. */
1774 formal->sym->backend_decl = NULL_TREE;
1775 if (formal->sym->ts.type == BT_CHARACTER)
1776 formal->sym->ts.cl->backend_decl = NULL_TREE;
1779 if (thunk_sym->attr.function)
1781 if (thunk_sym->ts.type == BT_CHARACTER)
1782 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1783 if (thunk_sym->result->ts.type == BT_CHARACTER)
1784 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1788 gfc_set_backend_locus (&old_loc);
1792 /* Create a decl for a function, and create any thunks for alternate entry
1796 gfc_create_function_decl (gfc_namespace * ns)
1798 /* Create a declaration for the master function. */
1799 build_function_decl (ns->proc_name);
1801 /* Compile the entry thunks. */
1803 build_entry_thunks (ns);
1805 /* Now create the read argument list. */
1806 create_function_arglist (ns->proc_name);
1809 /* Return the decl used to hold the function return value. If
1810 parent_flag is set, the context is the parent_scope. */
1813 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
1817 tree this_fake_result_decl;
1818 tree this_function_decl;
1820 char name[GFC_MAX_SYMBOL_LEN + 10];
1824 this_fake_result_decl = parent_fake_result_decl;
1825 this_function_decl = DECL_CONTEXT (current_function_decl);
1829 this_fake_result_decl = current_fake_result_decl;
1830 this_function_decl = current_function_decl;
1834 && sym->ns->proc_name->backend_decl == this_function_decl
1835 && sym->ns->proc_name->attr.entry_master
1836 && sym != sym->ns->proc_name)
1839 if (this_fake_result_decl != NULL)
1840 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
1841 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1844 return TREE_VALUE (t);
1845 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1848 this_fake_result_decl = parent_fake_result_decl;
1850 this_fake_result_decl = current_fake_result_decl;
1852 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1856 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1857 field; field = TREE_CHAIN (field))
1858 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1862 gcc_assert (field != NULL_TREE);
1863 decl = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field,
1867 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1869 gfc_add_decl_to_parent_function (var);
1871 gfc_add_decl_to_function (var);
1873 SET_DECL_VALUE_EXPR (var, decl);
1874 DECL_HAS_VALUE_EXPR_P (var) = 1;
1875 GFC_DECL_RESULT (var) = 1;
1877 TREE_CHAIN (this_fake_result_decl)
1878 = tree_cons (get_identifier (sym->name), var,
1879 TREE_CHAIN (this_fake_result_decl));
1883 if (this_fake_result_decl != NULL_TREE)
1884 return TREE_VALUE (this_fake_result_decl);
1886 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1891 if (sym->ts.type == BT_CHARACTER)
1893 if (sym->ts.cl->backend_decl == NULL_TREE)
1894 length = gfc_create_string_length (sym);
1896 length = sym->ts.cl->backend_decl;
1897 if (TREE_CODE (length) == VAR_DECL
1898 && DECL_CONTEXT (length) == NULL_TREE)
1899 gfc_add_decl_to_function (length);
1902 if (gfc_return_by_reference (sym))
1904 decl = DECL_ARGUMENTS (this_function_decl);
1906 if (sym->ns->proc_name->backend_decl == this_function_decl
1907 && sym->ns->proc_name->attr.entry_master)
1908 decl = TREE_CHAIN (decl);
1910 TREE_USED (decl) = 1;
1912 decl = gfc_build_dummy_array_decl (sym, decl);
1916 sprintf (name, "__result_%.20s",
1917 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
1919 if (!sym->attr.mixed_entry_master && sym->attr.function)
1920 decl = build_decl (VAR_DECL, get_identifier (name),
1921 gfc_sym_type (sym));
1923 decl = build_decl (VAR_DECL, get_identifier (name),
1924 TREE_TYPE (TREE_TYPE (this_function_decl)));
1925 DECL_ARTIFICIAL (decl) = 1;
1926 DECL_EXTERNAL (decl) = 0;
1927 TREE_PUBLIC (decl) = 0;
1928 TREE_USED (decl) = 1;
1929 GFC_DECL_RESULT (decl) = 1;
1930 TREE_ADDRESSABLE (decl) = 1;
1932 layout_decl (decl, 0);
1935 gfc_add_decl_to_parent_function (decl);
1937 gfc_add_decl_to_function (decl);
1941 parent_fake_result_decl = build_tree_list (NULL, decl);
1943 current_fake_result_decl = build_tree_list (NULL, decl);
1949 /* Builds a function decl. The remaining parameters are the types of the
1950 function arguments. Negative nargs indicates a varargs function. */
1953 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1962 /* Library functions must be declared with global scope. */
1963 gcc_assert (current_function_decl == NULL_TREE);
1965 va_start (p, nargs);
1968 /* Create a list of the argument types. */
1969 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1971 argtype = va_arg (p, tree);
1972 arglist = gfc_chainon_list (arglist, argtype);
1977 /* Terminate the list. */
1978 arglist = gfc_chainon_list (arglist, void_type_node);
1981 /* Build the function type and decl. */
1982 fntype = build_function_type (rettype, arglist);
1983 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1985 /* Mark this decl as external. */
1986 DECL_EXTERNAL (fndecl) = 1;
1987 TREE_PUBLIC (fndecl) = 1;
1993 rest_of_decl_compilation (fndecl, 1, 0);
1999 gfc_build_intrinsic_function_decls (void)
2001 tree gfc_int4_type_node = gfc_get_int_type (4);
2002 tree gfc_int8_type_node = gfc_get_int_type (8);
2003 tree gfc_int16_type_node = gfc_get_int_type (16);
2004 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2005 tree gfc_real4_type_node = gfc_get_real_type (4);
2006 tree gfc_real8_type_node = gfc_get_real_type (8);
2007 tree gfc_real10_type_node = gfc_get_real_type (10);
2008 tree gfc_real16_type_node = gfc_get_real_type (16);
2009 tree gfc_complex4_type_node = gfc_get_complex_type (4);
2010 tree gfc_complex8_type_node = gfc_get_complex_type (8);
2011 tree gfc_complex10_type_node = gfc_get_complex_type (10);
2012 tree gfc_complex16_type_node = gfc_get_complex_type (16);
2014 /* String functions. */
2015 gfor_fndecl_compare_string =
2016 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
2017 integer_type_node, 4,
2018 gfc_charlen_type_node, pchar_type_node,
2019 gfc_charlen_type_node, pchar_type_node);
2021 gfor_fndecl_concat_string =
2022 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
2025 gfc_charlen_type_node, pchar_type_node,
2026 gfc_charlen_type_node, pchar_type_node,
2027 gfc_charlen_type_node, pchar_type_node);
2029 gfor_fndecl_string_len_trim =
2030 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
2032 2, gfc_charlen_type_node,
2035 gfor_fndecl_string_index =
2036 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
2038 5, gfc_charlen_type_node, pchar_type_node,
2039 gfc_charlen_type_node, pchar_type_node,
2040 gfc_logical4_type_node);
2042 gfor_fndecl_string_scan =
2043 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
2045 5, gfc_charlen_type_node, pchar_type_node,
2046 gfc_charlen_type_node, pchar_type_node,
2047 gfc_logical4_type_node);
2049 gfor_fndecl_string_verify =
2050 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
2052 5, gfc_charlen_type_node, pchar_type_node,
2053 gfc_charlen_type_node, pchar_type_node,
2054 gfc_logical4_type_node);
2056 gfor_fndecl_string_trim =
2057 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2060 build_pointer_type (gfc_charlen_type_node),
2062 gfc_charlen_type_node,
2065 gfor_fndecl_string_minmax =
2066 gfc_build_library_function_decl (get_identifier (PREFIX("string_minmax")),
2068 build_pointer_type (gfc_charlen_type_node),
2069 ppvoid_type_node, integer_type_node,
2072 gfor_fndecl_ttynam =
2073 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2077 gfc_charlen_type_node,
2081 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2085 gfc_charlen_type_node);
2088 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2092 gfc_charlen_type_node,
2093 gfc_int8_type_node);
2095 gfor_fndecl_adjustl =
2096 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2100 gfc_charlen_type_node, pchar_type_node);
2102 gfor_fndecl_adjustr =
2103 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2107 gfc_charlen_type_node, pchar_type_node);
2109 gfor_fndecl_si_kind =
2110 gfc_build_library_function_decl (get_identifier
2111 (PREFIX("selected_int_kind")),
2116 gfor_fndecl_sr_kind =
2117 gfc_build_library_function_decl (get_identifier
2118 (PREFIX("selected_real_kind")),
2123 /* Power functions. */
2125 tree ctype, rtype, itype, jtype;
2126 int rkind, ikind, jkind;
2129 static int ikinds[NIKINDS] = {4, 8, 16};
2130 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2131 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2133 for (ikind=0; ikind < NIKINDS; ikind++)
2135 itype = gfc_get_int_type (ikinds[ikind]);
2137 for (jkind=0; jkind < NIKINDS; jkind++)
2139 jtype = gfc_get_int_type (ikinds[jkind]);
2142 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2144 gfor_fndecl_math_powi[jkind][ikind].integer =
2145 gfc_build_library_function_decl (get_identifier (name),
2146 jtype, 2, jtype, itype);
2147 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2151 for (rkind = 0; rkind < NRKINDS; rkind ++)
2153 rtype = gfc_get_real_type (rkinds[rkind]);
2156 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2158 gfor_fndecl_math_powi[rkind][ikind].real =
2159 gfc_build_library_function_decl (get_identifier (name),
2160 rtype, 2, rtype, itype);
2161 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2164 ctype = gfc_get_complex_type (rkinds[rkind]);
2167 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2169 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2170 gfc_build_library_function_decl (get_identifier (name),
2171 ctype, 2,ctype, itype);
2172 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2180 gfor_fndecl_math_cpowf =
2181 gfc_build_library_function_decl (get_identifier ("cpowf"),
2182 gfc_complex4_type_node,
2183 1, gfc_complex4_type_node);
2184 gfor_fndecl_math_cpow =
2185 gfc_build_library_function_decl (get_identifier ("cpow"),
2186 gfc_complex8_type_node,
2187 1, gfc_complex8_type_node);
2188 if (gfc_complex10_type_node)
2189 gfor_fndecl_math_cpowl10 =
2190 gfc_build_library_function_decl (get_identifier ("cpowl"),
2191 gfc_complex10_type_node, 1,
2192 gfc_complex10_type_node);
2193 if (gfc_complex16_type_node)
2194 gfor_fndecl_math_cpowl16 =
2195 gfc_build_library_function_decl (get_identifier ("cpowl"),
2196 gfc_complex16_type_node, 1,
2197 gfc_complex16_type_node);
2199 gfor_fndecl_math_ishftc4 =
2200 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2202 3, gfc_int4_type_node,
2203 gfc_int4_type_node, gfc_int4_type_node);
2204 gfor_fndecl_math_ishftc8 =
2205 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2207 3, gfc_int8_type_node,
2208 gfc_int4_type_node, gfc_int4_type_node);
2209 if (gfc_int16_type_node)
2210 gfor_fndecl_math_ishftc16 =
2211 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2212 gfc_int16_type_node, 3,
2213 gfc_int16_type_node,
2215 gfc_int4_type_node);
2217 gfor_fndecl_math_exponent4 =
2218 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
2220 1, gfc_real4_type_node);
2221 gfor_fndecl_math_exponent8 =
2222 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
2224 1, gfc_real8_type_node);
2225 if (gfc_real10_type_node)
2226 gfor_fndecl_math_exponent10 =
2227 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r10")),
2228 gfc_int4_type_node, 1,
2229 gfc_real10_type_node);
2230 if (gfc_real16_type_node)
2231 gfor_fndecl_math_exponent16 =
2232 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r16")),
2233 gfc_int4_type_node, 1,
2234 gfc_real16_type_node);
2236 /* BLAS functions. */
2238 tree pint = build_pointer_type (integer_type_node);
2239 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2240 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2241 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2242 tree pz = build_pointer_type
2243 (gfc_get_complex_type (gfc_default_double_kind));
2245 gfor_fndecl_sgemm = gfc_build_library_function_decl
2247 (gfc_option.flag_underscoring ? "sgemm_"
2249 void_type_node, 15, pchar_type_node,
2250 pchar_type_node, pint, pint, pint, ps, ps, pint,
2251 ps, pint, ps, ps, pint, integer_type_node,
2253 gfor_fndecl_dgemm = gfc_build_library_function_decl
2255 (gfc_option.flag_underscoring ? "dgemm_"
2257 void_type_node, 15, pchar_type_node,
2258 pchar_type_node, pint, pint, pint, pd, pd, pint,
2259 pd, pint, pd, pd, pint, integer_type_node,
2261 gfor_fndecl_cgemm = gfc_build_library_function_decl
2263 (gfc_option.flag_underscoring ? "cgemm_"
2265 void_type_node, 15, pchar_type_node,
2266 pchar_type_node, pint, pint, pint, pc, pc, pint,
2267 pc, pint, pc, pc, pint, integer_type_node,
2269 gfor_fndecl_zgemm = gfc_build_library_function_decl
2271 (gfc_option.flag_underscoring ? "zgemm_"
2273 void_type_node, 15, pchar_type_node,
2274 pchar_type_node, pint, pint, pint, pz, pz, pint,
2275 pz, pint, pz, pz, pint, integer_type_node,
2279 /* Other functions. */
2281 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2282 gfc_array_index_type,
2283 1, pvoid_type_node);
2285 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2286 gfc_array_index_type,
2288 gfc_array_index_type);
2291 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2297 /* Make prototypes for runtime library functions. */
2300 gfc_build_builtin_function_decls (void)
2302 tree gfc_int4_type_node = gfc_get_int_type (4);
2304 gfor_fndecl_stop_numeric =
2305 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2306 void_type_node, 1, gfc_int4_type_node);
2307 /* Stop doesn't return. */
2308 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2310 gfor_fndecl_stop_string =
2311 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2312 void_type_node, 2, pchar_type_node,
2313 gfc_int4_type_node);
2314 /* Stop doesn't return. */
2315 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2317 gfor_fndecl_pause_numeric =
2318 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2319 void_type_node, 1, gfc_int4_type_node);
2321 gfor_fndecl_pause_string =
2322 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2323 void_type_node, 2, pchar_type_node,
2324 gfc_int4_type_node);
2326 gfor_fndecl_select_string =
2327 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2328 integer_type_node, 0);
2330 gfor_fndecl_runtime_error =
2331 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2332 void_type_node, -1, pchar_type_node);
2333 /* The runtime_error function does not return. */
2334 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2336 gfor_fndecl_runtime_error_at =
2337 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2338 void_type_node, -2, pchar_type_node,
2340 /* The runtime_error_at function does not return. */
2341 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2343 gfor_fndecl_generate_error =
2344 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2345 void_type_node, 3, pvoid_type_node,
2346 integer_type_node, pchar_type_node);
2348 gfor_fndecl_os_error =
2349 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2350 void_type_node, 1, pchar_type_node);
2351 /* The runtime_error function does not return. */
2352 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2354 gfor_fndecl_set_fpe =
2355 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2356 void_type_node, 1, integer_type_node);
2358 /* Keep the array dimension in sync with the call, later in this file. */
2359 gfor_fndecl_set_options =
2360 gfc_build_library_function_decl (get_identifier (PREFIX("set_options")),
2361 void_type_node, 2, integer_type_node,
2364 gfor_fndecl_set_convert =
2365 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2366 void_type_node, 1, integer_type_node);
2368 gfor_fndecl_set_record_marker =
2369 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2370 void_type_node, 1, integer_type_node);
2372 gfor_fndecl_set_max_subrecord_length =
2373 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2374 void_type_node, 1, integer_type_node);
2376 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2377 get_identifier (PREFIX("internal_pack")),
2378 pvoid_type_node, 1, pvoid_type_node);
2380 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2381 get_identifier (PREFIX("internal_unpack")),
2382 pvoid_type_node, 1, pvoid_type_node);
2384 gfor_fndecl_associated =
2385 gfc_build_library_function_decl (
2386 get_identifier (PREFIX("associated")),
2387 integer_type_node, 2, ppvoid_type_node,
2390 gfc_build_intrinsic_function_decls ();
2391 gfc_build_intrinsic_lib_fndecls ();
2392 gfc_build_io_library_fndecls ();
2396 /* Evaluate the length of dummy character variables. */
2399 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2403 gfc_finish_decl (cl->backend_decl);
2405 gfc_start_block (&body);
2407 /* Evaluate the string length expression. */
2408 gfc_conv_string_length (cl, &body);
2410 gfc_trans_vla_type_sizes (sym, &body);
2412 gfc_add_expr_to_block (&body, fnbody);
2413 return gfc_finish_block (&body);
2417 /* Allocate and cleanup an automatic character variable. */
2420 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2426 gcc_assert (sym->backend_decl);
2427 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2429 gfc_start_block (&body);
2431 /* Evaluate the string length expression. */
2432 gfc_conv_string_length (sym->ts.cl, &body);
2434 gfc_trans_vla_type_sizes (sym, &body);
2436 decl = sym->backend_decl;
2438 /* Emit a DECL_EXPR for this variable, which will cause the
2439 gimplifier to allocate storage, and all that good stuff. */
2440 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2441 gfc_add_expr_to_block (&body, tmp);
2443 gfc_add_expr_to_block (&body, fnbody);
2444 return gfc_finish_block (&body);
2447 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2450 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2454 gcc_assert (sym->backend_decl);
2455 gfc_start_block (&body);
2457 /* Set the initial value to length. See the comments in
2458 function gfc_add_assign_aux_vars in this file. */
2459 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2460 build_int_cst (NULL_TREE, -2));
2462 gfc_add_expr_to_block (&body, fnbody);
2463 return gfc_finish_block (&body);
2467 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2469 tree t = *tp, var, val;
2471 if (t == NULL || t == error_mark_node)
2473 if (TREE_CONSTANT (t) || DECL_P (t))
2476 if (TREE_CODE (t) == SAVE_EXPR)
2478 if (SAVE_EXPR_RESOLVED_P (t))
2480 *tp = TREE_OPERAND (t, 0);
2483 val = TREE_OPERAND (t, 0);
2488 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2489 gfc_add_decl_to_function (var);
2490 gfc_add_modify_expr (body, var, val);
2491 if (TREE_CODE (t) == SAVE_EXPR)
2492 TREE_OPERAND (t, 0) = var;
2497 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2501 if (type == NULL || type == error_mark_node)
2504 type = TYPE_MAIN_VARIANT (type);
2506 if (TREE_CODE (type) == INTEGER_TYPE)
2508 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2509 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2511 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2513 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2514 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2517 else if (TREE_CODE (type) == ARRAY_TYPE)
2519 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2520 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2521 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2522 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2524 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2526 TYPE_SIZE (t) = TYPE_SIZE (type);
2527 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2532 /* Make sure all type sizes and array domains are either constant,
2533 or variable or parameter decls. This is a simplified variant
2534 of gimplify_type_sizes, but we can't use it here, as none of the
2535 variables in the expressions have been gimplified yet.
2536 As type sizes and domains for various variable length arrays
2537 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2538 time, without this routine gimplify_type_sizes in the middle-end
2539 could result in the type sizes being gimplified earlier than where
2540 those variables are initialized. */
2543 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2545 tree type = TREE_TYPE (sym->backend_decl);
2547 if (TREE_CODE (type) == FUNCTION_TYPE
2548 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2550 if (! current_fake_result_decl)
2553 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2556 while (POINTER_TYPE_P (type))
2557 type = TREE_TYPE (type);
2559 if (GFC_DESCRIPTOR_TYPE_P (type))
2561 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2563 while (POINTER_TYPE_P (etype))
2564 etype = TREE_TYPE (etype);
2566 gfc_trans_vla_type_sizes_1 (etype, body);
2569 gfc_trans_vla_type_sizes_1 (type, body);
2573 /* Initialize INTENT(OUT) derived type dummies. */
2575 init_intent_out_dt (gfc_symbol * proc_sym, tree body)
2577 stmtblock_t fnblock;
2578 gfc_formal_arglist *f;
2583 gfc_init_block (&fnblock);
2585 for (f = proc_sym->formal; f; f = f->next)
2587 if (f->sym && f->sym->attr.intent == INTENT_OUT
2588 && f->sym->ts.type == BT_DERIVED
2589 && !f->sym->ts.derived->attr.alloc_comp
2592 gcc_assert (!f->sym->attr.allocatable);
2593 gfc_set_sym_referenced (f->sym);
2594 tmpe = gfc_lval_expr_from_sym (f->sym);
2595 tmp = gfc_trans_assignment (tmpe, f->sym->value, false);
2597 present = gfc_conv_expr_present (f->sym);
2598 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present,
2599 tmp, build_empty_stmt ());
2600 gfc_add_expr_to_block (&fnblock, tmp);
2601 gfc_free_expr (tmpe);
2605 gfc_add_expr_to_block (&fnblock, body);
2606 return gfc_finish_block (&fnblock);
2611 /* Generate function entry and exit code, and add it to the function body.
2613 Allocation and initialization of array variables.
2614 Allocation of character string variables.
2615 Initialization and possibly repacking of dummy arrays.
2616 Initialization of ASSIGN statement auxiliary variable. */
2619 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2623 gfc_formal_arglist *f;
2625 bool seen_trans_deferred_array = false;
2627 /* Deal with implicit return variables. Explicit return variables will
2628 already have been added. */
2629 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2631 if (!current_fake_result_decl)
2633 gfc_entry_list *el = NULL;
2634 if (proc_sym->attr.entry_master)
2636 for (el = proc_sym->ns->entries; el; el = el->next)
2637 if (el->sym != el->sym->result)
2641 warning (0, "Function does not return a value");
2643 else if (proc_sym->as)
2645 tree result = TREE_VALUE (current_fake_result_decl);
2646 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2648 /* An automatic character length, pointer array result. */
2649 if (proc_sym->ts.type == BT_CHARACTER
2650 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2651 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2654 else if (proc_sym->ts.type == BT_CHARACTER)
2656 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2657 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2661 gcc_assert (gfc_option.flag_f2c
2662 && proc_sym->ts.type == BT_COMPLEX);
2665 /* Initialize the INTENT(OUT) derived type dummy arguments. This
2666 should be done here so that the offsets and lbounds of arrays
2668 fnbody = init_intent_out_dt (proc_sym, fnbody);
2670 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2672 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2673 && sym->ts.derived->attr.alloc_comp;
2674 if (sym->attr.dimension)
2676 switch (sym->as->type)
2679 if (sym->attr.dummy || sym->attr.result)
2681 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2682 else if (sym->attr.pointer || sym->attr.allocatable)
2684 if (TREE_STATIC (sym->backend_decl))
2685 gfc_trans_static_array_pointer (sym);
2688 seen_trans_deferred_array = true;
2689 fnbody = gfc_trans_deferred_array (sym, fnbody);
2694 if (sym_has_alloc_comp)
2696 seen_trans_deferred_array = true;
2697 fnbody = gfc_trans_deferred_array (sym, fnbody);
2700 gfc_get_backend_locus (&loc);
2701 gfc_set_backend_locus (&sym->declared_at);
2702 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2704 gfc_set_backend_locus (&loc);
2708 case AS_ASSUMED_SIZE:
2709 /* Must be a dummy parameter. */
2710 gcc_assert (sym->attr.dummy);
2712 /* We should always pass assumed size arrays the g77 way. */
2713 fnbody = gfc_trans_g77_array (sym, fnbody);
2716 case AS_ASSUMED_SHAPE:
2717 /* Must be a dummy parameter. */
2718 gcc_assert (sym->attr.dummy);
2720 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2725 seen_trans_deferred_array = true;
2726 fnbody = gfc_trans_deferred_array (sym, fnbody);
2732 if (sym_has_alloc_comp && !seen_trans_deferred_array)
2733 fnbody = gfc_trans_deferred_array (sym, fnbody);
2735 else if (sym_has_alloc_comp)
2736 fnbody = gfc_trans_deferred_array (sym, fnbody);
2737 else if (sym->ts.type == BT_CHARACTER)
2739 gfc_get_backend_locus (&loc);
2740 gfc_set_backend_locus (&sym->declared_at);
2741 if (sym->attr.dummy || sym->attr.result)
2742 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2744 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2745 gfc_set_backend_locus (&loc);
2747 else if (sym->attr.assign)
2749 gfc_get_backend_locus (&loc);
2750 gfc_set_backend_locus (&sym->declared_at);
2751 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2752 gfc_set_backend_locus (&loc);
2758 gfc_init_block (&body);
2760 for (f = proc_sym->formal; f; f = f->next)
2762 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2764 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2765 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2766 gfc_trans_vla_type_sizes (f->sym, &body);
2770 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2771 && current_fake_result_decl != NULL)
2773 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2774 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2775 gfc_trans_vla_type_sizes (proc_sym, &body);
2778 gfc_add_expr_to_block (&body, fnbody);
2779 return gfc_finish_block (&body);
2783 /* Output an initialized decl for a module variable. */
2786 gfc_create_module_variable (gfc_symbol * sym)
2790 /* Module functions with alternate entries are dealt with later and
2791 would get caught by the next condition. */
2792 if (sym->attr.entry)
2795 /* Make sure we convert the types of the derived types from iso_c_binding
2797 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
2798 && sym->ts.type == BT_DERIVED)
2799 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
2801 /* Only output variables and array valued, or derived type,
2803 if (sym->attr.flavor != FL_VARIABLE
2804 && !(sym->attr.flavor == FL_PARAMETER
2805 && (sym->attr.dimension || sym->ts.type == BT_DERIVED)))
2808 /* Don't generate variables from other modules. Variables from
2809 COMMONs will already have been generated. */
2810 if (sym->attr.use_assoc || sym->attr.in_common)
2813 /* Equivalenced variables arrive here after creation. */
2814 if (sym->backend_decl
2815 && (sym->equiv_built || sym->attr.in_equivalence))
2818 if (sym->backend_decl)
2819 internal_error ("backend decl for module variable %s already exists",
2822 /* We always want module variables to be created. */
2823 sym->attr.referenced = 1;
2824 /* Create the decl. */
2825 decl = gfc_get_symbol_decl (sym);
2827 /* Create the variable. */
2829 rest_of_decl_compilation (decl, 1, 0);
2831 /* Also add length of strings. */
2832 if (sym->ts.type == BT_CHARACTER)
2836 length = sym->ts.cl->backend_decl;
2837 if (!INTEGER_CST_P (length))
2840 rest_of_decl_compilation (length, 1, 0);
2846 /* Generate all the required code for module variables. */
2849 gfc_generate_module_vars (gfc_namespace * ns)
2851 module_namespace = ns;
2853 /* Check if the frontend left the namespace in a reasonable state. */
2854 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2856 /* Generate COMMON blocks. */
2857 gfc_trans_common (ns);
2859 /* Create decls for all the module variables. */
2860 gfc_traverse_ns (ns, gfc_create_module_variable);
2864 gfc_generate_contained_functions (gfc_namespace * parent)
2868 /* We create all the prototypes before generating any code. */
2869 for (ns = parent->contained; ns; ns = ns->sibling)
2871 /* Skip namespaces from used modules. */
2872 if (ns->parent != parent)
2875 gfc_create_function_decl (ns);
2878 for (ns = parent->contained; ns; ns = ns->sibling)
2880 /* Skip namespaces from used modules. */
2881 if (ns->parent != parent)
2884 gfc_generate_function_code (ns);
2889 /* Drill down through expressions for the array specification bounds and
2890 character length calling generate_local_decl for all those variables
2891 that have not already been declared. */
2894 generate_local_decl (gfc_symbol *);
2896 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2899 expr_decls (gfc_expr *e, gfc_symbol *sym,
2900 int *f ATTRIBUTE_UNUSED)
2902 if (e->expr_type != EXPR_VARIABLE
2903 || sym == e->symtree->n.sym
2904 || e->symtree->n.sym->mark
2905 || e->symtree->n.sym->ns != sym->ns)
2908 generate_local_decl (e->symtree->n.sym);
2913 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2915 gfc_traverse_expr (e, sym, expr_decls, 0);
2919 /* Check for dependencies in the character length and array spec. */
2922 generate_dependency_declarations (gfc_symbol *sym)
2926 if (sym->ts.type == BT_CHARACTER
2928 && sym->ts.cl->length
2929 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
2930 generate_expr_decls (sym, sym->ts.cl->length);
2932 if (sym->as && sym->as->rank)
2934 for (i = 0; i < sym->as->rank; i++)
2936 generate_expr_decls (sym, sym->as->lower[i]);
2937 generate_expr_decls (sym, sym->as->upper[i]);
2943 /* Generate decls for all local variables. We do this to ensure correct
2944 handling of expressions which only appear in the specification of
2948 generate_local_decl (gfc_symbol * sym)
2950 if (sym->attr.flavor == FL_VARIABLE)
2952 /* Check for dependencies in the array specification and string
2953 length, adding the necessary declarations to the function. We
2954 mark the symbol now, as well as in traverse_ns, to prevent
2955 getting stuck in a circular dependency. */
2957 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2958 generate_dependency_declarations (sym);
2960 if (sym->attr.referenced)
2961 gfc_get_symbol_decl (sym);
2962 /* INTENT(out) dummy arguments are likely meant to be set. */
2963 else if (warn_unused_variable
2965 && sym->attr.intent == INTENT_OUT)
2966 gfc_warning ("Dummy argument '%s' at %L was declared INTENT(OUT) but was not set",
2967 sym->name, &sym->declared_at);
2968 /* Specific warning for unused dummy arguments. */
2969 else if (warn_unused_variable && sym->attr.dummy)
2970 gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
2972 /* Warn for unused variables, but not if they're inside a common
2973 block or are use-associated. */
2974 else if (warn_unused_variable
2975 && !(sym->attr.in_common || sym->attr.use_assoc))
2976 gfc_warning ("Unused variable '%s' declared at %L", sym->name,
2978 /* For variable length CHARACTER parameters, the PARM_DECL already
2979 references the length variable, so force gfc_get_symbol_decl
2980 even when not referenced. If optimize > 0, it will be optimized
2981 away anyway. But do this only after emitting -Wunused-parameter
2982 warning if requested. */
2983 if (sym->attr.dummy && ! sym->attr.referenced
2984 && sym->ts.type == BT_CHARACTER
2985 && sym->ts.cl->backend_decl != NULL
2986 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2988 sym->attr.referenced = 1;
2989 gfc_get_symbol_decl (sym);
2992 /* We do not want the middle-end to warn about unused parameters
2993 as this was already done above. */
2994 if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
2995 TREE_NO_WARNING(sym->backend_decl) = 1;
2997 else if (sym->attr.flavor == FL_PARAMETER)
2999 if (warn_unused_parameter
3000 && !sym->attr.referenced
3001 && !sym->attr.use_assoc)
3002 gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
3006 if (sym->attr.dummy == 1)
3008 /* Modify the tree type for scalar character dummy arguments of bind(c)
3009 procedures if they are passed by value. The tree type for them will
3010 be promoted to INTEGER_TYPE for the middle end, which appears to be
3011 what C would do with characters passed by-value. The value attribute
3012 implies the dummy is a scalar. */
3013 if (sym->attr.value == 1 && sym->backend_decl != NULL
3014 && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
3015 && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
3016 gfc_conv_scalar_char_value (sym, NULL, NULL);
3019 /* Make sure we convert the types of the derived types from iso_c_binding
3021 if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
3022 && sym->ts.type == BT_DERIVED)
3023 sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
3027 generate_local_vars (gfc_namespace * ns)
3029 gfc_traverse_ns (ns, generate_local_decl);
3033 /* Generate a switch statement to jump to the correct entry point. Also
3034 creates the label decls for the entry points. */
3037 gfc_trans_entry_master_switch (gfc_entry_list * el)
3044 gfc_init_block (&block);
3045 for (; el; el = el->next)
3047 /* Add the case label. */
3048 label = gfc_build_label_decl (NULL_TREE);
3049 val = build_int_cst (gfc_array_index_type, el->id);
3050 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3051 gfc_add_expr_to_block (&block, tmp);
3053 /* And jump to the actual entry point. */
3054 label = gfc_build_label_decl (NULL_TREE);
3055 tmp = build1_v (GOTO_EXPR, label);
3056 gfc_add_expr_to_block (&block, tmp);
3058 /* Save the label decl. */
3061 tmp = gfc_finish_block (&block);
3062 /* The first argument selects the entry point. */
3063 val = DECL_ARGUMENTS (current_function_decl);
3064 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3069 /* Generate code for a function. */
3072 gfc_generate_function_code (gfc_namespace * ns)
3085 sym = ns->proc_name;
3087 /* Check that the frontend isn't still using this. */
3088 gcc_assert (sym->tlink == NULL);
3091 /* Create the declaration for functions with global scope. */
3092 if (!sym->backend_decl)
3093 gfc_create_function_decl (ns);
3095 fndecl = sym->backend_decl;
3096 old_context = current_function_decl;
3100 push_function_context ();
3101 saved_parent_function_decls = saved_function_decls;
3102 saved_function_decls = NULL_TREE;
3105 trans_function_start (sym);
3107 gfc_start_block (&block);
3109 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3111 /* Copy length backend_decls to all entry point result
3116 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3117 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3118 for (el = ns->entries; el; el = el->next)
3119 el->sym->result->ts.cl->backend_decl = backend_decl;
3122 /* Translate COMMON blocks. */
3123 gfc_trans_common (ns);
3125 /* Null the parent fake result declaration if this namespace is
3126 a module function or an external procedures. */
3127 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3128 || ns->parent == NULL)
3129 parent_fake_result_decl = NULL_TREE;
3131 gfc_generate_contained_functions (ns);
3133 generate_local_vars (ns);
3135 /* Keep the parent fake result declaration in module functions
3136 or external procedures. */
3137 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3138 || ns->parent == NULL)
3139 current_fake_result_decl = parent_fake_result_decl;
3141 current_fake_result_decl = NULL_TREE;
3143 current_function_return_label = NULL;
3145 /* Now generate the code for the body of this function. */
3146 gfc_init_block (&body);
3148 /* If this is the main program, add a call to set_options to set up the
3149 runtime library Fortran language standard parameters. */
3150 if (sym->attr.is_main_program)
3152 tree array_type, array, var;
3154 /* Passing a new option to the library requires four modifications:
3155 + add it to the tree_cons list below
3156 + change the array size in the call to build_array_type
3157 + change the first argument to the library call
3158 gfor_fndecl_set_options
3159 + modify the library (runtime/compile_options.c)! */
3160 array = tree_cons (NULL_TREE,
3161 build_int_cst (integer_type_node,
3162 gfc_option.warn_std), NULL_TREE);
3163 array = tree_cons (NULL_TREE,
3164 build_int_cst (integer_type_node,
3165 gfc_option.allow_std), array);
3166 array = tree_cons (NULL_TREE,
3167 build_int_cst (integer_type_node, pedantic), array);
3168 array = tree_cons (NULL_TREE,
3169 build_int_cst (integer_type_node,
3170 gfc_option.flag_dump_core), array);
3171 array = tree_cons (NULL_TREE,
3172 build_int_cst (integer_type_node,
3173 gfc_option.flag_backtrace), array);
3174 array = tree_cons (NULL_TREE,
3175 build_int_cst (integer_type_node,
3176 gfc_option.flag_sign_zero), array);
3178 array = tree_cons (NULL_TREE,
3179 build_int_cst (integer_type_node,
3180 flag_bounds_check), array);
3182 array_type = build_array_type (integer_type_node,
3183 build_index_type (build_int_cst (NULL_TREE,
3185 array = build_constructor_from_list (array_type, nreverse (array));
3186 TREE_CONSTANT (array) = 1;
3187 TREE_INVARIANT (array) = 1;
3188 TREE_STATIC (array) = 1;
3190 /* Create a static variable to hold the jump table. */
3191 var = gfc_create_var (array_type, "options");
3192 TREE_CONSTANT (var) = 1;
3193 TREE_INVARIANT (var) = 1;
3194 TREE_STATIC (var) = 1;
3195 TREE_READONLY (var) = 1;
3196 DECL_INITIAL (var) = array;
3197 var = gfc_build_addr_expr (pvoid_type_node, var);
3199 tmp = build_call_expr (gfor_fndecl_set_options, 2,
3200 build_int_cst (integer_type_node, 7), var);
3201 gfc_add_expr_to_block (&body, tmp);
3204 /* If this is the main program and a -ffpe-trap option was provided,
3205 add a call to set_fpe so that the library will raise a FPE when
3207 if (sym->attr.is_main_program && gfc_option.fpe != 0)
3209 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
3210 build_int_cst (integer_type_node,
3212 gfc_add_expr_to_block (&body, tmp);
3215 /* If this is the main program and an -fconvert option was provided,
3216 add a call to set_convert. */
3218 if (sym->attr.is_main_program && gfc_option.convert != GFC_CONVERT_NATIVE)
3220 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
3221 build_int_cst (integer_type_node,
3222 gfc_option.convert));
3223 gfc_add_expr_to_block (&body, tmp);
3226 /* If this is the main program and an -frecord-marker option was provided,
3227 add a call to set_record_marker. */
3229 if (sym->attr.is_main_program && gfc_option.record_marker != 0)
3231 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
3232 build_int_cst (integer_type_node,
3233 gfc_option.record_marker));
3234 gfc_add_expr_to_block (&body, tmp);
3237 if (sym->attr.is_main_program && gfc_option.max_subrecord_length != 0)
3239 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length,
3241 build_int_cst (integer_type_node,
3242 gfc_option.max_subrecord_length));
3243 gfc_add_expr_to_block (&body, tmp);
3246 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
3247 && sym->attr.subroutine)
3249 tree alternate_return;
3250 alternate_return = gfc_get_fake_result_decl (sym, 0);
3251 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
3256 /* Jump to the correct entry point. */
3257 tmp = gfc_trans_entry_master_switch (ns->entries);
3258 gfc_add_expr_to_block (&body, tmp);
3261 tmp = gfc_trans_code (ns->code);
3262 gfc_add_expr_to_block (&body, tmp);
3264 /* Add a return label if needed. */
3265 if (current_function_return_label)
3267 tmp = build1_v (LABEL_EXPR, current_function_return_label);
3268 gfc_add_expr_to_block (&body, tmp);
3271 tmp = gfc_finish_block (&body);
3272 /* Add code to create and cleanup arrays. */
3273 tmp = gfc_trans_deferred_vars (sym, tmp);
3275 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
3277 if (sym->attr.subroutine || sym == sym->result)
3279 if (current_fake_result_decl != NULL)
3280 result = TREE_VALUE (current_fake_result_decl);
3283 current_fake_result_decl = NULL_TREE;
3286 result = sym->result->backend_decl;
3288 if (result != NULL_TREE && sym->attr.function
3289 && sym->ts.type == BT_DERIVED
3290 && sym->ts.derived->attr.alloc_comp
3291 && !sym->attr.pointer)
3293 rank = sym->as ? sym->as->rank : 0;
3294 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
3295 gfc_add_expr_to_block (&block, tmp2);
3298 gfc_add_expr_to_block (&block, tmp);
3300 if (result == NULL_TREE)
3301 warning (0, "Function return value not set");
3304 /* Set the return value to the dummy result variable. The
3305 types may be different for scalar default REAL functions
3306 with -ff2c, therefore we have to convert. */
3307 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
3308 tmp = build2 (MODIFY_EXPR, TREE_TYPE (tmp),
3309 DECL_RESULT (fndecl), tmp);
3310 tmp = build1_v (RETURN_EXPR, tmp);
3311 gfc_add_expr_to_block (&block, tmp);
3315 gfc_add_expr_to_block (&block, tmp);
3318 /* Add all the decls we created during processing. */
3319 decl = saved_function_decls;
3324 next = TREE_CHAIN (decl);
3325 TREE_CHAIN (decl) = NULL_TREE;
3329 saved_function_decls = NULL_TREE;
3331 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
3333 /* Finish off this function and send it for code generation. */
3335 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3337 /* Output the GENERIC tree. */
3338 dump_function (TDI_original, fndecl);
3340 /* Store the end of the function, so that we get good line number
3341 info for the epilogue. */
3342 cfun->function_end_locus = input_location;
3344 /* We're leaving the context of this function, so zap cfun.
3345 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3346 tree_rest_of_compilation. */
3351 pop_function_context ();
3352 saved_function_decls = saved_parent_function_decls;
3354 current_function_decl = old_context;
3356 if (decl_function_context (fndecl))
3357 /* Register this function with cgraph just far enough to get it
3358 added to our parent's nested function list. */
3359 (void) cgraph_node (fndecl);
3362 gfc_gimplify_function (fndecl);
3363 cgraph_finalize_function (fndecl, false);
3368 gfc_generate_constructors (void)
3370 gcc_assert (gfc_static_ctors == NULL_TREE);
3378 if (gfc_static_ctors == NULL_TREE)
3381 fnname = get_file_function_name ("I");
3382 type = build_function_type (void_type_node,
3383 gfc_chainon_list (NULL_TREE, void_type_node));
3385 fndecl = build_decl (FUNCTION_DECL, fnname, type);
3386 TREE_PUBLIC (fndecl) = 1;
3388 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
3389 DECL_ARTIFICIAL (decl) = 1;
3390 DECL_IGNORED_P (decl) = 1;
3391 DECL_CONTEXT (decl) = fndecl;
3392 DECL_RESULT (fndecl) = decl;
3396 current_function_decl = fndecl;
3398 rest_of_decl_compilation (fndecl, 1, 0);
3400 make_decl_rtl (fndecl);
3402 init_function_start (fndecl);
3406 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3408 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
3409 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3414 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3416 free_after_parsing (cfun);
3417 free_after_compilation (cfun);
3419 tree_rest_of_compilation (fndecl);
3421 current_function_decl = NULL_TREE;
3425 /* Translates a BLOCK DATA program unit. This means emitting the
3426 commons contained therein plus their initializations. We also emit
3427 a globally visible symbol to make sure that each BLOCK DATA program
3428 unit remains unique. */
3431 gfc_generate_block_data (gfc_namespace * ns)
3436 /* Tell the backend the source location of the block data. */
3438 gfc_set_backend_locus (&ns->proc_name->declared_at);
3440 gfc_set_backend_locus (&gfc_current_locus);
3442 /* Process the DATA statements. */
3443 gfc_trans_common (ns);
3445 /* Create a global symbol with the mane of the block data. This is to
3446 generate linker errors if the same name is used twice. It is never
3449 id = gfc_sym_mangled_function_id (ns->proc_name);
3451 id = get_identifier ("__BLOCK_DATA__");
3453 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3454 TREE_PUBLIC (decl) = 1;
3455 TREE_STATIC (decl) = 1;
3458 rest_of_decl_compilation (decl, 1, 0);
3462 #include "gt-fortran-trans-decl.h"