1 /* Id: main.c,v 1.6 2014/10/12 11:52:13 ragge Exp */
2 /* $NetBSD: main.c,v 1.1.1.4 2016/02/09 20:28:59 plunky Exp $ */
3
4 /*
5 * Copyright (c) 2002 Anders Magnusson. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "config.h"
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <signal.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #include "pass1.h"
40 #include "pass2.h"
41
42 int bdebug, ddebug, edebug, idebug, ndebug;
43 int odebug, pdebug, sdebug, tdebug, xdebug, cppdebug, wdebug;
44 int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug;
45 int r2debug, s2debug, t2debug, u2debug, x2debug;
46 int gflag, kflag, pflag, sflag;
47 int sspflag;
48 int xssa, xtailcall, xtemps, xdeljumps, xdce, xinline, xccp, xgnu89, xgnu99;
49 int xuchar;
50 int freestanding;
51 char *prgname;
52
53 static void prtstats(void);
54
55 static void
usage(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
segvcatch(int a)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
xopt(char * str)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, "gnu89") == 0)
92 xgnu89++;
93 else if (strcmp(str, "gnu99") == 0)
94 xgnu99++;
95 else if (strcmp(str, "uchar") == 0)
96 xuchar++;
97 else {
98 fprintf(stderr, "unknown -x option '%s'\n", str);
99 usage();
100 }
101 }
102
103 static void
fflags(char * str)104 fflags(char *str)
105 {
106 int flagval = 1;
107
108 if (strncmp("no-", str, 3) == 0) {
109 str += 3;
110 flagval = 0;
111 }
112
113 if (strcmp(str, "stack-protector") == 0)
114 sspflag = flagval;
115 else if (strcmp(str, "stack-protector-all") == 0)
116 sspflag = flagval;
117 else if (strncmp(str, "pack-struct", 11) == 0)
118 pragma_allpacked = (strlen(str) > 12 ? atoi(str+12) : 1);
119 else if (strcmp(str, "freestanding") == 0)
120 freestanding = flagval;
121 else {
122 fprintf(stderr, "unknown -f option '%s'\n", str);
123 usage();
124 }
125 }
126
127 /* control multiple files */
128 int
main(int argc,char * argv[])129 main(int argc, char *argv[])
130 {
131 int ch;
132
133 #ifdef TIMING
134 struct timeval t1, t2;
135
136 (void)gettimeofday(&t1, NULL);
137 #endif
138
139 prgname = argv[0];
140
141 while ((ch = getopt(argc, argv, "OT:VW:X:Z:f:gkm:psvwx:")) != -1) {
142 switch (ch) {
143 #if !defined(MULTIPASS) || defined(PASS1)
144 case 'X': /* pass1 debugging */
145 while (*optarg)
146 switch (*optarg++) {
147 case 'b': ++bdebug; break; /* buildtree */
148 case 'd': ++ddebug; break; /* declarations */
149 case 'e': ++edebug; break; /* pass1 exit */
150 case 'i': ++idebug; break; /* initializations */
151 case 'n': ++ndebug; break; /* node allocation */
152 case 'o': ++odebug; break; /* optim */
153 case 'p': ++pdebug; break; /* prototype */
154 case 's': ++sdebug; break; /* inline */
155 case 't': ++tdebug; break; /* type match */
156 case 'x': ++xdebug; break; /* MD code */
157 case '+': ++cppdebug; break; /* C++ */
158 default:
159 fprintf(stderr, "unknown -X flag '%c'\n",
160 optarg[-1]);
161 exit(1);
162 }
163 break;
164 #endif
165 #if !defined(MULTIPASS) || defined(PASS2)
166 case 'Z': /* pass2 debugging */
167 while (*optarg)
168 switch (*optarg++) {
169 case 'b': /* basic block and SSA building */
170 ++b2debug;
171 break;
172 case 'c': /* code printout */
173 ++c2debug;
174 break;
175 case 'e': /* print tree upon pass2 enter */
176 ++e2debug;
177 break;
178 case 'f': /* instruction matching */
179 ++f2debug;
180 break;
181 case 'g': /* print flow graphs */
182 ++g2debug;
183 break;
184 case 'n': /* node allocation */
185 ++ndebug;
186 break;
187 case 'o': /* instruction generator */
188 ++o2debug;
189 break;
190 case 'r': /* register alloc/graph coloring */
191 ++r2debug;
192 break;
193 case 's': /* shape matching */
194 ++s2debug;
195 break;
196 case 't': /* type matching */
197 ++t2debug;
198 break;
199 case 'u': /* Sethi-Ullman debugging */
200 ++u2debug;
201 break;
202 case 'x': /* target specific */
203 ++x2debug;
204 break;
205 default:
206 fprintf(stderr, "unknown -Z flag '%c'\n",
207 optarg[-1]);
208 exit(1);
209 }
210 break;
211 #endif
212 case 'f': /* Language */
213 fflags(optarg);
214 break;
215
216 case 'g': /* Debugging */
217 gflag = 1;
218 break;
219
220 case 'k': /* PIC code */
221 ++kflag;
222 break;
223
224 case 'm': /* Target-specific */
225 mflags(optarg);
226 break;
227
228 case 'p': /* Profiling */
229 pflag = 1;
230 break;
231
232 case 's': /* Statistics */
233 ++sflag;
234 break;
235
236 case 'W': /* Enable different warnings */
237 Wflags(optarg);
238 break;
239
240 case 'x': /* Different settings */
241 xopt(optarg);
242 break;
243
244 case 'v':
245 printf("ccom: %s\n", VERSSTR);
246 break;
247
248 case '?':
249 default:
250 usage();
251 }
252 }
253 argc -= optind;
254 argv += optind;
255
256 if (argc > 0 && strcmp(argv[0], "-") != 0) {
257 if (freopen(argv[0], "r", stdin) == NULL) {
258 fprintf(stderr, "open input file '%s':",
259 argv[0]);
260 perror(NULL);
261 exit(1);
262 }
263 }
264 if (argc > 1 && strcmp(argv[1], "-") != 0) {
265 if (freopen(argv[1], "w", stdout) == NULL) {
266 fprintf(stderr, "open output file '%s':",
267 argv[1]);
268 perror(NULL);
269 exit(1);
270 }
271 }
272
273 mkdope();
274 signal(SIGSEGV, segvcatch);
275 #ifdef SIGBUS
276 signal(SIGBUS, segvcatch);
277 #endif
278 fregs = FREGS; /* number of free registers */
279 lineno = 1;
280 #ifdef GCC_COMPAT
281 gcc_init();
282 #endif
283
284 /* starts past any of the above */
285 reached = 1;
286
287 bjobcode();
288 #ifndef TARGET_VALIST
289 {
290 NODE *p = block(NAME, NIL, NIL, PTR|CHAR, NULL, 0);
291 struct symtab *sp = lookup(addname("__builtin_va_list"), 0);
292 p->n_sp = sp;
293 defid(p, TYPEDEF);
294 nfree(p);
295 }
296 #endif
297 complinit();
298
299 #ifdef STABS
300 if (gflag) {
301 stabs_file(argc ? argv[0] : "");
302 stabs_init();
303 }
304 #endif
305
306 if (sspflag)
307 sspinit();
308
309 (void) yyparse();
310 yyaccpt();
311
312 if (!nerrors)
313 lcommprint();
314
315 #ifdef STABS
316 if (gflag)
317 stabs_efile(argc ? argv[0] : "");
318 #endif
319
320 ejobcode( nerrors ? 1 : 0 );
321
322 #ifdef TIMING
323 (void)gettimeofday(&t2, NULL);
324 t2.tv_sec -= t1.tv_sec;
325 t2.tv_usec -= t1.tv_usec;
326 if (t2.tv_usec < 0) {
327 t2.tv_usec += 1000000;
328 t2.tv_sec -= 1;
329 }
330 fprintf(stderr, "ccom total time: %ld s %ld us\n",
331 t2.tv_sec, t2.tv_usec);
332 #endif
333
334 if (sflag)
335 prtstats();
336
337 return(nerrors?1:0);
338 }
339
340 void
prtstats(void)341 prtstats(void)
342 {
343 extern int nametabs, namestrlen, tmpallocsize, permallocsize;
344 extern int lostmem, arglistcnt, dimfuncnt, inlnodecnt, inlstatcnt;
345 extern int symtabcnt, suedefcnt;
346
347 fprintf(stderr, "Name table entries: %d pcs\n", nametabs);
348 fprintf(stderr, "Name string size: %d B\n", namestrlen);
349 fprintf(stderr, "Permanent allocated memory: %d B\n", permallocsize);
350 fprintf(stderr, "Temporary allocated memory: %d B\n", tmpallocsize);
351 fprintf(stderr, "Lost memory: %d B\n", lostmem);
352 fprintf(stderr, "Argument list unions: %d pcs\n", arglistcnt);
353 fprintf(stderr, "Dimension/function unions: %d pcs\n", dimfuncnt);
354 fprintf(stderr, "Struct/union/enum blocks: %d pcs\n", suedefcnt);
355 fprintf(stderr, "Inline node count: %d pcs\n", inlnodecnt);
356 fprintf(stderr, "Inline control blocks: %d pcs\n", inlstatcnt);
357 fprintf(stderr, "Permanent symtab entries: %d pcs\n", symtabcnt);
358 }
359