Debian Codegraph @

cc/cxxcom/main.c

raw symbols SCIP
1 /* $Id: main.c,v 1.7 2017/03/16 16:32:51 ragge 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, cppdebug, wdebug;
43 int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug;
44 int r2debug, s2debug, t2debug, u2debug, x2debug;
45 int gflag, kflag, pflag, sflag;
46 int sspflag;
47 int xscp, xssa, xtailcall, xtemps, xdeljumps, xdce, xinline, xccp, xgnu89, xgnu99;
48 int xuchar;
49 int freestanding;
50 char *prgname;
51
52 static void prtstats(void);
53
54 static void
55 usage(void)
56 {
57 (void)fprintf(stderr, "usage: %s [option] [infile] [outfile]...\n",
58 prgname);
59 exit(1);
60 }
61
62 static void
63 segvcatch(int a)
64 {
65 char buf[1024];
66
67 snprintf(buf, sizeof buf, "%sinternal compiler error: %s, line %d\n",
68 nerrors ? "" : "major ", ftitle, lineno);
69 (void)write(STDERR_FILENO, buf, strlen(buf));
70 _exit(1);
71 }
72
73 static void
74 xopt(char *str)
75 {
76 if (strcmp(str, "ssa") == 0)
77 xssa++;
78 else if (strcmp(str, "tailcall") == 0)
79 xtailcall++;
80 else if (strcmp(str, "temps") == 0)
81 xtemps++;
82 else if (strcmp(str, "deljumps") == 0)
83 xdeljumps++;
84 else if (strcmp(str, "dce") == 0)
85 xdce++;
86 else if (strcmp(str, "inline") == 0)
87 xinline++;
88 else if (strcmp(str, "ccp") == 0)
89 xccp++;
90 else if (strcmp(str, "gnu89") == 0)
91 xgnu89++;
92 else if (strcmp(str, "gnu99") == 0)
93 xgnu99++;
94 else if (strcmp(str, "uchar") == 0)
95 xuchar++;
96 else {
97 fprintf(stderr, "unknown -x option '%s'\n", str);
98 usage();
99 }
100 }
101
102 static void
103 fflags(char *str)
104 {
105 int flagval = 1;
106
107 if (strncmp("no-", str, 3) == 0) {
108 str += 3;
109 flagval = 0;
110 }
111
112 if (strcmp(str, "stack-protector") == 0)
113 sspflag = flagval;
114 else if (strcmp(str, "stack-protector-all") == 0)
115 sspflag = flagval;
116 else if (strncmp(str, "pack-struct", 11) == 0)
117 pragma_allpacked = (strlen(str) > 12 ? atoi(str+12) : 1);
118 else if (strcmp(str, "freestanding") == 0)
119 freestanding = flagval;
120 else {
121 fprintf(stderr, "unknown -f option '%s'\n", str);
122 usage();
123 }
124 }
125
126 /* control multiple files */
127 int
128 main(int argc, char *argv[])
129 {
130 int ch;
131
132 #ifdef TIMING
133 struct timeval t1, t2;
134
135 (void)gettimeofday(&t1, NULL);
136 #endif
137
138 prgname = argv[0];
139
140 while ((ch = getopt(argc, argv, "OT:VW:X:Z:f:gkm:psvwx:")) != -1) {
141 switch (ch) {
142 #if !defined(MULTIPASS) || defined(PASS1)
143 case 'X': /* pass1 debugging */
144 while (*optarg)
145 switch (*optarg++) {
146 case 'b': ++bdebug; break; /* buildtree */
147 case 'd': ++ddebug; break; /* declarations */
148 case 'e': ++edebug; break; /* pass1 exit */
149 case 'i': ++idebug; break; /* initializations */
150 case 'n': ++ndebug; break; /* node allocation */
151 case 'o': ++odebug; break; /* optim */
152 case 'p': ++pdebug; break; /* prototype */
153 case 's': ++sdebug; break; /* inline */
154 case 't': ++tdebug; break; /* type match */
155 case 'x': ++xdebug; break; /* MD code */
156 case '+': ++cppdebug; break; /* C++ */
157 default:
158 fprintf(stderr, "unknown -X flag '%c'\n",
159 optarg[-1]);
160 exit(1);
161 }
162 break;
163 #endif
164 #if !defined(MULTIPASS) || defined(PASS2)
165 case 'Z': /* pass2 debugging */
166 while (*optarg)
167 switch (*optarg++) {
168 case 'b': /* basic block and SSA building */
169 ++b2debug;
170 break;
171 case 'c': /* code printout */
172 ++c2debug;
173 break;
174 case 'e': /* print tree upon pass2 enter */
175 ++e2debug;
176 break;
177 case 'f': /* instruction matching */
178 ++f2debug;
179 break;
180 case 'g': /* print flow graphs */
181 ++g2debug;
182 break;
183 case 'n': /* node allocation */
184 ++ndebug;
185 break;
186 case 'o': /* instruction generator */
187 ++o2debug;
188 break;
189 case 'r': /* register alloc/graph coloring */
190 ++r2debug;
191 break;
192 case 's': /* shape matching */
193 ++s2debug;
194 break;
195 case 't': /* type matching */
196 ++t2debug;
197 break;
198 case 'u': /* Sethi-Ullman debugging */
199 ++u2debug;
200 break;
201 case 'x': /* target specific */
202 ++x2debug;
203 break;
204 default:
205 fprintf(stderr, "unknown -Z flag '%c'\n",
206 optarg[-1]);
207 exit(1);
208 }
209 break;
210 #endif
211 case 'f': /* Language */
212 fflags(optarg);
213 break;
214
215 case 'g': /* Debugging */
216 gflag = 1;
217 break;
218
219 case 'k': /* PIC code */
220 ++kflag;
221 break;
222
223 case 'm': /* Target-specific */
224 mflags(optarg);
225 break;
226
227 case 'p': /* Profiling */
228 pflag = 1;
229 break;
230
231 case 's': /* Statistics */
232 ++sflag;
233 break;
234
235 case 'W': /* Enable different warnings */
236 Wflags(optarg);
237 break;
238
239 case 'x': /* Different settings */
240 xopt(optarg);
241 break;
242
243 case 'v':
244 printf("ccom: %s\n", VERSSTR);
245 break;
246
247 case '?':
248 default:
249 usage();
250 }
251 }
252 argc -= optind;
253 argv += optind;
254
255 if (argc > 0 && strcmp(argv[0], "-") != 0) {
256 if (freopen(argv[0], "r", stdin) == NULL) {
257 fprintf(stderr, "open input file '%s':",
258 argv[0]);
259 perror(NULL);
260 exit(1);
261 }
262 }
263 if (argc > 1 && strcmp(argv[1], "-") != 0) {
264 if (freopen(argv[1], "w", stdout) == NULL) {
265 fprintf(stderr, "open output file '%s':",
266 argv[1]);
267 perror(NULL);
268 exit(1);
269 }
270 }
271
272 mkdope();
273 signal(SIGSEGV, segvcatch);
274 #ifdef SIGBUS
275 signal(SIGBUS, segvcatch);
276 #endif
277 fregs = FREGS; /* number of free registers */
278 lineno = 1;
279 #ifdef GCC_COMPAT
280 gcc_init();
281 #endif
282
283 /* starts past any of the above */
284 reached = 1;
285
286 bjobcode();
287 #ifndef TARGET_VALIST
288 {
289 NODE *p = block(NAME, NIL, NIL, PTR|CHAR, NULL, 0);
290 struct symtab *sp = lookup(addname("__builtin_va_list"), 0);
291 p->n_sp = sp;
292 defid(p, TYPEDEF);
293 nfree(p);
294 }
295 #endif
296 complinit();
297
298 #ifdef STABS
299 if (gflag) {
300 stabs_file(argc ? argv[0] : "");
301 stabs_init();
302 }
303 #endif
304
305 if (sspflag)
306 sspinit();
307
308 (void) yyparse();
309 yyaccpt();
310
311 if (!nerrors)
312 lcommprint();
313
314 #ifdef STABS
315 if (gflag)
316 stabs_efile(argc ? argv[0] : "");
317 #endif
318
319 ejobcode( nerrors ? 1 : 0 );
320
321 #ifdef TIMING
322 (void)gettimeofday(&t2, NULL);
323 t2.tv_sec -= t1.tv_sec;
324 t2.tv_usec -= t1.tv_usec;
325 if (t2.tv_usec < 0) {
326 t2.tv_usec += 1000000;
327 t2.tv_sec -= 1;
328 }
329 fprintf(stderr, "ccom total time: %ld s %ld us\n",
330 t2.tv_sec, t2.tv_usec);
331 #endif
332
333 if (sflag)
334 prtstats();
335
336 return(nerrors?1:0);
337 }
338
339 void
340 prtstats(void)
341 {
342 extern int nametabs, namestrlen, tmpallocsize, permallocsize;
343 extern int lostmem, arglistcnt, dimfuncnt, inlnodecnt, inlstatcnt;
344 extern int symtabcnt, suedefcnt;
345
346 fprintf(stderr, "Name table entries: %d pcs\n", nametabs);
347 fprintf(stderr, "Name string size: %d B\n", namestrlen);
348 fprintf(stderr, "Permanent allocated memory: %d B\n", permallocsize);
349 fprintf(stderr, "Temporary allocated memory: %d B\n", tmpallocsize);
350 fprintf(stderr, "Lost memory: %d B\n", lostmem);
351 fprintf(stderr, "Argument list unions: %d pcs\n", arglistcnt);
352 fprintf(stderr, "Dimension/function unions: %d pcs\n", dimfuncnt);
353 fprintf(stderr, "Struct/union/enum blocks: %d pcs\n", suedefcnt);
354 fprintf(stderr, "Inline node count: %d pcs\n", inlnodecnt);
355 fprintf(stderr, "Inline control blocks: %d pcs\n", inlstatcnt);
356 fprintf(stderr, "Permanent symtab entries: %d pcs\n", symtabcnt);
357 }