1 /* Backend function setup
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* trans-decl.c -- Handling of backend function and variable decls, etc */
26 #include "coretypes.h"
28 #include "tree-dump.h"
29 #include "tree-gimple.h"
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;
54 static GTY(()) tree current_function_return_label;
57 /* Holds the variable DECLs for the current function. */
59 static GTY(()) tree saved_function_decls = NULL_TREE;
60 static GTY(()) tree saved_parent_function_decls = NULL_TREE;
63 /* The namespace of the module we're currently generating. Only used while
64 outputting decls for module variables. Do not rely on this being set. */
66 static gfc_namespace *module_namespace;
69 /* List of static constructor functions. */
71 tree gfc_static_ctors;
74 /* Function declarations for builtin library functions. */
76 tree gfor_fndecl_internal_malloc;
77 tree gfor_fndecl_internal_malloc64;
78 tree gfor_fndecl_internal_free;
79 tree gfor_fndecl_allocate;
80 tree gfor_fndecl_allocate64;
81 tree gfor_fndecl_deallocate;
82 tree gfor_fndecl_pause_numeric;
83 tree gfor_fndecl_pause_string;
84 tree gfor_fndecl_stop_numeric;
85 tree gfor_fndecl_stop_string;
86 tree gfor_fndecl_select_string;
87 tree gfor_fndecl_runtime_error;
88 tree gfor_fndecl_in_pack;
89 tree gfor_fndecl_in_unpack;
90 tree gfor_fndecl_associated;
93 /* Math functions. Many other math functions are handled in
96 gfc_powdecl_list gfor_fndecl_math_powi[3][2];
97 tree gfor_fndecl_math_cpowf;
98 tree gfor_fndecl_math_cpow;
99 tree gfor_fndecl_math_ishftc4;
100 tree gfor_fndecl_math_ishftc8;
101 tree gfor_fndecl_math_exponent4;
102 tree gfor_fndecl_math_exponent8;
105 /* String functions. */
107 tree gfor_fndecl_copy_string;
108 tree gfor_fndecl_compare_string;
109 tree gfor_fndecl_concat_string;
110 tree gfor_fndecl_string_len_trim;
111 tree gfor_fndecl_string_index;
112 tree gfor_fndecl_string_scan;
113 tree gfor_fndecl_string_verify;
114 tree gfor_fndecl_string_trim;
115 tree gfor_fndecl_string_repeat;
116 tree gfor_fndecl_adjustl;
117 tree gfor_fndecl_adjustr;
120 /* Other misc. runtime library functions. */
122 tree gfor_fndecl_size0;
123 tree gfor_fndecl_size1;
124 tree gfor_fndecl_iargc;
126 /* Intrinsic functions implemented in FORTRAN. */
127 tree gfor_fndecl_si_kind;
128 tree gfor_fndecl_sr_kind;
132 gfc_add_decl_to_parent_function (tree decl)
135 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
136 DECL_NONLOCAL (decl) = 1;
137 TREE_CHAIN (decl) = saved_parent_function_decls;
138 saved_parent_function_decls = decl;
142 gfc_add_decl_to_function (tree decl)
145 TREE_USED (decl) = 1;
146 DECL_CONTEXT (decl) = current_function_decl;
147 TREE_CHAIN (decl) = saved_function_decls;
148 saved_function_decls = decl;
152 /* Build a backend label declaration.
153 Set TREE_USED for named lables. For artificial labels it's up to the
154 caller to mark the label as used. */
157 gfc_build_label_decl (tree label_id)
159 /* 2^32 temporaries should be enough. */
160 static unsigned int tmp_num = 1;
164 if (label_id == NULL_TREE)
166 /* Build an internal label name. */
167 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
168 label_id = get_identifier (label_name);
173 /* Build the LABEL_DECL node. Labels have no type. */
174 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
175 DECL_CONTEXT (label_decl) = current_function_decl;
176 DECL_MODE (label_decl) = VOIDmode;
180 DECL_ARTIFICIAL (label_decl) = 1;
184 /* We always define the label as used, even if the original source
185 file never references the label. We don't want all kinds of
186 spurious warnings for old-style Fortran code with too many
188 TREE_USED (label_decl) = 1;
195 /* Returns the return label for the current function. */
198 gfc_get_return_label (void)
200 char name[GFC_MAX_SYMBOL_LEN + 10];
202 if (current_function_return_label)
203 return current_function_return_label;
205 sprintf (name, "__return_%s",
206 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
208 current_function_return_label =
209 gfc_build_label_decl (get_identifier (name));
211 DECL_ARTIFICIAL (current_function_return_label) = 1;
213 return current_function_return_label;
217 /* Return the backend label declaration for a given label structure,
218 or create it if it doesn't exist yet. */
221 gfc_get_label_decl (gfc_st_label * lp)
223 if (lp->backend_decl)
224 return lp->backend_decl;
227 char label_name[GFC_MAX_SYMBOL_LEN + 1];
230 /* Validate the label declaration from the front end. */
231 assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
233 /* Build a mangled name for the label. */
234 sprintf (label_name, "__label_%.6d", lp->value);
236 /* Build the LABEL_DECL node. */
237 label_decl = gfc_build_label_decl (get_identifier (label_name));
239 /* Tell the debugger where the label came from. */
240 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
242 DECL_SOURCE_LINE (label_decl) = lp->where.lb->linenum;
243 DECL_SOURCE_FILE (label_decl) = lp->where.lb->file->filename;
246 DECL_ARTIFICIAL (label_decl) = 1;
248 /* Store the label in the label list and return the LABEL_DECL. */
249 lp->backend_decl = label_decl;
255 /* Convert a gfc_symbol to an identifier of the same name. */
258 gfc_sym_identifier (gfc_symbol * sym)
260 return (get_identifier (sym->name));
264 /* Construct mangled name from symbol name. */
267 gfc_sym_mangled_identifier (gfc_symbol * sym)
269 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
271 if (sym->module[0] == 0)
272 return gfc_sym_identifier (sym);
275 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
276 return get_identifier (name);
281 /* Construct mangled function name from symbol name. */
284 gfc_sym_mangled_function_id (gfc_symbol * sym)
287 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
289 if (sym->module[0] == 0 || sym->attr.proc == PROC_EXTERNAL
290 || (sym->module[0] != 0 && sym->attr.if_source == IFSRC_IFBODY))
292 if (strcmp (sym->name, "MAIN__") == 0
293 || sym->attr.proc == PROC_INTRINSIC)
294 return get_identifier (sym->name);
296 if (gfc_option.flag_underscoring)
298 has_underscore = strchr (sym->name, '_') != 0;
299 if (gfc_option.flag_second_underscore && has_underscore)
300 snprintf (name, sizeof name, "%s__", sym->name);
302 snprintf (name, sizeof name, "%s_", sym->name);
303 return get_identifier (name);
306 return get_identifier (sym->name);
310 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
311 return get_identifier (name);
316 /* Finish processing of a declaration and install its initial value. */
319 gfc_finish_decl (tree decl, tree init)
321 if (TREE_CODE (decl) == PARM_DECL)
322 assert (init == NULL_TREE);
323 /* Remember that PARM_DECL doesn't have a DECL_INITIAL field per se
324 -- it overlaps DECL_ARG_TYPE. */
325 else if (init == NULL_TREE)
326 assert (DECL_INITIAL (decl) == NULL_TREE);
328 assert (DECL_INITIAL (decl) == error_mark_node);
330 if (init != NULL_TREE)
332 if (TREE_CODE (decl) != TYPE_DECL)
333 DECL_INITIAL (decl) = init;
336 /* typedef foo = bar; store the type of bar as the type of foo. */
337 TREE_TYPE (decl) = TREE_TYPE (init);
338 DECL_INITIAL (decl) = init = 0;
342 if (TREE_CODE (decl) == VAR_DECL)
344 if (DECL_SIZE (decl) == NULL_TREE
345 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
346 layout_decl (decl, 0);
348 /* A static variable with an incomplete type is an error if it is
349 initialized. Also if it is not file scope. Otherwise, let it
350 through, but if it is not `extern' then it may cause an error
352 /* An automatic variable with an incomplete type is an error. */
353 if (DECL_SIZE (decl) == NULL_TREE
354 && (TREE_STATIC (decl) ? (DECL_INITIAL (decl) != 0
355 || DECL_CONTEXT (decl) != 0)
356 : !DECL_EXTERNAL (decl)))
358 gfc_fatal_error ("storage size not known");
361 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
362 && (DECL_SIZE (decl) != 0)
363 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST))
365 gfc_fatal_error ("storage size not constant");
372 /* Apply symbol attributes to a variable, and add it to the function scope. */
375 gfc_finish_var_decl (tree decl, gfc_symbol * sym)
377 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
378 This is the equivalent of the TARGET variables.
379 We also need to set this if the variable is passed by reference in a
381 if (sym->attr.target)
382 TREE_ADDRESSABLE (decl) = 1;
383 /* If it wasn't used we wouldn't be getting it. */
384 TREE_USED (decl) = 1;
386 /* Chain this decl to the pending declarations. Don't do pushdecl()
387 because this would add them to the current scope rather than the
389 if (current_function_decl != NULL_TREE)
391 if (sym->ns->proc_name->backend_decl == current_function_decl)
392 gfc_add_decl_to_function (decl);
394 gfc_add_decl_to_parent_function (decl);
397 /* If a variable is USE associated, it's always external. */
398 if (sym->attr.use_assoc)
400 DECL_EXTERNAL (decl) = 1;
401 TREE_PUBLIC (decl) = 1;
403 else if (sym->module[0] && !sym->attr.result)
405 /* TODO: Don't set sym->module for result variables. */
406 assert (current_function_decl == NULL_TREE);
407 /* This is the declaration of a module variable. */
408 TREE_PUBLIC (decl) = 1;
409 TREE_STATIC (decl) = 1;
412 if ((sym->attr.save || sym->attr.data || sym->value)
413 && !sym->attr.use_assoc)
414 TREE_STATIC (decl) = 1;
416 /* Keep variables larger than max-stack-var-size off stack. */
417 if (!sym->ns->proc_name->attr.recursive
418 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
419 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)))
420 TREE_STATIC (decl) = 1;
424 /* Allocate the lang-specific part of a decl. */
427 gfc_allocate_lang_decl (tree decl)
429 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
430 ggc_alloc_cleared (sizeof (struct lang_decl));
433 /* Remember a symbol to generate initialization/cleanup code at function
437 gfc_defer_symbol_init (gfc_symbol * sym)
443 /* Don't add a symbol twice. */
447 last = head = sym->ns->proc_name;
450 /* Make sure that setup code for dummy variables which are used in the
451 setup of other variables is generated first. */
454 /* Find the first dummy arg seen after us, or the first non-dummy arg.
455 This is a circular list, so don't go past the head. */
457 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
463 /* Insert in between last and p. */
469 /* Create an array index type variable with function scope. */
472 create_index_var (const char * pfx, int nest)
476 decl = gfc_create_var_np (gfc_array_index_type, pfx);
478 gfc_add_decl_to_parent_function (decl);
480 gfc_add_decl_to_function (decl);
485 /* Create variables to hold all the non-constant bits of info for a
486 descriptorless array. Remember these in the lang-specific part of the
490 gfc_build_qualified_array (tree decl, gfc_symbol * sym)
496 type = TREE_TYPE (decl);
498 /* We just use the descriptor, if there is one. */
499 if (GFC_DESCRIPTOR_TYPE_P (type))
502 assert (GFC_ARRAY_TYPE_P (type));
503 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
504 && !sym->attr.contained;
506 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
508 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
509 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
510 /* Don't try to use the unkown bound for assumed shape arrays. */
511 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
512 && (sym->as->type != AS_ASSUMED_SIZE
513 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
514 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
516 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
517 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
519 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
521 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
524 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
526 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
531 /* For some dummy arguments we don't use the actual argument directly.
532 Instead we create a local decl and use that. This allows us to preform
533 initialization, and construct full type information. */
536 gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
546 if (sym->attr.pointer || sym->attr.allocatable)
549 /* Add to list of variables if not a fake result variable. */
550 if (sym->attr.result || sym->attr.dummy)
551 gfc_defer_symbol_init (sym);
553 type = TREE_TYPE (dummy);
554 assert (TREE_CODE (dummy) == PARM_DECL
555 && POINTER_TYPE_P (type));
557 /* Do we know the element size? */
558 known_size = sym->ts.type != BT_CHARACTER
559 || INTEGER_CST_P (sym->ts.cl->backend_decl);
561 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
563 /* For descriptorless arrays with known element size the actual
564 argument is sufficient. */
565 assert (GFC_ARRAY_TYPE_P (type));
566 gfc_build_qualified_array (dummy, sym);
570 type = TREE_TYPE (type);
571 if (GFC_DESCRIPTOR_TYPE_P (type))
573 /* Create a decriptorless array pointer. */
576 if (!gfc_option.flag_repack_arrays)
578 if (as->type == AS_ASSUMED_SIZE)
583 if (as->type == AS_EXPLICIT)
586 for (n = 0; n < as->rank; n++)
590 && as->upper[n]->expr_type == EXPR_CONSTANT
591 && as->lower[n]->expr_type == EXPR_CONSTANT))
599 type = gfc_typenode_for_spec (&sym->ts);
600 type = gfc_get_nodesc_array_type (type, sym->as, packed);
604 /* We now have an expression for the element size, so create a fully
605 qualified type. Reset sym->backend decl or this will just return the
607 sym->backend_decl = NULL_TREE;
608 type = gfc_sym_type (sym);
612 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
613 decl = build_decl (VAR_DECL, get_identifier (name), type);
615 DECL_ARTIFICIAL (decl) = 1;
616 TREE_PUBLIC (decl) = 0;
617 TREE_STATIC (decl) = 0;
618 DECL_EXTERNAL (decl) = 0;
620 /* We should never get deferred shape arrays here. We used to because of
622 assert (sym->as->type != AS_DEFERRED);
627 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
631 GFC_DECL_PACKED_ARRAY (decl) = 1;
635 gfc_build_qualified_array (decl, sym);
637 if (DECL_LANG_SPECIFIC (dummy))
638 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
640 gfc_allocate_lang_decl (decl);
642 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
644 if (sym->ns->proc_name->backend_decl == current_function_decl
645 || sym->attr.contained)
646 gfc_add_decl_to_function (decl);
648 gfc_add_decl_to_parent_function (decl);
654 /* Return a constant or a variable to use as a string length. Does not
655 add the decl to the current scope. */
658 gfc_create_string_length (gfc_symbol * sym)
663 gfc_conv_const_charlen (sym->ts.cl);
665 if (sym->ts.cl->backend_decl == NULL_TREE)
667 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
669 /* Also prefix the mangled name. */
670 strcpy (&name[1], sym->name);
672 length = build_decl (VAR_DECL, get_identifier (name),
673 gfc_strlen_type_node);
674 DECL_ARTIFICIAL (length) = 1;
675 TREE_USED (length) = 1;
676 gfc_defer_symbol_init (sym);
677 sym->ts.cl->backend_decl = length;
680 return sym->ts.cl->backend_decl;
684 /* Return the decl for a gfc_symbol, create it if it doesn't already
688 gfc_get_symbol_decl (gfc_symbol * sym)
691 tree length = NULL_TREE;
694 assert (sym->attr.referenced);
696 if (sym->ns && sym->ns->proc_name->attr.function)
697 byref = gfc_return_by_reference (sym->ns->proc_name);
701 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
703 /* Return via extra parameter. */
704 if (sym->attr.result && byref
705 && !sym->backend_decl)
708 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
711 /* Dummy variables should already have been created. */
712 assert (sym->backend_decl);
714 /* Create a character length variable. */
715 if (sym->ts.type == BT_CHARACTER)
717 if (sym->ts.cl->backend_decl == NULL_TREE)
719 length = gfc_create_string_length (sym);
720 if (TREE_CODE (length) != INTEGER_CST)
722 gfc_finish_var_decl (length, sym);
723 gfc_defer_symbol_init (sym);
728 /* Use a copy of the descriptor for dummy arrays. */
729 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
732 gfc_build_dummy_array_decl (sym, sym->backend_decl);
735 TREE_USED (sym->backend_decl) = 1;
736 return sym->backend_decl;
739 if (sym->backend_decl)
740 return sym->backend_decl;
742 /* Catch function declarations. Only used for actual parameters. */
743 if (sym->attr.flavor == FL_PROCEDURE)
745 decl = gfc_get_extern_function_decl (sym);
749 if (sym->attr.intrinsic)
750 internal_error ("intrinsic variable which isn't a procedure");
752 /* Create string length decl first so that they can be used in the
754 if (sym->ts.type == BT_CHARACTER)
755 length = gfc_create_string_length (sym);
757 /* Create the decl for the variable. */
758 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
760 /* Symbols from modules should have their assembler names mangled.
761 This is done here rather than in gfc_finish_var_decl because it
762 is different for string length variables. */
764 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
766 if (sym->attr.dimension)
768 /* Create variables to hold the non-constant bits of array info. */
769 gfc_build_qualified_array (decl, sym);
771 /* Remember this variable for allocation/cleanup. */
772 gfc_defer_symbol_init (sym);
774 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
775 GFC_DECL_PACKED_ARRAY (decl) = 1;
778 gfc_finish_var_decl (decl, sym);
780 if (sym->attr.assign)
782 gfc_allocate_lang_decl (decl);
783 GFC_DECL_ASSIGN (decl) = 1;
784 length = gfc_create_var (gfc_strlen_type_node, sym->name);
785 GFC_DECL_STRING_LEN (decl) = length;
786 GFC_DECL_ASSIGN_ADDR (decl) = gfc_create_var (pvoid_type_node, sym->name);
787 /* TODO: Need to check we don't change TREE_STATIC (decl) later. */
788 TREE_STATIC (length) = TREE_STATIC (decl);
789 /* STRING_LENGTH is also used as flag. Less than -1 means that
790 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
791 target label's address. Other value is the length of format string
792 and ASSIGN_ADDR is the address of format string. */
793 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
796 if (sym->ts.type == BT_CHARACTER)
798 /* Character variables need special handling. */
799 gfc_allocate_lang_decl (decl);
801 if (TREE_CODE (length) != INTEGER_CST)
803 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
807 /* Also prefix the mangled name for symbols from modules. */
808 strcpy (&name[1], sym->name);
811 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
812 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
814 gfc_finish_var_decl (length, sym);
815 assert (!sym->value);
818 sym->backend_decl = decl;
820 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
822 /* Add static initializer. */
823 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
824 TREE_TYPE (decl), sym->attr.dimension,
825 sym->attr.pointer || sym->attr.allocatable);
832 /* Substitute a temporary variable in place of the real one. */
835 gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
837 save->attr = sym->attr;
838 save->decl = sym->backend_decl;
840 gfc_clear_attr (&sym->attr);
841 sym->attr.referenced = 1;
842 sym->attr.flavor = FL_VARIABLE;
844 sym->backend_decl = decl;
848 /* Restore the original variable. */
851 gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
853 sym->attr = save->attr;
854 sym->backend_decl = save->decl;
858 /* Get a basic decl for an external function. */
861 gfc_get_extern_function_decl (gfc_symbol * sym)
866 gfc_intrinsic_sym *isym;
868 char s[GFC_MAX_SYMBOL_LEN];
872 if (sym->backend_decl)
873 return sym->backend_decl;
875 /* We should never be creating external decls for alternate entry points.
876 The procedure may be an alternate entry point, but we don't want/need
878 assert (!(sym->attr.entry || sym->attr.entry_master));
880 if (sym->attr.intrinsic)
882 /* Call the resolution function to get the actual name. This is
883 a nasty hack which relies on the resolution functions only looking
884 at the first argument. We pass NULL for the second argument
885 otherwise things like AINT get confused. */
886 isym = gfc_find_function (sym->name);
887 assert (isym->resolve.f0 != NULL);
889 memset (&e, 0, sizeof (e));
890 e.expr_type = EXPR_FUNCTION;
892 memset (&argexpr, 0, sizeof (argexpr));
893 assert (isym->formal);
894 argexpr.ts = isym->formal->ts;
896 if (isym->formal->next == NULL)
897 isym->resolve.f1 (&e, &argexpr);
900 /* All specific intrinsics take one or two arguments. */
901 assert (isym->formal->next->next == NULL);
902 isym->resolve.f2 (&e, &argexpr, NULL);
904 sprintf (s, "specific%s", e.value.function.name);
905 name = get_identifier (s);
910 name = gfc_sym_identifier (sym);
911 mangled_name = gfc_sym_mangled_function_id (sym);
914 type = gfc_get_function_type (sym);
915 fndecl = build_decl (FUNCTION_DECL, name, type);
917 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
918 /* If the return type is a pointer, avoid alias issues by setting
919 DECL_IS_MALLOC to nonzero. This means that the function should be
920 treated as if it were a malloc, meaning it returns a pointer that
922 if (POINTER_TYPE_P (type))
923 DECL_IS_MALLOC (fndecl) = 1;
925 /* Set the context of this decl. */
926 if (0 && sym->ns && sym->ns->proc_name)
928 /* TODO: Add external decls to the appropriate scope. */
929 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
933 /* Global declaration, e.g. intrinsic subroutine. */
934 DECL_CONTEXT (fndecl) = NULL_TREE;
937 DECL_EXTERNAL (fndecl) = 1;
939 /* This specifies if a function is globally addressable, i.e. it is
940 the opposite of declaring static in C. */
941 TREE_PUBLIC (fndecl) = 1;
943 /* Set attributes for PURE functions. A call to PURE function in the
944 Fortran 95 sense is both pure and without side effects in the C
946 if (sym->attr.pure || sym->attr.elemental)
948 if (sym->attr.function)
949 DECL_IS_PURE (fndecl) = 1;
950 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
951 parameters and don't use alternate returns (is this
952 allowed?). In that case, calls to them are meaningless, and
953 can be optimized away. See also in build_function_decl(). */
954 TREE_SIDE_EFFECTS (fndecl) = 0;
957 sym->backend_decl = fndecl;
959 if (DECL_CONTEXT (fndecl) == NULL_TREE)
960 pushdecl_top_level (fndecl);
966 /* Create a declaration for a procedure. For external functions (in the C
967 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
968 a master function with alternate entry points. */
971 build_function_decl (gfc_symbol * sym)
974 symbol_attribute attr;
976 gfc_formal_arglist *f;
978 assert (!sym->backend_decl);
979 assert (!sym->attr.external);
981 /* Allow only one nesting level. Allow public declarations. */
982 assert (current_function_decl == NULL_TREE
983 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
985 type = gfc_get_function_type (sym);
986 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
988 /* Perform name mangling if this is a top level or module procedure. */
989 if (current_function_decl == NULL_TREE)
990 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
992 /* Figure out the return type of the declared function, and build a
993 RESULT_DECL for it. If this is a subroutine with alternate
994 returns, build a RESULT_DECL for it. */
997 result_decl = NULL_TREE;
998 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1001 if (gfc_return_by_reference (sym))
1002 type = void_type_node;
1005 if (sym->result != sym)
1006 result_decl = gfc_sym_identifier (sym->result);
1008 type = TREE_TYPE (TREE_TYPE (fndecl));
1013 /* Look for alternate return placeholders. */
1014 int has_alternate_returns = 0;
1015 for (f = sym->formal; f; f = f->next)
1019 has_alternate_returns = 1;
1024 if (has_alternate_returns)
1025 type = integer_type_node;
1027 type = void_type_node;
1030 result_decl = build_decl (RESULT_DECL, result_decl, type);
1031 DECL_ARTIFICIAL (result_decl) = 1;
1032 DECL_IGNORED_P (result_decl) = 1;
1033 DECL_CONTEXT (result_decl) = fndecl;
1034 DECL_RESULT (fndecl) = result_decl;
1036 /* Don't call layout_decl for a RESULT_DECL.
1037 layout_decl (result_decl, 0); */
1039 /* If the return type is a pointer, avoid alias issues by setting
1040 DECL_IS_MALLOC to nonzero. This means that the function should be
1041 treated as if it were a malloc, meaning it returns a pointer that
1043 if (POINTER_TYPE_P (type))
1044 DECL_IS_MALLOC (fndecl) = 1;
1046 /* Set up all attributes for the function. */
1047 DECL_CONTEXT (fndecl) = current_function_decl;
1048 DECL_EXTERNAL (fndecl) = 0;
1050 /* This specifies if a function is globally visible, i.e. it is
1051 the opposite of declaring static in C. */
1052 if (DECL_CONTEXT (fndecl) == NULL_TREE
1053 && !sym->attr.entry_master)
1054 TREE_PUBLIC (fndecl) = 1;
1056 /* TREE_STATIC means the function body is defined here. */
1057 TREE_STATIC (fndecl) = 1;
1059 /* Set attributes for PURE functions. A call to a PURE function in the
1060 Fortran 95 sense is both pure and without side effects in the C
1062 if (attr.pure || attr.elemental)
1064 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1065 including a alternate return. In that case it can also be
1066 marked as PURE. See also in gfc_get_extern_fucntion_decl(). */
1068 DECL_IS_PURE (fndecl) = 1;
1069 TREE_SIDE_EFFECTS (fndecl) = 0;
1072 /* Layout the function declaration and put it in the binding level
1073 of the current function. */
1076 sym->backend_decl = fndecl;
1080 /* Create the DECL_ARGUMENTS for a procedure. */
1083 create_function_arglist (gfc_symbol * sym)
1086 gfc_formal_arglist *f;
1093 fndecl = sym->backend_decl;
1095 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1096 the new FUNCTION_DECL node. */
1097 arglist = NULL_TREE;
1098 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1100 if (sym->attr.entry_master)
1102 type = TREE_VALUE (typelist);
1103 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1105 DECL_CONTEXT (parm) = fndecl;
1106 DECL_ARG_TYPE (parm) = type;
1107 TREE_READONLY (parm) = 1;
1108 gfc_finish_decl (parm, NULL_TREE);
1110 arglist = chainon (arglist, parm);
1111 typelist = TREE_CHAIN (typelist);
1114 if (gfc_return_by_reference (sym))
1116 type = TREE_VALUE (typelist);
1117 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1119 DECL_CONTEXT (parm) = fndecl;
1120 DECL_ARG_TYPE (parm) = type;
1121 TREE_READONLY (parm) = 1;
1122 gfc_finish_decl (parm, NULL_TREE);
1124 arglist = chainon (arglist, parm);
1125 typelist = TREE_CHAIN (typelist);
1127 if (sym->ts.type == BT_CHARACTER)
1129 gfc_allocate_lang_decl (parm);
1131 /* Length of character result. */
1132 type = TREE_VALUE (typelist);
1133 assert (type == gfc_strlen_type_node);
1135 length = build_decl (PARM_DECL,
1136 get_identifier (".__result"),
1138 if (!sym->ts.cl->length)
1140 sym->ts.cl->backend_decl = length;
1141 TREE_USED (length) = 1;
1143 assert (TREE_CODE (length) == PARM_DECL);
1144 arglist = chainon (arglist, length);
1145 typelist = TREE_CHAIN (typelist);
1146 DECL_CONTEXT (length) = fndecl;
1147 DECL_ARG_TYPE (length) = type;
1148 TREE_READONLY (length) = 1;
1149 gfc_finish_decl (length, NULL_TREE);
1153 for (f = sym->formal; f; f = f->next)
1155 if (f->sym != NULL) /* ignore alternate returns. */
1159 type = TREE_VALUE (typelist);
1161 /* Build a the argument declaration. */
1162 parm = build_decl (PARM_DECL,
1163 gfc_sym_identifier (f->sym), type);
1165 /* Fill in arg stuff. */
1166 DECL_CONTEXT (parm) = fndecl;
1167 DECL_ARG_TYPE (parm) = type;
1168 DECL_ARG_TYPE_AS_WRITTEN (parm) = type;
1169 /* All implementation args are read-only. */
1170 TREE_READONLY (parm) = 1;
1172 gfc_finish_decl (parm, NULL_TREE);
1174 f->sym->backend_decl = parm;
1176 arglist = chainon (arglist, parm);
1177 typelist = TREE_CHAIN (typelist);
1181 /* Add the hidden string length parameters. */
1183 for (f = sym->formal; f; f = f->next)
1185 char name[GFC_MAX_SYMBOL_LEN + 2];
1186 /* Ignore alternate returns. */
1190 if (f->sym->ts.type != BT_CHARACTER)
1193 parm = f->sym->backend_decl;
1194 type = TREE_VALUE (typelist);
1195 assert (type == gfc_strlen_type_node);
1197 strcpy (&name[1], f->sym->name);
1199 length = build_decl (PARM_DECL, get_identifier (name), type);
1201 arglist = chainon (arglist, length);
1202 DECL_CONTEXT (length) = fndecl;
1203 DECL_ARG_TYPE (length) = type;
1204 TREE_READONLY (length) = 1;
1205 gfc_finish_decl (length, NULL_TREE);
1207 /* TODO: Check string lengths when -fbounds-check. */
1209 /* Use the passed value for assumed length variables. */
1210 if (!f->sym->ts.cl->length)
1212 TREE_USED (length) = 1;
1213 if (!f->sym->ts.cl->backend_decl)
1214 f->sym->ts.cl->backend_decl = length;
1217 /* there is already another variable using this
1218 gfc_charlen node, build a new one for this variable
1219 and chain it into the list of gfc_charlens.
1220 This happens for e.g. in the case
1222 since CHARACTER declarations on the same line share
1223 the same gfc_charlen node. */
1226 cl = gfc_get_charlen ();
1227 cl->backend_decl = length;
1228 cl->next = f->sym->ts.cl->next;
1229 f->sym->ts.cl->next = cl;
1234 parm = TREE_CHAIN (parm);
1235 typelist = TREE_CHAIN (typelist);
1238 assert (TREE_VALUE (typelist) == void_type_node);
1239 DECL_ARGUMENTS (fndecl) = arglist;
1243 /* Finalize DECL and all nested functions with cgraph. */
1246 gfc_finalize (tree decl)
1248 struct cgraph_node *cgn;
1250 cgn = cgraph_node (decl);
1251 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
1252 gfc_finalize (cgn->decl);
1254 cgraph_finalize_function (decl, false);
1258 /* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1261 gfc_gimplify_function (tree fndecl)
1263 struct cgraph_node *cgn;
1265 gimplify_function_tree (fndecl);
1266 dump_function (TDI_generic, fndecl);
1268 /* Convert all nested functions to GIMPLE now. We do things in this order
1269 so that items like VLA sizes are expanded properly in the context of the
1270 correct function. */
1271 cgn = cgraph_node (fndecl);
1272 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1273 gfc_gimplify_function (cgn->decl);
1277 /* Do the setup necessary before generating the body of a function. */
1280 trans_function_start (gfc_symbol * sym)
1284 fndecl = sym->backend_decl;
1286 /* Let GCC know the current scope is this function. */
1287 current_function_decl = fndecl;
1289 /* Let the world know what we're about to do. */
1290 announce_function (fndecl);
1292 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1294 /* Create RTL for function declaration. */
1295 rest_of_decl_compilation (fndecl, 1, 0);
1298 /* Create RTL for function definition. */
1299 make_decl_rtl (fndecl);
1301 /* Set the line and filename. sym->declared_at seems to point to the
1302 last statement for subroutines, but it'll do for now. */
1303 gfc_set_backend_locus (&sym->declared_at);
1305 init_function_start (fndecl);
1307 /* Even though we're inside a function body, we still don't want to
1308 call expand_expr to calculate the size of a variable-sized array.
1309 We haven't necessarily assigned RTL to all variables yet, so it's
1310 not safe to try to expand expressions involving them. */
1311 cfun->x_dont_save_pending_sizes_p = 1;
1313 /* function.c requires a push at the start of the function. */
1317 /* Create thunks for alternate entry points. */
1320 build_entry_thunks (gfc_namespace * ns)
1322 gfc_formal_arglist *formal;
1323 gfc_formal_arglist *thunk_formal;
1325 gfc_symbol *thunk_sym;
1332 /* This should always be a toplevel function. */
1333 assert (current_function_decl == NULL_TREE);
1335 for (el = ns->entries; el; el = el->next)
1337 thunk_sym = el->sym;
1339 build_function_decl (thunk_sym);
1340 create_function_arglist (thunk_sym);
1342 trans_function_start (thunk_sym);
1344 thunk_fndecl = thunk_sym->backend_decl;
1346 gfc_start_block (&body);
1348 /* Pass extra parameter identifying this entry point. */
1349 tmp = build_int_cst (gfc_array_index_type, el->id);
1350 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1351 string_args = NULL_TREE;
1353 /* TODO: Pass return by reference parameters. */
1354 if (ns->proc_name->attr.function)
1355 gfc_todo_error ("Functons with multiple entry points");
1357 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1359 /* We don't have a clever way of identifying arguments, so resort to
1360 a brute-force search. */
1361 for (thunk_formal = thunk_sym->formal;
1363 thunk_formal = thunk_formal->next)
1365 if (thunk_formal->sym == formal->sym)
1371 /* Pass the argument. */
1372 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1374 if (formal->sym->ts.type == BT_CHARACTER)
1376 tmp = thunk_formal->sym->ts.cl->backend_decl;
1377 string_args = tree_cons (NULL_TREE, tmp, string_args);
1382 /* Pass NULL for a missing argument. */
1383 args = tree_cons (NULL_TREE, null_pointer_node, args);
1384 if (formal->sym->ts.type == BT_CHARACTER)
1386 tmp = convert (gfc_strlen_type_node, integer_zero_node);
1387 string_args = tree_cons (NULL_TREE, tmp, string_args);
1392 /* Call the master function. */
1393 args = nreverse (args);
1394 args = chainon (args, nreverse (string_args));
1395 tmp = ns->proc_name->backend_decl;
1396 tmp = gfc_build_function_call (tmp, args);
1397 /* TODO: function return value. */
1398 gfc_add_expr_to_block (&body, tmp);
1400 /* Finish off this function and send it for code generation. */
1401 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1403 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1405 /* Output the GENERIC tree. */
1406 dump_function (TDI_original, thunk_fndecl);
1408 /* Store the end of the function, so that we get good line number
1409 info for the epilogue. */
1410 cfun->function_end_locus = input_location;
1412 /* We're leaving the context of this function, so zap cfun.
1413 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1414 tree_rest_of_compilation. */
1417 current_function_decl = NULL_TREE;
1419 gfc_gimplify_function (thunk_fndecl);
1420 lower_nested_functions (thunk_fndecl);
1421 gfc_finalize (thunk_fndecl);
1423 /* We share the symbols in the formal argument list with other entry
1424 points and the master function. Clear them so that they are
1425 recreated for each function. */
1426 for (formal = thunk_sym->formal; formal; formal = formal->next)
1428 formal->sym->backend_decl = NULL_TREE;
1429 if (formal->sym->ts.type == BT_CHARACTER)
1430 formal->sym->ts.cl->backend_decl = NULL_TREE;
1436 /* Create a decl for a function, and create any thunks for alternate entry
1440 gfc_create_function_decl (gfc_namespace * ns)
1442 /* Create a declaration for the master function. */
1443 build_function_decl (ns->proc_name);
1445 /* Compile the entry thunks. */
1447 build_entry_thunks (ns);
1449 /* Now create the read argument list. */
1450 create_function_arglist (ns->proc_name);
1453 /* Return the decl used to hold the function return value. */
1456 gfc_get_fake_result_decl (gfc_symbol * sym)
1461 char name[GFC_MAX_SYMBOL_LEN + 10];
1463 if (current_fake_result_decl != NULL_TREE)
1464 return current_fake_result_decl;
1466 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1471 if (sym->ts.type == BT_CHARACTER
1472 && !sym->ts.cl->backend_decl)
1474 length = gfc_create_string_length (sym);
1475 gfc_finish_var_decl (length, sym);
1478 if (gfc_return_by_reference (sym))
1480 decl = DECL_ARGUMENTS (sym->backend_decl);
1482 TREE_USED (decl) = 1;
1484 decl = gfc_build_dummy_array_decl (sym, decl);
1488 sprintf (name, "__result_%.20s",
1489 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1491 decl = build_decl (VAR_DECL, get_identifier (name),
1492 TREE_TYPE (TREE_TYPE (current_function_decl)));
1494 DECL_ARTIFICIAL (decl) = 1;
1495 DECL_EXTERNAL (decl) = 0;
1496 TREE_PUBLIC (decl) = 0;
1497 TREE_USED (decl) = 1;
1499 layout_decl (decl, 0);
1501 gfc_add_decl_to_function (decl);
1504 current_fake_result_decl = decl;
1510 /* Builds a function decl. The remaining parameters are the types of the
1511 function arguments. Negative nargs indicates a varargs function. */
1514 gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1523 /* Library functions must be declared with global scope. */
1524 assert (current_function_decl == NULL_TREE);
1526 va_start (p, nargs);
1529 /* Create a list of the argument types. */
1530 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1532 argtype = va_arg (p, tree);
1533 arglist = gfc_chainon_list (arglist, argtype);
1538 /* Terminate the list. */
1539 arglist = gfc_chainon_list (arglist, void_type_node);
1542 /* Build the function type and decl. */
1543 fntype = build_function_type (rettype, arglist);
1544 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1546 /* Mark this decl as external. */
1547 DECL_EXTERNAL (fndecl) = 1;
1548 TREE_PUBLIC (fndecl) = 1;
1554 rest_of_decl_compilation (fndecl, 1, 0);
1560 gfc_build_intrinsic_function_decls (void)
1562 /* String functions. */
1563 gfor_fndecl_copy_string =
1564 gfc_build_library_function_decl (get_identifier (PREFIX("copy_string")),
1567 gfc_strlen_type_node, pchar_type_node,
1568 gfc_strlen_type_node, pchar_type_node);
1570 gfor_fndecl_compare_string =
1571 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
1574 gfc_strlen_type_node, pchar_type_node,
1575 gfc_strlen_type_node, pchar_type_node);
1577 gfor_fndecl_concat_string =
1578 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
1581 gfc_strlen_type_node, pchar_type_node,
1582 gfc_strlen_type_node, pchar_type_node,
1583 gfc_strlen_type_node, pchar_type_node);
1585 gfor_fndecl_string_len_trim =
1586 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
1588 2, gfc_strlen_type_node,
1591 gfor_fndecl_string_index =
1592 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
1594 5, gfc_strlen_type_node, pchar_type_node,
1595 gfc_strlen_type_node, pchar_type_node,
1596 gfc_logical4_type_node);
1598 gfor_fndecl_string_scan =
1599 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
1601 5, gfc_strlen_type_node, pchar_type_node,
1602 gfc_strlen_type_node, pchar_type_node,
1603 gfc_logical4_type_node);
1605 gfor_fndecl_string_verify =
1606 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
1608 5, gfc_strlen_type_node, pchar_type_node,
1609 gfc_strlen_type_node, pchar_type_node,
1610 gfc_logical4_type_node);
1612 gfor_fndecl_string_trim =
1613 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
1616 build_pointer_type (gfc_strlen_type_node),
1618 gfc_strlen_type_node,
1621 gfor_fndecl_string_repeat =
1622 gfc_build_library_function_decl (get_identifier (PREFIX("string_repeat")),
1626 gfc_strlen_type_node,
1628 gfc_int4_type_node);
1630 gfor_fndecl_adjustl =
1631 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
1635 gfc_strlen_type_node, pchar_type_node);
1637 gfor_fndecl_adjustr =
1638 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
1642 gfc_strlen_type_node, pchar_type_node);
1644 gfor_fndecl_si_kind =
1645 gfc_build_library_function_decl (get_identifier ("selected_int_kind"),
1650 gfor_fndecl_sr_kind =
1651 gfc_build_library_function_decl (get_identifier ("selected_real_kind"),
1656 /* Power functions. */
1662 static int kinds[2] = {4, 8};
1663 char name[PREFIX_LEN + 10]; /* _gfortran_pow_?n_?n */
1665 for (ikind=0; ikind < 2; ikind++)
1667 itype = gfc_get_int_type (kinds[ikind]);
1668 for (kind = 0; kind < 2; kind ++)
1670 type = gfc_get_int_type (kinds[kind]);
1671 sprintf(name, PREFIX("pow_i%d_i%d"), kinds[kind], kinds[ikind]);
1672 gfor_fndecl_math_powi[kind][ikind].integer =
1673 gfc_build_library_function_decl (get_identifier (name),
1674 type, 2, type, itype);
1676 type = gfc_get_real_type (kinds[kind]);
1677 sprintf(name, PREFIX("pow_r%d_i%d"), kinds[kind], kinds[ikind]);
1678 gfor_fndecl_math_powi[kind][ikind].real =
1679 gfc_build_library_function_decl (get_identifier (name),
1680 type, 2, type, itype);
1682 type = gfc_get_complex_type (kinds[kind]);
1683 sprintf(name, PREFIX("pow_c%d_i%d"), kinds[kind], kinds[ikind]);
1684 gfor_fndecl_math_powi[kind][ikind].cmplx =
1685 gfc_build_library_function_decl (get_identifier (name),
1686 type, 2, type, itype);
1691 gfor_fndecl_math_cpowf =
1692 gfc_build_library_function_decl (get_identifier ("cpowf"),
1693 gfc_complex4_type_node,
1694 1, gfc_complex4_type_node);
1695 gfor_fndecl_math_cpow =
1696 gfc_build_library_function_decl (get_identifier ("cpow"),
1697 gfc_complex8_type_node,
1698 1, gfc_complex8_type_node);
1699 gfor_fndecl_math_ishftc4 =
1700 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
1702 3, gfc_int4_type_node,
1703 gfc_int4_type_node, gfc_int4_type_node);
1704 gfor_fndecl_math_ishftc8 =
1705 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
1707 3, gfc_int8_type_node,
1708 gfc_int8_type_node, gfc_int8_type_node);
1709 gfor_fndecl_math_exponent4 =
1710 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
1712 1, gfc_real4_type_node);
1713 gfor_fndecl_math_exponent8 =
1714 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
1716 1, gfc_real8_type_node);
1718 /* Other functions. */
1720 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
1721 gfc_array_index_type,
1722 1, pvoid_type_node);
1724 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
1725 gfc_array_index_type,
1727 gfc_array_index_type);
1730 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
1736 /* Make prototypes for runtime library functions. */
1739 gfc_build_builtin_function_decls (void)
1741 gfor_fndecl_internal_malloc =
1742 gfc_build_library_function_decl (get_identifier (PREFIX("internal_malloc")),
1743 pvoid_type_node, 1, gfc_int4_type_node);
1745 gfor_fndecl_internal_malloc64 =
1746 gfc_build_library_function_decl (get_identifier
1747 (PREFIX("internal_malloc64")),
1748 pvoid_type_node, 1, gfc_int8_type_node);
1750 gfor_fndecl_internal_free =
1751 gfc_build_library_function_decl (get_identifier (PREFIX("internal_free")),
1752 void_type_node, 1, pvoid_type_node);
1754 gfor_fndecl_allocate =
1755 gfc_build_library_function_decl (get_identifier (PREFIX("allocate")),
1756 void_type_node, 2, ppvoid_type_node,
1757 gfc_int4_type_node);
1759 gfor_fndecl_allocate64 =
1760 gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")),
1761 void_type_node, 2, ppvoid_type_node,
1762 gfc_int8_type_node);
1764 gfor_fndecl_deallocate =
1765 gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")),
1766 void_type_node, 1, ppvoid_type_node);
1768 gfor_fndecl_stop_numeric =
1769 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
1770 void_type_node, 1, gfc_int4_type_node);
1772 gfor_fndecl_stop_string =
1773 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
1774 void_type_node, 2, pchar_type_node,
1775 gfc_int4_type_node);
1777 gfor_fndecl_pause_numeric =
1778 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
1779 void_type_node, 1, gfc_int4_type_node);
1781 gfor_fndecl_pause_string =
1782 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
1783 void_type_node, 2, pchar_type_node,
1784 gfc_int4_type_node);
1786 gfor_fndecl_select_string =
1787 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
1788 pvoid_type_node, 0);
1790 gfor_fndecl_runtime_error =
1791 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
1794 pchar_type_node, pchar_type_node,
1795 gfc_int4_type_node);
1797 gfor_fndecl_in_pack = gfc_build_library_function_decl (
1798 get_identifier (PREFIX("internal_pack")),
1799 pvoid_type_node, 1, pvoid_type_node);
1801 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
1802 get_identifier (PREFIX("internal_unpack")),
1803 pvoid_type_node, 1, pvoid_type_node);
1805 gfor_fndecl_associated =
1806 gfc_build_library_function_decl (
1807 get_identifier (PREFIX("associated")),
1808 gfc_logical4_type_node,
1813 gfc_build_intrinsic_function_decls ();
1814 gfc_build_intrinsic_lib_fndecls ();
1815 gfc_build_io_library_fndecls ();
1819 /* Exaluate the length of dummy character variables. */
1822 gfc_trans_dummy_character (gfc_charlen * cl, tree fnbody)
1826 gfc_finish_decl (cl->backend_decl, NULL_TREE);
1828 gfc_start_block (&body);
1830 /* Evaluate the string length expression. */
1831 gfc_trans_init_string_length (cl, &body);
1833 gfc_add_expr_to_block (&body, fnbody);
1834 return gfc_finish_block (&body);
1838 /* Allocate and cleanup an automatic character variable. */
1841 gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
1847 assert (sym->backend_decl);
1848 assert (sym->ts.cl && sym->ts.cl->length);
1850 gfc_start_block (&body);
1852 /* Evaluate the string length expression. */
1853 gfc_trans_init_string_length (sym->ts.cl, &body);
1855 decl = sym->backend_decl;
1857 /* Emit a DECL_EXPR for this variable, which will cause the
1858 gimplifier to allocate storage, and all that good stuff. */
1859 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
1860 gfc_add_expr_to_block (&body, tmp);
1862 gfc_add_expr_to_block (&body, fnbody);
1863 return gfc_finish_block (&body);
1867 /* Generate function entry and exit code, and add it to the function body.
1869 Allocation and initialization of array variables.
1870 Allocation of character string variables.
1871 Initialization and possibly repacking of dummy arrays. */
1874 gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
1879 /* Deal with implicit return variables. Explicit return variables will
1880 already have been added. */
1881 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
1883 if (!current_fake_result_decl)
1885 warning ("Function does not return a value");
1891 fnbody = gfc_trans_dummy_array_bias (proc_sym,
1892 current_fake_result_decl,
1895 else if (proc_sym->ts.type == BT_CHARACTER)
1897 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
1898 fnbody = gfc_trans_dummy_character (proc_sym->ts.cl, fnbody);
1901 gfc_todo_error ("Deferred non-array return by reference");
1904 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
1906 if (sym->attr.dimension)
1908 switch (sym->as->type)
1911 if (sym->attr.dummy || sym->attr.result)
1913 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
1914 else if (sym->attr.pointer || sym->attr.allocatable)
1916 if (TREE_STATIC (sym->backend_decl))
1917 gfc_trans_static_array_pointer (sym);
1919 fnbody = gfc_trans_deferred_array (sym, fnbody);
1923 gfc_get_backend_locus (&loc);
1924 gfc_set_backend_locus (&sym->declared_at);
1925 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
1927 gfc_set_backend_locus (&loc);
1931 case AS_ASSUMED_SIZE:
1932 /* Must be a dummy parameter. */
1933 assert (sym->attr.dummy);
1935 /* We should always pass assumed size arrays the g77 way. */
1936 fnbody = gfc_trans_g77_array (sym, fnbody);
1939 case AS_ASSUMED_SHAPE:
1940 /* Must be a dummy parameter. */
1941 assert (sym->attr.dummy);
1943 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
1948 fnbody = gfc_trans_deferred_array (sym, fnbody);
1955 else if (sym->ts.type == BT_CHARACTER)
1957 gfc_get_backend_locus (&loc);
1958 gfc_set_backend_locus (&sym->declared_at);
1959 if (sym->attr.dummy || sym->attr.result)
1960 fnbody = gfc_trans_dummy_character (sym->ts.cl, fnbody);
1962 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
1963 gfc_set_backend_locus (&loc);
1973 /* Output an initialized decl for a module variable. */
1976 gfc_create_module_variable (gfc_symbol * sym)
1980 /* Only output symbols from this module. */
1981 if (sym->ns != module_namespace)
1983 /* I don't think this should ever happen. */
1984 internal_error ("module symbol %s in wrong namespace", sym->name);
1987 /* Only output variables and array valued parametes. */
1988 if (sym->attr.flavor != FL_VARIABLE
1989 && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0))
1992 /* Don't generate variables from other modules. Variables from
1993 COMMONs will already have been generated. */
1994 if (sym->attr.use_assoc || sym->attr.in_common)
1997 if (sym->backend_decl)
1998 internal_error ("backend decl for module variable %s already exists",
2001 /* We always want module variables to be created. */
2002 sym->attr.referenced = 1;
2003 /* Create the decl. */
2004 decl = gfc_get_symbol_decl (sym);
2006 /* Create the variable. */
2008 rest_of_decl_compilation (decl, 1, 0);
2010 /* Also add length of strings. */
2011 if (sym->ts.type == BT_CHARACTER)
2015 length = sym->ts.cl->backend_decl;
2016 if (!INTEGER_CST_P (length))
2019 rest_of_decl_compilation (length, 1, 0);
2025 /* Generate all the required code for module variables. */
2028 gfc_generate_module_vars (gfc_namespace * ns)
2030 module_namespace = ns;
2032 /* Check if the frontend left the namespace in a reasonable state. */
2033 assert (ns->proc_name && !ns->proc_name->tlink);
2035 /* Generate COMMON blocks. */
2036 gfc_trans_common (ns);
2038 /* Create decls for all the module variables. */
2039 gfc_traverse_ns (ns, gfc_create_module_variable);
2043 gfc_generate_contained_functions (gfc_namespace * parent)
2047 /* We create all the prototypes before generating any code. */
2048 for (ns = parent->contained; ns; ns = ns->sibling)
2050 /* Skip namespaces from used modules. */
2051 if (ns->parent != parent)
2054 gfc_create_function_decl (ns);
2057 for (ns = parent->contained; ns; ns = ns->sibling)
2059 /* Skip namespaces from used modules. */
2060 if (ns->parent != parent)
2063 gfc_generate_function_code (ns);
2068 /* Generate decls for all local variables. We do this to ensure correct
2069 handling of expressions which only appear in the specification of
2073 generate_local_decl (gfc_symbol * sym)
2075 if (sym->attr.flavor == FL_VARIABLE)
2077 if (sym->attr.referenced)
2078 gfc_get_symbol_decl (sym);
2079 else if (sym->attr.dummy)
2081 if (warn_unused_parameter)
2082 warning ("unused parameter `%s'", sym->name);
2084 /* Warn for unused variables, but not if they're inside a common
2085 block or are use_associated. */
2086 else if (warn_unused_variable
2087 && !(sym->attr.in_common || sym->attr.use_assoc))
2088 warning ("unused variable `%s'", sym->name);
2093 generate_local_vars (gfc_namespace * ns)
2095 gfc_traverse_ns (ns, generate_local_decl);
2099 /* Generate a switch statement to jump to the correct entry point. Also
2100 creates the label decls for the entry points. */
2103 gfc_trans_entry_master_switch (gfc_entry_list * el)
2110 gfc_init_block (&block);
2111 for (; el; el = el->next)
2113 /* Add the case label. */
2114 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2115 DECL_CONTEXT (label) = current_function_decl;
2116 val = build_int_cst (gfc_array_index_type, el->id);
2117 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
2118 gfc_add_expr_to_block (&block, tmp);
2120 /* And jump to the actual entry point. */
2121 label = gfc_build_label_decl (NULL_TREE);
2122 TREE_USED (label) = 1;
2123 DECL_CONTEXT (label) = current_function_decl;
2124 tmp = build1_v (GOTO_EXPR, label);
2125 gfc_add_expr_to_block (&block, tmp);
2127 /* Save the label decl. */
2130 tmp = gfc_finish_block (&block);
2131 /* The first argument selects the entry point. */
2132 val = DECL_ARGUMENTS (current_function_decl);
2133 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
2138 /* Generate code for a function. */
2141 gfc_generate_function_code (gfc_namespace * ns)
2152 sym = ns->proc_name;
2154 /* Check that the frontend isn't still using this. */
2155 assert (sym->tlink == NULL);
2158 /* Create the declaration for functions with global scope. */
2159 if (!sym->backend_decl)
2160 gfc_create_function_decl (ns);
2162 fndecl = sym->backend_decl;
2163 old_context = current_function_decl;
2167 push_function_context ();
2168 saved_parent_function_decls = saved_function_decls;
2169 saved_function_decls = NULL_TREE;
2172 trans_function_start (sym);
2174 /* Will be created as needed. */
2175 current_fake_result_decl = NULL_TREE;
2177 gfc_start_block (&block);
2179 gfc_generate_contained_functions (ns);
2181 /* Translate COMMON blocks. */
2182 gfc_trans_common (ns);
2184 generate_local_vars (ns);
2186 current_function_return_label = NULL;
2188 /* Now generate the code for the body of this function. */
2189 gfc_init_block (&body);
2191 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
2192 && sym->attr.subroutine)
2194 tree alternate_return;
2195 alternate_return = gfc_get_fake_result_decl (sym);
2196 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
2201 /* Jump to the correct entry point. */
2202 tmp = gfc_trans_entry_master_switch (ns->entries);
2203 gfc_add_expr_to_block (&body, tmp);
2206 tmp = gfc_trans_code (ns->code);
2207 gfc_add_expr_to_block (&body, tmp);
2209 /* Add a return label if needed. */
2210 if (current_function_return_label)
2212 tmp = build1_v (LABEL_EXPR, current_function_return_label);
2213 gfc_add_expr_to_block (&body, tmp);
2216 tmp = gfc_finish_block (&body);
2217 /* Add code to create and cleanup arrays. */
2218 tmp = gfc_trans_deferred_vars (sym, tmp);
2219 gfc_add_expr_to_block (&block, tmp);
2221 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
2223 if (sym->attr.subroutine ||sym == sym->result)
2225 result = current_fake_result_decl;
2226 current_fake_result_decl = NULL_TREE;
2229 result = sym->result->backend_decl;
2231 if (result == NULL_TREE)
2232 warning ("Function return value not set");
2235 /* Set the return value to the dummy result variable. */
2236 tmp = build2 (MODIFY_EXPR, TREE_TYPE (result),
2237 DECL_RESULT (fndecl), result);
2238 tmp = build1_v (RETURN_EXPR, tmp);
2239 gfc_add_expr_to_block (&block, tmp);
2243 /* Add all the decls we created during processing. */
2244 decl = saved_function_decls;
2249 next = TREE_CHAIN (decl);
2250 TREE_CHAIN (decl) = NULL_TREE;
2254 saved_function_decls = NULL_TREE;
2256 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
2258 /* Finish off this function and send it for code generation. */
2260 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2262 /* Output the GENERIC tree. */
2263 dump_function (TDI_original, fndecl);
2265 /* Store the end of the function, so that we get good line number
2266 info for the epilogue. */
2267 cfun->function_end_locus = input_location;
2269 /* We're leaving the context of this function, so zap cfun.
2270 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2271 tree_rest_of_compilation. */
2276 pop_function_context ();
2277 saved_function_decls = saved_parent_function_decls;
2279 current_function_decl = old_context;
2281 if (decl_function_context (fndecl))
2282 /* Register this function with cgraph just far enough to get it
2283 added to our parent's nested function list. */
2284 (void) cgraph_node (fndecl);
2287 gfc_gimplify_function (fndecl);
2288 lower_nested_functions (fndecl);
2289 gfc_finalize (fndecl);
2294 gfc_generate_constructors (void)
2296 if (gfc_static_ctors != NULL_TREE)
2305 if (gfc_static_ctors == NULL_TREE)
2308 fnname = get_file_function_name ('I');
2309 type = build_function_type (void_type_node,
2310 gfc_chainon_list (NULL_TREE, void_type_node));
2312 fndecl = build_decl (FUNCTION_DECL, fnname, type);
2313 TREE_PUBLIC (fndecl) = 1;
2315 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
2316 DECL_ARTIFICIAL (decl) = 1;
2317 DECL_IGNORED_P (decl) = 1;
2318 DECL_CONTEXT (decl) = fndecl;
2319 DECL_RESULT (fndecl) = decl;
2323 current_function_decl = fndecl;
2325 rest_of_decl_compilation (fndecl, 1, 0);
2327 make_decl_rtl (fndecl);
2329 init_function_start (fndecl, input_filename, input_line);
2333 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
2336 gfc_build_function_call (TREE_VALUE (gfc_static_ctors), NULL_TREE);
2337 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
2342 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2344 free_after_parsing (cfun);
2345 free_after_compilation (cfun);
2347 tree_rest_of_compilation (fndecl, 0);
2349 current_function_decl = NULL_TREE;
2353 #include "gt-fortran-trans-decl.h"