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