OSDN Git Service

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