1 /* gfortran backend interface
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* f95-lang.c-- GCC backend interface stuff */
24 /* declare required prototypes: */
30 #include "coretypes.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
43 #include "diagnostic.h"
44 #include "tree-dump.h"
46 /* For gfc_maybe_initialize_eh. */
54 #include "trans-types.h"
55 #include "trans-const.h"
57 /* Language-dependent contents of an identifier. */
61 struct tree_identifier common;
64 /* The resulting tree type. */
66 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
67 chain_next ("(union lang_tree_node *)TREE_CHAIN (&%h.generic)")))
70 union tree_node GTY((tag ("0"),
71 desc ("tree_node_structure (&%h)"))) generic;
72 struct lang_identifier GTY((tag ("1"))) identifier;
75 /* Save and restore the variables in this file and elsewhere
76 that keep track of the progress of compilation of the current function.
77 Used for nested functions. */
81 /* struct gfc_language_function base; */
82 struct binding_level *binding_level;
85 /* We don't have a lex/yacc lexer/parser, but toplev expects these to
87 void yyerror (const char *str);
90 static void gfc_init_decl_processing (void);
91 static void gfc_init_builtin_functions (void);
93 /* Each front end provides its own. */
94 static bool gfc_init (void);
95 static void gfc_finish (void);
96 static void gfc_print_identifier (FILE *, tree, int);
97 static bool gfc_mark_addressable (tree);
98 void do_function_end (void);
99 int global_bindings_p (void);
100 static void clear_binding_stack (void);
101 static void gfc_be_parse_file (int);
102 static alias_set_type gfc_get_alias_set (tree);
103 static void gfc_init_ts (void);
105 #undef LANG_HOOKS_NAME
106 #undef LANG_HOOKS_INIT
107 #undef LANG_HOOKS_FINISH
108 #undef LANG_HOOKS_INIT_OPTIONS
109 #undef LANG_HOOKS_HANDLE_OPTION
110 #undef LANG_HOOKS_POST_OPTIONS
111 #undef LANG_HOOKS_PRINT_IDENTIFIER
112 #undef LANG_HOOKS_PARSE_FILE
113 #undef LANG_HOOKS_MARK_ADDRESSABLE
114 #undef LANG_HOOKS_TYPE_FOR_MODE
115 #undef LANG_HOOKS_TYPE_FOR_SIZE
116 #undef LANG_HOOKS_GET_ALIAS_SET
117 #undef LANG_HOOKS_INIT_TS
118 #undef LANG_HOOKS_OMP_PRIVATIZE_BY_REFERENCE
119 #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
120 #undef LANG_HOOKS_OMP_CLAUSE_DEFAULT_CTOR
121 #undef LANG_HOOKS_OMP_CLAUSE_COPY_CTOR
122 #undef LANG_HOOKS_OMP_CLAUSE_ASSIGN_OP
123 #undef LANG_HOOKS_OMP_CLAUSE_DTOR
124 #undef LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR
125 #undef LANG_HOOKS_OMP_PRIVATE_DEBUG_CLAUSE
126 #undef LANG_HOOKS_OMP_PRIVATE_OUTER_REF
127 #undef LANG_HOOKS_OMP_FIRSTPRIVATIZE_TYPE_SIZES
128 #undef LANG_HOOKS_BUILTIN_FUNCTION
129 #undef LANG_HOOKS_GET_ARRAY_DESCR_INFO
131 /* Define lang hooks. */
132 #define LANG_HOOKS_NAME "GNU Fortran"
133 #define LANG_HOOKS_INIT gfc_init
134 #define LANG_HOOKS_FINISH gfc_finish
135 #define LANG_HOOKS_INIT_OPTIONS gfc_init_options
136 #define LANG_HOOKS_HANDLE_OPTION gfc_handle_option
137 #define LANG_HOOKS_POST_OPTIONS gfc_post_options
138 #define LANG_HOOKS_PRINT_IDENTIFIER gfc_print_identifier
139 #define LANG_HOOKS_PARSE_FILE gfc_be_parse_file
140 #define LANG_HOOKS_MARK_ADDRESSABLE gfc_mark_addressable
141 #define LANG_HOOKS_TYPE_FOR_MODE gfc_type_for_mode
142 #define LANG_HOOKS_TYPE_FOR_SIZE gfc_type_for_size
143 #define LANG_HOOKS_GET_ALIAS_SET gfc_get_alias_set
144 #define LANG_HOOKS_INIT_TS gfc_init_ts
145 #define LANG_HOOKS_OMP_PRIVATIZE_BY_REFERENCE gfc_omp_privatize_by_reference
146 #define LANG_HOOKS_OMP_PREDETERMINED_SHARING gfc_omp_predetermined_sharing
147 #define LANG_HOOKS_OMP_CLAUSE_DEFAULT_CTOR gfc_omp_clause_default_ctor
148 #define LANG_HOOKS_OMP_CLAUSE_COPY_CTOR gfc_omp_clause_copy_ctor
149 #define LANG_HOOKS_OMP_CLAUSE_ASSIGN_OP gfc_omp_clause_assign_op
150 #define LANG_HOOKS_OMP_CLAUSE_DTOR gfc_omp_clause_dtor
151 #define LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR gfc_omp_disregard_value_expr
152 #define LANG_HOOKS_OMP_PRIVATE_DEBUG_CLAUSE gfc_omp_private_debug_clause
153 #define LANG_HOOKS_OMP_PRIVATE_OUTER_REF gfc_omp_private_outer_ref
154 #define LANG_HOOKS_OMP_FIRSTPRIVATIZE_TYPE_SIZES \
155 gfc_omp_firstprivatize_type_sizes
156 #define LANG_HOOKS_BUILTIN_FUNCTION gfc_builtin_function
157 #define LANG_HOOKS_GET_ARRAY_DESCR_INFO gfc_get_array_descr_info
159 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
161 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
163 /* A chain of binding_level structures awaiting reuse. */
165 static GTY(()) struct binding_level *free_binding_level;
167 /* The elements of `ridpointers' are identifier nodes
168 for the reserved type names and storage classes.
169 It is indexed by a RID_... value. */
170 tree *ridpointers = NULL;
172 /* True means we've initialized exception handling. */
173 bool gfc_eh_initialized_p;
176 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
177 or validate its data type for an `if' or `while' statement or ?..: exp.
179 This preparation consists of taking the ordinary
180 representation of an expression expr and producing a valid tree
181 boolean expression describing whether expr is nonzero. We could
182 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
183 but we optimize comparisons, &&, ||, and !.
185 The resulting type should always be `boolean_type_node'.
186 This is much simpler than the corresponding C version because we have a
187 distinct boolean type. */
190 gfc_truthvalue_conversion (tree expr)
192 switch (TREE_CODE (TREE_TYPE (expr)))
195 if (TREE_TYPE (expr) == boolean_type_node)
197 else if (COMPARISON_CLASS_P (expr))
199 TREE_TYPE (expr) = boolean_type_node;
202 else if (TREE_CODE (expr) == NOP_EXPR)
203 return fold_build1 (NOP_EXPR,
204 boolean_type_node, TREE_OPERAND (expr, 0));
206 return fold_build1 (NOP_EXPR, boolean_type_node, expr);
209 if (TREE_CODE (expr) == INTEGER_CST)
210 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
212 return fold_build2 (NE_EXPR, boolean_type_node, expr,
213 build_int_cst (TREE_TYPE (expr), 0));
216 internal_error ("Unexpected type in truthvalue_conversion");
222 gfc_create_decls (void)
225 gfc_init_builtin_functions ();
227 /* Runtime/IO library functions. */
228 gfc_build_builtin_function_decls ();
230 gfc_init_constants ();
235 gfc_be_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
242 gfc_generate_constructors ();
244 /* Tell the frontend about any errors. */
245 gfc_get_errors (&warnings, &errors);
246 errorcount += errors;
247 warningcount += warnings;
249 clear_binding_stack ();
253 /* Initialize everything. */
258 if (!gfc_cpp_enabled ())
260 linemap_add (line_table, LC_ENTER, false, gfc_source_file, 1);
261 linemap_add (line_table, LC_RENAME, false, "<built-in>", 0);
266 gfc_init_decl_processing ();
267 gfc_static_ctors = NULL_TREE;
269 if (gfc_cpp_enabled ())
274 if (gfc_new_file () != SUCCESS)
275 fatal_error ("can't open input file: %s", gfc_source_file);
286 gfc_release_include_path ();
291 gfc_print_identifier (FILE * file ATTRIBUTE_UNUSED,
292 tree node ATTRIBUTE_UNUSED,
293 int indent ATTRIBUTE_UNUSED)
299 /* These functions and variables deal with binding contours. We only
300 need these functions for the list of PARM_DECLs, but we leave the
301 functions more general; these are a simplified version of the
302 functions from GNAT. */
304 /* For each binding contour we allocate a binding_level structure which
305 records the entities defined or declared in that contour. Contours
309 one for each subprogram definition
310 one for each compound statement (declare block)
312 Binding contours are used to create GCC tree BLOCK nodes. */
316 /* A chain of ..._DECL nodes for all variables, constants, functions,
317 parameters and type declarations. These ..._DECL nodes are chained
318 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
319 in the reverse of the order supplied to be compatible with the
322 /* For each level (except the global one), a chain of BLOCK nodes for all
323 the levels that were entered and exited one level down from this one. */
325 /* The binding level containing this one (the enclosing binding level). */
326 struct binding_level *level_chain;
329 /* The binding level currently in effect. */
330 static GTY(()) struct binding_level *current_binding_level = NULL;
332 /* The outermost binding level. This binding level is created when the
333 compiler is started and it will exist through the entire compilation. */
334 static GTY(()) struct binding_level *global_binding_level;
336 /* Binding level structures are initialized by copying this one. */
337 static struct binding_level clear_binding_level = { NULL, NULL, NULL };
340 /* Return nonzero if we are currently in the global binding level. */
343 global_bindings_p (void)
345 return current_binding_level == global_binding_level ? -1 : 0;
351 return current_binding_level->names;
354 /* Enter a new binding level. The input parameter is ignored, but has to be
355 specified for back-end compatibility. */
358 pushlevel (int ignore ATTRIBUTE_UNUSED)
360 struct binding_level *newlevel
361 = (struct binding_level *) ggc_alloc (sizeof (struct binding_level));
363 *newlevel = clear_binding_level;
365 /* Add this level to the front of the chain (stack) of levels that are
367 newlevel->level_chain = current_binding_level;
368 current_binding_level = newlevel;
371 /* Exit a binding level.
372 Pop the level off, and restore the state of the identifier-decl mappings
373 that were in effect when this level was entered.
375 If KEEP is nonzero, this level had explicit declarations, so
376 and create a "block" (a BLOCK node) for the level
377 to record its declarations and subblocks for symbol table output.
379 If FUNCTIONBODY is nonzero, this level is the body of a function,
380 so create a block as if KEEP were set and also clear out all
383 If REVERSE is nonzero, reverse the order of decls before putting
384 them into the BLOCK. */
387 poplevel (int keep, int reverse, int functionbody)
389 /* Points to a BLOCK tree node. This is the BLOCK node constructed for the
390 binding level that we are about to exit and which is returned by this
392 tree block_node = NULL_TREE;
394 tree subblock_chain = current_binding_level->blocks;
397 /* Reverse the list of XXXX_DECL nodes if desired. Note that the ..._DECL
398 nodes chained through the `names' field of current_binding_level are in
399 reverse order except for PARM_DECL node, which are explicitly stored in
401 decl_chain = (reverse) ? nreverse (current_binding_level->names)
402 : current_binding_level->names;
404 /* If there were any declarations in the current binding level, or if this
405 binding level is a function body, or if there are any nested blocks then
406 create a BLOCK node to record them for the life of this function. */
407 if (keep || functionbody)
408 block_node = build_block (keep ? decl_chain : 0, subblock_chain, 0, 0);
410 /* Record the BLOCK node just built as the subblock its enclosing scope. */
411 for (subblock_node = subblock_chain; subblock_node;
412 subblock_node = TREE_CHAIN (subblock_node))
413 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
415 /* Clear out the meanings of the local variables of this level. */
417 for (subblock_node = decl_chain; subblock_node;
418 subblock_node = TREE_CHAIN (subblock_node))
419 if (DECL_NAME (subblock_node) != 0)
420 /* If the identifier was used or addressed via a local extern decl,
421 don't forget that fact. */
422 if (DECL_EXTERNAL (subblock_node))
424 if (TREE_USED (subblock_node))
425 TREE_USED (DECL_NAME (subblock_node)) = 1;
426 if (TREE_ADDRESSABLE (subblock_node))
427 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1;
430 /* Pop the current level. */
431 current_binding_level = current_binding_level->level_chain;
434 /* This is the top level block of a function. */
435 DECL_INITIAL (current_function_decl) = block_node;
436 else if (current_binding_level == global_binding_level)
437 /* When using gfc_start_block/gfc_finish_block from middle-end hooks,
438 don't add newly created BLOCKs as subblocks of global_binding_level. */
442 current_binding_level->blocks
443 = chainon (current_binding_level->blocks, block_node);
446 /* If we did not make a block for the level just exited, any blocks made for
447 inner levels (since they cannot be recorded as subblocks in that level)
448 must be carried forward so they will later become subblocks of something
450 else if (subblock_chain)
451 current_binding_level->blocks
452 = chainon (current_binding_level->blocks, subblock_chain);
454 TREE_USED (block_node) = 1;
460 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
461 Returns the ..._DECL node. */
466 /* External objects aren't nested, other objects may be. */
467 if ((DECL_EXTERNAL (decl)) || (decl == current_function_decl))
468 DECL_CONTEXT (decl) = 0;
470 DECL_CONTEXT (decl) = current_function_decl;
472 /* Put the declaration on the list. The list of declarations is in reverse
473 order. The list will be reversed later if necessary. This needs to be
474 this way for compatibility with the back-end. */
476 TREE_CHAIN (decl) = current_binding_level->names;
477 current_binding_level->names = decl;
479 /* For the declaration of a type, set its name if it is not already set. */
481 if (TREE_CODE (decl) == TYPE_DECL && TYPE_NAME (TREE_TYPE (decl)) == 0)
483 if (DECL_SOURCE_LINE (decl) == 0)
484 TYPE_NAME (TREE_TYPE (decl)) = decl;
486 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
493 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL. */
496 pushdecl_top_level (tree x)
499 struct binding_level *b = current_binding_level;
501 current_binding_level = global_binding_level;
503 current_binding_level = b;
508 /* Clear the binding stack. */
510 clear_binding_stack (void)
512 while (!global_bindings_p ())
517 #ifndef CHAR_TYPE_SIZE
518 #define CHAR_TYPE_SIZE BITS_PER_UNIT
521 #ifndef INT_TYPE_SIZE
522 #define INT_TYPE_SIZE BITS_PER_WORD
526 #define SIZE_TYPE "long unsigned int"
528 /* Create tree nodes for the basic scalar types of Fortran 95,
529 and some nodes representing standard constants (0, 1, (void *) 0).
530 Initialize the global binding level.
531 Make definitions for built-in primitive functions. */
533 gfc_init_decl_processing (void)
535 current_function_decl = NULL;
536 current_binding_level = NULL_BINDING_LEVEL;
537 free_binding_level = NULL_BINDING_LEVEL;
539 /* Make the binding_level structure for global names. We move all
540 variables that are in a COMMON block to this binding level. */
542 global_binding_level = current_binding_level;
544 /* Build common tree nodes. char_type_node is unsigned because we
545 only use it for actual characters, not for INTEGER(1). Also, we
546 want double_type_node to actually have double precision. */
547 build_common_tree_nodes (false, false);
548 /* x86_64 mingw32 has a sizetype of "unsigned long long", most other hosts
549 have a sizetype of "unsigned long". Therefore choose the correct size
550 in mostly target independent way. */
551 if (TYPE_MODE (long_unsigned_type_node) == ptr_mode)
552 set_sizetype (long_unsigned_type_node);
553 else if (TYPE_MODE (long_long_unsigned_type_node) == ptr_mode)
554 set_sizetype (long_long_unsigned_type_node);
556 set_sizetype (long_unsigned_type_node);
557 build_common_tree_nodes_2 (0);
558 void_list_node = build_tree_list (NULL_TREE, void_type_node);
560 /* Set up F95 type nodes. */
566 /* Mark EXP saying that we need to be able to take the
567 address of it; it should not be allocated in a register.
568 In Fortran 95 this is only the case for variables with
569 the TARGET attribute, but we implement it here for a
570 likely future Cray pointer extension.
571 Value is 1 if successful. */
572 /* TODO: Check/fix mark_addressable. */
575 gfc_mark_addressable (tree exp)
577 register tree x = exp;
579 switch (TREE_CODE (x))
586 x = TREE_OPERAND (x, 0);
590 TREE_ADDRESSABLE (x) = 1;
597 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) && DECL_NONLOCAL (x))
601 error ("global register variable %qs used in nested function",
602 IDENTIFIER_POINTER (DECL_NAME (x)));
605 pedwarn (input_location, 0, "register variable %qs used in nested function",
606 IDENTIFIER_POINTER (DECL_NAME (x)));
608 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
612 error ("address of global register variable %qs requested",
613 IDENTIFIER_POINTER (DECL_NAME (x)));
618 /* If we are making this addressable due to its having
619 volatile components, give a different error message. Also
620 handle the case of an unnamed parameter by not trying
623 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
625 error ("cannot put object with volatile field into register");
630 pedwarn (input_location, 0, "address of register variable %qs requested",
631 IDENTIFIER_POINTER (DECL_NAME (x)));
636 TREE_ADDRESSABLE (x) = 1;
644 /* Return the typed-based alias set for T, which may be an expression
645 or a type. Return -1 if we don't do anything special. */
647 static alias_set_type
648 gfc_get_alias_set (tree t)
652 /* Permit type-punning when accessing an EQUIVALENCEd variable or
653 mixed type entry master's return value. */
654 for (u = t; handled_component_p (u); u = TREE_OPERAND (u, 0))
655 if (TREE_CODE (u) == COMPONENT_REF
656 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
663 /* press the big red button - garbage (ggc) collection is on */
667 /* Builtin function initialization. */
670 gfc_builtin_function (tree decl)
672 make_decl_rtl (decl);
679 gfc_define_builtin (const char *name,
682 const char *library_name,
687 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
688 library_name, NULL_TREE);
690 TREE_READONLY (decl) = 1;
692 built_in_decls[code] = decl;
693 implicit_built_in_decls[code] = decl;
697 #define DO_DEFINE_MATH_BUILTIN(code, name, argtype, tbase) \
698 gfc_define_builtin ("__builtin_" name "l", tbase##longdouble[argtype], \
699 BUILT_IN_ ## code ## L, name "l", true); \
700 gfc_define_builtin ("__builtin_" name, tbase##double[argtype], \
701 BUILT_IN_ ## code, name, true); \
702 gfc_define_builtin ("__builtin_" name "f", tbase##float[argtype], \
703 BUILT_IN_ ## code ## F, name "f", true);
705 #define DEFINE_MATH_BUILTIN(code, name, argtype) \
706 DO_DEFINE_MATH_BUILTIN (code, name, argtype, mfunc_)
708 #define DEFINE_MATH_BUILTIN_C(code, name, argtype) \
709 DO_DEFINE_MATH_BUILTIN (code, name, argtype, mfunc_) \
710 DO_DEFINE_MATH_BUILTIN (C##code, "c" name, argtype, mfunc_c)
713 /* Create function types for builtin functions. */
716 build_builtin_fntypes (tree *fntype, tree type)
720 /* type (*) (type) */
721 tmp = tree_cons (NULL_TREE, type, void_list_node);
722 fntype[0] = build_function_type (type, tmp);
723 /* type (*) (type, type) */
724 tmp = tree_cons (NULL_TREE, type, tmp);
725 fntype[1] = build_function_type (type, tmp);
726 /* type (*) (int, type) */
727 tmp = tree_cons (NULL_TREE, integer_type_node, void_list_node);
728 tmp = tree_cons (NULL_TREE, type, tmp);
729 fntype[2] = build_function_type (type, tmp);
730 /* type (*) (void) */
731 fntype[3] = build_function_type (type, void_list_node);
732 /* type (*) (type, &int) */
733 tmp = tree_cons (NULL_TREE, type, void_list_node);
734 tmp = tree_cons (NULL_TREE, build_pointer_type (integer_type_node), tmp);
735 fntype[4] = build_function_type (type, tmp);
736 /* type (*) (type, int) */
737 tmp = tree_cons (NULL_TREE, type, void_list_node);
738 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
739 fntype[5] = build_function_type (type, tmp);
744 builtin_type_for_size (int size, bool unsignedp)
746 tree type = lang_hooks.types.type_for_size (size, unsignedp);
747 return type ? type : error_mark_node;
750 /* Initialization of builtin function nodes. */
753 gfc_init_builtin_functions (void)
757 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
758 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
759 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
760 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
761 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
762 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
763 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
764 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
765 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
766 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
767 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
769 #undef DEF_PRIMITIVE_TYPE
770 #undef DEF_FUNCTION_TYPE_0
771 #undef DEF_FUNCTION_TYPE_1
772 #undef DEF_FUNCTION_TYPE_2
773 #undef DEF_FUNCTION_TYPE_3
774 #undef DEF_FUNCTION_TYPE_4
775 #undef DEF_FUNCTION_TYPE_5
776 #undef DEF_FUNCTION_TYPE_6
777 #undef DEF_FUNCTION_TYPE_7
778 #undef DEF_FUNCTION_TYPE_VAR_0
779 #undef DEF_POINTER_TYPE
782 typedef enum builtin_type builtin_type;
785 /* So far we need just these 2 attribute types. */
787 ATTR_CONST_NOTHROW_LIST
791 tree mfunc_double[6];
792 tree mfunc_longdouble[6];
793 tree mfunc_cfloat[6];
794 tree mfunc_cdouble[6];
795 tree mfunc_clongdouble[6];
796 tree func_cfloat_float, func_float_cfloat;
797 tree func_cdouble_double, func_double_cdouble;
798 tree func_clongdouble_longdouble, func_longdouble_clongdouble;
799 tree func_float_floatp_floatp;
800 tree func_double_doublep_doublep;
801 tree func_longdouble_longdoublep_longdoublep;
804 tree builtin_types[(int) BT_LAST + 1];
806 build_builtin_fntypes (mfunc_float, float_type_node);
807 build_builtin_fntypes (mfunc_double, double_type_node);
808 build_builtin_fntypes (mfunc_longdouble, long_double_type_node);
809 build_builtin_fntypes (mfunc_cfloat, complex_float_type_node);
810 build_builtin_fntypes (mfunc_cdouble, complex_double_type_node);
811 build_builtin_fntypes (mfunc_clongdouble, complex_long_double_type_node);
813 tmp = tree_cons (NULL_TREE, complex_float_type_node, void_list_node);
814 func_cfloat_float = build_function_type (float_type_node, tmp);
816 tmp = tree_cons (NULL_TREE, float_type_node, void_list_node);
817 func_float_cfloat = build_function_type (complex_float_type_node, tmp);
819 tmp = tree_cons (NULL_TREE, complex_double_type_node, void_list_node);
820 func_cdouble_double = build_function_type (double_type_node, tmp);
822 tmp = tree_cons (NULL_TREE, double_type_node, void_list_node);
823 func_double_cdouble = build_function_type (complex_double_type_node, tmp);
825 tmp = tree_cons (NULL_TREE, complex_long_double_type_node, void_list_node);
826 func_clongdouble_longdouble =
827 build_function_type (long_double_type_node, tmp);
829 tmp = tree_cons (NULL_TREE, long_double_type_node, void_list_node);
830 func_longdouble_clongdouble =
831 build_function_type (complex_long_double_type_node, tmp);
833 ptype = build_pointer_type (float_type_node);
834 tmp = tree_cons (NULL_TREE, float_type_node,
835 tree_cons (NULL_TREE, ptype,
836 tree_cons (NULL_TREE, ptype, void_list_node)));
837 func_float_floatp_floatp =
838 build_function_type (void_type_node, tmp);
840 ptype = build_pointer_type (double_type_node);
841 tmp = tree_cons (NULL_TREE, double_type_node,
842 tree_cons (NULL_TREE, ptype,
843 tree_cons (NULL_TREE, ptype, void_list_node)));
844 func_double_doublep_doublep =
845 build_function_type (void_type_node, tmp);
847 ptype = build_pointer_type (long_double_type_node);
848 tmp = tree_cons (NULL_TREE, long_double_type_node,
849 tree_cons (NULL_TREE, ptype,
850 tree_cons (NULL_TREE, ptype, void_list_node)));
851 func_longdouble_longdoublep_longdoublep =
852 build_function_type (void_type_node, tmp);
854 #include "mathbuiltins.def"
856 /* We define these separately as the fortran versions have different
857 semantics (they return an integer type) */
858 gfc_define_builtin ("__builtin_roundl", mfunc_longdouble[0],
859 BUILT_IN_ROUNDL, "roundl", true);
860 gfc_define_builtin ("__builtin_round", mfunc_double[0],
861 BUILT_IN_ROUND, "round", true);
862 gfc_define_builtin ("__builtin_roundf", mfunc_float[0],
863 BUILT_IN_ROUNDF, "roundf", true);
865 gfc_define_builtin ("__builtin_truncl", mfunc_longdouble[0],
866 BUILT_IN_TRUNCL, "truncl", true);
867 gfc_define_builtin ("__builtin_trunc", mfunc_double[0],
868 BUILT_IN_TRUNC, "trunc", true);
869 gfc_define_builtin ("__builtin_truncf", mfunc_float[0],
870 BUILT_IN_TRUNCF, "truncf", true);
872 gfc_define_builtin ("__builtin_cabsl", func_clongdouble_longdouble,
873 BUILT_IN_CABSL, "cabsl", true);
874 gfc_define_builtin ("__builtin_cabs", func_cdouble_double,
875 BUILT_IN_CABS, "cabs", true);
876 gfc_define_builtin ("__builtin_cabsf", func_cfloat_float,
877 BUILT_IN_CABSF, "cabsf", true);
879 gfc_define_builtin ("__builtin_copysignl", mfunc_longdouble[1],
880 BUILT_IN_COPYSIGNL, "copysignl", true);
881 gfc_define_builtin ("__builtin_copysign", mfunc_double[1],
882 BUILT_IN_COPYSIGN, "copysign", true);
883 gfc_define_builtin ("__builtin_copysignf", mfunc_float[1],
884 BUILT_IN_COPYSIGNF, "copysignf", true);
886 gfc_define_builtin ("__builtin_nextafterl", mfunc_longdouble[1],
887 BUILT_IN_NEXTAFTERL, "nextafterl", true);
888 gfc_define_builtin ("__builtin_nextafter", mfunc_double[1],
889 BUILT_IN_NEXTAFTER, "nextafter", true);
890 gfc_define_builtin ("__builtin_nextafterf", mfunc_float[1],
891 BUILT_IN_NEXTAFTERF, "nextafterf", true);
893 gfc_define_builtin ("__builtin_frexpl", mfunc_longdouble[4],
894 BUILT_IN_FREXPL, "frexpl", false);
895 gfc_define_builtin ("__builtin_frexp", mfunc_double[4],
896 BUILT_IN_FREXP, "frexp", false);
897 gfc_define_builtin ("__builtin_frexpf", mfunc_float[4],
898 BUILT_IN_FREXPF, "frexpf", false);
900 gfc_define_builtin ("__builtin_fabsl", mfunc_longdouble[0],
901 BUILT_IN_FABSL, "fabsl", true);
902 gfc_define_builtin ("__builtin_fabs", mfunc_double[0],
903 BUILT_IN_FABS, "fabs", true);
904 gfc_define_builtin ("__builtin_fabsf", mfunc_float[0],
905 BUILT_IN_FABSF, "fabsf", true);
907 gfc_define_builtin ("__builtin_scalbnl", mfunc_longdouble[5],
908 BUILT_IN_SCALBNL, "scalbnl", true);
909 gfc_define_builtin ("__builtin_scalbn", mfunc_double[5],
910 BUILT_IN_SCALBN, "scalbn", true);
911 gfc_define_builtin ("__builtin_scalbnf", mfunc_float[5],
912 BUILT_IN_SCALBNF, "scalbnf", true);
914 gfc_define_builtin ("__builtin_fmodl", mfunc_longdouble[1],
915 BUILT_IN_FMODL, "fmodl", true);
916 gfc_define_builtin ("__builtin_fmod", mfunc_double[1],
917 BUILT_IN_FMOD, "fmod", true);
918 gfc_define_builtin ("__builtin_fmodf", mfunc_float[1],
919 BUILT_IN_FMODF, "fmodf", true);
921 gfc_define_builtin ("__builtin_huge_vall", mfunc_longdouble[3],
922 BUILT_IN_HUGE_VALL, "__builtin_huge_vall", true);
923 gfc_define_builtin ("__builtin_huge_val", mfunc_double[3],
924 BUILT_IN_HUGE_VAL, "__builtin_huge_val", true);
925 gfc_define_builtin ("__builtin_huge_valf", mfunc_float[3],
926 BUILT_IN_HUGE_VALF, "__builtin_huge_valf", true);
928 /* lround{f,,l} and llround{f,,l} */
929 type = tree_cons (NULL_TREE, float_type_node, void_list_node);
930 tmp = build_function_type (long_integer_type_node, type);
931 gfc_define_builtin ("__builtin_lroundf", tmp, BUILT_IN_LROUNDF,
933 tmp = build_function_type (long_long_integer_type_node, type);
934 gfc_define_builtin ("__builtin_llroundf", tmp, BUILT_IN_LLROUNDF,
937 type = tree_cons (NULL_TREE, double_type_node, void_list_node);
938 tmp = build_function_type (long_integer_type_node, type);
939 gfc_define_builtin ("__builtin_lround", tmp, BUILT_IN_LROUND,
941 tmp = build_function_type (long_long_integer_type_node, type);
942 gfc_define_builtin ("__builtin_llround", tmp, BUILT_IN_LLROUND,
945 type = tree_cons (NULL_TREE, long_double_type_node, void_list_node);
946 tmp = build_function_type (long_integer_type_node, type);
947 gfc_define_builtin ("__builtin_lroundl", tmp, BUILT_IN_LROUNDL,
949 tmp = build_function_type (long_long_integer_type_node, type);
950 gfc_define_builtin ("__builtin_llroundl", tmp, BUILT_IN_LLROUNDL,
953 /* These are used to implement the ** operator. */
954 gfc_define_builtin ("__builtin_powl", mfunc_longdouble[1],
955 BUILT_IN_POWL, "powl", true);
956 gfc_define_builtin ("__builtin_pow", mfunc_double[1],
957 BUILT_IN_POW, "pow", true);
958 gfc_define_builtin ("__builtin_powf", mfunc_float[1],
959 BUILT_IN_POWF, "powf", true);
960 gfc_define_builtin ("__builtin_cpowl", mfunc_clongdouble[1],
961 BUILT_IN_CPOWL, "cpowl", true);
962 gfc_define_builtin ("__builtin_cpow", mfunc_cdouble[1],
963 BUILT_IN_CPOW, "cpow", true);
964 gfc_define_builtin ("__builtin_cpowf", mfunc_cfloat[1],
965 BUILT_IN_CPOWF, "cpowf", true);
966 gfc_define_builtin ("__builtin_powil", mfunc_longdouble[2],
967 BUILT_IN_POWIL, "powil", true);
968 gfc_define_builtin ("__builtin_powi", mfunc_double[2],
969 BUILT_IN_POWI, "powi", true);
970 gfc_define_builtin ("__builtin_powif", mfunc_float[2],
971 BUILT_IN_POWIF, "powif", true);
974 if (TARGET_C99_FUNCTIONS)
976 gfc_define_builtin ("__builtin_cbrtl", mfunc_longdouble[0],
977 BUILT_IN_CBRTL, "cbrtl", true);
978 gfc_define_builtin ("__builtin_cbrt", mfunc_double[0],
979 BUILT_IN_CBRT, "cbrt", true);
980 gfc_define_builtin ("__builtin_cbrtf", mfunc_float[0],
981 BUILT_IN_CBRTF, "cbrtf", true);
982 gfc_define_builtin ("__builtin_cexpil", func_longdouble_clongdouble,
983 BUILT_IN_CEXPIL, "cexpil", true);
984 gfc_define_builtin ("__builtin_cexpi", func_double_cdouble,
985 BUILT_IN_CEXPI, "cexpi", true);
986 gfc_define_builtin ("__builtin_cexpif", func_float_cfloat,
987 BUILT_IN_CEXPIF, "cexpif", true);
990 if (TARGET_HAS_SINCOS)
992 gfc_define_builtin ("__builtin_sincosl",
993 func_longdouble_longdoublep_longdoublep,
994 BUILT_IN_SINCOSL, "sincosl", false);
995 gfc_define_builtin ("__builtin_sincos", func_double_doublep_doublep,
996 BUILT_IN_SINCOS, "sincos", false);
997 gfc_define_builtin ("__builtin_sincosf", func_float_floatp_floatp,
998 BUILT_IN_SINCOSF, "sincosf", false);
1001 /* For LEADZ / TRAILZ. */
1002 tmp = tree_cons (NULL_TREE, unsigned_type_node, void_list_node);
1003 ftype = build_function_type (integer_type_node, tmp);
1004 gfc_define_builtin ("__builtin_clz", ftype, BUILT_IN_CLZ,
1005 "__builtin_clz", true);
1007 tmp = tree_cons (NULL_TREE, long_unsigned_type_node, void_list_node);
1008 ftype = build_function_type (integer_type_node, tmp);
1009 gfc_define_builtin ("__builtin_clzl", ftype, BUILT_IN_CLZL,
1010 "__builtin_clzl", true);
1012 tmp = tree_cons (NULL_TREE, long_long_unsigned_type_node, void_list_node);
1013 ftype = build_function_type (integer_type_node, tmp);
1014 gfc_define_builtin ("__builtin_clzll", ftype, BUILT_IN_CLZLL,
1015 "__builtin_clzll", true);
1017 tmp = tree_cons (NULL_TREE, unsigned_type_node, void_list_node);
1018 ftype = build_function_type (integer_type_node, tmp);
1019 gfc_define_builtin ("__builtin_ctz", ftype, BUILT_IN_CTZ,
1020 "__builtin_ctz", true);
1022 tmp = tree_cons (NULL_TREE, long_unsigned_type_node, void_list_node);
1023 ftype = build_function_type (integer_type_node, tmp);
1024 gfc_define_builtin ("__builtin_ctzl", ftype, BUILT_IN_CTZL,
1025 "__builtin_ctzl", true);
1027 tmp = tree_cons (NULL_TREE, long_long_unsigned_type_node, void_list_node);
1028 ftype = build_function_type (integer_type_node, tmp);
1029 gfc_define_builtin ("__builtin_ctzll", ftype, BUILT_IN_CTZLL,
1030 "__builtin_ctzll", true);
1032 /* Other builtin functions we use. */
1034 tmp = tree_cons (NULL_TREE, long_integer_type_node, void_list_node);
1035 tmp = tree_cons (NULL_TREE, long_integer_type_node, tmp);
1036 ftype = build_function_type (long_integer_type_node, tmp);
1037 gfc_define_builtin ("__builtin_expect", ftype, BUILT_IN_EXPECT,
1038 "__builtin_expect", true);
1040 tmp = tree_cons (NULL_TREE, pvoid_type_node, void_list_node);
1041 ftype = build_function_type (void_type_node, tmp);
1042 gfc_define_builtin ("__builtin_free", ftype, BUILT_IN_FREE,
1045 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
1046 ftype = build_function_type (pvoid_type_node, tmp);
1047 gfc_define_builtin ("__builtin_malloc", ftype, BUILT_IN_MALLOC,
1049 DECL_IS_MALLOC (built_in_decls[BUILT_IN_MALLOC]) = 1;
1051 tmp = tree_cons (NULL_TREE, pvoid_type_node, void_list_node);
1052 tmp = tree_cons (NULL_TREE, size_type_node, tmp);
1053 ftype = build_function_type (pvoid_type_node, tmp);
1054 gfc_define_builtin ("__builtin_realloc", ftype, BUILT_IN_REALLOC,
1057 tmp = tree_cons (NULL_TREE, void_type_node, void_list_node);
1058 ftype = build_function_type (integer_type_node, tmp);
1059 gfc_define_builtin ("__builtin_isnan", ftype, BUILT_IN_ISNAN,
1060 "__builtin_isnan", true);
1062 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
1063 builtin_types[(int) ENUM] = VALUE;
1064 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
1065 builtin_types[(int) ENUM] \
1066 = build_function_type (builtin_types[(int) RETURN], \
1068 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
1069 builtin_types[(int) ENUM] \
1070 = build_function_type (builtin_types[(int) RETURN], \
1071 tree_cons (NULL_TREE, \
1072 builtin_types[(int) ARG1], \
1074 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
1075 builtin_types[(int) ENUM] \
1076 = build_function_type \
1077 (builtin_types[(int) RETURN], \
1078 tree_cons (NULL_TREE, \
1079 builtin_types[(int) ARG1], \
1080 tree_cons (NULL_TREE, \
1081 builtin_types[(int) ARG2], \
1083 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
1084 builtin_types[(int) ENUM] \
1085 = build_function_type \
1086 (builtin_types[(int) RETURN], \
1087 tree_cons (NULL_TREE, \
1088 builtin_types[(int) ARG1], \
1089 tree_cons (NULL_TREE, \
1090 builtin_types[(int) ARG2], \
1091 tree_cons (NULL_TREE, \
1092 builtin_types[(int) ARG3], \
1094 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
1095 builtin_types[(int) ENUM] \
1096 = build_function_type \
1097 (builtin_types[(int) RETURN], \
1098 tree_cons (NULL_TREE, \
1099 builtin_types[(int) ARG1], \
1100 tree_cons (NULL_TREE, \
1101 builtin_types[(int) ARG2], \
1104 builtin_types[(int) ARG3], \
1105 tree_cons (NULL_TREE, \
1106 builtin_types[(int) ARG4], \
1107 void_list_node)))));
1108 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
1109 builtin_types[(int) ENUM] \
1110 = build_function_type \
1111 (builtin_types[(int) RETURN], \
1112 tree_cons (NULL_TREE, \
1113 builtin_types[(int) ARG1], \
1114 tree_cons (NULL_TREE, \
1115 builtin_types[(int) ARG2], \
1118 builtin_types[(int) ARG3], \
1119 tree_cons (NULL_TREE, \
1120 builtin_types[(int) ARG4], \
1121 tree_cons (NULL_TREE, \
1122 builtin_types[(int) ARG5],\
1123 void_list_node))))));
1124 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1126 builtin_types[(int) ENUM] \
1127 = build_function_type \
1128 (builtin_types[(int) RETURN], \
1129 tree_cons (NULL_TREE, \
1130 builtin_types[(int) ARG1], \
1131 tree_cons (NULL_TREE, \
1132 builtin_types[(int) ARG2], \
1135 builtin_types[(int) ARG3], \
1138 builtin_types[(int) ARG4], \
1139 tree_cons (NULL_TREE, \
1140 builtin_types[(int) ARG5], \
1141 tree_cons (NULL_TREE, \
1142 builtin_types[(int) ARG6],\
1143 void_list_node)))))));
1144 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1146 builtin_types[(int) ENUM] \
1147 = build_function_type \
1148 (builtin_types[(int) RETURN], \
1149 tree_cons (NULL_TREE, \
1150 builtin_types[(int) ARG1], \
1151 tree_cons (NULL_TREE, \
1152 builtin_types[(int) ARG2], \
1155 builtin_types[(int) ARG3], \
1158 builtin_types[(int) ARG4], \
1159 tree_cons (NULL_TREE, \
1160 builtin_types[(int) ARG5], \
1161 tree_cons (NULL_TREE, \
1162 builtin_types[(int) ARG6],\
1163 tree_cons (NULL_TREE, \
1164 builtin_types[(int) ARG6], \
1165 void_list_node))))))));
1166 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
1167 builtin_types[(int) ENUM] \
1168 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
1169 #define DEF_POINTER_TYPE(ENUM, TYPE) \
1170 builtin_types[(int) ENUM] \
1171 = build_pointer_type (builtin_types[(int) TYPE]);
1172 #include "types.def"
1173 #undef DEF_PRIMITIVE_TYPE
1174 #undef DEF_FUNCTION_TYPE_1
1175 #undef DEF_FUNCTION_TYPE_2
1176 #undef DEF_FUNCTION_TYPE_3
1177 #undef DEF_FUNCTION_TYPE_4
1178 #undef DEF_FUNCTION_TYPE_5
1179 #undef DEF_FUNCTION_TYPE_6
1180 #undef DEF_FUNCTION_TYPE_VAR_0
1181 #undef DEF_POINTER_TYPE
1182 builtin_types[(int) BT_LAST] = NULL_TREE;
1184 /* Initialize synchronization builtins. */
1185 #undef DEF_SYNC_BUILTIN
1186 #define DEF_SYNC_BUILTIN(code, name, type, attr) \
1187 gfc_define_builtin (name, builtin_types[type], code, name, \
1188 attr == ATTR_CONST_NOTHROW_LIST);
1189 #include "../sync-builtins.def"
1190 #undef DEF_SYNC_BUILTIN
1192 if (gfc_option.flag_openmp || flag_tree_parallelize_loops)
1194 #undef DEF_GOMP_BUILTIN
1195 #define DEF_GOMP_BUILTIN(code, name, type, attr) \
1196 gfc_define_builtin ("__builtin_" name, builtin_types[type], \
1197 code, name, attr == ATTR_CONST_NOTHROW_LIST);
1198 #include "../omp-builtins.def"
1199 #undef DEF_GOMP_BUILTIN
1202 gfc_define_builtin ("__builtin_trap", builtin_types[BT_FN_VOID],
1203 BUILT_IN_TRAP, NULL, false);
1204 TREE_THIS_VOLATILE (built_in_decls[BUILT_IN_TRAP]) = 1;
1206 gfc_define_builtin ("__emutls_get_address",
1207 builtin_types[BT_FN_PTR_PTR], BUILT_IN_EMUTLS_GET_ADDRESS,
1208 "__emutls_get_address", true);
1209 gfc_define_builtin ("__emutls_register_common",
1210 builtin_types[BT_FN_VOID_PTR_WORD_WORD_PTR],
1211 BUILT_IN_EMUTLS_REGISTER_COMMON,
1212 "__emutls_register_common", false);
1214 build_common_builtin_nodes ();
1215 targetm.init_builtins ();
1218 #undef DEFINE_MATH_BUILTIN_C
1219 #undef DEFINE_MATH_BUILTIN
1224 tree_contains_struct[NAMESPACE_DECL][TS_DECL_NON_COMMON] = 1;
1225 tree_contains_struct[NAMESPACE_DECL][TS_DECL_WITH_VIS] = 1;
1226 tree_contains_struct[NAMESPACE_DECL][TS_DECL_WRTL] = 1;
1227 tree_contains_struct[NAMESPACE_DECL][TS_DECL_COMMON] = 1;
1228 tree_contains_struct[NAMESPACE_DECL][TS_DECL_MINIMAL] = 1;
1232 gfc_maybe_initialize_eh (void)
1234 if (!flag_exceptions || gfc_eh_initialized_p)
1237 gfc_eh_initialized_p = true;
1238 eh_personality_libfunc
1239 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1240 ? "__gcc_personality_sj0"
1241 : "__gcc_personality_v0");
1242 default_init_unwind_resume_libfunc ();
1243 using_eh_for_cleanups ();
1247 #include "gt-fortran-f95-lang.h"
1248 #include "gtype-fortran.h"