Add BID decimal support
[official-gcc.git] / gcc / toplev.c
blob8a471d8f955b8b4dbf9ae9ad2070e6322a9ba1f7
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
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
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This is the top level of cc1/c++.
24 It parses command args, opens files, invokes the various passes
25 in the proper order, and counts the time used by each.
26 Error messages and low-level interface to malloc also handled here. */
28 #include "config.h"
29 #undef FLOAT /* This is for hpux. They should change hpux. */
30 #undef FFS /* Some systems define this in param.h. */
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include <signal.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 # include <sys/resource.h>
38 #endif
40 #ifdef HAVE_SYS_TIMES_H
41 # include <sys/times.h>
42 #endif
44 #include "line-map.h"
45 #include "input.h"
46 #include "tree.h"
47 #include "version.h"
48 #include "rtl.h"
49 #include "tm_p.h"
50 #include "flags.h"
51 #include "insn-attr.h"
52 #include "insn-config.h"
53 #include "insn-flags.h"
54 #include "hard-reg-set.h"
55 #include "recog.h"
56 #include "output.h"
57 #include "except.h"
58 #include "function.h"
59 #include "toplev.h"
60 #include "expr.h"
61 #include "basic-block.h"
62 #include "intl.h"
63 #include "ggc.h"
64 #include "graph.h"
65 #include "regs.h"
66 #include "timevar.h"
67 #include "diagnostic.h"
68 #include "params.h"
69 #include "reload.h"
70 #include "dwarf2asm.h"
71 #include "integrate.h"
72 #include "real.h"
73 #include "debug.h"
74 #include "target.h"
75 #include "langhooks.h"
76 #include "cfglayout.h"
77 #include "cfgloop.h"
78 #include "hosthooks.h"
79 #include "cgraph.h"
80 #include "opts.h"
81 #include "coverage.h"
82 #include "value-prof.h"
83 #include "alloc-pool.h"
84 #include "tree-mudflap.h"
86 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
87 #include "dwarf2out.h"
88 #endif
90 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
91 #include "dbxout.h"
92 #endif
94 #ifdef SDB_DEBUGGING_INFO
95 #include "sdbout.h"
96 #endif
98 #ifdef XCOFF_DEBUGGING_INFO
99 #include "xcoffout.h" /* Needed for external data
100 declarations for e.g. AIX 4.x. */
101 #endif
103 static void general_init (const char *);
104 static void do_compile (void);
105 static void process_options (void);
106 static void backend_init (void);
107 static int lang_dependent_init (const char *);
108 static void init_asm_output (const char *);
109 static void finalize (void);
111 static void crash_signal (int) ATTRIBUTE_NORETURN;
112 static void setup_core_dumping (void);
113 static void compile_file (void);
115 /* Nonzero to dump debug info whilst parsing (-dy option). */
116 static int set_yydebug;
118 /* True if we don't need a backend (e.g. preprocessing only). */
119 static bool no_backend;
121 /* Length of line when printing switch values. */
122 #define MAX_LINE 75
124 /* Name of program invoked, sans directories. */
126 const char *progname;
128 /* Copy of argument vector to toplev_main. */
129 static const char **save_argv;
131 /* Name of top-level original source file (what was input to cpp).
132 This comes from the #-command at the beginning of the actual input.
133 If there isn't any there, then this is the cc1 input file name. */
135 const char *main_input_filename;
137 #ifndef USE_MAPPED_LOCATION
138 location_t unknown_location = { NULL, 0 };
139 #endif
141 /* Used to enable -fvar-tracking, -fweb and -frename-registers according
142 to optimize and default_debug_hooks in process_options (). */
143 #define AUTODETECT_VALUE 2
145 /* Current position in real source file. */
147 location_t input_location;
149 struct line_maps line_table;
151 /* Nonzero if it is unsafe to create any new pseudo registers. */
152 int no_new_pseudos;
154 /* Stack of currently pending input files. */
156 struct file_stack *input_file_stack;
158 /* Incremented on each change to input_file_stack. */
159 int input_file_stack_tick;
161 /* Record of input_file_stack at each tick. */
162 typedef struct file_stack *fs_p;
163 DEF_VEC_P(fs_p);
164 DEF_VEC_ALLOC_P(fs_p,heap);
165 static VEC(fs_p,heap) *input_file_stack_history;
167 /* Whether input_file_stack has been restored to a previous state (in
168 which case there should be no more pushing). */
169 static bool input_file_stack_restored;
171 /* Name to use as base of names for dump output files. */
173 const char *dump_base_name;
175 /* Name to use as a base for auxiliary output files. */
177 const char *aux_base_name;
179 /* Bit flags that specify the machine subtype we are compiling for.
180 Bits are tested using macros TARGET_... defined in the tm.h file
181 and set by `-m...' switches. Must be defined in rtlanal.c. */
183 extern int target_flags;
185 /* A mask of target_flags that includes bit X if X was set or cleared
186 on the command line. */
188 int target_flags_explicit;
190 /* Debug hooks - dependent upon command line options. */
192 const struct gcc_debug_hooks *debug_hooks;
194 /* Debug hooks - target default. */
196 static const struct gcc_debug_hooks *default_debug_hooks;
198 /* Other flags saying which kinds of debugging dump have been requested. */
200 int rtl_dump_and_exit;
201 int flag_print_asm_name;
202 enum graph_dump_types graph_dump_format;
204 /* Name for output file of assembly code, specified with -o. */
206 const char *asm_file_name;
208 /* Nonzero means do optimizations. -O.
209 Particular numeric values stand for particular amounts of optimization;
210 thus, -O2 stores 2 here. However, the optimizations beyond the basic
211 ones are not controlled directly by this variable. Instead, they are
212 controlled by individual `flag_...' variables that are defaulted
213 based on this variable. */
215 int optimize = 0;
217 /* Nonzero means optimize for size. -Os.
218 The only valid values are zero and nonzero. When optimize_size is
219 nonzero, optimize defaults to 2, but certain individual code
220 bloating optimizations are disabled. */
222 int optimize_size = 0;
224 /* The FUNCTION_DECL for the function currently being compiled,
225 or 0 if between functions. */
226 tree current_function_decl;
228 /* Set to the FUNC_BEGIN label of the current function, or NULL
229 if none. */
230 const char * current_function_func_begin_label;
232 /* Temporarily suppress certain warnings.
233 This is set while reading code from a system header file. */
235 int in_system_header = 0;
237 /* Nonzero means to collect statistics which might be expensive
238 and to print them when we are done. */
239 int flag_detailed_statistics = 0;
241 /* A random sequence of characters, unless overridden by user. */
242 static const char *flag_random_seed;
244 /* A local time stamp derived from the time of compilation. It will be
245 zero if the system cannot provide a time. It will be -1u, if the
246 user has specified a particular random seed. */
247 unsigned local_tick;
249 /* -f flags. */
251 /* Nonzero means `char' should be signed. */
253 int flag_signed_char;
255 /* Nonzero means give an enum type only as many bytes as it needs. A value
256 of 2 means it has not yet been initialized. */
258 int flag_short_enums;
260 /* Nonzero if structures and unions should be returned in memory.
262 This should only be defined if compatibility with another compiler or
263 with an ABI is needed, because it results in slower code. */
265 #ifndef DEFAULT_PCC_STRUCT_RETURN
266 #define DEFAULT_PCC_STRUCT_RETURN 1
267 #endif
269 /* Nonzero for -fpcc-struct-return: return values the same way PCC does. */
271 int flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
273 /* 0 means straightforward implementation of complex divide acceptable.
274 1 means wide ranges of inputs must work for complex divide.
275 2 means C99-like requirements for complex multiply and divide. */
277 int flag_complex_method = 1;
279 /* Nonzero means that we don't want inlining by virtue of -fno-inline,
280 not just because the tree inliner turned us off. */
282 int flag_really_no_inline = 2;
284 /* Nonzero means we should be saving declaration info into a .X file. */
286 int flag_gen_aux_info = 0;
288 /* Specified name of aux-info file. */
290 const char *aux_info_file_name;
292 /* Nonzero if we are compiling code for a shared library, zero for
293 executable. */
295 int flag_shlib;
297 /* Generate code for GNU or NeXT Objective-C runtime environment. */
299 #ifdef NEXT_OBJC_RUNTIME
300 int flag_next_runtime = 1;
301 #else
302 int flag_next_runtime = 0;
303 #endif
305 /* Set to the default thread-local storage (tls) model to use. */
307 enum tls_model flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
309 /* Nonzero means change certain warnings into errors.
310 Usually these are warnings about failure to conform to some standard. */
312 int flag_pedantic_errors = 0;
314 /* -dA causes debug commentary information to be produced in
315 the generated assembly code (to make it more readable). This option
316 is generally only of use to those who actually need to read the
317 generated assembly code (perhaps while debugging the compiler itself).
318 Currently, this switch is only used by dwarfout.c; however, it is intended
319 to be a catchall for printing debug information in the assembler file. */
321 int flag_debug_asm = 0;
323 /* -dP causes the rtl to be emitted as a comment in assembly. */
325 int flag_dump_rtl_in_asm = 0;
327 /* When non-NULL, indicates that whenever space is allocated on the
328 stack, the resulting stack pointer must not pass this
329 address---that is, for stacks that grow downward, the stack pointer
330 must always be greater than or equal to this address; for stacks
331 that grow upward, the stack pointer must be less than this address.
332 At present, the rtx may be either a REG or a SYMBOL_REF, although
333 the support provided depends on the backend. */
334 rtx stack_limit_rtx;
336 /* Nonzero if we should track variables. When
337 flag_var_tracking == AUTODETECT_VALUE it will be set according
338 to optimize, debug_info_level and debug_hooks in process_options (). */
339 int flag_var_tracking = AUTODETECT_VALUE;
341 /* True if the user has tagged the function with the 'section'
342 attribute. */
344 bool user_defined_section_attribute = false;
346 /* Values of the -falign-* flags: how much to align labels in code.
347 0 means `use default', 1 means `don't align'.
348 For each variable, there is an _log variant which is the power
349 of two not less than the variable, for .align output. */
351 int align_loops_log;
352 int align_loops_max_skip;
353 int align_jumps_log;
354 int align_jumps_max_skip;
355 int align_labels_log;
356 int align_labels_max_skip;
357 int align_functions_log;
359 /* Like align_functions_log above, but used by front-ends to force the
360 minimum function alignment. Zero means no alignment is forced. */
361 int force_align_functions_log;
363 typedef struct
365 const char *const string;
366 int *const variable;
367 const int on_value;
369 lang_independent_options;
371 /* Nonzero if subexpressions must be evaluated from left-to-right. */
372 int flag_evaluation_order = 0;
374 /* The user symbol prefix after having resolved same. */
375 const char *user_label_prefix;
377 static const param_info lang_independent_params[] = {
378 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
379 { OPTION, DEFAULT, false, MIN, MAX, HELP },
380 #include "params.def"
381 #undef DEFPARAM
382 { NULL, 0, false, 0, 0, NULL }
385 /* Output files for assembler code (real compiler output)
386 and debugging dumps. */
388 FILE *asm_out_file;
389 FILE *aux_info_file;
390 FILE *dump_file = NULL;
391 const char *dump_file_name;
393 /* The current working directory of a translation. It's generally the
394 directory from which compilation was initiated, but a preprocessed
395 file may specify the original directory in which it was
396 created. */
398 static const char *src_pwd;
400 /* Initialize src_pwd with the given string, and return true. If it
401 was already initialized, return false. As a special case, it may
402 be called with a NULL argument to test whether src_pwd has NOT been
403 initialized yet. */
405 bool
406 set_src_pwd (const char *pwd)
408 if (src_pwd)
410 if (strcmp (src_pwd, pwd) == 0)
411 return true;
412 else
413 return false;
416 src_pwd = xstrdup (pwd);
417 return true;
420 /* Return the directory from which the translation unit was initiated,
421 in case set_src_pwd() was not called before to assign it a
422 different value. */
424 const char *
425 get_src_pwd (void)
427 if (! src_pwd)
429 src_pwd = getpwd ();
430 if (!src_pwd)
431 src_pwd = ".";
434 return src_pwd;
437 /* Called when the start of a function definition is parsed,
438 this function prints on stderr the name of the function. */
439 void
440 announce_function (tree decl)
442 if (!quiet_flag)
444 if (rtl_dump_and_exit)
445 fprintf (stderr, "%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
446 else
447 fprintf (stderr, " %s", lang_hooks.decl_printable_name (decl, 2));
448 fflush (stderr);
449 pp_needs_newline (global_dc->printer) = true;
450 diagnostic_set_last_function (global_dc);
454 /* Initialize local_tick with the time of day, or -1 if
455 flag_random_seed is set. */
457 static void
458 init_local_tick (void)
460 if (!flag_random_seed)
462 /* Get some more or less random data. */
463 #ifdef HAVE_GETTIMEOFDAY
465 struct timeval tv;
467 gettimeofday (&tv, NULL);
468 local_tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
470 #else
472 time_t now = time (NULL);
474 if (now != (time_t)-1)
475 local_tick = (unsigned) now;
477 #endif
479 else
480 local_tick = -1;
483 /* Set up a default flag_random_seed and local_tick, unless the user
484 already specified one. Must be called after init_local_tick. */
486 static void
487 init_random_seed (void)
489 unsigned HOST_WIDE_INT value;
490 static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
492 value = local_tick ^ getpid ();
494 sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
495 flag_random_seed = random_seed;
498 /* Obtain the random_seed string. Unless NOINIT, initialize it if
499 it's not provided in the command line. */
501 const char *
502 get_random_seed (bool noinit)
504 if (!flag_random_seed && !noinit)
505 init_random_seed ();
506 return flag_random_seed;
509 /* Modify the random_seed string to VAL. Return its previous
510 value. */
512 const char *
513 set_random_seed (const char *val)
515 const char *old = flag_random_seed;
516 flag_random_seed = val;
517 return old;
520 /* Decode the string P as an integral parameter.
521 If the string is indeed an integer return its numeric value else
522 issue an Invalid Option error for the option PNAME and return DEFVAL.
523 If PNAME is zero just return DEFVAL, do not call error. */
526 read_integral_parameter (const char *p, const char *pname, const int defval)
528 const char *endp = p;
530 while (*endp)
532 if (ISDIGIT (*endp))
533 endp++;
534 else
535 break;
538 if (*endp != 0)
540 if (pname != 0)
541 error ("invalid option argument %qs", pname);
542 return defval;
545 return atoi (p);
548 /* When compiling with a recent enough GCC, we use the GNU C "extern inline"
549 for floor_log2 and exact_log2; see toplev.h. That construct, however,
550 conflicts with the ISO C++ One Definition Rule. */
552 #if GCC_VERSION < 3004 || !defined (__cplusplus)
554 /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
555 If X is 0, return -1. */
558 floor_log2 (unsigned HOST_WIDE_INT x)
560 int t = 0;
562 if (x == 0)
563 return -1;
565 #ifdef CLZ_HWI
566 t = HOST_BITS_PER_WIDE_INT - 1 - (int) CLZ_HWI (x);
567 #else
568 if (HOST_BITS_PER_WIDE_INT > 64)
569 if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
570 t += 64;
571 if (HOST_BITS_PER_WIDE_INT > 32)
572 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
573 t += 32;
574 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
575 t += 16;
576 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
577 t += 8;
578 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
579 t += 4;
580 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
581 t += 2;
582 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
583 t += 1;
584 #endif
586 return t;
589 /* Return the logarithm of X, base 2, considering X unsigned,
590 if X is a power of 2. Otherwise, returns -1. */
593 exact_log2 (unsigned HOST_WIDE_INT x)
595 if (x != (x & -x))
596 return -1;
597 #ifdef CTZ_HWI
598 return x ? CTZ_HWI (x) : -1;
599 #else
600 return floor_log2 (x);
601 #endif
604 #endif /* GCC_VERSION < 3004 || !defined (__cplusplus) */
606 /* Handler for fatal signals, such as SIGSEGV. These are transformed
607 into ICE messages, which is much more user friendly. In case the
608 error printer crashes, reset the signal to prevent infinite recursion. */
610 static void
611 crash_signal (int signo)
613 signal (signo, SIG_DFL);
615 /* If we crashed while processing an ASM statement, then be a little more
616 graceful. It's most likely the user's fault. */
617 if (this_is_asm_operands)
619 output_operand_lossage ("unrecoverable error");
620 exit (FATAL_EXIT_CODE);
623 internal_error ("%s", strsignal (signo));
626 /* Arrange to dump core on error. (The regular error message is still
627 printed first, except in the case of abort().) */
629 static void
630 setup_core_dumping (void)
632 #ifdef SIGABRT
633 signal (SIGABRT, SIG_DFL);
634 #endif
635 #if defined(HAVE_SETRLIMIT)
637 struct rlimit rlim;
638 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
639 fatal_error ("getting core file size maximum limit: %m");
640 rlim.rlim_cur = rlim.rlim_max;
641 if (setrlimit (RLIMIT_CORE, &rlim) != 0)
642 fatal_error ("setting core file size limit to maximum: %m");
644 #endif
645 diagnostic_abort_on_error (global_dc);
649 /* Strip off a legitimate source ending from the input string NAME of
650 length LEN. Rather than having to know the names used by all of
651 our front ends, we strip off an ending of a period followed by
652 up to five characters. (Java uses ".class".) */
654 void
655 strip_off_ending (char *name, int len)
657 int i;
658 for (i = 2; i < 6 && len > i; i++)
660 if (name[len - i] == '.')
662 name[len - i] = '\0';
663 break;
668 /* Output a quoted string. */
670 void
671 output_quoted_string (FILE *asm_file, const char *string)
673 #ifdef OUTPUT_QUOTED_STRING
674 OUTPUT_QUOTED_STRING (asm_file, string);
675 #else
676 char c;
678 putc ('\"', asm_file);
679 while ((c = *string++) != 0)
681 if (ISPRINT (c))
683 if (c == '\"' || c == '\\')
684 putc ('\\', asm_file);
685 putc (c, asm_file);
687 else
688 fprintf (asm_file, "\\%03o", (unsigned char) c);
690 putc ('\"', asm_file);
691 #endif
694 /* Output a file name in the form wanted by System V. */
696 void
697 output_file_directive (FILE *asm_file, const char *input_name)
699 int len;
700 const char *na;
702 if (input_name == NULL)
703 input_name = "<stdin>";
705 len = strlen (input_name);
706 na = input_name + len;
708 /* NA gets INPUT_NAME sans directory names. */
709 while (na > input_name)
711 if (IS_DIR_SEPARATOR (na[-1]))
712 break;
713 na--;
716 #ifdef ASM_OUTPUT_SOURCE_FILENAME
717 ASM_OUTPUT_SOURCE_FILENAME (asm_file, na);
718 #else
719 fprintf (asm_file, "\t.file\t");
720 output_quoted_string (asm_file, na);
721 fputc ('\n', asm_file);
722 #endif
725 /* A subroutine of wrapup_global_declarations. We've come to the end of
726 the compilation unit. All deferred variables should be undeferred,
727 and all incomplete decls should be finalized. */
729 void
730 wrapup_global_declaration_1 (tree decl)
732 /* We're not deferring this any longer. Assignment is conditional to
733 avoid needlessly dirtying PCH pages. */
734 if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
735 && DECL_DEFER_OUTPUT (decl) != 0)
736 DECL_DEFER_OUTPUT (decl) = 0;
738 if (TREE_CODE (decl) == VAR_DECL && DECL_SIZE (decl) == 0)
739 lang_hooks.finish_incomplete_decl (decl);
742 /* A subroutine of wrapup_global_declarations. Decide whether or not DECL
743 needs to be output. Return true if it is output. */
745 bool
746 wrapup_global_declaration_2 (tree decl)
748 if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl))
749 return false;
751 /* Don't write out static consts, unless we still need them.
753 We also keep static consts if not optimizing (for debugging),
754 unless the user specified -fno-keep-static-consts.
755 ??? They might be better written into the debug information.
756 This is possible when using DWARF.
758 A language processor that wants static constants to be always
759 written out (even if it is not used) is responsible for
760 calling rest_of_decl_compilation itself. E.g. the C front-end
761 calls rest_of_decl_compilation from finish_decl.
762 One motivation for this is that is conventional in some
763 environments to write things like:
764 static const char rcsid[] = "... version string ...";
765 intending to force the string to be in the executable.
767 A language processor that would prefer to have unneeded
768 static constants "optimized away" would just defer writing
769 them out until here. E.g. C++ does this, because static
770 constants are often defined in header files.
772 ??? A tempting alternative (for both C and C++) would be
773 to force a constant to be written if and only if it is
774 defined in a main file, as opposed to an include file. */
776 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
778 struct varpool_node *node;
779 bool needed = true;
780 node = varpool_node (decl);
782 if (node->finalized)
783 needed = false;
784 else if (node->alias)
785 needed = false;
786 else if (!cgraph_global_info_ready
787 && (TREE_USED (decl)
788 || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
789 /* needed */;
790 else if (node->needed)
791 /* needed */;
792 else if (DECL_COMDAT (decl))
793 needed = false;
794 else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
795 && (optimize || !flag_keep_static_consts
796 || DECL_ARTIFICIAL (decl)))
797 needed = false;
799 if (needed)
801 rest_of_decl_compilation (decl, 1, 1);
802 return true;
806 return false;
809 /* Do any final processing required for the declarations in VEC, of
810 which there are LEN. We write out inline functions and variables
811 that have been deferred until this point, but which are required.
812 Returns nonzero if anything was put out. */
814 bool
815 wrapup_global_declarations (tree *vec, int len)
817 bool reconsider, output_something = false;
818 int i;
820 for (i = 0; i < len; i++)
821 wrapup_global_declaration_1 (vec[i]);
823 /* Now emit any global variables or functions that we have been
824 putting off. We need to loop in case one of the things emitted
825 here references another one which comes earlier in the list. */
828 reconsider = false;
829 for (i = 0; i < len; i++)
830 reconsider |= wrapup_global_declaration_2 (vec[i]);
831 if (reconsider)
832 output_something = true;
834 while (reconsider);
836 return output_something;
839 /* A subroutine of check_global_declarations. Issue appropriate warnings
840 for the global declaration DECL. */
842 void
843 check_global_declaration_1 (tree decl)
845 /* Warn about any function declared static but not defined. We don't
846 warn about variables, because many programs have static variables
847 that exist only to get some text into the object file. */
848 if (TREE_CODE (decl) == FUNCTION_DECL
849 && DECL_INITIAL (decl) == 0
850 && DECL_EXTERNAL (decl)
851 && ! DECL_ARTIFICIAL (decl)
852 && ! TREE_NO_WARNING (decl)
853 && ! TREE_PUBLIC (decl)
854 && (warn_unused_function
855 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
857 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
858 pedwarn ("%q+F used but never defined", decl);
859 else
860 warning (0, "%q+F declared %<static%> but never defined", decl);
861 /* This symbol is effectively an "extern" declaration now. */
862 TREE_PUBLIC (decl) = 1;
863 assemble_external (decl);
866 /* Warn about static fns or vars defined but not used. */
867 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
868 /* We don't warn about "static const" variables because the
869 "rcs_id" idiom uses that construction. */
870 || (warn_unused_variable
871 && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
872 && ! DECL_IN_SYSTEM_HEADER (decl)
873 && ! TREE_USED (decl)
874 /* The TREE_USED bit for file-scope decls is kept in the identifier,
875 to handle multiple external decls in different scopes. */
876 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
877 && ! DECL_EXTERNAL (decl)
878 && ! TREE_PUBLIC (decl)
879 /* A volatile variable might be used in some non-obvious way. */
880 && ! TREE_THIS_VOLATILE (decl)
881 /* Global register variables must be declared to reserve them. */
882 && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
883 /* Otherwise, ask the language. */
884 && lang_hooks.decls.warn_unused_global (decl))
885 warning (0, "%q+D defined but not used", decl);
888 /* Issue appropriate warnings for the global declarations in VEC (of
889 which there are LEN). */
891 void
892 check_global_declarations (tree *vec, int len)
894 int i;
896 for (i = 0; i < len; i++)
897 check_global_declaration_1 (vec[i]);
900 /* Emit debugging information for all global declarations in VEC. */
902 void
903 emit_debug_global_declarations (tree *vec, int len)
905 int i;
907 /* Avoid confusing the debug information machinery when there are errors. */
908 if (errorcount != 0 || sorrycount != 0)
909 return;
911 timevar_push (TV_SYMOUT);
912 for (i = 0; i < len; i++)
913 debug_hooks->global_decl (vec[i]);
914 timevar_pop (TV_SYMOUT);
917 /* Warn about a use of an identifier which was marked deprecated. */
918 void
919 warn_deprecated_use (tree node)
921 if (node == 0 || !warn_deprecated_decl)
922 return;
924 if (DECL_P (node))
926 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
927 warning (OPT_Wdeprecated_declarations,
928 "%qD is deprecated (declared at %s:%d)",
929 node, xloc.file, xloc.line);
931 else if (TYPE_P (node))
933 const char *what = NULL;
934 tree decl = TYPE_STUB_DECL (node);
936 if (TYPE_NAME (node))
938 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
939 what = IDENTIFIER_POINTER (TYPE_NAME (node));
940 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
941 && DECL_NAME (TYPE_NAME (node)))
942 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
945 if (decl)
947 expanded_location xloc
948 = expand_location (DECL_SOURCE_LOCATION (decl));
949 if (what)
950 warning (OPT_Wdeprecated_declarations,
951 "%qs is deprecated (declared at %s:%d)", what,
952 xloc.file, xloc.line);
953 else
954 warning (OPT_Wdeprecated_declarations,
955 "type is deprecated (declared at %s:%d)",
956 xloc.file, xloc.line);
958 else
960 if (what)
961 warning (OPT_Wdeprecated_declarations, "%qs is deprecated", what);
962 else
963 warning (OPT_Wdeprecated_declarations, "type is deprecated");
968 /* Save the current INPUT_LOCATION on the top entry in the
969 INPUT_FILE_STACK. Push a new entry for FILE and LINE, and set the
970 INPUT_LOCATION accordingly. */
972 void
973 #ifdef USE_MAPPED_LOCATION
974 push_srcloc (location_t fline)
975 #else
976 push_srcloc (const char *file, int line)
977 #endif
979 struct file_stack *fs;
981 gcc_assert (!input_file_stack_restored);
982 if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
983 sorry ("GCC supports only %d input file changes", input_file_stack_tick);
985 fs = XNEW (struct file_stack);
986 fs->location = input_location;
987 fs->next = input_file_stack;
988 #ifdef USE_MAPPED_LOCATION
989 input_location = fline;
990 #else
991 input_filename = file;
992 input_line = line;
993 #endif
994 input_file_stack = fs;
995 input_file_stack_tick++;
996 VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
999 /* Pop the top entry off the stack of presently open source files.
1000 Restore the INPUT_LOCATION from the new topmost entry on the
1001 stack. */
1003 void
1004 pop_srcloc (void)
1006 struct file_stack *fs;
1008 gcc_assert (!input_file_stack_restored);
1009 if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
1010 sorry ("GCC supports only %d input file changes", input_file_stack_tick);
1012 fs = input_file_stack;
1013 input_location = fs->location;
1014 input_file_stack = fs->next;
1015 input_file_stack_tick++;
1016 VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
1019 /* Restore the input file stack to its state as of TICK, for the sake
1020 of diagnostics after processing the whole input. Once this has
1021 been called, push_srcloc and pop_srcloc may no longer be
1022 called. */
1023 void
1024 restore_input_file_stack (int tick)
1026 if (tick == 0)
1027 input_file_stack = NULL;
1028 else
1029 input_file_stack = VEC_index (fs_p, input_file_stack_history, tick - 1);
1030 input_file_stack_tick = tick;
1031 input_file_stack_restored = true;
1034 /* Compile an entire translation unit. Write a file of assembly
1035 output and various debugging dumps. */
1037 static void
1038 compile_file (void)
1040 /* Initialize yet another pass. */
1042 init_cgraph ();
1043 init_final (main_input_filename);
1044 coverage_init (aux_base_name);
1046 timevar_push (TV_PARSE);
1048 /* Call the parser, which parses the entire file (calling
1049 rest_of_compilation for each function). */
1050 lang_hooks.parse_file (set_yydebug);
1052 /* In case there were missing block closers,
1053 get us back to the global binding level. */
1054 lang_hooks.clear_binding_stack ();
1056 /* Compilation is now finished except for writing
1057 what's left of the symbol table output. */
1058 timevar_pop (TV_PARSE);
1060 if (flag_syntax_only)
1061 return;
1063 lang_hooks.decls.final_write_globals ();
1065 if (errorcount || sorrycount)
1066 return;
1068 varpool_assemble_pending_decls ();
1069 finish_aliases_2 ();
1071 /* This must occur after the loop to output deferred functions.
1072 Else the coverage initializer would not be emitted if all the
1073 functions in this compilation unit were deferred. */
1074 coverage_finish ();
1076 /* Likewise for mudflap static object registrations. */
1077 if (flag_mudflap)
1078 mudflap_finish_file ();
1080 /* Likewise for emulated thread-local storage. */
1081 if (!targetm.have_tls)
1082 emutls_finish ();
1084 output_shared_constant_pool ();
1085 output_object_blocks ();
1087 /* Write out any pending weak symbol declarations. */
1088 weak_finish ();
1090 /* Do dbx symbols. */
1091 timevar_push (TV_SYMOUT);
1093 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
1094 if (dwarf2out_do_frame ())
1095 dwarf2out_frame_finish ();
1096 #endif
1098 (*debug_hooks->finish) (main_input_filename);
1099 timevar_pop (TV_SYMOUT);
1101 /* Output some stuff at end of file if nec. */
1103 dw2_output_indirect_constants ();
1105 /* Flush any pending external directives. */
1106 process_pending_assemble_externals ();
1108 /* Attach a special .ident directive to the end of the file to identify
1109 the version of GCC which compiled this code. The format of the .ident
1110 string is patterned after the ones produced by native SVR4 compilers. */
1111 #ifdef IDENT_ASM_OP
1112 if (!flag_no_ident)
1113 fprintf (asm_out_file, "%s\"GCC: (GNU) %s\"\n",
1114 IDENT_ASM_OP, version_string);
1115 #endif
1117 /* This must be at the end. Some target ports emit end of file directives
1118 into the assembly file here, and hence we can not output anything to the
1119 assembly file after this point. */
1120 targetm.asm_out.file_end ();
1123 /* Parse a -d... command line switch. */
1125 void
1126 decode_d_option (const char *arg)
1128 int c;
1130 while (*arg)
1131 switch (c = *arg++)
1133 case 'A':
1134 flag_debug_asm = 1;
1135 break;
1136 case 'p':
1137 flag_print_asm_name = 1;
1138 break;
1139 case 'P':
1140 flag_dump_rtl_in_asm = 1;
1141 flag_print_asm_name = 1;
1142 break;
1143 case 'v':
1144 graph_dump_format = vcg;
1145 break;
1146 case 'x':
1147 rtl_dump_and_exit = 1;
1148 break;
1149 case 'y':
1150 set_yydebug = 1;
1151 break;
1152 case 'D': /* These are handled by the preprocessor. */
1153 case 'I':
1154 break;
1155 case 'H':
1156 setup_core_dumping();
1157 break;
1159 case 'a':
1160 default:
1161 if (!enable_rtl_dump_file (c))
1162 warning (0, "unrecognized gcc debugging option: %c", c);
1163 break;
1167 /* Indexed by enum debug_info_type. */
1168 const char *const debug_type_names[] =
1170 "none", "stabs", "coff", "dwarf-2", "xcoff", "vms"
1173 /* Print version information to FILE.
1174 Each line begins with INDENT (for the case where FILE is the
1175 assembler output file). */
1177 void
1178 print_version (FILE *file, const char *indent)
1180 static const char fmt1[] =
1181 #ifdef __GNUC__
1182 N_("%s%s%s version %s (%s)\n%s\tcompiled by GNU C version %s.\n")
1183 #else
1184 N_("%s%s%s version %s (%s) compiled by CC.\n")
1185 #endif
1187 static const char fmt2[] =
1188 N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
1189 #ifndef __VERSION__
1190 #define __VERSION__ "[?]"
1191 #endif
1192 fprintf (file,
1193 file == stderr ? _(fmt1) : fmt1,
1194 indent, *indent != 0 ? " " : "",
1195 lang_hooks.name, version_string, TARGET_NAME,
1196 indent, __VERSION__);
1197 fprintf (file,
1198 file == stderr ? _(fmt2) : fmt2,
1199 indent, *indent != 0 ? " " : "",
1200 PARAM_VALUE (GGC_MIN_EXPAND), PARAM_VALUE (GGC_MIN_HEAPSIZE));
1203 #ifdef ASM_COMMENT_START
1204 static int
1205 print_to_asm_out_file (print_switch_type type, const char * text)
1207 bool prepend_sep = true;
1209 switch (type)
1211 case SWITCH_TYPE_LINE_END:
1212 putc ('\n', asm_out_file);
1213 return 1;
1215 case SWITCH_TYPE_LINE_START:
1216 fputs (ASM_COMMENT_START, asm_out_file);
1217 return strlen (ASM_COMMENT_START);
1219 case SWITCH_TYPE_DESCRIPTIVE:
1220 if (ASM_COMMENT_START[0] == 0)
1221 prepend_sep = false;
1222 /* Drop through. */
1223 case SWITCH_TYPE_PASSED:
1224 case SWITCH_TYPE_ENABLED:
1225 if (prepend_sep)
1226 fputc (' ', asm_out_file);
1227 fprintf (asm_out_file, text);
1228 /* No need to return the length here as
1229 print_single_switch has already done it. */
1230 return 0;
1232 default:
1233 return -1;
1236 #endif
1238 static int
1239 print_to_stderr (print_switch_type type, const char * text)
1241 switch (type)
1243 case SWITCH_TYPE_LINE_END:
1244 putc ('\n', stderr);
1245 return 1;
1247 case SWITCH_TYPE_LINE_START:
1248 return 0;
1250 case SWITCH_TYPE_PASSED:
1251 case SWITCH_TYPE_ENABLED:
1252 fputc (' ', stderr);
1253 /* Drop through. */
1255 case SWITCH_TYPE_DESCRIPTIVE:
1256 fprintf (stderr, text);
1257 /* No need to return the length here as
1258 print_single_switch has already done it. */
1259 return 0;
1261 default:
1262 return -1;
1266 /* Print an option value and return the adjusted position in the line.
1267 ??? print_fn doesn't handle errors, eg disk full; presumably other
1268 code will catch a disk full though. */
1270 static int
1271 print_single_switch (print_switch_fn_type print_fn,
1272 int pos,
1273 print_switch_type type,
1274 const char * text)
1276 /* The ultrix fprintf returns 0 on success, so compute the result
1277 we want here since we need it for the following test. The +1
1278 is for the separator character that will probably be emitted. */
1279 int len = strlen (text) + 1;
1281 if (pos != 0
1282 && pos + len > MAX_LINE)
1284 print_fn (SWITCH_TYPE_LINE_END, NULL);
1285 pos = 0;
1288 if (pos == 0)
1289 pos += print_fn (SWITCH_TYPE_LINE_START, NULL);
1291 print_fn (type, text);
1292 return pos + len;
1295 /* Print active target switches using PRINT_FN.
1296 POS is the current cursor position and MAX is the size of a "line".
1297 Each line begins with INDENT and ends with TERM.
1298 Each switch is separated from the next by SEP. */
1300 static void
1301 print_switch_values (print_switch_fn_type print_fn)
1303 int pos = 0;
1304 size_t j;
1305 const char **p;
1307 /* Fill in the -frandom-seed option, if the user didn't pass it, so
1308 that it can be printed below. This helps reproducibility. */
1309 if (!flag_random_seed)
1310 init_random_seed ();
1312 /* Print the options as passed. */
1313 pos = print_single_switch (print_fn, pos,
1314 SWITCH_TYPE_DESCRIPTIVE, _("options passed: "));
1316 for (p = &save_argv[1]; *p != NULL; p++)
1318 if (**p == '-')
1320 /* Ignore these. */
1321 if (strcmp (*p, "-o") == 0
1322 || strcmp (*p, "-dumpbase") == 0
1323 || strcmp (*p, "-auxbase") == 0)
1325 if (p[1] != NULL)
1326 p++;
1327 continue;
1330 if (strcmp (*p, "-quiet") == 0
1331 || strcmp (*p, "-version") == 0)
1332 continue;
1334 if ((*p)[1] == 'd')
1335 continue;
1338 pos = print_single_switch (print_fn, pos, SWITCH_TYPE_PASSED, *p);
1341 if (pos > 0)
1342 print_fn (SWITCH_TYPE_LINE_END, NULL);
1344 /* Print the -f and -m options that have been enabled.
1345 We don't handle language specific options but printing argv
1346 should suffice. */
1347 pos = print_single_switch (print_fn, 0,
1348 SWITCH_TYPE_DESCRIPTIVE, _("options enabled: "));
1350 for (j = 0; j < cl_options_count; j++)
1351 if ((cl_options[j].flags & CL_REPORT)
1352 && option_enabled (j) > 0)
1353 pos = print_single_switch (print_fn, pos,
1354 SWITCH_TYPE_ENABLED, cl_options[j].opt_text);
1356 print_fn (SWITCH_TYPE_LINE_END, NULL);
1359 /* Open assembly code output file. Do this even if -fsyntax-only is
1360 on, because then the driver will have provided the name of a
1361 temporary file or bit bucket for us. NAME is the file specified on
1362 the command line, possibly NULL. */
1363 static void
1364 init_asm_output (const char *name)
1366 if (name == NULL && asm_file_name == 0)
1367 asm_out_file = stdout;
1368 else
1370 if (asm_file_name == 0)
1372 int len = strlen (dump_base_name);
1373 char *dumpname = XNEWVEC (char, len + 6);
1375 memcpy (dumpname, dump_base_name, len + 1);
1376 strip_off_ending (dumpname, len);
1377 strcat (dumpname, ".s");
1378 asm_file_name = dumpname;
1380 if (!strcmp (asm_file_name, "-"))
1381 asm_out_file = stdout;
1382 else
1383 asm_out_file = fopen (asm_file_name, "w+b");
1384 if (asm_out_file == 0)
1385 fatal_error ("can%'t open %s for writing: %m", asm_file_name);
1388 if (!flag_syntax_only)
1390 targetm.asm_out.file_start ();
1392 if (flag_record_gcc_switches)
1394 if (targetm.asm_out.record_gcc_switches)
1396 /* Let the target know that we are about to start recording. */
1397 targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1398 NULL);
1399 /* Now record the switches. */
1400 print_switch_values (targetm.asm_out.record_gcc_switches);
1401 /* Let the target know that the recording is over. */
1402 targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1403 NULL);
1405 else
1406 inform ("-frecord-gcc-switches is not supported by the current target");
1409 #ifdef ASM_COMMENT_START
1410 if (flag_verbose_asm)
1412 /* Print the list of switches in effect
1413 into the assembler file as comments. */
1414 print_version (asm_out_file, ASM_COMMENT_START);
1415 print_switch_values (print_to_asm_out_file);
1416 fprintf (asm_out_file, "\n");
1418 #endif
1422 /* Return true if the state of option OPTION should be stored in PCH files
1423 and checked by default_pch_valid_p. Store the option's current state
1424 in STATE if so. */
1426 static inline bool
1427 option_affects_pch_p (int option, struct cl_option_state *state)
1429 if ((cl_options[option].flags & CL_TARGET) == 0)
1430 return false;
1431 if (cl_options[option].flag_var == &target_flags)
1432 if (targetm.check_pch_target_flags)
1433 return false;
1434 return get_option_state (option, state);
1437 /* Default version of get_pch_validity.
1438 By default, every flag difference is fatal; that will be mostly right for
1439 most targets, but completely right for very few. */
1441 void *
1442 default_get_pch_validity (size_t *len)
1444 struct cl_option_state state;
1445 size_t i;
1446 char *result, *r;
1448 *len = 2;
1449 if (targetm.check_pch_target_flags)
1450 *len += sizeof (target_flags);
1451 for (i = 0; i < cl_options_count; i++)
1452 if (option_affects_pch_p (i, &state))
1453 *len += state.size;
1455 result = r = XNEWVEC (char, *len);
1456 r[0] = flag_pic;
1457 r[1] = flag_pie;
1458 r += 2;
1459 if (targetm.check_pch_target_flags)
1461 memcpy (r, &target_flags, sizeof (target_flags));
1462 r += sizeof (target_flags);
1465 for (i = 0; i < cl_options_count; i++)
1466 if (option_affects_pch_p (i, &state))
1468 memcpy (r, state.data, state.size);
1469 r += state.size;
1472 return result;
1475 /* Return a message which says that a PCH file was created with a different
1476 setting of OPTION. */
1478 static const char *
1479 pch_option_mismatch (const char *option)
1481 char *r;
1483 asprintf (&r, _("created and used with differing settings of '%s'"), option);
1484 if (r == NULL)
1485 return _("out of memory");
1486 return r;
1489 /* Default version of pch_valid_p. */
1491 const char *
1492 default_pch_valid_p (const void *data_p, size_t len)
1494 struct cl_option_state state;
1495 const char *data = (const char *)data_p;
1496 size_t i;
1498 /* -fpic and -fpie also usually make a PCH invalid. */
1499 if (data[0] != flag_pic)
1500 return _("created and used with different settings of -fpic");
1501 if (data[1] != flag_pie)
1502 return _("created and used with different settings of -fpie");
1503 data += 2;
1505 /* Check target_flags. */
1506 if (targetm.check_pch_target_flags)
1508 int tf;
1509 const char *r;
1511 memcpy (&tf, data, sizeof (target_flags));
1512 data += sizeof (target_flags);
1513 len -= sizeof (target_flags);
1514 r = targetm.check_pch_target_flags (tf);
1515 if (r != NULL)
1516 return r;
1519 for (i = 0; i < cl_options_count; i++)
1520 if (option_affects_pch_p (i, &state))
1522 if (memcmp (data, state.data, state.size) != 0)
1523 return pch_option_mismatch (cl_options[i].opt_text);
1524 data += state.size;
1525 len -= state.size;
1528 return NULL;
1531 /* Default tree printer. Handles declarations only. */
1532 static bool
1533 default_tree_printer (pretty_printer * pp, text_info *text, const char *spec,
1534 int precision, bool wide, bool set_locus, bool hash)
1536 tree t;
1538 /* FUTURE: %+x should set the locus. */
1539 if (precision != 0 || wide || hash)
1540 return false;
1542 switch (*spec)
1544 case 'D':
1545 t = va_arg (*text->args_ptr, tree);
1546 if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
1547 t = DECL_DEBUG_EXPR (t);
1548 break;
1550 case 'F':
1551 case 'T':
1552 t = va_arg (*text->args_ptr, tree);
1553 break;
1555 default:
1556 return false;
1559 if (set_locus && text->locus)
1560 *text->locus = DECL_SOURCE_LOCATION (t);
1562 if (DECL_P (t))
1564 const char *n = DECL_NAME (t)
1565 ? lang_hooks.decl_printable_name (t, 2)
1566 : "<anonymous>";
1567 pp_string (pp, n);
1569 else
1570 dump_generic_node (pp, t, 0, 0, 0);
1572 return true;
1575 /* Initialization of the front end environment, before command line
1576 options are parsed. Signal handlers, internationalization etc.
1577 ARGV0 is main's argv[0]. */
1578 static void
1579 general_init (const char *argv0)
1581 const char *p;
1583 p = argv0 + strlen (argv0);
1584 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
1585 --p;
1586 progname = p;
1588 xmalloc_set_program_name (progname);
1590 hex_init ();
1592 /* Unlock the stdio streams. */
1593 unlock_std_streams ();
1595 gcc_init_libintl ();
1597 /* Initialize the diagnostics reporting machinery, so option parsing
1598 can give warnings and errors. */
1599 diagnostic_initialize (global_dc);
1600 /* Set a default printer. Language specific initializations will
1601 override it later. */
1602 pp_format_decoder (global_dc->printer) = &default_tree_printer;
1604 /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages. */
1605 #ifdef SIGSEGV
1606 signal (SIGSEGV, crash_signal);
1607 #endif
1608 #ifdef SIGILL
1609 signal (SIGILL, crash_signal);
1610 #endif
1611 #ifdef SIGBUS
1612 signal (SIGBUS, crash_signal);
1613 #endif
1614 #ifdef SIGABRT
1615 signal (SIGABRT, crash_signal);
1616 #endif
1617 #if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
1618 signal (SIGIOT, crash_signal);
1619 #endif
1620 #ifdef SIGFPE
1621 signal (SIGFPE, crash_signal);
1622 #endif
1624 /* Other host-specific signal setup. */
1625 (*host_hooks.extra_signals)();
1627 /* Initialize the garbage-collector, string pools and tree type hash
1628 table. */
1629 init_ggc ();
1630 init_stringpool ();
1631 linemap_init (&line_table);
1632 init_ttree ();
1634 /* Initialize register usage now so switches may override. */
1635 init_reg_sets ();
1637 /* Register the language-independent parameters. */
1638 add_params (lang_independent_params, LAST_PARAM);
1640 /* This must be done after add_params but before argument processing. */
1641 init_ggc_heuristics();
1642 init_optimization_passes ();
1645 /* Return true if the current target supports -fsection-anchors. */
1647 static bool
1648 target_supports_section_anchors_p (void)
1650 if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1651 return false;
1653 if (targetm.asm_out.output_anchor == NULL)
1654 return false;
1656 return true;
1659 /* Process the options that have been parsed. */
1660 static void
1661 process_options (void)
1663 /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1664 This can happen with incorrect pre-processed input. */
1665 debug_hooks = &do_nothing_debug_hooks;
1667 /* Allow the front end to perform consistency checks and do further
1668 initialization based on the command line options. This hook also
1669 sets the original filename if appropriate (e.g. foo.i -> foo.c)
1670 so we can correctly initialize debug output. */
1671 no_backend = lang_hooks.post_options (&main_input_filename);
1672 #ifndef USE_MAPPED_LOCATION
1673 input_filename = main_input_filename;
1674 #endif
1676 #ifdef OVERRIDE_OPTIONS
1677 /* Some machines may reject certain combinations of options. */
1678 OVERRIDE_OPTIONS;
1679 #endif
1681 if (flag_section_anchors && !target_supports_section_anchors_p ())
1683 warning (OPT_fsection_anchors,
1684 "this target does not support %qs", "-fsection-anchors");
1685 flag_section_anchors = 0;
1688 if (flag_short_enums == 2)
1689 flag_short_enums = targetm.default_short_enums ();
1691 /* Set aux_base_name if not already set. */
1692 if (aux_base_name)
1694 else if (main_input_filename)
1696 char *name = xstrdup (lbasename (main_input_filename));
1698 strip_off_ending (name, strlen (name));
1699 aux_base_name = name;
1701 else
1702 aux_base_name = "gccaux";
1704 /* Set up the align_*_log variables, defaulting them to 1 if they
1705 were still unset. */
1706 if (align_loops <= 0) align_loops = 1;
1707 if (align_loops_max_skip > align_loops || !align_loops)
1708 align_loops_max_skip = align_loops - 1;
1709 align_loops_log = floor_log2 (align_loops * 2 - 1);
1710 if (align_jumps <= 0) align_jumps = 1;
1711 if (align_jumps_max_skip > align_jumps || !align_jumps)
1712 align_jumps_max_skip = align_jumps - 1;
1713 align_jumps_log = floor_log2 (align_jumps * 2 - 1);
1714 if (align_labels <= 0) align_labels = 1;
1715 align_labels_log = floor_log2 (align_labels * 2 - 1);
1716 if (align_labels_max_skip > align_labels || !align_labels)
1717 align_labels_max_skip = align_labels - 1;
1718 if (align_functions <= 0) align_functions = 1;
1719 align_functions_log = floor_log2 (align_functions * 2 - 1);
1721 /* Unrolling all loops implies that standard loop unrolling must also
1722 be done. */
1723 if (flag_unroll_all_loops)
1724 flag_unroll_loops = 1;
1726 /* The loop unrolling code assumes that cse will be run after loop.
1727 web and rename-registers also help when run after loop unrolling. */
1729 if (flag_rerun_cse_after_loop == AUTODETECT_VALUE)
1730 flag_rerun_cse_after_loop = flag_unroll_loops || flag_peel_loops;
1731 if (flag_web == AUTODETECT_VALUE)
1732 flag_web = flag_unroll_loops || flag_peel_loops;
1733 if (flag_rename_registers == AUTODETECT_VALUE)
1734 flag_rename_registers = flag_unroll_loops || flag_peel_loops;
1736 if (flag_non_call_exceptions)
1737 flag_asynchronous_unwind_tables = 1;
1738 if (flag_asynchronous_unwind_tables)
1739 flag_unwind_tables = 1;
1741 /* Disable unit-at-a-time mode for frontends not supporting callgraph
1742 interface. */
1743 if (flag_unit_at_a_time && ! lang_hooks.callgraph.expand_function)
1744 flag_unit_at_a_time = 0;
1746 if (!flag_unit_at_a_time)
1747 flag_section_anchors = 0;
1749 if (flag_value_profile_transformations)
1750 flag_profile_values = 1;
1752 /* Warn about options that are not supported on this machine. */
1753 #ifndef INSN_SCHEDULING
1754 if (flag_schedule_insns || flag_schedule_insns_after_reload)
1755 warning (0, "instruction scheduling not supported on this target machine");
1756 #endif
1757 #ifndef DELAY_SLOTS
1758 if (flag_delayed_branch)
1759 warning (0, "this target machine does not have delayed branches");
1760 #endif
1762 user_label_prefix = USER_LABEL_PREFIX;
1763 if (flag_leading_underscore != -1)
1765 /* If the default prefix is more complicated than "" or "_",
1766 issue a warning and ignore this option. */
1767 if (user_label_prefix[0] == 0 ||
1768 (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
1770 user_label_prefix = flag_leading_underscore ? "_" : "";
1772 else
1773 warning (0, "-f%sleading-underscore not supported on this target machine",
1774 flag_leading_underscore ? "" : "no-");
1777 /* If we are in verbose mode, write out the version and maybe all the
1778 option flags in use. */
1779 if (version_flag)
1781 print_version (stderr, "");
1782 if (! quiet_flag)
1783 print_switch_values (print_to_stderr);
1786 if (flag_syntax_only)
1788 write_symbols = NO_DEBUG;
1789 profile_flag = 0;
1792 /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1793 level is 0. */
1794 if (debug_info_level == DINFO_LEVEL_NONE)
1795 write_symbols = NO_DEBUG;
1797 /* Now we know write_symbols, set up the debug hooks based on it.
1798 By default we do nothing for debug output. */
1799 if (PREFERRED_DEBUGGING_TYPE == NO_DEBUG)
1800 default_debug_hooks = &do_nothing_debug_hooks;
1801 #if defined(DBX_DEBUGGING_INFO)
1802 else if (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG)
1803 default_debug_hooks = &dbx_debug_hooks;
1804 #endif
1805 #if defined(XCOFF_DEBUGGING_INFO)
1806 else if (PREFERRED_DEBUGGING_TYPE == XCOFF_DEBUG)
1807 default_debug_hooks = &xcoff_debug_hooks;
1808 #endif
1809 #ifdef SDB_DEBUGGING_INFO
1810 else if (PREFERRED_DEBUGGING_TYPE == SDB_DEBUG)
1811 default_debug_hooks = &sdb_debug_hooks;
1812 #endif
1813 #ifdef DWARF2_DEBUGGING_INFO
1814 else if (PREFERRED_DEBUGGING_TYPE == DWARF2_DEBUG)
1815 default_debug_hooks = &dwarf2_debug_hooks;
1816 #endif
1817 #ifdef VMS_DEBUGGING_INFO
1818 else if (PREFERRED_DEBUGGING_TYPE == VMS_DEBUG
1819 || PREFERRED_DEBUGGING_TYPE == VMS_AND_DWARF2_DEBUG)
1820 default_debug_hooks = &vmsdbg_debug_hooks;
1821 #endif
1823 if (write_symbols == NO_DEBUG)
1825 #if defined(DBX_DEBUGGING_INFO)
1826 else if (write_symbols == DBX_DEBUG)
1827 debug_hooks = &dbx_debug_hooks;
1828 #endif
1829 #if defined(XCOFF_DEBUGGING_INFO)
1830 else if (write_symbols == XCOFF_DEBUG)
1831 debug_hooks = &xcoff_debug_hooks;
1832 #endif
1833 #ifdef SDB_DEBUGGING_INFO
1834 else if (write_symbols == SDB_DEBUG)
1835 debug_hooks = &sdb_debug_hooks;
1836 #endif
1837 #ifdef DWARF2_DEBUGGING_INFO
1838 else if (write_symbols == DWARF2_DEBUG)
1839 debug_hooks = &dwarf2_debug_hooks;
1840 #endif
1841 #ifdef VMS_DEBUGGING_INFO
1842 else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
1843 debug_hooks = &vmsdbg_debug_hooks;
1844 #endif
1845 else
1846 error ("target system does not support the \"%s\" debug format",
1847 debug_type_names[write_symbols]);
1849 /* Now we know which debug output will be used so we can set
1850 flag_var_tracking, flag_rename_registers if the user has
1851 not specified them. */
1852 if (debug_info_level < DINFO_LEVEL_NORMAL
1853 || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1855 if (flag_var_tracking == 1)
1857 if (debug_info_level < DINFO_LEVEL_NORMAL)
1858 warning (0, "variable tracking requested, but useless unless "
1859 "producing debug info");
1860 else
1861 warning (0, "variable tracking requested, but not supported "
1862 "by this debug format");
1864 flag_var_tracking = 0;
1867 if (flag_rename_registers == AUTODETECT_VALUE)
1868 flag_rename_registers = default_debug_hooks->var_location
1869 != do_nothing_debug_hooks.var_location;
1871 if (flag_var_tracking == AUTODETECT_VALUE)
1872 flag_var_tracking = optimize >= 1;
1874 /* If auxiliary info generation is desired, open the output file.
1875 This goes in the same directory as the source file--unlike
1876 all the other output files. */
1877 if (flag_gen_aux_info)
1879 aux_info_file = fopen (aux_info_file_name, "w");
1880 if (aux_info_file == 0)
1881 fatal_error ("can%'t open %s: %m", aux_info_file_name);
1884 if (! targetm.have_named_sections)
1886 if (flag_function_sections)
1888 warning (0, "-ffunction-sections not supported for this target");
1889 flag_function_sections = 0;
1891 if (flag_data_sections)
1893 warning (0, "-fdata-sections not supported for this target");
1894 flag_data_sections = 0;
1898 if (flag_function_sections && profile_flag)
1900 warning (0, "-ffunction-sections disabled; it makes profiling impossible");
1901 flag_function_sections = 0;
1904 #ifndef HAVE_prefetch
1905 if (flag_prefetch_loop_arrays)
1907 warning (0, "-fprefetch-loop-arrays not supported for this target");
1908 flag_prefetch_loop_arrays = 0;
1910 #else
1911 if (flag_prefetch_loop_arrays && !HAVE_prefetch)
1913 warning (0, "-fprefetch-loop-arrays not supported for this target (try -march switches)");
1914 flag_prefetch_loop_arrays = 0;
1916 #endif
1918 /* This combination of options isn't handled for i386 targets and doesn't
1919 make much sense anyway, so don't allow it. */
1920 if (flag_prefetch_loop_arrays && optimize_size)
1922 warning (0, "-fprefetch-loop-arrays is not supported with -Os");
1923 flag_prefetch_loop_arrays = 0;
1926 #ifndef OBJECT_FORMAT_ELF
1927 #ifndef OBJECT_FORMAT_MACHO
1928 if (flag_function_sections && write_symbols != NO_DEBUG)
1929 warning (0, "-ffunction-sections may affect debugging on some targets");
1930 #endif
1931 #endif
1933 /* The presence of IEEE signaling NaNs, implies all math can trap. */
1934 if (flag_signaling_nans)
1935 flag_trapping_math = 1;
1937 /* With -fcx-limited-range, we do cheap and quick complex arithmetic. */
1938 if (flag_cx_limited_range)
1939 flag_complex_method = 0;
1941 /* Targets must be able to place spill slots at lower addresses. If the
1942 target already uses a soft frame pointer, the transition is trivial. */
1943 if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
1945 warning (0, "-fstack-protector not supported for this target");
1946 flag_stack_protect = 0;
1948 if (!flag_stack_protect)
1949 warn_stack_protect = 0;
1951 /* ??? Unwind info is not correct around the CFG unless either a frame
1952 pointer is present or A_O_A is set. Fixing this requires rewriting
1953 unwind info generation to be aware of the CFG and propagating states
1954 around edges. */
1955 if (flag_unwind_tables && !ACCUMULATE_OUTGOING_ARGS
1956 && flag_omit_frame_pointer)
1958 warning (0, "unwind tables currently requires a frame pointer "
1959 "for correctness");
1960 flag_omit_frame_pointer = 0;
1964 /* Initialize the compiler back end. */
1965 static void
1966 backend_init (void)
1968 init_emit_once (debug_info_level == DINFO_LEVEL_NORMAL
1969 || debug_info_level == DINFO_LEVEL_VERBOSE
1970 #ifdef VMS_DEBUGGING_INFO
1971 /* Enable line number info for traceback. */
1972 || debug_info_level > DINFO_LEVEL_NONE
1973 #endif
1974 || flag_test_coverage);
1976 init_rtlanal ();
1977 init_regs ();
1978 init_fake_stack_mems ();
1979 init_alias_once ();
1980 init_inline_once ();
1981 init_reload ();
1982 init_varasm_once ();
1984 /* The following initialization functions need to generate rtl, so
1985 provide a dummy function context for them. */
1986 init_dummy_function_start ();
1987 init_expmed ();
1988 if (flag_caller_saves)
1989 init_caller_save ();
1990 expand_dummy_function_end ();
1993 /* Language-dependent initialization. Returns nonzero on success. */
1994 static int
1995 lang_dependent_init (const char *name)
1997 location_t save_loc = input_location;
1998 if (dump_base_name == 0)
1999 dump_base_name = name && name[0] ? name : "gccdump";
2001 /* Other front-end initialization. */
2002 #ifdef USE_MAPPED_LOCATION
2003 input_location = BUILTINS_LOCATION;
2004 #else
2005 input_filename = "<built-in>";
2006 input_line = 0;
2007 #endif
2008 if (lang_hooks.init () == 0)
2009 return 0;
2010 input_location = save_loc;
2012 init_asm_output (name);
2014 /* These create various _DECL nodes, so need to be called after the
2015 front end is initialized. */
2016 init_eh ();
2017 init_optabs ();
2019 /* The following initialization functions need to generate rtl, so
2020 provide a dummy function context for them. */
2021 init_dummy_function_start ();
2022 init_expr_once ();
2024 /* Although the actions of init_set_costs are language-independent,
2025 it uses optabs, so we cannot call it from backend_init. */
2026 init_set_costs ();
2028 expand_dummy_function_end ();
2030 /* If dbx symbol table desired, initialize writing it and output the
2031 predefined types. */
2032 timevar_push (TV_SYMOUT);
2034 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
2035 if (dwarf2out_do_frame ())
2036 dwarf2out_frame_init ();
2037 #endif
2039 /* Now we have the correct original filename, we can initialize
2040 debug output. */
2041 (*debug_hooks->init) (name);
2043 timevar_pop (TV_SYMOUT);
2045 return 1;
2048 void
2049 dump_memory_report (bool final)
2051 ggc_print_statistics ();
2052 stringpool_statistics ();
2053 dump_tree_statistics ();
2054 dump_rtx_statistics ();
2055 dump_varray_statistics ();
2056 dump_alloc_pool_statistics ();
2057 dump_bitmap_statistics ();
2058 dump_ggc_loc_statistics (final);
2061 /* Clean up: close opened files, etc. */
2063 static void
2064 finalize (void)
2066 /* Close the dump files. */
2067 if (flag_gen_aux_info)
2069 fclose (aux_info_file);
2070 if (errorcount)
2071 unlink (aux_info_file_name);
2074 /* Close non-debugging input and output files. Take special care to note
2075 whether fclose returns an error, since the pages might still be on the
2076 buffer chain while the file is open. */
2078 if (asm_out_file)
2080 if (ferror (asm_out_file) != 0)
2081 fatal_error ("error writing to %s: %m", asm_file_name);
2082 if (fclose (asm_out_file) != 0)
2083 fatal_error ("error closing %s: %m", asm_file_name);
2086 finish_optimization_passes ();
2088 if (mem_report)
2089 dump_memory_report (true);
2091 /* Free up memory for the benefit of leak detectors. */
2092 free_reg_info ();
2094 /* Language-specific end of compilation actions. */
2095 lang_hooks.finish ();
2098 /* Initialize the compiler, and compile the input file. */
2099 static void
2100 do_compile (void)
2102 /* Initialize timing first. The C front ends read the main file in
2103 the post_options hook, and C++ does file timings. */
2104 if (time_report || !quiet_flag || flag_detailed_statistics)
2105 timevar_init ();
2106 timevar_start (TV_TOTAL);
2108 process_options ();
2110 /* Don't do any more if an error has already occurred. */
2111 if (!errorcount)
2113 /* This must be run always, because it is needed to compute the FP
2114 predefined macros, such as __LDBL_MAX__, for targets using non
2115 default FP formats. */
2116 init_adjust_machine_modes ();
2118 /* Set up the back-end if requested. */
2119 if (!no_backend)
2120 backend_init ();
2122 /* Language-dependent initialization. Returns true on success. */
2123 if (lang_dependent_init (main_input_filename))
2124 compile_file ();
2126 finalize ();
2129 /* Stop timing and print the times. */
2130 timevar_stop (TV_TOTAL);
2131 timevar_print (stderr);
2134 /* Entry point of cc1, cc1plus, jc1, f771, etc.
2135 Exit code is FATAL_EXIT_CODE if can't open files or if there were
2136 any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
2138 It is not safe to call this function more than once. */
2141 toplev_main (unsigned int argc, const char **argv)
2143 save_argv = argv;
2145 /* Initialization of GCC's environment, and diagnostics. */
2146 general_init (argv[0]);
2148 /* Parse the options and do minimal processing; basically just
2149 enough to default flags appropriately. */
2150 decode_options (argc, argv);
2152 init_local_tick ();
2154 /* Exit early if we can (e.g. -help). */
2155 if (!exit_after_options)
2156 do_compile ();
2158 if (errorcount || sorrycount)
2159 return (FATAL_EXIT_CODE);
2161 return (SUCCESS_EXIT_CODE);