1132734Skan/* Top level of GCC compilers (cc1, cc1plus, etc.)
272566Sobrien   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169699Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4169699Skan   Free Software Foundation, Inc.
518334Speter
690282SobrienThis file is part of GCC.
718334Speter
890282SobrienGCC is free software; you can redistribute it and/or modify it under
990282Sobrienthe terms of the GNU General Public License as published by the Free
1090282SobrienSoftware Foundation; either version 2, or (at your option) any later
1190282Sobrienversion.
1218334Speter
1390282SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1490282SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1590282SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1690282Sobrienfor more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
1990282Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
20169699SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169699Skan02110-1301, USA.  */
2218334Speter
2352558Sobrien/* $FreeBSD$ */
2452558Sobrien
2518334Speter/* This is the top level of cc1/c++.
2618334Speter   It parses command args, opens files, invokes the various passes
2718334Speter   in the proper order, and counts the time used by each.
2818334Speter   Error messages and low-level interface to malloc also handled here.  */
2918334Speter
3018334Speter#include "config.h"
3150615Sobrien#undef FLOAT /* This is for hpux. They should change hpux.  */
3250615Sobrien#undef FFS  /* Some systems define this in param.h.  */
3350615Sobrien#include "system.h"
34132734Skan#include "coretypes.h"
35132734Skan#include "tm.h"
3618334Speter#include <signal.h>
3718334Speter
3850615Sobrien#ifdef HAVE_SYS_RESOURCE_H
3950615Sobrien# include <sys/resource.h>
4018334Speter#endif
4150615Sobrien
4250615Sobrien#ifdef HAVE_SYS_TIMES_H
4350615Sobrien# include <sys/times.h>
4418334Speter#endif
4518334Speter
46169699Skan#include "line-map.h"
4718334Speter#include "input.h"
4818334Speter#include "tree.h"
49169699Skan#include "version.h"
5018334Speter#include "rtl.h"
5190282Sobrien#include "tm_p.h"
5218334Speter#include "flags.h"
5318334Speter#include "insn-attr.h"
5450615Sobrien#include "insn-config.h"
5590282Sobrien#include "insn-flags.h"
5690282Sobrien#include "hard-reg-set.h"
5750615Sobrien#include "recog.h"
5818334Speter#include "output.h"
5950615Sobrien#include "except.h"
6090282Sobrien#include "function.h"
6150615Sobrien#include "toplev.h"
6252558Sobrien#include "expr.h"
6352558Sobrien#include "basic-block.h"
6452558Sobrien#include "intl.h"
6590282Sobrien#include "ggc.h"
6690282Sobrien#include "graph.h"
6790282Sobrien#include "regs.h"
6890282Sobrien#include "timevar.h"
6990282Sobrien#include "diagnostic.h"
7090282Sobrien#include "params.h"
7190282Sobrien#include "reload.h"
7290282Sobrien#include "dwarf2asm.h"
7390282Sobrien#include "integrate.h"
74117414Skan#include "real.h"
7590282Sobrien#include "debug.h"
7690282Sobrien#include "target.h"
7790282Sobrien#include "langhooks.h"
78117414Skan#include "cfglayout.h"
79132734Skan#include "cfgloop.h"
80132734Skan#include "hosthooks.h"
81132734Skan#include "cgraph.h"
82132734Skan#include "opts.h"
83132734Skan#include "coverage.h"
84132734Skan#include "value-prof.h"
85132734Skan#include "alloc-pool.h"
86169699Skan#include "tree-mudflap.h"
8718334Speter
8850615Sobrien#if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
8950615Sobrien#include "dwarf2out.h"
9050615Sobrien#endif
9150615Sobrien
9250615Sobrien#if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
9350615Sobrien#include "dbxout.h"
9450615Sobrien#endif
9550615Sobrien
9650615Sobrien#ifdef SDB_DEBUGGING_INFO
9750615Sobrien#include "sdbout.h"
9850615Sobrien#endif
9950615Sobrien
10018334Speter#ifdef XCOFF_DEBUGGING_INFO
10190282Sobrien#include "xcoffout.h"		/* Needed for external data
10290282Sobrien				   declarations for e.g. AIX 4.x.  */
10318334Speter#endif
104132734Skan
105132734Skanstatic void general_init (const char *);
106132734Skanstatic void do_compile (void);
107132734Skanstatic void process_options (void);
108132734Skanstatic void backend_init (void);
109132734Skanstatic int lang_dependent_init (const char *);
110132734Skanstatic void init_asm_output (const char *);
111132734Skanstatic void finalize (void);
11218334Speter
113132734Skanstatic void crash_signal (int) ATTRIBUTE_NORETURN;
114132734Skanstatic void setup_core_dumping (void);
115132734Skanstatic void compile_file (void);
11650615Sobrien
117132734Skanstatic int print_single_switch (FILE *, int, int, const char *,
118132734Skan				const char *, const char *,
119132734Skan				const char *, const char *);
120132734Skanstatic void print_switch_values (FILE *, int, int, const char *,
121132734Skan				 const char *, const char *);
12218334Speter
123117414Skan/* Nonzero to dump debug info whilst parsing (-dy option).  */
124117414Skanstatic int set_yydebug;
125117414Skan
126132734Skan/* True if we don't need a backend (e.g. preprocessing only).  */
127132734Skanstatic bool no_backend;
128132734Skan
12950615Sobrien/* Length of line when printing switch values.  */
13050615Sobrien#define MAX_LINE 75
13118334Speter
13218334Speter/* Name of program invoked, sans directories.  */
13318334Speter
13490282Sobrienconst char *progname;
13518334Speter
136132734Skan/* Copy of argument vector to toplev_main.  */
137132734Skanstatic const char **save_argv;
13818334Speter
13918334Speter/* Name of top-level original source file (what was input to cpp).
14018334Speter   This comes from the #-command at the beginning of the actual input.
14118334Speter   If there isn't any there, then this is the cc1 input file name.  */
14218334Speter
14390282Sobrienconst char *main_input_filename;
14418334Speter
145169699Skan#ifndef USE_MAPPED_LOCATION
146169699Skanlocation_t unknown_location = { NULL, 0 };
147169699Skan#endif
148169699Skan
149169699Skan/* Used to enable -fvar-tracking, -fweb and -frename-registers according
150169699Skan   to optimize and default_debug_hooks in process_options ().  */
151169699Skan#define AUTODETECT_VALUE 2
152169699Skan
153132734Skan/* Current position in real source file.  */
15418334Speter
155132734Skanlocation_t input_location;
15618334Speter
157169699Skanstruct line_maps line_table;
158169699Skan
15952558Sobrien/* Nonzero if it is unsafe to create any new pseudo registers.  */
16052558Sobrienint no_new_pseudos;
16152558Sobrien
16218334Speter/* Stack of currently pending input files.  */
16318334Speter
16418334Speterstruct file_stack *input_file_stack;
16518334Speter
16618334Speter/* Incremented on each change to input_file_stack.  */
16718334Speterint input_file_stack_tick;
16818334Speter
169169699Skan/* Record of input_file_stack at each tick.  */
170169699Skantypedef struct file_stack *fs_p;
171169699SkanDEF_VEC_P(fs_p);
172169699SkanDEF_VEC_ALLOC_P(fs_p,heap);
173169699Skanstatic VEC(fs_p,heap) *input_file_stack_history;
174169699Skan
175169699Skan/* Whether input_file_stack has been restored to a previous state (in
176169699Skan   which case there should be no more pushing).  */
177169699Skanstatic bool input_file_stack_restored;
178169699Skan
17918334Speter/* Name to use as base of names for dump output files.  */
18018334Speter
18152558Sobrienconst char *dump_base_name;
18218334Speter
183117414Skan/* Name to use as a base for auxiliary output files.  */
184117414Skan
185117414Skanconst char *aux_base_name;
186117414Skan
18718334Speter/* Bit flags that specify the machine subtype we are compiling for.
18818334Speter   Bits are tested using macros TARGET_... defined in the tm.h file
18918334Speter   and set by `-m...' switches.  Must be defined in rtlanal.c.  */
19018334Speter
19118334Speterextern int target_flags;
19218334Speter
193117414Skan/* A mask of target_flags that includes bit X if X was set or cleared
194117414Skan   on the command line.  */
195117414Skan
196117414Skanint target_flags_explicit;
197117414Skan
19890282Sobrien/* Debug hooks - dependent upon command line options.  */
19918334Speter
200132734Skanconst struct gcc_debug_hooks *debug_hooks;
20190282Sobrien
202169699Skan/* Debug hooks - target default.  */
20390282Sobrien
204169699Skanstatic const struct gcc_debug_hooks *default_debug_hooks;
20590282Sobrien
20690282Sobrien/* Other flags saying which kinds of debugging dump have been requested.  */
20790282Sobrien
20890282Sobrienint rtl_dump_and_exit;
20990282Sobrienint flag_print_asm_name;
21052558Sobrienenum graph_dump_types graph_dump_format;
21118334Speter
21218334Speter/* Name for output file of assembly code, specified with -o.  */
21318334Speter
214132734Skanconst char *asm_file_name;
21518334Speter
21618334Speter/* Nonzero means do optimizations.  -O.
21718334Speter   Particular numeric values stand for particular amounts of optimization;
21818334Speter   thus, -O2 stores 2 here.  However, the optimizations beyond the basic
21918334Speter   ones are not controlled directly by this variable.  Instead, they are
22018334Speter   controlled by individual `flag_...' variables that are defaulted
22118334Speter   based on this variable.  */
22218334Speter
22318334Speterint optimize = 0;
22418334Speter
22550615Sobrien/* Nonzero means optimize for size.  -Os.
226117414Skan   The only valid values are zero and nonzero. When optimize_size is
227117414Skan   nonzero, optimize defaults to 2, but certain individual code
22850615Sobrien   bloating optimizations are disabled.  */
22950615Sobrien
23050615Sobrienint optimize_size = 0;
23150615Sobrien
23290282Sobrien/* The FUNCTION_DECL for the function currently being compiled,
23390282Sobrien   or 0 if between functions.  */
23490282Sobrientree current_function_decl;
23518334Speter
236169699Skan/* Set to the FUNC_BEGIN label of the current function, or NULL
23790282Sobrien   if none.  */
238169699Skanconst char * current_function_func_begin_label;
23990282Sobrien
24018334Speter/* Temporarily suppress certain warnings.
24118334Speter   This is set while reading code from a system header file.  */
24218334Speter
24318334Speterint in_system_header = 0;
24418334Speter
245132734Skan/* Nonzero means to collect statistics which might be expensive
24690282Sobrien   and to print them when we are done.  */
24790282Sobrienint flag_detailed_statistics = 0;
24890282Sobrien
249132734Skan/* A random sequence of characters, unless overridden by user.  */
250132734Skanconst char *flag_random_seed;
251132734Skan
252132734Skan/* A local time stamp derived from the time of compilation. It will be
253132734Skan   zero if the system cannot provide a time.  It will be -1u, if the
254132734Skan   user has specified a particular random seed.  */
255132734Skanunsigned local_tick;
256132734Skan
25718334Speter/* -f flags.  */
25818334Speter
25918334Speter/* Nonzero means `char' should be signed.  */
26018334Speter
26118334Speterint flag_signed_char;
26218334Speter
263169699Skan/* Nonzero means give an enum type only as many bytes as it needs.  A value
264169699Skan   of 2 means it has not yet been initialized.  */
26518334Speter
26618334Speterint flag_short_enums;
26718334Speter
26818334Speter/* Nonzero if structures and unions should be returned in memory.
26918334Speter
27018334Speter   This should only be defined if compatibility with another compiler or
27118334Speter   with an ABI is needed, because it results in slower code.  */
27218334Speter
27318334Speter#ifndef DEFAULT_PCC_STRUCT_RETURN
27418334Speter#define DEFAULT_PCC_STRUCT_RETURN 1
27518334Speter#endif
27618334Speter
27718334Speter/* Nonzero for -fpcc-struct-return: return values the same way PCC does.  */
27818334Speter
27918334Speterint flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
28018334Speter
28152558Sobrien/* 0 means straightforward implementation of complex divide acceptable.
28252558Sobrien   1 means wide ranges of inputs must work for complex divide.
283169699Skan   2 means C99-like requirements for complex multiply and divide.  */
28452558Sobrien
285169699Skanint flag_complex_method = 1;
28652558Sobrien
28796289Sobrien/* Nonzero means that we don't want inlining by virtue of -fno-inline,
28896289Sobrien   not just because the tree inliner turned us off.  */
28996289Sobrien
29096289Sobrienint flag_really_no_inline = 2;
29196289Sobrien
29218334Speter/* Nonzero means we should be saving declaration info into a .X file.  */
29318334Speter
29418334Speterint flag_gen_aux_info = 0;
29518334Speter
29618334Speter/* Specified name of aux-info file.  */
29718334Speter
298132734Skanconst char *aux_info_file_name;
29918334Speter
300132734Skan/* Nonzero if we are compiling code for a shared library, zero for
301132734Skan   executable.  */
302132734Skan
303132734Skanint flag_shlib;
304132734Skan
305169699Skan/* Generate code for GNU or NeXT Objective-C runtime environment.  */
306169699Skan
307169699Skan#ifdef NEXT_OBJC_RUNTIME
308169699Skanint flag_next_runtime = 1;
309169699Skan#else
310169699Skanint flag_next_runtime = 0;
311169699Skan#endif
312169699Skan
313117414Skan/* Set to the default thread-local storage (tls) model to use.  */
314117414Skan
315117414Skanenum tls_model flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
316117414Skan
31718334Speter/* Nonzero means change certain warnings into errors.
31818334Speter   Usually these are warnings about failure to conform to some standard.  */
31918334Speter
32018334Speterint flag_pedantic_errors = 0;
32118334Speter
32250615Sobrien/* -dA causes debug commentary information to be produced in
32350615Sobrien   the generated assembly code (to make it more readable).  This option
32450615Sobrien   is generally only of use to those who actually need to read the
32550615Sobrien   generated assembly code (perhaps while debugging the compiler itself).
32650615Sobrien   Currently, this switch is only used by dwarfout.c; however, it is intended
32750615Sobrien   to be a catchall for printing debug information in the assembler file.  */
32850615Sobrien
32950615Sobrienint flag_debug_asm = 0;
33050615Sobrien
33190282Sobrien/* -dP causes the rtl to be emitted as a comment in assembly.  */
33290282Sobrien
33390282Sobrienint flag_dump_rtl_in_asm = 0;
33490282Sobrien
33590282Sobrien/* When non-NULL, indicates that whenever space is allocated on the
33690282Sobrien   stack, the resulting stack pointer must not pass this
33790282Sobrien   address---that is, for stacks that grow downward, the stack pointer
33890282Sobrien   must always be greater than or equal to this address; for stacks
33990282Sobrien   that grow upward, the stack pointer must be less than this address.
34090282Sobrien   At present, the rtx may be either a REG or a SYMBOL_REF, although
34190282Sobrien   the support provided depends on the backend.  */
34290282Sobrienrtx stack_limit_rtx;
34350615Sobrien
34490282Sobrien/* If one, renumber instruction UIDs to reduce the number of
34590282Sobrien   unused UIDs if there are a lot of instructions.  If greater than
34690282Sobrien   one, unconditionally renumber instruction UIDs.  */
34790282Sobrienint flag_renumber_insns = 1;
34890282Sobrien
349169699Skan/* Nonzero if we should track variables.  When
350169699Skan   flag_var_tracking == AUTODETECT_VALUE it will be set according
351169699Skan   to optimize, debug_info_level and debug_hooks in process_options ().  */
352169699Skanint flag_var_tracking = AUTODETECT_VALUE;
353117414Skan
354169699Skan/* True if the user has tagged the function with the 'section'
355169699Skan   attribute.  */
356117414Skan
357169699Skanbool user_defined_section_attribute = false;
358117414Skan
35990282Sobrien/* Values of the -falign-* flags: how much to align labels in code.
36090282Sobrien   0 means `use default', 1 means `don't align'.
36190282Sobrien   For each variable, there is an _log variant which is the power
36290282Sobrien   of two not less than the variable, for .align output.  */
36390282Sobrien
36490282Sobrienint align_loops_log;
36590282Sobrienint align_loops_max_skip;
36690282Sobrienint align_jumps_log;
36790282Sobrienint align_jumps_max_skip;
36890282Sobrienint align_labels_log;
36990282Sobrienint align_labels_max_skip;
37090282Sobrienint align_functions_log;
37190282Sobrien
372104765Skan/* Like align_functions_log above, but used by front-ends to force the
373104765Skan   minimum function alignment.  Zero means no alignment is forced.  */
374104765Skanint force_align_functions_log;
375104765Skan
37650615Sobrientypedef struct
37750615Sobrien{
37890282Sobrien  const char *const string;
37990282Sobrien  int *const variable;
38090282Sobrien  const int on_value;
38150615Sobrien}
38250615Sobrienlang_independent_options;
38350615Sobrien
384132734Skan/* Nonzero if subexpressions must be evaluated from left-to-right.  */
385132734Skanint flag_evaluation_order = 0;
386132734Skan
38752558Sobrien/* The user symbol prefix after having resolved same.  */
38852558Sobrienconst char *user_label_prefix;
38952558Sobrien
39090282Sobrienstatic const param_info lang_independent_params[] = {
391169699Skan#define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
392169699Skan  { OPTION, DEFAULT, MIN, MAX, HELP },
39390282Sobrien#include "params.def"
39490282Sobrien#undef DEFPARAM
395169699Skan  { NULL, 0, 0, 0, NULL }
39690282Sobrien};
39790282Sobrien
398132734Skan/* Output files for assembler code (real compiler output)
399132734Skan   and debugging dumps.  */
40018334Speter
401132734SkanFILE *asm_out_file;
402132734SkanFILE *aux_info_file;
403169699SkanFILE *dump_file = NULL;
404169699Skanconst char *dump_file_name;
40518334Speter
406132734Skan/* The current working directory of a translation.  It's generally the
407132734Skan   directory from which compilation was initiated, but a preprocessed
408132734Skan   file may specify the original directory in which it was
409132734Skan   created.  */
41090282Sobrien
411132734Skanstatic const char *src_pwd;
41218334Speter
413132734Skan/* Initialize src_pwd with the given string, and return true.  If it
414132734Skan   was already initialized, return false.  As a special case, it may
415132734Skan   be called with a NULL argument to test whether src_pwd has NOT been
416132734Skan   initialized yet.  */
41718334Speter
418132734Skanbool
419132734Skanset_src_pwd (const char *pwd)
420132734Skan{
421132734Skan  if (src_pwd)
422169699Skan    {
423169699Skan      if (strcmp (src_pwd, pwd) == 0)
424169699Skan	return true;
425169699Skan      else
426169699Skan	return false;
427169699Skan    }
42818334Speter
429132734Skan  src_pwd = xstrdup (pwd);
430132734Skan  return true;
431132734Skan}
43218334Speter
433132734Skan/* Return the directory from which the translation unit was initiated,
434132734Skan   in case set_src_pwd() was not called before to assign it a
435132734Skan   different value.  */
43618334Speter
437132734Skanconst char *
438132734Skanget_src_pwd (void)
439132734Skan{
440132734Skan  if (! src_pwd)
441169699Skan    {
442169699Skan      src_pwd = getpwd ();
443169699Skan      if (!src_pwd)
444169699Skan	src_pwd = ".";
445169699Skan    }
44690282Sobrien
447132734Skan   return src_pwd;
448132734Skan}
44990282Sobrien
450132734Skan/* Called when the start of a function definition is parsed,
451132734Skan   this function prints on stderr the name of the function.  */
45290282Sobrienvoid
453132734Skanannounce_function (tree decl)
45490282Sobrien{
455132734Skan  if (!quiet_flag)
456132734Skan    {
457132734Skan      if (rtl_dump_and_exit)
458169699Skan	fprintf (stderr, "%s ", IDENTIFIER_POINTER (DECL_NAME (decl)));
459132734Skan      else
460169699Skan	fprintf (stderr, " %s", lang_hooks.decl_printable_name (decl, 2));
461132734Skan      fflush (stderr);
462132734Skan      pp_needs_newline (global_dc->printer) = true;
463132734Skan      diagnostic_set_last_function (global_dc);
464132734Skan    }
46590282Sobrien}
46690282Sobrien
467132734Skan/* Set up a default flag_random_seed and local_tick, unless the user
468132734Skan   already specified one.  */
46990282Sobrien
470132734Skanstatic void
471132734Skanrandomize (void)
47290282Sobrien{
473132734Skan  if (!flag_random_seed)
474132734Skan    {
475132734Skan      unsigned HOST_WIDE_INT value;
476132734Skan      static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
47790282Sobrien
478132734Skan      /* Get some more or less random data.  */
479132734Skan#ifdef HAVE_GETTIMEOFDAY
480132734Skan      {
481132734Skan 	struct timeval tv;
482132734Skan
483132734Skan 	gettimeofday (&tv, NULL);
484132734Skan	local_tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
485132734Skan      }
486132734Skan#else
487132734Skan      {
488132734Skan	time_t now = time (NULL);
489132734Skan
490132734Skan	if (now != (time_t)-1)
491132734Skan	  local_tick = (unsigned) now;
492132734Skan      }
493132734Skan#endif
494132734Skan      value = local_tick ^ getpid ();
495132734Skan
496132734Skan      sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
497132734Skan      flag_random_seed = random_seed;
498132734Skan    }
499132734Skan  else if (!local_tick)
500132734Skan    local_tick = -1;
50190282Sobrien}
50290282Sobrien
50318334Speter
50452558Sobrien/* Decode the string P as an integral parameter.
50552558Sobrien   If the string is indeed an integer return its numeric value else
50652558Sobrien   issue an Invalid Option error for the option PNAME and return DEFVAL.
50790282Sobrien   If PNAME is zero just return DEFVAL, do not call error.  */
50890282Sobrien
50952558Sobrienint
510132734Skanread_integral_parameter (const char *p, const char *pname, const int  defval)
51152558Sobrien{
51252558Sobrien  const char *endp = p;
51352558Sobrien
51452558Sobrien  while (*endp)
51552558Sobrien    {
51690282Sobrien      if (ISDIGIT (*endp))
51752558Sobrien	endp++;
51852558Sobrien      else
51952558Sobrien	break;
52052558Sobrien    }
52152558Sobrien
52252558Sobrien  if (*endp != 0)
52352558Sobrien    {
52452558Sobrien      if (pname != 0)
525169699Skan	error ("invalid option argument %qs", pname);
52652558Sobrien      return defval;
52752558Sobrien    }
52852558Sobrien
52952558Sobrien  return atoi (p);
53052558Sobrien}
53118334Speter
532169699Skan/* When compiling with a recent enough GCC, we use the GNU C "extern inline"
533169699Skan   for floor_log2 and exact_log2; see toplev.h.  That construct, however,
534169699Skan   conflicts with the ISO C++ One Definition Rule.   */
53518334Speter
536169699Skan#if GCC_VERSION < 3004 || !defined (__cplusplus)
53718334Speter
538169699Skan/* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
539169699Skan   If X is 0, return -1.  */
540169699Skan
54118334Speterint
542169699Skanfloor_log2 (unsigned HOST_WIDE_INT x)
54318334Speter{
544169699Skan  int t = 0;
545169699Skan
546169699Skan  if (x == 0)
54718334Speter    return -1;
548169699Skan
549169699Skan#ifdef CLZ_HWI
550169699Skan  t = HOST_BITS_PER_WIDE_INT - 1 - (int) CLZ_HWI (x);
551169699Skan#else
552169699Skan  if (HOST_BITS_PER_WIDE_INT > 64)
553169699Skan    if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
554169699Skan      t += 64;
555169699Skan  if (HOST_BITS_PER_WIDE_INT > 32)
556169699Skan    if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
557169699Skan      t += 32;
558169699Skan  if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
559169699Skan    t += 16;
560169699Skan  if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
561169699Skan    t += 8;
562169699Skan  if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
563169699Skan    t += 4;
564169699Skan  if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
565169699Skan    t += 2;
566169699Skan  if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
567169699Skan    t += 1;
568169699Skan#endif
569169699Skan
570169699Skan  return t;
57118334Speter}
57218334Speter
573169699Skan/* Return the logarithm of X, base 2, considering X unsigned,
574169699Skan   if X is a power of 2.  Otherwise, returns -1.  */
57518334Speter
57618334Speterint
577169699Skanexact_log2 (unsigned HOST_WIDE_INT x)
57818334Speter{
579169699Skan  if (x != (x & -x))
580169699Skan    return -1;
581169699Skan#ifdef CTZ_HWI
582169699Skan  return x ? CTZ_HWI (x) : -1;
583169699Skan#else
584169699Skan  return floor_log2 (x);
585169699Skan#endif
58618334Speter}
58718334Speter
588169699Skan#endif /*  GCC_VERSION < 3004 || !defined (__cplusplus)  */
589169699Skan
59090282Sobrien/* Handler for fatal signals, such as SIGSEGV.  These are transformed
591132734Skan   into ICE messages, which is much more user friendly.  In case the
592132734Skan   error printer crashes, reset the signal to prevent infinite recursion.  */
59318334Speter
59418334Speterstatic void
595132734Skancrash_signal (int signo)
59618334Speter{
597132734Skan  signal (signo, SIG_DFL);
598169699Skan
599169699Skan  /* If we crashed while processing an ASM statement, then be a little more
600169699Skan     graceful.  It's most likely the user's fault.  */
601169699Skan  if (this_is_asm_operands)
602169699Skan    {
603169699Skan      output_operand_lossage ("unrecoverable error");
604169699Skan      exit (FATAL_EXIT_CODE);
605169699Skan    }
606169699Skan
607117414Skan  internal_error ("%s", strsignal (signo));
60818334Speter}
60918334Speter
610132734Skan/* Arrange to dump core on error.  (The regular error message is still
611132734Skan   printed first, except in the case of abort().)  */
612132734Skan
613132734Skanstatic void
614132734Skansetup_core_dumping (void)
615132734Skan{
616132734Skan#ifdef SIGABRT
617132734Skan  signal (SIGABRT, SIG_DFL);
618132734Skan#endif
619132734Skan#if defined(HAVE_SETRLIMIT)
620132734Skan  {
621132734Skan    struct rlimit rlim;
622132734Skan    if (getrlimit (RLIMIT_CORE, &rlim) != 0)
623132734Skan      fatal_error ("getting core file size maximum limit: %m");
624132734Skan    rlim.rlim_cur = rlim.rlim_max;
625132734Skan    if (setrlimit (RLIMIT_CORE, &rlim) != 0)
626132734Skan      fatal_error ("setting core file size limit to maximum: %m");
627132734Skan  }
628132734Skan#endif
629132734Skan  diagnostic_abort_on_error (global_dc);
630132734Skan}
631132734Skan
632132734Skan
63318334Speter/* Strip off a legitimate source ending from the input string NAME of
63418334Speter   length LEN.  Rather than having to know the names used by all of
63550615Sobrien   our front ends, we strip off an ending of a period followed by
63690282Sobrien   up to five characters.  (Java uses ".class".)  */
63718334Speter
63818334Spetervoid
639132734Skanstrip_off_ending (char *name, int len)
64018334Speter{
64150615Sobrien  int i;
642117414Skan  for (i = 2; i < 6 && len > i; i++)
64350615Sobrien    {
64450615Sobrien      if (name[len - i] == '.')
64550615Sobrien	{
64650615Sobrien	  name[len - i] = '\0';
64750615Sobrien	  break;
64850615Sobrien	}
64950615Sobrien    }
65018334Speter}
65118334Speter
65218334Speter/* Output a quoted string.  */
65350615Sobrien
65418334Spetervoid
655132734Skanoutput_quoted_string (FILE *asm_file, const char *string)
65618334Speter{
65750615Sobrien#ifdef OUTPUT_QUOTED_STRING
65850615Sobrien  OUTPUT_QUOTED_STRING (asm_file, string);
65950615Sobrien#else
66018334Speter  char c;
66118334Speter
66218334Speter  putc ('\"', asm_file);
66318334Speter  while ((c = *string++) != 0)
66418334Speter    {
66596289Sobrien      if (ISPRINT (c))
66696289Sobrien	{
66796289Sobrien	  if (c == '\"' || c == '\\')
66896289Sobrien	    putc ('\\', asm_file);
66996289Sobrien	  putc (c, asm_file);
67096289Sobrien	}
67196289Sobrien      else
672117414Skan	fprintf (asm_file, "\\%03o", (unsigned char) c);
67318334Speter    }
67418334Speter  putc ('\"', asm_file);
67550615Sobrien#endif
67618334Speter}
67718334Speter
678132734Skan/* Output a file name in the form wanted by System V.  */
679132734Skan
680117414Skanvoid
681132734Skanoutput_file_directive (FILE *asm_file, const char *input_name)
682117414Skan{
683132734Skan  int len;
684132734Skan  const char *na;
685117414Skan
686132734Skan  if (input_name == NULL)
687132734Skan    input_name = "<stdin>";
688117414Skan
689132734Skan  len = strlen (input_name);
690132734Skan  na = input_name + len;
691117414Skan
69218334Speter  /* NA gets INPUT_NAME sans directory names.  */
69318334Speter  while (na > input_name)
69418334Speter    {
69590282Sobrien      if (IS_DIR_SEPARATOR (na[-1]))
696117414Skan	break;
69718334Speter      na--;
69818334Speter    }
69918334Speter
70018334Speter#ifdef ASM_OUTPUT_SOURCE_FILENAME
70118334Speter  ASM_OUTPUT_SOURCE_FILENAME (asm_file, na);
70218334Speter#else
70318334Speter  fprintf (asm_file, "\t.file\t");
70418334Speter  output_quoted_string (asm_file, na);
70518334Speter  fputc ('\n', asm_file);
70618334Speter#endif
70718334Speter}
708132734Skan
709169699Skan/* A subroutine of wrapup_global_declarations.  We've come to the end of
710169699Skan   the compilation unit.  All deferred variables should be undeferred,
711169699Skan   and all incomplete decls should be finalized.  */
71218334Speter
713169699Skanvoid
714169699Skanwrapup_global_declaration_1 (tree decl)
71518334Speter{
716169699Skan  /* We're not deferring this any longer.  Assignment is conditional to
717169699Skan     avoid needlessly dirtying PCH pages.  */
718169699Skan  if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
719169699Skan      && DECL_DEFER_OUTPUT (decl) != 0)
720169699Skan    DECL_DEFER_OUTPUT (decl) = 0;
72118334Speter
722169699Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_SIZE (decl) == 0)
723169699Skan    lang_hooks.finish_incomplete_decl (decl);
724169699Skan}
72550615Sobrien
726169699Skan/* A subroutine of wrapup_global_declarations.  Decide whether or not DECL
727169699Skan   needs to be output.  Return true if it is output.  */
72850615Sobrien
729169699Skanbool
730169699Skanwrapup_global_declaration_2 (tree decl)
731169699Skan{
732169699Skan  if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl))
733169699Skan    return false;
73450615Sobrien
735169699Skan  /* Don't write out static consts, unless we still need them.
73650615Sobrien
737169699Skan     We also keep static consts if not optimizing (for debugging),
738169699Skan     unless the user specified -fno-keep-static-consts.
739169699Skan     ??? They might be better written into the debug information.
740169699Skan     This is possible when using DWARF.
74150615Sobrien
742169699Skan     A language processor that wants static constants to be always
743169699Skan     written out (even if it is not used) is responsible for
744169699Skan     calling rest_of_decl_compilation itself.  E.g. the C front-end
745169699Skan     calls rest_of_decl_compilation from finish_decl.
746169699Skan     One motivation for this is that is conventional in some
747169699Skan     environments to write things like:
748169699Skan     static const char rcsid[] = "... version string ...";
749169699Skan     intending to force the string to be in the executable.
75090282Sobrien
751169699Skan     A language processor that would prefer to have unneeded
752169699Skan     static constants "optimized away" would just defer writing
753169699Skan     them out until here.  E.g. C++ does this, because static
754169699Skan     constants are often defined in header files.
75590282Sobrien
756169699Skan     ??? A tempting alternative (for both C and C++) would be
757169699Skan     to force a constant to be written if and only if it is
758169699Skan     defined in a main file, as opposed to an include file.  */
75990282Sobrien
760169699Skan  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
761169699Skan    {
762169699Skan      struct cgraph_varpool_node *node;
763169699Skan      bool needed = true;
764169699Skan      node = cgraph_varpool_node (decl);
76550615Sobrien
766169699Skan      if (node->finalized)
767169699Skan	needed = false;
768169699Skan      else if (node->alias)
769169699Skan	needed = false;
770169699Skan      else if (!cgraph_global_info_ready
771169699Skan	       && (TREE_USED (decl)
772169699Skan		   || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
773169699Skan	/* needed */;
774169699Skan      else if (node->needed)
775169699Skan	/* needed */;
776169699Skan      else if (DECL_COMDAT (decl))
777169699Skan	needed = false;
778169699Skan      else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
779169699Skan	       && (optimize || !flag_keep_static_consts
780169699Skan		   || DECL_ARTIFICIAL (decl)))
781169699Skan	needed = false;
78290282Sobrien
783169699Skan      if (needed)
784169699Skan	{
785169699Skan	  rest_of_decl_compilation (decl, 1, 1);
786169699Skan	  return true;
787169699Skan	}
78890282Sobrien    }
78950615Sobrien
790169699Skan  return false;
79118334Speter}
79218334Speter
79352558Sobrien/* Do any final processing required for the declarations in VEC, of
79452558Sobrien   which there are LEN.  We write out inline functions and variables
79552558Sobrien   that have been deferred until this point, but which are required.
796117414Skan   Returns nonzero if anything was put out.  */
79790282Sobrien
798169699Skanbool
799132734Skanwrapup_global_declarations (tree *vec, int len)
80052558Sobrien{
801169699Skan  bool reconsider, output_something = false;
80252558Sobrien  int i;
80350615Sobrien
80452558Sobrien  for (i = 0; i < len; i++)
805169699Skan    wrapup_global_declaration_1 (vec[i]);
80690282Sobrien
80752558Sobrien  /* Now emit any global variables or functions that we have been
80852558Sobrien     putting off.  We need to loop in case one of the things emitted
80952558Sobrien     here references another one which comes earlier in the list.  */
81052558Sobrien  do
81152558Sobrien    {
812169699Skan      reconsider = false;
81352558Sobrien      for (i = 0; i < len; i++)
814169699Skan	reconsider |= wrapup_global_declaration_2 (vec[i]);
81552558Sobrien      if (reconsider)
816169699Skan	output_something = true;
81752558Sobrien    }
81852558Sobrien  while (reconsider);
81952558Sobrien
82052558Sobrien  return output_something;
82152558Sobrien}
82252558Sobrien
823169699Skan/* A subroutine of check_global_declarations.  Issue appropriate warnings
824169699Skan   for the global declaration DECL.  */
825169699Skan
826169699Skanvoid
827169699Skancheck_global_declaration_1 (tree decl)
828169699Skan{
829169699Skan  /* Warn about any function declared static but not defined.  We don't
830169699Skan     warn about variables, because many programs have static variables
831169699Skan     that exist only to get some text into the object file.  */
832169699Skan  if (TREE_CODE (decl) == FUNCTION_DECL
833169699Skan      && DECL_INITIAL (decl) == 0
834169699Skan      && DECL_EXTERNAL (decl)
835169699Skan      && ! DECL_ARTIFICIAL (decl)
836169699Skan      && ! TREE_NO_WARNING (decl)
837169699Skan      && ! TREE_PUBLIC (decl)
838169699Skan      && (warn_unused_function
839169699Skan	  || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
840169699Skan    {
841169699Skan      if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
842169699Skan	pedwarn ("%q+F used but never defined", decl);
843169699Skan      else
844169699Skan	warning (0, "%q+F declared %<static%> but never defined", decl);
845169699Skan      /* This symbol is effectively an "extern" declaration now.  */
846169699Skan      TREE_PUBLIC (decl) = 1;
847169699Skan      assemble_external (decl);
848169699Skan    }
849169699Skan
850169699Skan  /* Warn about static fns or vars defined but not used.  */
851169699Skan  if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
852169699Skan       /* We don't warn about "static const" variables because the
853169699Skan	  "rcs_id" idiom uses that construction.  */
854169699Skan       || (warn_unused_variable
855169699Skan	   && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
856169699Skan      && ! DECL_IN_SYSTEM_HEADER (decl)
857169699Skan      && ! TREE_USED (decl)
858169699Skan      /* The TREE_USED bit for file-scope decls is kept in the identifier,
859169699Skan	 to handle multiple external decls in different scopes.  */
860169699Skan      && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
861169699Skan      && ! DECL_EXTERNAL (decl)
862169699Skan      && ! TREE_PUBLIC (decl)
863169699Skan      /* A volatile variable might be used in some non-obvious way.  */
864169699Skan      && ! TREE_THIS_VOLATILE (decl)
865169699Skan      /* Global register variables must be declared to reserve them.  */
866169699Skan      && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
867169699Skan      /* Otherwise, ask the language.  */
868169699Skan      && lang_hooks.decls.warn_unused_global (decl))
869169699Skan    warning (0, "%q+D defined but not used", decl);
870169699Skan}
871169699Skan
87252558Sobrien/* Issue appropriate warnings for the global declarations in VEC (of
873169699Skan   which there are LEN).  */
87490282Sobrien
87552558Sobrienvoid
876132734Skancheck_global_declarations (tree *vec, int len)
87752558Sobrien{
87852558Sobrien  int i;
87952558Sobrien
88052558Sobrien  for (i = 0; i < len; i++)
881169699Skan    check_global_declaration_1 (vec[i]);
882169699Skan}
88352558Sobrien
884169699Skan/* Emit debugging information for all global declarations in VEC.  */
88552558Sobrien
886169699Skanvoid
887169699Skanemit_debug_global_declarations (tree *vec, int len)
888169699Skan{
889169699Skan  int i;
89052558Sobrien
891169699Skan  /* Avoid confusing the debug information machinery when there are errors.  */
892169699Skan  if (errorcount != 0 || sorrycount != 0)
893169699Skan    return;
89452558Sobrien
895169699Skan  timevar_push (TV_SYMOUT);
896169699Skan  for (i = 0; i < len; i++)
897169699Skan    debug_hooks->global_decl (vec[i]);
898169699Skan  timevar_pop (TV_SYMOUT);
89952558Sobrien}
90052558Sobrien
901132734Skan/* Warn about a use of an identifier which was marked deprecated.  */
902132734Skanvoid
903132734Skanwarn_deprecated_use (tree node)
904132734Skan{
905132734Skan  if (node == 0 || !warn_deprecated_decl)
906132734Skan    return;
907132734Skan
908132734Skan  if (DECL_P (node))
909169699Skan    {
910169699Skan      expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
911169699Skan      warning (OPT_Wdeprecated_declarations,
912169699Skan	       "%qs is deprecated (declared at %s:%d)",
913169699Skan	       IDENTIFIER_POINTER (DECL_NAME (node)),
914169699Skan	       xloc.file, xloc.line);
915169699Skan    }
916132734Skan  else if (TYPE_P (node))
917132734Skan    {
918132734Skan      const char *what = NULL;
919132734Skan      tree decl = TYPE_STUB_DECL (node);
920132734Skan
921169699Skan      if (TYPE_NAME (node))
922169699Skan	{
923169699Skan	  if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
924169699Skan	    what = IDENTIFIER_POINTER (TYPE_NAME (node));
925169699Skan	  else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
926169699Skan		   && DECL_NAME (TYPE_NAME (node)))
927169699Skan	    what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
928169699Skan	}
929132734Skan
930169699Skan      if (decl)
931132734Skan	{
932169699Skan	  expanded_location xloc
933169699Skan	    = expand_location (DECL_SOURCE_LOCATION (decl));
934169699Skan	  if (what)
935169699Skan	    warning (OPT_Wdeprecated_declarations,
936169699Skan		     "%qs is deprecated (declared at %s:%d)", what,
937169699Skan		     xloc.file, xloc.line);
938132734Skan	  else
939169699Skan	    warning (OPT_Wdeprecated_declarations,
940169699Skan		     "type is deprecated (declared at %s:%d)",
941169699Skan		     xloc.file, xloc.line);
942132734Skan	}
943132734Skan      else
944169699Skan	{
945169699Skan	  if (what)
946169699Skan	    warning (OPT_Wdeprecated_declarations, "%qs is deprecated", what);
947169699Skan	  else
948169699Skan	    warning (OPT_Wdeprecated_declarations, "type is deprecated");
949169699Skan	}
950132734Skan    }
951132734Skan}
952132734Skan
953132734Skan/* Save the current INPUT_LOCATION on the top entry in the
95490282Sobrien   INPUT_FILE_STACK.  Push a new entry for FILE and LINE, and set the
955132734Skan   INPUT_LOCATION accordingly.  */
95618334Speter
95790282Sobrienvoid
958169699Skan#ifdef USE_MAPPED_LOCATION
959169699Skanpush_srcloc (location_t fline)
960169699Skan#else
961132734Skanpush_srcloc (const char *file, int line)
962169699Skan#endif
96318334Speter{
96490282Sobrien  struct file_stack *fs;
96518334Speter
966169699Skan  gcc_assert (!input_file_stack_restored);
967169699Skan  if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
968169699Skan    sorry ("GCC supports only %d input file changes", input_file_stack_tick);
969169699Skan
970169699Skan  fs = XNEW (struct file_stack);
971132734Skan  fs->location = input_location;
97290282Sobrien  fs->next = input_file_stack;
973169699Skan#ifdef USE_MAPPED_LOCATION
974169699Skan  input_location = fline;
975169699Skan#else
976132734Skan  input_filename = file;
977132734Skan  input_line = line;
978169699Skan#endif
97990282Sobrien  input_file_stack = fs;
98090282Sobrien  input_file_stack_tick++;
981169699Skan  VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
98290282Sobrien}
98318334Speter
98490282Sobrien/* Pop the top entry off the stack of presently open source files.
985132734Skan   Restore the INPUT_LOCATION from the new topmost entry on the
986132734Skan   stack.  */
98718334Speter
98890282Sobrienvoid
989132734Skanpop_srcloc (void)
99090282Sobrien{
99190282Sobrien  struct file_stack *fs;
99218334Speter
993169699Skan  gcc_assert (!input_file_stack_restored);
994169699Skan  if (input_file_stack_tick == (int) ((1U << INPUT_FILE_STACK_BITS) - 1))
995169699Skan    sorry ("GCC supports only %d input file changes", input_file_stack_tick);
996169699Skan
99790282Sobrien  fs = input_file_stack;
998132734Skan  input_location = fs->location;
99990282Sobrien  input_file_stack = fs->next;
100090282Sobrien  input_file_stack_tick++;
1001169699Skan  VEC_safe_push (fs_p, heap, input_file_stack_history, input_file_stack);
100290282Sobrien}
100318334Speter
1004169699Skan/* Restore the input file stack to its state as of TICK, for the sake
1005169699Skan   of diagnostics after processing the whole input.  Once this has
1006169699Skan   been called, push_srcloc and pop_srcloc may no longer be
1007169699Skan   called.  */
1008169699Skanvoid
1009169699Skanrestore_input_file_stack (int tick)
1010169699Skan{
1011169699Skan  if (tick == 0)
1012169699Skan    input_file_stack = NULL;
1013169699Skan  else
1014169699Skan    input_file_stack = VEC_index (fs_p, input_file_stack_history, tick - 1);
1015169699Skan  input_file_stack_tick = tick;
1016169699Skan  input_file_stack_restored = true;
1017169699Skan}
1018169699Skan
101990282Sobrien/* Compile an entire translation unit.  Write a file of assembly
102090282Sobrien   output and various debugging dumps.  */
102118334Speter
102290282Sobrienstatic void
1023132734Skancompile_file (void)
102490282Sobrien{
102518334Speter  /* Initialize yet another pass.  */
102618334Speter
1027169699Skan  init_cgraph ();
102850615Sobrien  init_final (main_input_filename);
1029132734Skan  coverage_init (aux_base_name);
103018334Speter
103190282Sobrien  timevar_push (TV_PARSE);
103218334Speter
1033117414Skan  /* Call the parser, which parses the entire file (calling
1034117414Skan     rest_of_compilation for each function).  */
1035169699Skan  lang_hooks.parse_file (set_yydebug);
103618334Speter
103790282Sobrien  /* In case there were missing block closers,
103890282Sobrien     get us back to the global binding level.  */
1039169699Skan  lang_hooks.clear_binding_stack ();
104018334Speter
104118334Speter  /* Compilation is now finished except for writing
104218334Speter     what's left of the symbol table output.  */
104390282Sobrien  timevar_pop (TV_PARSE);
104418334Speter
1045169699Skan  if (flag_syntax_only || errorcount || sorrycount)
104690282Sobrien    return;
104752558Sobrien
1048169699Skan  lang_hooks.decls.final_write_globals ();
1049132734Skan  cgraph_varpool_assemble_pending_decls ();
1050169699Skan  finish_aliases_2 ();
105118334Speter
1052132734Skan  /* This must occur after the loop to output deferred functions.
1053132734Skan     Else the coverage initializer would not be emitted if all the
1054132734Skan     functions in this compilation unit were deferred.  */
1055132734Skan  coverage_finish ();
105618334Speter
1057169699Skan  /* Likewise for mudflap static object registrations.  */
1058169699Skan  if (flag_mudflap)
1059169699Skan    mudflap_finish_file ();
1060169699Skan
1061169699Skan  output_shared_constant_pool ();
1062169699Skan  output_object_blocks ();
1063169699Skan
106418334Speter  /* Write out any pending weak symbol declarations.  */
106518334Speter
106618334Speter  weak_finish ();
106718334Speter
106890282Sobrien  /* Do dbx symbols.  */
106990282Sobrien  timevar_push (TV_SYMOUT);
107018334Speter
1071169699Skan#if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
107250615Sobrien  if (dwarf2out_do_frame ())
107350615Sobrien    dwarf2out_frame_finish ();
107450615Sobrien#endif
107550615Sobrien
107690282Sobrien  (*debug_hooks->finish) (main_input_filename);
107790282Sobrien  timevar_pop (TV_SYMOUT);
107850615Sobrien
107918334Speter  /* Output some stuff at end of file if nec.  */
108018334Speter
108190282Sobrien  dw2_output_indirect_constants ();
108290282Sobrien
1083215840Sdim  /* Flush any pending external directives.  */
1084169699Skan  process_pending_assemble_externals ();
1085146908Skan
108690282Sobrien  /* Attach a special .ident directive to the end of the file to identify
108790282Sobrien     the version of GCC which compiled this code.  The format of the .ident
108890282Sobrien     string is patterned after the ones produced by native SVR4 compilers.  */
108990282Sobrien#ifdef IDENT_ASM_OP
109090282Sobrien  if (!flag_no_ident)
109190282Sobrien    fprintf (asm_out_file, "%s\"GCC: (GNU) %s\"\n",
109290282Sobrien	     IDENT_ASM_OP, version_string);
109352558Sobrien#endif
109452558Sobrien
1095169699Skan  /* This must be at the end.  Some target ports emit end of file directives
1096169699Skan     into the assembly file here, and hence we can not output anything to the
1097169699Skan     assembly file after this point.  */
1098169699Skan  targetm.asm_out.file_end ();
109918334Speter}
1100132734Skan
110190282Sobrien/* Parse a -d... command line switch.  */
110250615Sobrien
1103132734Skanvoid
1104132734Skandecode_d_option (const char *arg)
110590282Sobrien{
1106169699Skan  int c;
110750615Sobrien
110890282Sobrien  while (*arg)
110990282Sobrien    switch (c = *arg++)
111090282Sobrien      {
111190282Sobrien      case 'A':
111290282Sobrien	flag_debug_asm = 1;
111390282Sobrien	break;
111490282Sobrien      case 'p':
111590282Sobrien	flag_print_asm_name = 1;
111690282Sobrien	break;
111790282Sobrien      case 'P':
1118117414Skan	flag_dump_rtl_in_asm = 1;
1119117414Skan	flag_print_asm_name = 1;
1120117414Skan	break;
112190282Sobrien      case 'v':
112290282Sobrien	graph_dump_format = vcg;
112390282Sobrien	break;
112490282Sobrien      case 'x':
112590282Sobrien	rtl_dump_and_exit = 1;
112690282Sobrien	break;
112790282Sobrien      case 'y':
1128117414Skan	set_yydebug = 1;
112990282Sobrien	break;
113090282Sobrien      case 'D':	/* These are handled by the preprocessor.  */
113190282Sobrien      case 'I':
113290282Sobrien	break;
1133132734Skan      case 'H':
1134132734Skan	setup_core_dumping();
1135132734Skan	break;
113690282Sobrien
1137169699Skan      case 'a':
113890282Sobrien      default:
1139169699Skan	if (!enable_rtl_dump_file (c))
1140169699Skan	  warning (0, "unrecognized gcc debugging option: %c", c);
114190282Sobrien	break;
114290282Sobrien      }
114390282Sobrien}
114490282Sobrien
1145132734Skan/* Indexed by enum debug_info_type.  */
1146132734Skanconst char *const debug_type_names[] =
114750615Sobrien{
1148169699Skan  "none", "stabs", "coff", "dwarf-2", "xcoff", "vms"
1149132734Skan};
115050615Sobrien
115150615Sobrien/* Print version information to FILE.
115250615Sobrien   Each line begins with INDENT (for the case where FILE is the
115350615Sobrien   assembler output file).  */
115418334Speter
1155132734Skanvoid
1156132734Skanprint_version (FILE *file, const char *indent)
115750615Sobrien{
1158169699Skan  static const char fmt1[] =
1159169699Skan#ifdef __GNUC__
1160169699Skan    N_("%s%s%s version %s (%s)\n%s\tcompiled by GNU C version %s.\n")
1161169699Skan#else
1162169699Skan    N_("%s%s%s version %s (%s) compiled by CC.\n")
1163169699Skan#endif
1164169699Skan    ;
1165169699Skan  static const char fmt2[] =
1166169699Skan    N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
116750615Sobrien#ifndef __VERSION__
116852558Sobrien#define __VERSION__ "[?]"
116950615Sobrien#endif
1170169699Skan  fprintf (file,
1171169699Skan	   file == stderr ? _(fmt1) : fmt1,
1172169699Skan	   indent, *indent != 0 ? " " : "",
117390282Sobrien	   lang_hooks.name, version_string, TARGET_NAME,
117490282Sobrien	   indent, __VERSION__);
1175169699Skan  fprintf (file,
1176169699Skan	   file == stderr ? _(fmt2) : fmt2,
1177117414Skan	   indent, *indent != 0 ? " " : "",
1178117414Skan	   PARAM_VALUE (GGC_MIN_EXPAND), PARAM_VALUE (GGC_MIN_HEAPSIZE));
117950615Sobrien}
118018334Speter
118150615Sobrien/* Print an option value and return the adjusted position in the line.
118250615Sobrien   ??? We don't handle error returns from fprintf (disk full); presumably
118350615Sobrien   other code will catch a disk full though.  */
118418334Speter
118552558Sobrienstatic int
1186132734Skanprint_single_switch (FILE *file, int pos, int max,
1187132734Skan		     const char *indent, const char *sep, const char *term,
1188132734Skan		     const char *type, const char *name)
118918334Speter{
119050615Sobrien  /* The ultrix fprintf returns 0 on success, so compute the result we want
119150615Sobrien     here since we need it for the following test.  */
119250615Sobrien  int len = strlen (sep) + strlen (type) + strlen (name);
119318334Speter
119450615Sobrien  if (pos != 0
119550615Sobrien      && pos + len > max)
119618334Speter    {
119750615Sobrien      fprintf (file, "%s", term);
119850615Sobrien      pos = 0;
119918334Speter    }
120050615Sobrien  if (pos == 0)
120150615Sobrien    {
120250615Sobrien      fprintf (file, "%s", indent);
120350615Sobrien      pos = strlen (indent);
120450615Sobrien    }
120550615Sobrien  fprintf (file, "%s%s%s", sep, type, name);
120650615Sobrien  pos += len;
120750615Sobrien  return pos;
120818334Speter}
120990282Sobrien
121050615Sobrien/* Print active target switches to FILE.
121150615Sobrien   POS is the current cursor position and MAX is the size of a "line".
121250615Sobrien   Each line begins with INDENT and ends with TERM.
121350615Sobrien   Each switch is separated from the next by SEP.  */
121418334Speter
121552558Sobrienstatic void
1216132734Skanprint_switch_values (FILE *file, int pos, int max,
1217132734Skan		     const char *indent, const char *sep, const char *term)
121818334Speter{
121950615Sobrien  size_t j;
1220132734Skan  const char **p;
122118334Speter
1222117414Skan  /* Fill in the -frandom-seed option, if the user didn't pass it, so
1223132734Skan     that it can be printed below.  This helps reproducibility.  */
1224132734Skan  randomize ();
1225117414Skan
122650615Sobrien  /* Print the options as passed.  */
122750615Sobrien  pos = print_single_switch (file, pos, max, indent, *indent ? " " : "", term,
122852558Sobrien			     _("options passed: "), "");
122950615Sobrien
123050615Sobrien  for (p = &save_argv[1]; *p != NULL; p++)
123150615Sobrien    if (**p == '-')
123250615Sobrien      {
123350615Sobrien	/* Ignore these.  */
123450615Sobrien	if (strcmp (*p, "-o") == 0)
123550615Sobrien	  {
123650615Sobrien	    if (p[1] != NULL)
123750615Sobrien	      p++;
123850615Sobrien	    continue;
123950615Sobrien	  }
124050615Sobrien	if (strcmp (*p, "-quiet") == 0)
124150615Sobrien	  continue;
124250615Sobrien	if (strcmp (*p, "-version") == 0)
124350615Sobrien	  continue;
124450615Sobrien	if ((*p)[1] == 'd')
124550615Sobrien	  continue;
124650615Sobrien
124750615Sobrien	pos = print_single_switch (file, pos, max, indent, sep, term, *p, "");
124850615Sobrien      }
124950615Sobrien  if (pos > 0)
125050615Sobrien    fprintf (file, "%s", term);
125150615Sobrien
125250615Sobrien  /* Print the -f and -m options that have been enabled.
125350615Sobrien     We don't handle language specific options but printing argv
125450615Sobrien     should suffice.  */
125550615Sobrien
125650615Sobrien  pos = print_single_switch (file, 0, max, indent, *indent ? " " : "", term,
125752558Sobrien			     _("options enabled: "), "");
125850615Sobrien
1259169699Skan  for (j = 0; j < cl_options_count; j++)
1260169699Skan    if ((cl_options[j].flags & CL_REPORT)
1261169699Skan	&& option_enabled (j) > 0)
126250615Sobrien      pos = print_single_switch (file, pos, max, indent, sep, term,
1263169699Skan				 "", cl_options[j].opt_text);
126418334Speter
126550615Sobrien  fprintf (file, "%s", term);
126618334Speter}
1267132734Skan
126890282Sobrien/* Open assembly code output file.  Do this even if -fsyntax-only is
126990282Sobrien   on, because then the driver will have provided the name of a
127090282Sobrien   temporary file or bit bucket for us.  NAME is the file specified on
127190282Sobrien   the command line, possibly NULL.  */
127290282Sobrienstatic void
1273132734Skaninit_asm_output (const char *name)
127490282Sobrien{
127590282Sobrien  if (name == NULL && asm_file_name == 0)
127690282Sobrien    asm_out_file = stdout;
127790282Sobrien  else
127890282Sobrien    {
127990282Sobrien      if (asm_file_name == 0)
1280117414Skan	{
1281117414Skan	  int len = strlen (dump_base_name);
1282169699Skan	  char *dumpname = XNEWVEC (char, len + 6);
1283117414Skan	  memcpy (dumpname, dump_base_name, len + 1);
1284117414Skan	  strip_off_ending (dumpname, len);
1285117414Skan	  strcat (dumpname, ".s");
1286117414Skan	  asm_file_name = dumpname;
1287117414Skan	}
128890282Sobrien      if (!strcmp (asm_file_name, "-"))
1289117414Skan	asm_out_file = stdout;
129090282Sobrien      else
1291169699Skan	asm_out_file = fopen (asm_file_name, "w+b");
129290282Sobrien      if (asm_out_file == 0)
1293169699Skan	fatal_error ("can%'t open %s for writing: %m", asm_file_name);
129490282Sobrien    }
129550615Sobrien
129690282Sobrien  if (!flag_syntax_only)
129790282Sobrien    {
1298132734Skan      targetm.asm_out.file_start ();
129990282Sobrien
130090282Sobrien#ifdef ASM_COMMENT_START
130190282Sobrien      if (flag_verbose_asm)
130290282Sobrien	{
130390282Sobrien	  /* Print the list of options in effect.  */
130490282Sobrien	  print_version (asm_out_file, ASM_COMMENT_START);
130590282Sobrien	  print_switch_values (asm_out_file, 0, MAX_LINE,
130690282Sobrien			       ASM_COMMENT_START, " ", "\n");
130790282Sobrien	  /* Add a blank line here so it appears in assembler output but not
130890282Sobrien	     screen output.  */
130990282Sobrien	  fprintf (asm_out_file, "\n");
131090282Sobrien	}
131190282Sobrien#endif
131290282Sobrien    }
131390282Sobrien}
1314132734Skan
1315169699Skan/* Return true if the state of option OPTION should be stored in PCH files
1316169699Skan   and checked by default_pch_valid_p.  Store the option's current state
1317169699Skan   in STATE if so.  */
1318169699Skan
1319169699Skanstatic inline bool
1320169699Skanoption_affects_pch_p (int option, struct cl_option_state *state)
1321169699Skan{
1322169699Skan  if ((cl_options[option].flags & CL_TARGET) == 0)
1323169699Skan    return false;
1324169699Skan  if (cl_options[option].flag_var == &target_flags)
1325169699Skan    if (targetm.check_pch_target_flags)
1326169699Skan      return false;
1327169699Skan  return get_option_state (option, state);
1328169699Skan}
1329169699Skan
1330132734Skan/* Default version of get_pch_validity.
1331132734Skan   By default, every flag difference is fatal; that will be mostly right for
1332132734Skan   most targets, but completely right for very few.  */
1333132734Skan
1334132734Skanvoid *
1335132734Skandefault_get_pch_validity (size_t *len)
1336132734Skan{
1337169699Skan  struct cl_option_state state;
1338132734Skan  size_t i;
1339132734Skan  char *result, *r;
1340132734Skan
1341169699Skan  *len = 2;
1342169699Skan  if (targetm.check_pch_target_flags)
1343169699Skan    *len += sizeof (target_flags);
1344169699Skan  for (i = 0; i < cl_options_count; i++)
1345169699Skan    if (option_affects_pch_p (i, &state))
1346169699Skan      *len += state.size;
1347132734Skan
1348169699Skan  result = r = XNEWVEC (char, *len);
1349132734Skan  r[0] = flag_pic;
1350132734Skan  r[1] = flag_pie;
1351132734Skan  r += 2;
1352169699Skan  if (targetm.check_pch_target_flags)
1353132734Skan    {
1354169699Skan      memcpy (r, &target_flags, sizeof (target_flags));
1355169699Skan      r += sizeof (target_flags);
1356132734Skan    }
1357132734Skan
1358169699Skan  for (i = 0; i < cl_options_count; i++)
1359169699Skan    if (option_affects_pch_p (i, &state))
1360169699Skan      {
1361169699Skan	memcpy (r, state.data, state.size);
1362169699Skan	r += state.size;
1363169699Skan      }
1364169699Skan
1365132734Skan  return result;
1366132734Skan}
1367132734Skan
1368169699Skan/* Return a message which says that a PCH file was created with a different
1369169699Skan   setting of OPTION.  */
1370169699Skan
1371169699Skanstatic const char *
1372169699Skanpch_option_mismatch (const char *option)
1373169699Skan{
1374169699Skan  char *r;
1375169699Skan
1376169699Skan  asprintf (&r, _("created and used with differing settings of '%s'"), option);
1377169699Skan  if (r == NULL)
1378169699Skan    return _("out of memory");
1379169699Skan  return r;
1380169699Skan}
1381169699Skan
1382132734Skan/* Default version of pch_valid_p.  */
1383132734Skan
1384132734Skanconst char *
1385132734Skandefault_pch_valid_p (const void *data_p, size_t len)
1386132734Skan{
1387169699Skan  struct cl_option_state state;
1388132734Skan  const char *data = (const char *)data_p;
1389132734Skan  size_t i;
1390132734Skan
1391132734Skan  /* -fpic and -fpie also usually make a PCH invalid.  */
1392132734Skan  if (data[0] != flag_pic)
1393132734Skan    return _("created and used with different settings of -fpic");
1394132734Skan  if (data[1] != flag_pie)
1395132734Skan    return _("created and used with different settings of -fpie");
1396132734Skan  data += 2;
1397132734Skan
1398132734Skan  /* Check target_flags.  */
1399169699Skan  if (targetm.check_pch_target_flags)
1400132734Skan    {
1401169699Skan      int tf;
1402169699Skan      const char *r;
1403132734Skan
1404169699Skan      memcpy (&tf, data, sizeof (target_flags));
1405169699Skan      data += sizeof (target_flags);
1406169699Skan      len -= sizeof (target_flags);
1407169699Skan      r = targetm.check_pch_target_flags (tf);
1408169699Skan      if (r != NULL)
1409169699Skan	return r;
1410132734Skan    }
1411132734Skan
1412169699Skan  for (i = 0; i < cl_options_count; i++)
1413169699Skan    if (option_affects_pch_p (i, &state))
1414169699Skan      {
1415169699Skan	if (memcmp (data, state.data, state.size) != 0)
1416169699Skan	  return pch_option_mismatch (cl_options[i].opt_text);
1417169699Skan	data += state.size;
1418169699Skan	len -= state.size;
1419169699Skan      }
1420132734Skan
1421132734Skan  return NULL;
1422132734Skan}
1423132734Skan
1424132734Skan/* Default tree printer.   Handles declarations only.  */
1425132734Skanstatic bool
1426169699Skandefault_tree_printer (pretty_printer * pp, text_info *text, const char *spec,
1427169699Skan		      int precision, bool wide, bool set_locus, bool hash)
1428132734Skan{
1429169699Skan  tree t;
1430169699Skan
1431169699Skan  /* FUTURE: %+x should set the locus.  */
1432169699Skan  if (precision != 0 || wide || hash)
1433169699Skan    return false;
1434169699Skan
1435169699Skan  switch (*spec)
1436132734Skan    {
1437132734Skan    case 'D':
1438169699Skan      t = va_arg (*text->args_ptr, tree);
1439169699Skan      if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
1440169699Skan	t = DECL_DEBUG_EXPR (t);
1441169699Skan      break;
1442169699Skan
1443132734Skan    case 'F':
1444132734Skan    case 'T':
1445169699Skan      t = va_arg (*text->args_ptr, tree);
1446169699Skan      break;
1447132734Skan
1448132734Skan    default:
1449132734Skan      return false;
1450132734Skan    }
1451169699Skan
1452169699Skan  if (set_locus && text->locus)
1453169699Skan    *text->locus = DECL_SOURCE_LOCATION (t);
1454169699Skan
1455169699Skan  if (DECL_P (t))
1456169699Skan    {
1457169699Skan      const char *n = DECL_NAME (t)
1458169699Skan        ? lang_hooks.decl_printable_name (t, 2)
1459169699Skan        : "<anonymous>";
1460169699Skan      pp_string (pp, n);
1461169699Skan    }
1462169699Skan  else
1463169699Skan    dump_generic_node (pp, t, 0, 0, 0);
1464169699Skan
1465169699Skan  return true;
1466132734Skan}
1467132734Skan
146890282Sobrien/* Initialization of the front end environment, before command line
146990282Sobrien   options are parsed.  Signal handlers, internationalization etc.
147090282Sobrien   ARGV0 is main's argv[0].  */
147190282Sobrienstatic void
1472132734Skangeneral_init (const char *argv0)
147350615Sobrien{
1474132734Skan  const char *p;
147590282Sobrien
147690282Sobrien  p = argv0 + strlen (argv0);
147790282Sobrien  while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
147890282Sobrien    --p;
147990282Sobrien  progname = p;
148090282Sobrien
148190282Sobrien  xmalloc_set_program_name (progname);
148290282Sobrien
1483117414Skan  hex_init ();
1484117414Skan
1485169699Skan  /* Unlock the stdio streams.  */
1486169699Skan  unlock_std_streams ();
1487169699Skan
148890282Sobrien  gcc_init_libintl ();
148990282Sobrien
1490132734Skan  /* Initialize the diagnostics reporting machinery, so option parsing
1491132734Skan     can give warnings and errors.  */
1492132734Skan  diagnostic_initialize (global_dc);
1493132734Skan  /* Set a default printer.  Language specific initializations will
1494132734Skan     override it later.  */
1495132734Skan  pp_format_decoder (global_dc->printer) = &default_tree_printer;
1496132734Skan
149790282Sobrien  /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages.  */
149890282Sobrien#ifdef SIGSEGV
149990282Sobrien  signal (SIGSEGV, crash_signal);
150050615Sobrien#endif
150190282Sobrien#ifdef SIGILL
150290282Sobrien  signal (SIGILL, crash_signal);
150350615Sobrien#endif
150490282Sobrien#ifdef SIGBUS
150590282Sobrien  signal (SIGBUS, crash_signal);
150690282Sobrien#endif
150790282Sobrien#ifdef SIGABRT
150890282Sobrien  signal (SIGABRT, crash_signal);
150990282Sobrien#endif
151090282Sobrien#if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
151190282Sobrien  signal (SIGIOT, crash_signal);
151290282Sobrien#endif
1513117414Skan#ifdef SIGFPE
1514117414Skan  signal (SIGFPE, crash_signal);
1515117414Skan#endif
151690282Sobrien
1517132734Skan  /* Other host-specific signal setup.  */
1518132734Skan  (*host_hooks.extra_signals)();
1519117414Skan
1520117414Skan  /* Initialize the garbage-collector, string pools and tree type hash
1521117414Skan     table.  */
1522117414Skan  init_ggc ();
1523117414Skan  init_stringpool ();
1524169699Skan  linemap_init (&line_table);
1525117414Skan  init_ttree ();
1526117414Skan
152790282Sobrien  /* Initialize register usage now so switches may override.  */
152890282Sobrien  init_reg_sets ();
152990282Sobrien
153090282Sobrien  /* Register the language-independent parameters.  */
153190282Sobrien  add_params (lang_independent_params, LAST_PARAM);
153290282Sobrien
1533117414Skan  /* This must be done after add_params but before argument processing.  */
1534117414Skan  init_ggc_heuristics();
1535169699Skan  init_optimization_passes ();
1536132734Skan}
1537117414Skan
1538169699Skan/* Return true if the current target supports -fsection-anchors.  */
1539169699Skan
1540169699Skanstatic bool
1541169699Skantarget_supports_section_anchors_p (void)
1542169699Skan{
1543169699Skan  if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1544169699Skan    return false;
1545169699Skan
1546169699Skan  if (targetm.asm_out.output_anchor == NULL)
1547169699Skan    return false;
1548169699Skan
1549169699Skan  return true;
1550169699Skan}
1551169699Skan
1552132734Skan/* Process the options that have been parsed.  */
1553132734Skanstatic void
1554132734Skanprocess_options (void)
1555132734Skan{
1556161660Skan  /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1557161660Skan     This can happen with incorrect pre-processed input. */
1558161660Skan  debug_hooks = &do_nothing_debug_hooks;
1559161660Skan
1560132734Skan  /* Allow the front end to perform consistency checks and do further
1561132734Skan     initialization based on the command line options.  This hook also
1562132734Skan     sets the original filename if appropriate (e.g. foo.i -> foo.c)
1563132734Skan     so we can correctly initialize debug output.  */
1564169699Skan  no_backend = lang_hooks.post_options (&main_input_filename);
1565169699Skan#ifndef USE_MAPPED_LOCATION
1566132734Skan  input_filename = main_input_filename;
1567169699Skan#endif
156890282Sobrien
1569132734Skan#ifdef OVERRIDE_OPTIONS
1570132734Skan  /* Some machines may reject certain combinations of options.  */
1571132734Skan  OVERRIDE_OPTIONS;
157290282Sobrien#endif
157390282Sobrien
1574169699Skan  if (flag_section_anchors && !target_supports_section_anchors_p ())
1575169699Skan    {
1576169699Skan      warning (OPT_fsection_anchors,
1577169699Skan	       "this target does not support %qs", "-fsection-anchors");
1578169699Skan      flag_section_anchors = 0;
1579169699Skan    }
1580169699Skan
1581169699Skan  if (flag_short_enums == 2)
1582169699Skan    flag_short_enums = targetm.default_short_enums ();
1583169699Skan
1584132734Skan  /* Set aux_base_name if not already set.  */
1585132734Skan  if (aux_base_name)
1586132734Skan    ;
1587132734Skan  else if (main_input_filename)
158890282Sobrien    {
1589132734Skan      char *name = xstrdup (lbasename (main_input_filename));
159090282Sobrien
1591132734Skan      strip_off_ending (name, strlen (name));
1592132734Skan      aux_base_name = name;
159390282Sobrien    }
159496289Sobrien  else
1595132734Skan    aux_base_name = "gccaux";
159696289Sobrien
159790282Sobrien  /* Set up the align_*_log variables, defaulting them to 1 if they
159890282Sobrien     were still unset.  */
159990282Sobrien  if (align_loops <= 0) align_loops = 1;
160090282Sobrien  if (align_loops_max_skip > align_loops || !align_loops)
160190282Sobrien    align_loops_max_skip = align_loops - 1;
160290282Sobrien  align_loops_log = floor_log2 (align_loops * 2 - 1);
160390282Sobrien  if (align_jumps <= 0) align_jumps = 1;
160490282Sobrien  if (align_jumps_max_skip > align_jumps || !align_jumps)
160590282Sobrien    align_jumps_max_skip = align_jumps - 1;
160690282Sobrien  align_jumps_log = floor_log2 (align_jumps * 2 - 1);
160790282Sobrien  if (align_labels <= 0) align_labels = 1;
160890282Sobrien  align_labels_log = floor_log2 (align_labels * 2 - 1);
160990282Sobrien  if (align_labels_max_skip > align_labels || !align_labels)
161090282Sobrien    align_labels_max_skip = align_labels - 1;
161190282Sobrien  if (align_functions <= 0) align_functions = 1;
161290282Sobrien  align_functions_log = floor_log2 (align_functions * 2 - 1);
161390282Sobrien
161490282Sobrien  /* Unrolling all loops implies that standard loop unrolling must also
161590282Sobrien     be done.  */
161690282Sobrien  if (flag_unroll_all_loops)
161790282Sobrien    flag_unroll_loops = 1;
1618132734Skan
1619169699Skan  /* The loop unrolling code assumes that cse will be run after loop.
1620169699Skan     web and rename-registers also help when run after loop unrolling.  */
1621132734Skan
1622169699Skan  if (flag_rerun_cse_after_loop == AUTODETECT_VALUE)
1623169699Skan    flag_rerun_cse_after_loop = flag_unroll_loops || flag_peel_loops;
1624169699Skan  if (flag_web == AUTODETECT_VALUE)
1625169699Skan    flag_web = flag_unroll_loops || flag_peel_loops;
1626169699Skan  if (flag_rename_registers == AUTODETECT_VALUE)
1627169699Skan    flag_rename_registers = flag_unroll_loops || flag_peel_loops;
1628132734Skan
162990282Sobrien  if (flag_non_call_exceptions)
163090282Sobrien    flag_asynchronous_unwind_tables = 1;
163190282Sobrien  if (flag_asynchronous_unwind_tables)
163290282Sobrien    flag_unwind_tables = 1;
163390282Sobrien
1634132734Skan  /* Disable unit-at-a-time mode for frontends not supporting callgraph
1635132734Skan     interface.  */
1636132734Skan  if (flag_unit_at_a_time && ! lang_hooks.callgraph.expand_function)
1637132734Skan    flag_unit_at_a_time = 0;
1638132734Skan
1639169699Skan  if (!flag_unit_at_a_time)
1640169699Skan    flag_section_anchors = 0;
1641169699Skan
1642132734Skan  if (flag_value_profile_transformations)
1643132734Skan    flag_profile_values = 1;
1644132734Skan
164590282Sobrien  /* Warn about options that are not supported on this machine.  */
164690282Sobrien#ifndef INSN_SCHEDULING
164790282Sobrien  if (flag_schedule_insns || flag_schedule_insns_after_reload)
1648169699Skan    warning (0, "instruction scheduling not supported on this target machine");
164990282Sobrien#endif
165090282Sobrien#ifndef DELAY_SLOTS
165190282Sobrien  if (flag_delayed_branch)
1652169699Skan    warning (0, "this target machine does not have delayed branches");
165390282Sobrien#endif
165490282Sobrien
165590282Sobrien  user_label_prefix = USER_LABEL_PREFIX;
165690282Sobrien  if (flag_leading_underscore != -1)
165790282Sobrien    {
165890282Sobrien      /* If the default prefix is more complicated than "" or "_",
165990282Sobrien	 issue a warning and ignore this option.  */
166090282Sobrien      if (user_label_prefix[0] == 0 ||
166190282Sobrien	  (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
166290282Sobrien	{
166390282Sobrien	  user_label_prefix = flag_leading_underscore ? "_" : "";
166490282Sobrien	}
166590282Sobrien      else
1666169699Skan	warning (0, "-f%sleading-underscore not supported on this target machine",
166790282Sobrien		 flag_leading_underscore ? "" : "no-");
166890282Sobrien    }
166990282Sobrien
167090282Sobrien  /* If we are in verbose mode, write out the version and maybe all the
167190282Sobrien     option flags in use.  */
167290282Sobrien  if (version_flag)
167390282Sobrien    {
167490282Sobrien      print_version (stderr, "");
167590282Sobrien      if (! quiet_flag)
167690282Sobrien	print_switch_values (stderr, 0, MAX_LINE, "", " ", "\n");
167790282Sobrien    }
167890282Sobrien
167990282Sobrien  if (flag_syntax_only)
168090282Sobrien    {
168190282Sobrien      write_symbols = NO_DEBUG;
168290282Sobrien      profile_flag = 0;
168390282Sobrien    }
168490282Sobrien
1685132734Skan  /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1686132734Skan     level is 0.  */
1687132734Skan  if (debug_info_level == DINFO_LEVEL_NONE)
1688132734Skan    write_symbols = NO_DEBUG;
1689132734Skan
169090282Sobrien  /* Now we know write_symbols, set up the debug hooks based on it.
169190282Sobrien     By default we do nothing for debug output.  */
1692169699Skan  if (PREFERRED_DEBUGGING_TYPE == NO_DEBUG)
1693169699Skan    default_debug_hooks = &do_nothing_debug_hooks;
1694169699Skan#if defined(DBX_DEBUGGING_INFO)
1695169699Skan  else if (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG)
1696169699Skan    default_debug_hooks = &dbx_debug_hooks;
1697169699Skan#endif
1698169699Skan#if defined(XCOFF_DEBUGGING_INFO)
1699169699Skan  else if (PREFERRED_DEBUGGING_TYPE == XCOFF_DEBUG)
1700169699Skan    default_debug_hooks = &xcoff_debug_hooks;
1701169699Skan#endif
1702169699Skan#ifdef SDB_DEBUGGING_INFO
1703169699Skan  else if (PREFERRED_DEBUGGING_TYPE == SDB_DEBUG)
1704169699Skan    default_debug_hooks = &sdb_debug_hooks;
1705169699Skan#endif
1706169699Skan#ifdef DWARF2_DEBUGGING_INFO
1707169699Skan  else if (PREFERRED_DEBUGGING_TYPE == DWARF2_DEBUG)
1708169699Skan    default_debug_hooks = &dwarf2_debug_hooks;
1709169699Skan#endif
1710169699Skan#ifdef VMS_DEBUGGING_INFO
1711169699Skan  else if (PREFERRED_DEBUGGING_TYPE == VMS_DEBUG
1712169699Skan	   || PREFERRED_DEBUGGING_TYPE == VMS_AND_DWARF2_DEBUG)
1713169699Skan    default_debug_hooks = &vmsdbg_debug_hooks;
1714169699Skan#endif
1715169699Skan
1716132734Skan  if (write_symbols == NO_DEBUG)
1717161660Skan    ;
171890282Sobrien#if defined(DBX_DEBUGGING_INFO)
1719132734Skan  else if (write_symbols == DBX_DEBUG)
172090282Sobrien    debug_hooks = &dbx_debug_hooks;
172150615Sobrien#endif
172290282Sobrien#if defined(XCOFF_DEBUGGING_INFO)
1723132734Skan  else if (write_symbols == XCOFF_DEBUG)
172490282Sobrien    debug_hooks = &xcoff_debug_hooks;
172590282Sobrien#endif
172650615Sobrien#ifdef SDB_DEBUGGING_INFO
1727132734Skan  else if (write_symbols == SDB_DEBUG)
172890282Sobrien    debug_hooks = &sdb_debug_hooks;
172950615Sobrien#endif
173090282Sobrien#ifdef DWARF2_DEBUGGING_INFO
1731132734Skan  else if (write_symbols == DWARF2_DEBUG)
173290282Sobrien    debug_hooks = &dwarf2_debug_hooks;
173390282Sobrien#endif
173490282Sobrien#ifdef VMS_DEBUGGING_INFO
1735132734Skan  else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
173690282Sobrien    debug_hooks = &vmsdbg_debug_hooks;
173790282Sobrien#endif
1738132734Skan  else
1739132734Skan    error ("target system does not support the \"%s\" debug format",
1740132734Skan	   debug_type_names[write_symbols]);
174190282Sobrien
1742169699Skan  /* Now we know which debug output will be used so we can set
1743169699Skan     flag_var_tracking, flag_rename_registers if the user has
1744169699Skan     not specified them.  */
1745169699Skan  if (debug_info_level < DINFO_LEVEL_NORMAL
1746169699Skan      || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1747169699Skan    {
1748169699Skan      if (flag_var_tracking == 1)
1749169699Skan        {
1750169699Skan	  if (debug_info_level < DINFO_LEVEL_NORMAL)
1751169699Skan	    warning (0, "variable tracking requested, but useless unless "
1752169699Skan		     "producing debug info");
1753169699Skan	  else
1754169699Skan	    warning (0, "variable tracking requested, but not supported "
1755169699Skan		     "by this debug format");
1756169699Skan	}
1757169699Skan      flag_var_tracking = 0;
1758169699Skan    }
1759169699Skan
1760169699Skan  if (flag_rename_registers == AUTODETECT_VALUE)
1761169699Skan    flag_rename_registers = default_debug_hooks->var_location
1762169699Skan	    		    != do_nothing_debug_hooks.var_location;
1763169699Skan
1764169699Skan  if (flag_var_tracking == AUTODETECT_VALUE)
1765169699Skan    flag_var_tracking = optimize >= 1;
1766169699Skan
176790282Sobrien  /* If auxiliary info generation is desired, open the output file.
176890282Sobrien     This goes in the same directory as the source file--unlike
176990282Sobrien     all the other output files.  */
177090282Sobrien  if (flag_gen_aux_info)
177190282Sobrien    {
177290282Sobrien      aux_info_file = fopen (aux_info_file_name, "w");
177390282Sobrien      if (aux_info_file == 0)
1774169699Skan	fatal_error ("can%'t open %s: %m", aux_info_file_name);
177590282Sobrien    }
177690282Sobrien
177790282Sobrien  if (! targetm.have_named_sections)
177890282Sobrien    {
177990282Sobrien      if (flag_function_sections)
178090282Sobrien	{
1781169699Skan	  warning (0, "-ffunction-sections not supported for this target");
178290282Sobrien	  flag_function_sections = 0;
178390282Sobrien	}
178490282Sobrien      if (flag_data_sections)
178590282Sobrien	{
1786169699Skan	  warning (0, "-fdata-sections not supported for this target");
178790282Sobrien	  flag_data_sections = 0;
178890282Sobrien	}
178990282Sobrien    }
179090282Sobrien
179190282Sobrien  if (flag_function_sections && profile_flag)
179290282Sobrien    {
1793169699Skan      warning (0, "-ffunction-sections disabled; it makes profiling impossible");
179490282Sobrien      flag_function_sections = 0;
179590282Sobrien    }
179690282Sobrien
179790282Sobrien#ifndef HAVE_prefetch
179890282Sobrien  if (flag_prefetch_loop_arrays)
179990282Sobrien    {
1800169699Skan      warning (0, "-fprefetch-loop-arrays not supported for this target");
180190282Sobrien      flag_prefetch_loop_arrays = 0;
180290282Sobrien    }
180390282Sobrien#else
180490282Sobrien  if (flag_prefetch_loop_arrays && !HAVE_prefetch)
180590282Sobrien    {
1806169699Skan      warning (0, "-fprefetch-loop-arrays not supported for this target (try -march switches)");
180790282Sobrien      flag_prefetch_loop_arrays = 0;
180890282Sobrien    }
180990282Sobrien#endif
181090282Sobrien
181190282Sobrien  /* This combination of options isn't handled for i386 targets and doesn't
181290282Sobrien     make much sense anyway, so don't allow it.  */
181390282Sobrien  if (flag_prefetch_loop_arrays && optimize_size)
181490282Sobrien    {
1815169699Skan      warning (0, "-fprefetch-loop-arrays is not supported with -Os");
181690282Sobrien      flag_prefetch_loop_arrays = 0;
181790282Sobrien    }
181890282Sobrien
181990282Sobrien#ifndef OBJECT_FORMAT_ELF
1820169699Skan#ifndef OBJECT_FORMAT_MACHO
182190282Sobrien  if (flag_function_sections && write_symbols != NO_DEBUG)
1822169699Skan    warning (0, "-ffunction-sections may affect debugging on some targets");
182390282Sobrien#endif
1824169699Skan#endif
1825117414Skan
1826169699Skan  /* The presence of IEEE signaling NaNs, implies all math can trap.  */
1827169699Skan  if (flag_signaling_nans)
1828169699Skan    flag_trapping_math = 1;
1829169699Skan
1830169699Skan  /* With -fcx-limited-range, we do cheap and quick complex arithmetic.  */
1831169699Skan  if (flag_cx_limited_range)
1832169699Skan    flag_complex_method = 0;
1833169699Skan
1834169699Skan  /* Targets must be able to place spill slots at lower addresses.  If the
1835169699Skan     target already uses a soft frame pointer, the transition is trivial.  */
1836169699Skan  if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
1837169699Skan    {
1838169699Skan      warning (0, "-fstack-protector not supported for this target");
1839169699Skan      flag_stack_protect = 0;
1840169699Skan    }
1841169699Skan  if (!flag_stack_protect)
1842169699Skan    warn_stack_protect = 0;
1843169699Skan
1844169699Skan  /* ??? Unwind info is not correct around the CFG unless either a frame
1845169699Skan     pointer is present or A_O_A is set.  Fixing this requires rewriting
1846169699Skan     unwind info generation to be aware of the CFG and propagating states
1847169699Skan     around edges.  */
1848169699Skan  if (flag_unwind_tables && !ACCUMULATE_OUTGOING_ARGS
1849169699Skan      && flag_omit_frame_pointer)
1850169699Skan    {
1851169699Skan      warning (0, "unwind tables currently requires a frame pointer "
1852169699Skan	       "for correctness");
1853169699Skan      flag_omit_frame_pointer = 0;
1854169699Skan    }
185550615Sobrien}
1856132734Skan
1857117414Skan/* Initialize the compiler back end.  */
185890282Sobrienstatic void
1859132734Skanbackend_init (void)
186090282Sobrien{
186190282Sobrien  init_emit_once (debug_info_level == DINFO_LEVEL_NORMAL
186290282Sobrien		  || debug_info_level == DINFO_LEVEL_VERBOSE
186390282Sobrien#ifdef VMS_DEBUGGING_INFO
1864132734Skan		    /* Enable line number info for traceback.  */
186590282Sobrien		    || debug_info_level > DINFO_LEVEL_NONE
186690282Sobrien#endif
1867169699Skan		    || flag_test_coverage);
1868132734Skan
1869169699Skan  init_rtlanal ();
1870132734Skan  init_regs ();
1871117414Skan  init_fake_stack_mems ();
187290282Sobrien  init_alias_once ();
187390282Sobrien  init_reload ();
187490282Sobrien  init_varasm_once ();
187590282Sobrien
187690282Sobrien  /* The following initialization functions need to generate rtl, so
187790282Sobrien     provide a dummy function context for them.  */
187890282Sobrien  init_dummy_function_start ();
187990282Sobrien  init_expmed ();
188090282Sobrien  if (flag_caller_saves)
188190282Sobrien    init_caller_save ();
188290282Sobrien  expand_dummy_function_end ();
188390282Sobrien}
1884132734Skan
1885117414Skan/* Language-dependent initialization.  Returns nonzero on success.  */
188690282Sobrienstatic int
1887132734Skanlang_dependent_init (const char *name)
188850615Sobrien{
1889169699Skan  location_t save_loc = input_location;
189090282Sobrien  if (dump_base_name == 0)
1891169699Skan    dump_base_name = name && name[0] ? name : "gccdump";
1892132734Skan
1893132734Skan  /* Other front-end initialization.  */
1894169699Skan#ifdef USE_MAPPED_LOCATION
1895169699Skan  input_location = BUILTINS_LOCATION;
1896169699Skan#else
1897169699Skan  input_filename = "<built-in>";
1898169699Skan  input_line = 0;
1899169699Skan#endif
1900169699Skan  if (lang_hooks.init () == 0)
190190282Sobrien    return 0;
1902169699Skan  input_location = save_loc;
190390282Sobrien
190490282Sobrien  init_asm_output (name);
190590282Sobrien
190690282Sobrien  /* These create various _DECL nodes, so need to be called after the
190790282Sobrien     front end is initialized.  */
190890282Sobrien  init_eh ();
190990282Sobrien  init_optabs ();
191090282Sobrien
1911117414Skan  /* The following initialization functions need to generate rtl, so
1912117414Skan     provide a dummy function context for them.  */
1913117414Skan  init_dummy_function_start ();
1914117414Skan  init_expr_once ();
1915117414Skan  expand_dummy_function_end ();
1916117414Skan
191790282Sobrien  /* If dbx symbol table desired, initialize writing it and output the
191890282Sobrien     predefined types.  */
191990282Sobrien  timevar_push (TV_SYMOUT);
192090282Sobrien
1921169699Skan#if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
192290282Sobrien  if (dwarf2out_do_frame ())
192390282Sobrien    dwarf2out_frame_init ();
192490282Sobrien#endif
192590282Sobrien
192690282Sobrien  /* Now we have the correct original filename, we can initialize
192790282Sobrien     debug output.  */
192890282Sobrien  (*debug_hooks->init) (name);
192990282Sobrien
193090282Sobrien  timevar_pop (TV_SYMOUT);
193190282Sobrien
193290282Sobrien  return 1;
193350615Sobrien}
1934132734Skan
193590282Sobrien/* Clean up: close opened files, etc.  */
193650615Sobrien
193790282Sobrienstatic void
1938132734Skanfinalize (void)
193990282Sobrien{
194090282Sobrien  /* Close the dump files.  */
194190282Sobrien  if (flag_gen_aux_info)
194290282Sobrien    {
194390282Sobrien      fclose (aux_info_file);
194490282Sobrien      if (errorcount)
194590282Sobrien	unlink (aux_info_file_name);
194690282Sobrien    }
194750615Sobrien
194890282Sobrien  /* Close non-debugging input and output files.  Take special care to note
194990282Sobrien     whether fclose returns an error, since the pages might still be on the
195090282Sobrien     buffer chain while the file is open.  */
195190282Sobrien
195290282Sobrien  if (asm_out_file)
195390282Sobrien    {
195490282Sobrien      if (ferror (asm_out_file) != 0)
1955132734Skan	fatal_error ("error writing to %s: %m", asm_file_name);
195690282Sobrien      if (fclose (asm_out_file) != 0)
1957132734Skan	fatal_error ("error closing %s: %m", asm_file_name);
195890282Sobrien    }
195990282Sobrien
1960169699Skan  finish_optimization_passes ();
196190282Sobrien
196290282Sobrien  if (mem_report)
196390282Sobrien    {
196490282Sobrien      ggc_print_statistics ();
196590282Sobrien      stringpool_statistics ();
196690282Sobrien      dump_tree_statistics ();
1967132734Skan      dump_rtx_statistics ();
1968132734Skan      dump_varray_statistics ();
1969132734Skan      dump_alloc_pool_statistics ();
1970169699Skan      dump_ggc_loc_statistics ();
197190282Sobrien    }
197290282Sobrien
197390282Sobrien  /* Free up memory for the benefit of leak detectors.  */
197490282Sobrien  free_reg_info ();
197590282Sobrien
197690282Sobrien  /* Language-specific end of compilation actions.  */
1977169699Skan  lang_hooks.finish ();
197890282Sobrien}
1979132734Skan
198090282Sobrien/* Initialize the compiler, and compile the input file.  */
198190282Sobrienstatic void
1982132734Skando_compile (void)
198350615Sobrien{
1984132734Skan  /* Initialize timing first.  The C front ends read the main file in
1985132734Skan     the post_options hook, and C++ does file timings.  */
1986132734Skan  if (time_report || !quiet_flag  || flag_detailed_statistics)
1987132734Skan    timevar_init ();
1988132734Skan  timevar_start (TV_TOTAL);
1989117414Skan
199090282Sobrien  process_options ();
199190282Sobrien
1992132734Skan  /* Don't do any more if an error has already occurred.  */
1993132734Skan  if (!errorcount)
1994117414Skan    {
1995146908Skan      /* This must be run always, because it is needed to compute the FP
1996146908Skan	 predefined macros, such as __LDBL_MAX__, for targets using non
1997146908Skan	 default FP formats.  */
1998146908Skan      init_adjust_machine_modes ();
1999146908Skan
2000132734Skan      /* Set up the back-end if requested.  */
2001132734Skan      if (!no_backend)
2002132734Skan	backend_init ();
2003117414Skan
2004132734Skan      /* Language-dependent initialization.  Returns true on success.  */
2005132734Skan      if (lang_dependent_init (main_input_filename))
2006169699Skan	compile_file ();
200790282Sobrien
2008132734Skan      finalize ();
2009132734Skan    }
201090282Sobrien
201190282Sobrien  /* Stop timing and print the times.  */
201290282Sobrien  timevar_stop (TV_TOTAL);
201390282Sobrien  timevar_print (stderr);
201450615Sobrien}
2015132734Skan
201690282Sobrien/* Entry point of cc1, cc1plus, jc1, f771, etc.
201790282Sobrien   Exit code is FATAL_EXIT_CODE if can't open files or if there were
201890282Sobrien   any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
201990282Sobrien
202090282Sobrien   It is not safe to call this function more than once.  */
202190282Sobrien
202290282Sobrienint
2023132734Skantoplev_main (unsigned int argc, const char **argv)
202490282Sobrien{
2025132734Skan  save_argv = argv;
2026132734Skan
202790282Sobrien  /* Initialization of GCC's environment, and diagnostics.  */
2028117414Skan  general_init (argv[0]);
202990282Sobrien
203090282Sobrien  /* Parse the options and do minimal processing; basically just
203190282Sobrien     enough to default flags appropriately.  */
2032132734Skan  decode_options (argc, argv);
203390282Sobrien
2034132734Skan  randomize ();
2035132734Skan
203690282Sobrien  /* Exit early if we can (e.g. -help).  */
2037117414Skan  if (!exit_after_options)
203890282Sobrien    do_compile ();
203990282Sobrien
204090282Sobrien  if (errorcount || sorrycount)
204190282Sobrien    return (FATAL_EXIT_CODE);
204290282Sobrien
204390282Sobrien  return (SUCCESS_EXIT_CODE);
204490282Sobrien}
2045