]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/toplev.c
Update copyright years.
[thirdparty/gcc.git] / gcc / toplev.c
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "alloc-pool.h"
34 #include "timevar.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "optabs-libfuncs.h"
38 #include "insn-config.h"
39 #include "ira.h"
40 #include "recog.h"
41 #include "cgraph.h"
42 #include "coverage.h"
43 #include "diagnostic.h"
44 #include "varasm.h"
45 #include "tree-inline.h"
46 #include "realmpfr.h" /* For GMP/MPFR/MPC versions, in print_version. */
47 #include "version.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "output.h"
51 #include "toplev.h"
52 #include "expr.h"
53 #include "intl.h"
54 #include "tree-diagnostic.h"
55 #include "reload.h"
56 #include "lra.h"
57 #include "dwarf2asm.h"
58 #include "debug.h"
59 #include "common/common-target.h"
60 #include "langhooks.h"
61 #include "cfgloop.h" /* for init_set_costs */
62 #include "hosthooks.h"
63 #include "opts.h"
64 #include "opts-diagnostic.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "asan.h"
68 #include "tsan.h"
69 #include "plugin.h"
70 #include "context.h"
71 #include "pass_manager.h"
72 #include "auto-profile.h"
73 #include "dwarf2out.h"
74 #include "ipa-reference.h"
75 #include "symbol-summary.h"
76 #include "tree-vrp.h"
77 #include "ipa-prop.h"
78 #include "gcse.h"
79 #include "omp-offload.h"
80 #include "edit-context.h"
81 #include "tree-pass.h"
82 #include "dumpfile.h"
83 #include "ipa-fnsummary.h"
84 #include "dump-context.h"
85 #include "print-tree.h"
86 #include "optinfo-emit-json.h"
87 #include "ipa-modref-tree.h"
88 #include "ipa-modref.h"
89 #include "dbgcnt.h"
90
91 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
92 #include "dbxout.h"
93 #endif
94
95 #ifdef XCOFF_DEBUGGING_INFO
96 #include "xcoffout.h" /* Needed for external data declarations. */
97 #endif
98
99 #include "selftest.h"
100
101 #ifdef HAVE_isl
102 #include <isl/version.h>
103 #endif
104
105 static void general_init (const char *, bool);
106 static void do_compile ();
107 static void process_options (void);
108 static void backend_init (void);
109 static int lang_dependent_init (const char *);
110 static void init_asm_output (const char *);
111 static void finalize (bool);
112
113 static void crash_signal (int) ATTRIBUTE_NORETURN;
114 static void compile_file (void);
115
116 /* True if we don't need a backend (e.g. preprocessing only). */
117 static bool no_backend;
118
119 /* Decoded options, and number of such options. */
120 struct cl_decoded_option *save_decoded_options;
121 unsigned int save_decoded_options_count;
122
123 /* Used to enable -fvar-tracking, -fweb and -frename-registers according
124 to optimize in process_options (). */
125 #define AUTODETECT_VALUE 2
126
127 /* Debug hooks - dependent upon command line options. */
128
129 const struct gcc_debug_hooks *debug_hooks;
130
131 /* The FUNCTION_DECL for the function currently being compiled,
132 or 0 if between functions. */
133 tree current_function_decl;
134
135 /* Set to the FUNC_BEGIN label of the current function, or NULL
136 if none. */
137 const char * current_function_func_begin_label;
138
139 /* A random sequence of characters, unless overridden by user. */
140 static const char *flag_random_seed;
141
142 /* A local time stamp derived from the time of compilation. It will be
143 zero if the system cannot provide a time. It will be -1u, if the
144 user has specified a particular random seed. */
145 unsigned local_tick;
146
147 /* Random number for this compilation */
148 HOST_WIDE_INT random_seed;
149
150 /* -f flags. */
151
152 /* When non-NULL, indicates that whenever space is allocated on the
153 stack, the resulting stack pointer must not pass this
154 address---that is, for stacks that grow downward, the stack pointer
155 must always be greater than or equal to this address; for stacks
156 that grow upward, the stack pointer must be less than this address.
157 At present, the rtx may be either a REG or a SYMBOL_REF, although
158 the support provided depends on the backend. */
159 rtx stack_limit_rtx;
160
161 class target_flag_state default_target_flag_state;
162 #if SWITCHABLE_TARGET
163 class target_flag_state *this_target_flag_state = &default_target_flag_state;
164 #else
165 #define this_target_flag_state (&default_target_flag_state)
166 #endif
167
168 /* The user symbol prefix after having resolved same. */
169 const char *user_label_prefix;
170
171 /* Output files for assembler code (real compiler output)
172 and debugging dumps. */
173
174 FILE *asm_out_file;
175 FILE *aux_info_file;
176 FILE *callgraph_info_file = NULL;
177 static bitmap callgraph_info_external_printed;
178 FILE *stack_usage_file = NULL;
179
180 /* The current working directory of a translation. It's generally the
181 directory from which compilation was initiated, but a preprocessed
182 file may specify the original directory in which it was
183 created. */
184
185 static const char *src_pwd;
186
187 /* Initialize src_pwd with the given string, and return true. If it
188 was already initialized, return false. As a special case, it may
189 be called with a NULL argument to test whether src_pwd has NOT been
190 initialized yet. */
191
192 bool
193 set_src_pwd (const char *pwd)
194 {
195 if (src_pwd)
196 {
197 if (strcmp (src_pwd, pwd) == 0)
198 return true;
199 else
200 return false;
201 }
202
203 src_pwd = xstrdup (pwd);
204 return true;
205 }
206
207 /* Return the directory from which the translation unit was initiated,
208 in case set_src_pwd() was not called before to assign it a
209 different value. */
210
211 const char *
212 get_src_pwd (void)
213 {
214 if (! src_pwd)
215 {
216 src_pwd = getpwd ();
217 if (!src_pwd)
218 src_pwd = ".";
219 }
220
221 return src_pwd;
222 }
223
224 /* Called when the start of a function definition is parsed,
225 this function prints on stderr the name of the function. */
226 void
227 announce_function (tree decl)
228 {
229 if (!quiet_flag)
230 {
231 if (rtl_dump_and_exit)
232 fprintf (stderr, "%s ",
233 identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl))));
234 else
235 fprintf (stderr, " %s",
236 identifier_to_locale (lang_hooks.decl_printable_name (decl, 2)));
237 fflush (stderr);
238 pp_needs_newline (global_dc->printer) = true;
239 diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
240 }
241 }
242
243 /* Initialize local_tick with the time of day, or -1 if
244 flag_random_seed is set. */
245
246 static void
247 init_local_tick (void)
248 {
249 if (!flag_random_seed)
250 {
251 #ifdef HAVE_GETTIMEOFDAY
252 {
253 struct timeval tv;
254
255 gettimeofday (&tv, NULL);
256 local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000;
257 }
258 #else
259 {
260 time_t now = time (NULL);
261
262 if (now != (time_t)-1)
263 local_tick = (unsigned) now;
264 }
265 #endif
266 }
267 else
268 local_tick = -1;
269 }
270
271 /* Obtain the random_seed. Unless NOINIT, initialize it if
272 it's not provided in the command line. */
273
274 HOST_WIDE_INT
275 get_random_seed (bool noinit)
276 {
277 if (!random_seed && !noinit)
278 {
279 int fd = open ("/dev/urandom", O_RDONLY);
280 if (fd >= 0)
281 {
282 if (read (fd, &random_seed, sizeof (random_seed))
283 != sizeof (random_seed))
284 random_seed = 0;
285 close (fd);
286 }
287 if (!random_seed)
288 random_seed = local_tick ^ getpid ();
289 }
290 return random_seed;
291 }
292
293 /* Set flag_random_seed to VAL, and if non-null, reinitialize random_seed. */
294
295 void
296 set_random_seed (const char *val)
297 {
298 flag_random_seed = val;
299 if (flag_random_seed)
300 {
301 char *endp;
302
303 /* When the driver passed in a hex number don't crc it again */
304 random_seed = strtoul (flag_random_seed, &endp, 0);
305 if (!(endp > flag_random_seed && *endp == 0))
306 random_seed = crc32_string (0, flag_random_seed);
307 }
308 }
309
310 /* Handler for fatal signals, such as SIGSEGV. These are transformed
311 into ICE messages, which is much more user friendly. In case the
312 error printer crashes, reset the signal to prevent infinite recursion. */
313
314 static void
315 crash_signal (int signo)
316 {
317 signal (signo, SIG_DFL);
318
319 /* If we crashed while processing an ASM statement, then be a little more
320 graceful. It's most likely the user's fault. */
321 if (this_is_asm_operands)
322 {
323 output_operand_lossage ("unrecoverable error");
324 exit (FATAL_EXIT_CODE);
325 }
326
327 internal_error ("%s", strsignal (signo));
328 }
329
330 /* A subroutine of wrapup_global_declarations. We've come to the end of
331 the compilation unit. All deferred variables should be undeferred,
332 and all incomplete decls should be finalized. */
333
334 void
335 wrapup_global_declaration_1 (tree decl)
336 {
337 /* We're not deferring this any longer. Assignment is conditional to
338 avoid needlessly dirtying PCH pages. */
339 if (CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_WITH_VIS)
340 && DECL_DEFER_OUTPUT (decl) != 0)
341 DECL_DEFER_OUTPUT (decl) = 0;
342
343 if (VAR_P (decl) && DECL_SIZE (decl) == 0)
344 lang_hooks.finish_incomplete_decl (decl);
345 }
346
347 /* A subroutine of wrapup_global_declarations. Decide whether or not DECL
348 needs to be output. Return true if it is output. */
349
350 bool
351 wrapup_global_declaration_2 (tree decl)
352 {
353 if (TREE_ASM_WRITTEN (decl) || DECL_EXTERNAL (decl)
354 || (VAR_P (decl) && DECL_HAS_VALUE_EXPR_P (decl)))
355 return false;
356
357 /* Don't write out static consts, unless we still need them.
358
359 We also keep static consts if not optimizing (for debugging),
360 unless the user specified -fno-keep-static-consts.
361 ??? They might be better written into the debug information.
362 This is possible when using DWARF.
363
364 A language processor that wants static constants to be always
365 written out (even if it is not used) is responsible for
366 calling rest_of_decl_compilation itself. E.g. the C front-end
367 calls rest_of_decl_compilation from finish_decl.
368 One motivation for this is that is conventional in some
369 environments to write things like:
370 static const char rcsid[] = "... version string ...";
371 intending to force the string to be in the executable.
372
373 A language processor that would prefer to have unneeded
374 static constants "optimized away" would just defer writing
375 them out until here. E.g. C++ does this, because static
376 constants are often defined in header files.
377
378 ??? A tempting alternative (for both C and C++) would be
379 to force a constant to be written if and only if it is
380 defined in a main file, as opposed to an include file. */
381
382 if (VAR_P (decl) && TREE_STATIC (decl))
383 {
384 varpool_node *node;
385 bool needed = true;
386 node = varpool_node::get (decl);
387
388 if (!node && flag_ltrans)
389 needed = false;
390 else if (node && node->definition)
391 needed = false;
392 else if (node && node->alias)
393 needed = false;
394 else if (!symtab->global_info_ready
395 && (TREE_USED (decl)
396 || TREE_USED (DECL_ASSEMBLER_NAME (decl))))
397 /* needed */;
398 else if (node && node->analyzed)
399 /* needed */;
400 else if (DECL_COMDAT (decl))
401 needed = false;
402 else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
403 && (optimize || !flag_keep_static_consts
404 || DECL_ARTIFICIAL (decl)))
405 needed = false;
406
407 if (needed)
408 {
409 rest_of_decl_compilation (decl, 1, 1);
410 return true;
411 }
412 }
413
414 return false;
415 }
416
417 /* Do any final processing required for the declarations in VEC, of
418 which there are LEN. We write out inline functions and variables
419 that have been deferred until this point, but which are required.
420 Returns nonzero if anything was put out. */
421
422 bool
423 wrapup_global_declarations (tree *vec, int len)
424 {
425 bool reconsider, output_something = false;
426 int i;
427
428 for (i = 0; i < len; i++)
429 wrapup_global_declaration_1 (vec[i]);
430
431 /* Now emit any global variables or functions that we have been
432 putting off. We need to loop in case one of the things emitted
433 here references another one which comes earlier in the list. */
434 do
435 {
436 reconsider = false;
437 for (i = 0; i < len; i++)
438 reconsider |= wrapup_global_declaration_2 (vec[i]);
439 if (reconsider)
440 output_something = true;
441 }
442 while (reconsider);
443
444 return output_something;
445 }
446
447 /* Compile an entire translation unit. Write a file of assembly
448 output and various debugging dumps. */
449
450 static void
451 compile_file (void)
452 {
453 timevar_start (TV_PHASE_PARSING);
454 timevar_push (TV_PARSE_GLOBAL);
455
456 /* Parse entire file and generate initial debug information. */
457 lang_hooks.parse_file ();
458
459 timevar_pop (TV_PARSE_GLOBAL);
460 timevar_stop (TV_PHASE_PARSING);
461
462 if (flag_dump_locations)
463 dump_location_info (stderr);
464
465 /* Compilation is now finished except for writing
466 what's left of the symbol table output. */
467
468 if (flag_syntax_only || flag_wpa)
469 return;
470
471 /* Reset maximum_field_alignment, it can be adjusted by #pragma pack
472 and this shouldn't influence any types built by the middle-end
473 from now on (like gcov_info_type). */
474 maximum_field_alignment = initial_max_fld_align * BITS_PER_UNIT;
475
476 ggc_protect_identifiers = false;
477
478 /* Run the actual compilation process. */
479 if (!in_lto_p)
480 {
481 timevar_start (TV_PHASE_OPT_GEN);
482 symtab->finalize_compilation_unit ();
483 timevar_stop (TV_PHASE_OPT_GEN);
484 }
485
486 /* Perform any post compilation-proper parser cleanups and
487 processing. This is currently only needed for the C++ parser,
488 which can be hopefully cleaned up so this hook is no longer
489 necessary. */
490 if (lang_hooks.decls.post_compilation_parsing_cleanups)
491 lang_hooks.decls.post_compilation_parsing_cleanups ();
492
493 dump_context::get ().finish_any_json_writer ();
494
495 if (seen_error ())
496 return;
497
498 timevar_start (TV_PHASE_LATE_ASM);
499
500 /* Compilation unit is finalized. When producing non-fat LTO object, we are
501 basically finished. */
502 if ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
503 || !flag_lto || flag_fat_lto_objects)
504 {
505 /* File-scope initialization for AddressSanitizer. */
506 if (flag_sanitize & SANITIZE_ADDRESS)
507 asan_finish_file ();
508
509 if (flag_sanitize & SANITIZE_THREAD)
510 tsan_finish_file ();
511
512 if (gate_hwasan ())
513 hwasan_finish_file ();
514
515 omp_finish_file ();
516
517 output_shared_constant_pool ();
518 output_object_blocks ();
519 finish_tm_clone_pairs ();
520
521 /* Write out any pending weak symbol declarations. */
522 weak_finish ();
523
524 /* This must be at the end before unwind and debug info.
525 Some target ports emit PIC setup thunks here. */
526 targetm.asm_out.code_end ();
527
528 /* Do dbx symbols. */
529 timevar_push (TV_SYMOUT);
530
531 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_UNWIND_INFO
532 dwarf2out_frame_finish ();
533 #endif
534
535 debuginfo_start ();
536 (*debug_hooks->finish) (main_input_filename);
537 debuginfo_stop ();
538 timevar_pop (TV_SYMOUT);
539
540 /* Output some stuff at end of file if nec. */
541
542 dw2_output_indirect_constants ();
543
544 /* Flush any pending external directives. */
545 process_pending_assemble_externals ();
546 }
547
548 /* Let linker plugin know that this is a slim object and must be LTOed
549 even when user did not ask for it. */
550 if (flag_generate_lto && !flag_fat_lto_objects)
551 {
552 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
553 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, NULL_TREE, "__gnu_lto_slim",
554 HOST_WIDE_INT_1U, 8);
555 #elif defined ASM_OUTPUT_ALIGNED_COMMON
556 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, "__gnu_lto_slim",
557 HOST_WIDE_INT_1U, 8);
558 #else
559 ASM_OUTPUT_COMMON (asm_out_file, "__gnu_lto_slim",
560 HOST_WIDE_INT_1U,
561 HOST_WIDE_INT_1U);
562 #endif
563 }
564
565 /* Attach a special .ident directive to the end of the file to identify
566 the version of GCC which compiled this code. The format of the .ident
567 string is patterned after the ones produced by native SVR4 compilers. */
568 if (!flag_no_ident)
569 {
570 const char *pkg_version = "(GNU) ";
571 char *ident_str;
572
573 if (strcmp ("(GCC) ", pkgversion_string))
574 pkg_version = pkgversion_string;
575
576 ident_str = ACONCAT (("GCC: ", pkg_version, version_string, NULL));
577 targetm.asm_out.output_ident (ident_str);
578 }
579
580 /* Auto profile finalization. */
581 if (flag_auto_profile)
582 end_auto_profile ();
583
584 /* Invoke registered plugin callbacks. */
585 invoke_plugin_callbacks (PLUGIN_FINISH_UNIT, NULL);
586
587 /* This must be at the end. Some target ports emit end of file directives
588 into the assembly file here, and hence we cannot output anything to the
589 assembly file after this point. */
590 targetm.asm_out.file_end ();
591
592 timevar_stop (TV_PHASE_LATE_ASM);
593 }
594
595 /* Print version information to FILE.
596 Each line begins with INDENT (for the case where FILE is the
597 assembler output file).
598
599 If SHOW_GLOBAL_STATE is true (for cc1 etc), we are within the compiler
600 proper and can print pertinent state (e.g. params and plugins).
601
602 If SHOW_GLOBAL_STATE is false (for use by libgccjit), we are outside the
603 compiler, and we don't hold the mutex on the compiler's global state:
604 we can't print params and plugins, since they might not be initialized,
605 or might be being manipulated by a compile running in another
606 thread. */
607
608 void
609 print_version (FILE *file, const char *indent, bool show_global_state)
610 {
611 static const char fmt1[] =
612 #ifdef __GNUC__
613 N_("%s%s%s %sversion %s (%s)\n%s\tcompiled by GNU C version %s, ")
614 #else
615 N_("%s%s%s %sversion %s (%s) compiled by CC, ")
616 #endif
617 ;
618 static const char fmt2[] =
619 N_("GMP version %s, MPFR version %s, MPC version %s, isl version %s\n");
620 static const char fmt3[] =
621 N_("%s%swarning: %s header version %s differs from library version %s.\n");
622 static const char fmt4[] =
623 N_("%s%sGGC heuristics: --param ggc-min-expand=%d --param ggc-min-heapsize=%d\n");
624 #ifndef __VERSION__
625 #define __VERSION__ "[?]"
626 #endif
627 fprintf (file,
628 file == stderr ? _(fmt1) : fmt1,
629 indent, *indent != 0 ? " " : "",
630 lang_hooks.name, pkgversion_string, version_string, TARGET_NAME,
631 indent, __VERSION__);
632
633 /* We need to stringify the GMP macro values. Ugh, gmp_version has
634 two string formats, "i.j.k" and "i.j" when k is zero. As of
635 gmp-4.3.0, GMP always uses the 3 number format. */
636 #define GCC_GMP_STRINGIFY_VERSION3(X) #X
637 #define GCC_GMP_STRINGIFY_VERSION2(X) GCC_GMP_STRINGIFY_VERSION3 (X)
638 #define GCC_GMP_VERSION_NUM(X,Y,Z) (((X) << 16L) | ((Y) << 8) | (Z))
639 #define GCC_GMP_VERSION \
640 GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
641 #if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4,3,0) && __GNU_MP_VERSION_PATCHLEVEL == 0
642 #define GCC_GMP_STRINGIFY_VERSION \
643 GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION) "." \
644 GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_MINOR)
645 #else
646 #define GCC_GMP_STRINGIFY_VERSION \
647 GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION) "." \
648 GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_MINOR) "." \
649 GCC_GMP_STRINGIFY_VERSION2 (__GNU_MP_VERSION_PATCHLEVEL)
650 #endif
651 fprintf (file,
652 file == stderr ? _(fmt2) : fmt2,
653 GCC_GMP_STRINGIFY_VERSION, MPFR_VERSION_STRING, MPC_VERSION_STRING,
654 #ifndef HAVE_isl
655 "none"
656 #else
657 isl_version ()
658 #endif
659 );
660 if (strcmp (GCC_GMP_STRINGIFY_VERSION, gmp_version))
661 fprintf (file,
662 file == stderr ? _(fmt3) : fmt3,
663 indent, *indent != 0 ? " " : "",
664 "GMP", GCC_GMP_STRINGIFY_VERSION, gmp_version);
665 if (strcmp (MPFR_VERSION_STRING, mpfr_get_version ()))
666 fprintf (file,
667 file == stderr ? _(fmt3) : fmt3,
668 indent, *indent != 0 ? " " : "",
669 "MPFR", MPFR_VERSION_STRING, mpfr_get_version ());
670 if (strcmp (MPC_VERSION_STRING, mpc_get_version ()))
671 fprintf (file,
672 file == stderr ? _(fmt3) : fmt3,
673 indent, *indent != 0 ? " " : "",
674 "MPC", MPC_VERSION_STRING, mpc_get_version ());
675
676 if (show_global_state)
677 {
678 fprintf (file,
679 file == stderr ? _(fmt4) : fmt4,
680 indent, *indent != 0 ? " " : "",
681 param_ggc_min_expand, param_ggc_min_heapsize);
682
683 print_plugins_versions (file, indent);
684 }
685 }
686
687
688
689 /* Open assembly code output file. Do this even if -fsyntax-only is
690 on, because then the driver will have provided the name of a
691 temporary file or bit bucket for us. NAME is the file specified on
692 the command line, possibly NULL. */
693 static void
694 init_asm_output (const char *name)
695 {
696 if (name == NULL && asm_file_name == 0)
697 asm_out_file = stdout;
698 else
699 {
700 if (asm_file_name == 0)
701 {
702 int len = strlen (dump_base_name);
703 char *dumpname = XNEWVEC (char, len + 6);
704
705 memcpy (dumpname, dump_base_name, len + 1);
706 strip_off_ending (dumpname, len);
707 strcat (dumpname, ".s");
708 asm_file_name = dumpname;
709 }
710 if (!strcmp (asm_file_name, "-"))
711 asm_out_file = stdout;
712 else if (!canonical_filename_eq (asm_file_name, name)
713 || !strcmp (asm_file_name, HOST_BIT_BUCKET))
714 asm_out_file = fopen (asm_file_name, "w");
715 else
716 /* Use UNKOWN_LOCATION to prevent gcc from printing the first
717 line in the current file. */
718 fatal_error (UNKNOWN_LOCATION,
719 "input file %qs is the same as output file",
720 asm_file_name);
721 if (asm_out_file == 0)
722 fatal_error (UNKNOWN_LOCATION,
723 "cannot open %qs for writing: %m", asm_file_name);
724 }
725
726 if (!flag_syntax_only)
727 {
728 targetm.asm_out.file_start ();
729
730 if (flag_record_gcc_switches)
731 {
732 if (targetm.asm_out.record_gcc_switches)
733 {
734 const char *str
735 = gen_producer_string (lang_hooks.name,
736 save_decoded_options,
737 save_decoded_options_count);
738 targetm.asm_out.record_gcc_switches (str);
739 }
740 else
741 inform (UNKNOWN_LOCATION,
742 "%<-frecord-gcc-switches%> is not supported by "
743 "the current target");
744 }
745
746 if (flag_verbose_asm)
747 {
748 print_version (asm_out_file, ASM_COMMENT_START, true);
749 fputs (ASM_COMMENT_START, asm_out_file);
750 fputs (" options passed: ", asm_out_file);
751 fputs (gen_command_line_string (save_decoded_options,
752 save_decoded_options_count),
753 asm_out_file);
754 fputc ('\n', asm_out_file);
755 }
756 }
757 }
758
759 /* A helper function; used as the reallocator function for cpp's line
760 table. */
761 static void *
762 realloc_for_line_map (void *ptr, size_t len)
763 {
764 return ggc_realloc (ptr, len);
765 }
766
767 /* A helper function: used as the allocator function for
768 identifier_to_locale. */
769 static void *
770 alloc_for_identifier_to_locale (size_t len)
771 {
772 return ggc_alloc_atomic (len);
773 }
774
775 /* Output stack usage information. */
776 static void
777 output_stack_usage_1 (FILE *cf)
778 {
779 static bool warning_issued = false;
780 enum stack_usage_kind_type { STATIC = 0, DYNAMIC, DYNAMIC_BOUNDED };
781 const char *stack_usage_kind_str[] = {
782 "static",
783 "dynamic",
784 "dynamic,bounded"
785 };
786 HOST_WIDE_INT stack_usage = current_function_static_stack_size;
787 enum stack_usage_kind_type stack_usage_kind;
788
789 if (stack_usage < 0)
790 {
791 if (!warning_issued)
792 {
793 warning (0, "stack usage computation not supported for this target");
794 warning_issued = true;
795 }
796 return;
797 }
798
799 stack_usage_kind = STATIC;
800
801 /* Add the maximum amount of space pushed onto the stack. */
802 if (maybe_ne (current_function_pushed_stack_size, 0))
803 {
804 HOST_WIDE_INT extra;
805 if (current_function_pushed_stack_size.is_constant (&extra))
806 {
807 stack_usage += extra;
808 stack_usage_kind = DYNAMIC_BOUNDED;
809 }
810 else
811 {
812 extra = constant_lower_bound (current_function_pushed_stack_size);
813 stack_usage += extra;
814 stack_usage_kind = DYNAMIC;
815 }
816 }
817
818 /* Now on to the tricky part: dynamic stack allocation. */
819 if (current_function_allocates_dynamic_stack_space)
820 {
821 if (stack_usage_kind != DYNAMIC)
822 {
823 if (current_function_has_unbounded_dynamic_stack_size)
824 stack_usage_kind = DYNAMIC;
825 else
826 stack_usage_kind = DYNAMIC_BOUNDED;
827 }
828
829 /* Add the size even in the unbounded case, this can't hurt. */
830 stack_usage += current_function_dynamic_stack_size;
831 }
832
833 if (cf && flag_callgraph_info & CALLGRAPH_INFO_STACK_USAGE)
834 fprintf (cf, "\\n" HOST_WIDE_INT_PRINT_DEC " bytes (%s)",
835 stack_usage,
836 stack_usage_kind_str[stack_usage_kind]);
837
838 if (stack_usage_file)
839 {
840 print_decl_identifier (stack_usage_file, current_function_decl,
841 PRINT_DECL_ORIGIN | PRINT_DECL_NAME);
842 fprintf (stack_usage_file, "\t" HOST_WIDE_INT_PRINT_DEC"\t%s\n",
843 stack_usage, stack_usage_kind_str[stack_usage_kind]);
844 }
845
846 if (warn_stack_usage >= 0 && warn_stack_usage < HOST_WIDE_INT_MAX)
847 {
848 const location_t loc = DECL_SOURCE_LOCATION (current_function_decl);
849
850 if (stack_usage_kind == DYNAMIC)
851 warning_at (loc, OPT_Wstack_usage_, "stack usage might be unbounded");
852 else if (stack_usage > warn_stack_usage)
853 {
854 if (stack_usage_kind == DYNAMIC_BOUNDED)
855 warning_at (loc,
856 OPT_Wstack_usage_, "stack usage might be %wu bytes",
857 stack_usage);
858 else
859 warning_at (loc, OPT_Wstack_usage_, "stack usage is %wu bytes",
860 stack_usage);
861 }
862 }
863 }
864
865 /* Dump placeholder node for indirect calls in VCG format. */
866
867 #define INDIRECT_CALL_NAME "__indirect_call"
868
869 static void
870 dump_final_node_vcg_start (FILE *f, tree decl)
871 {
872 fputs ("node: { title: \"", f);
873 if (decl)
874 print_decl_identifier (f, decl, PRINT_DECL_UNIQUE_NAME);
875 else
876 fputs (INDIRECT_CALL_NAME, f);
877 fputs ("\" label: \"", f);
878 if (decl)
879 {
880 print_decl_identifier (f, decl, PRINT_DECL_NAME);
881 fputs ("\\n", f);
882 print_decl_identifier (f, decl, PRINT_DECL_ORIGIN);
883 }
884 else
885 fputs ("Indirect Call Placeholder", f);
886 }
887
888 /* Dump final cgraph edge in VCG format. */
889
890 static void
891 dump_final_callee_vcg (FILE *f, location_t location, tree callee)
892 {
893 if ((!callee || DECL_EXTERNAL (callee))
894 && bitmap_set_bit (callgraph_info_external_printed,
895 callee ? DECL_UID (callee) + 1 : 0))
896 {
897 dump_final_node_vcg_start (f, callee);
898 fputs ("\" shape : ellipse }\n", f);
899 }
900
901 fputs ("edge: { sourcename: \"", f);
902 print_decl_identifier (f, current_function_decl, PRINT_DECL_UNIQUE_NAME);
903 fputs ("\" targetname: \"", f);
904 if (callee)
905 print_decl_identifier (f, callee, PRINT_DECL_UNIQUE_NAME);
906 else
907 fputs (INDIRECT_CALL_NAME, f);
908 if (LOCATION_LOCUS (location) != UNKNOWN_LOCATION)
909 {
910 expanded_location loc;
911 fputs ("\" label: \"", f);
912 loc = expand_location (location);
913 fprintf (f, "%s:%d:%d", loc.file, loc.line, loc.column);
914 }
915 fputs ("\" }\n", f);
916 }
917
918 /* Dump final cgraph node in VCG format. */
919
920 static void
921 dump_final_node_vcg (FILE *f)
922 {
923 dump_final_node_vcg_start (f, current_function_decl);
924
925 if (flag_stack_usage_info
926 || (flag_callgraph_info & CALLGRAPH_INFO_STACK_USAGE))
927 output_stack_usage_1 (f);
928
929 if (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC)
930 {
931 fprintf (f, "\\n%u dynamic objects", vec_safe_length (cfun->su->dallocs));
932
933 unsigned i;
934 callinfo_dalloc *cda;
935 FOR_EACH_VEC_SAFE_ELT (cfun->su->dallocs, i, cda)
936 {
937 expanded_location loc = expand_location (cda->location);
938 fprintf (f, "\\n %s", cda->name);
939 fprintf (f, " %s:%d:%d", loc.file, loc.line, loc.column);
940 }
941
942 vec_free (cfun->su->dallocs);
943 cfun->su->dallocs = NULL;
944 }
945
946 fputs ("\" }\n", f);
947
948 unsigned i;
949 callinfo_callee *c;
950 FOR_EACH_VEC_SAFE_ELT (cfun->su->callees, i, c)
951 dump_final_callee_vcg (f, c->location, c->decl);
952 vec_free (cfun->su->callees);
953 cfun->su->callees = NULL;
954 }
955
956 /* Output stack usage and callgraph info, as requested. */
957 void
958 output_stack_usage (void)
959 {
960 if (flag_callgraph_info)
961 dump_final_node_vcg (callgraph_info_file);
962 else
963 output_stack_usage_1 (NULL);
964 }
965
966 /* Open an auxiliary output file. */
967 static FILE *
968 open_auxiliary_file (const char *ext)
969 {
970 char *filename;
971 FILE *file;
972
973 filename = concat (aux_base_name, ".", ext, NULL);
974 file = fopen (filename, "w");
975 if (!file)
976 fatal_error (input_location, "cannot open %s for writing: %m", filename);
977 free (filename);
978 return file;
979 }
980
981 /* Alternative diagnostics callback for reentered ICE reporting. */
982
983 static void
984 internal_error_reentered (diagnostic_context *, const char *, va_list *)
985 {
986 /* Flush the dump file if emergency_dump_function itself caused an ICE. */
987 if (dump_file)
988 fflush (dump_file);
989 }
990
991 /* Auxiliary callback for the diagnostics code. */
992
993 static void
994 internal_error_function (diagnostic_context *, const char *, va_list *)
995 {
996 global_dc->internal_error = internal_error_reentered;
997 warn_if_plugins ();
998 emergency_dump_function ();
999 }
1000
1001 /* Initialization of the front end environment, before command line
1002 options are parsed. Signal handlers, internationalization etc.
1003 ARGV0 is main's argv[0]. */
1004 static void
1005 general_init (const char *argv0, bool init_signals)
1006 {
1007 const char *p;
1008
1009 p = argv0 + strlen (argv0);
1010 while (p != argv0 && !IS_DIR_SEPARATOR (p[-1]))
1011 --p;
1012 progname = p;
1013
1014 xmalloc_set_program_name (progname);
1015
1016 hex_init ();
1017
1018 /* Unlock the stdio streams. */
1019 unlock_std_streams ();
1020
1021 gcc_init_libintl ();
1022
1023 identifier_to_locale_alloc = alloc_for_identifier_to_locale;
1024 identifier_to_locale_free = ggc_free;
1025
1026 /* Initialize the diagnostics reporting machinery, so option parsing
1027 can give warnings and errors. */
1028 diagnostic_initialize (global_dc, N_OPTS);
1029 global_dc->lang_mask = lang_hooks.option_lang_mask ();
1030 /* Set a default printer. Language specific initializations will
1031 override it later. */
1032 tree_diagnostics_defaults (global_dc);
1033
1034 global_dc->show_caret
1035 = global_options_init.x_flag_diagnostics_show_caret;
1036 global_dc->show_labels_p
1037 = global_options_init.x_flag_diagnostics_show_labels;
1038 global_dc->show_line_numbers_p
1039 = global_options_init.x_flag_diagnostics_show_line_numbers;
1040 global_dc->show_cwe
1041 = global_options_init.x_flag_diagnostics_show_cwe;
1042 global_dc->path_format
1043 = (enum diagnostic_path_format)global_options_init.x_flag_diagnostics_path_format;
1044 global_dc->show_path_depths
1045 = global_options_init.x_flag_diagnostics_show_path_depths;
1046 global_dc->show_option_requested
1047 = global_options_init.x_flag_diagnostics_show_option;
1048 global_dc->min_margin_width
1049 = global_options_init.x_diagnostics_minimum_margin_width;
1050 global_dc->show_column
1051 = global_options_init.x_flag_show_column;
1052 global_dc->internal_error = internal_error_function;
1053 global_dc->option_enabled = option_enabled;
1054 global_dc->option_state = &global_options;
1055 global_dc->option_name = option_name;
1056 global_dc->get_option_url = get_option_url;
1057
1058 if (init_signals)
1059 {
1060 /* Trap fatal signals, e.g. SIGSEGV, and convert them to ICE messages. */
1061 #ifdef SIGSEGV
1062 signal (SIGSEGV, crash_signal);
1063 #endif
1064 #ifdef SIGILL
1065 signal (SIGILL, crash_signal);
1066 #endif
1067 #ifdef SIGBUS
1068 signal (SIGBUS, crash_signal);
1069 #endif
1070 #ifdef SIGABRT
1071 signal (SIGABRT, crash_signal);
1072 #endif
1073 #if defined SIGIOT && (!defined SIGABRT || SIGABRT != SIGIOT)
1074 signal (SIGIOT, crash_signal);
1075 #endif
1076 #ifdef SIGFPE
1077 signal (SIGFPE, crash_signal);
1078 #endif
1079
1080 /* Other host-specific signal setup. */
1081 (*host_hooks.extra_signals)();
1082 }
1083
1084 /* Initialize the garbage-collector, string pools and tree type hash
1085 table. */
1086 init_ggc ();
1087 init_stringpool ();
1088 input_location = UNKNOWN_LOCATION;
1089 line_table = ggc_alloc<line_maps> ();
1090 linemap_init (line_table, BUILTINS_LOCATION);
1091 line_table->reallocator = realloc_for_line_map;
1092 line_table->round_alloc_size = ggc_round_alloc_size;
1093 line_table->default_range_bits = 5;
1094 init_ttree ();
1095
1096 /* Initialize register usage now so switches may override. */
1097 init_reg_sets ();
1098
1099 /* Create the singleton holder for global state. This creates the
1100 dump manager. */
1101 g = new gcc::context ();
1102
1103 /* Allow languages and middle-end to register their dumps before the
1104 optimization passes. */
1105 g->get_dumps ()->register_dumps ();
1106
1107 /* Create the passes. */
1108 g->set_passes (new gcc::pass_manager (g));
1109
1110 symtab = new (ggc_alloc <symbol_table> ()) symbol_table ();
1111
1112 statistics_early_init ();
1113 debuginfo_early_init ();
1114 }
1115
1116 /* Return true if the current target supports -fsection-anchors. */
1117
1118 static bool
1119 target_supports_section_anchors_p (void)
1120 {
1121 if (targetm.min_anchor_offset == 0 && targetm.max_anchor_offset == 0)
1122 return false;
1123
1124 if (targetm.asm_out.output_anchor == NULL)
1125 return false;
1126
1127 return true;
1128 }
1129
1130 /* Parse "N[:M][:...]" into struct align_flags A.
1131 VALUES contains parsed values (in reverse order), all processed
1132 values are popped. */
1133
1134 static void
1135 read_log_maxskip (auto_vec<unsigned> &values, align_flags_tuple *a)
1136 {
1137 unsigned n = values.pop ();
1138 if (n != 0)
1139 a->log = floor_log2 (n * 2 - 1);
1140
1141 if (values.is_empty ())
1142 a->maxskip = n ? n - 1 : 0;
1143 else
1144 {
1145 unsigned m = values.pop ();
1146 /* -falign-foo=N:M means M-1 max bytes of padding, not M. */
1147 if (m > 0)
1148 m--;
1149 a->maxskip = m;
1150 }
1151
1152 /* Normalize the tuple. */
1153 a->normalize ();
1154 }
1155
1156 /* Parse "N[:M[:N2[:M2]]]" string FLAG into a pair of struct align_flags. */
1157
1158 static void
1159 parse_N_M (const char *flag, align_flags &a)
1160 {
1161 if (flag)
1162 {
1163 static hash_map <nofree_string_hash, align_flags> cache;
1164 align_flags *entry = cache.get (flag);
1165 if (entry)
1166 {
1167 a = *entry;
1168 return;
1169 }
1170
1171 auto_vec<unsigned> result_values;
1172 bool r = parse_and_check_align_values (flag, NULL, result_values, false,
1173 UNKNOWN_LOCATION);
1174 if (!r)
1175 return;
1176
1177 /* Reverse values for easier manipulation. */
1178 result_values.reverse ();
1179
1180 read_log_maxskip (result_values, &a.levels[0]);
1181 if (!result_values.is_empty ())
1182 read_log_maxskip (result_values, &a.levels[1]);
1183 #ifdef SUBALIGN_LOG
1184 else
1185 {
1186 /* N2[:M2] is not specified. This arch has a default for N2.
1187 Before -falign-foo=N:M:N2:M2 was introduced, x86 had a tweak.
1188 -falign-functions=N with N > 8 was adding secondary alignment.
1189 -falign-functions=10 was emitting this before every function:
1190 .p2align 4,,9
1191 .p2align 3
1192 Now this behavior (and more) can be explicitly requested:
1193 -falign-functions=16:10:8
1194 Retain old behavior if N2 is missing: */
1195
1196 int align = 1 << a.levels[0].log;
1197 int subalign = 1 << SUBALIGN_LOG;
1198
1199 if (a.levels[0].log > SUBALIGN_LOG
1200 && a.levels[0].maxskip >= subalign - 1)
1201 {
1202 /* Set N2 unless subalign can never have any effect. */
1203 if (align > a.levels[0].maxskip + 1)
1204 {
1205 a.levels[1].log = SUBALIGN_LOG;
1206 a.levels[1].normalize ();
1207 }
1208 }
1209 }
1210 #endif
1211
1212 /* Cache seen value. */
1213 cache.put (flag, a);
1214 }
1215 }
1216
1217 /* Process -falign-foo=N[:M[:N2[:M2]]] options. */
1218
1219 void
1220 parse_alignment_opts (void)
1221 {
1222 parse_N_M (str_align_loops, align_loops);
1223 parse_N_M (str_align_jumps, align_jumps);
1224 parse_N_M (str_align_labels, align_labels);
1225 parse_N_M (str_align_functions, align_functions);
1226 }
1227
1228 /* Process the options that have been parsed. */
1229 static void
1230 process_options (void)
1231 {
1232 /* Just in case lang_hooks.post_options ends up calling a debug_hook.
1233 This can happen with incorrect pre-processed input. */
1234 debug_hooks = &do_nothing_debug_hooks;
1235
1236 maximum_field_alignment = initial_max_fld_align * BITS_PER_UNIT;
1237
1238 /* Allow the front end to perform consistency checks and do further
1239 initialization based on the command line options. This hook also
1240 sets the original filename if appropriate (e.g. foo.i -> foo.c)
1241 so we can correctly initialize debug output. */
1242 no_backend = lang_hooks.post_options (&main_input_filename);
1243
1244 /* Some machines may reject certain combinations of options. */
1245 location_t saved_location = input_location;
1246 input_location = UNKNOWN_LOCATION;
1247 targetm.target_option.override ();
1248 input_location = saved_location;
1249
1250 if (flag_diagnostics_generate_patch)
1251 global_dc->edit_context_ptr = new edit_context ();
1252
1253 /* Avoid any informative notes in the second run of -fcompare-debug. */
1254 if (flag_compare_debug)
1255 diagnostic_inhibit_notes (global_dc);
1256
1257 if (flag_section_anchors && !target_supports_section_anchors_p ())
1258 {
1259 warning_at (UNKNOWN_LOCATION, OPT_fsection_anchors,
1260 "this target does not support %qs",
1261 "-fsection-anchors");
1262 flag_section_anchors = 0;
1263 }
1264
1265 if (flag_short_enums == 2)
1266 flag_short_enums = targetm.default_short_enums ();
1267
1268 /* Set aux_base_name if not already set. */
1269 if (aux_base_name)
1270 ;
1271 else if (dump_base_name)
1272 {
1273 const char *name = dump_base_name;
1274 int nlen, len;
1275
1276 if (dump_base_ext && (len = strlen (dump_base_ext))
1277 && (nlen = strlen (name)) && nlen > len
1278 && strcmp (name + nlen - len, dump_base_ext) == 0)
1279 {
1280 char *p = xstrndup (name, nlen - len);
1281 name = p;
1282 }
1283
1284 aux_base_name = name;
1285 }
1286 else
1287 aux_base_name = "gccaux";
1288
1289 #ifndef HAVE_isl
1290 if (flag_graphite
1291 || flag_loop_nest_optimize
1292 || flag_graphite_identity
1293 || flag_loop_parallelize_all)
1294 sorry ("Graphite loop optimizations cannot be used (isl is not available) "
1295 "(%<-fgraphite%>, %<-fgraphite-identity%>, "
1296 "%<-floop-nest-optimize%>, %<-floop-parallelize-all%>)");
1297 #endif
1298
1299 if (flag_cf_protection != CF_NONE
1300 && !(flag_cf_protection & CF_SET))
1301 {
1302 if (flag_cf_protection == CF_FULL)
1303 {
1304 error_at (UNKNOWN_LOCATION,
1305 "%<-fcf-protection=full%> is not supported for this "
1306 "target");
1307 flag_cf_protection = CF_NONE;
1308 }
1309 if (flag_cf_protection == CF_BRANCH)
1310 {
1311 error_at (UNKNOWN_LOCATION,
1312 "%<-fcf-protection=branch%> is not supported for this "
1313 "target");
1314 flag_cf_protection = CF_NONE;
1315 }
1316 if (flag_cf_protection == CF_RETURN)
1317 {
1318 error_at (UNKNOWN_LOCATION,
1319 "%<-fcf-protection=return%> is not supported for this "
1320 "target");
1321 flag_cf_protection = CF_NONE;
1322 }
1323 }
1324
1325 /* One region RA really helps to decrease the code size. */
1326 if (flag_ira_region == IRA_REGION_AUTODETECT)
1327 flag_ira_region
1328 = optimize_size || !optimize ? IRA_REGION_ONE : IRA_REGION_MIXED;
1329
1330 if (!abi_version_at_least (2))
1331 {
1332 /* -fabi-version=1 support was removed after GCC 4.9. */
1333 error_at (UNKNOWN_LOCATION,
1334 "%<-fabi-version=1%> is no longer supported");
1335 flag_abi_version = 2;
1336 }
1337
1338 /* web and rename-registers help when run after loop unrolling. */
1339 if (flag_web == AUTODETECT_VALUE)
1340 flag_web = flag_unroll_loops;
1341
1342 if (flag_rename_registers == AUTODETECT_VALUE)
1343 flag_rename_registers = flag_unroll_loops;
1344
1345 if (flag_non_call_exceptions)
1346 flag_asynchronous_unwind_tables = 1;
1347 if (flag_asynchronous_unwind_tables)
1348 flag_unwind_tables = 1;
1349
1350 if (flag_value_profile_transformations)
1351 flag_profile_values = 1;
1352
1353 /* Warn about options that are not supported on this machine. */
1354 #ifndef INSN_SCHEDULING
1355 if (flag_schedule_insns || flag_schedule_insns_after_reload)
1356 warning_at (UNKNOWN_LOCATION, 0,
1357 "instruction scheduling not supported on this target machine");
1358 #endif
1359 if (!DELAY_SLOTS && flag_delayed_branch)
1360 warning_at (UNKNOWN_LOCATION, 0,
1361 "this target machine does not have delayed branches");
1362
1363 user_label_prefix = USER_LABEL_PREFIX;
1364 if (flag_leading_underscore != -1)
1365 {
1366 /* If the default prefix is more complicated than "" or "_",
1367 issue a warning and ignore this option. */
1368 if (user_label_prefix[0] == 0 ||
1369 (user_label_prefix[0] == '_' && user_label_prefix[1] == 0))
1370 {
1371 user_label_prefix = flag_leading_underscore ? "_" : "";
1372 }
1373 else
1374 warning_at (UNKNOWN_LOCATION, 0,
1375 "%<-f%sleading-underscore%> not supported on this "
1376 "target machine", flag_leading_underscore ? "" : "no-");
1377 }
1378
1379 /* If we are in verbose mode, write out the version and maybe all the
1380 option flags in use. */
1381 if (version_flag)
1382 {
1383 print_version (stderr, "", true);
1384 if (!quiet_flag)
1385 {
1386 fputs ("options passed: ", stderr);
1387 fputs (gen_command_line_string (save_decoded_options,
1388 save_decoded_options_count), stderr);
1389 fputc ('\n', stderr);
1390 }
1391 }
1392
1393 if (flag_syntax_only)
1394 {
1395 write_symbols = NO_DEBUG;
1396 profile_flag = 0;
1397 }
1398
1399 if (flag_gtoggle)
1400 {
1401 if (debug_info_level == DINFO_LEVEL_NONE)
1402 {
1403 debug_info_level = DINFO_LEVEL_NORMAL;
1404
1405 if (write_symbols == NO_DEBUG)
1406 write_symbols = PREFERRED_DEBUGGING_TYPE;
1407 }
1408 else
1409 debug_info_level = DINFO_LEVEL_NONE;
1410 }
1411
1412 if (flag_dump_final_insns && !flag_syntax_only && !no_backend)
1413 {
1414 FILE *final_output = fopen (flag_dump_final_insns, "w");
1415 if (!final_output)
1416 {
1417 error_at (UNKNOWN_LOCATION,
1418 "could not open final insn dump file %qs: %m",
1419 flag_dump_final_insns);
1420 flag_dump_final_insns = NULL;
1421 }
1422 else if (fclose (final_output))
1423 {
1424 error_at (UNKNOWN_LOCATION,
1425 "could not close zeroed insn dump file %qs: %m",
1426 flag_dump_final_insns);
1427 flag_dump_final_insns = NULL;
1428 }
1429 }
1430
1431 /* A lot of code assumes write_symbols == NO_DEBUG if the debugging
1432 level is 0. */
1433 if (debug_info_level == DINFO_LEVEL_NONE)
1434 write_symbols = NO_DEBUG;
1435
1436 if (write_symbols == NO_DEBUG)
1437 ;
1438 #if defined(DBX_DEBUGGING_INFO)
1439 else if (write_symbols == DBX_DEBUG)
1440 debug_hooks = &dbx_debug_hooks;
1441 #endif
1442 #if defined(XCOFF_DEBUGGING_INFO)
1443 else if (write_symbols == XCOFF_DEBUG)
1444 debug_hooks = &xcoff_debug_hooks;
1445 #endif
1446 #ifdef DWARF2_DEBUGGING_INFO
1447 else if (write_symbols == DWARF2_DEBUG)
1448 debug_hooks = &dwarf2_debug_hooks;
1449 #endif
1450 #ifdef VMS_DEBUGGING_INFO
1451 else if (write_symbols == VMS_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
1452 debug_hooks = &vmsdbg_debug_hooks;
1453 #endif
1454 #ifdef DWARF2_LINENO_DEBUGGING_INFO
1455 else if (write_symbols == DWARF2_DEBUG)
1456 debug_hooks = &dwarf2_lineno_debug_hooks;
1457 #endif
1458 else
1459 error_at (UNKNOWN_LOCATION,
1460 "target system does not support the %qs debug format",
1461 debug_type_names[write_symbols]);
1462
1463 /* We know which debug output will be used so we can set flag_var_tracking
1464 and flag_var_tracking_uninit if the user has not specified them. */
1465 if (debug_info_level < DINFO_LEVEL_NORMAL
1466 || debug_hooks->var_location == do_nothing_debug_hooks.var_location)
1467 {
1468 if (flag_var_tracking == 1
1469 || flag_var_tracking_uninit == 1)
1470 {
1471 if (debug_info_level < DINFO_LEVEL_NORMAL)
1472 warning_at (UNKNOWN_LOCATION, 0,
1473 "variable tracking requested, but useless unless "
1474 "producing debug info");
1475 else
1476 warning_at (UNKNOWN_LOCATION, 0,
1477 "variable tracking requested, but not supported "
1478 "by this debug format");
1479 }
1480 flag_var_tracking = 0;
1481 flag_var_tracking_uninit = 0;
1482 }
1483
1484 /* The debug hooks are used to implement -fdump-go-spec because it
1485 gives a simple and stable API for all the information we need to
1486 dump. */
1487 if (flag_dump_go_spec != NULL)
1488 debug_hooks = dump_go_spec_init (flag_dump_go_spec, debug_hooks);
1489
1490 /* If the user specifically requested variable tracking with tagging
1491 uninitialized variables, we need to turn on variable tracking.
1492 (We already determined above that variable tracking is feasible.) */
1493 if (flag_var_tracking_uninit == 1)
1494 flag_var_tracking = 1;
1495
1496 if (flag_var_tracking == AUTODETECT_VALUE)
1497 flag_var_tracking = optimize >= 1;
1498
1499 if (flag_var_tracking_uninit == AUTODETECT_VALUE)
1500 flag_var_tracking_uninit = flag_var_tracking;
1501
1502 if (flag_var_tracking_assignments == AUTODETECT_VALUE)
1503 flag_var_tracking_assignments
1504 = (flag_var_tracking
1505 && !(flag_selective_scheduling || flag_selective_scheduling2));
1506
1507 if (flag_var_tracking_assignments_toggle)
1508 flag_var_tracking_assignments = !flag_var_tracking_assignments;
1509
1510 if (flag_var_tracking_assignments && !flag_var_tracking)
1511 flag_var_tracking = flag_var_tracking_assignments = -1;
1512
1513 if (flag_var_tracking_assignments
1514 && (flag_selective_scheduling || flag_selective_scheduling2))
1515 warning_at (UNKNOWN_LOCATION, 0,
1516 "var-tracking-assignments changes selective scheduling");
1517
1518 if (debug_nonbind_markers_p == AUTODETECT_VALUE)
1519 debug_nonbind_markers_p
1520 = (optimize
1521 && debug_info_level >= DINFO_LEVEL_NORMAL
1522 && (write_symbols == DWARF2_DEBUG
1523 || write_symbols == VMS_AND_DWARF2_DEBUG)
1524 && !(flag_selective_scheduling || flag_selective_scheduling2));
1525
1526 if (dwarf2out_as_loc_support == AUTODETECT_VALUE)
1527 dwarf2out_as_loc_support
1528 = dwarf2out_default_as_loc_support ();
1529 if (dwarf2out_as_locview_support == AUTODETECT_VALUE)
1530 dwarf2out_as_locview_support
1531 = dwarf2out_default_as_locview_support ();
1532
1533 if (debug_variable_location_views == AUTODETECT_VALUE)
1534 {
1535 debug_variable_location_views
1536 = (flag_var_tracking
1537 && debug_info_level >= DINFO_LEVEL_NORMAL
1538 && (write_symbols == DWARF2_DEBUG
1539 || write_symbols == VMS_AND_DWARF2_DEBUG)
1540 && !dwarf_strict
1541 && dwarf2out_as_loc_support
1542 && dwarf2out_as_locview_support);
1543 }
1544 else if (debug_variable_location_views == -1 && dwarf_version != 5)
1545 {
1546 warning_at (UNKNOWN_LOCATION, 0,
1547 "without %<-gdwarf-5%>, "
1548 "%<-gvariable-location-views=incompat5%> "
1549 "is equivalent to %<-gvariable-location-views%>");
1550 debug_variable_location_views = 1;
1551 }
1552
1553 if (debug_internal_reset_location_views == 2)
1554 {
1555 debug_internal_reset_location_views
1556 = (debug_variable_location_views
1557 && targetm.reset_location_view);
1558 }
1559 else if (debug_internal_reset_location_views
1560 && !debug_variable_location_views)
1561 {
1562 warning_at (UNKNOWN_LOCATION, 0,
1563 "%<-ginternal-reset-location-views%> is forced disabled "
1564 "without %<-gvariable-location-views%>");
1565 debug_internal_reset_location_views = 0;
1566 }
1567
1568 if (debug_inline_points == AUTODETECT_VALUE)
1569 debug_inline_points = debug_variable_location_views;
1570 else if (debug_inline_points && !debug_nonbind_markers_p)
1571 {
1572 warning_at (UNKNOWN_LOCATION, 0,
1573 "%<-ginline-points%> is forced disabled without "
1574 "%<-gstatement-frontiers%>");
1575 debug_inline_points = 0;
1576 }
1577
1578 if (flag_tree_cselim == AUTODETECT_VALUE)
1579 {
1580 if (HAVE_conditional_move)
1581 flag_tree_cselim = 1;
1582 else
1583 flag_tree_cselim = 0;
1584 }
1585
1586 /* If auxiliary info generation is desired, open the output file.
1587 This goes in the same directory as the source file--unlike
1588 all the other output files. */
1589 if (flag_gen_aux_info)
1590 {
1591 aux_info_file = fopen (aux_info_file_name, "w");
1592 if (aux_info_file == 0)
1593 fatal_error (UNKNOWN_LOCATION,
1594 "cannot open %s: %m", aux_info_file_name);
1595 }
1596
1597 if (!targetm_common.have_named_sections)
1598 {
1599 if (flag_function_sections)
1600 {
1601 warning_at (UNKNOWN_LOCATION, 0,
1602 "%<-ffunction-sections%> not supported for this target");
1603 flag_function_sections = 0;
1604 }
1605 if (flag_data_sections)
1606 {
1607 warning_at (UNKNOWN_LOCATION, 0,
1608 "%<-fdata-sections%> not supported for this target");
1609 flag_data_sections = 0;
1610 }
1611 }
1612
1613 if (flag_prefetch_loop_arrays > 0 && !targetm.code_for_prefetch)
1614 {
1615 warning_at (UNKNOWN_LOCATION, 0,
1616 "%<-fprefetch-loop-arrays%> not supported for this target");
1617 flag_prefetch_loop_arrays = 0;
1618 }
1619 else if (flag_prefetch_loop_arrays > 0 && !targetm.have_prefetch ())
1620 {
1621 warning_at (UNKNOWN_LOCATION, 0,
1622 "%<-fprefetch-loop-arrays%> not supported for this target "
1623 "(try %<-march%> switches)");
1624 flag_prefetch_loop_arrays = 0;
1625 }
1626
1627 /* This combination of options isn't handled for i386 targets and doesn't
1628 make much sense anyway, so don't allow it. */
1629 if (flag_prefetch_loop_arrays > 0 && optimize_size)
1630 {
1631 warning_at (UNKNOWN_LOCATION, 0,
1632 "%<-fprefetch-loop-arrays%> is not supported with %<-Os%>");
1633 flag_prefetch_loop_arrays = 0;
1634 }
1635
1636 /* The presence of IEEE signaling NaNs, implies all math can trap. */
1637 if (flag_signaling_nans)
1638 flag_trapping_math = 1;
1639
1640 /* We cannot reassociate if we want traps or signed zeros. */
1641 if (flag_associative_math && (flag_trapping_math || flag_signed_zeros))
1642 {
1643 warning_at (UNKNOWN_LOCATION, 0,
1644 "%<-fassociative-math%> disabled; other options take "
1645 "precedence");
1646 flag_associative_math = 0;
1647 }
1648
1649 /* -fstack-clash-protection is not currently supported on targets
1650 where the stack grows up. */
1651 if (flag_stack_clash_protection && !STACK_GROWS_DOWNWARD)
1652 {
1653 warning_at (UNKNOWN_LOCATION, 0,
1654 "%<-fstack-clash-protection%> is not supported on targets "
1655 "where the stack grows from lower to higher addresses");
1656 flag_stack_clash_protection = 0;
1657 }
1658
1659 /* We cannot support -fstack-check= and -fstack-clash-protection at
1660 the same time. */
1661 if (flag_stack_check != NO_STACK_CHECK && flag_stack_clash_protection)
1662 {
1663 warning_at (UNKNOWN_LOCATION, 0,
1664 "%<-fstack-check=%> and %<-fstack-clash_protection%> are "
1665 "mutually exclusive; disabling %<-fstack-check=%>");
1666 flag_stack_check = NO_STACK_CHECK;
1667 }
1668
1669 /* With -fcx-limited-range, we do cheap and quick complex arithmetic. */
1670 if (flag_cx_limited_range)
1671 flag_complex_method = 0;
1672
1673 /* With -fcx-fortran-rules, we do something in-between cheap and C99. */
1674 if (flag_cx_fortran_rules)
1675 flag_complex_method = 1;
1676
1677 /* Targets must be able to place spill slots at lower addresses. If the
1678 target already uses a soft frame pointer, the transition is trivial. */
1679 if (!FRAME_GROWS_DOWNWARD && flag_stack_protect)
1680 {
1681 warning_at (UNKNOWN_LOCATION, 0,
1682 "%<-fstack-protector%> not supported for this target");
1683 flag_stack_protect = 0;
1684 }
1685 if (!flag_stack_protect)
1686 warn_stack_protect = 0;
1687
1688 /* Address Sanitizer needs porting to each target architecture. */
1689
1690 if ((flag_sanitize & SANITIZE_ADDRESS)
1691 && !FRAME_GROWS_DOWNWARD)
1692 {
1693 warning_at (UNKNOWN_LOCATION, 0,
1694 "%<-fsanitize=address%> and %<-fsanitize=kernel-address%> "
1695 "are not supported for this target");
1696 flag_sanitize &= ~SANITIZE_ADDRESS;
1697 }
1698
1699 if ((flag_sanitize & SANITIZE_USER_ADDRESS)
1700 && ((targetm.asan_shadow_offset == NULL)
1701 || (targetm.asan_shadow_offset () == 0)))
1702 {
1703 warning_at (UNKNOWN_LOCATION, 0,
1704 "%<-fsanitize=address%> not supported for this target");
1705 flag_sanitize &= ~SANITIZE_ADDRESS;
1706 }
1707
1708 if ((flag_sanitize & SANITIZE_KERNEL_ADDRESS)
1709 && (targetm.asan_shadow_offset == NULL
1710 && !asan_shadow_offset_set_p ()))
1711 {
1712 warning_at (UNKNOWN_LOCATION, 0,
1713 "%<-fsanitize=kernel-address%> with stack protection "
1714 "is not supported without %<-fasan-shadow-offset=%> "
1715 "for this target");
1716 flag_sanitize &= ~SANITIZE_ADDRESS;
1717 }
1718
1719 /* HWAsan requires top byte ignore feature in the backend. */
1720 if (flag_sanitize & SANITIZE_HWADDRESS
1721 && ! targetm.memtag.can_tag_addresses ())
1722 {
1723 warning_at (UNKNOWN_LOCATION, 0, "%qs is not supported for this target",
1724 "-fsanitize=hwaddress");
1725 flag_sanitize &= ~SANITIZE_HWADDRESS;
1726 }
1727
1728 /* Do not use IPA optimizations for register allocation if profiler is active
1729 or patchable function entries are inserted for run-time instrumentation
1730 or port does not emit prologue and epilogue as RTL. */
1731 if (profile_flag || function_entry_patch_area_size
1732 || !targetm.have_prologue () || !targetm.have_epilogue ())
1733 flag_ipa_ra = 0;
1734
1735 /* Enable -Werror=coverage-mismatch when -Werror and -Wno-error
1736 have not been set. */
1737 if (!global_options_set.x_warnings_are_errors
1738 && warn_coverage_mismatch
1739 && (global_dc->classify_diagnostic[OPT_Wcoverage_mismatch] ==
1740 DK_UNSPECIFIED))
1741 diagnostic_classify_diagnostic (global_dc, OPT_Wcoverage_mismatch,
1742 DK_ERROR, UNKNOWN_LOCATION);
1743
1744 /* Save the current optimization options. */
1745 optimization_default_node
1746 = build_optimization_node (&global_options, &global_options_set);
1747 optimization_current_node = optimization_default_node;
1748
1749 if (flag_checking >= 2)
1750 hash_table_sanitize_eq_limit
1751 = param_hash_table_verification_limit;
1752
1753 if (flag_large_source_files)
1754 line_table->default_range_bits = 0;
1755
1756 /* Please don't change global_options after this point, those changes won't
1757 be reflected in optimization_{default,current}_node. */
1758 }
1759
1760 /* This function can be called multiple times to reinitialize the compiler
1761 back end when register classes or instruction sets have changed,
1762 before each function. */
1763 static void
1764 backend_init_target (void)
1765 {
1766 /* This depends on stack_pointer_rtx. */
1767 init_fake_stack_mems ();
1768
1769 /* Sets static_base_value[HARD_FRAME_POINTER_REGNUM], which is
1770 mode-dependent. */
1771 init_alias_target ();
1772
1773 /* Depends on HARD_FRAME_POINTER_REGNUM. */
1774 if (!ira_use_lra_p)
1775 init_reload ();
1776
1777 /* Depends on the enabled attribute. */
1778 recog_init ();
1779
1780 /* The following initialization functions need to generate rtl, so
1781 provide a dummy function context for them. */
1782 init_dummy_function_start ();
1783
1784 /* rtx_cost is mode-dependent, so cached values need to be recomputed
1785 on a mode change. */
1786 init_expmed ();
1787 init_lower_subreg ();
1788 init_set_costs ();
1789
1790 init_expr_target ();
1791 ira_init ();
1792
1793 /* We may need to recompute regno_save_code[] and regno_restore_code[]
1794 after a mode change as well. */
1795 caller_save_initialized_p = false;
1796
1797 expand_dummy_function_end ();
1798 }
1799
1800 /* Initialize the compiler back end. This function is called only once,
1801 when starting the compiler. */
1802 static void
1803 backend_init (void)
1804 {
1805 init_emit_once ();
1806
1807 init_rtlanal ();
1808 init_inline_once ();
1809 init_varasm_once ();
1810 save_register_info ();
1811
1812 /* Middle end needs this initialization for default mem attributes
1813 used by early calls to make_decl_rtl. */
1814 init_emit_regs ();
1815
1816 /* Middle end needs this initialization for mode tables used to assign
1817 modes to vector variables. */
1818 init_regs ();
1819 }
1820
1821 /* Initialize things that are both lang-dependent and target-dependent.
1822 This function can be called more than once if target parameters change. */
1823 static void
1824 lang_dependent_init_target (void)
1825 {
1826 /* This creates various _DECL nodes, so needs to be called after the
1827 front end is initialized. It also depends on the HAVE_xxx macros
1828 generated from the target machine description. */
1829 init_optabs ();
1830
1831 gcc_assert (!this_target_rtl->target_specific_initialized);
1832 }
1833
1834 /* Perform initializations that are lang-dependent or target-dependent.
1835 but matters only for late optimizations and RTL generation. */
1836
1837 static int rtl_initialized;
1838
1839 void
1840 initialize_rtl (void)
1841 {
1842 auto_timevar tv (g_timer, TV_INITIALIZE_RTL);
1843
1844 /* Initialization done just once per compilation, but delayed
1845 till code generation. */
1846 if (!rtl_initialized)
1847 ira_init_once ();
1848 rtl_initialized = true;
1849
1850 /* Target specific RTL backend initialization. */
1851 if (!this_target_rtl->target_specific_initialized)
1852 {
1853 backend_init_target ();
1854 this_target_rtl->target_specific_initialized = true;
1855 }
1856 }
1857
1858 /* Language-dependent initialization. Returns nonzero on success. */
1859 static int
1860 lang_dependent_init (const char *name)
1861 {
1862 location_t save_loc = input_location;
1863 if (!dump_base_name)
1864 {
1865 dump_base_name = name && name[0] ? name : "gccdump";
1866
1867 /* We do not want to derive a non-empty dumpbase-ext from an
1868 explicit -dumpbase argument, only from a defaulted
1869 dumpbase. */
1870 if (!dump_base_ext)
1871 {
1872 const char *base = lbasename (dump_base_name);
1873 const char *ext = strrchr (base, '.');
1874 if (ext)
1875 dump_base_ext = ext;
1876 }
1877 }
1878
1879 /* Other front-end initialization. */
1880 input_location = BUILTINS_LOCATION;
1881 if (lang_hooks.init () == 0)
1882 return 0;
1883 input_location = save_loc;
1884
1885 if (!flag_wpa)
1886 {
1887 init_asm_output (name);
1888
1889 if (!flag_generate_lto && !flag_compare_debug)
1890 {
1891 /* If stack usage information is desired, open the output file. */
1892 if (flag_stack_usage)
1893 stack_usage_file = open_auxiliary_file ("su");
1894
1895 /* If call graph information is desired, open the output file. */
1896 if (flag_callgraph_info)
1897 {
1898 callgraph_info_file = open_auxiliary_file ("ci");
1899 /* Write the file header. */
1900 fprintf (callgraph_info_file,
1901 "graph: { title: \"%s\"\n", main_input_filename);
1902 bitmap_obstack_initialize (NULL);
1903 callgraph_info_external_printed = BITMAP_ALLOC (NULL);
1904 }
1905 }
1906 else
1907 flag_stack_usage = flag_callgraph_info = false;
1908 }
1909
1910 /* This creates various _DECL nodes, so needs to be called after the
1911 front end is initialized. */
1912 init_eh ();
1913
1914 /* Do the target-specific parts of the initialization. */
1915 lang_dependent_init_target ();
1916
1917 if (!flag_wpa)
1918 {
1919 /* If dbx symbol table desired, initialize writing it and output the
1920 predefined types. */
1921 timevar_push (TV_SYMOUT);
1922
1923 /* Now we have the correct original filename, we can initialize
1924 debug output. */
1925 (*debug_hooks->init) (name);
1926
1927 timevar_pop (TV_SYMOUT);
1928 }
1929
1930 return 1;
1931 }
1932
1933
1934 /* Reinitialize everything when target parameters, such as register usage,
1935 have changed. */
1936 void
1937 target_reinit (void)
1938 {
1939 struct rtl_data saved_x_rtl;
1940 rtx *saved_regno_reg_rtx;
1941 tree saved_optimization_current_node;
1942 struct target_optabs *saved_this_fn_optabs;
1943
1944 /* Temporarily switch to the default optimization node, so that
1945 *this_target_optabs is set to the default, not reflecting
1946 whatever a previous function used for the optimize
1947 attribute. */
1948 saved_optimization_current_node = optimization_current_node;
1949 saved_this_fn_optabs = this_fn_optabs;
1950 if (saved_optimization_current_node != optimization_default_node)
1951 {
1952 optimization_current_node = optimization_default_node;
1953 cl_optimization_restore
1954 (&global_options, &global_options_set,
1955 TREE_OPTIMIZATION (optimization_default_node));
1956 }
1957 this_fn_optabs = this_target_optabs;
1958
1959 /* Save *crtl and regno_reg_rtx around the reinitialization
1960 to allow target_reinit being called even after prepare_function_start. */
1961 saved_regno_reg_rtx = regno_reg_rtx;
1962 if (saved_regno_reg_rtx)
1963 {
1964 saved_x_rtl = *crtl;
1965 memset (crtl, '\0', sizeof (*crtl));
1966 regno_reg_rtx = NULL;
1967 }
1968
1969 this_target_rtl->target_specific_initialized = false;
1970
1971 /* This initializes hard_frame_pointer, and calls init_reg_modes_target()
1972 to initialize reg_raw_mode[]. */
1973 init_emit_regs ();
1974
1975 /* This invokes target hooks to set fixed_reg[] etc, which is
1976 mode-dependent. */
1977 init_regs ();
1978
1979 /* Reinitialize lang-dependent parts. */
1980 lang_dependent_init_target ();
1981
1982 /* Restore the original optimization node. */
1983 if (saved_optimization_current_node != optimization_default_node)
1984 {
1985 optimization_current_node = saved_optimization_current_node;
1986 cl_optimization_restore (&global_options, &global_options_set,
1987 TREE_OPTIMIZATION (optimization_current_node));
1988 }
1989 this_fn_optabs = saved_this_fn_optabs;
1990
1991 /* Restore regno_reg_rtx at the end, as free_after_compilation from
1992 expand_dummy_function_end clears it. */
1993 if (saved_regno_reg_rtx)
1994 {
1995 *crtl = saved_x_rtl;
1996 regno_reg_rtx = saved_regno_reg_rtx;
1997 saved_regno_reg_rtx = NULL;
1998 }
1999 }
2000
2001 void
2002 dump_memory_report (const char *header)
2003 {
2004 /* Print significant header. */
2005 fputc ('\n', stderr);
2006 for (unsigned i = 0; i < 80; i++)
2007 fputc ('#', stderr);
2008 fprintf (stderr, "\n# %-77s#\n", header);
2009 for (unsigned i = 0; i < 80; i++)
2010 fputc ('#', stderr);
2011 fputs ("\n\n", stderr);
2012
2013 dump_line_table_statistics ();
2014 ggc_print_statistics ();
2015 stringpool_statistics ();
2016 dump_tree_statistics ();
2017 dump_gimple_statistics ();
2018 dump_rtx_statistics ();
2019 dump_alloc_pool_statistics ();
2020 dump_bitmap_statistics ();
2021 dump_hash_table_loc_statistics ();
2022 dump_vec_loc_statistics ();
2023 dump_ggc_loc_statistics ();
2024 dump_alias_stats (stderr);
2025 dump_pta_stats (stderr);
2026 }
2027
2028 /* Clean up: close opened files, etc. */
2029
2030 static void
2031 finalize (bool no_backend)
2032 {
2033 /* Close the dump files. */
2034 if (flag_gen_aux_info)
2035 {
2036 fclose (aux_info_file);
2037 aux_info_file = NULL;
2038 if (seen_error ())
2039 unlink (aux_info_file_name);
2040 }
2041
2042 /* Close non-debugging input and output files. Take special care to note
2043 whether fclose returns an error, since the pages might still be on the
2044 buffer chain while the file is open. */
2045
2046 if (asm_out_file)
2047 {
2048 if (ferror (asm_out_file) != 0)
2049 fatal_error (input_location, "error writing to %s: %m", asm_file_name);
2050 if (fclose (asm_out_file) != 0)
2051 fatal_error (input_location, "error closing %s: %m", asm_file_name);
2052 asm_out_file = NULL;
2053 }
2054
2055 if (stack_usage_file)
2056 {
2057 fclose (stack_usage_file);
2058 stack_usage_file = NULL;
2059 }
2060
2061 if (callgraph_info_file)
2062 {
2063 fputs ("}\n", callgraph_info_file);
2064 fclose (callgraph_info_file);
2065 callgraph_info_file = NULL;
2066 BITMAP_FREE (callgraph_info_external_printed);
2067 bitmap_obstack_release (NULL);
2068 }
2069
2070 if (seen_error ())
2071 coverage_remove_note_file ();
2072
2073 if (!no_backend)
2074 {
2075 statistics_fini ();
2076 debuginfo_fini ();
2077
2078 g->get_passes ()->finish_optimization_passes ();
2079
2080 lra_finish_once ();
2081 }
2082
2083 if (mem_report)
2084 dump_memory_report ("Final");
2085
2086 if (profile_report)
2087 dump_profile_report ();
2088
2089 if (flag_dbg_cnt_list)
2090 dbg_cnt_list_all_counters ();
2091
2092 /* Language-specific end of compilation actions. */
2093 lang_hooks.finish ();
2094 }
2095
2096 static bool
2097 standard_type_bitsize (int bitsize)
2098 {
2099 /* As a special exception, we always want __int128 enabled if possible. */
2100 if (bitsize == 128)
2101 return false;
2102 if (bitsize == CHAR_TYPE_SIZE
2103 || bitsize == SHORT_TYPE_SIZE
2104 || bitsize == INT_TYPE_SIZE
2105 || bitsize == LONG_TYPE_SIZE
2106 || bitsize == LONG_LONG_TYPE_SIZE)
2107 return true;
2108 return false;
2109 }
2110
2111 /* Initialize the compiler, and compile the input file. */
2112 static void
2113 do_compile ()
2114 {
2115 process_options ();
2116
2117 /* Don't do any more if an error has already occurred. */
2118 if (!seen_error ())
2119 {
2120 int i;
2121
2122 timevar_start (TV_PHASE_SETUP);
2123
2124 if (flag_save_optimization_record)
2125 {
2126 dump_context::get ().set_json_writer (new optrecord_json_writer ());
2127 }
2128
2129 /* This must be run always, because it is needed to compute the FP
2130 predefined macros, such as __LDBL_MAX__, for targets using non
2131 default FP formats. */
2132 init_adjust_machine_modes ();
2133 init_derived_machine_modes ();
2134
2135 /* This must happen after the backend has a chance to process
2136 command line options, but before the parsers are
2137 initialized. */
2138 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2139 if (targetm.scalar_mode_supported_p (int_n_data[i].m)
2140 && ! standard_type_bitsize (int_n_data[i].bitsize))
2141 int_n_enabled_p[i] = true;
2142 else
2143 int_n_enabled_p[i] = false;
2144
2145 /* Initialize mpfrs exponent range. This is important to get
2146 underflow/overflow in a reasonable timeframe. */
2147 machine_mode mode;
2148 int min_exp = -1;
2149 int max_exp = 1;
2150 FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT)
2151 if (SCALAR_FLOAT_MODE_P (mode))
2152 {
2153 const real_format *fmt = REAL_MODE_FORMAT (mode);
2154 if (fmt)
2155 {
2156 /* fmt->emin - fmt->p + 1 should be enough but the
2157 back-and-forth dance in real_to_decimal_for_mode we
2158 do for checking fails due to rounding effects then. */
2159 if ((fmt->emin - fmt->p) < min_exp)
2160 min_exp = fmt->emin - fmt->p;
2161 if (fmt->emax > max_exp)
2162 max_exp = fmt->emax;
2163 }
2164 }
2165 /* E.g. mpc_norm assumes it can square a number without bothering with
2166 with range scaling, so until that is fixed, double the minimum
2167 and maximum exponents, plus add some buffer for arithmetics
2168 on the squared numbers. */
2169 if (mpfr_set_emin (2 * (min_exp - 1))
2170 || mpfr_set_emax (2 * (max_exp + 1)))
2171 sorry ("mpfr not configured to handle all floating modes");
2172
2173 /* Set up the back-end if requested. */
2174 if (!no_backend)
2175 backend_init ();
2176
2177 /* Language-dependent initialization. Returns true on success. */
2178 if (lang_dependent_init (main_input_filename))
2179 {
2180 /* Initialize yet another pass. */
2181
2182 ggc_protect_identifiers = true;
2183
2184 symtab->initialize ();
2185 init_final (main_input_filename);
2186 coverage_init (aux_base_name);
2187 statistics_init ();
2188 debuginfo_init ();
2189 invoke_plugin_callbacks (PLUGIN_START_UNIT, NULL);
2190
2191 timevar_stop (TV_PHASE_SETUP);
2192
2193 compile_file ();
2194 }
2195 else
2196 {
2197 timevar_stop (TV_PHASE_SETUP);
2198 }
2199
2200 timevar_start (TV_PHASE_FINALIZE);
2201
2202 finalize (no_backend);
2203
2204 timevar_stop (TV_PHASE_FINALIZE);
2205 }
2206 }
2207
2208 toplev::toplev (timer *external_timer,
2209 bool init_signals)
2210 : m_use_TV_TOTAL (external_timer == NULL),
2211 m_init_signals (init_signals)
2212 {
2213 if (external_timer)
2214 g_timer = external_timer;
2215 }
2216
2217 toplev::~toplev ()
2218 {
2219 if (g_timer && m_use_TV_TOTAL)
2220 {
2221 g_timer->stop (TV_TOTAL);
2222 g_timer->print (stderr);
2223 delete g_timer;
2224 g_timer = NULL;
2225 }
2226 }
2227
2228 /* Potentially call timevar_init (which will create g_timevars if it
2229 doesn't already exist). */
2230
2231 void
2232 toplev::start_timevars ()
2233 {
2234 if (time_report || !quiet_flag || flag_detailed_statistics)
2235 timevar_init ();
2236
2237 timevar_start (TV_TOTAL);
2238 }
2239
2240 /* Handle -fself-test. */
2241
2242 void
2243 toplev::run_self_tests ()
2244 {
2245 if (no_backend)
2246 {
2247 error_at (UNKNOWN_LOCATION, "self-tests incompatible with %<-E%>");
2248 return;
2249 }
2250 #if CHECKING_P
2251 /* Reset some state. */
2252 input_location = UNKNOWN_LOCATION;
2253 bitmap_obstack_initialize (NULL);
2254
2255 /* Run the tests; any failures will lead to an abort of the process.
2256 Use "make selftests-gdb" to run under the debugger. */
2257 ::selftest::run_tests ();
2258
2259 /* Cleanup. */
2260 bitmap_obstack_release (NULL);
2261 #else
2262 inform (UNKNOWN_LOCATION, "self-tests are not enabled in this build");
2263 #endif /* #if CHECKING_P */
2264 }
2265
2266 /* Entry point of cc1, cc1plus, jc1, f771, etc.
2267 Exit code is FATAL_EXIT_CODE if can't open files or if there were
2268 any errors, or SUCCESS_EXIT_CODE if compilation succeeded.
2269
2270 It is not safe to call this function more than once. */
2271
2272 int
2273 toplev::main (int argc, char **argv)
2274 {
2275 /* Parsing and gimplification sometimes need quite large stack.
2276 Increase stack size limits if possible. */
2277 stack_limit_increase (64 * 1024 * 1024);
2278
2279 expandargv (&argc, &argv);
2280
2281 /* Initialization of GCC's environment, and diagnostics. */
2282 general_init (argv[0], m_init_signals);
2283
2284 /* One-off initialization of options that does not need to be
2285 repeated when options are added for particular functions. */
2286 init_options_once ();
2287 init_opts_obstack ();
2288
2289 /* Initialize global options structures; this must be repeated for
2290 each structure used for parsing options. */
2291 init_options_struct (&global_options, &global_options_set);
2292 lang_hooks.init_options_struct (&global_options);
2293
2294 /* Init GGC heuristics must be caller after we initialize
2295 options. */
2296 init_ggc_heuristics ();
2297
2298 /* Convert the options to an array. */
2299 decode_cmdline_options_to_array_default_mask (argc,
2300 CONST_CAST2 (const char **,
2301 char **, argv),
2302 &save_decoded_options,
2303 &save_decoded_options_count);
2304
2305 /* Perform language-specific options initialization. */
2306 lang_hooks.init_options (save_decoded_options_count, save_decoded_options);
2307
2308 /* Parse the options and do minimal processing; basically just
2309 enough to default flags appropriately. */
2310 decode_options (&global_options, &global_options_set,
2311 save_decoded_options, save_decoded_options_count,
2312 UNKNOWN_LOCATION, global_dc,
2313 targetm.target_option.override);
2314
2315 handle_common_deferred_options ();
2316
2317 init_local_tick ();
2318
2319 initialize_plugins ();
2320
2321 if (version_flag)
2322 print_version (stderr, "", true);
2323
2324 if (help_flag)
2325 print_plugins_help (stderr, "");
2326
2327 /* Exit early if we can (e.g. -help). */
2328 if (!exit_after_options)
2329 {
2330 if (m_use_TV_TOTAL)
2331 start_timevars ();
2332 do_compile ();
2333 }
2334
2335 if (warningcount || errorcount || werrorcount)
2336 print_ignored_options ();
2337
2338 if (flag_self_test)
2339 run_self_tests ();
2340
2341 /* Invoke registered plugin callbacks if any. Some plugins could
2342 emit some diagnostics here. */
2343 invoke_plugin_callbacks (PLUGIN_FINISH, NULL);
2344
2345 if (flag_diagnostics_generate_patch)
2346 {
2347 gcc_assert (global_dc->edit_context_ptr);
2348
2349 pretty_printer pp;
2350 pp_show_color (&pp) = pp_show_color (global_dc->printer);
2351 global_dc->edit_context_ptr->print_diff (&pp, true);
2352 pp_flush (&pp);
2353 }
2354
2355 diagnostic_finish (global_dc);
2356
2357 finalize_plugins ();
2358
2359 after_memory_report = true;
2360
2361 if (seen_error () || werrorcount)
2362 return (FATAL_EXIT_CODE);
2363
2364 return (SUCCESS_EXIT_CODE);
2365 }
2366
2367 /* For those that want to, this function aims to clean up enough state that
2368 you can call toplev::main again. */
2369 void
2370 toplev::finalize (void)
2371 {
2372 rtl_initialized = false;
2373 this_target_rtl->target_specific_initialized = false;
2374
2375 /* Needs to be called before cgraph_c_finalize since it uses symtab. */
2376 ipa_reference_c_finalize ();
2377 ipa_fnsummary_c_finalize ();
2378 ipa_modref_c_finalize ();
2379
2380 cgraph_c_finalize ();
2381 cgraphunit_c_finalize ();
2382 symtab_thunks_cc_finalize ();
2383 dwarf2out_c_finalize ();
2384 gcse_c_finalize ();
2385 ipa_cp_c_finalize ();
2386 ira_costs_c_finalize ();
2387
2388 /* save_decoded_options uses opts_obstack, so these must
2389 be cleaned up together. */
2390 obstack_free (&opts_obstack, NULL);
2391 XDELETEVEC (save_decoded_options);
2392 save_decoded_options = NULL;
2393 save_decoded_options_count = 0;
2394
2395 /* Clean up the context (and pass_manager etc). */
2396 delete g;
2397 g = NULL;
2398
2399 }