1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2010, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 ****************************************************************************/
26 /* This file contains parts of the compiler that are required for interfacing
27 with GCC but otherwise do nothing and parts of Gigi that need to know
32 #include "coretypes.h"
35 #include "diagnostic.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
46 #include "function.h" /* For pass_by_reference. */
49 #include "adadecode.h"
63 static bool gnat_init (void);
64 static unsigned int gnat_init_options (unsigned int, const char **);
65 static int gnat_handle_option (size_t, const char *, int, int);
66 static bool gnat_post_options (const char **);
67 static alias_set_type gnat_get_alias_set (tree);
68 static void gnat_print_decl (FILE *, tree, int);
69 static void gnat_print_type (FILE *, tree, int);
70 static const char *gnat_printable_name (tree, int);
71 static const char *gnat_dwarf_name (tree, int);
72 static tree gnat_return_tree (tree);
73 static void gnat_parse_file (int);
74 static void internal_error_function (diagnostic_context *,
75 const char *, va_list *);
76 static tree gnat_type_max_size (const_tree);
77 static void gnat_get_subrange_bounds (const_tree, tree *, tree *);
78 static tree gnat_eh_personality (void);
80 /* Definitions for our language-specific hooks. */
82 #undef LANG_HOOKS_NAME
83 #define LANG_HOOKS_NAME "GNU Ada"
84 #undef LANG_HOOKS_IDENTIFIER_SIZE
85 #define LANG_HOOKS_IDENTIFIER_SIZE sizeof (struct tree_identifier)
86 #undef LANG_HOOKS_INIT
87 #define LANG_HOOKS_INIT gnat_init
88 #undef LANG_HOOKS_INIT_OPTIONS
89 #define LANG_HOOKS_INIT_OPTIONS gnat_init_options
90 #undef LANG_HOOKS_HANDLE_OPTION
91 #define LANG_HOOKS_HANDLE_OPTION gnat_handle_option
92 #undef LANG_HOOKS_POST_OPTIONS
93 #define LANG_HOOKS_POST_OPTIONS gnat_post_options
94 #undef LANG_HOOKS_PARSE_FILE
95 #define LANG_HOOKS_PARSE_FILE gnat_parse_file
96 #undef LANG_HOOKS_HASH_TYPES
97 #define LANG_HOOKS_HASH_TYPES false
98 #undef LANG_HOOKS_GETDECLS
99 #define LANG_HOOKS_GETDECLS lhd_return_null_tree_v
100 #undef LANG_HOOKS_PUSHDECL
101 #define LANG_HOOKS_PUSHDECL gnat_return_tree
102 #undef LANG_HOOKS_WRITE_GLOBALS
103 #define LANG_HOOKS_WRITE_GLOBALS gnat_write_global_declarations
104 #undef LANG_HOOKS_GET_ALIAS_SET
105 #define LANG_HOOKS_GET_ALIAS_SET gnat_get_alias_set
106 #undef LANG_HOOKS_PRINT_DECL
107 #define LANG_HOOKS_PRINT_DECL gnat_print_decl
108 #undef LANG_HOOKS_PRINT_TYPE
109 #define LANG_HOOKS_PRINT_TYPE gnat_print_type
110 #undef LANG_HOOKS_TYPE_MAX_SIZE
111 #define LANG_HOOKS_TYPE_MAX_SIZE gnat_type_max_size
112 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
113 #define LANG_HOOKS_DECL_PRINTABLE_NAME gnat_printable_name
114 #undef LANG_HOOKS_DWARF_NAME
115 #define LANG_HOOKS_DWARF_NAME gnat_dwarf_name
116 #undef LANG_HOOKS_GIMPLIFY_EXPR
117 #define LANG_HOOKS_GIMPLIFY_EXPR gnat_gimplify_expr
118 #undef LANG_HOOKS_TYPE_FOR_MODE
119 #define LANG_HOOKS_TYPE_FOR_MODE gnat_type_for_mode
120 #undef LANG_HOOKS_TYPE_FOR_SIZE
121 #define LANG_HOOKS_TYPE_FOR_SIZE gnat_type_for_size
122 #undef LANG_HOOKS_TYPES_COMPATIBLE_P
123 #define LANG_HOOKS_TYPES_COMPATIBLE_P gnat_types_compatible_p
124 #undef LANG_HOOKS_GET_SUBRANGE_BOUNDS
125 #define LANG_HOOKS_GET_SUBRANGE_BOUNDS gnat_get_subrange_bounds
126 #undef LANG_HOOKS_ATTRIBUTE_TABLE
127 #define LANG_HOOKS_ATTRIBUTE_TABLE gnat_internal_attribute_table
128 #undef LANG_HOOKS_BUILTIN_FUNCTION
129 #define LANG_HOOKS_BUILTIN_FUNCTION gnat_builtin_function
130 #undef LANG_HOOKS_EH_PERSONALITY
131 #define LANG_HOOKS_EH_PERSONALITY gnat_eh_personality
132 #undef LANG_HOOKS_DEEP_UNSHARING
133 #define LANG_HOOKS_DEEP_UNSHARING true
135 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
137 /* This symbol needs to be defined for the front-end. */
138 void *callgraph_info_file = NULL;
140 /* How much we want of our DWARF extensions. Some of our dwarf+ extensions
141 are incompatible with regular GDB versions, so we must make sure to only
142 produce them on explicit request. This is eventually reflected into the
143 use_gnu_debug_info_extensions common flag for later processing. */
144 static int gnat_dwarf_extensions = 0;
146 /* Command-line argc and argv. These variables are global
147 since they are imported in back_end.adb. */
148 unsigned int save_argc;
149 const char **save_argv;
151 /* GNAT argc and argv. */
152 extern int gnat_argc;
153 extern char **gnat_argv;
155 /* Declare functions we use as part of startup. */
156 extern void __gnat_initialize (void *);
157 extern void __gnat_install_SEH_handler (void *);
158 extern void adainit (void);
159 extern void _ada_gnat1drv (void);
161 /* The parser for the language. For us, we process the GNAT tree. */
164 gnat_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
168 /* Call the target specific initializations. */
169 __gnat_initialize (NULL);
171 /* ??? Call the SEH initialization routine. This is to workaround
172 a bootstrap path problem. The call below should be removed at some
173 point and the SEH pointer passed to __gnat_initialize() above. */
174 __gnat_install_SEH_handler((void *)seh);
176 /* Call the front-end elaboration procedures. */
179 /* Call the front end. */
183 /* Decode all the language specific options that cannot be decoded by GCC.
184 The option decoding phase of GCC calls this routine on the flags that
185 it cannot decode. Return the number of consecutive arguments from ARGV
186 that have been successfully decoded or 0 on failure. */
189 gnat_handle_option (size_t scode, const char *arg, int value,
190 int kind ATTRIBUTE_UNUSED)
192 const struct cl_option *option = &cl_options[scode];
193 enum opt_code code = (enum opt_code) scode;
196 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
198 error ("missing argument to \"-%s\"", option->opt_text);
205 q = XNEWVEC (char, sizeof("-I") + strlen (arg));
208 gnat_argv[gnat_argc] = q;
214 warn_uninitialized = value;
217 /* These are used in the GCC Makefile. */
218 case OPT_Wmissing_prototypes:
219 case OPT_Wstrict_prototypes:
220 case OPT_Wwrite_strings:
222 case OPT_Wvariadic_macros:
223 case OPT_Wold_style_definition:
224 case OPT_Wmissing_format_attribute:
225 case OPT_Woverlength_strings:
228 /* This is handled by the front-end. */
233 gnat_argv[gnat_argc] = xstrdup ("-nostdlib");
237 case OPT_feliminate_unused_debug_types:
238 /* We arrange for post_option to be able to only set the corresponding
239 flag to 1 when explicitly requested by the user. We expect the
240 default flag value to be either 0 or positive, and expose a positive
241 -f as a negative value to post_option. */
242 flag_eliminate_unused_debug_types = -value;
246 gnat_argv[gnat_argc] = xstrdup ("-fRTS");
251 warning (0, "%<-gnat%> misspelled as %<-gant%>");
253 /* ... fall through ... */
256 /* Recopy the switches without the 'gnat' prefix. */
257 gnat_argv[gnat_argc] = XNEWVEC (char, strlen (arg) + 2);
258 gnat_argv[gnat_argc][0] = '-';
259 strcpy (gnat_argv[gnat_argc] + 1, arg);
264 gnat_argv[gnat_argc] = xstrdup ("-O");
266 gnat_argv[gnat_argc] = xstrdup (arg);
271 gnat_dwarf_extensions = 1;
281 /* Initialize for option processing. */
284 gnat_init_options (unsigned int argc, const char **argv)
286 /* Initialize gnat_argv with save_argv size. */
287 gnat_argv = (char **) xmalloc ((argc + 1) * sizeof (argv[0]));
288 gnat_argv[0] = xstrdup (argv[0]); /* name of the command */
294 /* Uninitialized really means uninitialized in Ada. */
295 flag_zero_initialized_in_bss = 0;
300 /* Post-switch processing. */
303 gnat_post_options (const char **pfilename ATTRIBUTE_UNUSED)
305 /* Excess precision other than "fast" requires front-end
307 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
308 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
309 sorry ("-fexcess-precision=standard for Ada");
310 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
312 /* ??? The warning machinery is outsmarted by Ada. */
313 warn_unused_parameter = 0;
315 /* No psABI change warnings for Ada. */
318 /* Force eliminate_unused_debug_types to 0 unless an explicit positive
319 -f has been passed. This forces the default to 0 for Ada, which might
320 differ from the common default. */
321 if (flag_eliminate_unused_debug_types < 0)
322 flag_eliminate_unused_debug_types = 1;
324 flag_eliminate_unused_debug_types = 0;
326 /* Reflect the explicit request of DWARF extensions into the common
327 flag for use by later passes. */
328 if (write_symbols == DWARF2_DEBUG)
329 use_gnu_debug_info_extensions = gnat_dwarf_extensions > 0;
334 /* Here is the function to handle the compiler error processing in GCC. */
337 internal_error_function (diagnostic_context *context,
338 const char *msgid, va_list *ap)
341 char *buffer, *p, *loc;
342 String_Template temp, temp_loc;
343 Fat_Pointer fp, fp_loc;
346 /* Warn if plugins present. */
349 /* Reset the pretty-printer. */
350 pp_clear_output_area (context->printer);
352 /* Format the message into the pretty-printer. */
353 tinfo.format_spec = msgid;
355 tinfo.err_no = errno;
356 pp_format_verbatim (context->printer, &tinfo);
358 /* Extract a (writable) pointer to the formatted text. */
359 buffer = xstrdup (pp_formatted_text (context->printer));
361 /* Go up to the first newline. */
362 for (p = buffer; *p; p++)
370 temp.High_Bound = p - buffer;
374 s = expand_location (input_location);
375 if (context->show_column && s.column != 0)
376 asprintf (&loc, "%s:%d:%d", s.file, s.line, s.column);
378 asprintf (&loc, "%s:%d", s.file, s.line);
379 temp_loc.Low_Bound = 1;
380 temp_loc.High_Bound = strlen (loc);
381 fp_loc.Bounds = &temp_loc;
384 Current_Error_Node = error_gnat_node;
385 Compiler_Abort (fp, -1, fp_loc);
388 /* Perform all the initialization steps that are language-specific. */
393 /* Do little here, most of the standard declarations are set up after the
394 front-end has been run. Use the same `char' as C, this doesn't really
395 matter since we'll use the explicit `unsigned char' for Character. */
396 build_common_tree_nodes (flag_signed_char);
398 /* In Ada, we use the unsigned type corresponding to the width of Pmode as
399 SIZETYPE. In most cases when ptr_mode and Pmode differ, C will use the
400 width of ptr_mode for SIZETYPE, but we get better code using the width
401 of Pmode. Note that, although we manipulate negative offsets for some
402 internal constructs and rely on compile time overflow detection in size
403 computations, using unsigned types for SIZETYPEs is fine since they are
404 treated specially by the middle-end, in particular sign-extended. */
405 size_type_node = gnat_type_for_mode (Pmode, 1);
406 set_sizetype (size_type_node);
407 TYPE_NAME (sizetype) = get_identifier ("size_type");
409 /* In Ada, we use an unsigned 8-bit type for the default boolean type. */
410 boolean_type_node = make_unsigned_type (8);
411 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
412 SET_TYPE_RM_MAX_VALUE (boolean_type_node,
413 build_int_cst (boolean_type_node, 1));
414 SET_TYPE_RM_SIZE (boolean_type_node, bitsize_int (1));
416 build_common_tree_nodes_2 (0);
417 sbitsize_one_node = sbitsize_int (1);
418 sbitsize_unit_node = sbitsize_int (BITS_PER_UNIT);
419 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
421 ptr_void_type_node = build_pointer_type (void_type_node);
423 /* Show that REFERENCE_TYPEs are internal and should be Pmode. */
424 internal_reference_types ();
426 /* Add the input filename as the last argument. */
427 if (main_input_filename)
429 gnat_argv[gnat_argc] = xstrdup (main_input_filename);
431 gnat_argv[gnat_argc] = NULL;
434 /* Register our internal error function. */
435 global_dc->internal_error = &internal_error_function;
440 /* If we are using the GCC mechanism to process exception handling, we
441 have to register the personality routine for Ada and to initialize
442 various language dependent hooks. */
445 gnat_init_gcc_eh (void)
447 #ifdef DWARF2_UNWIND_INFO
448 /* lang_dependent_init already called dwarf2out_frame_init if true. */
449 int dwarf2out_frame_initialized = dwarf2out_do_frame ();
452 /* We shouldn't do anything if the No_Exceptions_Handler pragma is set,
453 though. This could for instance lead to the emission of tables with
454 references to symbols (such as the Ada eh personality routine) within
455 libraries we won't link against. */
456 if (No_Exception_Handlers_Set ())
459 /* Tell GCC we are handling cleanup actions through exception propagation.
460 This opens possibilities that we don't take advantage of yet, but is
461 nonetheless necessary to ensure that fixup code gets assigned to the
462 right exception regions. */
463 using_eh_for_cleanups ();
465 /* Turn on -fexceptions and -fnon-call-exceptions. The first one triggers
466 the generation of the necessary exception tables. The second one is
467 useful for two reasons: 1/ we map some asynchronous signals like SEGV to
468 exceptions, so we need to ensure that the insns which can lead to such
469 signals are correctly attached to the exception region they pertain to,
470 2/ Some calls to pure subprograms are handled as libcall blocks and then
471 marked as "cannot trap" if the flag is not set (see emit_libcall_block).
472 We should not let this be since it is possible for such calls to actually
475 flag_non_call_exceptions = 1;
478 #ifdef DWARF2_UNWIND_INFO
479 if (!dwarf2out_frame_initialized && dwarf2out_do_frame ())
480 dwarf2out_frame_init ();
484 /* Print language-specific items in declaration NODE. */
487 gnat_print_decl (FILE *file, tree node, int indent)
489 switch (TREE_CODE (node))
492 print_node (file, "corresponding var",
493 DECL_CONST_CORRESPONDING_VAR (node), indent + 4);
497 print_node (file, "original field", DECL_ORIGINAL_FIELD (node),
502 print_node (file, "renamed object", DECL_RENAMED_OBJECT (node),
511 /* Print language-specific items in type NODE. */
514 gnat_print_type (FILE *file, tree node, int indent)
516 switch (TREE_CODE (node))
519 print_node (file, "ci/co list", TYPE_CI_CO_LIST (node), indent + 4);
523 if (TYPE_MODULAR_P (node))
524 print_node_brief (file, "modulus", TYPE_MODULUS (node), indent + 4);
525 else if (TYPE_HAS_ACTUAL_BOUNDS_P (node))
526 print_node (file, "actual bounds", TYPE_ACTUAL_BOUNDS (node),
528 else if (TYPE_VAX_FLOATING_POINT_P (node))
531 print_node (file, "index type", TYPE_INDEX_TYPE (node), indent + 4);
533 /* ... fall through ... */
537 print_node_brief (file, "RM size", TYPE_RM_SIZE (node), indent + 4);
539 /* ... fall through ... */
542 print_node_brief (file, "RM min", TYPE_RM_MIN_VALUE (node), indent + 4);
543 print_node_brief (file, "RM max", TYPE_RM_MAX_VALUE (node), indent + 4);
547 print_node (file,"actual bounds", TYPE_ACTUAL_BOUNDS (node), indent + 4);
551 print_node (file,"representative array",
552 TYPE_REPRESENTATIVE_ARRAY (node), indent + 4);
556 if (TYPE_FAT_POINTER_P (node) || TYPE_CONTAINS_TEMPLATE_P (node))
557 print_node (file, "unconstrained array",
558 TYPE_UNCONSTRAINED_ARRAY (node), indent + 4);
560 print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
564 case QUAL_UNION_TYPE:
565 print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
573 /* Return the name to be printed for DECL. */
576 gnat_printable_name (tree decl, int verbosity)
578 const char *coded_name = IDENTIFIER_POINTER (DECL_NAME (decl));
579 char *ada_name = (char *) ggc_alloc_atomic (strlen (coded_name) * 2 + 60);
581 __gnat_decode (coded_name, ada_name, 0);
583 if (verbosity == 2 && !DECL_IS_BUILTIN (decl))
585 Set_Identifier_Casing (ada_name, DECL_SOURCE_FILE (decl));
586 return ggc_strdup (Name_Buffer);
592 /* Return the name to be used in DWARF debug info for DECL. */
595 gnat_dwarf_name (tree decl, int verbosity ATTRIBUTE_UNUSED)
597 gcc_assert (DECL_P (decl));
598 return (const char *) IDENTIFIER_POINTER (DECL_NAME (decl));
601 /* Do nothing (return the tree node passed). */
604 gnat_return_tree (tree t)
609 /* Get the alias set corresponding to a type or expression. */
611 static alias_set_type
612 gnat_get_alias_set (tree type)
614 /* If this is a padding type, use the type of the first field. */
615 if (TYPE_IS_PADDING_P (type))
616 return get_alias_set (TREE_TYPE (TYPE_FIELDS (type)));
618 /* If the type is an unconstrained array, use the type of the
619 self-referential array we make. */
620 else if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
622 get_alias_set (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type)))));
624 /* If the type can alias any other types, return the alias set 0. */
625 else if (TYPE_P (type)
626 && TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (type)))
632 /* GNU_TYPE is a type. Return its maximum size in bytes, if known,
633 as a constant when possible. */
636 gnat_type_max_size (const_tree gnu_type)
638 /* First see what we can get from TYPE_SIZE_UNIT, which might not
639 be constant even for simple expressions if it has already been
640 elaborated and possibly replaced by a VAR_DECL. */
641 tree max_unitsize = max_size (TYPE_SIZE_UNIT (gnu_type), true);
643 /* If we don't have a constant, see what we can get from TYPE_ADA_SIZE,
644 which should stay untouched. */
645 if (!host_integerp (max_unitsize, 1)
646 && (TREE_CODE (gnu_type) == RECORD_TYPE
647 || TREE_CODE (gnu_type) == UNION_TYPE
648 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
649 && TYPE_ADA_SIZE (gnu_type))
651 tree max_adasize = max_size (TYPE_ADA_SIZE (gnu_type), true);
653 /* If we have succeeded in finding a constant, round it up to the
654 type's alignment and return the result in units. */
655 if (host_integerp (max_adasize, 1))
657 = size_binop (CEIL_DIV_EXPR,
658 round_up (max_adasize, TYPE_ALIGN (gnu_type)),
665 /* GNU_TYPE is a subtype of an integral type. Set LOWVAL to the low bound
666 and HIGHVAL to the high bound, respectively. */
669 gnat_get_subrange_bounds (const_tree gnu_type, tree *lowval, tree *highval)
671 *lowval = TYPE_MIN_VALUE (gnu_type);
672 *highval = TYPE_MAX_VALUE (gnu_type);
675 /* GNU_TYPE is a type. Determine if it should be passed by reference by
679 default_pass_by_ref (tree gnu_type)
681 /* We pass aggregates by reference if they are sufficiently large. The
682 choice of constant here is somewhat arbitrary. We also pass by
683 reference if the target machine would either pass or return by
684 reference. Strictly speaking, we need only check the return if this
685 is an In Out parameter, but it's probably best to err on the side of
686 passing more things by reference. */
688 if (pass_by_reference (NULL, TYPE_MODE (gnu_type), gnu_type, 1))
691 if (targetm.calls.return_in_memory (gnu_type, NULL_TREE))
694 if (AGGREGATE_TYPE_P (gnu_type)
695 && (!host_integerp (TYPE_SIZE (gnu_type), 1)
696 || 0 < compare_tree_int (TYPE_SIZE (gnu_type),
697 8 * TYPE_ALIGN (gnu_type))))
703 /* GNU_TYPE is the type of a subprogram parameter. Determine from the type if
704 it should be passed by reference. */
707 must_pass_by_ref (tree gnu_type)
709 /* We pass only unconstrained objects, those required by the language
710 to be passed by reference, and objects of variable size. The latter
711 is more efficient, avoids problems with variable size temporaries,
712 and does not produce compatibility problems with C, since C does
713 not have such objects. */
714 return (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
715 || TREE_ADDRESSABLE (gnu_type)
716 || (TYPE_SIZE (gnu_type)
717 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST));
720 /* Return the size of the FP mode with precision PREC. */
723 fp_prec_to_size (int prec)
725 enum machine_mode mode;
727 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
728 mode = GET_MODE_WIDER_MODE (mode))
729 if (GET_MODE_PRECISION (mode) == prec)
730 return GET_MODE_BITSIZE (mode);
735 /* Return the precision of the FP mode with size SIZE. */
738 fp_size_to_prec (int size)
740 enum machine_mode mode;
742 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
743 mode = GET_MODE_WIDER_MODE (mode))
744 if (GET_MODE_BITSIZE (mode) == size)
745 return GET_MODE_PRECISION (mode);
750 static GTY(()) tree gnat_eh_personality_decl;
753 gnat_eh_personality (void)
755 if (!gnat_eh_personality_decl)
756 gnat_eh_personality_decl
757 = build_personality_function (USING_SJLJ_EXCEPTIONS
758 ? "__gnat_eh_personality_sj"
759 : "__gnat_eh_personality");
761 return gnat_eh_personality_decl;
764 #include "gt-ada-misc.h"