1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
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"
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;
53 static GTY(()) tree current_function_return_label;
56 /* Holds the variable DECLs for the current function. */
58 static GTY(()) tree saved_function_decls;
59 static GTY(()) tree saved_parent_function_decls;
62 /* The namespace of the module we're currently generating. Only used while
63 outputting decls for module variables. Do not rely on this being set. */
65 static gfc_namespace *module_namespace;
68 /* List of static constructor functions. */
70 tree gfc_static_ctors;
73 /* Function declarations for builtin library functions. */
75 tree gfor_fndecl_internal_malloc;
76 tree gfor_fndecl_internal_malloc64;
77 tree gfor_fndecl_internal_realloc;
78 tree gfor_fndecl_internal_realloc64;
79 tree gfor_fndecl_internal_free;
80 tree gfor_fndecl_allocate;
81 tree gfor_fndecl_allocate64;
82 tree gfor_fndecl_deallocate;
83 tree gfor_fndecl_pause_numeric;
84 tree gfor_fndecl_pause_string;
85 tree gfor_fndecl_stop_numeric;
86 tree gfor_fndecl_stop_string;
87 tree gfor_fndecl_select_string;
88 tree gfor_fndecl_runtime_error;
89 tree gfor_fndecl_set_fpe;
90 tree gfor_fndecl_set_std;
91 tree gfor_fndecl_set_convert;
92 tree gfor_fndecl_ctime;
93 tree gfor_fndecl_fdate;
94 tree gfor_fndecl_ttynam;
95 tree gfor_fndecl_in_pack;
96 tree gfor_fndecl_in_unpack;
97 tree gfor_fndecl_associated;
100 /* Math functions. Many other math functions are handled in
101 trans-intrinsic.c. */
103 gfc_powdecl_list gfor_fndecl_math_powi[4][3];
104 tree gfor_fndecl_math_cpowf;
105 tree gfor_fndecl_math_cpow;
106 tree gfor_fndecl_math_cpowl10;
107 tree gfor_fndecl_math_cpowl16;
108 tree gfor_fndecl_math_ishftc4;
109 tree gfor_fndecl_math_ishftc8;
110 tree gfor_fndecl_math_ishftc16;
111 tree gfor_fndecl_math_exponent4;
112 tree gfor_fndecl_math_exponent8;
113 tree gfor_fndecl_math_exponent10;
114 tree gfor_fndecl_math_exponent16;
117 /* String functions. */
119 tree gfor_fndecl_copy_string;
120 tree gfor_fndecl_compare_string;
121 tree gfor_fndecl_concat_string;
122 tree gfor_fndecl_string_len_trim;
123 tree gfor_fndecl_string_index;
124 tree gfor_fndecl_string_scan;
125 tree gfor_fndecl_string_verify;
126 tree gfor_fndecl_string_trim;
127 tree gfor_fndecl_string_repeat;
128 tree gfor_fndecl_adjustl;
129 tree gfor_fndecl_adjustr;
132 /* Other misc. runtime library functions. */
134 tree gfor_fndecl_size0;
135 tree gfor_fndecl_size1;
136 tree gfor_fndecl_iargc;
138 /* Intrinsic functions implemented in FORTRAN. */
139 tree gfor_fndecl_si_kind;
140 tree gfor_fndecl_sr_kind;
144 gfc_add_decl_to_parent_function (tree decl)
147 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
148 DECL_NONLOCAL (decl) = 1;
149 TREE_CHAIN (decl) = saved_parent_function_decls;
150 saved_parent_function_decls = decl;
154 gfc_add_decl_to_function (tree decl)
157 TREE_USED (decl) = 1;
158 DECL_CONTEXT (decl) = current_function_decl;
159 TREE_CHAIN (decl) = saved_function_decls;
160 saved_function_decls = decl;
164 /* Build a backend label declaration. Set TREE_USED for named labels.
165 The context of the label is always the current_function_decl. All
166 labels are marked artificial. */
169 gfc_build_label_decl (tree label_id)
171 /* 2^32 temporaries should be enough. */
172 static unsigned int tmp_num = 1;
176 if (label_id == NULL_TREE)
178 /* Build an internal label name. */
179 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
180 label_id = get_identifier (label_name);
185 /* Build the LABEL_DECL node. Labels have no type. */
186 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
187 DECL_CONTEXT (label_decl) = current_function_decl;
188 DECL_MODE (label_decl) = VOIDmode;
190 /* We always define the label as used, even if the original source
191 file never references the label. We don't want all kinds of
192 spurious warnings for old-style Fortran code with too many
194 TREE_USED (label_decl) = 1;
196 DECL_ARTIFICIAL (label_decl) = 1;
201 /* Returns the return label for the current function. */
204 gfc_get_return_label (void)
206 char name[GFC_MAX_SYMBOL_LEN + 10];
208 if (current_function_return_label)
209 return current_function_return_label;
211 sprintf (name, "__return_%s",
212 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
214 current_function_return_label =
215 gfc_build_label_decl (get_identifier (name));
217 DECL_ARTIFICIAL (current_function_return_label) = 1;
219 return current_function_return_label;
223 /* Set the backend source location of a decl. */
226 gfc_set_decl_location (tree decl, locus * loc)
228 #ifdef USE_MAPPED_LOCATION
229 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
231 DECL_SOURCE_LINE (decl) = loc->lb->linenum;
232 DECL_SOURCE_FILE (decl) = loc->lb->file->filename;
237 /* Return the backend label declaration for a given label structure,
238 or create it if it doesn't exist yet. */
241 gfc_get_label_decl (gfc_st_label * lp)
243 if (lp->backend_decl)
244 return lp->backend_decl;
247 char label_name[GFC_MAX_SYMBOL_LEN + 1];
250 /* Validate the label declaration from the front end. */
251 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
253 /* Build a mangled name for the label. */
254 sprintf (label_name, "__label_%.6d", lp->value);
256 /* Build the LABEL_DECL node. */
257 label_decl = gfc_build_label_decl (get_identifier (label_name));
259 /* Tell the debugger where the label came from. */
260 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
261 gfc_set_decl_location (label_decl, &lp->where);
263 DECL_ARTIFICIAL (label_decl) = 1;
265 /* Store the label in the label list and return the LABEL_DECL. */
266 lp->backend_decl = label_decl;
272 /* Convert a gfc_symbol to an identifier of the same name. */
275 gfc_sym_identifier (gfc_symbol * sym)
277 return (get_identifier (sym->name));
281 /* Construct mangled name from symbol name. */
284 gfc_sym_mangled_identifier (gfc_symbol * sym)
286 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
288 if (sym->module == NULL)
289 return gfc_sym_identifier (sym);
292 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
293 return get_identifier (name);
298 /* Construct mangled function name from symbol name. */
301 gfc_sym_mangled_function_id (gfc_symbol * sym)
304 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
306 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
307 || (sym->module != NULL && sym->attr.if_source == IFSRC_IFBODY))
309 if (strcmp (sym->name, "MAIN__") == 0
310 || sym->attr.proc == PROC_INTRINSIC)
311 return get_identifier (sym->name);
313 if (gfc_option.flag_underscoring)
315 has_underscore = strchr (sym->name, '_') != 0;
316 if (gfc_option.flag_second_underscore && has_underscore)
317 snprintf (name, sizeof name, "%s__", sym->name);
319 snprintf (name, sizeof name, "%s_", sym->name);
320 return get_identifier (name);
323 return get_identifier (sym->name);
327 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
328 return get_identifier (name);
333 /* Returns true if a variable of specified size should go on the stack. */
336 gfc_can_put_var_on_stack (tree size)
338 unsigned HOST_WIDE_INT low;
340 if (!INTEGER_CST_P (size))
343 if (gfc_option.flag_max_stack_var_size < 0)
346 if (TREE_INT_CST_HIGH (size) != 0)
349 low = TREE_INT_CST_LOW (size);
350 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
353 /* TODO: Set a per-function stack size limit. */
359 /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
360 an expression involving its corresponding pointer. There are
361 2 cases; one for variable size arrays, and one for everything else,
362 because variable-sized arrays require one fewer level of
366 gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
368 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
371 /* Parameters need to be dereferenced. */
372 if (sym->cp_pointer->attr.dummy)
373 ptr_decl = build_fold_indirect_ref (ptr_decl);
375 /* Check to see if we're dealing with a variable-sized array. */
376 if (sym->attr.dimension
377 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
379 /* These decls will be dereferenced later, so we don't dereference
381 value = convert (TREE_TYPE (decl), ptr_decl);
385 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
387 value = build_fold_indirect_ref (ptr_decl);
390 SET_DECL_VALUE_EXPR (decl, value);
391 DECL_HAS_VALUE_EXPR_P (decl) = 1;
392 /* This is a fake variable just for debugging purposes. */
393 TREE_ASM_WRITTEN (decl) = 1;
397 /* Finish processing of a declaration and install its initial value. */
400 gfc_finish_decl (tree decl, tree init)
402 if (TREE_CODE (decl) == PARM_DECL)
403 gcc_assert (init == NULL_TREE);
404 /* Remember that PARM_DECL doesn't have a DECL_INITIAL field per se
405 -- it overlaps DECL_ARG_TYPE. */
406 else if (init == NULL_TREE)
407 gcc_assert (DECL_INITIAL (decl) == NULL_TREE);
409 gcc_assert (DECL_INITIAL (decl) == error_mark_node);
411 if (init != NULL_TREE)
413 if (TREE_CODE (decl) != TYPE_DECL)
414 DECL_INITIAL (decl) = init;
417 /* typedef foo = bar; store the type of bar as the type of foo. */
418 TREE_TYPE (decl) = TREE_TYPE (init);
419 DECL_INITIAL (decl) = init = 0;
423 if (TREE_CODE (decl) == VAR_DECL)
425 if (DECL_SIZE (decl) == NULL_TREE
426 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
427 layout_decl (decl, 0);
429 /* A static variable with an incomplete type is an error if it is
430 initialized. Also if it is not file scope. Otherwise, let it
431 through, but if it is not `extern' then it may cause an error
433 /* An automatic variable with an incomplete type is an error. */
434 if (DECL_SIZE (decl) == NULL_TREE
435 && (TREE_STATIC (decl) ? (DECL_INITIAL (decl) != 0
436 || DECL_CONTEXT (decl) != 0)
437 : !DECL_EXTERNAL (decl)))
439 gfc_fatal_error ("storage size not known");
442 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
443 && (DECL_SIZE (decl) != 0)
444 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST))
446 gfc_fatal_error ("storage size not constant");
453 /* Apply symbol attributes to a variable, and add it to the function scope. */
456 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
458 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
459 This is the equivalent of the TARGET variables.
460 We also need to set this if the variable is passed by reference in a
463 /* Set DECL_VALUE_EXPR for Cray Pointees. */
464 if (sym->attr.cray_pointee)
465 gfc_finish_cray_pointee (decl, sym);
467 if (sym->attr.target)
468 TREE_ADDRESSABLE (decl) = 1;
469 /* If it wasn't used we wouldn't be getting it. */
470 TREE_USED (decl) = 1;
472 /* Chain this decl to the pending declarations. Don't do pushdecl()
473 because this would add them to the current scope rather than the
475 if (current_function_decl != NULL_TREE)
477 if (sym->ns->proc_name->backend_decl == current_function_decl
478 || sym->result == sym)
479 gfc_add_decl_to_function (decl);
481 gfc_add_decl_to_parent_function (decl);
484 if (sym->attr.cray_pointee)
487 /* If a variable is USE associated, it's always external. */
488 if (sym->attr.use_assoc)
490 DECL_EXTERNAL (decl) = 1;
491 TREE_PUBLIC (decl) = 1;
493 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
495 /* TODO: Don't set sym->module for result or dummy variables. */
496 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
497 /* This is the declaration of a module variable. */
498 TREE_PUBLIC (decl) = 1;
499 TREE_STATIC (decl) = 1;
502 if ((sym->attr.save || sym->attr.data || sym->value)
503 && !sym->attr.use_assoc)
504 TREE_STATIC (decl) = 1;
506 /* Keep variables larger than max-stack-var-size off stack. */
507 if (!sym->ns->proc_name->attr.recursive
508 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
509 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)))
510 TREE_STATIC (decl) = 1;
514 /* Allocate the lang-specific part of a decl. */
517 gfc_allocate_lang_decl (tree decl)
519 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
520 ggc_alloc_cleared (sizeof (struct lang_decl));
523 /* Remember a symbol to generate initialization/cleanup code at function
527 gfc_defer_symbol_init (gfc_symbol * sym)
533 /* Don't add a symbol twice. */
537 last = head = sym->ns->proc_name;
540 /* Make sure that setup code for dummy variables which are used in the
541 setup of other variables is generated first. */
544 /* Find the first dummy arg seen after us, or the first non-dummy arg.
545 This is a circular list, so don't go past the head. */
547 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
553 /* Insert in between last and p. */
559 /* Create an array index type variable with function scope. */
562 create_index_var (const char * pfx, int nest)
566 decl = gfc_create_var_np (gfc_array_index_type, pfx);
568 gfc_add_decl_to_parent_function (decl);
570 gfc_add_decl_to_function (decl);
575 /* Create variables to hold all the non-constant bits of info for a
576 descriptorless array. Remember these in the lang-specific part of the
580 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
586 type = TREE_TYPE (decl);
588 /* We just use the descriptor, if there is one. */
589 if (GFC_DESCRIPTOR_TYPE_P (type))
592 gcc_assert (GFC_ARRAY_TYPE_P (type));
593 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
594 && !sym->attr.contained;
596 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
598 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
599 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
600 /* Don't try to use the unknown bound for assumed shape arrays. */
601 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
602 && (sym->as->type != AS_ASSUMED_SIZE
603 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
604 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
606 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
607 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
609 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
611 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
614 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
616 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
619 if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE
620 && sym->as->type != AS_ASSUMED_SIZE)
621 GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
623 if (POINTER_TYPE_P (type))
625 gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
626 gcc_assert (TYPE_LANG_SPECIFIC (type)
627 == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
628 type = TREE_TYPE (type);
631 if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
635 size = build2 (MINUS_EXPR, gfc_array_index_type,
636 GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
637 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
639 TYPE_DOMAIN (type) = range;
645 /* For some dummy arguments we don't use the actual argument directly.
646 Instead we create a local decl and use that. This allows us to perform
647 initialization, and construct full type information. */
650 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
660 if (sym->attr.pointer || sym->attr.allocatable)
663 /* Add to list of variables if not a fake result variable. */
664 if (sym->attr.result || sym->attr.dummy)
665 gfc_defer_symbol_init (sym);
667 type = TREE_TYPE (dummy);
668 gcc_assert (TREE_CODE (dummy) == PARM_DECL
669 && POINTER_TYPE_P (type));
671 /* Do we know the element size? */
672 known_size = sym->ts.type != BT_CHARACTER
673 || INTEGER_CST_P (sym->ts.cl->backend_decl);
675 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
677 /* For descriptorless arrays with known element size the actual
678 argument is sufficient. */
679 gcc_assert (GFC_ARRAY_TYPE_P (type));
680 gfc_build_qualified_array (dummy, sym);
684 type = TREE_TYPE (type);
685 if (GFC_DESCRIPTOR_TYPE_P (type))
687 /* Create a decriptorless array pointer. */
690 if (!gfc_option.flag_repack_arrays)
692 if (as->type == AS_ASSUMED_SIZE)
697 if (as->type == AS_EXPLICIT)
700 for (n = 0; n < as->rank; n++)
704 && as->upper[n]->expr_type == EXPR_CONSTANT
705 && as->lower[n]->expr_type == EXPR_CONSTANT))
713 type = gfc_typenode_for_spec (&sym->ts);
714 type = gfc_get_nodesc_array_type (type, sym->as, packed);
718 /* We now have an expression for the element size, so create a fully
719 qualified type. Reset sym->backend decl or this will just return the
721 sym->backend_decl = NULL_TREE;
722 type = gfc_sym_type (sym);
726 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
727 decl = build_decl (VAR_DECL, get_identifier (name), type);
729 DECL_ARTIFICIAL (decl) = 1;
730 TREE_PUBLIC (decl) = 0;
731 TREE_STATIC (decl) = 0;
732 DECL_EXTERNAL (decl) = 0;
734 /* We should never get deferred shape arrays here. We used to because of
736 gcc_assert (sym->as->type != AS_DEFERRED);
741 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
745 GFC_DECL_PACKED_ARRAY (decl) = 1;
749 gfc_build_qualified_array (decl, sym);
751 if (DECL_LANG_SPECIFIC (dummy))
752 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
754 gfc_allocate_lang_decl (decl);
756 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
758 if (sym->ns->proc_name->backend_decl == current_function_decl
759 || sym->attr.contained)
760 gfc_add_decl_to_function (decl);
762 gfc_add_decl_to_parent_function (decl);
768 /* Return a constant or a variable to use as a string length. Does not
769 add the decl to the current scope. */
772 gfc_create_string_length (gfc_symbol * sym)
776 gcc_assert (sym->ts.cl);
777 gfc_conv_const_charlen (sym->ts.cl);
779 if (sym->ts.cl->backend_decl == NULL_TREE)
781 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
783 /* Also prefix the mangled name. */
784 strcpy (&name[1], sym->name);
786 length = build_decl (VAR_DECL, get_identifier (name),
787 gfc_charlen_type_node);
788 DECL_ARTIFICIAL (length) = 1;
789 TREE_USED (length) = 1;
790 if (sym->ns->proc_name->tlink != NULL)
791 gfc_defer_symbol_init (sym);
792 sym->ts.cl->backend_decl = length;
795 return sym->ts.cl->backend_decl;
798 /* If a variable is assigned a label, we add another two auxiliary
802 gfc_add_assign_aux_vars (gfc_symbol * sym)
808 gcc_assert (sym->backend_decl);
810 decl = sym->backend_decl;
811 gfc_allocate_lang_decl (decl);
812 GFC_DECL_ASSIGN (decl) = 1;
813 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
814 gfc_charlen_type_node);
815 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
817 gfc_finish_var_decl (length, sym);
818 gfc_finish_var_decl (addr, sym);
819 /* STRING_LENGTH is also used as flag. Less than -1 means that
820 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
821 target label's address. Otherwise, value is the length of a format string
822 and ASSIGN_ADDR is its address. */
823 if (TREE_STATIC (length))
824 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
826 gfc_defer_symbol_init (sym);
828 GFC_DECL_STRING_LEN (decl) = length;
829 GFC_DECL_ASSIGN_ADDR (decl) = addr;
832 /* Return the decl for a gfc_symbol, create it if it doesn't already
836 gfc_get_symbol_decl (gfc_symbol * sym)
839 tree length = NULL_TREE;
842 gcc_assert (sym->attr.referenced);
844 if (sym->ns && sym->ns->proc_name->attr.function)
845 byref = gfc_return_by_reference (sym->ns->proc_name);
849 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
851 /* Return via extra parameter. */
852 if (sym->attr.result && byref
853 && !sym->backend_decl)
856 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
857 /* For entry master function skip over the __entry
859 if (sym->ns->proc_name->attr.entry_master)
860 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
863 /* Dummy variables should already have been created. */
864 gcc_assert (sym->backend_decl);
866 /* Create a character length variable. */
867 if (sym->ts.type == BT_CHARACTER)
869 if (sym->ts.cl->backend_decl == NULL_TREE)
870 length = gfc_create_string_length (sym);
872 length = sym->ts.cl->backend_decl;
873 if (TREE_CODE (length) == VAR_DECL
874 && DECL_CONTEXT (length) == NULL_TREE)
876 gfc_add_decl_to_function (length);
877 gfc_defer_symbol_init (sym);
881 /* Use a copy of the descriptor for dummy arrays. */
882 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
885 gfc_build_dummy_array_decl (sym, sym->backend_decl);
888 TREE_USED (sym->backend_decl) = 1;
889 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
891 gfc_add_assign_aux_vars (sym);
893 return sym->backend_decl;
896 if (sym->backend_decl)
897 return sym->backend_decl;
899 /* Catch function declarations. Only used for actual parameters. */
900 if (sym->attr.flavor == FL_PROCEDURE)
902 decl = gfc_get_extern_function_decl (sym);
906 if (sym->attr.intrinsic)
907 internal_error ("intrinsic variable which isn't a procedure");
909 /* Create string length decl first so that they can be used in the
911 if (sym->ts.type == BT_CHARACTER)
912 length = gfc_create_string_length (sym);
914 /* Create the decl for the variable. */
915 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
917 gfc_set_decl_location (decl, &sym->declared_at);
919 /* Symbols from modules should have their assembler names mangled.
920 This is done here rather than in gfc_finish_var_decl because it
921 is different for string length variables. */
923 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
925 if (sym->attr.dimension)
927 /* Create variables to hold the non-constant bits of array info. */
928 gfc_build_qualified_array (decl, sym);
930 /* Remember this variable for allocation/cleanup. */
931 gfc_defer_symbol_init (sym);
933 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
934 GFC_DECL_PACKED_ARRAY (decl) = 1;
937 gfc_finish_var_decl (decl, sym);
939 if (sym->ts.type == BT_CHARACTER)
941 /* Character variables need special handling. */
942 gfc_allocate_lang_decl (decl);
944 if (TREE_CODE (length) != INTEGER_CST)
946 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
950 /* Also prefix the mangled name for symbols from modules. */
951 strcpy (&name[1], sym->name);
954 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
955 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
957 gfc_finish_var_decl (length, sym);
958 gcc_assert (!sym->value);
961 sym->backend_decl = decl;
963 if (sym->attr.assign)
965 gfc_add_assign_aux_vars (sym);
968 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
970 /* Add static initializer. */
971 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
972 TREE_TYPE (decl), sym->attr.dimension,
973 sym->attr.pointer || sym->attr.allocatable);
980 /* Substitute a temporary variable in place of the real one. */
983 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
985 save->attr = sym->attr;
986 save->decl = sym->backend_decl;
988 gfc_clear_attr (&sym->attr);
989 sym->attr.referenced = 1;
990 sym->attr.flavor = FL_VARIABLE;
992 sym->backend_decl = decl;
996 /* Restore the original variable. */
999 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
1001 sym->attr = save->attr;
1002 sym->backend_decl = save->decl;
1006 /* Get a basic decl for an external function. */
1009 gfc_get_extern_function_decl (gfc_symbol * sym)
1014 gfc_intrinsic_sym *isym;
1016 char s[GFC_MAX_SYMBOL_LEN + 13]; /* "f2c_specific" and '\0'. */
1020 if (sym->backend_decl)
1021 return sym->backend_decl;
1023 /* We should never be creating external decls for alternate entry points.
1024 The procedure may be an alternate entry point, but we don't want/need
1026 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1028 if (sym->attr.intrinsic)
1030 /* Call the resolution function to get the actual name. This is
1031 a nasty hack which relies on the resolution functions only looking
1032 at the first argument. We pass NULL for the second argument
1033 otherwise things like AINT get confused. */
1034 isym = gfc_find_function (sym->name);
1035 gcc_assert (isym->resolve.f0 != NULL);
1037 memset (&e, 0, sizeof (e));
1038 e.expr_type = EXPR_FUNCTION;
1040 memset (&argexpr, 0, sizeof (argexpr));
1041 gcc_assert (isym->formal);
1042 argexpr.ts = isym->formal->ts;
1044 if (isym->formal->next == NULL)
1045 isym->resolve.f1 (&e, &argexpr);
1048 /* All specific intrinsics take one or two arguments. */
1049 gcc_assert (isym->formal->next->next == NULL);
1050 isym->resolve.f2 (&e, &argexpr, NULL);
1053 if (gfc_option.flag_f2c
1054 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1055 || e.ts.type == BT_COMPLEX))
1057 /* Specific which needs a different implementation if f2c
1058 calling conventions are used. */
1059 sprintf (s, "f2c_specific%s", e.value.function.name);
1062 sprintf (s, "specific%s", e.value.function.name);
1064 name = get_identifier (s);
1065 mangled_name = name;
1069 name = gfc_sym_identifier (sym);
1070 mangled_name = gfc_sym_mangled_function_id (sym);
1073 type = gfc_get_function_type (sym);
1074 fndecl = build_decl (FUNCTION_DECL, name, type);
1076 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1077 /* If the return type is a pointer, avoid alias issues by setting
1078 DECL_IS_MALLOC to nonzero. This means that the function should be
1079 treated as if it were a malloc, meaning it returns a pointer that
1081 if (POINTER_TYPE_P (type))
1082 DECL_IS_MALLOC (fndecl) = 1;
1084 /* Set the context of this decl. */
1085 if (0 && sym->ns && sym->ns->proc_name)
1087 /* TODO: Add external decls to the appropriate scope. */
1088 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1092 /* Global declaration, e.g. intrinsic subroutine. */
1093 DECL_CONTEXT (fndecl) = NULL_TREE;
1096 DECL_EXTERNAL (fndecl) = 1;
1098 /* This specifies if a function is globally addressable, i.e. it is
1099 the opposite of declaring static in C. */
1100 TREE_PUBLIC (fndecl) = 1;
1102 /* Set attributes for PURE functions. A call to PURE function in the
1103 Fortran 95 sense is both pure and without side effects in the C
1105 if (sym->attr.pure || sym->attr.elemental)
1107 if (sym->attr.function && !gfc_return_by_reference (sym))
1108 DECL_IS_PURE (fndecl) = 1;
1109 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1110 parameters and don't use alternate returns (is this
1111 allowed?). In that case, calls to them are meaningless, and
1112 can be optimized away. See also in build_function_decl(). */
1113 TREE_SIDE_EFFECTS (fndecl) = 0;
1116 /* Mark non-returning functions. */
1117 if (sym->attr.noreturn)
1118 TREE_THIS_VOLATILE(fndecl) = 1;
1120 sym->backend_decl = fndecl;
1122 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1123 pushdecl_top_level (fndecl);
1129 /* Create a declaration for a procedure. For external functions (in the C
1130 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1131 a master function with alternate entry points. */
1134 build_function_decl (gfc_symbol * sym)
1137 symbol_attribute attr;
1139 gfc_formal_arglist *f;
1141 gcc_assert (!sym->backend_decl);
1142 gcc_assert (!sym->attr.external);
1144 /* Set the line and filename. sym->declared_at seems to point to the
1145 last statement for subroutines, but it'll do for now. */
1146 gfc_set_backend_locus (&sym->declared_at);
1148 /* Allow only one nesting level. Allow public declarations. */
1149 gcc_assert (current_function_decl == NULL_TREE
1150 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1152 type = gfc_get_function_type (sym);
1153 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1155 /* Perform name mangling if this is a top level or module procedure. */
1156 if (current_function_decl == NULL_TREE)
1157 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1159 /* Figure out the return type of the declared function, and build a
1160 RESULT_DECL for it. If this is a subroutine with alternate
1161 returns, build a RESULT_DECL for it. */
1164 result_decl = NULL_TREE;
1165 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1168 if (gfc_return_by_reference (sym))
1169 type = void_type_node;
1172 if (sym->result != sym)
1173 result_decl = gfc_sym_identifier (sym->result);
1175 type = TREE_TYPE (TREE_TYPE (fndecl));
1180 /* Look for alternate return placeholders. */
1181 int has_alternate_returns = 0;
1182 for (f = sym->formal; f; f = f->next)
1186 has_alternate_returns = 1;
1191 if (has_alternate_returns)
1192 type = integer_type_node;
1194 type = void_type_node;
1197 result_decl = build_decl (RESULT_DECL, result_decl, type);
1198 DECL_ARTIFICIAL (result_decl) = 1;
1199 DECL_IGNORED_P (result_decl) = 1;
1200 DECL_CONTEXT (result_decl) = fndecl;
1201 DECL_RESULT (fndecl) = result_decl;
1203 /* Don't call layout_decl for a RESULT_DECL.
1204 layout_decl (result_decl, 0); */
1206 /* If the return type is a pointer, avoid alias issues by setting
1207 DECL_IS_MALLOC to nonzero. This means that the function should be
1208 treated as if it were a malloc, meaning it returns a pointer that
1210 if (POINTER_TYPE_P (type))
1211 DECL_IS_MALLOC (fndecl) = 1;
1213 /* Set up all attributes for the function. */
1214 DECL_CONTEXT (fndecl) = current_function_decl;
1215 DECL_EXTERNAL (fndecl) = 0;
1217 /* This specifies if a function is globally visible, i.e. it is
1218 the opposite of declaring static in C. */
1219 if (DECL_CONTEXT (fndecl) == NULL_TREE
1220 && !sym->attr.entry_master)
1221 TREE_PUBLIC (fndecl) = 1;
1223 /* TREE_STATIC means the function body is defined here. */
1224 TREE_STATIC (fndecl) = 1;
1226 /* Set attributes for PURE functions. A call to a PURE function in the
1227 Fortran 95 sense is both pure and without side effects in the C
1229 if (attr.pure || attr.elemental)
1231 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1232 including a alternate return. In that case it can also be
1233 marked as PURE. See also in gfc_get_extern_function_decl(). */
1234 if (attr.function && !gfc_return_by_reference (sym))
1235 DECL_IS_PURE (fndecl) = 1;
1236 TREE_SIDE_EFFECTS (fndecl) = 0;
1239 /* Layout the function declaration and put it in the binding level
1240 of the current function. */
1243 sym->backend_decl = fndecl;
1247 /* Create the DECL_ARGUMENTS for a procedure. */
1250 create_function_arglist (gfc_symbol * sym)
1253 gfc_formal_arglist *f;
1254 tree typelist, hidden_typelist;
1255 tree arglist, hidden_arglist;
1259 fndecl = sym->backend_decl;
1261 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1262 the new FUNCTION_DECL node. */
1263 arglist = NULL_TREE;
1264 hidden_arglist = NULL_TREE;
1265 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1267 if (sym->attr.entry_master)
1269 type = TREE_VALUE (typelist);
1270 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1272 DECL_CONTEXT (parm) = fndecl;
1273 DECL_ARG_TYPE (parm) = type;
1274 TREE_READONLY (parm) = 1;
1275 gfc_finish_decl (parm, NULL_TREE);
1277 arglist = chainon (arglist, parm);
1278 typelist = TREE_CHAIN (typelist);
1281 if (gfc_return_by_reference (sym))
1283 tree type = TREE_VALUE (typelist), length = NULL;
1285 if (sym->ts.type == BT_CHARACTER)
1287 /* Length of character result. */
1288 tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
1289 gcc_assert (len_type == gfc_charlen_type_node);
1291 length = build_decl (PARM_DECL,
1292 get_identifier (".__result"),
1294 if (!sym->ts.cl->length)
1296 sym->ts.cl->backend_decl = length;
1297 TREE_USED (length) = 1;
1299 gcc_assert (TREE_CODE (length) == PARM_DECL);
1300 DECL_CONTEXT (length) = fndecl;
1301 DECL_ARG_TYPE (length) = len_type;
1302 TREE_READONLY (length) = 1;
1303 DECL_ARTIFICIAL (length) = 1;
1304 gfc_finish_decl (length, NULL_TREE);
1305 if (sym->ts.cl->backend_decl == NULL
1306 || sym->ts.cl->backend_decl == length)
1311 if (sym->ts.cl->backend_decl == NULL)
1313 tree len = build_decl (VAR_DECL,
1314 get_identifier ("..__result"),
1315 gfc_charlen_type_node);
1316 DECL_ARTIFICIAL (len) = 1;
1317 TREE_USED (len) = 1;
1318 sym->ts.cl->backend_decl = len;
1321 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1322 arg = sym->result ? sym->result : sym;
1323 backend_decl = arg->backend_decl;
1324 /* Temporary clear it, so that gfc_sym_type creates complete
1326 arg->backend_decl = NULL;
1327 type = gfc_sym_type (arg);
1328 arg->backend_decl = backend_decl;
1329 type = build_reference_type (type);
1333 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1335 DECL_CONTEXT (parm) = fndecl;
1336 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1337 TREE_READONLY (parm) = 1;
1338 DECL_ARTIFICIAL (parm) = 1;
1339 gfc_finish_decl (parm, NULL_TREE);
1341 arglist = chainon (arglist, parm);
1342 typelist = TREE_CHAIN (typelist);
1344 if (sym->ts.type == BT_CHARACTER)
1346 gfc_allocate_lang_decl (parm);
1347 arglist = chainon (arglist, length);
1348 typelist = TREE_CHAIN (typelist);
1352 hidden_typelist = typelist;
1353 for (f = sym->formal; f; f = f->next)
1354 if (f->sym != NULL) /* Ignore alternate returns. */
1355 hidden_typelist = TREE_CHAIN (hidden_typelist);
1357 for (f = sym->formal; f; f = f->next)
1359 char name[GFC_MAX_SYMBOL_LEN + 2];
1361 /* Ignore alternate returns. */
1365 type = TREE_VALUE (typelist);
1367 if (f->sym->ts.type == BT_CHARACTER)
1369 tree len_type = TREE_VALUE (hidden_typelist);
1370 tree length = NULL_TREE;
1371 gcc_assert (len_type == gfc_charlen_type_node);
1373 strcpy (&name[1], f->sym->name);
1375 length = build_decl (PARM_DECL, get_identifier (name), len_type);
1377 hidden_arglist = chainon (hidden_arglist, length);
1378 DECL_CONTEXT (length) = fndecl;
1379 DECL_ARTIFICIAL (length) = 1;
1380 DECL_ARG_TYPE (length) = len_type;
1381 TREE_READONLY (length) = 1;
1382 gfc_finish_decl (length, NULL_TREE);
1384 /* TODO: Check string lengths when -fbounds-check. */
1386 /* Use the passed value for assumed length variables. */
1387 if (!f->sym->ts.cl->length)
1389 TREE_USED (length) = 1;
1390 if (!f->sym->ts.cl->backend_decl)
1391 f->sym->ts.cl->backend_decl = length;
1394 /* there is already another variable using this
1395 gfc_charlen node, build a new one for this variable
1396 and chain it into the list of gfc_charlens.
1397 This happens for e.g. in the case
1399 since CHARACTER declarations on the same line share
1400 the same gfc_charlen node. */
1403 cl = gfc_get_charlen ();
1404 cl->backend_decl = length;
1405 cl->next = f->sym->ts.cl->next;
1406 f->sym->ts.cl->next = cl;
1411 hidden_typelist = TREE_CHAIN (hidden_typelist);
1413 if (f->sym->ts.cl->backend_decl == NULL
1414 || f->sym->ts.cl->backend_decl == length)
1416 if (f->sym->ts.cl->backend_decl == NULL)
1417 gfc_create_string_length (f->sym);
1419 /* Make sure PARM_DECL type doesn't point to incomplete type. */
1420 if (f->sym->attr.flavor == FL_PROCEDURE)
1421 type = build_pointer_type (gfc_get_function_type (f->sym));
1423 type = gfc_sym_type (f->sym);
1427 /* For non-constant length array arguments, make sure they use
1428 a different type node from TYPE_ARG_TYPES type. */
1429 if (f->sym->attr.dimension
1430 && type == TREE_VALUE (typelist)
1431 && TREE_CODE (type) == POINTER_TYPE
1432 && GFC_ARRAY_TYPE_P (type)
1433 && f->sym->as->type != AS_ASSUMED_SIZE
1434 && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
1436 if (f->sym->attr.flavor == FL_PROCEDURE)
1437 type = build_pointer_type (gfc_get_function_type (f->sym));
1439 type = gfc_sym_type (f->sym);
1442 /* Build a the argument declaration. */
1443 parm = build_decl (PARM_DECL, gfc_sym_identifier (f->sym), type);
1445 /* Fill in arg stuff. */
1446 DECL_CONTEXT (parm) = fndecl;
1447 DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
1448 /* All implementation args are read-only. */
1449 TREE_READONLY (parm) = 1;
1451 gfc_finish_decl (parm, NULL_TREE);
1453 f->sym->backend_decl = parm;
1455 arglist = chainon (arglist, parm);
1456 typelist = TREE_CHAIN (typelist);
1459 /* Add the hidden string length parameters. */
1460 arglist = chainon (arglist, hidden_arglist);
1462 gcc_assert (TREE_VALUE (hidden_typelist) == void_type_node);
1463 DECL_ARGUMENTS (fndecl) = arglist;
1466 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1469 gfc_gimplify_function (tree fndecl)
1471 struct cgraph_node *cgn;
1473 gimplify_function_tree (fndecl);
1474 dump_function (TDI_generic, fndecl);
1476 /* Convert all nested functions to GIMPLE now. We do things in this order
1477 so that items like VLA sizes are expanded properly in the context of the
1478 correct function. */
1479 cgn = cgraph_node (fndecl);
1480 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1481 gfc_gimplify_function (cgn->decl);
1485 /* Do the setup necessary before generating the body of a function. */
1488 trans_function_start (gfc_symbol * sym)
1492 fndecl = sym->backend_decl;
1494 /* Let GCC know the current scope is this function. */
1495 current_function_decl = fndecl;
1497 /* Let the world know what we're about to do. */
1498 announce_function (fndecl);
1500 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1502 /* Create RTL for function declaration. */
1503 rest_of_decl_compilation (fndecl, 1, 0);
1506 /* Create RTL for function definition. */
1507 make_decl_rtl (fndecl);
1509 init_function_start (fndecl);
1511 /* Even though we're inside a function body, we still don't want to
1512 call expand_expr to calculate the size of a variable-sized array.
1513 We haven't necessarily assigned RTL to all variables yet, so it's
1514 not safe to try to expand expressions involving them. */
1515 cfun->x_dont_save_pending_sizes_p = 1;
1517 /* function.c requires a push at the start of the function. */
1521 /* Create thunks for alternate entry points. */
1524 build_entry_thunks (gfc_namespace * ns)
1526 gfc_formal_arglist *formal;
1527 gfc_formal_arglist *thunk_formal;
1529 gfc_symbol *thunk_sym;
1537 /* This should always be a toplevel function. */
1538 gcc_assert (current_function_decl == NULL_TREE);
1540 gfc_get_backend_locus (&old_loc);
1541 for (el = ns->entries; el; el = el->next)
1543 thunk_sym = el->sym;
1545 build_function_decl (thunk_sym);
1546 create_function_arglist (thunk_sym);
1548 trans_function_start (thunk_sym);
1550 thunk_fndecl = thunk_sym->backend_decl;
1552 gfc_start_block (&body);
1554 /* Pass extra parameter identifying this entry point. */
1555 tmp = build_int_cst (gfc_array_index_type, el->id);
1556 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1557 string_args = NULL_TREE;
1559 if (thunk_sym->attr.function)
1561 if (gfc_return_by_reference (ns->proc_name))
1563 tree ref = DECL_ARGUMENTS (current_function_decl);
1564 args = tree_cons (NULL_TREE, ref, args);
1565 if (ns->proc_name->ts.type == BT_CHARACTER)
1566 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1571 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1573 /* Ignore alternate returns. */
1574 if (formal->sym == NULL)
1577 /* We don't have a clever way of identifying arguments, so resort to
1578 a brute-force search. */
1579 for (thunk_formal = thunk_sym->formal;
1581 thunk_formal = thunk_formal->next)
1583 if (thunk_formal->sym == formal->sym)
1589 /* Pass the argument. */
1590 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1592 if (formal->sym->ts.type == BT_CHARACTER)
1594 tmp = thunk_formal->sym->ts.cl->backend_decl;
1595 string_args = tree_cons (NULL_TREE, tmp, string_args);
1600 /* Pass NULL for a missing argument. */
1601 args = tree_cons (NULL_TREE, null_pointer_node, args);
1602 if (formal->sym->ts.type == BT_CHARACTER)
1604 tmp = convert (gfc_charlen_type_node, integer_zero_node);
1605 string_args = tree_cons (NULL_TREE, tmp, string_args);
1610 /* Call the master function. */
1611 args = nreverse (args);
1612 args = chainon (args, nreverse (string_args));
1613 tmp = ns->proc_name->backend_decl;
1614 tmp = build_function_call_expr (tmp, args);
1615 if (ns->proc_name->attr.mixed_entry_master)
1617 tree union_decl, field;
1618 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1620 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1621 TREE_TYPE (master_type));
1622 DECL_ARTIFICIAL (union_decl) = 1;
1623 DECL_EXTERNAL (union_decl) = 0;
1624 TREE_PUBLIC (union_decl) = 0;
1625 TREE_USED (union_decl) = 1;
1626 layout_decl (union_decl, 0);
1627 pushdecl (union_decl);
1629 DECL_CONTEXT (union_decl) = current_function_decl;
1630 tmp = build2 (MODIFY_EXPR,
1631 TREE_TYPE (union_decl),
1633 gfc_add_expr_to_block (&body, tmp);
1635 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1636 field; field = TREE_CHAIN (field))
1637 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1638 thunk_sym->result->name) == 0)
1640 gcc_assert (field != NULL_TREE);
1641 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), union_decl, field,
1643 tmp = build2 (MODIFY_EXPR,
1644 TREE_TYPE (DECL_RESULT (current_function_decl)),
1645 DECL_RESULT (current_function_decl), tmp);
1646 tmp = build1_v (RETURN_EXPR, tmp);
1648 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1651 tmp = build2 (MODIFY_EXPR,
1652 TREE_TYPE (DECL_RESULT (current_function_decl)),
1653 DECL_RESULT (current_function_decl), tmp);
1654 tmp = build1_v (RETURN_EXPR, tmp);
1656 gfc_add_expr_to_block (&body, tmp);
1658 /* Finish off this function and send it for code generation. */
1659 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1661 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1663 /* Output the GENERIC tree. */
1664 dump_function (TDI_original, thunk_fndecl);
1666 /* Store the end of the function, so that we get good line number
1667 info for the epilogue. */
1668 cfun->function_end_locus = input_location;
1670 /* We're leaving the context of this function, so zap cfun.
1671 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1672 tree_rest_of_compilation. */
1675 current_function_decl = NULL_TREE;
1677 gfc_gimplify_function (thunk_fndecl);
1678 cgraph_finalize_function (thunk_fndecl, false);
1680 /* We share the symbols in the formal argument list with other entry
1681 points and the master function. Clear them so that they are
1682 recreated for each function. */
1683 for (formal = thunk_sym->formal; formal; formal = formal->next)
1684 if (formal->sym != NULL) /* Ignore alternate returns. */
1686 formal->sym->backend_decl = NULL_TREE;
1687 if (formal->sym->ts.type == BT_CHARACTER)
1688 formal->sym->ts.cl->backend_decl = NULL_TREE;
1691 if (thunk_sym->attr.function)
1693 if (thunk_sym->ts.type == BT_CHARACTER)
1694 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1695 if (thunk_sym->result->ts.type == BT_CHARACTER)
1696 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1700 gfc_set_backend_locus (&old_loc);
1704 /* Create a decl for a function, and create any thunks for alternate entry
1708 gfc_create_function_decl (gfc_namespace * ns)
1710 /* Create a declaration for the master function. */
1711 build_function_decl (ns->proc_name);
1713 /* Compile the entry thunks. */
1715 build_entry_thunks (ns);
1717 /* Now create the read argument list. */
1718 create_function_arglist (ns->proc_name);
1721 /* Return the decl used to hold the function return value. */
1724 gfc_get_fake_result_decl (gfc_symbol * sym)
1728 char name[GFC_MAX_SYMBOL_LEN + 10];
1731 && sym->ns->proc_name->backend_decl == current_function_decl
1732 && sym->ns->proc_name->attr.entry_master
1733 && sym != sym->ns->proc_name)
1736 if (current_fake_result_decl != NULL)
1737 for (t = TREE_CHAIN (current_fake_result_decl); t; t = TREE_CHAIN (t))
1738 if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
1741 return TREE_VALUE (t);
1742 decl = gfc_get_fake_result_decl (sym->ns->proc_name);
1743 if (decl && sym->ns->proc_name->attr.mixed_entry_master)
1747 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1748 field; field = TREE_CHAIN (field))
1749 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1753 gcc_assert (field != NULL_TREE);
1754 decl = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field,
1757 var = gfc_create_var (TREE_TYPE (decl), sym->name);
1758 SET_DECL_VALUE_EXPR (var, decl);
1759 DECL_HAS_VALUE_EXPR_P (var) = 1;
1760 TREE_CHAIN (current_fake_result_decl)
1761 = tree_cons (get_identifier (sym->name), var,
1762 TREE_CHAIN (current_fake_result_decl));
1766 if (current_fake_result_decl != NULL_TREE)
1767 return TREE_VALUE (current_fake_result_decl);
1769 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1774 if (sym->ts.type == BT_CHARACTER)
1776 if (sym->ts.cl->backend_decl == NULL_TREE)
1777 length = gfc_create_string_length (sym);
1779 length = sym->ts.cl->backend_decl;
1780 if (TREE_CODE (length) == VAR_DECL
1781 && DECL_CONTEXT (length) == NULL_TREE)
1782 gfc_add_decl_to_function (length);
1785 if (gfc_return_by_reference (sym))
1787 decl = DECL_ARGUMENTS (current_function_decl);
1789 if (sym->ns->proc_name->backend_decl == current_function_decl
1790 && sym->ns->proc_name->attr.entry_master)
1791 decl = TREE_CHAIN (decl);
1793 TREE_USED (decl) = 1;
1795 decl = gfc_build_dummy_array_decl (sym, decl);
1799 sprintf (name, "__result_%.20s",
1800 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1802 decl = build_decl (VAR_DECL, get_identifier (name),
1803 TREE_TYPE (TREE_TYPE (current_function_decl)));
1805 DECL_ARTIFICIAL (decl) = 1;
1806 DECL_EXTERNAL (decl) = 0;
1807 TREE_PUBLIC (decl) = 0;
1808 TREE_USED (decl) = 1;
1810 layout_decl (decl, 0);
1812 gfc_add_decl_to_function (decl);
1815 current_fake_result_decl = build_tree_list (NULL, decl);
1821 /* Builds a function decl. The remaining parameters are the types of the
1822 function arguments. Negative nargs indicates a varargs function. */
1825 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1834 /* Library functions must be declared with global scope. */
1835 gcc_assert (current_function_decl == NULL_TREE);
1837 va_start (p, nargs);
1840 /* Create a list of the argument types. */
1841 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1843 argtype = va_arg (p, tree);
1844 arglist = gfc_chainon_list (arglist, argtype);
1849 /* Terminate the list. */
1850 arglist = gfc_chainon_list (arglist, void_type_node);
1853 /* Build the function type and decl. */
1854 fntype = build_function_type (rettype, arglist);
1855 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1857 /* Mark this decl as external. */
1858 DECL_EXTERNAL (fndecl) = 1;
1859 TREE_PUBLIC (fndecl) = 1;
1865 rest_of_decl_compilation (fndecl, 1, 0);
1871 gfc_build_intrinsic_function_decls (void)
1873 tree gfc_int4_type_node = gfc_get_int_type (4);
1874 tree gfc_int8_type_node = gfc_get_int_type (8);
1875 tree gfc_int16_type_node = gfc_get_int_type (16);
1876 tree gfc_logical4_type_node = gfc_get_logical_type (4);
1877 tree gfc_real4_type_node = gfc_get_real_type (4);
1878 tree gfc_real8_type_node = gfc_get_real_type (8);
1879 tree gfc_real10_type_node = gfc_get_real_type (10);
1880 tree gfc_real16_type_node = gfc_get_real_type (16);
1881 tree gfc_complex4_type_node = gfc_get_complex_type (4);
1882 tree gfc_complex8_type_node = gfc_get_complex_type (8);
1883 tree gfc_complex10_type_node = gfc_get_complex_type (10);
1884 tree gfc_complex16_type_node = gfc_get_complex_type (16);
1885 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
1887 /* String functions. */
1888 gfor_fndecl_copy_string =
1889 gfc_build_library_function_decl (get_identifier (PREFIX("copy_string")),
1892 gfc_charlen_type_node, pchar_type_node,
1893 gfc_charlen_type_node, pchar_type_node);
1895 gfor_fndecl_compare_string =
1896 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
1899 gfc_charlen_type_node, pchar_type_node,
1900 gfc_charlen_type_node, pchar_type_node);
1902 gfor_fndecl_concat_string =
1903 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
1906 gfc_charlen_type_node, pchar_type_node,
1907 gfc_charlen_type_node, pchar_type_node,
1908 gfc_charlen_type_node, pchar_type_node);
1910 gfor_fndecl_string_len_trim =
1911 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
1913 2, gfc_charlen_type_node,
1916 gfor_fndecl_string_index =
1917 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
1919 5, gfc_charlen_type_node, pchar_type_node,
1920 gfc_charlen_type_node, pchar_type_node,
1921 gfc_logical4_type_node);
1923 gfor_fndecl_string_scan =
1924 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
1926 5, gfc_charlen_type_node, pchar_type_node,
1927 gfc_charlen_type_node, pchar_type_node,
1928 gfc_logical4_type_node);
1930 gfor_fndecl_string_verify =
1931 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
1933 5, gfc_charlen_type_node, pchar_type_node,
1934 gfc_charlen_type_node, pchar_type_node,
1935 gfc_logical4_type_node);
1937 gfor_fndecl_string_trim =
1938 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
1941 build_pointer_type (gfc_charlen_type_node),
1943 gfc_charlen_type_node,
1946 gfor_fndecl_string_repeat =
1947 gfc_build_library_function_decl (get_identifier (PREFIX("string_repeat")),
1951 gfc_charlen_type_node,
1953 gfc_int4_type_node);
1955 gfor_fndecl_ttynam =
1956 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
1960 gfc_charlen_type_node,
1961 gfc_c_int_type_node);
1964 gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
1968 gfc_charlen_type_node);
1971 gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
1975 gfc_charlen_type_node,
1976 gfc_int8_type_node);
1978 gfor_fndecl_adjustl =
1979 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
1983 gfc_charlen_type_node, pchar_type_node);
1985 gfor_fndecl_adjustr =
1986 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
1990 gfc_charlen_type_node, pchar_type_node);
1992 gfor_fndecl_si_kind =
1993 gfc_build_library_function_decl (get_identifier ("selected_int_kind"),
1998 gfor_fndecl_sr_kind =
1999 gfc_build_library_function_decl (get_identifier ("selected_real_kind"),
2004 /* Power functions. */
2006 tree ctype, rtype, itype, jtype;
2007 int rkind, ikind, jkind;
2010 static int ikinds[NIKINDS] = {4, 8, 16};
2011 static int rkinds[NRKINDS] = {4, 8, 10, 16};
2012 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
2014 for (ikind=0; ikind < NIKINDS; ikind++)
2016 itype = gfc_get_int_type (ikinds[ikind]);
2018 for (jkind=0; jkind < NIKINDS; jkind++)
2020 jtype = gfc_get_int_type (ikinds[jkind]);
2023 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
2025 gfor_fndecl_math_powi[jkind][ikind].integer =
2026 gfc_build_library_function_decl (get_identifier (name),
2027 jtype, 2, jtype, itype);
2031 for (rkind = 0; rkind < NRKINDS; rkind ++)
2033 rtype = gfc_get_real_type (rkinds[rkind]);
2036 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
2038 gfor_fndecl_math_powi[rkind][ikind].real =
2039 gfc_build_library_function_decl (get_identifier (name),
2040 rtype, 2, rtype, itype);
2043 ctype = gfc_get_complex_type (rkinds[rkind]);
2046 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
2048 gfor_fndecl_math_powi[rkind][ikind].cmplx =
2049 gfc_build_library_function_decl (get_identifier (name),
2050 ctype, 2,ctype, itype);
2058 gfor_fndecl_math_cpowf =
2059 gfc_build_library_function_decl (get_identifier ("cpowf"),
2060 gfc_complex4_type_node,
2061 1, gfc_complex4_type_node);
2062 gfor_fndecl_math_cpow =
2063 gfc_build_library_function_decl (get_identifier ("cpow"),
2064 gfc_complex8_type_node,
2065 1, gfc_complex8_type_node);
2066 if (gfc_complex10_type_node)
2067 gfor_fndecl_math_cpowl10 =
2068 gfc_build_library_function_decl (get_identifier ("cpowl"),
2069 gfc_complex10_type_node, 1,
2070 gfc_complex10_type_node);
2071 if (gfc_complex16_type_node)
2072 gfor_fndecl_math_cpowl16 =
2073 gfc_build_library_function_decl (get_identifier ("cpowl"),
2074 gfc_complex16_type_node, 1,
2075 gfc_complex16_type_node);
2077 gfor_fndecl_math_ishftc4 =
2078 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2080 3, gfc_int4_type_node,
2081 gfc_int4_type_node, gfc_int4_type_node);
2082 gfor_fndecl_math_ishftc8 =
2083 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2085 3, gfc_int8_type_node,
2086 gfc_int4_type_node, gfc_int4_type_node);
2087 if (gfc_int16_type_node)
2088 gfor_fndecl_math_ishftc16 =
2089 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2090 gfc_int16_type_node, 3,
2091 gfc_int16_type_node,
2093 gfc_int4_type_node);
2095 gfor_fndecl_math_exponent4 =
2096 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
2098 1, gfc_real4_type_node);
2099 gfor_fndecl_math_exponent8 =
2100 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
2102 1, gfc_real8_type_node);
2103 if (gfc_real10_type_node)
2104 gfor_fndecl_math_exponent10 =
2105 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r10")),
2106 gfc_int4_type_node, 1,
2107 gfc_real10_type_node);
2108 if (gfc_real16_type_node)
2109 gfor_fndecl_math_exponent16 =
2110 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r16")),
2111 gfc_int4_type_node, 1,
2112 gfc_real16_type_node);
2114 /* Other functions. */
2116 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2117 gfc_array_index_type,
2118 1, pvoid_type_node);
2120 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2121 gfc_array_index_type,
2123 gfc_array_index_type);
2126 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2132 /* Make prototypes for runtime library functions. */
2135 gfc_build_builtin_function_decls (void)
2137 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2138 tree gfc_int4_type_node = gfc_get_int_type (4);
2139 tree gfc_int8_type_node = gfc_get_int_type (8);
2140 tree gfc_logical4_type_node = gfc_get_logical_type (4);
2141 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
2143 /* Treat these two internal malloc wrappers as malloc. */
2144 gfor_fndecl_internal_malloc =
2145 gfc_build_library_function_decl (get_identifier (PREFIX("internal_malloc")),
2146 pvoid_type_node, 1, gfc_int4_type_node);
2147 DECL_IS_MALLOC (gfor_fndecl_internal_malloc) = 1;
2149 gfor_fndecl_internal_malloc64 =
2150 gfc_build_library_function_decl (get_identifier
2151 (PREFIX("internal_malloc64")),
2152 pvoid_type_node, 1, gfc_int8_type_node);
2153 DECL_IS_MALLOC (gfor_fndecl_internal_malloc64) = 1;
2155 gfor_fndecl_internal_realloc =
2156 gfc_build_library_function_decl (get_identifier
2157 (PREFIX("internal_realloc")),
2158 pvoid_type_node, 2, pvoid_type_node,
2159 gfc_int4_type_node);
2161 gfor_fndecl_internal_realloc64 =
2162 gfc_build_library_function_decl (get_identifier
2163 (PREFIX("internal_realloc64")),
2164 pvoid_type_node, 2, pvoid_type_node,
2165 gfc_int8_type_node);
2167 gfor_fndecl_internal_free =
2168 gfc_build_library_function_decl (get_identifier (PREFIX("internal_free")),
2169 void_type_node, 1, pvoid_type_node);
2171 gfor_fndecl_allocate =
2172 gfc_build_library_function_decl (get_identifier (PREFIX("allocate")),
2173 void_type_node, 2, ppvoid_type_node,
2174 gfc_int4_type_node);
2176 gfor_fndecl_allocate64 =
2177 gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")),
2178 void_type_node, 2, ppvoid_type_node,
2179 gfc_int8_type_node);
2181 gfor_fndecl_deallocate =
2182 gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")),
2183 void_type_node, 2, ppvoid_type_node,
2184 gfc_pint4_type_node);
2186 gfor_fndecl_stop_numeric =
2187 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2188 void_type_node, 1, gfc_int4_type_node);
2190 /* Stop doesn't return. */
2191 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2193 gfor_fndecl_stop_string =
2194 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2195 void_type_node, 2, pchar_type_node,
2196 gfc_int4_type_node);
2197 /* Stop doesn't return. */
2198 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2200 gfor_fndecl_pause_numeric =
2201 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2202 void_type_node, 1, gfc_int4_type_node);
2204 gfor_fndecl_pause_string =
2205 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2206 void_type_node, 2, pchar_type_node,
2207 gfc_int4_type_node);
2209 gfor_fndecl_select_string =
2210 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2211 pvoid_type_node, 0);
2213 gfor_fndecl_runtime_error =
2214 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2217 pchar_type_node, pchar_type_node,
2218 gfc_int4_type_node);
2219 /* The runtime_error function does not return. */
2220 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2222 gfor_fndecl_set_fpe =
2223 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2224 void_type_node, 1, gfc_c_int_type_node);
2226 gfor_fndecl_set_std =
2227 gfc_build_library_function_decl (get_identifier (PREFIX("set_std")),
2232 gfc_int4_type_node);
2234 gfor_fndecl_set_convert =
2235 gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2236 void_type_node, 1, gfc_c_int_type_node);
2238 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2239 get_identifier (PREFIX("internal_pack")),
2240 pvoid_type_node, 1, pvoid_type_node);
2242 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2243 get_identifier (PREFIX("internal_unpack")),
2244 pvoid_type_node, 1, pvoid_type_node);
2246 gfor_fndecl_associated =
2247 gfc_build_library_function_decl (
2248 get_identifier (PREFIX("associated")),
2249 gfc_logical4_type_node,
2254 gfc_build_intrinsic_function_decls ();
2255 gfc_build_intrinsic_lib_fndecls ();
2256 gfc_build_io_library_fndecls ();
2260 /* Evaluate the length of dummy character variables. */
2263 gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl, tree fnbody)
2267 gfc_finish_decl (cl->backend_decl, NULL_TREE);
2269 gfc_start_block (&body);
2271 /* Evaluate the string length expression. */
2272 gfc_trans_init_string_length (cl, &body);
2274 gfc_trans_vla_type_sizes (sym, &body);
2276 gfc_add_expr_to_block (&body, fnbody);
2277 return gfc_finish_block (&body);
2281 /* Allocate and cleanup an automatic character variable. */
2284 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2290 gcc_assert (sym->backend_decl);
2291 gcc_assert (sym->ts.cl && sym->ts.cl->length);
2293 gfc_start_block (&body);
2295 /* Evaluate the string length expression. */
2296 gfc_trans_init_string_length (sym->ts.cl, &body);
2298 gfc_trans_vla_type_sizes (sym, &body);
2300 decl = sym->backend_decl;
2302 /* Emit a DECL_EXPR for this variable, which will cause the
2303 gimplifier to allocate storage, and all that good stuff. */
2304 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2305 gfc_add_expr_to_block (&body, tmp);
2307 gfc_add_expr_to_block (&body, fnbody);
2308 return gfc_finish_block (&body);
2311 /* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2314 gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2318 gcc_assert (sym->backend_decl);
2319 gfc_start_block (&body);
2321 /* Set the initial value to length. See the comments in
2322 function gfc_add_assign_aux_vars in this file. */
2323 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2324 build_int_cst (NULL_TREE, -2));
2326 gfc_add_expr_to_block (&body, fnbody);
2327 return gfc_finish_block (&body);
2331 gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
2333 tree t = *tp, var, val;
2335 if (t == NULL || t == error_mark_node)
2337 if (TREE_CONSTANT (t) || DECL_P (t))
2340 if (TREE_CODE (t) == SAVE_EXPR)
2342 if (SAVE_EXPR_RESOLVED_P (t))
2344 *tp = TREE_OPERAND (t, 0);
2347 val = TREE_OPERAND (t, 0);
2352 var = gfc_create_var_np (TREE_TYPE (t), NULL);
2353 gfc_add_decl_to_function (var);
2354 gfc_add_modify_expr (body, var, val);
2355 if (TREE_CODE (t) == SAVE_EXPR)
2356 TREE_OPERAND (t, 0) = var;
2361 gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
2365 if (type == NULL || type == error_mark_node)
2368 type = TYPE_MAIN_VARIANT (type);
2370 if (TREE_CODE (type) == INTEGER_TYPE)
2372 gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
2373 gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
2375 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2377 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
2378 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
2381 else if (TREE_CODE (type) == ARRAY_TYPE)
2383 gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
2384 gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
2385 gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
2386 gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
2388 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2390 TYPE_SIZE (t) = TYPE_SIZE (type);
2391 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
2396 /* Make sure all type sizes and array domains are either constant,
2397 or variable or parameter decls. This is a simplified variant
2398 of gimplify_type_sizes, but we can't use it here, as none of the
2399 variables in the expressions have been gimplified yet.
2400 As type sizes and domains for various variable length arrays
2401 contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
2402 time, without this routine gimplify_type_sizes in the middle-end
2403 could result in the type sizes being gimplified earlier than where
2404 those variables are initialized. */
2407 gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
2409 tree type = TREE_TYPE (sym->backend_decl);
2411 if (TREE_CODE (type) == FUNCTION_TYPE
2412 && (sym->attr.function || sym->attr.result || sym->attr.entry))
2414 if (! current_fake_result_decl)
2417 type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
2420 while (POINTER_TYPE_P (type))
2421 type = TREE_TYPE (type);
2423 if (GFC_DESCRIPTOR_TYPE_P (type))
2425 tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2427 while (POINTER_TYPE_P (etype))
2428 etype = TREE_TYPE (etype);
2430 gfc_trans_vla_type_sizes_1 (etype, body);
2433 gfc_trans_vla_type_sizes_1 (type, body);
2437 /* Generate function entry and exit code, and add it to the function body.
2439 Allocation and initialization of array variables.
2440 Allocation of character string variables.
2441 Initialization and possibly repacking of dummy arrays.
2442 Initialization of ASSIGN statement auxiliary variable. */
2445 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2449 gfc_formal_arglist *f;
2452 /* Deal with implicit return variables. Explicit return variables will
2453 already have been added. */
2454 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2456 if (!current_fake_result_decl)
2458 gfc_entry_list *el = NULL;
2459 if (proc_sym->attr.entry_master)
2461 for (el = proc_sym->ns->entries; el; el = el->next)
2462 if (el->sym != el->sym->result)
2466 warning (0, "Function does not return a value");
2468 else if (proc_sym->as)
2470 tree result = TREE_VALUE (current_fake_result_decl);
2471 fnbody = gfc_trans_dummy_array_bias (proc_sym, result, fnbody);
2473 else if (proc_sym->ts.type == BT_CHARACTER)
2475 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2476 fnbody = gfc_trans_dummy_character (proc_sym, proc_sym->ts.cl,
2480 gcc_assert (gfc_option.flag_f2c
2481 && proc_sym->ts.type == BT_COMPLEX);
2484 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2486 if (sym->attr.dimension)
2488 switch (sym->as->type)
2491 if (sym->attr.dummy || sym->attr.result)
2493 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2494 else if (sym->attr.pointer || sym->attr.allocatable)
2496 if (TREE_STATIC (sym->backend_decl))
2497 gfc_trans_static_array_pointer (sym);
2499 fnbody = gfc_trans_deferred_array (sym, fnbody);
2503 gfc_get_backend_locus (&loc);
2504 gfc_set_backend_locus (&sym->declared_at);
2505 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2507 gfc_set_backend_locus (&loc);
2511 case AS_ASSUMED_SIZE:
2512 /* Must be a dummy parameter. */
2513 gcc_assert (sym->attr.dummy);
2515 /* We should always pass assumed size arrays the g77 way. */
2516 fnbody = gfc_trans_g77_array (sym, fnbody);
2519 case AS_ASSUMED_SHAPE:
2520 /* Must be a dummy parameter. */
2521 gcc_assert (sym->attr.dummy);
2523 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2528 fnbody = gfc_trans_deferred_array (sym, fnbody);
2535 else if (sym->ts.type == BT_CHARACTER)
2537 gfc_get_backend_locus (&loc);
2538 gfc_set_backend_locus (&sym->declared_at);
2539 if (sym->attr.dummy || sym->attr.result)
2540 fnbody = gfc_trans_dummy_character (sym, sym->ts.cl, fnbody);
2542 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2543 gfc_set_backend_locus (&loc);
2545 else if (sym->attr.assign)
2547 gfc_get_backend_locus (&loc);
2548 gfc_set_backend_locus (&sym->declared_at);
2549 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2550 gfc_set_backend_locus (&loc);
2556 gfc_init_block (&body);
2558 for (f = proc_sym->formal; f; f = f->next)
2559 if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER)
2561 gcc_assert (f->sym->ts.cl->backend_decl != NULL);
2562 if (TREE_CODE (f->sym->ts.cl->backend_decl) == PARM_DECL)
2563 gfc_trans_vla_type_sizes (f->sym, &body);
2566 if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
2567 && current_fake_result_decl != NULL)
2569 gcc_assert (proc_sym->ts.cl->backend_decl != NULL);
2570 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == PARM_DECL)
2571 gfc_trans_vla_type_sizes (proc_sym, &body);
2574 gfc_add_expr_to_block (&body, fnbody);
2575 return gfc_finish_block (&body);
2579 /* Output an initialized decl for a module variable. */
2582 gfc_create_module_variable (gfc_symbol * sym)
2586 /* Only output symbols from this module. */
2587 if (sym->ns != module_namespace)
2589 /* I don't think this should ever happen. */
2590 internal_error ("module symbol %s in wrong namespace", sym->name);
2593 /* Only output variables and array valued parameters. */
2594 if (sym->attr.flavor != FL_VARIABLE
2595 && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0))
2598 /* Don't generate variables from other modules. Variables from
2599 COMMONs will already have been generated. */
2600 if (sym->attr.use_assoc || sym->attr.in_common)
2603 /* Equivalenced variables arrive here after creation. */
2604 if (sym->backend_decl
2605 && (sym->equiv_built || sym->attr.in_equivalence))
2608 if (sym->backend_decl)
2609 internal_error ("backend decl for module variable %s already exists",
2612 /* We always want module variables to be created. */
2613 sym->attr.referenced = 1;
2614 /* Create the decl. */
2615 decl = gfc_get_symbol_decl (sym);
2617 /* Create the variable. */
2619 rest_of_decl_compilation (decl, 1, 0);
2621 /* Also add length of strings. */
2622 if (sym->ts.type == BT_CHARACTER)
2626 length = sym->ts.cl->backend_decl;
2627 if (!INTEGER_CST_P (length))
2630 rest_of_decl_compilation (length, 1, 0);
2636 /* Generate all the required code for module variables. */
2639 gfc_generate_module_vars (gfc_namespace * ns)
2641 module_namespace = ns;
2643 /* Check if the frontend left the namespace in a reasonable state. */
2644 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2646 /* Generate COMMON blocks. */
2647 gfc_trans_common (ns);
2649 /* Create decls for all the module variables. */
2650 gfc_traverse_ns (ns, gfc_create_module_variable);
2654 gfc_generate_contained_functions (gfc_namespace * parent)
2658 /* We create all the prototypes before generating any code. */
2659 for (ns = parent->contained; ns; ns = ns->sibling)
2661 /* Skip namespaces from used modules. */
2662 if (ns->parent != parent)
2665 gfc_create_function_decl (ns);
2668 for (ns = parent->contained; ns; ns = ns->sibling)
2670 /* Skip namespaces from used modules. */
2671 if (ns->parent != parent)
2674 gfc_generate_function_code (ns);
2679 /* Generate decls for all local variables. We do this to ensure correct
2680 handling of expressions which only appear in the specification of
2684 generate_local_decl (gfc_symbol * sym)
2686 if (sym->attr.flavor == FL_VARIABLE)
2688 if (sym->attr.referenced)
2689 gfc_get_symbol_decl (sym);
2690 else if (sym->attr.dummy && warn_unused_parameter)
2691 warning (0, "unused parameter %qs", sym->name);
2692 /* Warn for unused variables, but not if they're inside a common
2693 block or are use-associated. */
2694 else if (warn_unused_variable
2695 && !(sym->attr.in_common || sym->attr.use_assoc))
2696 warning (0, "unused variable %qs", sym->name);
2697 /* For variable length CHARACTER parameters, the PARM_DECL already
2698 references the length variable, so force gfc_get_symbol_decl
2699 even when not referenced. If optimize > 0, it will be optimized
2700 away anyway. But do this only after emitting -Wunused-parameter
2701 warning if requested. */
2702 if (sym->attr.dummy && ! sym->attr.referenced
2703 && sym->ts.type == BT_CHARACTER
2704 && sym->ts.cl->backend_decl != NULL
2705 && TREE_CODE (sym->ts.cl->backend_decl) == VAR_DECL)
2707 sym->attr.referenced = 1;
2708 gfc_get_symbol_decl (sym);
2714 generate_local_vars (gfc_namespace * ns)
2716 gfc_traverse_ns (ns, generate_local_decl);
2720 /* Generate a switch statement to jump to the correct entry point. Also
2721 creates the label decls for the entry points. */
2724 gfc_trans_entry_master_switch (gfc_entry_list * el)
2731 gfc_init_block (&block);
2732 for (; el; el = el->next)
2734 /* Add the case label. */
2735 label = gfc_build_label_decl (NULL_TREE);
2736 val = build_int_cst (gfc_array_index_type, el->id);
2737 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
2738 gfc_add_expr_to_block (&block, tmp);
2740 /* And jump to the actual entry point. */
2741 label = gfc_build_label_decl (NULL_TREE);
2742 tmp = build1_v (GOTO_EXPR, label);
2743 gfc_add_expr_to_block (&block, tmp);
2745 /* Save the label decl. */
2748 tmp = gfc_finish_block (&block);
2749 /* The first argument selects the entry point. */
2750 val = DECL_ARGUMENTS (current_function_decl);
2751 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
2756 /* Generate code for a function. */
2759 gfc_generate_function_code (gfc_namespace * ns)
2770 sym = ns->proc_name;
2772 /* Check that the frontend isn't still using this. */
2773 gcc_assert (sym->tlink == NULL);
2776 /* Create the declaration for functions with global scope. */
2777 if (!sym->backend_decl)
2778 gfc_create_function_decl (ns);
2780 fndecl = sym->backend_decl;
2781 old_context = current_function_decl;
2785 push_function_context ();
2786 saved_parent_function_decls = saved_function_decls;
2787 saved_function_decls = NULL_TREE;
2790 trans_function_start (sym);
2792 gfc_start_block (&block);
2794 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
2796 /* Copy length backend_decls to all entry point result
2801 gfc_conv_const_charlen (ns->proc_name->ts.cl);
2802 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
2803 for (el = ns->entries; el; el = el->next)
2804 el->sym->result->ts.cl->backend_decl = backend_decl;
2807 /* Translate COMMON blocks. */
2808 gfc_trans_common (ns);
2810 gfc_generate_contained_functions (ns);
2812 generate_local_vars (ns);
2814 /* Will be created as needed. */
2815 current_fake_result_decl = NULL_TREE;
2816 current_function_return_label = NULL;
2818 /* Now generate the code for the body of this function. */
2819 gfc_init_block (&body);
2821 /* If this is the main program, add a call to set_std to set up the
2822 runtime library Fortran language standard parameters. */
2824 if (sym->attr.is_main_program)
2826 tree arglist, gfc_int4_type_node;
2828 gfc_int4_type_node = gfc_get_int_type (4);
2829 arglist = gfc_chainon_list (NULL_TREE,
2830 build_int_cst (gfc_int4_type_node,
2831 gfc_option.warn_std));
2832 arglist = gfc_chainon_list (arglist,
2833 build_int_cst (gfc_int4_type_node,
2834 gfc_option.allow_std));
2835 arglist = gfc_chainon_list (arglist,
2836 build_int_cst (gfc_int4_type_node,
2838 tmp = build_function_call_expr (gfor_fndecl_set_std, arglist);
2839 gfc_add_expr_to_block (&body, tmp);
2842 /* If this is the main program and a -ffpe-trap option was provided,
2843 add a call to set_fpe so that the library will raise a FPE when
2845 if (sym->attr.is_main_program && gfc_option.fpe != 0)
2847 tree arglist, gfc_c_int_type_node;
2849 gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2850 arglist = gfc_chainon_list (NULL_TREE,
2851 build_int_cst (gfc_c_int_type_node,
2853 tmp = build_function_call_expr (gfor_fndecl_set_fpe, arglist);
2854 gfc_add_expr_to_block (&body, tmp);
2857 /* If this is the main program and an -fconvert option was provided,
2858 add a call to set_convert. */
2860 if (sym->attr.is_main_program && gfc_option.convert != CONVERT_NATIVE)
2862 tree arglist, gfc_c_int_type_node;
2864 gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2865 arglist = gfc_chainon_list (NULL_TREE,
2866 build_int_cst (gfc_c_int_type_node,
2867 gfc_option.convert));
2868 tmp = build_function_call_expr (gfor_fndecl_set_convert, arglist);
2869 gfc_add_expr_to_block (&body, tmp);
2873 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
2874 && sym->attr.subroutine)
2876 tree alternate_return;
2877 alternate_return = gfc_get_fake_result_decl (sym);
2878 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
2883 /* Jump to the correct entry point. */
2884 tmp = gfc_trans_entry_master_switch (ns->entries);
2885 gfc_add_expr_to_block (&body, tmp);
2888 tmp = gfc_trans_code (ns->code);
2889 gfc_add_expr_to_block (&body, tmp);
2891 /* Add a return label if needed. */
2892 if (current_function_return_label)
2894 tmp = build1_v (LABEL_EXPR, current_function_return_label);
2895 gfc_add_expr_to_block (&body, tmp);
2898 tmp = gfc_finish_block (&body);
2899 /* Add code to create and cleanup arrays. */
2900 tmp = gfc_trans_deferred_vars (sym, tmp);
2901 gfc_add_expr_to_block (&block, tmp);
2903 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
2905 if (sym->attr.subroutine || sym == sym->result)
2907 if (current_fake_result_decl != NULL)
2908 result = TREE_VALUE (current_fake_result_decl);
2911 current_fake_result_decl = NULL_TREE;
2914 result = sym->result->backend_decl;
2916 if (result == NULL_TREE)
2917 warning (0, "Function return value not set");
2920 /* Set the return value to the dummy result variable. */
2921 tmp = build2 (MODIFY_EXPR, TREE_TYPE (result),
2922 DECL_RESULT (fndecl), result);
2923 tmp = build1_v (RETURN_EXPR, tmp);
2924 gfc_add_expr_to_block (&block, tmp);
2928 /* Add all the decls we created during processing. */
2929 decl = saved_function_decls;
2934 next = TREE_CHAIN (decl);
2935 TREE_CHAIN (decl) = NULL_TREE;
2939 saved_function_decls = NULL_TREE;
2941 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
2943 /* Finish off this function and send it for code generation. */
2945 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2947 /* Output the GENERIC tree. */
2948 dump_function (TDI_original, fndecl);
2950 /* Store the end of the function, so that we get good line number
2951 info for the epilogue. */
2952 cfun->function_end_locus = input_location;
2954 /* We're leaving the context of this function, so zap cfun.
2955 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2956 tree_rest_of_compilation. */
2961 pop_function_context ();
2962 saved_function_decls = saved_parent_function_decls;
2964 current_function_decl = old_context;
2966 if (decl_function_context (fndecl))
2967 /* Register this function with cgraph just far enough to get it
2968 added to our parent's nested function list. */
2969 (void) cgraph_node (fndecl);
2972 gfc_gimplify_function (fndecl);
2973 cgraph_finalize_function (fndecl, false);
2978 gfc_generate_constructors (void)
2980 gcc_assert (gfc_static_ctors == NULL_TREE);
2988 if (gfc_static_ctors == NULL_TREE)
2991 fnname = get_file_function_name ('I');
2992 type = build_function_type (void_type_node,
2993 gfc_chainon_list (NULL_TREE, void_type_node));
2995 fndecl = build_decl (FUNCTION_DECL, fnname, type);
2996 TREE_PUBLIC (fndecl) = 1;
2998 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
2999 DECL_ARTIFICIAL (decl) = 1;
3000 DECL_IGNORED_P (decl) = 1;
3001 DECL_CONTEXT (decl) = fndecl;
3002 DECL_RESULT (fndecl) = decl;
3006 current_function_decl = fndecl;
3008 rest_of_decl_compilation (fndecl, 1, 0);
3010 make_decl_rtl (fndecl);
3012 init_function_start (fndecl);
3016 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
3019 build_function_call_expr (TREE_VALUE (gfc_static_ctors), NULL_TREE);
3020 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
3025 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
3027 free_after_parsing (cfun);
3028 free_after_compilation (cfun);
3030 tree_rest_of_compilation (fndecl);
3032 current_function_decl = NULL_TREE;
3036 /* Translates a BLOCK DATA program unit. This means emitting the
3037 commons contained therein plus their initializations. We also emit
3038 a globally visible symbol to make sure that each BLOCK DATA program
3039 unit remains unique. */
3042 gfc_generate_block_data (gfc_namespace * ns)
3047 /* Tell the backend the source location of the block data. */
3049 gfc_set_backend_locus (&ns->proc_name->declared_at);
3051 gfc_set_backend_locus (&gfc_current_locus);
3053 /* Process the DATA statements. */
3054 gfc_trans_common (ns);
3056 /* Create a global symbol with the mane of the block data. This is to
3057 generate linker errors if the same name is used twice. It is never
3060 id = gfc_sym_mangled_function_id (ns->proc_name);
3062 id = get_identifier ("__BLOCK_DATA__");
3064 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
3065 TREE_PUBLIC (decl) = 1;
3066 TREE_STATIC (decl) = 1;
3069 rest_of_decl_compilation (decl, 1, 0);
3073 #include "gt-fortran-trans-decl.h"