OSDN Git Service

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