toplev.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* Top level of GNU C compiler
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This is the top level of cc1.
  18. It parses command args, opens files, invokes the various passes
  19. in the proper order, and counts the time used by each.
  20. Error messages and low-level interface to malloc also handled here. */
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <strings.h>
  25. #include <sys/time.h>
  26. #include <sys/resource.h>
  27. #ifndef _TYPES_
  28. #include <sys/types.h>
  29. #endif
  30. #include <sys/stat.h>
  31. #include "tree.h"
  32. #include "c-tree.h"
  33. #include "rtl.h"
  34. extern void dump_tree ();
  35. extern int yydebug;
  36. extern FILE *finput;
  37. extern void init_lex ();
  38. extern void init_decl_processing ();
  39. extern void init_tree ();
  40. extern void init_rtl ();
  41. extern rtx expand_function ();
  42. extern void init_optabs ();
  43. extern void dump_flow_info ();
  44. extern void dump_local_alloc ();
  45. /* Bit flags that specify the machine subtype we are compiling for.
  46. Bits are tested using macros TARGET_... defined in the tm-...h file
  47. and set by `-m...' switches. */
  48. int target_flags;
  49. /* Name of current real source file (what was input to cpp).
  50. # commands in the input that specify file names
  51. change this value. */
  52. extern char *input_filename;
  53. /* Current line number in real source file. */
  54. extern int lineno;
  55. /* FUNCTION_DECL for function now being parsed or compiled. */
  56. extern tree current_function_decl;
  57. /* Name to use as base of names for dump output files. */
  58. char *dump_base_name;
  59. /* Flags saying which kinds of debugging dump have been requested. */
  60. int tree_dump = 0;
  61. int rtl_dump = 0;
  62. int rtl_dump_and_exit = 0;
  63. int jump_opt_dump = 0;
  64. int cse_dump = 0;
  65. int loop_dump = 0;
  66. int flow_dump = 0;
  67. int combine_dump = 0;
  68. int local_reg_dump = 0;
  69. int global_reg_dump = 0;
  70. /* 1 => write gdb debugging output (using symout.c). -g
  71. 2 => write dbx debugging output (using dbxout.c). -G */
  72. int write_symbols = 0;
  73. /* Nonzero means do optimizations. -opt. */
  74. int optimize = 0;
  75. /* Nonzero for -optforcemem: load memory value into a register
  76. before arithmetic on it. This makes better cse but slower compilation. */
  77. int force_mem = 0;
  78. /* Nonzero for -optforcemem: load memory address into a register before
  79. reference to memory. This makes better cse but slower compilation. */
  80. int force_addr = 0;
  81. /* Nonzero means do stupid register allocation. -noreg.
  82. This an OPTIMIZE are controlled by different switches in cc1,
  83. but normally cc controls them both with the -O switch. */
  84. int obey_regdecls = 0;
  85. /* Don't print functions as they are compiled and don't print
  86. times taken by the various passes. -quiet. */
  87. int quiet_flag = 0;
  88. /* Don't print warning messages. -w. */
  89. int inhibit_warnings = 0;
  90. /* Number of error messages and warning messages so far. */
  91. int errorcount = 0;
  92. int warningcount = 0;
  93. /* Nonzero for -pedantic switch: warn about anything
  94. that standard C forbids. */
  95. int pedantic = 0;
  96. /* Name for output file of assembly code, specified with -o. */
  97. char *asm_file_name;
  98. /* Name for output file of GDB symbol segment, specified with -symout. */
  99. char *sym_file_name;
  100. /* Output files for assembler code (real compiler output)
  101. and debugging dumps. */
  102. FILE *asm_out_file;
  103. FILE *tree_dump_file;
  104. FILE *rtl_dump_file;
  105. FILE *jump_opt_dump_file;
  106. FILE *cse_dump_file;
  107. FILE *loop_dump_file;
  108. FILE *flow_dump_file;
  109. FILE *combine_dump_file;
  110. FILE *local_reg_dump_file;
  111. FILE *global_reg_dump_file;
  112. /* Time accumulators, to count the total time spent in various passes. */
  113. int parse_time;
  114. int varconst_time;
  115. int expand_time;
  116. int jump_time;
  117. int cse_time;
  118. int loop_time;
  119. int flow_time;
  120. int combine_time;
  121. int local_alloc_time;
  122. int global_alloc_time;
  123. int final_time;
  124. int symout_time;
  125. int dump_time;
  126. /* Return time used so far, in microseconds. */
  127. gettime ()
  128. {
  129. struct rusage rusage;
  130. if (quiet_flag)
  131. return 0;
  132. getrusage (0, &rusage);
  133. return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  134. + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  135. }
  136. #define TIMEVAR(VAR, BODY) \
  137. { int otime = gettime (); BODY; VAR += gettime () - otime; }
  138. print_time (str, total)
  139. char *str;
  140. int total;
  141. {
  142. printf ("time in %s: %d.%06d\n", str, total / 1000000, total % 1000000);
  143. }
  144. /* Count an error or warning. Return 1 if the message should be printed. */
  145. int
  146. count_error (warningp)
  147. int warningp;
  148. {
  149. if (warningp && inhibit_warnings)
  150. return 0;
  151. if (warningp)
  152. warningcount++;
  153. else
  154. errorcount++;
  155. /* If we are printing function names, make sure error message
  156. starts at beginning of line. */
  157. if (!quiet_flag)
  158. fprintf (stderr, "\n");
  159. return 1;
  160. }
  161. /* Print a fatal error message. NAME is the text.
  162. Also include a system error message based on `errno'. */
  163. int
  164. pfatal_with_name (name)
  165. {
  166. fprintf (stderr, "cc1: ");
  167. perror (name);
  168. exit (35);
  169. }
  170. void
  171. fatal (s)
  172. char *s;
  173. {
  174. yyerror (s, 0);
  175. exit (34);
  176. }
  177. /* Called when the start of a function definition is parsed,
  178. this function prints on stderr the name of the function. */
  179. void
  180. announce_function (decl)
  181. tree decl;
  182. {
  183. if (! quiet_flag)
  184. {
  185. fprintf (stderr, " %s", IDENTIFIER_POINTER (DECL_NAME (decl)));
  186. fflush (stderr);
  187. }
  188. }
  189. static tree last_error_function;
  190. /* Report an error at the current line number.
  191. S and V are a string and an arg for `printf'. */
  192. void
  193. yyerror (s, v)
  194. char *s;
  195. int v; /* @@also used as pointer */
  196. {
  197. yylineerror (lineno, s, v);
  198. }
  199. /* Report an error at line LINE.
  200. S and V are a string and an arg for `printf'. */
  201. yylineerror (line, s, v)
  202. int line;
  203. char *s;
  204. int v;
  205. {
  206. count_error (0);
  207. if (last_error_function != current_function_decl)
  208. {
  209. fprintf (stderr, "In function %s:\n",
  210. IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
  211. last_error_function = current_function_decl;
  212. }
  213. fprintf (stderr, "%s:%d: ", input_filename, line);
  214. fprintf (stderr, s, v);
  215. fprintf (stderr, "\n");
  216. }
  217. /* Report a warning at the current line number.
  218. S and V are a string and an arg for `printf'. */
  219. void
  220. warning (s, v)
  221. char *s;
  222. int v; /* @@also used as pointer */
  223. {
  224. warning_with_line (lineno, s, v);
  225. }
  226. /* Report a warning at line LINE.
  227. S and V are a string and an arg for `printf'. */
  228. warning_with_line (line, s, v)
  229. int line;
  230. char *s;
  231. int v;
  232. {
  233. if (count_error (1) == 0)
  234. return;
  235. if (last_error_function != current_function_decl)
  236. {
  237. fprintf (stderr, "In function %s:\n",
  238. IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
  239. last_error_function = current_function_decl;
  240. }
  241. fprintf (stderr, "%s:%d: ", input_filename, line);
  242. fprintf (stderr, "warning: ");
  243. fprintf (stderr, s, v);
  244. fprintf (stderr, "\n");
  245. }
  246. /* When `malloc.c' is compiled with `rcheck' defined,
  247. it calls this function to report clobberage. */
  248. botch (s)
  249. {
  250. abort ();
  251. }
  252. /* Same as `malloc' but report error if no memory available. */
  253. xmalloc (size)
  254. unsigned size;
  255. {
  256. register int value = (int) malloc (size);
  257. if (value == 0)
  258. fatal ("Virtual memory exhausted.");
  259. return value;
  260. }
  261. /* Same as `realloc' but report error if no memory available. */
  262. int
  263. xrealloc (ptr, size)
  264. char *ptr;
  265. int size;
  266. {
  267. int result = realloc (ptr, size);
  268. if (!result)
  269. abort ();
  270. return result;
  271. }
  272. /* Return the logarithm of X, base 2, considering X unsigned,
  273. if X is a power of 2. Otherwise, returns -1. */
  274. int
  275. exact_log2 (x)
  276. register unsigned int x;
  277. {
  278. register int log = 0;
  279. for (log = 0; log < HOST_BITS_PER_INT; log++)
  280. if (x == (1 << log))
  281. return log;
  282. return -1;
  283. }
  284. /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
  285. If X is 0, return -1. */
  286. int
  287. floor_log2 (x)
  288. register unsigned int x;
  289. {
  290. register int log = 0;
  291. for (log = 0; log < HOST_BITS_PER_INT; log++)
  292. if ((x & ((-1) << log)) == 0)
  293. return log - 1;
  294. return HOST_BITS_PER_INT - 1;
  295. }
  296. /* Compile an entire file of output from cpp, named NAME.
  297. Write a file of assembly output and various debugging dumps. */
  298. static void
  299. compile_file (name)
  300. char *name;
  301. {
  302. tree globals;
  303. int start_time;
  304. int dump_base_name_length = strlen (dump_base_name);
  305. parse_time = 0;
  306. varconst_time = 0;
  307. expand_time = 0;
  308. jump_time = 0;
  309. cse_time = 0;
  310. loop_time = 0;
  311. flow_time = 0;
  312. combine_time = 0;
  313. local_alloc_time = 0;
  314. global_alloc_time = 0;
  315. final_time = 0;
  316. symout_time = 0;
  317. dump_time;
  318. /* Open input file. */
  319. finput = fopen (name, "r");
  320. if (finput == 0)
  321. pfatal_with_name (name);
  322. /* Initialize data in various passes. */
  323. init_tree ();
  324. init_lex ();
  325. init_rtl ();
  326. init_decl_processing ();
  327. init_optabs ();
  328. /* If tree dump desired, open the output file. */
  329. if (tree_dump)
  330. {
  331. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  332. strcpy (dumpname, dump_base_name);
  333. strcat (dumpname, ".tree");
  334. tree_dump_file = fopen (dumpname, "w");
  335. if (tree_dump_file == 0)
  336. pfatal_with_name (dumpname);
  337. }
  338. /* If rtl dump desired, open the output file. */
  339. if (rtl_dump)
  340. {
  341. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  342. strcpy (dumpname, dump_base_name);
  343. strcat (dumpname, ".rtl");
  344. rtl_dump_file = fopen (dumpname, "w");
  345. if (rtl_dump_file == 0)
  346. pfatal_with_name (dumpname);
  347. }
  348. /* If jump_opt dump desired, open the output file. */
  349. if (jump_opt_dump)
  350. {
  351. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  352. strcpy (dumpname, dump_base_name);
  353. strcat (dumpname, ".jump");
  354. jump_opt_dump_file = fopen (dumpname, "w");
  355. if (jump_opt_dump_file == 0)
  356. pfatal_with_name (dumpname);
  357. }
  358. /* If cse dump desired, open the output file. */
  359. if (cse_dump)
  360. {
  361. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  362. strcpy (dumpname, dump_base_name);
  363. strcat (dumpname, ".cse");
  364. cse_dump_file = fopen (dumpname, "w");
  365. if (cse_dump_file == 0)
  366. pfatal_with_name (dumpname);
  367. }
  368. /* If loop dump desired, open the output file. */
  369. if (loop_dump)
  370. {
  371. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  372. strcpy (dumpname, dump_base_name);
  373. strcat (dumpname, ".loop");
  374. loop_dump_file = fopen (dumpname, "w");
  375. if (loop_dump_file == 0)
  376. pfatal_with_name (dumpname);
  377. }
  378. /* If flow dump desired, open the output file. */
  379. if (flow_dump)
  380. {
  381. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  382. strcpy (dumpname, dump_base_name);
  383. strcat (dumpname, ".flow");
  384. flow_dump_file = fopen (dumpname, "w");
  385. if (flow_dump_file == 0)
  386. pfatal_with_name (dumpname);
  387. }
  388. /* If combine dump desired, open the output file. */
  389. if (combine_dump)
  390. {
  391. register char *dumpname = (char *) xmalloc (dump_base_name_length + 10);
  392. strcpy (dumpname, dump_base_name);
  393. strcat (dumpname, ".combine");
  394. combine_dump_file = fopen (dumpname, "w");
  395. if (combine_dump_file == 0)
  396. pfatal_with_name (dumpname);
  397. }
  398. /* If local_reg dump desired, open the output file. */
  399. if (local_reg_dump)
  400. {
  401. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  402. strcpy (dumpname, dump_base_name);
  403. strcat (dumpname, ".lreg");
  404. local_reg_dump_file = fopen (dumpname, "w");
  405. if (local_reg_dump_file == 0)
  406. pfatal_with_name (dumpname);
  407. }
  408. /* If global_reg dump desired, open the output file. */
  409. if (global_reg_dump)
  410. {
  411. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  412. strcpy (dumpname, dump_base_name);
  413. strcat (dumpname, ".greg");
  414. global_reg_dump_file = fopen (dumpname, "w");
  415. if (global_reg_dump_file == 0)
  416. pfatal_with_name (dumpname);
  417. }
  418. /* Open assembler code output file. */
  419. {
  420. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  421. strcpy (dumpname, dump_base_name);
  422. strcat (dumpname, ".s");
  423. asm_out_file = fopen (asm_file_name ? asm_file_name : dumpname, "w");
  424. if (asm_out_file == 0)
  425. pfatal_with_name (asm_file_name ? asm_file_name : dumpname);
  426. }
  427. input_filename = name;
  428. /* the beginning of the file is a new line; check for # */
  429. /* With luck, we discover the real source file's name from that
  430. and put it in input_filename. */
  431. check_newline ();
  432. /* If GDB symbol table desired, open the GDB symbol output file. */
  433. if (write_symbols == 1)
  434. {
  435. register char *dumpname = (char *) xmalloc (dump_base_name_length + 6);
  436. strcpy (dumpname, dump_base_name);
  437. strcat (dumpname, ".sym");
  438. if (sym_file_name == 0)
  439. sym_file_name = dumpname;
  440. symout_init (sym_file_name, asm_out_file, input_filename);
  441. }
  442. /* If dbx symbol table desired, initialize writing it
  443. and output the predefined types. */
  444. if (write_symbols == 2)
  445. dbxout_init (asm_out_file, input_filename);
  446. /* Initialize yet another pass. */
  447. init_final (input_filename);
  448. start_time = gettime ();
  449. /* Call the parser, which parses the entire file
  450. (calling rest_of_compilation for each function). */
  451. yyparse ();
  452. /* Compilation is now finished except for writing
  453. what's left of the symbol table output. */
  454. parse_time += gettime () - start_time;
  455. globals = getdecls ();
  456. /* Do dbx symbols */
  457. if (write_symbols == 2)
  458. TIMEVAR (symout_time,
  459. {
  460. dbxout_tags (gettags ());
  461. dbxout_types (get_permanent_types ());
  462. });
  463. /* Do gdb symbols */
  464. if (write_symbols == 1)
  465. TIMEVAR (symout_time,
  466. {
  467. struct stat statbuf;
  468. fstat (fileno (finput), &statbuf);
  469. symout_types (get_permanent_types ());
  470. symout_top_blocks (globals, gettags ());
  471. symout_finish (name, statbuf.st_ctime);
  472. });
  473. /* Close non-debugging input and output files. */
  474. fclose (finput);
  475. fclose (asm_out_file);
  476. if (!quiet_flag)
  477. fprintf (stderr,"\n");
  478. /* Dump the global nodes and close the tree dump file. */
  479. if (tree_dump)
  480. {
  481. dump_tree (tree_dump_file, globals);
  482. fclose (tree_dump_file);
  483. }
  484. /* Close all other dump files. */
  485. if (rtl_dump)
  486. fclose (rtl_dump_file);
  487. if (jump_opt_dump)
  488. fclose (jump_opt_dump_file);
  489. if (cse_dump)
  490. fclose (cse_dump_file);
  491. if (loop_dump)
  492. fclose (loop_dump_file);
  493. if (flow_dump)
  494. fclose (flow_dump_file);
  495. if (combine_dump)
  496. {
  497. dump_combine_total_stats (combine_dump_file);
  498. fclose (combine_dump_file);
  499. }
  500. if (local_reg_dump)
  501. fclose (local_reg_dump_file);
  502. if (global_reg_dump)
  503. fclose (global_reg_dump_file);
  504. /* Print the times. */
  505. if (! quiet_flag)
  506. {
  507. print_time ("parse", parse_time);
  508. print_time ("expand", expand_time);
  509. print_time ("jump", jump_time);
  510. print_time ("cse", cse_time);
  511. print_time ("loop", loop_time);
  512. print_time ("flow", flow_time);
  513. print_time ("combine", combine_time);
  514. print_time ("local-alloc", local_alloc_time);
  515. print_time ("global-alloc", global_alloc_time);
  516. print_time ("final", final_time);
  517. print_time ("varconst", varconst_time);
  518. print_time ("symout", symout_time);
  519. print_time ("dump", dump_time);
  520. }
  521. }
  522. /* This is called from finish_function (within yyparse)
  523. after each top-level definition is parsed, and from
  524. finish_decl (also within yyparse) for each other top-level declaration.
  525. It is supposed to compile that function or variable
  526. and output the assembler code for it.
  527. After we return, the tree storage is freed. */
  528. void
  529. rest_of_compilation (decl, top_level)
  530. tree decl;
  531. int top_level;
  532. {
  533. register rtx insns;
  534. int start_time = gettime ();
  535. int tem;
  536. /* Declarations of variables, and of functions defined elsewhere. */
  537. if ((TREE_CODE (decl) == VAR_DECL
  538. || (TREE_CODE (decl) == FUNCTION_DECL
  539. && DECL_INITIAL (decl) == 0))
  540. && (TREE_STATIC (decl) || TREE_EXTERNAL (decl)))
  541. {
  542. TIMEVAR (varconst_time,
  543. {
  544. assemble_variable (decl, top_level);
  545. if (write_symbols == 2)
  546. dbxout_symbol (decl, 0);
  547. });
  548. }
  549. /* Function definitions are the real work
  550. (all the rest of this function). */
  551. else if (TREE_CODE (decl) == FUNCTION_DECL
  552. && DECL_INITIAL (decl))
  553. {
  554. /* Dump the function's tree if we are dumping trees. */
  555. if (tree_dump)
  556. TIMEVAR (dump_time,
  557. dump_tree (tree_dump_file, decl));
  558. /* Output some preliminaries for assembler. */
  559. TIMEVAR (varconst_time, assemble_function (decl));
  560. /* Generate rtl code for this function (see stmt.c, expr.c). */
  561. TIMEVAR (expand_time,
  562. {
  563. init_emit (write_symbols);
  564. insns = expand_function (decl, !optimize);
  565. });
  566. /* Dump the rtl code if we are dumping rtl. */
  567. if (rtl_dump)
  568. TIMEVAR (dump_time,
  569. {
  570. fprintf (rtl_dump_file, "\n;; Function %s\n\n",
  571. IDENTIFIER_POINTER (DECL_NAME (decl)));
  572. print_rtl (rtl_dump_file, insns);
  573. fflush (rtl_dump_file);
  574. });
  575. if (rtl_dump_and_exit)
  576. goto exit_rest_of_compilation;
  577. /* Do jump optimization the first time, if -opt. */
  578. if (optimize)
  579. TIMEVAR (jump_time, jump_optimize (insns, 0));
  580. /* Dump rtl code after jump, if we are doing that. */
  581. if (jump_opt_dump)
  582. TIMEVAR (dump_time,
  583. {
  584. fprintf (jump_opt_dump_file, "\n;; Function %s\n\n",
  585. IDENTIFIER_POINTER (DECL_NAME (decl)));
  586. print_rtl (jump_opt_dump_file, insns);
  587. fflush (jump_opt_dump_file);
  588. });
  589. /* Perform common subexpression elimination.
  590. Nonzero value from `cse_main' means that jumps were simplified
  591. and some code may now be unreachable, so do
  592. jump optimization again. */
  593. if (optimize)
  594. {
  595. TIMEVAR (cse_time, reg_scan (insns, max_reg_num ()));
  596. TIMEVAR (cse_time, tem = cse_main (insns, max_reg_num ()));
  597. if (tem)
  598. TIMEVAR (jump_time, jump_optimize (insns, 0));
  599. }
  600. /* Dump rtl code after cse, if we are doing that. */
  601. if (cse_dump)
  602. TIMEVAR (dump_time,
  603. {
  604. fprintf (cse_dump_file, "\n;; Function %s\n\n",
  605. IDENTIFIER_POINTER (DECL_NAME (decl)));
  606. print_rtl (cse_dump_file, insns);
  607. fflush (cse_dump_file);
  608. });
  609. /* Move constant computations out of loops. */
  610. if (optimize)
  611. {
  612. TIMEVAR (loop_time, loop_optimize (insns, max_reg_num ()));
  613. }
  614. /* Dump rtl code after loop opt, if we are doing that. */
  615. if (loop_dump)
  616. TIMEVAR (dump_time,
  617. {
  618. fprintf (loop_dump_file, "\n;; Function %s\n\n",
  619. IDENTIFIER_POINTER (DECL_NAME (decl)));
  620. print_rtl (loop_dump_file, insns);
  621. fflush (loop_dump_file);
  622. });
  623. /* Now we choose between stupid (pcc-like) register allocation
  624. (if we got the -noreg switch and not -opt)
  625. and smart register allocation. */
  626. if (optimize) /* Stupid allocation probably won't work */
  627. obey_regdecls = 0; /* if optimizations being done. */
  628. /* Print function header into flow dump now
  629. because doing the flow analysis makes some of the dump. */
  630. if (flow_dump)
  631. TIMEVAR (dump_time,
  632. {
  633. fprintf (flow_dump_file, "\n;; Function %s\n\n",
  634. IDENTIFIER_POINTER (DECL_NAME (decl)));
  635. });
  636. if (obey_regdecls)
  637. {
  638. TIMEVAR (flow_time,
  639. {
  640. regclass (insns, max_reg_num ());
  641. stupid_life_analysis (insns, max_reg_num (),
  642. flow_dump_file);
  643. });
  644. }
  645. else
  646. {
  647. /* Do control and data flow analysis,
  648. and write some of the results to dump file. */
  649. TIMEVAR (flow_time, flow_analysis (insns, max_reg_num (),
  650. flow_dump_file));
  651. }
  652. /* Dump rtl after flow analysis. */
  653. if (flow_dump)
  654. TIMEVAR (dump_time,
  655. {
  656. print_rtl (flow_dump_file, insns);
  657. fflush (flow_dump_file);
  658. });
  659. /* If -opt, try combining insns through substitution. */
  660. if (optimize)
  661. TIMEVAR (combine_time, combine_instructions (insns, max_reg_num ()));
  662. /* Dump rtl code after insn combination. */
  663. if (combine_dump)
  664. TIMEVAR (dump_time,
  665. {
  666. fprintf (combine_dump_file, "\n;; Function %s\n\n",
  667. IDENTIFIER_POINTER (DECL_NAME (decl)));
  668. dump_combine_stats (combine_dump_file);
  669. print_rtl (combine_dump_file, insns);
  670. fflush (combine_dump_file);
  671. });
  672. /* Unless we did stupid register allocation,
  673. allocate pseudo-regs that are used only within 1 basic block. */
  674. if (!obey_regdecls)
  675. TIMEVAR (local_alloc_time,
  676. {
  677. regclass (insns, max_reg_num ());
  678. local_alloc ();
  679. });
  680. /* Dump rtl code after allocating regs within basic blocks. */
  681. if (local_reg_dump)
  682. TIMEVAR (dump_time,
  683. {
  684. fprintf (local_reg_dump_file, "\n;; Function %s\n\n",
  685. IDENTIFIER_POINTER (DECL_NAME (decl)));
  686. dump_flow_info (local_reg_dump_file);
  687. dump_local_alloc (local_reg_dump_file);
  688. print_rtl (local_reg_dump_file, insns);
  689. fflush (local_reg_dump_file);
  690. });
  691. if (global_reg_dump)
  692. TIMEVAR (dump_time,
  693. fprintf (global_reg_dump_file, "\n;; Function %s\n\n",
  694. IDENTIFIER_POINTER (DECL_NAME (decl))));
  695. /* Unless we did stupid register allocation,
  696. allocate remaining pseudo-regs, then do the reload pass
  697. fixing up any insns that are invalid. */
  698. TIMEVAR (global_alloc_time,
  699. {
  700. if (!obey_regdecls)
  701. global_alloc (global_reg_dump ? global_reg_dump_file : 0);
  702. else
  703. reload (insns, 0,
  704. global_reg_dump ? global_reg_dump_file : 0);
  705. });
  706. if (global_reg_dump)
  707. TIMEVAR (dump_time,
  708. {
  709. dump_global_regs (global_reg_dump_file);
  710. print_rtl (global_reg_dump_file, insns);
  711. fflush (global_reg_dump_file);
  712. });
  713. /* One more attempt to remove jumps to .+1
  714. left by dead-store-elimination.
  715. Also do cross-jumping this time. */
  716. if (optimize)
  717. TIMEVAR (jump_time, jump_optimize (insns, 1));
  718. /* Now turn the rtl into assembler code. */
  719. TIMEVAR (final_time,
  720. {
  721. final (insns, asm_out_file,
  722. IDENTIFIER_POINTER (DECL_NAME (decl)),
  723. write_symbols, optimize);
  724. fflush (asm_out_file);
  725. });
  726. /* Write GDB symbols if requested */
  727. if (write_symbols == 1)
  728. TIMEVAR (symout_time,
  729. {
  730. symout_types (get_permanent_types ());
  731. symout_types (get_temporary_types ());
  732. DECL_BLOCK_SYMTAB_ADDRESS (decl)
  733. = symout_function (DECL_INITIAL (decl),
  734. DECL_ARGUMENTS (decl), 0);
  735. });
  736. /* Write DBX symbols if requested */
  737. if (write_symbols == 2)
  738. TIMEVAR (symout_time, dbxout_function (decl));
  739. }
  740. exit_rest_of_compilation:
  741. /* The parsing time is all the time spent in yyparse
  742. *except* what is spent in this function. */
  743. parse_time -= gettime () - start_time;
  744. }
  745. /* Entry point of cc1. Decode command args, then call compile_file.
  746. Exit code is 34 if fatal error, else 33 if have error messages,
  747. else 1 if have warnings, else 0. */
  748. int
  749. main (argc, argv, envp)
  750. int argc;
  751. char **argv;
  752. char **envp;
  753. {
  754. register int i;
  755. char *filename;
  756. target_flags = 0;
  757. set_target_switch ("");
  758. for (i = 1; i < argc; i++)
  759. if (argv[i][0] == '-')
  760. {
  761. register char *str = argv[i] + 1;
  762. if (str[0] == 'Y')
  763. str++;
  764. if (str[0] == 'm')
  765. set_target_switch (&str[1]);
  766. else if (!strcmp (str, "dumpbase"))
  767. {
  768. dump_base_name = argv[++i];
  769. }
  770. else if (str[0] == 'd')
  771. {
  772. register char *p = &str[1];
  773. while (*p)
  774. switch (*p++)
  775. {
  776. case 'c':
  777. combine_dump = 1;
  778. break;
  779. case 'f':
  780. flow_dump = 1;
  781. break;
  782. case 'g':
  783. global_reg_dump = 1;
  784. break;
  785. case 'j':
  786. jump_opt_dump = 1;
  787. break;
  788. case 'l':
  789. local_reg_dump = 1;
  790. break;
  791. case 'L':
  792. loop_dump = 1;
  793. break;
  794. case 'r':
  795. rtl_dump = 1;
  796. break;
  797. case 's':
  798. cse_dump = 1;
  799. break;
  800. case 't':
  801. tree_dump = 1;
  802. break;
  803. case 'y':
  804. yydebug = 1;
  805. break;
  806. }
  807. }
  808. else if (!strcmp (str, "quiet"))
  809. quiet_flag = 1;
  810. else if (!strcmp (str, "opt"))
  811. optimize = 1;
  812. else if (!strcmp (str, "optforcemem"))
  813. force_mem = 1;
  814. else if (!strcmp (str, "optforceaddr"))
  815. force_addr = 1;
  816. else if (!strcmp (str, "noreg"))
  817. obey_regdecls = 1;
  818. else if (!strcmp (str, "w"))
  819. inhibit_warnings = 1;
  820. else if (!strcmp (str, "g"))
  821. write_symbols = 1;
  822. else if (!strcmp (str, "G"))
  823. write_symbols = 2;
  824. else if (!strcmp (str, "symout"))
  825. {
  826. if (write_symbols == 0)
  827. write_symbols = 1;
  828. sym_file_name = argv[++i];
  829. }
  830. else if (!strcmp (str, "o"))
  831. {
  832. asm_file_name = argv[++i];
  833. }
  834. else
  835. yylineerror (0, "Invalid switch, %s.", argv[i]);
  836. }
  837. else
  838. filename = argv[i];
  839. if (filename == 0)
  840. fatal ("no input file specified");
  841. if (dump_base_name == 0)
  842. dump_base_name = filename;
  843. compile_file (filename);
  844. if (errorcount)
  845. return 33;
  846. else
  847. return (warningcount > 0);
  848. }
  849. /* Decode -m switches. */
  850. /* Here is a table, controlled by the tm-...h file, listing each -m switch
  851. and which bits in `target_switches' it should set or clear.
  852. If VALUE is positive, it is bits to set.
  853. If VALUE is negative, -VALUE is bits to clear.
  854. (The sign bit is not used so there is no confusion.) */
  855. struct {char *name; int value;} target_switches []
  856. = TARGET_SWITCHES;
  857. /* Decode the switch -mNAME. */
  858. set_target_switch (name)
  859. char *name;
  860. {
  861. register int j = 0;
  862. for (j = 0; j < sizeof target_switches / sizeof target_switches[0]; j++)
  863. if (!strcmp (target_switches[j].name, name))
  864. {
  865. if (target_switches[j].value < 0)
  866. target_flags &= ~-target_switches[j].value;
  867. else
  868. target_flags |= target_switches[j].value;
  869. break;
  870. }
  871. }