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 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, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* trans-decl.c -- Handling of backend function and variable decls, etc */
27 #include "coretypes.h"
29 #include "tree-dump.h"
30 #include "tree-gimple.h"
41 #include "trans-types.h"
42 #include "trans-array.h"
43 #include "trans-const.h"
44 /* Only for gfc_trans_code. Shouldn't need to include this. */
45 #include "trans-stmt.h"
47 #define MAX_LABEL_VALUE 99999
50 /* Holds the result of the function if no result variable specified. */
52 static GTY(()) tree current_fake_result_decl;
53 static GTY(()) tree parent_fake_result_decl;
55 static GTY(()) tree current_function_return_label;
58 /* Holds the variable DECLs for the current function. */
60 static GTY(()) tree saved_function_decls;
61 static GTY(()) tree saved_parent_function_decls;
64 /* The namespace of the module we're currently generating. Only used while
65 outputting decls for module variables. Do not rely on this being set. */
67 static gfc_namespace *module_namespace;
70 /* List of static constructor functions. */
72 tree gfc_static_ctors;
75 /* Function declarations for builtin library functions. */
77 tree gfor_fndecl_internal_realloc;
78 tree gfor_fndecl_internal_realloc64;
79 tree gfor_fndecl_allocate;
80 tree gfor_fndecl_allocate64;
81 tree gfor_fndecl_allocate_array;
82 tree gfor_fndecl_allocate64_array;
83 tree gfor_fndecl_deallocate;
84 tree gfor_fndecl_pause_numeric;
85 tree gfor_fndecl_pause_string;
86 tree gfor_fndecl_stop_numeric;
87 tree gfor_fndecl_stop_string;
88 tree gfor_fndecl_select_string;
89 tree gfor_fndecl_runtime_error;
90 tree gfor_fndecl_runtime_error_at;
91 tree gfor_fndecl_os_error;
92 tree gfor_fndecl_generate_error;
93 tree gfor_fndecl_set_fpe;
94 tree gfor_fndecl_set_std;
95 tree gfor_fndecl_set_convert;
96 tree gfor_fndecl_set_record_marker;
97 tree gfor_fndecl_set_max_subrecord_length;
98 tree gfor_fndecl_ctime;
99 tree gfor_fndecl_fdate;
100 tree gfor_fndecl_ttynam;
101 tree gfor_fndecl_in_pack;
102 tree gfor_fndecl_in_unpack;
103 tree gfor_fndecl_associated;
106 /* Math functions. Many other math functions are handled in
107 trans-intrinsic.c. */
109 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
110 tree gfor_fndecl_math_cpowf;
111 tree gfor_fndecl_math_cpow;
112 tree gfor_fndecl_math_cpowl10;
113 tree gfor_fndecl_math_cpowl16;
114 tree gfor_fndecl_math_ishftc4;
115 tree gfor_fndecl_math_ishftc8;
116 tree gfor_fndecl_math_ishftc16;
117 tree gfor_fndecl_math_exponent4;
118 tree gfor_fndecl_math_exponent8;
119 tree gfor_fndecl_math_exponent10;
120 tree gfor_fndecl_math_exponent16;
123 /* String functions. */
125 tree gfor_fndecl_compare_string;
126 tree gfor_fndecl_concat_string;
127 tree gfor_fndecl_string_len_trim;
128 tree gfor_fndecl_string_index;
129 tree gfor_fndecl_string_scan;
130 tree gfor_fndecl_string_verify;
131 tree gfor_fndecl_string_trim;
132 tree gfor_fndecl_adjustl;
133 tree gfor_fndecl_adjustr;
136 /* Other misc. runtime library functions. */
138 tree gfor_fndecl_size0;
139 tree gfor_fndecl_size1;
140 tree gfor_fndecl_iargc;
142 /* Intrinsic functions implemented in FORTRAN. */
143 tree gfor_fndecl_si_kind;
144 tree gfor_fndecl_sr_kind;
146 /* BLAS gemm functions. */
147 tree gfor_fndecl_sgemm;
148 tree gfor_fndecl_dgemm;
149 tree gfor_fndecl_cgemm;
150 tree gfor_fndecl_zgemm;
154 gfc_add_decl_to_parent_function (tree decl)
157 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
158 DECL_NONLOCAL (decl) = 1;
159 TREE_CHAIN (decl) = saved_parent_function_decls;
160 saved_parent_function_decls = decl;
164 gfc_add_decl_to_function (tree decl)
167 TREE_USED (decl) = 1;
168 DECL_CONTEXT (decl) = current_function_decl;
169 TREE_CHAIN (decl) = saved_function_decls;
170 saved_function_decls = decl;
174 /* Build a backend label declaration. Set TREE_USED for named labels.
175 The context of the label is always the current_function_decl. All
176 labels are marked artificial. */
179 gfc_build_label_decl (tree label_id)
181 /* 2^32 temporaries should be enough. */
182 static unsigned int tmp_num = 1;
186 if (label_id == NULL_TREE)
188 /* Build an internal label name. */
189 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
190 label_id = get_identifier (label_name);
195 /* Build the LABEL_DECL node. Labels have no type. */
196 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
197 DECL_CONTEXT (label_decl) = current_function_decl;
198 DECL_MODE (label_decl) = VOIDmode;
200 /* We always define the label as used, even if the original source
201 file never references the label. We don't want all kinds of
202 spurious warnings for old-style Fortran code with too many
204 TREE_USED (label_decl) = 1;
206 DECL_ARTIFICIAL (label_decl) = 1;
211 /* Returns the return label for the current function. */
214 gfc_get_return_label (void)
216 char name[GFC_MAX_SYMBOL_LEN + 10];
218 if (current_function_return_label)
219 return current_function_return_label;
221 sprintf (name, "__return_%s",
222 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
224 current_function_return_label =
225 gfc_build_label_decl (get_identifier (name));
227 DECL_ARTIFICIAL (current_function_return_label) = 1;
229 return current_function_return_label;
233 /* Set the backend source location of a decl. */
236 gfc_set_decl_location (tree decl, locus * loc)
238 #ifdef USE_MAPPED_LOCATION
239 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
241 DECL_SOURCE_LINE (decl) = loc->lb->linenum;
242 DECL_SOURCE_FILE (decl) = loc->lb->file->filename;
247 /* Return the backend label declaration for a given label structure,
248 or create it if it doesn't exist yet. */
251 gfc_get_label_decl (gfc_st_label * lp)
253 if (lp->backend_decl)
254 return lp->backend_decl;
257 char label_name[GFC_MAX_SYMBOL_LEN + 1];
260 /* Validate the label declaration from the front end. */
261 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
263 /* Build a mangled name for the label. */
264 sprintf (label_name, "__label_%.6d", lp->value);
266 /* Build the LABEL_DECL node. */
267 label_decl = gfc_build_label_decl (get_identifier (label_name));
269 /* Tell the debugger where the label came from. */
270 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
271 gfc_set_decl_location (label_decl, &lp->where);
273 DECL_ARTIFICIAL (label_decl) = 1;
275 /* Store the label in the label list and return the LABEL_DECL. */
276 lp->backend_decl = label_decl;
282 /* Convert a gfc_symbol to an identifier of the same name. */
285 gfc_sym_identifier (gfc_symbol * sym)
287 return (get_identifier (sym->name));
291 /* Construct mangled name from symbol name. */
294 gfc_sym_mangled_identifier (gfc_symbol * sym)
296 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
298 if (sym->module == NULL)
299 return gfc_sym_identifier (sym);
302 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
303 return get_identifier (name);
308 /* Construct mangled function name from symbol name. */
311 gfc_sym_mangled_function_id (gfc_symbol * sym)
314 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
316 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
317 || (sym->module != NULL && (sym->attr.external
318 || sym->attr.if_source == IFSRC_IFBODY)))
320 if (strcmp (sym->name, "MAIN__") == 0
321 || sym->attr.proc == PROC_INTRINSIC)
322 return get_identifier (sym->name);
324 if (gfc_option.flag_underscoring)
326 has_underscore = strchr (sym->name, '_') != 0;
327 if (gfc_option.flag_second_underscore && has_underscore)
328 snprintf (name, sizeof name, "%s__", sym->name);
330 snprintf (name, sizeof name, "%s_", sym->name);
331 return get_identifier (name);
334 return get_identifier (sym->name);
338 snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
339 return get_identifier (name);
344 /* Returns true if a variable of specified size should go on the stack. */
347 gfc_can_put_var_on_stack (tree size)
349 unsigned HOST_WIDE_INT low;
351 if (!INTEGER_CST_P (size))
354 if (gfc_option.flag_max_stack_var_size < 0)
357 if (TREE_INT_CST_HIGH (size) != 0)
360 low = TREE_INT_CST_LOW (size);
361 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
364 /* TODO: Set a per-function stack size limit. */
370 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
371 an expression involving its corresponding pointer. There are
372 2 cases; one for variable size arrays, and one for everything else,
373 because variable-sized arrays require one fewer level of
377 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
379 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
382 /* Parameters need to be dereferenced. */
383 if (sym->cp_pointer->attr.dummy)
384 ptr_decl = build_fold_indirect_ref (ptr_decl);
386 /* Check to see if we're dealing with a variable-sized array. */
387 if (sym->attr.dimension
388 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
390 /* These decls will be dereferenced later, so we don't dereference
392 value = convert (TREE_TYPE (decl), ptr_decl);
396 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
398 value = build_fold_indirect_ref (ptr_decl);
401 SET_DECL_VALUE_EXPR (decl, value);
402 DECL_HAS_VALUE_EXPR_P (decl) = 1;
403 GFC_DECL_CRAY_POINTEE (decl) = 1;
404 /* This is a fake variable just for debugging purposes. */
405 TREE_ASM_WRITTEN (decl) = 1;
409 /* Finish processing of a declaration without an initial value. */
412 gfc_finish_decl (tree decl)
414 gcc_assert (TREE_CODE (decl) == PARM_DECL
415 || DECL_INITIAL (decl) == NULL_TREE);
417 if (TREE_CODE (decl) != VAR_DECL)
420 if (DECL_SIZE (decl) == NULL_TREE
421 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
422 layout_decl (decl, 0);
424 /* A few consistency checks. */
425 /* A static variable with an incomplete type is an error if it is
426 initialized. Also if it is not file scope. Otherwise, let it
427 through, but if it is not `extern' then it may cause an error
429 /* An automatic variable with an incomplete type is an error. */
431 /* We should know the storage size. */
432 gcc_assert (DECL_SIZE (decl) != NULL_TREE
433 || (TREE_STATIC (decl)
434 ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
435 : DECL_EXTERNAL (decl)));
437 /* The storage size should be constant. */
438 gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
440 || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
444 /* Apply symbol attributes to a variable, and add it to the function scope. */
447 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
450 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
451 This is the equivalent of the TARGET variables.
452 We also need to set this if the variable is passed by reference in a
455 /* Set DECL_VALUE_EXPR for Cray Pointees. */
456 if (sym->attr.cray_pointee)
457 gfc_finish_cray_pointee (decl, sym);
459 if (sym->attr.target)
460 TREE_ADDRESSABLE (decl) = 1;
461 /* If it wasn't used we wouldn't be getting it. */
462 TREE_USED (decl) = 1;
464 /* Chain this decl to the pending declarations. Don't do pushdecl()
465 because this would add them to the current scope rather than the
467 if (current_function_decl != NULL_TREE)
469 if (sym->ns->proc_name->backend_decl == current_function_decl
470 || sym->result == sym)
471 gfc_add_decl_to_function (decl);
473 gfc_add_decl_to_parent_function (decl);
476 if (sym->attr.cray_pointee)
479 /* If a variable is USE associated, it's always external. */
480 if (sym->attr.use_assoc)
482 DECL_EXTERNAL (decl) = 1;
483 TREE_PUBLIC (decl) = 1;
485 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
487 /* TODO: Don't set sym->module for result or dummy variables. */
488 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
489 /* This is the declaration of a module variable. */
490 TREE_PUBLIC (decl) = 1;
491 TREE_STATIC (decl) = 1;
494 if ((sym->attr.save || sym->attr.data || sym->value)
495 && !sym->attr.use_assoc)
496 TREE_STATIC (decl) = 1;
498 if (sym->attr.volatile_)
500 TREE_THIS_VOLATILE (decl) = 1;
501 new = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
502 TREE_TYPE (decl) = new;
505 /* Keep variables larger than max-stack-var-size off stack. */
506 if (!sym->ns->proc_name->attr.recursive
507 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
508 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
509 /* Put variable length auto array pointers always into stack. */
510 && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
511 || sym->attr.dimension == 0
512 || sym->as->type != AS_EXPLICIT
514 || sym->attr.allocatable)
515 && !DECL_ARTIFICIAL (decl))
516 TREE_STATIC (decl) = 1;
518 /* Handle threadprivate variables. */
519 if (sym->attr.threadprivate
520 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
521 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
525 /* Allocate the lang-specific part of a decl. */
528 gfc_allocate_lang_decl (tree decl)
530 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
531 ggc_alloc_cleared (sizeof (struct lang_decl));
534 /* Remember a symbol to generate initialization/cleanup code at function
538 gfc_defer_symbol_init (gfc_symbol * sym)
544 /* Don't add a symbol twice. */
548 last = head = sym->ns->proc_name;
551 /* Make sure that setup code for dummy variables which are used in the
552 setup of other variables is generated first. */
555 /* Find the first dummy arg seen after us, or the first non-dummy arg.
556 This is a circular list, so don't go past the head. */
558 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
564 /* Insert in between last and p. */
570 /* Create an array index type variable with function scope. */
573 create_index_var (const char * pfx, int nest)
577 decl = gfc_create_var_np (gfc_array_index_type, pfx);
579 gfc_add_decl_to_parent_function (decl);
581 gfc_add_decl_to_function (decl);
586 /* Create variables to hold all the non-constant bits of info for a
587 descriptorless array. Remember these in the lang-specific part of the
591 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
597 type = TREE_TYPE (decl);
599 /* We just use the descriptor, if there is one. */
600 if (GFC_DESCRIPTOR_TYPE_P (type))
603 gcc_assert (GFC_ARRAY_TYPE_P (type));
604 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
605 && !sym->attr.contained;
607 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
609 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
610 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
611 /* Don't try to use the unknown bound for assumed shape arrays. */
612 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
613 && (sym->as->type != AS_ASSUMED_SIZE
614 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
615 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
617 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
618 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
620 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
622 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
625 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
627 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
630 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
631 && sym->as->type != AS_ASSUMED_SIZE)
632 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
634 if (POINTER_TYPE_P (type))
636 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
637 gcc_assert (TYPE_LANG_SPECIFIC (type)
638 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
639 type = TREE_TYPE (type);
642 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
646 size = build2 (MINUS_EXPR, gfc_array_index_type,
647 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
648 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
650 TYPE_DOMAIN (type) = range;
656 /* For some dummy arguments we don't use the actual argument directly.
657 Instead we create a local decl and use that. This allows us to perform
658 initialization, and construct full type information. */
661 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
671 if (sym->attr.pointer || sym->attr.allocatable)
674 /* Add to list of variables if not a fake result variable. */
675 if (sym->attr.result || sym->attr.dummy)
676 gfc_defer_symbol_init (sym);
678 type = TREE_TYPE (dummy);
679 gcc_assert (TREE_CODE (dummy) == PARM_DECL
680 && POINTER_TYPE_P (type));
682 /* Do we know the element size? */
683 known_size = sym->ts.type != BT_CHARACTER
684 || INTEGER_CST_P (sym->ts.cl->backend_decl);
686 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
688 /* For descriptorless arrays with known element size the actual
689 argument is sufficient. */
690 gcc_assert (GFC_ARRAY_TYPE_P (type));
691 gfc_build_qualified_array (dummy, sym);
695 type = TREE_TYPE (type);
696 if (GFC_DESCRIPTOR_TYPE_P (type))
698 /* Create a descriptorless array pointer. */
701 if (!gfc_option.flag_repack_arrays)
703 if (as->type == AS_ASSUMED_SIZE)
704 packed = PACKED_FULL;
708 if (as->type == AS_EXPLICIT)
710 packed = PACKED_FULL;
711 for (n = 0; n < as->rank; n++)
715 && as->upper[n]->expr_type == EXPR_CONSTANT
716 && as->lower[n]->expr_type == EXPR_CONSTANT))
717 packed = PACKED_PARTIAL;
721 packed = PACKED_PARTIAL;
724 type = gfc_typenode_for_spec (&sym->ts);
725 type = gfc_get_nodesc_array_type (type, sym->as, packed);
729 /* We now have an expression for the element size, so create a fully
730 qualified type. Reset sym->backend decl or this will just return the
732 DECL_ARTIFICIAL (sym->backend_decl) = 1;
733 sym->backend_decl = NULL_TREE;
734 type = gfc_sym_type (sym);
735 packed = PACKED_FULL;
738 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
739 decl = build_decl (VAR_DECL, get_identifier (name), type);
741 DECL_ARTIFICIAL (decl) = 1;
742 TREE_PUBLIC (decl) = 0;
743 TREE_STATIC (decl) = 0;
744 DECL_EXTERNAL (decl) = 0;
746 /* We should never get deferred shape arrays here. We used to because of
748 gcc_assert (sym->as->type != AS_DEFERRED);
750 if (packed == PACKED_PARTIAL)
751 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
752 else if (packed == PACKED_FULL)
753 GFC_DECL_PACKED_ARRAY (decl) = 1;
755 gfc_build_qualified_array (decl, sym);
757 if (DECL_LANG_SPECIFIC (dummy))
758 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
760 gfc_allocate_lang_decl (decl);
762 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
764 if (sym->ns->proc_name->backend_decl == current_function_decl
765 || sym->attr.contained)
766 gfc_add_decl_to_function (decl);
768 gfc_add_decl_to_parent_function (decl);
774 /* Return a constant or a variable to use as a string length. Does not
775 add the decl to the current scope. */
778 gfc_create_string_length (gfc_symbol * sym)
782 gcc_assert (sym->ts.cl);
783 gfc_conv_const_charlen (sym->ts.cl);
785 if (sym->ts.cl->backend_decl == NULL_TREE)
787 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
789 /* Also prefix the mangled name. */
790 strcpy (&name[1], sym->name);
792 length = build_decl (VAR_DECL, get_identifier (name),
793 gfc_charlen_type_node);
794 DECL_ARTIFICIAL (length) = 1;
795 TREE_USED (length) = 1;
796 if (sym->ns->proc_name->tlink != NULL)
797 gfc_defer_symbol_init (sym);
798 sym->ts.cl->backend_decl = length;
801 return sym->ts.cl->backend_decl;
804 /* If a variable is assigned a label, we add another two auxiliary
808 gfc_add_assign_aux_vars (gfc_symbol * sym)
814 gcc_assert (sym->backend_decl);
816 decl = sym->backend_decl;
817 gfc_allocate_lang_decl (decl);
818 GFC_DECL_ASSIGN (decl) = 1;
819 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
820 gfc_charlen_type_node);
821 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
823 gfc_finish_var_decl (length, sym);
824 gfc_finish_var_decl (addr, sym);
825 /* STRING_LENGTH is also used as flag. Less than -1 means that
826 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
827 target label's address. Otherwise, value is the length of a format string
828 and ASSIGN_ADDR is its address. */
829 if (TREE_STATIC (length))
830 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
832 gfc_defer_symbol_init (sym);
834 GFC_DECL_STRING_LEN (decl) = length;
835 GFC_DECL_ASSIGN_ADDR (decl) = addr;
838 /* Return the decl for a gfc_symbol, create it if it doesn't already
842 gfc_get_symbol_decl (gfc_symbol * sym)
845 tree length = NULL_TREE;
848 gcc_assert (sym->attr.referenced
849 || sym->attr.use_assoc
850 || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
852 if (sym->ns && sym->ns->proc_name->attr.function)
853 byref = gfc_return_by_reference (sym->ns->proc_name);
857 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
859 /* Return via extra parameter. */
860 if (sym->attr.result && byref
861 && !sym->backend_decl)
864 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
865 /* For entry master function skip over the __entry
867 if (sym->ns->proc_name->attr.entry_master)
868 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
871 /* Dummy variables should already have been created. */
872 gcc_assert (sym->backend_decl);
874 /* Create a character length variable. */
875 if (sym->ts.type == BT_CHARACTER)
877 if (sym->ts.cl->backend_decl == NULL_TREE)
878 length = gfc_create_string_length (sym);
880 length = sym->ts.cl->backend_decl;
881 if (TREE_CODE (length) == VAR_DECL
882 && DECL_CONTEXT (length) == NULL_TREE)
884 /* Add the string length to the same context as the symbol. */
885 if (DECL_CONTEXT (sym->backend_decl) == current_function_decl)
886 gfc_add_decl_to_function (length);
888 gfc_add_decl_to_parent_function (length);
890 gcc_assert (DECL_CONTEXT (sym->backend_decl) ==
891 DECL_CONTEXT (length));
893 gfc_defer_symbol_init (sym);
897 /* Use a copy of the descriptor for dummy arrays. */
898 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
900 decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
901 /* Prevent the dummy from being detected as unused if it is copied. */
902 if (sym->backend_decl != NULL && decl != sym->backend_decl)
903 DECL_ARTIFICIAL (sym->backend_decl) = 1;
904 sym->backend_decl = decl;
907 TREE_USED (sym->backend_decl) = 1;
908 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
910 gfc_add_assign_aux_vars (sym);
912 return sym->backend_decl;
915 if (sym->backend_decl)
916 return sym->backend_decl;
918 /* Catch function declarations. Only used for actual parameters. */
919 if (sym->attr.flavor == FL_PROCEDURE)
921 decl = gfc_get_extern_function_decl (sym);
925 if (sym->attr.intrinsic)
926 internal_error ("intrinsic variable which isn't a procedure");
928 /* Create string length decl first so that they can be used in the
930 if (sym->ts.type == BT_CHARACTER)
931 length = gfc_create_string_length (sym);
933 /* Create the decl for the variable. */
934 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
936 gfc_set_decl_location (decl, &sym->declared_at);
938 /* Symbols from modules should have their assembler names mangled.
939 This is done here rather than in gfc_finish_var_decl because it
940 is different for string length variables. */
942 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
944 if (sym->attr.dimension)
946 /* Create variables to hold the non-constant bits of array info. */
947 gfc_build_qualified_array (decl, sym);
949 /* Remember this variable for allocation/cleanup. */
950 gfc_defer_symbol_init (sym);
952 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
953 GFC_DECL_PACKED_ARRAY (decl) = 1;
956 if (sym->ts.type == BT_DERIVED && sym->ts.derived->attr.alloc_comp)
957 gfc_defer_symbol_init (sym);
959 gfc_finish_var_decl (decl, sym);
961 if (sym->ts.type == BT_CHARACTER)
963 /* Character variables need special handling. */
964 gfc_allocate_lang_decl (decl);
966 if (TREE_CODE (length) != INTEGER_CST)
968 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
972 /* Also prefix the mangled name for symbols from modules. */
973 strcpy (&name[1], sym->name);
976 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
977 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
979 gfc_finish_var_decl (length, sym);
980 gcc_assert (!sym->value);
983 sym->backend_decl = decl;
985 if (sym->attr.assign)
986 gfc_add_assign_aux_vars (sym);
988 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
990 /* Add static initializer. */
991 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
992 TREE_TYPE (decl), sym->attr.dimension,
993 sym->attr.pointer || sym->attr.allocatable);
1000 /* Substitute a temporary variable in place of the real one. */
1003 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
1005 save->attr = sym->attr;
1006 save->decl = sym->backend_decl;
1008 gfc_clear_attr (&sym->attr);
1009 sym->attr.referenced = 1;
1010 sym->attr.flavor = FL_VARIABLE;
1012 sym->backend_decl = decl;
1016 /* Restore the original variable. */
1019 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1021 sym->attr = save->attr;
1022 sym->backend_decl = save->decl;
1026 /* Get a basic decl for an external function. */
1029 gfc_get_extern_function_decl (gfc_symbol * sym)
1034 gfc_intrinsic_sym *isym;
1036 char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'. */
1040 if (sym->backend_decl)
1041 return sym->backend_decl;
1043 /* We should never be creating external decls for alternate entry points.
1044 The procedure may be an alternate entry point, but we don't want/need
1046 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1048 if (sym->attr.intrinsic)
1050 /* Call the resolution function to get the actual name. This is
1051 a nasty hack which relies on the resolution functions only looking
1052 at the first argument. We pass NULL for the second argument
1053 otherwise things like AINT get confused. */
1054 isym = gfc_find_function (sym->name);
1055 gcc_assert (isym->resolve.f0 != NULL);
1057 memset (&e, 0, sizeof (e));
1058 e.expr_type = EXPR_FUNCTION;
1060 memset (&argexpr, 0, sizeof (argexpr));
1061 gcc_assert (isym->formal);
1062 argexpr.ts = isym->formal->ts;
1064 if (isym->formal->next == NULL)
1065 isym->resolve.f1 (&e, &argexpr);
1068 if (isym->formal->next->next == NULL)
1069 isym->resolve.f2 (&e, &argexpr, NULL);
1072 /* All specific intrinsics take less than 4 arguments. */
1073 gcc_assert (isym->formal->next->next->next == NULL);
1074 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
1078 if (gfc_option.flag_f2c
1079 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1080 || e.ts.type == BT_COMPLEX))
1082 /* Specific which needs a different implementation if f2c
1083 calling conventions are used. */
1084 sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
1087 sprintf (s, "_gfortran_specific%s", e.value.function.name);
1089 name = get_identifier (s);
1090 mangled_name = name;
1094 name = gfc_sym_identifier (sym);
1095 mangled_name = gfc_sym_mangled_function_id (sym);
1098 type = gfc_get_function_type (sym);
1099 fndecl = build_decl (FUNCTION_DECL, name, type);
1101 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1102 /* If the return type is a pointer, avoid alias issues by setting
1103 DECL_IS_MALLOC to nonzero. This means that the function should be
1104 treated as if it were a malloc, meaning it returns a pointer that
1106 if (POINTER_TYPE_P (type))
1107 DECL_IS_MALLOC (fndecl) = 1;
1109 /* Set the context of this decl. */
1110 if (0 && sym->ns && sym->ns->proc_name)
1112 /* TODO: Add external decls to the appropriate scope. */
1113 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1117 /* Global declaration, e.g. intrinsic subroutine. */
1118 DECL_CONTEXT (fndecl) = NULL_TREE;
1121 DECL_EXTERNAL (fndecl) = 1;
1123 /* This specifies if a function is globally addressable, i.e. it is
1124 the opposite of declaring static in C. */
1125 TREE_PUBLIC (fndecl) = 1;
1127 /* Set attributes for PURE functions. A call to PURE function in the
1128 Fortran 95 sense is both pure and without side effects in the C
1130 if (sym->attr.pure || sym->attr.elemental)
1132 if (sym->attr.function && !gfc_return_by_reference (sym))
1133 DECL_IS_PURE (fndecl) = 1;
1134 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1135 parameters and don't use alternate returns (is this
1136 allowed?). In that case, calls to them are meaningless, and
1137 can be optimized away. See also in build_function_decl(). */
1138 TREE_SIDE_EFFECTS (fndecl) = 0;
1141 /* Mark non-returning functions. */
1142 if (sym->attr.noreturn)
1143 TREE_THIS_VOLATILE(fndecl) = 1;
1145 sym->backend_decl = fndecl;
1147 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1148 pushdecl_top_level (fndecl);
1154 /* Create a declaration for a procedure. For external functions (in the C
1155 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1156 a master function with alternate entry points. */
1159 build_function_decl (gfc_symbol * sym)
1162 symbol_attribute attr;
1164 gfc_formal_arglist *f;
1166 gcc_assert (!sym->backend_decl);
1167 gcc_assert (!sym->attr.external);
1169 /* Set the line and filename. sym->declared_at seems to point to the
1170 last statement for subroutines, but it'll do for now. */
1171 gfc_set_backend_locus (&sym->declared_at);
1173 /* Allow only one nesting level. Allow public declarations. */
1174 gcc_assert (current_function_decl == NULL_TREE
1175 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1177 type = gfc_get_function_type (sym);
1178 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1180 /* Perform name mangling if this is a top level or module procedure. */
1181 if (current_function_decl == NULL_TREE)
1182 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1184 /* Figure out the return type of the declared function, and build a
1185 RESULT_DECL for it. If this is a subroutine with alternate
1186 returns, build a RESULT_DECL for it. */
1189 result_decl = NULL_TREE;
1190 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1193 if (gfc_return_by_reference (sym))
1194 type = void_type_node;
1197 if (sym->result != sym)
1198 result_decl = gfc_sym_identifier (sym->result);
1200 type = TREE_TYPE (TREE_TYPE (fndecl));
1205 /* Look for alternate return placeholders. */
1206 int has_alternate_returns = 0;
1207 for (f = sym->formal; f; f = f->next)
1211 has_alternate_returns = 1;
1216 if (has_alternate_returns)
1217 type = integer_type_node;
1219 type = void_type_node;
1222 result_decl = build_decl (RESULT_DECL, result_decl, type);
1223 DECL_ARTIFICIAL (result_decl) = 1;
1224 DECL_IGNORED_P (result_decl) = 1;
1225 DECL_CONTEXT (result_decl) = fndecl;
1226 DECL_RESULT (fndecl) = result_decl;
1228 /* Don't call layout_decl for a RESULT_DECL.
1229 layout_decl (result_decl, 0); */
1231 /* If the return type is a pointer, avoid alias issues by setting
1232 DECL_IS_MALLOC to nonzero. This means that the function should be
1233 treated as if it were a malloc, meaning it returns a pointer that
1235 if (POINTER_TYPE_P (type))
1236 DECL_IS_MALLOC (fndecl) = 1;
1238 /* Set up all attributes for the function. */
1239 DECL_CONTEXT (fndecl) = current_function_decl;
1240 DECL_EXTERNAL (fndecl) = 0;
1242 /* This specifies if a function is globally visible, i.e. it is
1243 the opposite of declaring static in C. */
1244 if (DECL_CONTEXT (fndecl) == NULL_TREE
1245 && !sym->attr.entry_master)
1246 TREE_PUBLIC (fndecl) = 1;
1248 /* TREE_STATIC means the function body is defined here. */
1249 TREE_STATIC (fndecl) = 1;
1251 /* Set attributes for PURE functions. A call to a PURE function in the
1252 Fortran 95 sense is both pure and without side effects in the C
1254 if (attr.pure || attr.elemental)
1256 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1257 including a alternate return. In that case it can also be
1258 marked as PURE. See also in gfc_get_extern_function_decl(). */
1259 if (attr.function && !gfc_return_by_reference (sym))
1260 DECL_IS_PURE (fndecl) = 1;
1261 TREE_SIDE_EFFECTS (fndecl) = 0;
1264 /* Layout the function declaration and put it in the binding level
1265 of the current function. */
1268 sym->backend_decl = fndecl;
1272 /* Create the DECL_ARGUMENTS for a procedure. */
1275 create_function_arglist (gfc_symbol * sym)
1278 gfc_formal_arglist *f;
1279 tree typelist, hidden_typelist;
1280 tree arglist, hidden_arglist;
1284 fndecl = sym->backend_decl;
1286 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1287 the new FUNCTION_DECL node. */
1288 arglist = NULL_TREE;
1289 hidden_arglist = NULL_TREE;
1290 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1292 if (sym->attr.entry_master)
1294 type = TREE_VALUE (typelist);
1295 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1297 DECL_CONTEXT (parm) = fndecl;
1298 DECL_ARG_TYPE (parm) = type;
1299 TREE_READONLY (parm) = 1;
1300 gfc_finish_decl (parm);
1301 DECL_ARTIFICIAL (parm) = 1;
1303 arglist = chainon (arglist, parm);
1304 typelist = TREE_CHAIN (typelist);
1307 if (gfc_return_by_reference (sym))
1309 tree type = TREE_VALUE (typelist), length = NULL;
1311 if (sym->ts.type == BT_CHARACTER)
1313 /* Length of character result. */
1314 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1315 gcc_assert (len_type == gfc_charlen_type_node);
1317 length = build_decl (PARM_DECL,
1318 get_identifier (".__result"),
1320 if (!sym->ts.cl->length)
1322 sym->ts.cl->backend_decl = length;
1323 TREE_USED (length) = 1;
1325 gcc_assert (TREE_CODE (length) == PARM_DECL);
1326 DECL_CONTEXT (length) = fndecl;
1327 DECL_ARG_TYPE (length) = len_type;
1328 TREE_READONLY (length) = 1;
1329 DECL_ARTIFICIAL (length) = 1;
1330 gfc_finish_decl (length);
1331 if (sym->ts.cl->backend_decl == NULL
1332 || sym->ts.cl->backend_decl == length)
1337 if (sym->ts.cl->backend_decl == NULL)
1339 tree len = build_decl (VAR_DECL,
1340 get_identifier ("..__result"),
1341 gfc_charlen_type_node);
1342 DECL_ARTIFICIAL (len) = 1;
1343 TREE_USED (len) = 1;
1344 sym->ts.cl->backend_decl = len;
1347 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1348 arg = sym->result ? sym->result : sym;
1349 backend_decl = arg->backend_decl;
1350 /* Temporary clear it, so that gfc_sym_type creates complete
1352 arg->backend_decl = NULL;
1353 type = gfc_sym_type (arg);
1354 arg->backend_decl = backend_decl;
1355 type = build_reference_type (type);
1359 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1361 DECL_CONTEXT (parm) = fndecl;
1362 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1363 TREE_READONLY (parm) = 1;
1364 DECL_ARTIFICIAL (parm) = 1;
1365 gfc_finish_decl (parm);
1367 arglist = chainon (arglist, parm);
1368 typelist = TREE_CHAIN (typelist);
1370 if (sym->ts.type == BT_CHARACTER)
1372 gfc_allocate_lang_decl (parm);
1373 arglist = chainon (arglist, length);
1374 typelist = TREE_CHAIN (typelist);
1378 hidden_typelist = typelist;
1379 for (f = sym->formal; f; f = f->next)
1380 if (f->sym != NULL) /* Ignore alternate returns. */
1381 hidden_typelist = TREE_CHAIN (hidden_typelist);
1383 for (f = sym->formal; f; f = f->next)
1385 char name[GFC_MAX_SYMBOL_LEN + 2];
1387 /* Ignore alternate returns. */
1391 type = TREE_VALUE (typelist);
1393 if (f->sym->ts.type == BT_CHARACTER)
1395 tree len_type = TREE_VALUE (hidden_typelist);
1396 tree length = NULL_TREE;
1397 gcc_assert (len_type == gfc_charlen_type_node);
1399 strcpy (&name[1], f->sym->name);
1401 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1403 hidden_arglist = chainon (hidden_arglist, length);
1404 DECL_CONTEXT (length) = fndecl;
1405 DECL_ARTIFICIAL (length) = 1;
1406 DECL_ARG_TYPE (length) = len_type;
1407 TREE_READONLY (length) = 1;
1408 gfc_finish_decl (length);
1410 /* TODO: Check string lengths when -fbounds-check. */
1412 /* Use the passed value for assumed length variables. */
1413 if (!f->sym->ts.cl->length)
1415 TREE_USED (length) = 1;
1416 if (!f->sym->ts.cl->backend_decl)
1417 f->sym->ts.cl->backend_decl = length;
1420 /* there is already another variable using this
1421 gfc_charlen node, build a new one for this variable
1422 and chain it into the list of gfc_charlens.
1423 This happens for e.g. in the case
1425 since CHARACTER declarations on the same line share
1426 the same gfc_charlen node. */
1429 cl = gfc_get_charlen ();
1430 cl->backend_decl = length;
1431 cl->next = f->sym->ts.cl->next;
1432 f->sym->ts.cl->next = cl;
1437 hidden_typelist = TREE_CHAIN (hidden_typelist);
1439 if (f->sym->ts.cl->backend_decl == NULL
1440 || f->sym->ts.cl->backend_decl == length)
1442 if (f->sym->ts.cl->backend_decl == NULL)
1443 gfc_create_string_length (f->sym);
1445 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1446 if (f->sym->attr.flavor == FL_PROCEDURE)
1447 type = build_pointer_type (gfc_get_function_type (f->sym));
1449 type = gfc_sym_type (f->sym);
1453 /* For non-constant length array arguments, make sure they use
1454 a different type node from TYPE_ARG_TYPES type. */
1455 if (f->sym->attr.dimension
1456 && type == TREE_VALUE (typelist)
1457 && TREE_CODE (type) == POINTER_TYPE
1458 && GFC_ARRAY_TYPE_P (type)
1459 && f->sym->as->type != AS_ASSUMED_SIZE
1460 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1462 if (f->sym->attr.flavor == FL_PROCEDURE)
1463 type = build_pointer_type (gfc_get_function_type (f->sym));
1465 type = gfc_sym_type (f->sym);
1468 /* Build a the argument declaration. */
1469 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1471 /* Fill in arg stuff. */
1472 DECL_CONTEXT (parm) = fndecl;
1473 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1474 /* All implementation args are read-only. */
1475 TREE_READONLY (parm) = 1;
1477 gfc_finish_decl (parm);
1479 f->sym->backend_decl = parm;
1481 arglist = chainon (arglist, parm);
1482 typelist = TREE_CHAIN (typelist);
1485 /* Add the hidden string length parameters. */
1486 arglist = chainon (arglist, hidden_arglist);
1488 gcc_assert (hidden_typelist == NULL_TREE
1489 || TREE_VALUE (hidden_typelist) == void_type_node);
1490 DECL_ARGUMENTS (fndecl) = arglist;
1493 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1496 gfc_gimplify_function (tree fndecl)
1498 struct cgraph_node *cgn;
1500 gimplify_function_tree (fndecl);
1501 dump_function (TDI_generic, fndecl);
1503 /* Generate errors for structured block violations. */
1504 /* ??? Could be done as part of resolve_labels. */
1506 diagnose_omp_structured_block_errors (fndecl);
1508 /* Convert all nested functions to GIMPLE now. We do things in this order
1509 so that items like VLA sizes are expanded properly in the context of the
1510 correct function. */
1511 cgn = cgraph_node (fndecl);
1512 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1513 gfc_gimplify_function (cgn->decl);
1517 /* Do the setup necessary before generating the body of a function. */
1520 trans_function_start (gfc_symbol * sym)
1524 fndecl = sym->backend_decl;
1526 /* Let GCC know the current scope is this function. */
1527 current_function_decl = fndecl;
1529 /* Let the world know what we're about to do. */
1530 announce_function (fndecl);
1532 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1534 /* Create RTL for function declaration. */
1535 rest_of_decl_compilation (fndecl, 1, 0);
1538 /* Create RTL for function definition. */
1539 make_decl_rtl (fndecl);
1541 init_function_start (fndecl);
1543 /* Even though we're inside a function body, we still don't want to
1544 call expand_expr to calculate the size of a variable-sized array.
1545 We haven't necessarily assigned RTL to all variables yet, so it's
1546 not safe to try to expand expressions involving them. */
1547 cfun->x_dont_save_pending_sizes_p = 1;
1549 /* function.c requires a push at the start of the function. */
1553 /* Create thunks for alternate entry points. */
1556 build_entry_thunks (gfc_namespace * ns)
1558 gfc_formal_arglist *formal;
1559 gfc_formal_arglist *thunk_formal;
1561 gfc_symbol *thunk_sym;
1569 /* This should always be a toplevel function. */
1570 gcc_assert (current_function_decl == NULL_TREE);
1572 gfc_get_backend_locus (&old_loc);
1573 for (el = ns->entries; el; el = el->next)
1575 thunk_sym = el->sym;
1577 build_function_decl (thunk_sym);
1578 create_function_arglist (thunk_sym);
1580 trans_function_start (thunk_sym);
1582 thunk_fndecl = thunk_sym->backend_decl;
1584 gfc_start_block (&body);
1586 /* Pass extra parameter identifying this entry point. */
1587 tmp = build_int_cst (gfc_array_index_type, el->id);
1588 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1589 string_args = NULL_TREE;
1591 if (thunk_sym->attr.function)
1593 if (gfc_return_by_reference (ns->proc_name))
1595 tree ref = DECL_ARGUMENTS (current_function_decl);
1596 args = tree_cons (NULL_TREE, ref, args);
1597 if (ns->proc_name->ts.type == BT_CHARACTER)
1598 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1603 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1605 /* Ignore alternate returns. */
1606 if (formal->sym == NULL)
1609 /* We don't have a clever way of identifying arguments, so resort to
1610 a brute-force search. */
1611 for (thunk_formal = thunk_sym->formal;
1613 thunk_formal = thunk_formal->next)
1615 if (thunk_formal->sym == formal->sym)
1621 /* Pass the argument. */
1622 DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
1623 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1625 if (formal->sym->ts.type == BT_CHARACTER)
1627 tmp = thunk_formal->sym->ts.cl->backend_decl;
1628 string_args = tree_cons (NULL_TREE, tmp, string_args);
1633 /* Pass NULL for a missing argument. */
1634 args = tree_cons (NULL_TREE, null_pointer_node, args);
1635 if (formal->sym->ts.type == BT_CHARACTER)
1637 tmp = build_int_cst (gfc_charlen_type_node, 0);
1638 string_args = tree_cons (NULL_TREE, tmp, string_args);
1643 /* Call the master function. */
1644 args = nreverse (args);
1645 args = chainon (args, nreverse (string_args));
1646 tmp = ns->proc_name->backend_decl;
1647 tmp = build_function_call_expr (tmp, args);
1648 if (ns->proc_name->attr.mixed_entry_master)
1650 tree union_decl, field;
1651 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1653 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1654 TREE_TYPE (master_type));
1655 DECL_ARTIFICIAL (union_decl) = 1;
1656 DECL_EXTERNAL (union_decl) = 0;
1657 TREE_PUBLIC (union_decl) = 0;
1658 TREE_USED (union_decl) = 1;
1659 layout_decl (union_decl, 0);
1660 pushdecl (union_decl);
1662 DECL_CONTEXT (union_decl) = current_function_decl;
1663 tmp = build2 (MODIFY_EXPR,
1664 TREE_TYPE (union_decl),
1666 gfc_add_expr_to_block (&body, tmp);
1668 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1669 field; field = TREE_CHAIN (field))
1670 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1671 thunk_sym->result->name) == 0)
1673 gcc_assert (field != NULL_TREE);
1674 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), union_decl, field,
1676 tmp = build2 (MODIFY_EXPR,
1677 TREE_TYPE (DECL_RESULT (current_function_decl)),
1678 DECL_RESULT (current_function_decl), tmp);
1679 tmp = build1_v (RETURN_EXPR, tmp);
1681 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1684 tmp = build2 (MODIFY_EXPR,
1685 TREE_TYPE (DECL_RESULT (current_function_decl)),
1686 DECL_RESULT (current_function_decl), tmp);
1687 tmp = build1_v (RETURN_EXPR, tmp);
1689 gfc_add_expr_to_block (&body, tmp);
1691 /* Finish off this function and send it for code generation. */
1692 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1694 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1696 /* Output the GENERIC tree. */
1697 dump_function (TDI_original, thunk_fndecl);
1699 /* Store the end of the function, so that we get good line number
1700 info for the epilogue. */
1701 cfun->function_end_locus = input_location;
1703 /* We're leaving the context of this function, so zap cfun.
1704 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1705 tree_rest_of_compilation. */
1708 current_function_decl = NULL_TREE;
1710 gfc_gimplify_function (thunk_fndecl);
1711 cgraph_finalize_function (thunk_fndecl, false);
1713 /* We share the symbols in the formal argument list with other entry
1714 points and the master function. Clear them so that they are
1715 recreated for each function. */
1716 for (formal = thunk_sym->formal; formal; formal = formal->next)
1717 if (formal->sym != NULL) /* Ignore alternate returns. */
1719 formal->sym->backend_decl = NULL_TREE;
1720 if (formal->sym->ts.type == BT_CHARACTER)
1721 formal->sym->ts.cl->backend_decl = NULL_TREE;
1724 if (thunk_sym->attr.function)
1726 if (thunk_sym->ts.type == BT_CHARACTER)
1727 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1728 if (thunk_sym->result->ts.type == BT_CHARACTER)
1729 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1733 gfc_set_backend_locus (&old_loc);
1737 /* Create a decl for a function, and create any thunks for alternate entry
1741 gfc_create_function_decl (gfc_namespace * ns)
1743 /* Create a declaration for the master function. */
1744 build_function_decl (ns->proc_name);
1746 /* Compile the entry thunks. */
1748 build_entry_thunks (ns);
1750 /* Now create the read argument list. */
1751 create_function_arglist (ns->proc_name);
1754 /* Return the decl used to hold the function return value. If
1755 parent_flag is set, the context is the parent_scope. */
1758 gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
1762 tree this_fake_result_decl;
1763 tree this_function_decl;
1765 char name[GFC_MAX_SYMBOL_LEN + 10];
1769 this_fake_result_decl = parent_fake_result_decl;
1770 this_function_decl = DECL_CONTEXT (current_function_decl);
1774 this_fake_result_decl = current_fake_result_decl;
1775 this_function_decl = current_function_decl;
1779 && sym->ns->proc_name->backend_decl == this_function_decl
1780 && sym->ns->proc_name->attr.entry_master
1781 && sym != sym->ns->proc_name)
1784 if (this_fake_result_decl != NULL)
1785 for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
1786 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1789 return TREE_VALUE (t);
1790 decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
1793 this_fake_result_decl = parent_fake_result_decl;
1795 this_fake_result_decl = current_fake_result_decl;
1797 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1801 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1802 field; field = TREE_CHAIN (field))
1803 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1807 gcc_assert (field != NULL_TREE);
1808 decl = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field,
1812 var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
1814 gfc_add_decl_to_parent_function (var);
1816 gfc_add_decl_to_function (var);
1818 SET_DECL_VALUE_EXPR (var, decl);
1819 DECL_HAS_VALUE_EXPR_P (var) = 1;
1820 GFC_DECL_RESULT (var) = 1;
1822 TREE_CHAIN (this_fake_result_decl)
1823 = tree_cons (get_identifier (sym->name), var,
1824 TREE_CHAIN (this_fake_result_decl));
1828 if (this_fake_result_decl != NULL_TREE)
1829 return TREE_VALUE (this_fake_result_decl);
1831 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1836 if (sym->ts.type == BT_CHARACTER)
1838 if (sym->ts.cl->backend_decl == NULL_TREE)
1839 length = gfc_create_string_length (sym);
1841 length = sym->ts.cl->backend_decl;
1842 if (TREE_CODE (length) == VAR_DECL
1843 && DECL_CONTEXT (length) == NULL_TREE)
1844 gfc_add_decl_to_function (length);
1847 if (gfc_return_by_reference (sym))
1849 decl = DECL_ARGUMENTS (this_function_decl);
1851 if (sym->ns->proc_name->backend_decl == this_function_decl
1852 && sym->ns->proc_name->attr.entry_master)
1853 decl = TREE_CHAIN (decl);
1855 TREE_USED (decl) = 1;
1857 decl = gfc_build_dummy_array_decl (sym, decl);
1861 sprintf (name, "__result_%.20s",
1862 IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
1864 if (!sym->attr.mixed_entry_master && sym->attr.function)
1865 decl = build_decl (VAR_DECL, get_identifier (name),
1866 gfc_sym_type (sym));
1868 decl = build_decl (VAR_DECL, get_identifier (name),
1869 TREE_TYPE (TREE_TYPE (this_function_decl)));
1870 DECL_ARTIFICIAL (decl) = 1;
1871 DECL_EXTERNAL (decl) = 0;
1872 TREE_PUBLIC (decl) = 0;
1873 TREE_USED (decl) = 1;
1874 GFC_DECL_RESULT (decl) = 1;
1875 TREE_ADDRESSABLE (decl) = 1;
1877 layout_decl (decl, 0);
1880 gfc_add_decl_to_parent_function (decl);
1882 gfc_add_decl_to_function (decl);
1886 parent_fake_result_decl = build_tree_list (NULL, decl);
1888 current_fake_result_decl = build_tree_list (NULL, decl);
1894 /* Builds a function decl. The remaining parameters are the types of the
1895 function arguments. Negative nargs indicates a varargs function. */
1898 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1907 /* Library functions must be declared with global scope. */
1908 gcc_assert (current_function_decl == NULL_TREE);
1910 va_start (p, nargs);
1913 /* Create a list of the argument types. */
1914 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1916 argtype = va_arg (p, tree);
1917 arglist = gfc_chainon_list (arglist, argtype);
1922 /* Terminate the list. */
1923 arglist = gfc_chainon_list (arglist, void_type_node);
1926 /* Build the function type and decl. */
1927 fntype = build_function_type (rettype, arglist);
1928 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1930 /* Mark this decl as external. */
1931 DECL_EXTERNAL (fndecl) = 1;
1932 TREE_PUBLIC (fndecl) = 1;
1938 rest_of_decl_compilation (fndecl, 1, 0);
1944 gfc_build_intrinsic_function_decls (void)
1946 tree gfc_int4_type_node = gfc_get_int_type (4);
1947 tree gfc_int8_type_node = gfc_get_int_type (8);
1948 tree gfc_int16_type_node = gfc_get_int_type (16);
1949 tree gfc_logical4_type_node = gfc_get_logical_type (4);
1950 tree gfc_real4_type_node = gfc_get_real_type (4);
1951 tree gfc_real8_type_node = gfc_get_real_type (8);
1952 tree gfc_real10_type_node = gfc_get_real_type (10);
1953 tree gfc_real16_type_node = gfc_get_real_type (16);
1954 tree gfc_complex4_type_node = gfc_get_complex_type (4);
1955 tree gfc_complex8_type_node = gfc_get_complex_type (8);
1956 tree gfc_complex10_type_node = gfc_get_complex_type (10);
1957 tree gfc_complex16_type_node = gfc_get_complex_type (16);
1958 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
1960 /* String functions. */
1961 gfor_fndecl_compare_string =
1962 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
1965 gfc_charlen_type_node, pchar_type_node,
1966 gfc_charlen_type_node, pchar_type_node);
1968 gfor_fndecl_concat_string =
1969 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
1972 gfc_charlen_type_node, pchar_type_node,
1973 gfc_charlen_type_node, pchar_type_node,
1974 gfc_charlen_type_node, pchar_type_node);
1976 gfor_fndecl_string_len_trim =
1977 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
1979 2, gfc_charlen_type_node,
1982 gfor_fndecl_string_index =
1983 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
1985 5, gfc_charlen_type_node, pchar_type_node,
1986 gfc_charlen_type_node, pchar_type_node,
1987 gfc_logical4_type_node);
1989 gfor_fndecl_string_scan =
1990 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
1992 5, gfc_charlen_type_node, pchar_type_node,
1993 gfc_charlen_type_node, pchar_type_node,
1994 gfc_logical4_type_node);
1996 gfor_fndecl_string_verify =
1997 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
1999 5, gfc_charlen_type_node, pchar_type_node,
2000 gfc_charlen_type_node, pchar_type_node,
2001 gfc_logical4_type_node);
2003 gfor_fndecl_string_trim =
2004 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
2007 build_pointer_type (gfc_charlen_type_node),
2009 gfc_charlen_type_node,
2012 gfor_fndecl_ttynam =
2013 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
2017 gfc_charlen_type_node,
2018 gfc_c_int_type_node);
2021 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
2025 gfc_charlen_type_node);
2028 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
2032 gfc_charlen_type_node,
2033 gfc_int8_type_node);
2035 gfor_fndecl_adjustl =
2036 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
2040 gfc_charlen_type_node, pchar_type_node);
2042 gfor_fndecl_adjustr =
2043 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
2047 gfc_charlen_type_node, pchar_type_node);
2049 gfor_fndecl_si_kind =
2050 gfc_build_library_function_decl (get_identifier
2051 (PREFIX("selected_int_kind")),
2056 gfor_fndecl_sr_kind =
2057 gfc_build_library_function_decl (get_identifier
2058 (PREFIX("selected_real_kind")),
2063 /* Power functions. */
2065 tree ctype, rtype, itype, jtype;
2066 int rkind, ikind, jkind;
2069 static int ikinds[NIKINDS] = {4, 8, 16};
2070 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2071 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2073 for (ikind=0; ikind < NIKINDS; ikind++)
2075 itype = gfc_get_int_type (ikinds[ikind]);
2077 for (jkind=0; jkind < NIKINDS; jkind++)
2079 jtype = gfc_get_int_type (ikinds[jkind]);
2082 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2084 gfor_fndecl_math_powi[jkind][ikind].integer =
2085 gfc_build_library_function_decl (get_identifier (name),
2086 jtype, 2, jtype, itype);
2087 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
2091 for (rkind = 0; rkind < NRKINDS; rkind ++)
2093 rtype = gfc_get_real_type (rkinds[rkind]);
2096 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2098 gfor_fndecl_math_powi[rkind][ikind].real =
2099 gfc_build_library_function_decl (get_identifier (name),
2100 rtype, 2, rtype, itype);
2101 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
2104 ctype = gfc_get_complex_type (rkinds[rkind]);
2107 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2109 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2110 gfc_build_library_function_decl (get_identifier (name),
2111 ctype, 2,ctype, itype);
2112 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
2120 gfor_fndecl_math_cpowf =
2121 gfc_build_library_function_decl (get_identifier ("cpowf"),
2122 gfc_complex4_type_node,
2123 1, gfc_complex4_type_node);
2124 gfor_fndecl_math_cpow =
2125 gfc_build_library_function_decl (get_identifier ("cpow"),
2126 gfc_complex8_type_node,
2127 1, gfc_complex8_type_node);
2128 if (gfc_complex10_type_node)
2129 gfor_fndecl_math_cpowl10 =
2130 gfc_build_library_function_decl (get_identifier ("cpowl"),
2131 gfc_complex10_type_node, 1,
2132 gfc_complex10_type_node);
2133 if (gfc_complex16_type_node)
2134 gfor_fndecl_math_cpowl16 =
2135 gfc_build_library_function_decl (get_identifier ("cpowl"),
2136 gfc_complex16_type_node, 1,
2137 gfc_complex16_type_node);
2139 gfor_fndecl_math_ishftc4 =
2140 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2142 3, gfc_int4_type_node,
2143 gfc_int4_type_node, gfc_int4_type_node);
2144 gfor_fndecl_math_ishftc8 =
2145 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2147 3, gfc_int8_type_node,
2148 gfc_int4_type_node, gfc_int4_type_node);
2149 if (gfc_int16_type_node)
2150 gfor_fndecl_math_ishftc16 =
2151 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2152 gfc_int16_type_node, 3,
2153 gfc_int16_type_node,
2155 gfc_int4_type_node);
2157 gfor_fndecl_math_exponent4 =
2158 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
2160 1, gfc_real4_type_node);
2161 gfor_fndecl_math_exponent8 =
2162 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
2164 1, gfc_real8_type_node);
2165 if (gfc_real10_type_node)
2166 gfor_fndecl_math_exponent10 =
2167 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r10")),
2168 gfc_int4_type_node, 1,
2169 gfc_real10_type_node);
2170 if (gfc_real16_type_node)
2171 gfor_fndecl_math_exponent16 =
2172 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r16")),
2173 gfc_int4_type_node, 1,
2174 gfc_real16_type_node);
2176 /* BLAS functions. */
2178 tree pint = build_pointer_type (gfc_c_int_type_node);
2179 tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
2180 tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
2181 tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
2182 tree pz = build_pointer_type
2183 (gfc_get_complex_type (gfc_default_double_kind));
2185 gfor_fndecl_sgemm = gfc_build_library_function_decl
2187 (gfc_option.flag_underscoring ? "sgemm_"
2189 void_type_node, 15, pchar_type_node,
2190 pchar_type_node, pint, pint, pint, ps, ps, pint,
2191 ps, pint, ps, ps, pint, gfc_c_int_type_node,
2192 gfc_c_int_type_node);
2193 gfor_fndecl_dgemm = gfc_build_library_function_decl
2195 (gfc_option.flag_underscoring ? "dgemm_"
2197 void_type_node, 15, pchar_type_node,
2198 pchar_type_node, pint, pint, pint, pd, pd, pint,
2199 pd, pint, pd, pd, pint, gfc_c_int_type_node,
2200 gfc_c_int_type_node);
2201 gfor_fndecl_cgemm = gfc_build_library_function_decl
2203 (gfc_option.flag_underscoring ? "cgemm_"
2205 void_type_node, 15, pchar_type_node,
2206 pchar_type_node, pint, pint, pint, pc, pc, pint,
2207 pc, pint, pc, pc, pint, gfc_c_int_type_node,
2208 gfc_c_int_type_node);
2209 gfor_fndecl_zgemm = gfc_build_library_function_decl
2211 (gfc_option.flag_underscoring ? "zgemm_"
2213 void_type_node, 15, pchar_type_node,
2214 pchar_type_node, pint, pint, pint, pz, pz, pint,
2215 pz, pint, pz, pz, pint, gfc_c_int_type_node,
2216 gfc_c_int_type_node);
2219 /* Other functions. */
2221 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2222 gfc_array_index_type,
2223 1, pvoid_type_node);
2225 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2226 gfc_array_index_type,
2228 gfc_array_index_type);
2231 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2237 /* Make prototypes for runtime library functions. */
2240 gfc_build_builtin_function_decls (void)
2242 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2243 tree gfc_int4_type_node = gfc_get_int_type (4);
2244 tree gfc_int8_type_node = gfc_get_int_type (8);
2245 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2246 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
2248 gfor_fndecl_internal_realloc =
2249 gfc_build_library_function_decl (get_identifier
2250 (PREFIX("internal_realloc")),
2251 pvoid_type_node, 2, pvoid_type_node,
2252 gfc_int4_type_node);
2254 gfor_fndecl_internal_realloc64 =
2255 gfc_build_library_function_decl (get_identifier
2256 (PREFIX("internal_realloc64")),
2257 pvoid_type_node, 2, pvoid_type_node,
2258 gfc_int8_type_node);
2260 gfor_fndecl_allocate =
2261 gfc_build_library_function_decl (get_identifier (PREFIX("allocate")),
2263 gfc_int4_type_node, gfc_pint4_type_node);
2264 DECL_IS_MALLOC (gfor_fndecl_allocate) = 1;
2266 gfor_fndecl_allocate64 =
2267 gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")),
2269 gfc_int8_type_node, gfc_pint4_type_node);
2270 DECL_IS_MALLOC (gfor_fndecl_allocate64) = 1;
2272 gfor_fndecl_allocate_array =
2273 gfc_build_library_function_decl (get_identifier (PREFIX("allocate_array")),
2274 pvoid_type_node, 3, pvoid_type_node,
2275 gfc_int4_type_node, gfc_pint4_type_node);
2276 DECL_IS_MALLOC (gfor_fndecl_allocate_array) = 1;
2278 gfor_fndecl_allocate64_array =
2279 gfc_build_library_function_decl (get_identifier (PREFIX("allocate64_array")),
2280 pvoid_type_node, 3, pvoid_type_node,
2281 gfc_int8_type_node, gfc_pint4_type_node);
2282 DECL_IS_MALLOC (gfor_fndecl_allocate64_array) = 1;
2284 gfor_fndecl_deallocate =
2285 gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")),
2286 void_type_node, 2, pvoid_type_node,
2287 gfc_pint4_type_node);
2289 gfor_fndecl_stop_numeric =
2290 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2291 void_type_node, 1, gfc_int4_type_node);
2293 /* Stop doesn't return. */
2294 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2296 gfor_fndecl_stop_string =
2297 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2298 void_type_node, 2, pchar_type_node,
2299 gfc_int4_type_node);
2300 /* Stop doesn't return. */
2301 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2303 gfor_fndecl_pause_numeric =
2304 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2305 void_type_node, 1, gfc_int4_type_node);
2307 gfor_fndecl_pause_string =
2308 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2309 void_type_node, 2, pchar_type_node,
2310 gfc_int4_type_node);
2312 gfor_fndecl_select_string =
2313 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2314 pvoid_type_node, 0);
2316 gfor_fndecl_runtime_error =
2317 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2318 void_type_node, 1, pchar_type_node);
2319 /* The runtime_error function does not return. */
2320 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2322 gfor_fndecl_runtime_error_at =
2323 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error_at")),
2324 void_type_node, 2, pchar_type_node,
2326 /* The runtime_error_at function does not return. */
2327 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
2329 gfor_fndecl_generate_error =
2330 gfc_build_library_function_decl (get_identifier (PREFIX("generate_error")),
2331 void_type_node, 3, pvoid_type_node,
2332 gfc_c_int_type_node, pchar_type_node);
2334 gfor_fndecl_os_error =
2335 gfc_build_library_function_decl (get_identifier (PREFIX("os_error")),
2336 void_type_node, 1, pchar_type_node);
2337 /* The runtime_error function does not return. */
2338 TREE_THIS_VOLATILE (gfor_fndecl_os_error) = 1;
2340 gfor_fndecl_set_fpe =
2341 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2342 void_type_node, 1, gfc_c_int_type_node);
2344 gfor_fndecl_set_std =
2345 gfc_build_library_function_decl (get_identifier (PREFIX("set_std")),
2352 gfc_int4_type_node);
2354 gfor_fndecl_set_convert =
2355 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2356 void_type_node, 1, gfc_c_int_type_node);
2358 gfor_fndecl_set_record_marker =
2359 gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2360 void_type_node, 1, gfc_c_int_type_node);
2362 gfor_fndecl_set_max_subrecord_length =
2363 gfc_build_library_function_decl (get_identifier (PREFIX("set_max_subrecord_length")),
2364 void_type_node, 1, gfc_c_int_type_node);
2366 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2367 get_identifier (PREFIX("internal_pack")),
2368 pvoid_type_node, 1, pvoid_type_node);
2370 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2371 get_identifier (PREFIX("internal_unpack")),
2372 pvoid_type_node, 1, pvoid_type_node);
2374 gfor_fndecl_associated =
2375 gfc_build_library_function_decl (
2376 get_identifier (PREFIX("associated")),
2377 gfc_logical4_type_node,
2382 gfc_build_intrinsic_function_decls ();
2383 gfc_build_intrinsic_lib_fndecls ();
2384 gfc_build_io_library_fndecls ();
2388 /* Evaluate the length of dummy character variables. */
2391 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2395 gfc_finish_decl (cl->backend_decl);
2397 gfc_start_block (&body);
2399 /* Evaluate the string length expression. */
2400 gfc_trans_init_string_length (cl, &body);
2402 gfc_trans_vla_type_sizes (sym, &body);
2404 gfc_add_expr_to_block (&body, fnbody);
2405 return gfc_finish_block (&body);
2409 /* Allocate and cleanup an automatic character variable. */
2412 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2418 gcc_assert (sym->backend_decl);
2419 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2421 gfc_start_block (&body);
2423 /* Evaluate the string length expression. */
2424 gfc_trans_init_string_length (sym->ts.cl, &body);
2426 gfc_trans_vla_type_sizes (sym, &body);
2428 decl = sym->backend_decl;
2430 /* Emit a DECL_EXPR for this variable, which will cause the
2431 gimplifier to allocate storage, and all that good stuff. */
2432 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2433 gfc_add_expr_to_block (&body, tmp);
2435 gfc_add_expr_to_block (&body, fnbody);
2436 return gfc_finish_block (&body);
2439 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2442 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2446 gcc_assert (sym->backend_decl);
2447 gfc_start_block (&body);
2449 /* Set the initial value to length. See the comments in
2450 function gfc_add_assign_aux_vars in this file. */
2451 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2452 build_int_cst (NULL_TREE, -2));
2454 gfc_add_expr_to_block (&body, fnbody);
2455 return gfc_finish_block (&body);
2459 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2461 tree t = *tp, var, val;
2463 if (t == NULL || t == error_mark_node)
2465 if (TREE_CONSTANT (t) || DECL_P (t))
2468 if (TREE_CODE (t) == SAVE_EXPR)
2470 if (SAVE_EXPR_RESOLVED_P (t))
2472 *tp = TREE_OPERAND (t, 0);
2475 val = TREE_OPERAND (t, 0);
2480 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2481 gfc_add_decl_to_function (var);
2482 gfc_add_modify_expr (body, var, val);
2483 if (TREE_CODE (t) == SAVE_EXPR)
2484 TREE_OPERAND (t, 0) = var;
2489 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2493 if (type == NULL || type == error_mark_node)
2496 type = TYPE_MAIN_VARIANT (type);
2498 if (TREE_CODE (type) == INTEGER_TYPE)
2500 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2501 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2503 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2505 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2506 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2509 else if (TREE_CODE (type) == ARRAY_TYPE)
2511 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2512 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2513 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2514 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2516 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2518 TYPE_SIZE (t) = TYPE_SIZE (type);
2519 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2524 /* Make sure all type sizes and array domains are either constant,
2525 or variable or parameter decls. This is a simplified variant
2526 of gimplify_type_sizes, but we can't use it here, as none of the
2527 variables in the expressions have been gimplified yet.
2528 As type sizes and domains for various variable length arrays
2529 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2530 time, without this routine gimplify_type_sizes in the middle-end
2531 could result in the type sizes being gimplified earlier than where
2532 those variables are initialized. */
2535 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2537 tree type = TREE_TYPE (sym->backend_decl);
2539 if (TREE_CODE (type) == FUNCTION_TYPE
2540 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2542 if (! current_fake_result_decl)
2545 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2548 while (POINTER_TYPE_P (type))
2549 type = TREE_TYPE (type);
2551 if (GFC_DESCRIPTOR_TYPE_P (type))
2553 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2555 while (POINTER_TYPE_P (etype))
2556 etype = TREE_TYPE (etype);
2558 gfc_trans_vla_type_sizes_1 (etype, body);
2561 gfc_trans_vla_type_sizes_1 (type, body);
2565 /* Generate function entry and exit code, and add it to the function body.
2567 Allocation and initialization of array variables.
2568 Allocation of character string variables.
2569 Initialization and possibly repacking of dummy arrays.
2570 Initialization of ASSIGN statement auxiliary variable. */
2573 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2577 gfc_formal_arglist *f;
2579 bool seen_trans_deferred_array = false;
2581 /* Deal with implicit return variables. Explicit return variables will
2582 already have been added. */
2583 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2585 if (!current_fake_result_decl)
2587 gfc_entry_list *el = NULL;
2588 if (proc_sym->attr.entry_master)
2590 for (el = proc_sym->ns->entries; el; el = el->next)
2591 if (el->sym != el->sym->result)
2595 warning (0, "Function does not return a value");
2597 else if (proc_sym->as)
2599 tree result = TREE_VALUE (current_fake_result_decl);
2600 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2602 /* An automatic character length, pointer array result. */
2603 if (proc_sym->ts.type == BT_CHARACTER
2604 && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2605 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2608 else if (proc_sym->ts.type == BT_CHARACTER)
2610 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2611 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2615 gcc_assert (gfc_option.flag_f2c
2616 && proc_sym->ts.type == BT_COMPLEX);
2619 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2621 bool sym_has_alloc_comp = (sym->ts.type == BT_DERIVED)
2622 && sym->ts.derived->attr.alloc_comp;
2623 if (sym->attr.dimension)
2625 switch (sym->as->type)
2628 if (sym->attr.dummy || sym->attr.result)
2630 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2631 else if (sym->attr.pointer || sym->attr.allocatable)
2633 if (TREE_STATIC (sym->backend_decl))
2634 gfc_trans_static_array_pointer (sym);
2637 seen_trans_deferred_array = true;
2638 fnbody = gfc_trans_deferred_array (sym, fnbody);
2643 if (sym_has_alloc_comp)
2645 seen_trans_deferred_array = true;
2646 fnbody = gfc_trans_deferred_array (sym, fnbody);
2649 gfc_get_backend_locus (&loc);
2650 gfc_set_backend_locus (&sym->declared_at);
2651 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2653 gfc_set_backend_locus (&loc);
2657 case AS_ASSUMED_SIZE:
2658 /* Must be a dummy parameter. */
2659 gcc_assert (sym->attr.dummy);
2661 /* We should always pass assumed size arrays the g77 way. */
2662 fnbody = gfc_trans_g77_array (sym, fnbody);
2665 case AS_ASSUMED_SHAPE:
2666 /* Must be a dummy parameter. */
2667 gcc_assert (sym->attr.dummy);
2669 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2674 seen_trans_deferred_array = true;
2675 fnbody = gfc_trans_deferred_array (sym, fnbody);
2681 if (sym_has_alloc_comp && !seen_trans_deferred_array)
2682 fnbody = gfc_trans_deferred_array (sym, fnbody);
2684 else if (sym_has_alloc_comp)
2685 fnbody = gfc_trans_deferred_array (sym, fnbody);
2686 else if (sym->ts.type == BT_CHARACTER)
2688 gfc_get_backend_locus (&loc);
2689 gfc_set_backend_locus (&sym->declared_at);
2690 if (sym->attr.dummy || sym->attr.result)
2691 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2693 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2694 gfc_set_backend_locus (&loc);
2696 else if (sym->attr.assign)
2698 gfc_get_backend_locus (&loc);
2699 gfc_set_backend_locus (&sym->declared_at);
2700 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2701 gfc_set_backend_locus (&loc);
2707 gfc_init_block (&body);
2709 for (f = proc_sym->formal; f; f = f->next)
2710 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2712 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2713 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2714 gfc_trans_vla_type_sizes (f->sym, &body);
2717 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2718 && current_fake_result_decl != NULL)
2720 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2721 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2722 gfc_trans_vla_type_sizes (proc_sym, &body);
2725 gfc_add_expr_to_block (&body, fnbody);
2726 return gfc_finish_block (&body);
2730 /* Output an initialized decl for a module variable. */
2733 gfc_create_module_variable (gfc_symbol * sym)
2737 /* Module functions with alternate entries are dealt with later and
2738 would get caught by the next condition. */
2739 if (sym->attr.entry)
2742 /* Only output variables and array valued parameters. */
2743 if (sym->attr.flavor != FL_VARIABLE
2744 && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0))
2747 /* Don't generate variables from other modules. Variables from
2748 COMMONs will already have been generated. */
2749 if (sym->attr.use_assoc || sym->attr.in_common)
2752 /* Equivalenced variables arrive here after creation. */
2753 if (sym->backend_decl
2754 && (sym->equiv_built || sym->attr.in_equivalence))
2757 if (sym->backend_decl)
2758 internal_error ("backend decl for module variable %s already exists",
2761 /* We always want module variables to be created. */
2762 sym->attr.referenced = 1;
2763 /* Create the decl. */
2764 decl = gfc_get_symbol_decl (sym);
2766 /* Create the variable. */
2768 rest_of_decl_compilation (decl, 1, 0);
2770 /* Also add length of strings. */
2771 if (sym->ts.type == BT_CHARACTER)
2775 length = sym->ts.cl->backend_decl;
2776 if (!INTEGER_CST_P (length))
2779 rest_of_decl_compilation (length, 1, 0);
2785 /* Generate all the required code for module variables. */
2788 gfc_generate_module_vars (gfc_namespace * ns)
2790 module_namespace = ns;
2792 /* Check if the frontend left the namespace in a reasonable state. */
2793 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2795 /* Generate COMMON blocks. */
2796 gfc_trans_common (ns);
2798 /* Create decls for all the module variables. */
2799 gfc_traverse_ns (ns, gfc_create_module_variable);
2803 gfc_generate_contained_functions (gfc_namespace * parent)
2807 /* We create all the prototypes before generating any code. */
2808 for (ns = parent->contained; ns; ns = ns->sibling)
2810 /* Skip namespaces from used modules. */
2811 if (ns->parent != parent)
2814 gfc_create_function_decl (ns);
2817 for (ns = parent->contained; ns; ns = ns->sibling)
2819 /* Skip namespaces from used modules. */
2820 if (ns->parent != parent)
2823 gfc_generate_function_code (ns);
2828 /* Drill down through expressions for the array specification bounds and
2829 character length calling generate_local_decl for all those variables
2830 that have not already been declared. */
2833 generate_local_decl (gfc_symbol *);
2836 generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
2838 gfc_actual_arglist *arg;
2845 switch (e->expr_type)
2848 for (arg = e->value.function.actual; arg; arg = arg->next)
2849 generate_expr_decls (sym, arg->expr);
2852 /* If the variable is not the same as the dependent, 'sym', and
2853 it is not marked as being declared and it is in the same
2854 namespace as 'sym', add it to the local declarations. */
2856 if (sym == e->symtree->n.sym
2857 || e->symtree->n.sym->mark
2858 || e->symtree->n.sym->ns != sym->ns)
2861 generate_local_decl (e->symtree->n.sym);
2865 generate_expr_decls (sym, e->value.op.op1);
2866 generate_expr_decls (sym, e->value.op.op2);
2875 for (ref = e->ref; ref; ref = ref->next)
2880 for (i = 0; i < ref->u.ar.dimen; i++)
2882 generate_expr_decls (sym, ref->u.ar.start[i]);
2883 generate_expr_decls (sym, ref->u.ar.end[i]);
2884 generate_expr_decls (sym, ref->u.ar.stride[i]);
2889 generate_expr_decls (sym, ref->u.ss.start);
2890 generate_expr_decls (sym, ref->u.ss.end);
2894 if (ref->u.c.component->ts.type == BT_CHARACTER
2895 && ref->u.c.component->ts.cl->length->expr_type
2897 generate_expr_decls (sym, ref->u.c.component->ts.cl->length);
2899 if (ref->u.c.component->as)
2900 for (i = 0; i < ref->u.c.component->as->rank; i++)
2902 generate_expr_decls (sym, ref->u.c.component->as->lower[i]);
2903 generate_expr_decls (sym, ref->u.c.component->as->upper[i]);
2912 /* Check for dependencies in the character length and array spec. */
2915 generate_dependency_declarations (gfc_symbol *sym)
2919 if (sym->ts.type == BT_CHARACTER
2920 && sym->ts.cl->length->expr_type != EXPR_CONSTANT)
2921 generate_expr_decls (sym, sym->ts.cl->length);
2923 if (sym->as && sym->as->rank)
2925 for (i = 0; i < sym->as->rank; i++)
2927 generate_expr_decls (sym, sym->as->lower[i]);
2928 generate_expr_decls (sym, sym->as->upper[i]);
2934 /* Generate decls for all local variables. We do this to ensure correct
2935 handling of expressions which only appear in the specification of
2939 generate_local_decl (gfc_symbol * sym)
2941 if (sym->attr.flavor == FL_VARIABLE)
2943 /* Check for dependencies in the array specification and string
2944 length, adding the necessary declarations to the function. We
2945 mark the symbol now, as well as in traverse_ns, to prevent
2946 getting stuck in a circular dependency. */
2948 if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
2949 generate_dependency_declarations (sym);
2951 if (sym->attr.referenced)
2952 gfc_get_symbol_decl (sym);
2953 else if (sym->attr.dummy && warn_unused_parameter)
2954 gfc_warning ("Unused parameter %s declared at %L", sym->name,
2956 /* Warn for unused variables, but not if they're inside a common
2957 block or are use-associated. */
2958 else if (warn_unused_variable
2959 && !(sym->attr.in_common || sym->attr.use_assoc))
2960 gfc_warning ("Unused variable %s declared at %L", sym->name,
2962 /* For variable length CHARACTER parameters, the PARM_DECL already
2963 references the length variable, so force gfc_get_symbol_decl
2964 even when not referenced. If optimize > 0, it will be optimized
2965 away anyway. But do this only after emitting -Wunused-parameter
2966 warning if requested. */
2967 if (sym->attr.dummy && ! sym->attr.referenced
2968 && sym->ts.type == BT_CHARACTER
2969 && sym->ts.cl->backend_decl != NULL
2970 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2972 sym->attr.referenced = 1;
2973 gfc_get_symbol_decl (sym);
2979 generate_local_vars (gfc_namespace * ns)
2981 gfc_traverse_ns (ns, generate_local_decl);
2985 /* Generate a switch statement to jump to the correct entry point. Also
2986 creates the label decls for the entry points. */
2989 gfc_trans_entry_master_switch (gfc_entry_list * el)
2996 gfc_init_block (&block);
2997 for (; el; el = el->next)
2999 /* Add the case label. */
3000 label = gfc_build_label_decl (NULL_TREE);
3001 val = build_int_cst (gfc_array_index_type, el->id);
3002 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3003 gfc_add_expr_to_block (&block, tmp);
3005 /* And jump to the actual entry point. */
3006 label = gfc_build_label_decl (NULL_TREE);
3007 tmp = build1_v (GOTO_EXPR, label);
3008 gfc_add_expr_to_block (&block, tmp);
3010 /* Save the label decl. */
3013 tmp = gfc_finish_block (&block);
3014 /* The first argument selects the entry point. */
3015 val = DECL_ARGUMENTS (current_function_decl);
3016 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3021 /* Generate code for a function. */
3024 gfc_generate_function_code (gfc_namespace * ns)
3037 sym = ns->proc_name;
3039 /* Check that the frontend isn't still using this. */
3040 gcc_assert (sym->tlink == NULL);
3043 /* Create the declaration for functions with global scope. */
3044 if (!sym->backend_decl)
3045 gfc_create_function_decl (ns);
3047 fndecl = sym->backend_decl;
3048 old_context = current_function_decl;
3052 push_function_context ();
3053 saved_parent_function_decls = saved_function_decls;
3054 saved_function_decls = NULL_TREE;
3057 trans_function_start (sym);
3059 gfc_start_block (&block);
3061 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
3063 /* Copy length backend_decls to all entry point result
3068 gfc_conv_const_charlen (ns->proc_name->ts.cl);
3069 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
3070 for (el = ns->entries; el; el = el->next)
3071 el->sym->result->ts.cl->backend_decl = backend_decl;
3074 /* Translate COMMON blocks. */
3075 gfc_trans_common (ns);
3077 /* Null the parent fake result declaration if this namespace is
3078 a module function or an external procedures. */
3079 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3080 || ns->parent == NULL)
3081 parent_fake_result_decl = NULL_TREE;
3083 gfc_generate_contained_functions (ns);
3085 generate_local_vars (ns);
3087 /* Keep the parent fake result declaration in module functions
3088 or external procedures. */
3089 if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
3090 || ns->parent == NULL)
3091 current_fake_result_decl = parent_fake_result_decl;
3093 current_fake_result_decl = NULL_TREE;
3095 current_function_return_label = NULL;
3097 /* Now generate the code for the body of this function. */
3098 gfc_init_block (&body);
3100 /* If this is the main program, add a call to set_std to set up the
3101 runtime library Fortran language standard parameters. */
3103 if (sym->attr.is_main_program)
3105 tree gfc_int4_type_node = gfc_get_int_type (4);
3106 tmp = build_call_expr (gfor_fndecl_set_std, 5,
3107 build_int_cst (gfc_int4_type_node,
3108 gfc_option.warn_std),
3109 build_int_cst (gfc_int4_type_node,
3110 gfc_option.allow_std),
3111 build_int_cst (gfc_int4_type_node,
3113 build_int_cst (gfc_int4_type_node,
3114 gfc_option.flag_dump_core),
3115 build_int_cst (gfc_int4_type_node,
3116 gfc_option.flag_backtrace));
3117 gfc_add_expr_to_block (&body, tmp);
3120 /* If this is the main program and a -ffpe-trap option was provided,
3121 add a call to set_fpe so that the library will raise a FPE when
3123 if (sym->attr.is_main_program && gfc_option.fpe != 0)
3125 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
3126 tmp = build_call_expr (gfor_fndecl_set_fpe, 1,
3127 build_int_cst (gfc_c_int_type_node,
3129 gfc_add_expr_to_block (&body, tmp);
3132 /* If this is the main program and an -fconvert option was provided,
3133 add a call to set_convert. */
3135 if (sym->attr.is_main_program && gfc_option.convert != CONVERT_NATIVE)
3137 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
3138 tmp = build_call_expr (gfor_fndecl_set_convert, 1,
3139 build_int_cst (gfc_c_int_type_node,
3140 gfc_option.convert));
3141 gfc_add_expr_to_block (&body, tmp);
3144 /* If this is the main program and an -frecord-marker option was provided,
3145 add a call to set_record_marker. */
3147 if (sym->attr.is_main_program && gfc_option.record_marker != 0)
3149 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
3150 tmp = build_call_expr (gfor_fndecl_set_record_marker, 1,
3151 build_int_cst (gfc_c_int_type_node,
3152 gfc_option.record_marker));
3153 gfc_add_expr_to_block (&body, tmp);
3156 if (sym->attr.is_main_program && gfc_option.max_subrecord_length != 0)
3158 tree gfc_c_int_type_node;
3160 gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
3161 tmp = build_call_expr (gfor_fndecl_set_max_subrecord_length,
3163 build_int_cst (gfc_c_int_type_node,
3164 gfc_option.max_subrecord_length));
3165 gfc_add_expr_to_block (&body, tmp);
3168 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
3169 && sym->attr.subroutine)
3171 tree alternate_return;
3172 alternate_return = gfc_get_fake_result_decl (sym, 0);
3173 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
3178 /* Jump to the correct entry point. */
3179 tmp = gfc_trans_entry_master_switch (ns->entries);
3180 gfc_add_expr_to_block (&body, tmp);
3183 tmp = gfc_trans_code (ns->code);
3184 gfc_add_expr_to_block (&body, tmp);
3186 /* Add a return label if needed. */
3187 if (current_function_return_label)
3189 tmp = build1_v (LABEL_EXPR, current_function_return_label);
3190 gfc_add_expr_to_block (&body, tmp);
3193 tmp = gfc_finish_block (&body);
3194 /* Add code to create and cleanup arrays. */
3195 tmp = gfc_trans_deferred_vars (sym, tmp);
3197 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
3199 if (sym->attr.subroutine || sym == sym->result)
3201 if (current_fake_result_decl != NULL)
3202 result = TREE_VALUE (current_fake_result_decl);
3205 current_fake_result_decl = NULL_TREE;
3208 result = sym->result->backend_decl;
3210 if (result != NULL_TREE && sym->attr.function
3211 && sym->ts.type == BT_DERIVED
3212 && sym->ts.derived->attr.alloc_comp
3213 && !sym->attr.pointer)
3215 rank = sym->as ? sym->as->rank : 0;
3216 tmp2 = gfc_nullify_alloc_comp (sym->ts.derived, result, rank);
3217 gfc_add_expr_to_block (&block, tmp2);
3220 gfc_add_expr_to_block (&block, tmp);
3222 if (result == NULL_TREE)
3223 warning (0, "Function return value not set");
3226 /* Set the return value to the dummy result variable. The
3227 types may be different for scalar default REAL functions
3228 with -ff2c, therefore we have to convert. */
3229 tmp = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
3230 tmp = build2 (MODIFY_EXPR, TREE_TYPE (tmp),
3231 DECL_RESULT (fndecl), tmp);
3232 tmp = build1_v (RETURN_EXPR, tmp);
3233 gfc_add_expr_to_block (&block, tmp);
3237 gfc_add_expr_to_block (&block, tmp);
3240 /* Add all the decls we created during processing. */
3241 decl = saved_function_decls;
3246 next = TREE_CHAIN (decl);
3247 TREE_CHAIN (decl) = NULL_TREE;
3251 saved_function_decls = NULL_TREE;
3253 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
3255 /* Finish off this function and send it for code generation. */
3257 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3259 /* Output the GENERIC tree. */
3260 dump_function (TDI_original, fndecl);
3262 /* Store the end of the function, so that we get good line number
3263 info for the epilogue. */
3264 cfun->function_end_locus = input_location;
3266 /* We're leaving the context of this function, so zap cfun.
3267 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
3268 tree_rest_of_compilation. */
3273 pop_function_context ();
3274 saved_function_decls = saved_parent_function_decls;
3276 current_function_decl = old_context;
3278 if (decl_function_context (fndecl))
3279 /* Register this function with cgraph just far enough to get it
3280 added to our parent's nested function list. */
3281 (void) cgraph_node (fndecl);
3284 gfc_gimplify_function (fndecl);
3285 cgraph_finalize_function (fndecl, false);
3290 gfc_generate_constructors (void)
3292 gcc_assert (gfc_static_ctors == NULL_TREE);
3300 if (gfc_static_ctors == NULL_TREE)
3303 fnname = get_file_function_name ("I");
3304 type = build_function_type (void_type_node,
3305 gfc_chainon_list (NULL_TREE, void_type_node));
3307 fndecl = build_decl (FUNCTION_DECL, fnname, type);
3308 TREE_PUBLIC (fndecl) = 1;
3310 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
3311 DECL_ARTIFICIAL (decl) = 1;
3312 DECL_IGNORED_P (decl) = 1;
3313 DECL_CONTEXT (decl) = fndecl;
3314 DECL_RESULT (fndecl) = decl;
3318 current_function_decl = fndecl;
3320 rest_of_decl_compilation (fndecl, 1, 0);
3322 make_decl_rtl (fndecl);
3324 init_function_start (fndecl);
3328 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3330 tmp = build_call_expr (TREE_VALUE (gfc_static_ctors), 0);
3331 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3336 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3338 free_after_parsing (cfun);
3339 free_after_compilation (cfun);
3341 tree_rest_of_compilation (fndecl);
3343 current_function_decl = NULL_TREE;
3347 /* Translates a BLOCK DATA program unit. This means emitting the
3348 commons contained therein plus their initializations. We also emit
3349 a globally visible symbol to make sure that each BLOCK DATA program
3350 unit remains unique. */
3353 gfc_generate_block_data (gfc_namespace * ns)
3358 /* Tell the backend the source location of the block data. */
3360 gfc_set_backend_locus (&ns->proc_name->declared_at);
3362 gfc_set_backend_locus (&gfc_current_locus);
3364 /* Process the DATA statements. */
3365 gfc_trans_common (ns);
3367 /* Create a global symbol with the mane of the block data. This is to
3368 generate linker errors if the same name is used twice. It is never
3371 id = gfc_sym_mangled_function_id (ns->proc_name);
3373 id = get_identifier ("__BLOCK_DATA__");
3375 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3376 TREE_PUBLIC (decl) = 1;
3377 TREE_STATIC (decl) = 1;
3380 rest_of_decl_compilation (decl, 1, 0);
3384 #include "gt-fortran-trans-decl.h"