OSDN Git Service

* opt-functions.awk (static_var): Update comment.
[pf3gnuchains/gcc-fork.git] / gcc / toplev.c
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, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
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.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include <signal.h>
32
33 #ifdef HAVE_SYS_RESOURCE_H
34 # include <sys/resource.h>
35 #endif
36
37 #ifdef HAVE_SYS_TIMES_H
38 # include <sys/times.h>
39 #endif
40
41 #include "line-map.h"
42 #include "input.h"
43 #include "tree.h"
44 #include "realmpfr.h"   /* For GMP/MPFR/MPC versions, in print_version.  */
45 #include "version.h"
46 #include "rtl.h"
47 #include "tm_p.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "insn-config.h"
51 #include "insn-flags.h"
52 #include "hard-reg-set.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "except.h"
56 #include "function.h"
57 #include "toplev.h"
58 #include "expr.h"
59 #include "basic-block.h"
60 #include "intl.h"
61 #include "ggc.h"
62 #include "graph.h"
63 #include "regs.h"
64 #include "timevar.h"
65 #include "diagnostic.h"
66 #include "tree-diagnostic.h"
67 #include "tree-pretty-print.h"
68 #include "params.h"
69 #include "reload.h"
70 #include "ira.h"
71 #include "dwarf2asm.h"
72 #include "integrate.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 "opts-diagnostic.h"
82 #include "coverage.h"
83 #include "value-prof.h"
84 #include "alloc-pool.h"
85 #include "tree-mudflap.h"
86 #include "tree-pass.h"
87 #include "gimple.h"
88 #include "tree-ssa-alias.h"
89 #include "plugin.h"
90
91 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
92 #include "dwarf2out.h"
93 #endif
94
95 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
96 #include "dbxout.h"
97 #endif
98
99 #ifdef SDB_DEBUGGING_INFO
100 #include "sdbout.h"
101 #endif
102
103 #ifdef XCOFF_DEBUGGING_INFO
104 #include "xcoffout.h"           /* Needed for external data
105                                    declarations for e.g. AIX 4.x.  */
106 #endif
107
108 static void general_init (const char *);
109 static void do_compile (void);
110 static void process_options (void);
111 static void backend_init (void);
112 static int lang_dependent_init (const char *);
113 static void init_asm_output (const char *);
114 static void finalize (void);
115
116 static void crash_signal (int) ATTRIBUTE_NORETURN;
117 static void setup_core_dumping (void);
118 static void compile_file (void);
119
120 /* Nonzero to dump debug info whilst parsing (-dy option).  */
121 static int set_yydebug;
122
123 /* True if we don't need a backend (e.g. preprocessing only).  */
124 static bool no_backend;
125
126 /* Length of line when printing switch values.  */
127 #define MAX_LINE 75
128
129 /* Decoded options, and number of such options.  */
130 struct cl_decoded_option *save_decoded_options;
131 unsigned int save_decoded_options_count;
132
133 /* Name of top-level original source file (what was input to cpp).
134    This comes from the #-command at the beginning of the actual input.
135    If there isn't any there, then this is the cc1 input file name.  */
136
137 const char *main_input_filename;
138
139 /* Used to enable -fvar-tracking, -fweb and -frename-registers according
140    to optimize in process_options ().  */
141 #define AUTODETECT_VALUE 2
142
143 /* Name to use as base of names for dump output files.  */
144
145 const char *dump_base_name;
146
147 /* Directory used for dump output files.  */
148
149 const char *dump_dir_name;
150
151 /* Name to use as a base for auxiliary output files.  */
152
153 const char *aux_base_name;
154
155 /* Prefix for profile data files */
156 const char *profile_data_prefix;
157
158 /* A mask of target_flags that includes bit X if X was set or cleared
159    on the command line.  */
160
161 int target_flags_explicit;
162
163 /* Debug hooks - dependent upon command line options.  */
164
165 const struct gcc_debug_hooks *debug_hooks;
166
167 /* Other flags saying which kinds of debugging dump have been requested.  */
168
169 int rtl_dump_and_exit;
170 int flag_print_asm_name;
171 enum graph_dump_types graph_dump_format;
172
173 /* Name for output file of assembly code, specified with -o.  */
174
175 const char *asm_file_name;
176
177 /* True if this is the lto front end.  This is used to disable
178    gimple generation and lowering passes that are normally run on the
179    output of a front end.  These passes must be bypassed for lto since
180    they have already been done before the gimple was written.  */
181
182 bool in_lto_p = false;
183
184 /* Nonzero if we should write GIMPLE bytecode for link-time optimization.  */
185
186 int flag_generate_lto;
187
188 /* The FUNCTION_DECL for the function currently being compiled,
189    or 0 if between functions.  */
190 tree current_function_decl;
191
192 /* Set to the FUNC_BEGIN label of the current function, or NULL
193    if none.  */
194 const char * current_function_func_begin_label;
195
196 /* A random sequence of characters, unless overridden by user.  */
197 static const char *flag_random_seed;
198
199 /* A local time stamp derived from the time of compilation. It will be
200    zero if the system cannot provide a time.  It will be -1u, if the
201    user has specified a particular random seed.  */
202 unsigned local_tick;
203
204 /* -f flags.  */
205
206 /* 0 means straightforward implementation of complex divide acceptable.
207    1 means wide ranges of inputs must work for complex divide.
208    2 means C99-like requirements for complex multiply and divide.  */
209
210 int flag_complex_method = 1;
211
212 /* Nonzero means we should be saving declaration info into a .X file.  */
213
214 int flag_gen_aux_info = 0;
215
216 /* Specified name of aux-info file.  */
217
218 const char *aux_info_file_name;
219
220 /* Nonzero if we are compiling code for a shared library, zero for
221    executable.  */
222
223 int flag_shlib;
224
225 /* Generate code for GNU or NeXT Objective-C runtime environment.  */
226
227 #ifdef NEXT_OBJC_RUNTIME
228 int flag_next_runtime = 1;
229 #else
230 int flag_next_runtime = 0;
231 #endif
232
233 /* Set to the default thread-local storage (tls) model to use.  */
234
235 enum tls_model flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
236
237 /* Set the default region and algorithm for the integrated register
238    allocator.  */
239
240 enum ira_algorithm flag_ira_algorithm = IRA_ALGORITHM_CB;
241 enum ira_region flag_ira_region = IRA_REGION_MIXED;
242
243 /* Set the default for excess precision.  */
244
245 enum excess_precision flag_excess_precision_cmdline = EXCESS_PRECISION_DEFAULT;
246
247 /* Nonzero means change certain warnings into errors.
248    Usually these are warnings about failure to conform to some standard.  */
249
250 int flag_pedantic_errors = 0;
251
252 /* Nonzero means make permerror produce warnings instead of errors.  */
253
254 int flag_permissive = 0;
255
256 /* -dA causes debug commentary information to be produced in
257    the generated assembly code (to make it more readable).  This option
258    is generally only of use to those who actually need to read the
259    generated assembly code (perhaps while debugging the compiler itself).
260    Currently, this switch is only used by dwarfout.c; however, it is intended
261    to be a catchall for printing debug information in the assembler file.  */
262
263 int flag_debug_asm = 0;
264
265 /* -dP causes the rtl to be emitted as a comment in assembly.  */
266
267 int flag_dump_rtl_in_asm = 0;
268
269 /* When non-NULL, indicates that whenever space is allocated on the
270    stack, the resulting stack pointer must not pass this
271    address---that is, for stacks that grow downward, the stack pointer
272    must always be greater than or equal to this address; for stacks
273    that grow upward, the stack pointer must be less than this address.
274    At present, the rtx may be either a REG or a SYMBOL_REF, although
275    the support provided depends on the backend.  */
276 rtx stack_limit_rtx;
277
278 /* Type of stack check.  */
279 enum stack_check_type flag_stack_check = NO_STACK_CHECK;
280
281 /* True if the user has tagged the function with the 'section'
282    attribute.  */
283
284 bool user_defined_section_attribute = false;
285
286 struct target_flag_state default_target_flag_state;
287 #if SWITCHABLE_TARGET
288 struct target_flag_state *this_target_flag_state = &default_target_flag_state;
289 #else
290 #define this_target_flag_state (&default_target_flag_state)
291 #endif
292
293 typedef struct
294 {
295   const char *const string;
296   int *const variable;
297   const int on_value;
298 }
299 lang_independent_options;
300
301 /* Nonzero if subexpressions must be evaluated from left-to-right.  */
302 int flag_evaluation_order = 0;
303
304 /* The user symbol prefix after having resolved same.  */
305 const char *user_label_prefix;
306
307 static const param_info lang_independent_params[] = {
308 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
309   { OPTION, DEFAULT, false, MIN, MAX, HELP },
310 #include "params.def"
311 #undef DEFPARAM
312   { NULL, 0, false, 0, 0, NULL }
313 };
314
315 /* Output files for assembler code (real compiler output)
316    and debugging dumps.  */
317
318 FILE *asm_out_file;
319 FILE *aux_info_file;
320 FILE *stack_usage_file = NULL;
321 FILE *dump_file = NULL;
322 const char *dump_file_name;
323
324 /* The current working directory of a translation.  It's generally the
325    directory from which compilation was initiated, but a preprocessed
326    file may specify the original directory in which it was
327    created.  */
328
329 static const char *src_pwd;
330
331 /* Initialize src_pwd with the given string, and return true.  If it
332    was already initialized, return false.  As a special case, it may
333    be called with a NULL argument to test whether src_pwd has NOT been
334    initialized yet.  */
335
336 bool
337 set_src_pwd (const char *pwd)
338 {
339   if (src_pwd)
340     {
341       if (strcmp (src_pwd, pwd) == 0)
342         return true;
343       else
344         return false;
345     }
346
347   src_pwd = xstrdup (pwd);
348   return true;
349 }
350
351 /* Return the directory from which the translation unit was initiated,
352    in case set_src_pwd() was not called before to assign it a
353    different value.  */
354
355 const char *
356 get_src_pwd (void)
357 {
358   if (! src_pwd)
359     {
360       src_pwd = getpwd ();
361       if (!src_pwd)
362         src_pwd = ".";
363     }
364
365    return src_pwd;
366 }
367
368 /* Called when the start of a function definition is parsed,
369    this function prints on stderr the name of the function.  */
370 void
371 announce_function (tree decl)
372 {
373   if (!quiet_flag)
374     {
375       if (rtl_dump_and_exit)
376         fprintf (stderr, "%s ",
377                  identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))));
378       else
379         fprintf (stderr, " %s",
380                  identifier_to_locale (lang_hooks.decl_printable_name (decl, 2)));
381       fflush (stderr);
382       pp_needs_newline (global_dc->printer) = true;
383       diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
384     }
385 }
386
387 /* Initialize local_tick with the time of day, or -1 if
388    flag_random_seed is set.  */
389
390 static void
391 init_local_tick (void)
392 {
393   if (!flag_random_seed)
394     {
395       /* Get some more or less random data.  */
396 #ifdef HAVE_GETTIMEOFDAY
397       {
398         struct timeval tv;
399
400         gettimeofday (&tv, NULL);
401         local_tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
402       }
403 #else
404       {
405         time_t now = time (NULL);
406
407         if (now != (time_t)-1)
408           local_tick = (unsigned) now;
409       }
410 #endif
411     }
412   else
413     local_tick = -1;
414 }
415
416 /* Set up a default flag_random_seed and local_tick, unless the user
417    already specified one.  Must be called after init_local_tick.  */
418
419 static void
420 init_random_seed (void)
421 {
422   unsigned HOST_WIDE_INT value;
423   static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
424
425   value = local_tick ^ getpid ();
426
427   sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
428   flag_random_seed = random_seed;
429 }
430
431 /* Obtain the random_seed string.  Unless NOINIT, initialize it if
432    it's not provided in the command line.  */
433
434 const char *
435 get_random_seed (bool noinit)
436 {
437   if (!flag_random_seed && !noinit)
438     init_random_seed ();
439   return flag_random_seed;
440 }
441
442 /* Modify the random_seed string to VAL.  Return its previous
443    value.  */
444
445 const char *
446 set_random_seed (const char *val)
447 {
448   const char *old = flag_random_seed;
449   flag_random_seed = val;
450   return old;
451 }
452
453 #if GCC_VERSION < 3004
454
455 /* The functions clz_hwi, ctz_hwi, ffs_hwi, floor_log2 and exact_log2
456    are defined as inline functions in toplev.h if GCC_VERSION >= 3004.
457    The definitions here are used for older versions of gcc.  */
458
459 /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
460    If X is 0, return -1.  */
461
462 int
463 floor_log2 (unsigned HOST_WIDE_INT x)
464 {
465   int t = 0;
466
467   if (x == 0)
468     return -1;
469
470   if (HOST_BITS_PER_WIDE_INT > 64)
471     if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
472       t += 64;
473   if (HOST_BITS_PER_WIDE_INT > 32)
474     if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
475       t += 32;
476   if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
477     t += 16;
478   if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
479     t += 8;
480   if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
481     t += 4;
482   if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
483     t += 2;
484   if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
485     t += 1;
486
487   return t;
488 }
489
490 /* Return the logarithm of X, base 2, considering X unsigned,
491    if X is a power of 2.  Otherwise, returns -1.  */
492
493 int
494 exact_log2 (unsigned HOST_WIDE_INT x)
495 {
496   if (x != (x & -x))
497     return -1;
498   return floor_log2 (x);
499 }
500
501 /* Given X, an unsigned number, return the number of least significant bits
502    that are zero.  When X == 0, the result is the word size.  */
503
504 int
505 ctz_hwi (unsigned HOST_WIDE_INT x)
506 {
507   return x ? floor_log2 (x & -x) : HOST_BITS_PER_WIDE_INT;
508 }
509
510 /* Similarly for most significant bits.  */
511
512 int
513 clz_hwi (unsigned HOST_WIDE_INT x)
514 {
515   return HOST_BITS_PER_WIDE_INT - 1 - floor_log2(x);
516 }
517
518 /* Similar to ctz_hwi, except that the least significant bit is numbered
519    starting from 1, and X == 0 yields 0.  */
520
521 int
522 ffs_hwi (unsigned HOST_WIDE_INT x)
523 {
524   return 1 + floor_log2 (x & -x);
525 }
526
527 #endif /* GCC_VERSION < 3004 */
528
529 /* Handler for fatal signals, such as SIGSEGV.  These are transformed
530    into ICE messages, which is much more user friendly.  In case the
531    error printer crashes, reset the signal to prevent infinite recursion.  */
532
533 static void
534 crash_signal (int signo)
535 {
536   signal (signo, SIG_DFL);
537
538   /* If we crashed while processing an ASM statement, then be a little more
539      graceful.  It's most likely the user's fault.  */
540   if (this_is_asm_operands)
541     {
542       output_operand_lossage ("unrecoverable error");
543       exit (FATAL_EXIT_CODE);
544     }
545
546   internal_error ("%s", strsignal (signo));
547 }
548
549 /* Arrange to dump core on error.  (The regular error message is still
550    printed first, except in the case of abort().)  */
551
552 static void
553 setup_core_dumping (void)
554 {
555 #ifdef SIGABRT
556   signal (SIGABRT, SIG_DFL);
557 #endif
558 #if defined(HAVE_SETRLIMIT)
559   {
560     struct rlimit rlim;
561     if (getrlimit (RLIMIT_CORE, &rlim) != 0)
562       fatal_error ("getting core file size maximum limit: %m");
563     rlim.rlim_cur = rlim.rlim_max;
564     if (setrlimit (RLIMIT_CORE, &rlim) != 0)
565       fatal_error ("setting core file size limit to maximum: %m");
566   }
567 #endif
568   diagnostic_abort_on_error (global_dc);
569 }
570
571
572 /* Strip off a legitimate source ending from the input string NAME of
573    length LEN.  Rather than having to know the names used by all of
574    our front ends, we strip off an ending of a period followed by
575    up to five characters.  (Java uses ".class".)  */
576
577 void
578 strip_off_ending (char *name, int len)
579 {
580   int i;
581   for (i = 2; i < 6 && len > i; i++)
582     {
583       if (name[len - i] == '.')
584         {
585           name[len - i] = '\0';
586           break;
587         }
588     }
589 }
590
591 /* Output a quoted string.  */
592
593 void
594 output_quoted_string (FILE *asm_file, const char *string)
595 {
596 #ifdef OUTPUT_QUOTED_STRING
597   OUTPUT_QUOTED_STRING (asm_file, string);
598 #else
599   char c;
600
601   putc ('\"', asm_file);
602   while ((c = *string++) != 0)
603     {
604       if (ISPRINT (c))
605         {
606           if (c == '\"' || c == '\\')
607             putc ('\\', asm_file);
608           putc (c, asm_file);
609         }
610       else
611         fprintf (asm_file, "\\%03o", (unsigned char) c);
612     }
613   putc ('\"', asm_file);
614 #endif
615 }
616
617 /* A subroutine of wrapup_global_declarations.  We've come to the end of
618    the compilation unit.  All deferred variables should be undeferred,
619    and all incomplete decls should be finalized.  */
620
621 void
622 wrapup_global_declaration_1 (tree decl)
623 {
624   /* We're not deferring this any longer.  Assignment is conditional to
625      avoid needlessly dirtying PCH pages.  */
626   if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
627       && DECL_DEFER_OUTPUT (decl) != 0)
628     DECL_DEFER_OUTPUT (decl) = 0;
629
630   if (TREE_CODE (decl) == VAR_DECL && DECL_SIZE (decl) == 0)
631     lang_hooks.finish_incomplete_decl (decl);
632 }
633
634 /* A subroutine of wrapup_global_declarations.  Decide whether or not DECL
635    needs to be output.  Return true if it is output.  */
636
637 bool
638 wrapup_global_declaration_2 (tree decl)
639 {
640   if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl))
641     return false;
642
643   /* Don't write out static consts, unless we still need them.
644
645      We also keep static consts if not optimizing (for debugging),
646      unless the user specified -fno-keep-static-consts.
647      ??? They might be better written into the debug information.
648      This is possible when using DWARF.
649
650      A language processor that wants static constants to be always
651      written out (even if it is not used) is responsible for
652      calling rest_of_decl_compilation itself.  E.g. the C front-end
653      calls rest_of_decl_compilation from finish_decl.
654      One motivation for this is that is conventional in some
655      environments to write things like:
656      static const char rcsid[] = "... version string ...";
657      intending to force the string to be in the executable.
658
659      A language processor that would prefer to have unneeded
660      static constants "optimized away" would just defer writing
661      them out until here.  E.g. C++ does this, because static
662      constants are often defined in header files.
663
664      ??? A tempting alternative (for both C and C++) would be
665      to force a constant to be written if and only if it is
666      defined in a main file, as opposed to an include file.  */
667
668   if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
669     {
670       struct varpool_node *node;
671       bool needed = true;
672       node = varpool_get_node (decl);
673
674       if (!node && flag_ltrans)
675         needed = false;
676       else if (node && node->finalized)
677         needed = false;
678       else if (node && node->alias)
679         needed = false;
680       else if (!cgraph_global_info_ready
681                && (TREE_USED (decl)
682                    || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
683         /* needed */;
684       else if (node && node->needed)
685         /* needed */;
686       else if (DECL_COMDAT (decl))
687         needed = false;
688       else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
689                && (optimize || !flag_keep_static_consts
690                    || DECL_ARTIFICIAL (decl)))
691         needed = false;
692
693       if (needed)
694         {
695           rest_of_decl_compilation (decl, 1, 1);
696           return true;
697         }
698     }
699
700   return false;
701 }
702
703 /* Do any final processing required for the declarations in VEC, of
704    which there are LEN.  We write out inline functions and variables
705    that have been deferred until this point, but which are required.
706    Returns nonzero if anything was put out.  */
707
708 bool
709 wrapup_global_declarations (tree *vec, int len)
710 {
711   bool reconsider, output_something = false;
712   int i;
713
714   for (i = 0; i < len; i++)
715     wrapup_global_declaration_1 (vec[i]);
716
717   /* Now emit any global variables or functions that we have been
718      putting off.  We need to loop in case one of the things emitted
719      here references another one which comes earlier in the list.  */
720   do
721     {
722       reconsider = false;
723       for (i = 0; i < len; i++)
724         reconsider |= wrapup_global_declaration_2 (vec[i]);
725       if (reconsider)
726         output_something = true;
727     }
728   while (reconsider);
729
730   return output_something;
731 }
732
733 /* A subroutine of check_global_declarations.  Issue appropriate warnings
734    for the global declaration DECL.  */
735
736 void
737 check_global_declaration_1 (tree decl)
738 {
739   /* Warn about any function declared static but not defined.  We don't
740      warn about variables, because many programs have static variables
741      that exist only to get some text into the object file.  */
742   if (TREE_CODE (decl) == FUNCTION_DECL
743       && DECL_INITIAL (decl) == 0
744       && DECL_EXTERNAL (decl)
745       && ! DECL_ARTIFICIAL (decl)
746       && ! TREE_NO_WARNING (decl)
747       && ! TREE_PUBLIC (decl)
748       && (warn_unused_function
749           || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
750     {
751       if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
752         pedwarn (input_location, 0, "%q+F used but never defined", decl);
753       else
754         warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
755       /* This symbol is effectively an "extern" declaration now.  */
756       TREE_PUBLIC (decl) = 1;
757       assemble_external (decl);
758     }
759
760   /* Warn about static fns or vars defined but not used.  */
761   if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
762        /* We don't warn about "static const" variables because the
763           "rcs_id" idiom uses that construction.  */
764        || (warn_unused_variable
765            && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
766       && ! DECL_IN_SYSTEM_HEADER (decl)
767       && ! TREE_USED (decl)
768       /* The TREE_USED bit for file-scope decls is kept in the identifier,
769          to handle multiple external decls in different scopes.  */
770       && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
771       && ! DECL_EXTERNAL (decl)
772       && ! TREE_PUBLIC (decl)
773       /* A volatile variable might be used in some non-obvious way.  */
774       && ! TREE_THIS_VOLATILE (decl)
775       /* Global register variables must be declared to reserve them.  */
776       && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
777       /* Otherwise, ask the language.  */
778       && lang_hooks.decls.warn_unused_global (decl))
779     warning ((TREE_CODE (decl) == FUNCTION_DECL)
780              ? OPT_Wunused_function
781              : OPT_Wunused_variable,
782              "%q+D defined but not used", decl);
783 }
784
785 /* Issue appropriate warnings for the global declarations in VEC (of
786    which there are LEN).  */
787
788 void
789 check_global_declarations (tree *vec, int len)
790 {
791   int i;
792
793   for (i = 0; i < len; i++)
794     check_global_declaration_1 (vec[i]);
795 }
796
797 /* Emit debugging information for all global declarations in VEC.  */
798
799 void
800 emit_debug_global_declarations (tree *vec, int len)
801 {
802   int i;
803
804   /* Avoid confusing the debug information machinery when there are errors.  */
805   if (seen_error ())
806     return;
807
808   timevar_push (TV_SYMOUT);
809   for (i = 0; i < len; i++)
810     debug_hooks->global_decl (vec[i]);
811   timevar_pop (TV_SYMOUT);
812 }
813
814 /* Warn about a use of an identifier which was marked deprecated.  */
815 void
816 warn_deprecated_use (tree node, tree attr)
817 {
818   const char *msg;
819
820   if (node == 0 || !warn_deprecated_decl)
821     return;
822
823   if (!attr)
824     {
825       if (DECL_P (node))
826         attr = DECL_ATTRIBUTES (node);
827       else if (TYPE_P (node))
828         {
829           tree decl = TYPE_STUB_DECL (node);
830           if (decl)
831             attr = lookup_attribute ("deprecated",
832                                      TYPE_ATTRIBUTES (TREE_TYPE (decl)));
833         }
834     }
835
836   if (attr)
837     attr = lookup_attribute ("deprecated", attr);
838
839   if (attr)
840     msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
841   else
842     msg = NULL;
843
844   if (DECL_P (node))
845     {
846       expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
847       if (msg)
848         warning (OPT_Wdeprecated_declarations,
849                  "%qD is deprecated (declared at %s:%d): %s",
850                  node, xloc.file, xloc.line, msg);
851       else
852         warning (OPT_Wdeprecated_declarations,
853                  "%qD is deprecated (declared at %s:%d)",
854                  node, xloc.file, xloc.line);
855     }
856   else if (TYPE_P (node))
857     {
858       tree what = NULL_TREE;
859       tree decl = TYPE_STUB_DECL (node);
860
861       if (TYPE_NAME (node))
862         {
863           if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
864             what = TYPE_NAME (node);
865           else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
866                    && DECL_NAME (TYPE_NAME (node)))
867             what = DECL_NAME (TYPE_NAME (node));
868         }
869
870       if (decl)
871         {
872           expanded_location xloc
873             = expand_location (DECL_SOURCE_LOCATION (decl));
874           if (what)
875             {
876               if (msg)
877                 warning (OPT_Wdeprecated_declarations,
878                          "%qE is deprecated (declared at %s:%d): %s",
879                          what, xloc.file, xloc.line, msg);
880               else
881                 warning (OPT_Wdeprecated_declarations,
882                          "%qE is deprecated (declared at %s:%d)", what,
883                          xloc.file, xloc.line);
884             }
885           else
886             {
887               if (msg)
888                 warning (OPT_Wdeprecated_declarations,
889                          "type is deprecated (declared at %s:%d): %s",
890                          xloc.file, xloc.line, msg);
891               else
892                 warning (OPT_Wdeprecated_declarations,
893                          "type is deprecated (declared at %s:%d)",
894                          xloc.file, xloc.line);
895             }
896         }
897       else
898         {
899           if (what)
900             {
901               if (msg)
902                 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
903                          what, msg);
904               else
905                 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
906             }
907           else
908             {
909               if (msg)
910                 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
911                          msg);
912               else
913                 warning (OPT_Wdeprecated_declarations, "type is deprecated");
914             }
915         }
916     }
917 }
918
919 /* Compile an entire translation unit.  Write a file of assembly
920    output and various debugging dumps.  */
921
922 static void
923 compile_file (void)
924 {
925   /* Initialize yet another pass.  */
926
927   ggc_protect_identifiers = true;
928
929   init_cgraph ();
930   init_final (main_input_filename);
931   coverage_init (aux_base_name);
932   statistics_init ();
933   invoke_plugin_callbacks (PLUGIN_START_UNIT, NULL);
934
935   timevar_push (TV_PARSE);
936
937   /* Call the parser, which parses the entire file (calling
938      rest_of_compilation for each function).  */
939   lang_hooks.parse_file (set_yydebug);
940
941   /* Compilation is now finished except for writing
942      what's left of the symbol table output.  */
943   timevar_pop (TV_PARSE);
944
945   if (flag_syntax_only || flag_wpa)
946     return;
947
948   ggc_protect_identifiers = false;
949
950   /* This must also call cgraph_finalize_compilation_unit.  */
951   lang_hooks.decls.final_write_globals ();
952
953   if (seen_error ())
954     return;
955
956   varpool_assemble_pending_decls ();
957   finish_aliases_2 ();
958
959   /* Likewise for mudflap static object registrations.  */
960   if (flag_mudflap)
961     mudflap_finish_file ();
962
963   output_shared_constant_pool ();
964   output_object_blocks ();
965
966   /* Write out any pending weak symbol declarations.  */
967   weak_finish ();
968
969   /* This must be at the end before unwind and debug info.
970      Some target ports emit PIC setup thunks here.  */
971   targetm.asm_out.code_end ();
972
973   /* Do dbx symbols.  */
974   timevar_push (TV_SYMOUT);
975
976 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
977   if (dwarf2out_do_frame ())
978     dwarf2out_frame_finish ();
979 #endif
980
981   (*debug_hooks->finish) (main_input_filename);
982   timevar_pop (TV_SYMOUT);
983
984   /* Output some stuff at end of file if nec.  */
985
986   dw2_output_indirect_constants ();
987
988   /* Flush any pending external directives.  */
989   process_pending_assemble_externals ();
990
991   /* Emit LTO marker if LTO info has been previously emitted.  This is
992      used by collect2 to determine whether an object file contains IL.
993      We used to emit an undefined reference here, but this produces
994      link errors if an object file with IL is stored into a shared
995      library without invoking lto1.  */
996   if (flag_generate_lto)
997     {
998 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
999       ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, NULL_TREE,
1000                                       "__gnu_lto_v1",
1001                                       (unsigned HOST_WIDE_INT) 1, 8);
1002 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1003       ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, "__gnu_lto_v1",
1004                                  (unsigned HOST_WIDE_INT) 1, 8);
1005 #else
1006       ASM_OUTPUT_COMMON (asm_out_file, "__gnu_lto_v1",
1007                          (unsigned HOST_WIDE_INT) 1,
1008                          (unsigned HOST_WIDE_INT) 1);
1009 #endif
1010     }
1011
1012   /* Attach a special .ident directive to the end of the file to identify
1013      the version of GCC which compiled this code.  The format of the .ident
1014      string is patterned after the ones produced by native SVR4 compilers.  */
1015 #ifdef IDENT_ASM_OP
1016   if (!flag_no_ident)
1017     {
1018       const char *pkg_version = "(GNU) ";
1019
1020       if (strcmp ("(GCC) ", pkgversion_string))
1021         pkg_version = pkgversion_string;
1022       fprintf (asm_out_file, "%s\"GCC: %s%s\"\n",
1023                IDENT_ASM_OP, pkg_version, version_string);
1024     }
1025 #endif
1026
1027   /* Invoke registered plugin callbacks.  */
1028   invoke_plugin_callbacks (PLUGIN_FINISH_UNIT, NULL);
1029
1030   /* This must be at the end.  Some target ports emit end of file directives
1031      into the assembly file here, and hence we can not output anything to the
1032      assembly file after this point.  */
1033   targetm.asm_out.file_end ();
1034 }
1035
1036 /* Parse a -d... command line switch.  */
1037
1038 void
1039 decode_d_option (const char *arg)
1040 {
1041   int c;
1042
1043   while (*arg)
1044     switch (c = *arg++)
1045       {
1046       case 'A':
1047         flag_debug_asm = 1;
1048         break;
1049       case 'p':
1050         flag_print_asm_name = 1;
1051         break;
1052       case 'P':
1053         flag_dump_rtl_in_asm = 1;
1054         flag_print_asm_name = 1;
1055         break;
1056       case 'v':
1057         graph_dump_format = vcg;
1058         break;
1059       case 'x':
1060         rtl_dump_and_exit = 1;
1061         break;
1062       case 'y':
1063         set_yydebug = 1;
1064         break;
1065       case 'D': /* These are handled by the preprocessor.  */
1066       case 'I':
1067       case 'M':
1068       case 'N':
1069       case 'U':
1070         break;
1071       case 'H':
1072         setup_core_dumping();
1073         break;
1074       case 'a':
1075         enable_rtl_dump_file ();
1076         break;
1077
1078       default:
1079           warning (0, "unrecognized gcc debugging option: %c", c);
1080         break;
1081       }
1082 }
1083
1084 /* Indexed by enum debug_info_type.  */
1085 const char *const debug_type_names[] =
1086 {
1087   "none", "stabs", "coff", "dwarf-2", "xcoff", "vms"
1088 };
1089
1090 /* Print version information to FILE.
1091    Each line begins with INDENT (for the case where FILE is the
1092    assembler output file).  */
1093
1094 void
1095 print_version (FILE *file, const char *indent)
1096 {
1097   static const char fmt1[] =
1098 #ifdef __GNUC__
1099     N_("%s%s%s %sversion %s (%s)\n%s\tcompiled by GNU C version %s, ")
1100 #else
1101     N_("%s%s%s %sversion %s (%s) compiled by CC, ")
1102 #endif
1103     ;
1104   static const char fmt2[] =
1105     N_("GMP version %s, MPFR version %s, MPC version %s\n");
1106   static const char fmt3[] =
1107     N_("%s%swarning: %s header version %s differs from library version %s.\n");
1108   static const char fmt4[] =
1109     N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
1110 #ifndef __VERSION__
1111 #define __VERSION__ "[?]"
1112 #endif
1113   fprintf (file,
1114            file == stderr ? _(fmt1) : fmt1,
1115            indent, *indent != 0 ? " " : "",
1116            lang_hooks.name, pkgversion_string, version_string, TARGET_NAME,
1117            indent, __VERSION__);
1118
1119   /* We need to stringify the GMP macro values.  Ugh, gmp_version has
1120      two string formats, "i.j.k" and "i.j" when k is zero.  As of
1121      gmp-4.3.0, GMP always uses the 3 number format.  */
1122 #define GCC_GMP_STRINGIFY_VERSION3(X) #X
1123 #define GCC_GMP_STRINGIFY_VERSION2(X) GCC_GMP_STRINGIFY_VERSION3(X)
1124 #define GCC_GMP_VERSION_NUM(X,Y,Z) (((X) << 16L) | ((Y) << 8) | (Z))
1125 #define GCC_GMP_VERSION \
1126   GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
1127 #if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4,3,0) && __GNU_MP_VERSION_PATCHLEVEL == 0
1128 #define GCC_GMP_STRINGIFY_VERSION GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION) "." \
1129   GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_MINOR)
1130 #else
1131 #define GCC_GMP_STRINGIFY_VERSION GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION) "." \
1132   GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_MINOR) "." \
1133   GCC_GMP_STRINGIFY_VERSION2(__GNU_MP_VERSION_PATCHLEVEL)
1134 #endif
1135   fprintf (file,
1136            file == stderr ? _(fmt2) : fmt2,
1137            GCC_GMP_STRINGIFY_VERSION, MPFR_VERSION_STRING, MPC_VERSION_STRING);
1138   if (strcmp (GCC_GMP_STRINGIFY_VERSION, gmp_version))
1139     fprintf (file,
1140              file == stderr ? _(fmt3) : fmt3,
1141              indent, *indent != 0 ? " " : "",
1142              "GMP", GCC_GMP_STRINGIFY_VERSION, gmp_version);
1143   if (strcmp (MPFR_VERSION_STRING, mpfr_get_version ()))
1144     fprintf (file,
1145              file == stderr ? _(fmt3) : fmt3,
1146              indent, *indent != 0 ? " " : "",
1147              "MPFR", MPFR_VERSION_STRING, mpfr_get_version ());
1148   if (strcmp (MPC_VERSION_STRING, mpc_get_version ()))
1149     fprintf (file,
1150              file == stderr ? _(fmt3) : fmt3,
1151              indent, *indent != 0 ? " " : "",
1152              "MPC", MPC_VERSION_STRING, mpc_get_version ());
1153   fprintf (file,
1154            file == stderr ? _(fmt4) : fmt4,
1155            indent, *indent != 0 ? " " : "",
1156            PARAM_VALUE (GGC_MIN_EXPAND), PARAM_VALUE (GGC_MIN_HEAPSIZE));
1157
1158   print_plugins_versions (file, indent);
1159 }
1160
1161 #ifdef ASM_COMMENT_START
1162 static int
1163 print_to_asm_out_file (print_switch_type type, const char * text)
1164 {
1165   bool prepend_sep = true;
1166
1167   switch (type)
1168     {
1169     case SWITCH_TYPE_LINE_END:
1170       putc ('\n', asm_out_file);
1171       return 1;
1172
1173     case SWITCH_TYPE_LINE_START:
1174       fputs (ASM_COMMENT_START, asm_out_file);
1175       return strlen (ASM_COMMENT_START);
1176
1177     case SWITCH_TYPE_DESCRIPTIVE:
1178       if (ASM_COMMENT_START[0] == 0)
1179         prepend_sep = false;
1180       /* Drop through.  */
1181     case SWITCH_TYPE_PASSED:
1182     case SWITCH_TYPE_ENABLED:
1183       if (prepend_sep)
1184         fputc (' ', asm_out_file);
1185       fputs (text, asm_out_file);
1186       /* No need to return the length here as
1187          print_single_switch has already done it.  */
1188       return 0;
1189
1190     default:
1191       return -1;
1192     }
1193 }
1194 #endif
1195
1196 static int
1197 print_to_stderr (print_switch_type type, const char * text)
1198 {
1199   switch (type)
1200     {
1201     case SWITCH_TYPE_LINE_END:
1202       putc ('\n', stderr);
1203       return 1;
1204
1205     case SWITCH_TYPE_LINE_START:
1206       return 0;
1207
1208     case SWITCH_TYPE_PASSED:
1209     case SWITCH_TYPE_ENABLED:
1210       fputc (' ', stderr);
1211       /* Drop through.  */
1212
1213     case SWITCH_TYPE_DESCRIPTIVE:
1214       fputs (text, stderr);
1215       /* No need to return the length here as
1216          print_single_switch has already done it.  */
1217       return 0;
1218
1219     default:
1220       return -1;
1221     }
1222 }
1223
1224 /* Print an option value and return the adjusted position in the line.
1225    ??? print_fn doesn't handle errors, eg disk full; presumably other
1226    code will catch a disk full though.  */
1227
1228 static int
1229 print_single_switch (print_switch_fn_type print_fn,
1230                      int pos,
1231                      print_switch_type type,
1232                      const char * text)
1233 {
1234   /* The ultrix fprintf returns 0 on success, so compute the result
1235      we want here since we need it for the following test.  The +1
1236      is for the separator character that will probably be emitted.  */
1237   int len = strlen (text) + 1;
1238
1239   if (pos != 0
1240       && pos + len > MAX_LINE)
1241     {
1242       print_fn (SWITCH_TYPE_LINE_END, NULL);
1243       pos = 0;
1244     }
1245
1246   if (pos == 0)
1247     pos += print_fn (SWITCH_TYPE_LINE_START, NULL);
1248
1249   print_fn (type, text);
1250   return pos + len;
1251 }
1252
1253 /* Print active target switches using PRINT_FN.
1254    POS is the current cursor position and MAX is the size of a "line".
1255    Each line begins with INDENT and ends with TERM.
1256    Each switch is separated from the next by SEP.  */
1257
1258 static void
1259 print_switch_values (print_switch_fn_type print_fn)
1260 {
1261   int pos = 0;
1262   size_t j;
1263
1264   /* Fill in the -frandom-seed option, if the user didn't pass it, so
1265      that it can be printed below.  This helps reproducibility.  */
1266   if (!flag_random_seed)
1267     init_random_seed ();
1268
1269   /* Print the options as passed.  */
1270   pos = print_single_switch (print_fn, pos,
1271                              SWITCH_TYPE_DESCRIPTIVE, _("options passed: "));
1272
1273   for (j = 1; j < save_decoded_options_count; j++)
1274     {
1275       switch (save_decoded_options[j].opt_index)
1276         {
1277         case OPT_o:
1278         case OPT_d:
1279         case OPT_dumpbase:
1280         case OPT_dumpdir:
1281         case OPT_auxbase:
1282         case OPT_quiet:
1283         case OPT_version:
1284           /* Ignore these.  */
1285           continue;
1286         }
1287
1288       pos = print_single_switch (print_fn, pos, SWITCH_TYPE_PASSED,
1289                                  save_decoded_options[j].orig_option_with_args_text);
1290     }
1291
1292   if (pos > 0)
1293     print_fn (SWITCH_TYPE_LINE_END, NULL);
1294
1295   /* Print the -f and -m options that have been enabled.
1296      We don't handle language specific options but printing argv
1297      should suffice.  */
1298   pos = print_single_switch (print_fn, 0,
1299                              SWITCH_TYPE_DESCRIPTIVE, _("options enabled: "));
1300
1301   for (j = 0; j < cl_options_count; j++)
1302     if ((cl_options[j].flags & CL_REPORT)
1303         && option_enabled (j, &global_options) > 0)
1304       pos = print_single_switch (print_fn, pos,
1305                                  SWITCH_TYPE_ENABLED, cl_options[j].opt_text);
1306
1307   print_fn (SWITCH_TYPE_LINE_END, NULL);
1308 }
1309
1310 /* Open assembly code output file.  Do this even if -fsyntax-only is
1311    on, because then the driver will have provided the name of a
1312    temporary file or bit bucket for us.  NAME is the file specified on
1313    the command line, possibly NULL.  */
1314 static void
1315 init_asm_output (const char *name)
1316 {
1317   if (name == NULL && asm_file_name == 0)
1318     asm_out_file = stdout;
1319   else
1320     {
1321       if (asm_file_name == 0)
1322         {
1323           int len = strlen (dump_base_name);
1324           char *dumpname = XNEWVEC (char, len + 6);
1325
1326           memcpy (dumpname, dump_base_name, len + 1);
1327           strip_off_ending (dumpname, len);
1328           strcat (dumpname, ".s");
1329           asm_file_name = dumpname;
1330         }
1331       if (!strcmp (asm_file_name, "-"))
1332         asm_out_file = stdout;
1333       else
1334         asm_out_file = fopen (asm_file_name, "w+b");
1335       if (asm_out_file == 0)
1336         fatal_error ("can%'t open %s for writing: %m", asm_file_name);
1337     }
1338
1339   if (!flag_syntax_only)
1340     {
1341       targetm.asm_out.file_start ();
1342
1343       if (flag_record_gcc_switches)
1344         {
1345           if (targetm.asm_out.record_gcc_switches)
1346             {
1347               /* Let the target know that we are about to start recording.  */
1348               targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1349                                                    NULL);
1350               /* Now record the switches.  */
1351               print_switch_values (targetm.asm_out.record_gcc_switches);
1352               /* Let the target know that the recording is over.  */
1353               targetm.asm_out.record_gcc_switches (SWITCH_TYPE_DESCRIPTIVE,
1354                                                    NULL);
1355             }
1356           else
1357             inform (input_location, "-frecord-gcc-switches is not supported by the current target");
1358         }
1359
1360 #ifdef ASM_COMMENT_START
1361       if (flag_verbose_asm)
1362         {
1363           /* Print the list of switches in effect
1364              into the assembler file as comments.  */
1365           print_version (asm_out_file, ASM_COMMENT_START);
1366           print_switch_values (print_to_asm_out_file);
1367           putc ('\n', asm_out_file);
1368         }
1369 #endif
1370     }
1371 }
1372
1373 /* Return true if the state of option OPTION should be stored in PCH files
1374    and checked by default_pch_valid_p.  Store the option's current state
1375    in STATE if so.  */
1376
1377 static inline bool
1378 option_affects_pch_p (int option, struct cl_option_state *state)
1379 {
1380   if ((cl_options[option].flags & CL_TARGET) == 0)
1381     return false;
1382   if (option_flag_var (option, &global_options) == &target_flags)
1383     if (targetm.check_pch_target_flags)
1384       return false;
1385   return get_option_state (&global_options, option, state);
1386 }
1387
1388 /* Default version of get_pch_validity.
1389    By default, every flag difference is fatal; that will be mostly right for
1390    most targets, but completely right for very few.  */
1391
1392 void *
1393 default_get_pch_validity (size_t *sz)
1394 {
1395   struct cl_option_state state;
1396   size_t i;
1397   char *result, *r;
1398
1399   *sz = 2;
1400   if (targetm.check_pch_target_flags)
1401     *sz += sizeof (target_flags);
1402   for (i = 0; i < cl_options_count; i++)
1403     if (option_affects_pch_p (i, &state))
1404       *sz += state.size;
1405
1406   result = r = XNEWVEC (char, *sz);
1407   r[0] = flag_pic;
1408   r[1] = flag_pie;
1409   r += 2;
1410   if (targetm.check_pch_target_flags)
1411     {
1412       memcpy (r, &target_flags, sizeof (target_flags));
1413       r += sizeof (target_flags);
1414     }
1415
1416   for (i = 0; i < cl_options_count; i++)
1417     if (option_affects_pch_p (i, &state))
1418       {
1419         memcpy (r, state.data, state.size);
1420         r += state.size;
1421       }
1422
1423   return result;
1424 }
1425
1426 /* Return a message which says that a PCH file was created with a different
1427    setting of OPTION.  */
1428
1429 static const char *
1430 pch_option_mismatch (const char *option)
1431 {
1432   char *r;
1433
1434   asprintf (&r, _("created and used with differing settings of '%s'"), option);
1435   if (r == NULL)
1436     return _("out of memory");
1437   return r;
1438 }
1439
1440 /* Default version of pch_valid_p.  */
1441
1442 const char *
1443 default_pch_valid_p (const void *data_p, size_t len)
1444 {
1445   struct cl_option_state state;
1446   const char *data = (const char *)data_p;
1447   size_t i;
1448
1449   /* -fpic and -fpie also usually make a PCH invalid.  */
1450   if (data[0] != flag_pic)
1451     return _("created and used with different settings of -fpic");
1452   if (data[1] != flag_pie)
1453     return _("created and used with different settings of -fpie");
1454   data += 2;
1455
1456   /* Check target_flags.  */
1457   if (targetm.check_pch_target_flags)
1458     {
1459       int tf;
1460       const char *r;
1461
1462       memcpy (&tf, data, sizeof (target_flags));
1463       data += sizeof (target_flags);
1464       len -= sizeof (target_flags);
1465       r = targetm.check_pch_target_flags (tf);
1466       if (r != NULL)
1467         return r;
1468     }
1469
1470   for (i = 0; i < cl_options_count; i++)
1471     if (option_affects_pch_p (i, &state))
1472       {
1473         if (memcmp (data, state.data, state.size) != 0)
1474           return pch_option_mismatch (cl_options[i].opt_text);
1475         data += state.size;
1476         len -= state.size;
1477       }
1478
1479   return NULL;
1480 }
1481
1482 /* Default tree printer.   Handles declarations only.  */
1483 bool
1484 default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
1485                       int precision, bool wide, bool set_locus, bool hash)
1486 {
1487   tree t;
1488
1489   /* FUTURE: %+x should set the locus.  */
1490   if (precision != 0 || wide || hash)
1491     return false;
1492
1493   switch (*spec)
1494     {
1495     case 'E':
1496       t = va_arg (*text->args_ptr, tree);
1497       if (TREE_CODE (t) == IDENTIFIER_NODE)
1498         {
1499           pp_identifier (pp, IDENTIFIER_POINTER (t));
1500           return true;
1501         }
1502       break;
1503
1504     case 'D':
1505       t = va_arg (*text->args_ptr, tree);
1506       if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
1507         t = DECL_DEBUG_EXPR (t);
1508       break;
1509
1510     case 'F':
1511     case 'T':
1512       t = va_arg (*text->args_ptr, tree);
1513       break;
1514
1515     case 'K':
1516       percent_K_format (text);
1517       return true;
1518
1519     default:
1520       return false;
1521     }
1522
1523   if (set_locus && text->locus)
1524     *text->locus = DECL_SOURCE_LOCATION (t);
1525
1526   if (DECL_P (t))
1527     {
1528       const char *n = DECL_NAME (t)
1529         ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
1530         : _("<anonymous>");
1531       pp_string (pp, n);
1532     }
1533   else
1534     dump_generic_node (pp, t, 0, TDF_DIAGNOSTIC, 0);
1535
1536   return true;
1537 }
1538
1539 /* A helper function; used as the reallocator function for cpp's line
1540    table.  */
1541 static void *
1542 realloc_for_line_map (void *ptr, size_t len)
1543 {
1544   return GGC_RESIZEVAR (void, ptr, len);
1545 }
1546
1547 /* A helper function: used as the allocator function for
1548    identifier_to_locale.  */
1549 static void *
1550 alloc_for_identifier_to_locale (size_t len)
1551 {
1552   return ggc_alloc_atomic (len);
1553 }
1554
1555 /* Output stack usage information.  */
1556 void
1557 output_stack_usage (void)
1558 {
1559   static bool warning_issued = false;
1560   enum stack_usage_kind_type { STATIC = 0, DYNAMIC, DYNAMIC_BOUNDED };
1561   const char *stack_usage_kind_str[] = {
1562     "static",
1563     "dynamic",
1564     "dynamic,bounded"
1565   };
1566   HOST_WIDE_INT stack_usage = current_function_static_stack_size;
1567   enum stack_usage_kind_type stack_usage_kind;
1568   expanded_location loc;
1569   const char *raw_id, *id;
1570
1571   if (stack_usage < 0)
1572     {
1573       if (!warning_issued)
1574         {
1575           warning (0, "-fstack-usage not supported for this target");
1576           warning_issued = true;
1577         }
1578       return;
1579     }
1580
1581   stack_usage_kind = STATIC;
1582
1583   /* Add the maximum amount of space pushed onto the stack.  */
1584   if (current_function_pushed_stack_size > 0)
1585     {
1586       stack_usage += current_function_pushed_stack_size;
1587       stack_usage_kind = DYNAMIC_BOUNDED;
1588     }
1589
1590   /* Now on to the tricky part: dynamic stack allocation.  */
1591   if (current_function_allocates_dynamic_stack_space)
1592     {
1593       if (current_function_has_unbounded_dynamic_stack_size)
1594         stack_usage_kind = DYNAMIC;
1595       else
1596         stack_usage_kind = DYNAMIC_BOUNDED;
1597
1598       /* Add the size even in the unbounded case, this can't hurt.  */
1599       stack_usage += current_function_dynamic_stack_size;
1600     }
1601
1602   loc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1603
1604   /* Strip the scope prefix if any.  */
1605   raw_id = lang_hooks.decl_printable_name (current_function_decl, 2);
1606   id = strrchr (raw_id, '.');
1607   if (id)
1608     id++;
1609   else
1610     id = raw_id;
1611
1612   fprintf (stack_usage_file,
1613            "%s:%d:%d:%s\t"HOST_WIDE_INT_PRINT_DEC"\t%s\n",
1614            lbasename (loc.file),
1615            loc.line,
1616            loc.column,
1617            id,
1618            stack_usage,
1619            stack_usage_kind_str[stack_usage_kind]);
1620 }
1621
1622 /* Open an auxiliary output file.  */
1623 static FILE *
1624 open_auxiliary_file (const char *ext)
1625 {
1626   char *filename;
1627   FILE *file;
1628
1629   filename = concat (aux_base_name, ".", ext, NULL);
1630   file = fopen (filename, "w");
1631   if (!file)
1632     fatal_error ("can't open %s for writing: %m", filename);
1633   free (filename);
1634   return file;
1635 }
1636
1637 /* Initialization of the front end environment, before command line
1638    options are parsed.  Signal handlers, internationalization etc.
1639    ARGV0 is main's argv[0].  */
1640 static void
1641 general_init (const char *argv0)
1642 {
1643   const char *p;
1644
1645   p = argv0 + strlen (argv0);
1646   while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
1647     --p;
1648   progname = p;
1649
1650   xmalloc_set_program_name (progname);
1651
1652   hex_init ();
1653
1654   /* Unlock the stdio streams.  */
1655   unlock_std_streams ();
1656
1657   gcc_init_libintl ();
1658
1659   identifier_to_locale_alloc = alloc_for_identifier_to_locale;
1660   identifier_to_locale_free = ggc_free;
1661
1662   /* Initialize the diagnostics reporting machinery, so option parsing
1663      can give warnings and errors.  */
1664   diagnostic_initialize (global_dc, N_OPTS);
1665   diagnostic_starter (global_dc) = default_tree_diagnostic_starter;
1666   /* Set a default printer.  Language specific initializations will
1667      override it later.  */
1668   pp_format_decoder (global_dc->printer) = &default_tree_printer;
1669   global_dc->show_option_requested = flag_diagnostics_show_option;
1670   global_dc->show_column = flag_show_column;
1671   global_dc->internal_error = plugins_internal_error_function;
1672   global_dc->option_enabled = option_enabled;
1673   global_dc->option_state = &global_options;
1674   global_dc->option_name = option_name;
1675
1676   /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages.  */
1677 #ifdef SIGSEGV
1678   signal (SIGSEGV, crash_signal);
1679 #endif
1680 #ifdef SIGILL
1681   signal (SIGILL, crash_signal);
1682 #endif
1683 #ifdef SIGBUS
1684   signal (SIGBUS, crash_signal);
1685 #endif
1686 #ifdef SIGABRT
1687   signal (SIGABRT, crash_signal);
1688 #endif
1689 #if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
1690   signal (SIGIOT, crash_signal);
1691 #endif
1692 #ifdef SIGFPE
1693   signal (SIGFPE, crash_signal);
1694 #endif
1695
1696   /* Other host-specific signal setup.  */
1697   (*host_hooks.extra_signals)();
1698
1699   /* Initialize the garbage-collector, string pools and tree type hash
1700      table.  */
1701   init_ggc ();
1702   init_stringpool ();
1703   line_table = ggc_alloc_line_maps ();
1704   linemap_init (line_table);
1705   line_table->reallocator = realloc_for_line_map;
1706   init_ttree ();
1707
1708   /* Initialize register usage now so switches may override.  */
1709   init_reg_sets ();
1710
1711   /* Register the language-independent parameters.  */
1712   add_params (lang_independent_params, LAST_PARAM);
1713
1714   /* This must be done after add_params but before argument processing.  */
1715   init_ggc_heuristics();
1716   init_optimization_passes ();
1717   statistics_early_init ();
1718 }
1719
1720 /* Return true if the current target supports -fsection-anchors.  */
1721
1722 static bool
1723 target_supports_section_anchors_p (void)
1724 {
1725   if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1726     return false;
1727
1728   if (targetm.asm_out.output_anchor == NULL)
1729     return false;
1730
1731   return true;
1732 }
1733
1734 /* Default the align_* variables to 1 if they're still unset, and
1735    set up the align_*_log variables.  */
1736 static void
1737 init_alignments (void)
1738 {
1739   if (align_loops <= 0)
1740     align_loops = 1;
1741   if (align_loops_max_skip > align_loops)
1742     align_loops_max_skip = align_loops - 1;
1743   align_loops_log = floor_log2 (align_loops * 2 - 1);
1744   if (align_jumps <= 0)
1745     align_jumps = 1;
1746   if (align_jumps_max_skip > align_jumps)
1747     align_jumps_max_skip = align_jumps - 1;
1748   align_jumps_log = floor_log2 (align_jumps * 2 - 1);
1749   if (align_labels <= 0)
1750     align_labels = 1;
1751   align_labels_log = floor_log2 (align_labels * 2 - 1);
1752   if (align_labels_max_skip > align_labels)
1753     align_labels_max_skip = align_labels - 1;
1754   if (align_functions <= 0)
1755     align_functions = 1;
1756   align_functions_log = floor_log2 (align_functions * 2 - 1);
1757 }
1758
1759 /* Process the options that have been parsed.  */
1760 static void
1761 process_options (void)
1762 {
1763   /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1764      This can happen with incorrect pre-processed input. */
1765   debug_hooks = &do_nothing_debug_hooks;
1766
1767   /* This replaces set_Wunused.  */
1768   if (warn_unused_function == -1)
1769     warn_unused_function = warn_unused;
1770   if (warn_unused_label == -1)
1771     warn_unused_label = warn_unused;
1772   /* Wunused-parameter is enabled if both -Wunused -Wextra are enabled.  */
1773   if (warn_unused_parameter == -1)
1774     warn_unused_parameter = (warn_unused && extra_warnings);
1775   if (warn_unused_variable == -1)
1776     warn_unused_variable = warn_unused;
1777   /* Wunused-but-set-parameter is enabled if both -Wunused -Wextra are
1778      enabled.  */
1779   if (warn_unused_but_set_parameter == -1)
1780     warn_unused_but_set_parameter = (warn_unused && extra_warnings);
1781   if (warn_unused_but_set_variable == -1)
1782     warn_unused_but_set_variable = warn_unused;
1783   if (warn_unused_value == -1)
1784     warn_unused_value = warn_unused;
1785
1786   /* This replaces set_Wextra.  */
1787   if (warn_uninitialized == -1)
1788     warn_uninitialized = extra_warnings;
1789
1790   /* Allow the front end to perform consistency checks and do further
1791      initialization based on the command line options.  This hook also
1792      sets the original filename if appropriate (e.g. foo.i -> foo.c)
1793      so we can correctly initialize debug output.  */
1794   no_backend = lang_hooks.post_options (&main_input_filename);
1795
1796   /* Some machines may reject certain combinations of options.  */
1797   targetm.target_option.override ();
1798
1799   /* Avoid any informative notes in the second run of -fcompare-debug.  */
1800   if (flag_compare_debug) 
1801     diagnostic_inhibit_notes (global_dc);
1802
1803   if (flag_section_anchors && !target_supports_section_anchors_p ())
1804     {
1805       warning (OPT_fsection_anchors,
1806                "this target does not support %qs", "-fsection-anchors");
1807       flag_section_anchors = 0;
1808     }
1809
1810   if (flag_short_enums == 2)
1811     flag_short_enums = targetm.default_short_enums ();
1812
1813   /* Set aux_base_name if not already set.  */
1814   if (aux_base_name)
1815     ;
1816   else if (main_input_filename)
1817     {
1818       char *name = xstrdup (lbasename (main_input_filename));
1819
1820       strip_off_ending (name, strlen (name));
1821       aux_base_name = name;
1822     }
1823   else
1824     aux_base_name = "gccaux";
1825
1826 #ifndef HAVE_cloog
1827   if (flag_graphite
1828       || flag_loop_block
1829       || flag_loop_interchange
1830       || flag_loop_strip_mine
1831       || flag_graphite_identity
1832       || flag_loop_parallelize_all)
1833     sorry ("Graphite loop optimizations cannot be used");
1834 #endif
1835
1836   /* Unrolling all loops implies that standard loop unrolling must also
1837      be done.  */
1838   if (flag_unroll_all_loops)
1839     flag_unroll_loops = 1;
1840
1841   /* The loop unrolling code assumes that cse will be run after loop.
1842      web and rename-registers also help when run after loop unrolling.  */
1843   if (flag_rerun_cse_after_loop == AUTODETECT_VALUE)
1844     flag_rerun_cse_after_loop = flag_unroll_loops || flag_peel_loops;
1845
1846   if (flag_web == AUTODETECT_VALUE)
1847     flag_web = flag_unroll_loops || flag_peel_loops;
1848
1849   if (flag_rename_registers == AUTODETECT_VALUE)
1850     flag_rename_registers = flag_unroll_loops || flag_peel_loops;
1851
1852   if (flag_non_call_exceptions)
1853     flag_asynchronous_unwind_tables = 1;
1854   if (flag_asynchronous_unwind_tables)
1855     flag_unwind_tables = 1;
1856
1857   if (flag_value_profile_transformations)
1858     flag_profile_values = 1;
1859
1860   /* Warn about options that are not supported on this machine.  */
1861 #ifndef INSN_SCHEDULING
1862   if (flag_schedule_insns || flag_schedule_insns_after_reload)
1863     warning (0, "instruction scheduling not supported on this target machine");
1864 #endif
1865 #ifndef DELAY_SLOTS
1866   if (flag_delayed_branch)
1867     warning (0, "this target machine does not have delayed branches");
1868 #endif
1869
1870   user_label_prefix = USER_LABEL_PREFIX;
1871   if (flag_leading_underscore != -1)
1872     {
1873       /* If the default prefix is more complicated than "" or "_",
1874          issue a warning and ignore this option.  */
1875       if (user_label_prefix[0] == 0 ||
1876           (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
1877         {
1878           user_label_prefix = flag_leading_underscore ? "_" : "";
1879         }
1880       else
1881         warning (0, "-f%sleading-underscore not supported on this target machine",
1882                  flag_leading_underscore ? "" : "no-");
1883     }
1884
1885   /* If we are in verbose mode, write out the version and maybe all the
1886      option flags in use.  */
1887   if (version_flag)
1888     {
1889       print_version (stderr, "");
1890       if (! quiet_flag)
1891         print_switch_values (print_to_stderr);
1892     }
1893
1894   if (flag_syntax_only)
1895     {
1896       write_symbols = NO_DEBUG;
1897       profile_flag = 0;
1898     }
1899
1900   if (flag_gtoggle)
1901     {
1902       if (debug_info_level == DINFO_LEVEL_NONE)
1903         {
1904           debug_info_level = DINFO_LEVEL_NORMAL;
1905
1906           if (write_symbols == NO_DEBUG)
1907             write_symbols = PREFERRED_DEBUGGING_TYPE;
1908         }
1909       else
1910         debug_info_level = DINFO_LEVEL_NONE;
1911     }
1912
1913   if (flag_dump_final_insns && !flag_syntax_only && !no_backend)
1914     {
1915       FILE *final_output = fopen (flag_dump_final_insns, "w");
1916       if (!final_output)
1917         {
1918           error ("could not open final insn dump file %qs: %m",
1919                  flag_dump_final_insns);
1920           flag_dump_final_insns = NULL;
1921         }
1922       else if (fclose (final_output))
1923         {
1924           error ("could not close zeroed insn dump file %qs: %m",
1925                  flag_dump_final_insns);
1926           flag_dump_final_insns = NULL;
1927         }
1928     }
1929
1930   /* Unless over-ridden for the target, assume that all DWARF levels
1931      may be emitted, if DWARF2_DEBUG is selected.  */
1932   if (dwarf_strict < 0)
1933     dwarf_strict = 0;
1934
1935   /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1936      level is 0.  */
1937   if (debug_info_level == DINFO_LEVEL_NONE)
1938     write_symbols = NO_DEBUG;
1939
1940   if (write_symbols == NO_DEBUG)
1941     ;
1942 #if defined(DBX_DEBUGGING_INFO)
1943   else if (write_symbols == DBX_DEBUG)
1944     debug_hooks = &dbx_debug_hooks;
1945 #endif
1946 #if defined(XCOFF_DEBUGGING_INFO)
1947   else if (write_symbols == XCOFF_DEBUG)
1948     debug_hooks = &xcoff_debug_hooks;
1949 #endif
1950 #ifdef SDB_DEBUGGING_INFO
1951   else if (write_symbols == SDB_DEBUG)
1952     debug_hooks = &sdb_debug_hooks;
1953 #endif
1954 #ifdef DWARF2_DEBUGGING_INFO
1955   else if (write_symbols == DWARF2_DEBUG)
1956     debug_hooks = &dwarf2_debug_hooks;
1957 #endif
1958 #ifdef VMS_DEBUGGING_INFO
1959   else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
1960     debug_hooks = &vmsdbg_debug_hooks;
1961 #endif
1962   else
1963     error ("target system does not support the \"%s\" debug format",
1964            debug_type_names[write_symbols]);
1965
1966   /* We know which debug output will be used so we can set flag_var_tracking
1967      and flag_var_tracking_uninit if the user has not specified them.  */
1968   if (debug_info_level < DINFO_LEVEL_NORMAL
1969       || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1970     {
1971       if (flag_var_tracking == 1
1972           || flag_var_tracking_uninit == 1)
1973         {
1974           if (debug_info_level < DINFO_LEVEL_NORMAL)
1975             warning (0, "variable tracking requested, but useless unless "
1976                      "producing debug info");
1977           else
1978             warning (0, "variable tracking requested, but not supported "
1979                      "by this debug format");
1980         }
1981       flag_var_tracking = 0;
1982       flag_var_tracking_uninit = 0;
1983     }
1984
1985   /* If the user specifically requested variable tracking with tagging
1986      uninitialized variables, we need to turn on variable tracking.
1987      (We already determined above that variable tracking is feasible.)  */
1988   if (flag_var_tracking_uninit)
1989     flag_var_tracking = 1;
1990
1991   if (flag_var_tracking == AUTODETECT_VALUE)
1992     flag_var_tracking = optimize >= 1;
1993
1994   if (flag_var_tracking_assignments == AUTODETECT_VALUE)
1995     flag_var_tracking_assignments = flag_var_tracking
1996       && !(flag_selective_scheduling || flag_selective_scheduling2);
1997
1998   if (flag_var_tracking_assignments_toggle)
1999     flag_var_tracking_assignments = !flag_var_tracking_assignments;
2000
2001   if (flag_var_tracking_assignments && !flag_var_tracking)
2002     flag_var_tracking = flag_var_tracking_assignments = -1;
2003
2004   if (flag_var_tracking_assignments
2005       && (flag_selective_scheduling || flag_selective_scheduling2))
2006     warning (0, "var-tracking-assignments changes selective scheduling");
2007
2008   if (flag_tree_cselim == AUTODETECT_VALUE)
2009 #ifdef HAVE_conditional_move
2010     flag_tree_cselim = 1;
2011 #else
2012     flag_tree_cselim = 0;
2013 #endif
2014
2015   /* If auxiliary info generation is desired, open the output file.
2016      This goes in the same directory as the source file--unlike
2017      all the other output files.  */
2018   if (flag_gen_aux_info)
2019     {
2020       aux_info_file = fopen (aux_info_file_name, "w");
2021       if (aux_info_file == 0)
2022         fatal_error ("can%'t open %s: %m", aux_info_file_name);
2023     }
2024
2025   if (! targetm.have_named_sections)
2026     {
2027       if (flag_function_sections)
2028         {
2029           warning (0, "-ffunction-sections not supported for this target");
2030           flag_function_sections = 0;
2031         }
2032       if (flag_data_sections)
2033         {
2034           warning (0, "-fdata-sections not supported for this target");
2035           flag_data_sections = 0;
2036         }
2037     }
2038
2039   if (flag_function_sections && profile_flag)
2040     {
2041       warning (0, "-ffunction-sections disabled; it makes profiling impossible");
2042       flag_function_sections = 0;
2043     }
2044
2045 #ifndef HAVE_prefetch
2046   if (flag_prefetch_loop_arrays > 0)
2047     {
2048       warning (0, "-fprefetch-loop-arrays not supported for this target");
2049       flag_prefetch_loop_arrays = 0;
2050     }
2051 #else
2052   if (flag_prefetch_loop_arrays > 0 && !HAVE_prefetch)
2053     {
2054       warning (0, "-fprefetch-loop-arrays not supported for this target (try -march switches)");
2055       flag_prefetch_loop_arrays = 0;
2056     }
2057 #endif
2058
2059   /* This combination of options isn't handled for i386 targets and doesn't
2060      make much sense anyway, so don't allow it.  */
2061   if (flag_prefetch_loop_arrays > 0 && optimize_size)
2062     {
2063       warning (0, "-fprefetch-loop-arrays is not supported with -Os");
2064       flag_prefetch_loop_arrays = 0;
2065     }
2066
2067   /* The presence of IEEE signaling NaNs, implies all math can trap.  */
2068   if (flag_signaling_nans)
2069     flag_trapping_math = 1;
2070
2071   /* We cannot reassociate if we want traps or signed zeros.  */
2072   if (flag_associative_math && (flag_trapping_math || flag_signed_zeros))
2073     {
2074       warning (0, "-fassociative-math disabled; other options take precedence");
2075       flag_associative_math = 0;
2076     }
2077
2078   /* With -fcx-limited-range, we do cheap and quick complex arithmetic.  */
2079   if (flag_cx_limited_range)
2080     flag_complex_method = 0;
2081
2082   /* With -fcx-fortran-rules, we do something in-between cheap and C99.  */
2083   if (flag_cx_fortran_rules)
2084     flag_complex_method = 1;
2085
2086   /* Targets must be able to place spill slots at lower addresses.  If the
2087      target already uses a soft frame pointer, the transition is trivial.  */
2088   if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
2089     {
2090       warning (0, "-fstack-protector not supported for this target");
2091       flag_stack_protect = 0;
2092     }
2093   if (!flag_stack_protect)
2094     warn_stack_protect = 0;
2095
2096   /* ??? Unwind info is not correct around the CFG unless either a frame
2097      pointer is present or A_O_A is set.  Fixing this requires rewriting
2098      unwind info generation to be aware of the CFG and propagating states
2099      around edges.  */
2100   if (flag_unwind_tables && !ACCUMULATE_OUTGOING_ARGS
2101       && flag_omit_frame_pointer)
2102     {
2103       warning (0, "unwind tables currently require a frame pointer "
2104                "for correctness");
2105       flag_omit_frame_pointer = 0;
2106     }
2107
2108   /* Save the current optimization options.  */
2109   optimization_default_node = build_optimization_node ();
2110   optimization_current_node = optimization_default_node;
2111 }
2112
2113 /* This function can be called multiple times to reinitialize the compiler
2114    back end when register classes or instruction sets have changed,
2115    before each function.  */
2116 static void
2117 backend_init_target (void)
2118 {
2119   /* Initialize alignment variables.  */
2120   init_alignments ();
2121
2122   /* This reinitializes hard_frame_pointer, and calls init_reg_modes_target()
2123      to initialize reg_raw_mode[].  */
2124   init_emit_regs ();
2125
2126   /* This invokes target hooks to set fixed_reg[] etc, which is
2127      mode-dependent.  */
2128   init_regs ();
2129
2130   /* This depends on stack_pointer_rtx.  */
2131   init_fake_stack_mems ();
2132
2133   /* Sets static_base_value[HARD_FRAME_POINTER_REGNUM], which is
2134      mode-dependent.  */
2135   init_alias_target ();
2136
2137   /* Depends on HARD_FRAME_POINTER_REGNUM.  */
2138   init_reload ();
2139
2140   /* The following initialization functions need to generate rtl, so
2141      provide a dummy function context for them.  */
2142   init_dummy_function_start ();
2143
2144   /* rtx_cost is mode-dependent, so cached values need to be recomputed
2145      on a mode change.  */
2146   init_expmed ();
2147
2148   /* We may need to recompute regno_save_code[] and regno_restore_code[]
2149      after a mode change as well.  */
2150   caller_save_initialized_p = false;
2151
2152   expand_dummy_function_end ();
2153 }
2154
2155 /* Initialize the compiler back end.  This function is called only once,
2156    when starting the compiler.  */
2157 static void
2158 backend_init (void)
2159 {
2160   init_emit_once ();
2161
2162   init_rtlanal ();
2163   init_inline_once ();
2164   init_varasm_once ();
2165   save_register_info ();
2166
2167   /* Initialize the target-specific back end pieces.  */
2168   ira_init_once ();
2169   backend_init_target ();
2170 }
2171
2172 /* Initialize excess precision settings.  */
2173 static void
2174 init_excess_precision (void)
2175 {
2176   /* Adjust excess precision handling based on the target options.  If
2177      the front end cannot handle it, flag_excess_precision_cmdline
2178      will already have been set accordingly in the post_options
2179      hook.  */
2180   gcc_assert (flag_excess_precision_cmdline != EXCESS_PRECISION_DEFAULT);
2181   flag_excess_precision = flag_excess_precision_cmdline;
2182   if (flag_unsafe_math_optimizations)
2183     flag_excess_precision = EXCESS_PRECISION_FAST;
2184   if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
2185     {
2186       int flt_eval_method = TARGET_FLT_EVAL_METHOD;
2187       switch (flt_eval_method)
2188         {
2189         case -1:
2190         case 0:
2191           /* Either the target acts unpredictably (-1) or has all the
2192              operations required not to have excess precision (0).  */
2193           flag_excess_precision = EXCESS_PRECISION_FAST;
2194           break;
2195         case 1:
2196         case 2:
2197           /* In these cases, predictable excess precision makes
2198              sense.  */
2199           break;
2200         default:
2201           /* Any other implementation-defined FLT_EVAL_METHOD values
2202              require the compiler to handle the associated excess
2203              precision rules in excess_precision_type.  */
2204           gcc_unreachable ();
2205         }
2206     }
2207 }
2208
2209 /* Initialize things that are both lang-dependent and target-dependent.
2210    This function can be called more than once if target parameters change.  */
2211 static void
2212 lang_dependent_init_target (void)
2213 {
2214   /* This determines excess precision settings.  */
2215   init_excess_precision ();
2216
2217   /* This creates various _DECL nodes, so needs to be called after the
2218      front end is initialized.  It also depends on the HAVE_xxx macros
2219      generated from the target machine description.  */
2220   init_optabs ();
2221
2222   /* The following initialization functions need to generate rtl, so
2223      provide a dummy function context for them.  */
2224   init_dummy_function_start ();
2225
2226   /* Do the target-specific parts of expr initialization.  */
2227   init_expr_target ();
2228
2229   /* Although the actions of these functions are language-independent,
2230      they use optabs, so we cannot call them from backend_init.  */
2231   init_set_costs ();
2232   ira_init ();
2233
2234   expand_dummy_function_end ();
2235 }
2236
2237 /* Language-dependent initialization.  Returns nonzero on success.  */
2238 static int
2239 lang_dependent_init (const char *name)
2240 {
2241   location_t save_loc = input_location;
2242   if (dump_base_name == 0)
2243     dump_base_name = name && name[0] ? name : "gccdump";
2244
2245   /* Other front-end initialization.  */
2246   input_location = BUILTINS_LOCATION;
2247   if (lang_hooks.init () == 0)
2248     return 0;
2249   input_location = save_loc;
2250
2251   init_asm_output (name);
2252
2253   /* If stack usage information is desired, open the output file.  */
2254   if (flag_stack_usage)
2255     stack_usage_file = open_auxiliary_file ("su");
2256
2257   /* This creates various _DECL nodes, so needs to be called after the
2258      front end is initialized.  */
2259   init_eh ();
2260
2261   /* Do the target-specific parts of the initialization.  */
2262   lang_dependent_init_target ();
2263
2264   /* If dbx symbol table desired, initialize writing it and output the
2265      predefined types.  */
2266   timevar_push (TV_SYMOUT);
2267
2268 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
2269   if (dwarf2out_do_frame ())
2270     dwarf2out_frame_init ();
2271 #endif
2272
2273   /* Now we have the correct original filename, we can initialize
2274      debug output.  */
2275   (*debug_hooks->init) (name);
2276
2277   timevar_pop (TV_SYMOUT);
2278
2279   return 1;
2280 }
2281
2282
2283 /* Reinitialize everything when target parameters, such as register usage,
2284    have changed.  */
2285 void
2286 target_reinit (void)
2287 {
2288   /* Reinitialize RTL backend.  */
2289   backend_init_target ();
2290
2291   /* Reinitialize lang-dependent parts.  */
2292   lang_dependent_init_target ();
2293 }
2294
2295 void
2296 dump_memory_report (bool final)
2297 {
2298   ggc_print_statistics ();
2299   stringpool_statistics ();
2300   dump_tree_statistics ();
2301   dump_gimple_statistics ();
2302   dump_rtx_statistics ();
2303   dump_alloc_pool_statistics ();
2304   dump_bitmap_statistics ();
2305   dump_vec_loc_statistics ();
2306   dump_ggc_loc_statistics (final);
2307   dump_alias_stats (stderr);
2308   dump_pta_stats (stderr);
2309 }
2310
2311 /* Clean up: close opened files, etc.  */
2312
2313 static void
2314 finalize (void)
2315 {
2316   /* Close the dump files.  */
2317   if (flag_gen_aux_info)
2318     {
2319       fclose (aux_info_file);
2320       if (seen_error ())
2321         unlink (aux_info_file_name);
2322     }
2323
2324   /* Close non-debugging input and output files.  Take special care to note
2325      whether fclose returns an error, since the pages might still be on the
2326      buffer chain while the file is open.  */
2327
2328   if (asm_out_file)
2329     {
2330       if (ferror (asm_out_file) != 0)
2331         fatal_error ("error writing to %s: %m", asm_file_name);
2332       if (fclose (asm_out_file) != 0)
2333         fatal_error ("error closing %s: %m", asm_file_name);
2334       if (flag_wpa)
2335         unlink_if_ordinary (asm_file_name);
2336     }
2337
2338   if (stack_usage_file)
2339     fclose (stack_usage_file);
2340
2341   statistics_fini ();
2342   finish_optimization_passes ();
2343
2344   ira_finish_once ();
2345
2346   if (mem_report)
2347     dump_memory_report (true);
2348
2349   /* Language-specific end of compilation actions.  */
2350   lang_hooks.finish ();
2351 }
2352
2353 /* Initialize the compiler, and compile the input file.  */
2354 static void
2355 do_compile (void)
2356 {
2357   /* Initialize timing first.  The C front ends read the main file in
2358      the post_options hook, and C++ does file timings.  */
2359   if (time_report || !quiet_flag  || flag_detailed_statistics)
2360     timevar_init ();
2361   timevar_start (TV_TOTAL);
2362
2363   process_options ();
2364
2365   /* Don't do any more if an error has already occurred.  */
2366   if (!seen_error ())
2367     {
2368       /* This must be run always, because it is needed to compute the FP
2369          predefined macros, such as __LDBL_MAX__, for targets using non
2370          default FP formats.  */
2371       init_adjust_machine_modes ();
2372
2373       /* Set up the back-end if requested.  */
2374       if (!no_backend)
2375         backend_init ();
2376
2377       /* Language-dependent initialization.  Returns true on success.  */
2378       if (lang_dependent_init (main_input_filename))
2379         compile_file ();
2380
2381       finalize ();
2382     }
2383
2384   /* Stop timing and print the times.  */
2385   timevar_stop (TV_TOTAL);
2386   timevar_print (stderr);
2387 }
2388
2389 /* Entry point of cc1, cc1plus, jc1, f771, etc.
2390    Exit code is FATAL_EXIT_CODE if can't open files or if there were
2391    any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
2392
2393    It is not safe to call this function more than once.  */
2394
2395 int
2396 toplev_main (int argc, char **argv)
2397 {
2398   expandargv (&argc, &argv);
2399
2400   /* Initialization of GCC's environment, and diagnostics.  */
2401   general_init (argv[0]);
2402
2403   /* Parse the options and do minimal processing; basically just
2404      enough to default flags appropriately.  */
2405   decode_options (argc, CONST_CAST2 (const char **, char **, argv),
2406                   &save_decoded_options, &save_decoded_options_count);
2407
2408   init_local_tick ();
2409
2410   initialize_plugins ();
2411
2412   if (version_flag)
2413     print_version (stderr, "");
2414
2415   if (help_flag)
2416     print_plugins_help (stderr, "");
2417
2418   /* Exit early if we can (e.g. -help).  */
2419   if (!exit_after_options)
2420     do_compile ();
2421
2422   if (warningcount || errorcount)
2423     print_ignored_options ();
2424   diagnostic_finish (global_dc);
2425
2426   /* Invoke registered plugin callbacks if any.  */
2427   invoke_plugin_callbacks (PLUGIN_FINISH, NULL);
2428
2429   finalize_plugins ();
2430   if (seen_error ())
2431     return (FATAL_EXIT_CODE);
2432
2433   return (SUCCESS_EXIT_CODE);
2434 }