Debian Codegraph @

cc/ccom/main.c

raw symbols SCIP
1 /* $Id: main.c,v 1.137 2021/08/29 09:21:56 gmcgarry Exp $ */
2
3 /*
4 * Copyright (c) 2002 Anders Magnusson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <signal.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "pass1.h"
39 #include "pass2.h"
40
41 int bdebug, ddebug, edebug, idebug, ndebug;
42 int odebug, pdebug, sdebug, tdebug, xdebug, wdebug;
43 int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug;
44 int r2debug, s2debug, t2debug, u2debug, x2debug;
45 int gflag, kflag;
46 int pflag, sflag;
47 int sspflag;
48 int xscp, xssa, xtailcall, xtemps, xdeljumps, xdce, xinline, xccp, xgnu89, xgnu99;
49 int xuchar;
50 int freestanding;
51 char *prgname, *ftitle;
52
53 static void prtstats(void);
54
55 static void
56 usage(void)
57 {
58 (void)fprintf(stderr, "usage: %s [option] [infile] [outfile]...\n",
59 prgname);
60 exit(1);
61 }
62
63 static void
64 segvcatch(int a)
65 {
66 char buf[1024];
67
68 snprintf(buf, sizeof buf, "%sinternal compiler error: %s, line %d\n",
69 nerrors ? "" : "major ", ftitle, lineno);
70 (void)write(STDERR_FILENO, buf, strlen(buf));
71 _exit(1);
72 }
73
74 static void
75 xopt(char *str)
76 {
77 if (strcmp(str, "ssa") == 0)
78 xssa++;
79 else if (strcmp(str, "tailcall") == 0)
80 xtailcall++;
81 else if (strcmp(str, "temps") == 0)
82 xtemps++;
83 else if (strcmp(str, "deljumps") == 0)
84 xdeljumps++;
85 else if (strcmp(str, "dce") == 0)
86 xdce++;
87 else if (strcmp(str, "inline") == 0)
88 xinline++;
89 else if (strcmp(str, "ccp") == 0)
90 xccp++;
91 else if (strcmp(str, "scp") == 0)
92 xscp++;
93 else if (strcmp(str, "gnu89") == 0)
94 xgnu89++;
95 else if (strcmp(str, "gnu99") == 0)
96 xgnu99++;
97 else if (strcmp(str, "uchar") == 0)
98 xuchar++;
99 else {
100 fprintf(stderr, "unknown -x option '%s'\n", str);
101 usage();
102 }
103 }
104
105 static void
106 fflags(char *str)
107 {
108 #ifndef PASS2
109 int flagval = 1;
110
111 if (strncmp("no-", str, 3) == 0) {
112 str += 3;
113 flagval = 0;
114 }
115
116 if (strcmp(str, "stack-protector") == 0)
117 sspflag = flagval;
118 else if (strcmp(str, "stack-protector-all") == 0)
119 sspflag = flagval;
120 else if (strncmp(str, "pack-struct", 11) == 0)
121 pragma_allpacked = (strlen(str) > 12 ? atoi(str+12) : 1);
122 else if (strcmp(str, "freestanding") == 0)
123 freestanding = flagval;
124 else {
125 fprintf(stderr, "unknown -f option '%s'\n", str);
126 usage();
127 }
128 #endif
129 }
130
131 /* control multiple files */
132 int
133 main(int argc, char *argv[])
134 {
135 int ch;
136 #ifndef PASS2
137 int sdflag;
138 #endif
139
140 //kflag = 1;
141 #ifdef TIMING
142 struct timeval t1, t2;
143
144 (void)gettimeofday(&t1, NULL);
145 #endif
146
147 prgname = argv[0];
148
149 while ((ch = getopt(argc, argv, "OT:VW:X:Z:f:gkm:psvwx:")) != -1) {
150 switch (ch) {
151 #ifndef PASS2
152 case 'X': /* pass1 debugging */
153 while (*optarg)
154 switch (*optarg++) {
155 case 'b': ++bdebug; break; /* buildtree */
156 case 'd': ++ddebug; break; /* declarations */
157 case 'e': ++edebug; break; /* pass1 exit */
158 case 'i': ++idebug; break; /* initializations */
159 case 'n': ++ndebug; break; /* node allocation */
160 case 'o': ++odebug; break; /* optim */
161 case 'p': ++pdebug; break; /* prototype */
162 case 's': ++sdebug; break; /* inline */
163 case 't': ++tdebug; break; /* type match */
164 case 'x': ++xdebug; break; /* MD code */
165 default:
166 fprintf(stderr, "unknown -X flag '%c'\n",
167 optarg[-1]);
168 exit(1);
169 }
170 break;
171 #endif
172 #ifndef PASS1
173 case 'Z': /* pass2 debugging */
174 while (*optarg)
175 switch (*optarg++) {
176 case 'b': /* basic block and SSA building */
177 ++b2debug;
178 break;
179 case 'c': /* code printout */
180 ++c2debug;
181 break;
182 case 'e': /* print tree upon pass2 enter */
183 ++e2debug;
184 break;
185 case 'f': /* instruction matching */
186 ++f2debug;
187 break;
188 case 'g': /* print flow graphs */
189 ++g2debug;
190 break;
191 case 'n': /* node allocation */
192 ++ndebug;
193 break;
194 case 'o': /* instruction generator */
195 ++o2debug;
196 break;
197 case 'r': /* register alloc/graph coloring */
198 ++r2debug;
199 break;
200 case 's': /* shape matching */
201 ++s2debug;
202 break;
203 case 't': /* type matching */
204 ++t2debug;
205 break;
206 case 'u': /* Sethi-Ullman debugging */
207 ++u2debug;
208 break;
209 case 'x': /* target specific */
210 ++x2debug;
211 break;
212 default:
213 fprintf(stderr, "unknown -Z flag '%c'\n",
214 optarg[-1]);
215 exit(1);
216 }
217 break;
218 #endif
219 case 'f': /* Language */
220 fflags(optarg);
221 break;
222
223 case 'g': /* Debugging */
224 ++gflag;
225 break;
226
227 case 'k': /* PIC code */
228 ++kflag;
229 break;
230 #ifndef PASS1
231 case 'm': /* Target-specific */
232 mflags(optarg);
233 break;
234 #endif
235 case 'p': /* Profiling */
236 ++pflag;
237 break;
238
239 case 's': /* Statistics */
240 ++sflag;
241 break;
242
243 case 'w': /* No warnings emitted */
244 ++wdebug;
245 break;
246
247 case 'W': /* Enable different warnings */
248 Wflags(optarg);
249 break;
250
251 case 'x': /* Different settings */
252 xopt(optarg);
253 break;
254
255 case 'v':
256 printf("ccom: %s\n", VERSSTR);
257 break;
258
259 case '?':
260 default:
261 usage();
262 }
263 }
264 argc -= optind;
265 argv += optind;
266
267 ftitle = xstrdup("<stdin>");
268 if (argc > 0 && strcmp(argv[0], "-") != 0) {
269 if (freopen(argv[0], "r", stdin) == NULL) {
270 fprintf(stderr, "open input file '%s':",
271 argv[0]);
272 perror(NULL);
273 exit(1);
274 }
275 }
276 if (argc > 1 && strcmp(argv[1], "-") != 0) {
277 if (freopen(argv[1], "w", stdout) == NULL) {
278 fprintf(stderr, "open output file '%s':",
279 argv[1]);
280 perror(NULL);
281 exit(1);
282 }
283 }
284
285 mkdope();
286 signal(SIGSEGV, segvcatch);
287 #ifdef SIGBUS
288 signal(SIGBUS, segvcatch);
289 #endif
290
291 #ifndef PASS2
292 lineno = 1;
293 sdflag = ddebug;
294 ddebug = 0;
295 flostat = FP_CONTR_CBR;
296 #ifdef GCC_COMPAT
297 gcc_init();
298 #endif
299
300 /* starts past any of the above */
301 reached = 1;
302
303 bjobcode();
304 #ifndef TARGET_VALIST
305 {
306 P1ND *p = block(NAME, NULL, NULL, PTR|CHAR, NULL, 0);
307 struct symtab *sp = lookup(addname("__builtin_va_list"), 0);
308 p->n_sp = sp;
309 defid(p, TYPEDEF);
310 p1nfree(p);
311 }
312 #endif
313 complinit();
314 kwinit();
315 #ifndef NO_BUILTIN
316 builtin_init();
317 #endif
318 ddebug = sdflag;
319
320 #ifdef DWARF
321 if (gflag)
322 dwarf_init(argc ? argv[0] : "");
323 #endif
324 #ifdef STABS
325 if (gflag) {
326 stabs_file(argc ? argv[0] : "");
327 stabs_init();
328 }
329 #endif
330
331 if (sspflag)
332 sspinit();
333 #endif /* PASS2 */
334
335 #ifndef PASS1
336 fregs = FREGS; /* number of free registers */
337 #ifdef PASS2
338 mainp2();
339 #endif
340 #endif
341
342 #ifndef PASS2
343 (void) yyparse();
344 yyaccpt();
345
346 if (!nerrors) {
347 lcommprint();
348 #ifndef NO_STRING_SAVE
349 strprint();
350 #endif
351 }
352 #endif
353
354 #ifndef PASS2
355 #ifdef STABS
356 if (gflag)
357 stabs_efile(argc ? argv[0] : "");
358 #endif
359 ejobcode( nerrors ? 1 : 0 );
360 #endif
361
362 #ifdef TIMING
363 (void)gettimeofday(&t2, NULL);
364 t2.tv_sec -= t1.tv_sec;
365 t2.tv_usec -= t1.tv_usec;
366 if (t2.tv_usec < 0) {
367 t2.tv_usec += 1000000;
368 t2.tv_sec -= 1;
369 }
370 fprintf(stderr, "ccom total time: %ld s %ld us\n",
371 t2.tv_sec, t2.tv_usec);
372 #endif
373 #ifdef DWARF
374 if (gflag)
375 dwarf_end();
376 #endif
377
378 if (sflag)
379 prtstats();
380
381 return(nerrors?1:0);
382 }
383
384 void
385 prtstats(void)
386 {
387 #ifndef PASS2
388 extern int nametabs, namestrlen, treestrsz;
389 extern int arglistcnt, dimfuncnt, inlstatcnt;
390 extern int symtabcnt, suedefcnt, strtabs, strstrlen;
391 extern int blkalloccnt, lcommsz, istatsz;
392 extern int savstringsz, newattrsz, nodesszcnt, symtreecnt;
393 #endif
394 extern size_t permallocsize, tmpallocsize, lostmem;
395
396 /* common allocations */
397 fprintf(stderr, "Permanent allocated memory: %zu B\n", permallocsize);
398 fprintf(stderr, "Temporary allocated memory: %zu B\n", tmpallocsize);
399 fprintf(stderr, "Lost memory: %zu B\n", lostmem);
400
401 #ifndef PASS2
402 /* pass1 allocations */
403 fprintf(stderr, "Name table entries: %d pcs\n", nametabs);
404 fprintf(stderr, "String table entries: %d pcs\n", strtabs);
405 fprintf(stderr, "Argument list unions: %d pcs\n", arglistcnt);
406 fprintf(stderr, "Dimension/function unions: %d pcs\n", dimfuncnt);
407 fprintf(stderr, "Struct/union/enum blocks: %d pcs\n", suedefcnt);
408 fprintf(stderr, "Inline control blocks: %d pcs\n", inlstatcnt);
409 fprintf(stderr, "Permanent symtab entries: %d pcs\n", symtabcnt);
410 fprintf(stderr, "\n");
411 fprintf(stderr, "Name table tree size: %d B\n",
412 nametabs * treestrsz);
413 fprintf(stderr, "Name string size: %d B\n", namestrlen);
414 fprintf(stderr, "String table tree size: %d B\n",
415 strtabs * treestrsz);
416 fprintf(stderr, "String size: %d B\n", strstrlen);
417 fprintf(stderr, "Inline control block size: %d B\n",
418 inlstatcnt * istatsz);
419 fprintf(stderr, "Argument list size: %d B\n",
420 arglistcnt * (int)sizeof(union arglist));
421 fprintf(stderr, "Dimension/function size: %d B\n",
422 dimfuncnt * (int)sizeof(union dimfun));
423 fprintf(stderr, "Permanent symtab size: %d B\n",
424 symtabcnt * (int)sizeof(struct symtab));
425 fprintf(stderr, "Symtab tree size: %d B\n",
426 symtreecnt * treestrsz);
427 fprintf(stderr, "lcomm struct size: %d B\n", lcommsz);
428 fprintf(stderr, "blkalloc size: %d B\n", blkalloccnt);
429 fprintf(stderr, "(saved strings size): %d B\n", savstringsz);
430 fprintf(stderr, "attribute size: %d B\n", newattrsz);
431 fprintf(stderr, "nodes size: %d B\n", nodesszcnt);
432 fprintf(stderr, "\n");
433 fprintf(stderr, "Not accounted for: %d B\n",
434 (int)permallocsize-(nametabs * treestrsz)-namestrlen-strstrlen-
435 (arglistcnt * (int)sizeof(union arglist))-(strtabs * treestrsz)-
436 (dimfuncnt * (int)sizeof(union dimfun))-(inlstatcnt * istatsz)-
437 (symtabcnt * (int)sizeof(struct symtab))-(symtreecnt * treestrsz)-
438 lcommsz-blkalloccnt-newattrsz-nodesszcnt);
439 #endif
440 }