[multiple changes]
[official-gcc.git] / gcc / toplev.c
blob675cdf249cfb61f19b09492670a572d812e9e5c2
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, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This is the top level of cc1/c++.
23 It parses command args, opens files, invokes the various passes
24 in the proper order, and counts the time used by each.
25 Error messages and low-level interface to malloc also handled here. */
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
41 #endif
43 #include "line-map.h"
44 #include "input.h"
45 #include "tree.h"
46 #include "version.h"
47 #include "rtl.h"
48 #include "tm_p.h"
49 #include "flags.h"
50 #include "insn-attr.h"
51 #include "insn-config.h"
52 #include "insn-flags.h"
53 #include "hard-reg-set.h"
54 #include "recog.h"
55 #include "output.h"
56 #include "except.h"
57 #include "function.h"
58 #include "toplev.h"
59 #include "expr.h"
60 #include "basic-block.h"
61 #include "intl.h"
62 #include "ggc.h"
63 #include "graph.h"
64 #include "regs.h"
65 #include "timevar.h"
66 #include "diagnostic.h"
67 #include "params.h"
68 #include "reload.h"
69 #include "ira.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"
85 #include "tree-pass.h"
86 #include "gimple.h"
87 #include "tree-ssa-alias.h"
88 #include "plugin.h"
90 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
91 #include "dwarf2out.h"
92 #endif
94 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
95 #include "dbxout.h"
96 #endif
98 #ifdef SDB_DEBUGGING_INFO
99 #include "sdbout.h"
100 #endif
102 #ifdef XCOFF_DEBUGGING_INFO
103 #include "xcoffout.h" /* Needed for external data
104 declarations for e.g. AIX 4.x. */
105 #endif
107 static void general_init (const char *);
108 static void do_compile (void);
109 static void process_options (void);
110 static void backend_init (void);
111 static int lang_dependent_init (const char *);
112 static void init_asm_output (const char *);
113 static void finalize (void);
115 static void crash_signal (int) ATTRIBUTE_NORETURN;
116 static void setup_core_dumping (void);
117 static void compile_file (void);
119 /* Nonzero to dump debug info whilst parsing (-dy option). */
120 static int set_yydebug;
122 /* True if we don't need a backend (e.g. preprocessing only). */
123 static bool no_backend;
125 /* Length of line when printing switch values. */
126 #define MAX_LINE 75
128 /* Name of program invoked, sans directories. */
130 const char *progname;
132 /* Copy of argument vector to toplev_main. */
133 static const char **save_argv;
135 /* Name of top-level original source file (what was input to cpp).
136 This comes from the #-command at the beginning of the actual input.
137 If there isn't any there, then this is the cc1 input file name. */
139 const char *main_input_filename;
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 /* Name to use as base of names for dump output files. */
153 const char *dump_base_name;
155 /* Name to use as a base for auxiliary output files. */
157 const char *aux_base_name;
159 /* Prefix for profile data files */
160 const char *profile_data_prefix;
162 /* A mask of target_flags that includes bit X if X was set or cleared
163 on the command line. */
165 int target_flags_explicit;
167 /* Debug hooks - dependent upon command line options. */
169 const struct gcc_debug_hooks *debug_hooks;
171 /* Debug hooks - target default. */
173 static const struct gcc_debug_hooks *default_debug_hooks;
175 /* Other flags saying which kinds of debugging dump have been requested. */
177 int rtl_dump_and_exit;
178 int flag_print_asm_name;
179 enum graph_dump_types graph_dump_format;
181 /* Name for output file of assembly code, specified with -o. */
183 const char *asm_file_name;
185 /* Nonzero means do optimizations. -O.
186 Particular numeric values stand for particular amounts of optimization;
187 thus, -O2 stores 2 here. However, the optimizations beyond the basic
188 ones are not controlled directly by this variable. Instead, they are
189 controlled by individual `flag_...' variables that are defaulted
190 based on this variable. */
192 int optimize = 0;
194 /* Nonzero means optimize for size. -Os.
195 The only valid values are zero and nonzero. When optimize_size is
196 nonzero, optimize defaults to 2, but certain individual code
197 bloating optimizations are disabled. */
199 int optimize_size = 0;
201 /* The FUNCTION_DECL for the function currently being compiled,
202 or 0 if between functions. */
203 tree current_function_decl;
205 /* Set to the FUNC_BEGIN label of the current function, or NULL
206 if none. */
207 const char * current_function_func_begin_label;
209 /* Nonzero means to collect statistics which might be expensive
210 and to print them when we are done. */
211 int flag_detailed_statistics = 0;
213 /* A random sequence of characters, unless overridden by user. */
214 static const char *flag_random_seed;
216 /* A local time stamp derived from the time of compilation. It will be
217 zero if the system cannot provide a time. It will be -1u, if the
218 user has specified a particular random seed. */
219 unsigned local_tick;
221 /* -f flags. */
223 /* Nonzero means `char' should be signed. */
225 int flag_signed_char;
227 /* Nonzero means give an enum type only as many bytes as it needs. A value
228 of 2 means it has not yet been initialized. */
230 int flag_short_enums;
232 /* Nonzero if structures and unions should be returned in memory.
234 This should only be defined if compatibility with another compiler or
235 with an ABI is needed, because it results in slower code. */
237 #ifndef DEFAULT_PCC_STRUCT_RETURN
238 #define DEFAULT_PCC_STRUCT_RETURN 1
239 #endif
241 /* Nonzero for -fpcc-struct-return: return values the same way PCC does. */
243 int flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
245 /* 0 means straightforward implementation of complex divide acceptable.
246 1 means wide ranges of inputs must work for complex divide.
247 2 means C99-like requirements for complex multiply and divide. */
249 int flag_complex_method = 1;
251 /* Nonzero means we should be saving declaration info into a .X file. */
253 int flag_gen_aux_info = 0;
255 /* Specified name of aux-info file. */
257 const char *aux_info_file_name;
259 /* Nonzero if we are compiling code for a shared library, zero for
260 executable. */
262 int flag_shlib;
264 /* Generate code for GNU or NeXT Objective-C runtime environment. */
266 #ifdef NEXT_OBJC_RUNTIME
267 int flag_next_runtime = 1;
268 #else
269 int flag_next_runtime = 0;
270 #endif
272 /* Set to the default thread-local storage (tls) model to use. */
274 enum tls_model flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
276 /* Set the default region and algorithm for the integrated register
277 allocator. */
279 enum ira_algorithm flag_ira_algorithm = IRA_ALGORITHM_CB;
280 enum ira_region flag_ira_region = IRA_REGION_MIXED;
282 /* Set the default value for -fira-verbose. */
284 unsigned int flag_ira_verbose = 5;
286 /* Set the default for excess precision. */
288 enum excess_precision flag_excess_precision_cmdline = EXCESS_PRECISION_DEFAULT;
289 enum excess_precision flag_excess_precision = EXCESS_PRECISION_DEFAULT;
291 /* Nonzero means change certain warnings into errors.
292 Usually these are warnings about failure to conform to some standard. */
294 int flag_pedantic_errors = 0;
296 /* Nonzero means make permerror produce warnings instead of errors. */
298 int flag_permissive = 0;
300 /* -dA causes debug commentary information to be produced in
301 the generated assembly code (to make it more readable). This option
302 is generally only of use to those who actually need to read the
303 generated assembly code (perhaps while debugging the compiler itself).
304 Currently, this switch is only used by dwarfout.c; however, it is intended
305 to be a catchall for printing debug information in the assembler file. */
307 int flag_debug_asm = 0;
309 /* -dP causes the rtl to be emitted as a comment in assembly. */
311 int flag_dump_rtl_in_asm = 0;
313 /* When non-NULL, indicates that whenever space is allocated on the
314 stack, the resulting stack pointer must not pass this
315 address---that is, for stacks that grow downward, the stack pointer
316 must always be greater than or equal to this address; for stacks
317 that grow upward, the stack pointer must be less than this address.
318 At present, the rtx may be either a REG or a SYMBOL_REF, although
319 the support provided depends on the backend. */
320 rtx stack_limit_rtx;
322 /* Nonzero if we should track variables. When
323 flag_var_tracking == AUTODETECT_VALUE it will be set according
324 to optimize, debug_info_level and debug_hooks in process_options (). */
325 int flag_var_tracking = AUTODETECT_VALUE;
327 /* Type of stack check. */
328 enum stack_check_type flag_stack_check = NO_STACK_CHECK;
330 /* True if the user has tagged the function with the 'section'
331 attribute. */
333 bool user_defined_section_attribute = false;
335 /* Values of the -falign-* flags: how much to align labels in code.
336 0 means `use default', 1 means `don't align'.
337 For each variable, there is an _log variant which is the power
338 of two not less than the variable, for .align output. */
340 int align_loops_log;
341 int align_loops_max_skip;
342 int align_jumps_log;
343 int align_jumps_max_skip;
344 int align_labels_log;
345 int align_labels_max_skip;
346 int align_functions_log;
348 typedef struct
350 const char *const string;
351 int *const variable;
352 const int on_value;
354 lang_independent_options;
356 /* Nonzero if subexpressions must be evaluated from left-to-right. */
357 int flag_evaluation_order = 0;
359 /* The user symbol prefix after having resolved same. */
360 const char *user_label_prefix;
362 static const param_info lang_independent_params[] = {
363 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
364 { OPTION, DEFAULT, false, MIN, MAX, HELP },
365 #include "params.def"
366 #undef DEFPARAM
367 { NULL, 0, false, 0, 0, NULL }
370 /* Output files for assembler code (real compiler output)
371 and debugging dumps. */
373 FILE *asm_out_file;
374 FILE *aux_info_file;
375 FILE *dump_file = NULL;
376 const char *dump_file_name;
378 /* The current working directory of a translation. It's generally the
379 directory from which compilation was initiated, but a preprocessed
380 file may specify the original directory in which it was
381 created. */
383 static const char *src_pwd;
385 /* Initialize src_pwd with the given string, and return true. If it
386 was already initialized, return false. As a special case, it may
387 be called with a NULL argument to test whether src_pwd has NOT been
388 initialized yet. */
390 bool
391 set_src_pwd (const char *pwd)
393 if (src_pwd)
395 if (strcmp (src_pwd, pwd) == 0)
396 return true;
397 else
398 return false;
401 src_pwd = xstrdup (pwd);
402 return true;
405 /* Return the directory from which the translation unit was initiated,
406 in case set_src_pwd() was not called before to assign it a
407 different value. */
409 const char *
410 get_src_pwd (void)
412 if (! src_pwd)
414 src_pwd = getpwd ();
415 if (!src_pwd)
416 src_pwd = ".";
419 return src_pwd;
422 /* Called when the start of a function definition is parsed,
423 this function prints on stderr the name of the function. */
424 void
425 announce_function (tree decl)
427 if (!quiet_flag)
429 if (rtl_dump_and_exit)
430 fprintf (stderr, "%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
431 else
432 fprintf (stderr, " %s", lang_hooks.decl_printable_name (decl, 2));
433 fflush (stderr);
434 pp_needs_newline (global_dc->printer) = true;
435 diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
439 /* Initialize local_tick with the time of day, or -1 if
440 flag_random_seed is set. */
442 static void
443 init_local_tick (void)
445 if (!flag_random_seed)
447 /* Get some more or less random data. */
448 #ifdef HAVE_GETTIMEOFDAY
450 struct timeval tv;
452 gettimeofday (&tv, NULL);
453 local_tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
455 #else
457 time_t now = time (NULL);
459 if (now != (time_t)-1)
460 local_tick = (unsigned) now;
462 #endif
464 else
465 local_tick = -1;
468 /* Set up a default flag_random_seed and local_tick, unless the user
469 already specified one. Must be called after init_local_tick. */
471 static void
472 init_random_seed (void)
474 unsigned HOST_WIDE_INT value;
475 static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
477 value = local_tick ^ getpid ();
479 sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
480 flag_random_seed = random_seed;
483 /* Obtain the random_seed string. Unless NOINIT, initialize it if
484 it's not provided in the command line. */
486 const char *
487 get_random_seed (bool noinit)
489 if (!flag_random_seed && !noinit)
490 init_random_seed ();
491 return flag_random_seed;
494 /* Modify the random_seed string to VAL. Return its previous
495 value. */
497 const char *
498 set_random_seed (const char *val)
500 const char *old = flag_random_seed;
501 flag_random_seed = val;
502 return old;
505 /* Decode the string P as an integral parameter.
506 If the string is indeed an integer return its numeric value else
507 issue an Invalid Option error for the option PNAME and return DEFVAL.
508 If PNAME is zero just return DEFVAL, do not call error. */
511 read_integral_parameter (const char *p, const char *pname, const int defval)
513 const char *endp = p;
515 while (*endp)
517 if (ISDIGIT (*endp))
518 endp++;
519 else
520 break;
523 if (*endp != 0)
525 if (pname != 0)
526 error ("invalid option argument %qs", pname);
527 return defval;
530 return atoi (p);
533 /* When compiling with a recent enough GCC, we use the GNU C "extern inline"
534 for floor_log2 and exact_log2; see toplev.h. That construct, however,
535 conflicts with the ISO C++ One Definition Rule. */
537 #if GCC_VERSION < 3004 || !defined (__cplusplus)
539 /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
540 If X is 0, return -1. */
543 floor_log2 (unsigned HOST_WIDE_INT x)
545 int t = 0;
547 if (x == 0)
548 return -1;
550 #ifdef CLZ_HWI
551 t = HOST_BITS_PER_WIDE_INT - 1 - (int) CLZ_HWI (x);
552 #else
553 if (HOST_BITS_PER_WIDE_INT > 64)
554 if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
555 t += 64;
556 if (HOST_BITS_PER_WIDE_INT > 32)
557 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
558 t += 32;
559 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
560 t += 16;
561 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
562 t += 8;
563 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
564 t += 4;
565 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
566 t += 2;
567 if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
568 t += 1;
569 #endif
571 return t;
574 /* Return the logarithm of X, base 2, considering X unsigned,
575 if X is a power of 2. Otherwise, returns -1. */
578 exact_log2 (unsigned HOST_WIDE_INT x)
580 if (x != (x & -x))
581 return -1;
582 #ifdef CTZ_HWI
583 return x ? CTZ_HWI (x) : -1;
584 #else
585 return floor_log2 (x);
586 #endif
589 #endif /* GCC_VERSION < 3004 || !defined (__cplusplus) */
591 /* Handler for fatal signals, such as SIGSEGV. These are transformed
592 into ICE messages, which is much more user friendly. In case the
593 error printer crashes, reset the signal to prevent infinite recursion. */
595 static void
596 crash_signal (int signo)
598 signal (signo, SIG_DFL);
600 /* If we crashed while processing an ASM statement, then be a little more
601 graceful. It's most likely the user's fault. */
602 if (this_is_asm_operands)
604 output_operand_lossage ("unrecoverable error");
605 exit (FATAL_EXIT_CODE);
608 internal_error ("%s", strsignal (signo));
611 /* Arrange to dump core on error. (The regular error message is still
612 printed first, except in the case of abort().) */
614 static void
615 setup_core_dumping (void)
617 #ifdef SIGABRT
618 signal (SIGABRT, SIG_DFL);
619 #endif
620 #if defined(HAVE_SETRLIMIT)
622 struct rlimit rlim;
623 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
624 fatal_error ("getting core file size maximum limit: %m");
625 rlim.rlim_cur = rlim.rlim_max;
626 if (setrlimit (RLIMIT_CORE, &rlim) != 0)
627 fatal_error ("setting core file size limit to maximum: %m");
629 #endif
630 diagnostic_abort_on_error (global_dc);
634 /* Strip off a legitimate source ending from the input string NAME of
635 length LEN. Rather than having to know the names used by all of
636 our front ends, we strip off an ending of a period followed by
637 up to five characters. (Java uses ".class".) */
639 void
640 strip_off_ending (char *name, int len)
642 int i;
643 for (i = 2; i < 6 && len > i; i++)
645 if (name[len - i] == '.')
647 name[len - i] = '\0';
648 break;
653 /* Output a quoted string. */
655 void
656 output_quoted_string (FILE *asm_file, const char *string)
658 #ifdef OUTPUT_QUOTED_STRING
659 OUTPUT_QUOTED_STRING (asm_file, string);
660 #else
661 char c;
663 putc ('\"', asm_file);
664 while ((c = *string++) != 0)
666 if (ISPRINT (c))
668 if (c == '\"' || c == '\\')
669 putc ('\\', asm_file);
670 putc (c, asm_file);
672 else
673 fprintf (asm_file, "\\%03o", (unsigned char) c);
675 putc ('\"', asm_file);
676 #endif
679 /* Output a file name in the form wanted by System V. */
681 void
682 output_file_directive (FILE *asm_file, const char *input_name)
684 int len;
685 const char *na;
687 if (input_name == NULL)
688 input_name = "<stdin>";
689 else
690 input_name = remap_debug_filename (input_name);
692 len = strlen (input_name);
693 na = input_name + len;
695 /* NA gets INPUT_NAME sans directory names. */
696 while (na > input_name)
698 if (IS_DIR_SEPARATOR (na[-1]))
699 break;
700 na--;
703 #ifdef ASM_OUTPUT_SOURCE_FILENAME
704 ASM_OUTPUT_SOURCE_FILENAME (asm_file, na);
705 #else
706 fprintf (asm_file, "\t.file\t");
707 output_quoted_string (asm_file, na);
708 fputc ('\n', asm_file);
709 #endif
712 /* A subroutine of wrapup_global_declarations. We've come to the end of
713 the compilation unit. All deferred variables should be undeferred,
714 and all incomplete decls should be finalized. */
716 void
717 wrapup_global_declaration_1 (tree decl)
719 /* We're not deferring this any longer. Assignment is conditional to
720 avoid needlessly dirtying PCH pages. */
721 if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
722 && DECL_DEFER_OUTPUT (decl) != 0)
723 DECL_DEFER_OUTPUT (decl) = 0;
725 if (TREE_CODE (decl) == VAR_DECL && DECL_SIZE (decl) == 0)
726 lang_hooks.finish_incomplete_decl (decl);
729 /* A subroutine of wrapup_global_declarations. Decide whether or not DECL
730 needs to be output. Return true if it is output. */
732 bool
733 wrapup_global_declaration_2 (tree decl)
735 if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl))
736 return false;
738 /* Don't write out static consts, unless we still need them.
740 We also keep static consts if not optimizing (for debugging),
741 unless the user specified -fno-keep-static-consts.
742 ??? They might be better written into the debug information.
743 This is possible when using DWARF.
745 A language processor that wants static constants to be always
746 written out (even if it is not used) is responsible for
747 calling rest_of_decl_compilation itself. E.g. the C front-end
748 calls rest_of_decl_compilation from finish_decl.
749 One motivation for this is that is conventional in some
750 environments to write things like:
751 static const char rcsid[] = "... version string ...";
752 intending to force the string to be in the executable.
754 A language processor that would prefer to have unneeded
755 static constants "optimized away" would just defer writing
756 them out until here. E.g. C++ does this, because static
757 constants are often defined in header files.
759 ??? A tempting alternative (for both C and C++) would be
760 to force a constant to be written if and only if it is
761 defined in a main file, as opposed to an include file. */
763 if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
765 struct varpool_node *node;
766 bool needed = true;
767 node = varpool_node (decl);
769 if (node->finalized)
770 needed = false;
771 else if (node->alias)
772 needed = false;
773 else if (!cgraph_global_info_ready
774 && (TREE_USED (decl)
775 || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
776 /* needed */;
777 else if (node->needed)
778 /* needed */;
779 else if (DECL_COMDAT (decl))
780 needed = false;
781 else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
782 && (optimize || !flag_keep_static_consts
783 || DECL_ARTIFICIAL (decl)))
784 needed = false;
786 if (needed)
788 rest_of_decl_compilation (decl, 1, 1);
789 return true;
793 return false;
796 /* Do any final processing required for the declarations in VEC, of
797 which there are LEN. We write out inline functions and variables
798 that have been deferred until this point, but which are required.
799 Returns nonzero if anything was put out. */
801 bool
802 wrapup_global_declarations (tree *vec, int len)
804 bool reconsider, output_something = false;
805 int i;
807 for (i = 0; i < len; i++)
808 wrapup_global_declaration_1 (vec[i]);
810 /* Now emit any global variables or functions that we have been
811 putting off. We need to loop in case one of the things emitted
812 here references another one which comes earlier in the list. */
815 reconsider = false;
816 for (i = 0; i < len; i++)
817 reconsider |= wrapup_global_declaration_2 (vec[i]);
818 if (reconsider)
819 output_something = true;
821 while (reconsider);
823 return output_something;
826 /* A subroutine of check_global_declarations. Issue appropriate warnings
827 for the global declaration DECL. */
829 void
830 check_global_declaration_1 (tree decl)
832 /* Warn about any function declared static but not defined. We don't
833 warn about variables, because many programs have static variables
834 that exist only to get some text into the object file. */
835 if (TREE_CODE (decl) == FUNCTION_DECL
836 && DECL_INITIAL (decl) == 0
837 && DECL_EXTERNAL (decl)
838 && ! DECL_ARTIFICIAL (decl)
839 && ! TREE_NO_WARNING (decl)
840 && ! TREE_PUBLIC (decl)
841 && (warn_unused_function
842 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
844 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
845 pedwarn (input_location, 0, "%q+F used but never defined", decl);
846 else
847 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
848 /* This symbol is effectively an "extern" declaration now. */
849 TREE_PUBLIC (decl) = 1;
850 assemble_external (decl);
853 /* Warn about static fns or vars defined but not used. */
854 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
855 /* We don't warn about "static const" variables because the
856 "rcs_id" idiom uses that construction. */
857 || (warn_unused_variable
858 && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
859 && ! DECL_IN_SYSTEM_HEADER (decl)
860 && ! TREE_USED (decl)
861 /* The TREE_USED bit for file-scope decls is kept in the identifier,
862 to handle multiple external decls in different scopes. */
863 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
864 && ! DECL_EXTERNAL (decl)
865 && ! TREE_PUBLIC (decl)
866 /* A volatile variable might be used in some non-obvious way. */
867 && ! TREE_THIS_VOLATILE (decl)
868 /* Global register variables must be declared to reserve them. */
869 && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
870 /* Otherwise, ask the language. */
871 && lang_hooks.decls.warn_unused_global (decl))
872 warning ((TREE_CODE (decl) == FUNCTION_DECL)
873 ? OPT_Wunused_function
874 : OPT_Wunused_variable,
875 "%q+D defined but not used", decl);
878 /* Issue appropriate warnings for the global declarations in VEC (of
879 which there are LEN). */
881 void
882 check_global_declarations (tree *vec, int len)
884 int i;
886 for (i = 0; i < len; i++)
887 check_global_declaration_1 (vec[i]);
890 /* Emit debugging information for all global declarations in VEC. */
892 void
893 emit_debug_global_declarations (tree *vec, int len)
895 int i;
897 /* Avoid confusing the debug information machinery when there are errors. */
898 if (errorcount != 0 || sorrycount != 0)
899 return;
901 timevar_push (TV_SYMOUT);
902 for (i = 0; i < len; i++)
903 debug_hooks->global_decl (vec[i]);
904 timevar_pop (TV_SYMOUT);
907 /* Warn about a use of an identifier which was marked deprecated. */
908 void
909 warn_deprecated_use (tree node)
911 if (node == 0 || !warn_deprecated_decl)
912 return;
914 if (DECL_P (node))
916 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
917 warning (OPT_Wdeprecated_declarations,
918 "%qD is deprecated (declared at %s:%d)",
919 node, xloc.file, xloc.line);
921 else if (TYPE_P (node))
923 const char *what = NULL;
924 tree decl = TYPE_STUB_DECL (node);
926 if (TYPE_NAME (node))
928 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
929 what = IDENTIFIER_POINTER (TYPE_NAME (node));
930 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
931 && DECL_NAME (TYPE_NAME (node)))
932 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
935 if (decl)
937 expanded_location xloc
938 = expand_location (DECL_SOURCE_LOCATION (decl));
939 if (what)
940 warning (OPT_Wdeprecated_declarations,
941 "%qs is deprecated (declared at %s:%d)", what,
942 xloc.file, xloc.line);
943 else
944 warning (OPT_Wdeprecated_declarations,
945 "type is deprecated (declared at %s:%d)",
946 xloc.file, xloc.line);
948 else
950 if (what)
951 warning (OPT_Wdeprecated_declarations, "%qs is deprecated", what);
952 else
953 warning (OPT_Wdeprecated_declarations, "type is deprecated");
958 /* Compile an entire translation unit. Write a file of assembly
959 output and various debugging dumps. */
961 static void
962 compile_file (void)
964 /* Initialize yet another pass. */
966 ggc_protect_identifiers = true;
968 init_cgraph ();
969 init_final (main_input_filename);
970 coverage_init (aux_base_name);
971 statistics_init ();
972 initialize_plugins ();
974 timevar_push (TV_PARSE);
976 /* Call the parser, which parses the entire file (calling
977 rest_of_compilation for each function). */
978 lang_hooks.parse_file (set_yydebug);
980 /* Compilation is now finished except for writing
981 what's left of the symbol table output. */
982 timevar_pop (TV_PARSE);
984 if (flag_syntax_only)
985 return;
987 ggc_protect_identifiers = false;
989 lang_hooks.decls.final_write_globals ();
991 if (errorcount || sorrycount)
992 return;
994 varpool_assemble_pending_decls ();
995 finish_aliases_2 ();
997 /* This must occur after the loop to output deferred functions.
998 Else the coverage initializer would not be emitted if all the
999 functions in this compilation unit were deferred. */
1000 coverage_finish ();
1002 /* Likewise for mudflap static object registrations. */
1003 if (flag_mudflap)
1004 mudflap_finish_file ();
1006 /* Likewise for emulated thread-local storage. */
1007 if (!targetm.have_tls)
1008 emutls_finish ();
1010 output_shared_constant_pool ();
1011 output_object_blocks ();
1013 /* Write out any pending weak symbol declarations. */
1014 weak_finish ();
1016 /* Do dbx symbols. */
1017 timevar_push (TV_SYMOUT);
1019 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
1020 if (dwarf2out_do_frame ())
1021 dwarf2out_frame_finish ();
1022 #endif
1024 (*debug_hooks->finish) (main_input_filename);
1025 timevar_pop (TV_SYMOUT);
1027 /* Output some stuff at end of file if nec. */
1029 dw2_output_indirect_constants ();
1031 /* Flush any pending external directives. */
1032 process_pending_assemble_externals ();
1034 /* Attach a special .ident directive to the end of the file to identify
1035 the version of GCC which compiled this code. The format of the .ident
1036 string is patterned after the ones produced by native SVR4 compilers. */
1037 #ifdef IDENT_ASM_OP
1038 if (!flag_no_ident)
1040 const char *pkg_version = "(GNU) ";
1042 if (strcmp ("(GCC) ", pkgversion_string))
1043 pkg_version = pkgversion_string;
1044 fprintf (asm_out_file, "%s\"GCC: %s%s\"\n",
1045 IDENT_ASM_OP, pkg_version, version_string);
1047 #endif
1049 /* This must be at the end. Some target ports emit end of file directives
1050 into the assembly file here, and hence we can not output anything to the
1051 assembly file after this point. */
1052 targetm.asm_out.file_end ();
1055 /* Parse a -d... command line switch. */
1057 void
1058 decode_d_option (const char *arg)
1060 int c;
1062 while (*arg)
1063 switch (c = *arg++)
1065 case 'A':
1066 flag_debug_asm = 1;
1067 break;
1068 case 'p':
1069 flag_print_asm_name = 1;
1070 break;
1071 case 'P':
1072 flag_dump_rtl_in_asm = 1;
1073 flag_print_asm_name = 1;
1074 break;
1075 case 'v':
1076 graph_dump_format = vcg;
1077 break;
1078 case 'x':
1079 rtl_dump_and_exit = 1;
1080 break;
1081 case 'y':
1082 set_yydebug = 1;
1083 break;
1084 case 'D': /* These are handled by the preprocessor. */
1085 case 'I':
1086 case 'M':
1087 case 'N':
1088 case 'U':
1089 break;
1090 case 'H':
1091 setup_core_dumping();
1092 break;
1093 case 'a':
1094 enable_rtl_dump_file ();
1095 break;
1097 default:
1098 warning (0, "unrecognized gcc debugging option: %c", c);
1099 break;
1103 /* Indexed by enum debug_info_type. */
1104 const char *const debug_type_names[] =
1106 "none", "stabs", "coff", "dwarf-2", "xcoff", "vms"
1109 /* Print version information to FILE.
1110 Each line begins with INDENT (for the case where FILE is the
1111 assembler output file). */
1113 void
1114 print_version (FILE *file, const char *indent)
1116 static const char fmt1[] =
1117 #ifdef __GNUC__
1118 N_("%s%s%s %sversion %s (%s)\n%s\tcompiled by GNU C version %s, ")
1119 #else
1120 N_("%s%s%s %sversion %s (%s) compiled by CC, ")
1121 #endif
1123 static const char fmt2[] =
1124 N_("GMP version %s, MPFR version %s.\n");
1125 static const char fmt3[] =
1126 N_("%s%swarning: %s header version %s differs from library version %s.\n");
1127 static const char fmt4[] =
1128 N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
1129 #ifndef __VERSION__
1130 #define __VERSION__ "[?]"
1131 #endif
1132 fprintf (file,
1133 file == stderr ? _(fmt1) : fmt1,
1134 indent, *indent != 0 ? " " : "",
1135 lang_hooks.name, pkgversion_string, version_string, TARGET_NAME,
1136 indent, __VERSION__);
1138 /* We need to stringify the GMP macro values. Ugh, gmp_version has
1139 two string formats, "i.j.k" and "i.j" when k is zero. */
1140 #define GCC_GMP_STRINGIFY_VERSION3(X) #X
1141 #define GCC_GMP_STRINGIFY_VERSION2(X) GCC_GMP_STRINGIFY_VERSION3(X)
1142 #if __GNU_MP_VERSION_PATCHLEVEL == 0
1143 #define GCC_GMP_STRINGIFY_VERSION GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION) "." \
1144 GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_MINOR)
1145 #else
1146 #define GCC_GMP_STRINGIFY_VERSION GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION) "." \
1147 GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_MINOR) "." \
1148 GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_PATCHLEVEL)
1149 #endif
1150 fprintf (file,
1151 file == stderr ? _(fmt2) : fmt2,
1152 GCC_GMP_STRINGIFY_VERSION, MPFR_VERSION_STRING);
1153 if (strcmp (GCC_GMP_STRINGIFY_VERSION, gmp_version))
1154 fprintf (file,
1155 file == stderr ? _(fmt3) : fmt3,
1156 indent, *indent != 0 ? " " : "",
1157 "GMP", GCC_GMP_STRINGIFY_VERSION, gmp_version);
1158 if (strcmp (MPFR_VERSION_STRING, mpfr_get_version ()))
1159 fprintf (file,
1160 file == stderr ? _(fmt3) : fmt3,
1161 indent, *indent != 0 ? " " : "",
1162 "MPFR", MPFR_VERSION_STRING, mpfr_get_version ());
1163 fprintf (file,
1164 file == stderr ? _(fmt4) : fmt4,
1165 indent, *indent != 0 ? " " : "",
1166 PARAM_VALUE (GGC_MIN_EXPAND), PARAM_VALUE (GGC_MIN_HEAPSIZE));
1169 #ifdef ASM_COMMENT_START
1170 static int
1171 print_to_asm_out_file (print_switch_type type, const char * text)
1173 bool prepend_sep = true;
1175 switch (type)
1177 case SWITCH_TYPE_LINE_END:
1178 putc ('\n', asm_out_file);
1179 return 1;
1181 case SWITCH_TYPE_LINE_START:
1182 fputs (ASM_COMMENT_START, asm_out_file);
1183 return strlen (ASM_COMMENT_START);
1185 case SWITCH_TYPE_DESCRIPTIVE:
1186 if (ASM_COMMENT_START[0] == 0)
1187 prepend_sep = false;
1188 /* Drop through. */
1189 case SWITCH_TYPE_PASSED:
1190 case SWITCH_TYPE_ENABLED:
1191 if (prepend_sep)
1192 fputc (' ', asm_out_file);
1193 fprintf (asm_out_file, text);
1194 /* No need to return the length here as
1195 print_single_switch has already done it. */
1196 return 0;
1198 default:
1199 return -1;
1202 #endif
1204 static int
1205 print_to_stderr (print_switch_type type, const char * text)
1207 switch (type)
1209 case SWITCH_TYPE_LINE_END:
1210 putc ('\n', stderr);
1211 return 1;
1213 case SWITCH_TYPE_LINE_START:
1214 return 0;
1216 case SWITCH_TYPE_PASSED:
1217 case SWITCH_TYPE_ENABLED:
1218 fputc (' ', stderr);
1219 /* Drop through. */
1221 case SWITCH_TYPE_DESCRIPTIVE:
1222 fprintf (stderr, text);
1223 /* No need to return the length here as
1224 print_single_switch has already done it. */
1225 return 0;
1227 default:
1228 return -1;
1232 /* Print an option value and return the adjusted position in the line.
1233 ??? print_fn doesn't handle errors, eg disk full; presumably other
1234 code will catch a disk full though. */
1236 static int
1237 print_single_switch (print_switch_fn_type print_fn,
1238 int pos,
1239 print_switch_type type,
1240 const char * text)
1242 /* The ultrix fprintf returns 0 on success, so compute the result
1243 we want here since we need it for the following test. The +1
1244 is for the separator character that will probably be emitted. */
1245 int len = strlen (text) + 1;
1247 if (pos != 0
1248 && pos + len > MAX_LINE)
1250 print_fn (SWITCH_TYPE_LINE_END, NULL);
1251 pos = 0;
1254 if (pos == 0)
1255 pos += print_fn (SWITCH_TYPE_LINE_START, NULL);
1257 print_fn (type, text);
1258 return pos + len;
1261 /* Print active target switches using PRINT_FN.
1262 POS is the current cursor position and MAX is the size of a "line".
1263 Each line begins with INDENT and ends with TERM.
1264 Each switch is separated from the next by SEP. */
1266 static void
1267 print_switch_values (print_switch_fn_type print_fn)
1269 int pos = 0;
1270 size_t j;
1271 const char **p;
1273 /* Fill in the -frandom-seed option, if the user didn't pass it, so
1274 that it can be printed below. This helps reproducibility. */
1275 if (!flag_random_seed)
1276 init_random_seed ();
1278 /* Print the options as passed. */
1279 pos = print_single_switch (print_fn, pos,
1280 SWITCH_TYPE_DESCRIPTIVE, _("options passed: "));
1282 for (p = &save_argv[1]; *p != NULL; p++)
1284 if (**p == '-')
1286 /* Ignore these. */
1287 if (strcmp (*p, "-o") == 0
1288 || strcmp (*p, "-dumpbase") == 0
1289 || strcmp (*p, "-auxbase") == 0)
1291 if (p[1] != NULL)
1292 p++;
1293 continue;
1296 if (strcmp (*p, "-quiet") == 0
1297 || strcmp (*p, "-version") == 0)
1298 continue;
1300 if ((*p)[1] == 'd')
1301 continue;
1304 pos = print_single_switch (print_fn, pos, SWITCH_TYPE_PASSED, *p);
1307 if (pos > 0)
1308 print_fn (SWITCH_TYPE_LINE_END, NULL);
1310 /* Print the -f and -m options that have been enabled.
1311 We don't handle language specific options but printing argv
1312 should suffice. */
1313 pos = print_single_switch (print_fn, 0,
1314 SWITCH_TYPE_DESCRIPTIVE, _("options enabled: "));
1316 for (j = 0; j < cl_options_count; j++)
1317 if ((cl_options[j].flags & CL_REPORT)
1318 && option_enabled (j) > 0)
1319 pos = print_single_switch (print_fn, pos,
1320 SWITCH_TYPE_ENABLED, cl_options[j].opt_text);
1322 print_fn (SWITCH_TYPE_LINE_END, NULL);
1325 /* Open assembly code output file. Do this even if -fsyntax-only is
1326 on, because then the driver will have provided the name of a
1327 temporary file or bit bucket for us. NAME is the file specified on
1328 the command line, possibly NULL. */
1329 static void
1330 init_asm_output (const char *name)
1332 if (name == NULL && asm_file_name == 0)
1333 asm_out_file = stdout;
1334 else
1336 if (asm_file_name == 0)
1338 int len = strlen (dump_base_name);
1339 char *dumpname = XNEWVEC (char, len + 6);
1341 memcpy (dumpname, dump_base_name, len + 1);
1342 strip_off_ending (dumpname, len);
1343 strcat (dumpname, ".s");
1344 asm_file_name = dumpname;
1346 if (!strcmp (asm_file_name, "-"))
1347 asm_out_file = stdout;
1348 else
1349 asm_out_file = fopen (asm_file_name, "w+b");
1350 if (asm_out_file == 0)
1351 fatal_error ("can%'t open %s for writing: %m", asm_file_name);
1354 if (!flag_syntax_only)
1356 targetm.asm_out.file_start ();
1358 if (flag_record_gcc_switches)
1360 if (targetm.asm_out.record_gcc_switches)
1362 /* Let the target know that we are about to start recording. */
1363 targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1364 NULL);
1365 /* Now record the switches. */
1366 print_switch_values (targetm.asm_out.record_gcc_switches);
1367 /* Let the target know that the recording is over. */
1368 targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1369 NULL);
1371 else
1372 inform (input_location, "-frecord-gcc-switches is not supported by the current target");
1375 #ifdef ASM_COMMENT_START
1376 if (flag_verbose_asm)
1378 /* Print the list of switches in effect
1379 into the assembler file as comments. */
1380 print_version (asm_out_file, ASM_COMMENT_START);
1381 print_switch_values (print_to_asm_out_file);
1382 fprintf (asm_out_file, "\n");
1384 #endif
1388 /* Return true if the state of option OPTION should be stored in PCH files
1389 and checked by default_pch_valid_p. Store the option's current state
1390 in STATE if so. */
1392 static inline bool
1393 option_affects_pch_p (int option, struct cl_option_state *state)
1395 if ((cl_options[option].flags & CL_TARGET) == 0)
1396 return false;
1397 if (cl_options[option].flag_var == &target_flags)
1398 if (targetm.check_pch_target_flags)
1399 return false;
1400 return get_option_state (option, state);
1403 /* Default version of get_pch_validity.
1404 By default, every flag difference is fatal; that will be mostly right for
1405 most targets, but completely right for very few. */
1407 void *
1408 default_get_pch_validity (size_t *len)
1410 struct cl_option_state state;
1411 size_t i;
1412 char *result, *r;
1414 *len = 2;
1415 if (targetm.check_pch_target_flags)
1416 *len += sizeof (target_flags);
1417 for (i = 0; i < cl_options_count; i++)
1418 if (option_affects_pch_p (i, &state))
1419 *len += state.size;
1421 result = r = XNEWVEC (char, *len);
1422 r[0] = flag_pic;
1423 r[1] = flag_pie;
1424 r += 2;
1425 if (targetm.check_pch_target_flags)
1427 memcpy (r, &target_flags, sizeof (target_flags));
1428 r += sizeof (target_flags);
1431 for (i = 0; i < cl_options_count; i++)
1432 if (option_affects_pch_p (i, &state))
1434 memcpy (r, state.data, state.size);
1435 r += state.size;
1438 return result;
1441 /* Return a message which says that a PCH file was created with a different
1442 setting of OPTION. */
1444 static const char *
1445 pch_option_mismatch (const char *option)
1447 char *r;
1449 asprintf (&r, _("created and used with differing settings of '%s'"), option);
1450 if (r == NULL)
1451 return _("out of memory");
1452 return r;
1455 /* Default version of pch_valid_p. */
1457 const char *
1458 default_pch_valid_p (const void *data_p, size_t len)
1460 struct cl_option_state state;
1461 const char *data = (const char *)data_p;
1462 size_t i;
1464 /* -fpic and -fpie also usually make a PCH invalid. */
1465 if (data[0] != flag_pic)
1466 return _("created and used with different settings of -fpic");
1467 if (data[1] != flag_pie)
1468 return _("created and used with different settings of -fpie");
1469 data += 2;
1471 /* Check target_flags. */
1472 if (targetm.check_pch_target_flags)
1474 int tf;
1475 const char *r;
1477 memcpy (&tf, data, sizeof (target_flags));
1478 data += sizeof (target_flags);
1479 len -= sizeof (target_flags);
1480 r = targetm.check_pch_target_flags (tf);
1481 if (r != NULL)
1482 return r;
1485 for (i = 0; i < cl_options_count; i++)
1486 if (option_affects_pch_p (i, &state))
1488 if (memcmp (data, state.data, state.size) != 0)
1489 return pch_option_mismatch (cl_options[i].opt_text);
1490 data += state.size;
1491 len -= state.size;
1494 return NULL;
1497 /* Default tree printer. Handles declarations only. */
1498 static bool
1499 default_tree_printer (pretty_printer * pp, text_info *text, const char *spec,
1500 int precision, bool wide, bool set_locus, bool hash)
1502 tree t;
1504 /* FUTURE: %+x should set the locus. */
1505 if (precision != 0 || wide || hash)
1506 return false;
1508 switch (*spec)
1510 case 'D':
1511 t = va_arg (*text->args_ptr, tree);
1512 if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
1513 t = DECL_DEBUG_EXPR (t);
1514 break;
1516 case 'F':
1517 case 'T':
1518 t = va_arg (*text->args_ptr, tree);
1519 break;
1521 default:
1522 return false;
1525 if (set_locus && text->locus)
1526 *text->locus = DECL_SOURCE_LOCATION (t);
1528 if (DECL_P (t))
1530 const char *n = DECL_NAME (t)
1531 ? lang_hooks.decl_printable_name (t, 2)
1532 : "<anonymous>";
1533 pp_string (pp, n);
1535 else
1536 dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
1538 return true;
1541 /* A helper function; used as the reallocator function for cpp's line
1542 table. */
1543 static void *
1544 realloc_for_line_map (void *ptr, size_t len)
1546 return ggc_realloc (ptr, len);
1549 /* Initialization of the front end environment, before command line
1550 options are parsed. Signal handlers, internationalization etc.
1551 ARGV0 is main's argv[0]. */
1552 static void
1553 general_init (const char *argv0)
1555 const char *p;
1557 p = argv0 + strlen (argv0);
1558 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
1559 --p;
1560 progname = p;
1562 xmalloc_set_program_name (progname);
1564 hex_init ();
1566 /* Unlock the stdio streams. */
1567 unlock_std_streams ();
1569 gcc_init_libintl ();
1571 /* Initialize the diagnostics reporting machinery, so option parsing
1572 can give warnings and errors. */
1573 diagnostic_initialize (global_dc);
1574 /* Set a default printer. Language specific initializations will
1575 override it later. */
1576 pp_format_decoder (global_dc->printer) = &default_tree_printer;
1578 /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages. */
1579 #ifdef SIGSEGV
1580 signal (SIGSEGV, crash_signal);
1581 #endif
1582 #ifdef SIGILL
1583 signal (SIGILL, crash_signal);
1584 #endif
1585 #ifdef SIGBUS
1586 signal (SIGBUS, crash_signal);
1587 #endif
1588 #ifdef SIGABRT
1589 signal (SIGABRT, crash_signal);
1590 #endif
1591 #if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
1592 signal (SIGIOT, crash_signal);
1593 #endif
1594 #ifdef SIGFPE
1595 signal (SIGFPE, crash_signal);
1596 #endif
1598 /* Other host-specific signal setup. */
1599 (*host_hooks.extra_signals)();
1601 /* Initialize the garbage-collector, string pools and tree type hash
1602 table. */
1603 init_ggc ();
1604 init_stringpool ();
1605 line_table = GGC_NEW (struct line_maps);
1606 linemap_init (line_table);
1607 line_table->reallocator = realloc_for_line_map;
1608 init_ttree ();
1610 /* Initialize register usage now so switches may override. */
1611 init_reg_sets ();
1613 /* Register the language-independent parameters. */
1614 add_params (lang_independent_params, LAST_PARAM);
1616 /* This must be done after add_params but before argument processing. */
1617 init_ggc_heuristics();
1618 init_optimization_passes ();
1619 statistics_early_init ();
1622 /* Return true if the current target supports -fsection-anchors. */
1624 static bool
1625 target_supports_section_anchors_p (void)
1627 if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1628 return false;
1630 if (targetm.asm_out.output_anchor == NULL)
1631 return false;
1633 return true;
1636 /* Default the align_* variables to 1 if they're still unset, and
1637 set up the align_*_log variables. */
1638 static void
1639 init_alignments (void)
1641 if (align_loops <= 0)
1642 align_loops = 1;
1643 if (align_loops_max_skip > align_loops)
1644 align_loops_max_skip = align_loops - 1;
1645 align_loops_log = floor_log2 (align_loops * 2 - 1);
1646 if (align_jumps <= 0)
1647 align_jumps = 1;
1648 if (align_jumps_max_skip > align_jumps)
1649 align_jumps_max_skip = align_jumps - 1;
1650 align_jumps_log = floor_log2 (align_jumps * 2 - 1);
1651 if (align_labels <= 0)
1652 align_labels = 1;
1653 align_labels_log = floor_log2 (align_labels * 2 - 1);
1654 if (align_labels_max_skip > align_labels)
1655 align_labels_max_skip = align_labels - 1;
1656 if (align_functions <= 0)
1657 align_functions = 1;
1658 align_functions_log = floor_log2 (align_functions * 2 - 1);
1661 /* Process the options that have been parsed. */
1662 static void
1663 process_options (void)
1665 /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1666 This can happen with incorrect pre-processed input. */
1667 debug_hooks = &do_nothing_debug_hooks;
1669 /* This replaces set_Wunused. */
1670 if (warn_unused_function == -1)
1671 warn_unused_function = warn_unused;
1672 if (warn_unused_label == -1)
1673 warn_unused_label = warn_unused;
1674 /* Wunused-parameter is enabled if both -Wunused -Wextra are enabled. */
1675 if (warn_unused_parameter == -1)
1676 warn_unused_parameter = (warn_unused && extra_warnings);
1677 if (warn_unused_variable == -1)
1678 warn_unused_variable = warn_unused;
1679 if (warn_unused_value == -1)
1680 warn_unused_value = warn_unused;
1682 /* Allow the front end to perform consistency checks and do further
1683 initialization based on the command line options. This hook also
1684 sets the original filename if appropriate (e.g. foo.i -> foo.c)
1685 so we can correctly initialize debug output. */
1686 no_backend = lang_hooks.post_options (&main_input_filename);
1688 #ifdef OVERRIDE_OPTIONS
1689 /* Some machines may reject certain combinations of options. */
1690 OVERRIDE_OPTIONS;
1691 #endif
1693 if (flag_section_anchors && !target_supports_section_anchors_p ())
1695 warning (OPT_fsection_anchors,
1696 "this target does not support %qs", "-fsection-anchors");
1697 flag_section_anchors = 0;
1700 if (flag_short_enums == 2)
1701 flag_short_enums = targetm.default_short_enums ();
1703 /* Set aux_base_name if not already set. */
1704 if (aux_base_name)
1706 else if (main_input_filename)
1708 char *name = xstrdup (lbasename (main_input_filename));
1710 strip_off_ending (name, strlen (name));
1711 aux_base_name = name;
1713 else
1714 aux_base_name = "gccaux";
1716 #ifndef HAVE_cloog
1717 if (flag_graphite
1718 || flag_loop_block
1719 || flag_loop_interchange
1720 || flag_loop_strip_mine
1721 || flag_graphite_identity)
1722 sorry ("Graphite loop optimizations cannot be used");
1723 #endif
1725 /* Unrolling all loops implies that standard loop unrolling must also
1726 be done. */
1727 if (flag_unroll_all_loops)
1728 flag_unroll_loops = 1;
1730 /* The loop unrolling code assumes that cse will be run after loop.
1731 web and rename-registers also help when run after loop unrolling. */
1733 if (flag_rerun_cse_after_loop == AUTODETECT_VALUE)
1734 flag_rerun_cse_after_loop = flag_unroll_loops || flag_peel_loops;
1735 if (flag_web == AUTODETECT_VALUE)
1736 flag_web = flag_unroll_loops || flag_peel_loops;
1737 if (flag_rename_registers == AUTODETECT_VALUE)
1738 flag_rename_registers = flag_unroll_loops || flag_peel_loops;
1740 if (flag_non_call_exceptions)
1741 flag_asynchronous_unwind_tables = 1;
1742 if (flag_asynchronous_unwind_tables)
1743 flag_unwind_tables = 1;
1745 if (flag_value_profile_transformations)
1746 flag_profile_values = 1;
1748 /* Warn about options that are not supported on this machine. */
1749 #ifndef INSN_SCHEDULING
1750 if (flag_schedule_insns || flag_schedule_insns_after_reload)
1751 warning (0, "instruction scheduling not supported on this target machine");
1752 #endif
1753 #ifndef DELAY_SLOTS
1754 if (flag_delayed_branch)
1755 warning (0, "this target machine does not have delayed branches");
1756 #endif
1758 user_label_prefix = USER_LABEL_PREFIX;
1759 if (flag_leading_underscore != -1)
1761 /* If the default prefix is more complicated than "" or "_",
1762 issue a warning and ignore this option. */
1763 if (user_label_prefix[0] == 0 ||
1764 (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
1766 user_label_prefix = flag_leading_underscore ? "_" : "";
1768 else
1769 warning (0, "-f%sleading-underscore not supported on this target machine",
1770 flag_leading_underscore ? "" : "no-");
1773 /* If we are in verbose mode, write out the version and maybe all the
1774 option flags in use. */
1775 if (version_flag)
1777 print_version (stderr, "");
1778 if (! quiet_flag)
1779 print_switch_values (print_to_stderr);
1782 if (flag_syntax_only)
1784 write_symbols = NO_DEBUG;
1785 profile_flag = 0;
1788 /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1789 level is 0. */
1790 if (debug_info_level == DINFO_LEVEL_NONE)
1791 write_symbols = NO_DEBUG;
1793 /* Now we know write_symbols, set up the debug hooks based on it.
1794 By default we do nothing for debug output. */
1795 if (PREFERRED_DEBUGGING_TYPE == NO_DEBUG)
1796 default_debug_hooks = &do_nothing_debug_hooks;
1797 #if defined(DBX_DEBUGGING_INFO)
1798 else if (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG)
1799 default_debug_hooks = &dbx_debug_hooks;
1800 #endif
1801 #if defined(XCOFF_DEBUGGING_INFO)
1802 else if (PREFERRED_DEBUGGING_TYPE == XCOFF_DEBUG)
1803 default_debug_hooks = &xcoff_debug_hooks;
1804 #endif
1805 #ifdef SDB_DEBUGGING_INFO
1806 else if (PREFERRED_DEBUGGING_TYPE == SDB_DEBUG)
1807 default_debug_hooks = &sdb_debug_hooks;
1808 #endif
1809 #ifdef DWARF2_DEBUGGING_INFO
1810 else if (PREFERRED_DEBUGGING_TYPE == DWARF2_DEBUG)
1811 default_debug_hooks = &dwarf2_debug_hooks;
1812 #endif
1813 #ifdef VMS_DEBUGGING_INFO
1814 else if (PREFERRED_DEBUGGING_TYPE == VMS_DEBUG
1815 || PREFERRED_DEBUGGING_TYPE == VMS_AND_DWARF2_DEBUG)
1816 default_debug_hooks = &vmsdbg_debug_hooks;
1817 #endif
1819 if (write_symbols == NO_DEBUG)
1821 #if defined(DBX_DEBUGGING_INFO)
1822 else if (write_symbols == DBX_DEBUG)
1823 debug_hooks = &dbx_debug_hooks;
1824 #endif
1825 #if defined(XCOFF_DEBUGGING_INFO)
1826 else if (write_symbols == XCOFF_DEBUG)
1827 debug_hooks = &xcoff_debug_hooks;
1828 #endif
1829 #ifdef SDB_DEBUGGING_INFO
1830 else if (write_symbols == SDB_DEBUG)
1831 debug_hooks = &sdb_debug_hooks;
1832 #endif
1833 #ifdef DWARF2_DEBUGGING_INFO
1834 else if (write_symbols == DWARF2_DEBUG)
1835 debug_hooks = &dwarf2_debug_hooks;
1836 #endif
1837 #ifdef VMS_DEBUGGING_INFO
1838 else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
1839 debug_hooks = &vmsdbg_debug_hooks;
1840 #endif
1841 else
1842 error ("target system does not support the \"%s\" debug format",
1843 debug_type_names[write_symbols]);
1845 /* Now we know which debug output will be used so we can set
1846 flag_var_tracking, flag_rename_registers if the user has
1847 not specified them. */
1848 if (debug_info_level < DINFO_LEVEL_NORMAL
1849 || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1851 if (flag_var_tracking == 1
1852 || flag_var_tracking_uninit == 1)
1854 if (debug_info_level < DINFO_LEVEL_NORMAL)
1855 warning (0, "variable tracking requested, but useless unless "
1856 "producing debug info");
1857 else
1858 warning (0, "variable tracking requested, but not supported "
1859 "by this debug format");
1861 flag_var_tracking = 0;
1862 flag_var_tracking_uninit = 0;
1865 if (flag_rename_registers == AUTODETECT_VALUE)
1866 flag_rename_registers = default_debug_hooks->var_location
1867 != do_nothing_debug_hooks.var_location;
1869 if (flag_var_tracking == AUTODETECT_VALUE)
1870 flag_var_tracking = optimize >= 1;
1872 if (flag_tree_cselim == AUTODETECT_VALUE)
1873 #ifdef HAVE_conditional_move
1874 flag_tree_cselim = 1;
1875 #else
1876 flag_tree_cselim = 0;
1877 #endif
1879 /* If the user specifically requested variable tracking with tagging
1880 uninitialized variables, we need to turn on variable tracking.
1881 (We already determined above that variable tracking is feasible.) */
1882 if (flag_var_tracking_uninit)
1883 flag_var_tracking = 1;
1885 /* If auxiliary info generation is desired, open the output file.
1886 This goes in the same directory as the source file--unlike
1887 all the other output files. */
1888 if (flag_gen_aux_info)
1890 aux_info_file = fopen (aux_info_file_name, "w");
1891 if (aux_info_file == 0)
1892 fatal_error ("can%'t open %s: %m", aux_info_file_name);
1895 if (! targetm.have_named_sections)
1897 if (flag_function_sections)
1899 warning (0, "-ffunction-sections not supported for this target");
1900 flag_function_sections = 0;
1902 if (flag_data_sections)
1904 warning (0, "-fdata-sections not supported for this target");
1905 flag_data_sections = 0;
1909 if (flag_function_sections && profile_flag)
1911 warning (0, "-ffunction-sections disabled; it makes profiling impossible");
1912 flag_function_sections = 0;
1915 #ifndef HAVE_prefetch
1916 if (flag_prefetch_loop_arrays)
1918 warning (0, "-fprefetch-loop-arrays not supported for this target");
1919 flag_prefetch_loop_arrays = 0;
1921 #else
1922 if (flag_prefetch_loop_arrays && !HAVE_prefetch)
1924 warning (0, "-fprefetch-loop-arrays not supported for this target (try -march switches)");
1925 flag_prefetch_loop_arrays = 0;
1927 #endif
1929 /* This combination of options isn't handled for i386 targets and doesn't
1930 make much sense anyway, so don't allow it. */
1931 if (flag_prefetch_loop_arrays && optimize_size)
1933 warning (0, "-fprefetch-loop-arrays is not supported with -Os");
1934 flag_prefetch_loop_arrays = 0;
1937 /* The presence of IEEE signaling NaNs, implies all math can trap. */
1938 if (flag_signaling_nans)
1939 flag_trapping_math = 1;
1941 /* We cannot reassociate if we want traps or signed zeros.  */
1942 if (flag_associative_math && (flag_trapping_math || flag_signed_zeros))
1944 warning (0, "-fassociative-math disabled; other options take precedence");
1945 flag_associative_math = 0;
1948 /* With -fcx-limited-range, we do cheap and quick complex arithmetic. */
1949 if (flag_cx_limited_range)
1950 flag_complex_method = 0;
1952 /* With -fcx-fortran-rules, we do something in-between cheap and C99. */
1953 if (flag_cx_fortran_rules)
1954 flag_complex_method = 1;
1956 /* Targets must be able to place spill slots at lower addresses. If the
1957 target already uses a soft frame pointer, the transition is trivial. */
1958 if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
1960 warning (0, "-fstack-protector not supported for this target");
1961 flag_stack_protect = 0;
1963 if (!flag_stack_protect)
1964 warn_stack_protect = 0;
1966 /* ??? Unwind info is not correct around the CFG unless either a frame
1967 pointer is present or A_O_A is set. Fixing this requires rewriting
1968 unwind info generation to be aware of the CFG and propagating states
1969 around edges. */
1970 if (flag_unwind_tables && !ACCUMULATE_OUTGOING_ARGS
1971 && flag_omit_frame_pointer)
1973 warning (0, "unwind tables currently require a frame pointer "
1974 "for correctness");
1975 flag_omit_frame_pointer = 0;
1979 /* This function can be called multiple times to reinitialize the compiler
1980 back end when register classes or instruction sets have changed,
1981 before each function. */
1982 static void
1983 backend_init_target (void)
1985 /* Initialize alignment variables. */
1986 init_alignments ();
1988 /* This reinitializes hard_frame_pointer, and calls init_reg_modes_target()
1989 to initialize reg_raw_mode[]. */
1990 init_emit_regs ();
1992 /* This invokes target hooks to set fixed_reg[] etc, which is
1993 mode-dependent. */
1994 init_regs ();
1996 /* This depends on stack_pointer_rtx. */
1997 init_fake_stack_mems ();
1999 /* Sets static_base_value[HARD_FRAME_POINTER_REGNUM], which is
2000 mode-dependent. */
2001 init_alias_target ();
2003 /* Depends on HARD_FRAME_POINTER_REGNUM. */
2004 init_reload ();
2006 /* The following initialization functions need to generate rtl, so
2007 provide a dummy function context for them. */
2008 init_dummy_function_start ();
2010 /* rtx_cost is mode-dependent, so cached values need to be recomputed
2011 on a mode change. */
2012 init_expmed ();
2014 /* We may need to recompute regno_save_code[] and regno_restore_code[]
2015 after a mode change as well. */
2016 if (flag_caller_saves)
2017 init_caller_save ();
2018 expand_dummy_function_end ();
2021 /* Initialize the compiler back end. This function is called only once,
2022 when starting the compiler. */
2023 static void
2024 backend_init (void)
2026 init_emit_once (debug_info_level == DINFO_LEVEL_NORMAL
2027 || debug_info_level == DINFO_LEVEL_VERBOSE
2028 #ifdef VMS_DEBUGGING_INFO
2029 /* Enable line number info for traceback. */
2030 || debug_info_level > DINFO_LEVEL_NONE
2031 #endif
2032 || flag_test_coverage);
2034 init_rtlanal ();
2035 init_inline_once ();
2036 init_varasm_once ();
2037 save_register_info ();
2039 /* Initialize the target-specific back end pieces. */
2040 ira_init_once ();
2041 backend_init_target ();
2044 /* Initialize excess precision settings. */
2045 static void
2046 init_excess_precision (void)
2048 /* Adjust excess precision handling based on the target options. If
2049 the front end cannot handle it, flag_excess_precision_cmdline
2050 will already have been set accordingly in the post_options
2051 hook. */
2052 gcc_assert (flag_excess_precision_cmdline != EXCESS_PRECISION_DEFAULT);
2053 flag_excess_precision = flag_excess_precision_cmdline;
2054 if (flag_unsafe_math_optimizations)
2055 flag_excess_precision = EXCESS_PRECISION_FAST;
2056 if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
2058 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
2059 switch (flt_eval_method)
2061 case -1:
2062 case 0:
2063 /* Either the target acts unpredictably (-1) or has all the
2064 operations required not to have excess precision (0). */
2065 flag_excess_precision = EXCESS_PRECISION_FAST;
2066 break;
2067 case 1:
2068 case 2:
2069 /* In these cases, predictable excess precision makes
2070 sense. */
2071 break;
2072 default:
2073 /* Any other implementation-defined FLT_EVAL_METHOD values
2074 require the compiler to handle the associated excess
2075 precision rules in excess_precision_type. */
2076 gcc_unreachable ();
2081 /* Initialize things that are both lang-dependent and target-dependent.
2082 This function can be called more than once if target parameters change. */
2083 static void
2084 lang_dependent_init_target (void)
2086 /* This determines excess precision settings. */
2087 init_excess_precision ();
2089 /* This creates various _DECL nodes, so needs to be called after the
2090 front end is initialized. It also depends on the HAVE_xxx macros
2091 generated from the target machine description. */
2092 init_optabs ();
2094 /* The following initialization functions need to generate rtl, so
2095 provide a dummy function context for them. */
2096 init_dummy_function_start ();
2098 /* Do the target-specific parts of expr initialization. */
2099 init_expr_target ();
2101 /* Although the actions of these functions are language-independent,
2102 they use optabs, so we cannot call them from backend_init. */
2103 init_set_costs ();
2104 ira_init ();
2106 expand_dummy_function_end ();
2109 /* Language-dependent initialization. Returns nonzero on success. */
2110 static int
2111 lang_dependent_init (const char *name)
2113 location_t save_loc = input_location;
2114 if (dump_base_name == 0)
2115 dump_base_name = name && name[0] ? name : "gccdump";
2117 /* Other front-end initialization. */
2118 input_location = BUILTINS_LOCATION;
2119 if (lang_hooks.init () == 0)
2120 return 0;
2121 input_location = save_loc;
2123 init_asm_output (name);
2125 /* This creates various _DECL nodes, so needs to be called after the
2126 front end is initialized. */
2127 init_eh ();
2129 /* Do the target-specific parts of the initialization. */
2130 lang_dependent_init_target ();
2132 /* If dbx symbol table desired, initialize writing it and output the
2133 predefined types. */
2134 timevar_push (TV_SYMOUT);
2136 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
2137 if (dwarf2out_do_frame ())
2138 dwarf2out_frame_init ();
2139 #endif
2141 /* Now we have the correct original filename, we can initialize
2142 debug output. */
2143 (*debug_hooks->init) (name);
2145 timevar_pop (TV_SYMOUT);
2147 return 1;
2151 /* Reinitialize everything when target parameters, such as register usage,
2152 have changed. */
2153 void
2154 target_reinit (void)
2156 /* Reinitialize RTL backend. */
2157 backend_init_target ();
2159 /* Reinitialize lang-dependent parts. */
2160 lang_dependent_init_target ();
2163 void
2164 dump_memory_report (bool final)
2166 ggc_print_statistics ();
2167 stringpool_statistics ();
2168 dump_tree_statistics ();
2169 dump_gimple_statistics ();
2170 dump_rtx_statistics ();
2171 dump_varray_statistics ();
2172 dump_alloc_pool_statistics ();
2173 dump_bitmap_statistics ();
2174 dump_vec_loc_statistics ();
2175 dump_ggc_loc_statistics (final);
2176 dump_alias_stats (stderr);
2177 dump_pta_stats (stderr);
2180 /* Clean up: close opened files, etc. */
2182 static void
2183 finalize (void)
2185 /* Close the dump files. */
2186 if (flag_gen_aux_info)
2188 fclose (aux_info_file);
2189 if (errorcount)
2190 unlink (aux_info_file_name);
2193 /* Close non-debugging input and output files. Take special care to note
2194 whether fclose returns an error, since the pages might still be on the
2195 buffer chain while the file is open. */
2197 if (asm_out_file)
2199 if (ferror (asm_out_file) != 0)
2200 fatal_error ("error writing to %s: %m", asm_file_name);
2201 if (fclose (asm_out_file) != 0)
2202 fatal_error ("error closing %s: %m", asm_file_name);
2205 statistics_fini ();
2206 finish_optimization_passes ();
2208 ira_finish_once ();
2210 if (mem_report)
2211 dump_memory_report (true);
2213 /* Language-specific end of compilation actions. */
2214 lang_hooks.finish ();
2217 /* Initialize the compiler, and compile the input file. */
2218 static void
2219 do_compile (void)
2221 /* Initialize timing first. The C front ends read the main file in
2222 the post_options hook, and C++ does file timings. */
2223 if (time_report || !quiet_flag || flag_detailed_statistics)
2224 timevar_init ();
2225 timevar_start (TV_TOTAL);
2227 process_options ();
2229 /* Don't do any more if an error has already occurred. */
2230 if (!errorcount)
2232 /* This must be run always, because it is needed to compute the FP
2233 predefined macros, such as __LDBL_MAX__, for targets using non
2234 default FP formats. */
2235 init_adjust_machine_modes ();
2237 /* Set up the back-end if requested. */
2238 if (!no_backend)
2239 backend_init ();
2241 /* Language-dependent initialization. Returns true on success. */
2242 if (lang_dependent_init (main_input_filename))
2243 compile_file ();
2245 finalize ();
2247 /* Invoke registered plugin callbacks. */
2248 invoke_plugin_callbacks (PLUGIN_FINISH_UNIT, NULL);
2251 /* Stop timing and print the times. */
2252 timevar_stop (TV_TOTAL);
2253 timevar_print (stderr);
2256 /* Entry point of cc1, cc1plus, jc1, f771, etc.
2257 Exit code is FATAL_EXIT_CODE if can't open files or if there were
2258 any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
2260 It is not safe to call this function more than once. */
2263 toplev_main (unsigned int argc, const char **argv)
2265 save_argv = argv;
2267 /* Initialization of GCC's environment, and diagnostics. */
2268 general_init (argv[0]);
2270 /* Parse the options and do minimal processing; basically just
2271 enough to default flags appropriately. */
2272 decode_options (argc, argv);
2274 init_local_tick ();
2276 /* Exit early if we can (e.g. -help). */
2277 if (!exit_after_options)
2278 do_compile ();
2280 if (warningcount || errorcount)
2281 print_ignored_options ();
2283 /* Invoke registered plugin callbacks if any. */
2284 invoke_plugin_callbacks (PLUGIN_FINISH, NULL);
2286 if (errorcount || sorrycount)
2287 return (FATAL_EXIT_CODE);
2289 return (SUCCESS_EXIT_CODE);