hugeval test
[cparser] / main.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #ifdef _WIN32
32
33 #include <fcntl.h>
34 #include <io.h>
35
36 /* no eXecute on Win32 */
37 #define X_OK 0
38 #define W_OK 2
39 #define R_OK 4
40
41 #define O_RDWR          _O_RDWR
42 #define O_CREAT         _O_CREAT
43 #define O_EXCL          _O_EXCL
44 #define O_BINARY        _O_BINARY
45
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode)      _access(fname, mode)
48 #define mktemp(tmpl)             _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode)         _fdopen(fd, mode)
51 #define popen(cmd, mode)         _popen(cmd, mode)
52 #define pclose(file)             _pclose(file)
53
54 #else
55 #include <unistd.h>
56 #define HAVE_MKSTEMP
57 #endif
58
59 #ifndef WITH_LIBCORE
60 #define WITH_LIBCORE
61 #endif
62
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
65
66 #include "lexer.h"
67 #include "token_t.h"
68 #include "types.h"
69 #include "type_hash.h"
70 #include "parser.h"
71 #include "ast2firm.h"
72 #include "diagnostic.h"
73 #include "lang_features.h"
74 #include "driver/firm_opt.h"
75 #include "driver/firm_cmdline.h"
76 #include "adt/error.h"
77 #include "write_fluffy.h"
78 #include "revision.h"
79 #include "warning.h"
80
81 #ifndef PREPROCESSOR
82 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
83 #endif
84
85 #ifndef LINKER
86 #define LINKER    "gcc -m32"
87 #endif
88
89 #ifndef ASSEMBLER
90 #define ASSEMBLER "as --32"
91 #endif
92
93 /** The current c mode/dialect. */
94 unsigned int c_mode = _C89|_C99|_GNUC;
95
96 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
97 unsigned int machine_size = 32;
98
99 /** true if the char type is signed. */
100 bool char_is_signed = true;
101
102 /** true for strict language checking. */
103 bool strict_mode = false;
104
105 /* to switch on printing of implicit casts */
106 extern bool print_implicit_casts;
107
108 /* to switch on printing of parenthesis to indicate operator precedence */
109 extern bool print_parenthesis;
110
111 static int            verbose;
112 static struct obstack cppflags_obst, ldflags_obst;
113
114 #if defined(_DEBUG) || defined(FIRM_DEBUG)
115 /**
116  * Debug printf implementation.
117  *
118  * @param fmt  printf style format parameter
119  */
120 void dbg_printf(const char *fmt, ...)
121 {
122         va_list list;
123
124         if (firm_dump.debug_print) {
125                 va_start(list, fmt);
126                 vprintf(fmt, list);
127                 va_end(list);
128         }  /* if */
129 }
130 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
131
132 static void initialize_firm(void)
133 {
134         firm_early_init();
135
136         dump_consts_local(1);
137         dump_keepalive_edges(1);
138 }
139
140 static void get_output_name(char *buf, size_t buflen, const char *inputname,
141                             const char *newext)
142 {
143         size_t last_dot = 0xffffffff;
144         size_t i = 0;
145
146         if(inputname == NULL) {
147                 snprintf(buf, buflen, "a%s", newext);
148                 return;
149         }
150
151         for(const char *c = inputname; *c != 0; ++c) {
152                 if(*c == '.')
153                         last_dot = i;
154                 ++i;
155         }
156         if(last_dot == 0xffffffff)
157                 last_dot = i;
158
159         if(last_dot >= buflen)
160                 panic("filename too long");
161         memcpy(buf, inputname, last_dot);
162
163         size_t extlen = strlen(newext) + 1;
164         if(extlen + last_dot >= buflen)
165                 panic("filename too long");
166         memcpy(buf+last_dot, newext, extlen);
167 }
168
169 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
170 {
171         lexer_open_stream(in, input_name);
172         translation_unit_t *unit = parse();
173         return unit;
174 }
175
176 static void lextest(FILE *in, const char *fname)
177 {
178         lexer_open_stream(in, fname);
179
180         do {
181                 lexer_next_preprocessing_token();
182                 print_token(stdout, &lexer_token);
183                 puts("");
184         } while(lexer_token.type != T_EOF);
185 }
186
187 static FILE *preprocess(FILE *in, const char *fname)
188 {
189         char buf[4096];
190         obstack_1grow(&cppflags_obst, '\0');
191         const char *flags = obstack_finish(&cppflags_obst);
192
193         if(in != stdin) {
194                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
195         } else {
196                 /* read from stdin */
197                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
198         }
199
200         if(verbose) {
201                 puts(buf);
202         }
203         FILE *f = popen(buf, "r");
204         if(f == NULL) {
205                 fprintf(stderr, "invoking preprocessor failed\n");
206                 exit(1);
207         }
208         return f;
209 }
210
211 static void do_link(const char *out, const char *in)
212 {
213         char buf[4096];
214         obstack_1grow(&ldflags_obst, '\0');
215         const char *flags = obstack_finish(&ldflags_obst);
216
217         snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
218         if(verbose) {
219                 puts(buf);
220         }
221         int err = system(buf);
222         if(err != 0) {
223                 fprintf(stderr, "linker reported an error\n");
224                 exit(1);
225         }
226 }
227
228 static void assemble(const char *out, const char *in)
229 {
230         char buf[4096];
231
232         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
233         if(verbose) {
234                 puts(buf);
235         }
236
237         int err = system(buf);
238         if(err != 0) {
239                 fprintf(stderr, "assembler reported an error\n");
240                 exit(1);
241         }
242 }
243
244 static const char *try_dir(const char *dir)
245 {
246         if(dir == NULL)
247                 return dir;
248         if(access(dir, R_OK | W_OK | X_OK) == 0)
249                 return dir;
250         return NULL;
251 }
252
253 static const char *get_tempdir(void)
254 {
255         static const char *tmpdir = NULL;
256
257         if(tmpdir != NULL)
258                 return tmpdir;
259
260         if(tmpdir == NULL)
261                 tmpdir = try_dir(getenv("TMPDIR"));
262         if(tmpdir == NULL)
263                 tmpdir = try_dir(getenv("TMP"));
264         if(tmpdir == NULL)
265                 tmpdir = try_dir(getenv("TEMP"));
266
267 #ifdef P_tmpdir
268         if(tmpdir == NULL)
269                 tmpdir = try_dir(P_tmpdir);
270 #endif
271
272         if(tmpdir == NULL)
273                 tmpdir = try_dir("/var/tmp");
274         if(tmpdir == NULL)
275                 tmpdir = try_dir("/usr/tmp");
276         if(tmpdir == NULL)
277                 tmpdir = try_dir("/tmp");
278
279         if(tmpdir == NULL)
280                 tmpdir = ".";
281
282         return tmpdir;
283 }
284
285 #ifndef HAVE_MKSTEMP
286 /* cheap and nasty mkstemp replacement */
287 static int mkstemp(char *templ)
288 {
289         mktemp(templ);
290         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
291 }
292 #endif
293
294 /**
295  * an own version of tmpnam, which: writes in a buffer, appends a user specified
296  * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
297  */
298 static FILE *make_temp_file(char *buffer, size_t buflen,
299                             const char *prefix, const char *suffix)
300 {
301         const char *tempdir = get_tempdir();
302
303         /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
304         (void) suffix;
305         suffix = "";
306
307         snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
308
309         int fd = mkstemp(buffer);
310         if(fd == -1) {
311                 fprintf(stderr, "couldn't create temporary file: %s\n",
312                         strerror(errno));
313                 exit(1);
314         }
315         FILE *out = fdopen(fd, "w");
316         if(out == NULL) {
317                 fprintf(stderr, "couldn't create temporary file FILE*\n");
318                 exit(1);
319         }
320
321         return out;
322 }
323
324 /**
325  * Do the necessary lowering for compound parameters.
326  */
327 void lower_compound_params(void)
328 {
329         lower_params_t params;
330
331         params.def_ptr_alignment    = 4;
332         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
333         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
334         params.find_pointer_type    = NULL;
335         params.ret_compound_in_regs = NULL;
336         lower_calls_with_compounds(&params);
337 }
338
339 typedef enum compile_mode_t {
340         BenchmarkParser,
341         ParseOnly,
342         Compile,
343         CompileDump,
344         CompileAssemble,
345         CompileAssembleLink,
346         LexTest,
347         PrintAst,
348         PrintFluffy
349 } compile_mode_t;
350
351 static void usage(const char *argv0)
352 {
353         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
354 }
355
356 int main(int argc, char **argv)
357 {
358         initialize_firm();
359
360         const char     *input        = NULL;
361         const char     *outname      = NULL;
362         const char     *dumpfunction = NULL;
363         compile_mode_t  mode         = CompileAssembleLink;
364         int             opt_level    = 1;
365         int             result       = EXIT_SUCCESS;
366         char            cpu_arch[16] = "ia32";
367
368         obstack_init(&cppflags_obst);
369         obstack_init(&ldflags_obst);
370
371 #define GET_ARG_AFTER(def, args)                                             \
372         def = &arg[sizeof(args)-1];                                              \
373         if(def[0] == '\0') {                                                     \
374                 ++i;                                                                 \
375                 if(i >= argc) {                                                      \
376                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
377                         argument_errors = true;                                          \
378                         break;                                                           \
379                 }                                                                    \
380                 def = argv[i];                                                       \
381                 if(def[0] == '-' && def[1] != '\0') {                                \
382                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
383                         argument_errors = true;                                          \
384                         continue;                                                        \
385                 }                                                                    \
386         }
387
388 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
389
390         /* early options parsing (find out optimisation level) */
391         for(int i = 1; i < argc; ++i) {
392                 const char *arg = argv[i];
393                 if(arg[0] != '-')
394                         continue;
395
396                 const char *option = &arg[1];
397                 if(option[0] == 'O') {
398                         sscanf(&option[1], "%d", &opt_level);
399                 }
400         }
401
402         /* apply optimisation level */
403         switch(opt_level) {
404         case 0:
405                 firm_option("no-opt");
406                 break;
407         case 1:
408                 firm_option("no-inline");
409                 break;
410         default:
411         case 4:
412                 firm_option("strict-aliasing");
413                 /* fallthrough */
414         case 3:
415                 firm_option("cond-eval");
416                 firm_option("if-conv");
417                 /* fallthrough */
418         case 2:
419                 firm_option("inline");
420                 firm_option("deconv");
421                 firm_be_option("omitfp");
422                 break;
423         }
424
425         /* parse rest of options */
426         bool help_displayed  = false;
427         bool argument_errors = false;
428         for(int i = 1; i < argc; ++i) {
429                 const char *arg = argv[i];
430                 if(arg[0] == '-') {
431                         /* an option */
432                         const char *option = &arg[1];
433                         if(option[0] == 'o') {
434                                 GET_ARG_AFTER(outname, "-o");
435                         } else if(option[0] == 'g') {
436                                 firm_be_option("stabs=yes");
437                                 firm_be_option("omitfp=no");
438                                 firm_be_option("ia32-nooptcc=yes");
439                         } else if(SINGLE_OPTION('c')) {
440                                 mode = CompileAssemble;
441                         } else if(SINGLE_OPTION('S')) {
442                                 mode = Compile;
443                         } else if(option[0] == 'O') {
444                                 continue;
445                         } else if(option[0] == 'I') {
446                                 const char *opt;
447                                 GET_ARG_AFTER(opt, "-I");
448                                 obstack_printf(&cppflags_obst, " -I%s", opt);
449                         } else if(option[0] == 'D') {
450                                 const char *opt;
451                                 GET_ARG_AFTER(opt, "-D");
452                                 obstack_printf(&cppflags_obst, " -D%s", opt);
453                         } else if(option[0] == 'U') {
454                                 const char *opt;
455                                 GET_ARG_AFTER(opt, "-U");
456                                 obstack_printf(&cppflags_obst, " -U%s", opt);
457                         } else if(option[0] == 'l') {
458                                 const char *opt;
459                                 GET_ARG_AFTER(opt, "-l");
460                                 obstack_printf(&ldflags_obst, " -l%s", opt);
461                         } else if(option[0] == 'L') {
462                                 const char *opt;
463                                 GET_ARG_AFTER(opt, "-L");
464                                 obstack_printf(&ldflags_obst, " -L%s", opt);
465                         } else if(SINGLE_OPTION('v')) {
466                                 verbose = 1;
467                         } else if(SINGLE_OPTION('w')) {
468                                 inhibit_all_warnings = true;
469                         } else if(option[0] == 'f') {
470                                 const char *opt;
471                                 GET_ARG_AFTER(opt, "-f");
472
473                                 if(strcmp(opt, "syntax-only") == 0) {
474                                         mode = ParseOnly;
475                                 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
476                                         firm_be_option("omitfp");
477                                 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
478                                         firm_be_option("omitfp=no");
479                                 } else {
480                                         int res = firm_option(opt);
481                                         if (res == 0) {
482                                                 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
483                                                         opt);
484                                                 argument_errors = true;
485                                                 continue;
486                                         } else if (res == -1) {
487                                                 help_displayed = true;
488                                         }
489                                 }
490                         } else if(option[0] == 'b') {
491                                 const char *opt;
492                                 GET_ARG_AFTER(opt, "-b");
493                                 int res = firm_be_option(opt);
494                                 if (res == 0) {
495                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
496                                                 opt);
497                                         argument_errors = true;
498                                 } else if (res == -1) {
499                                         help_displayed = true;
500                                 } else {
501                                         if (strncmp(opt, "isa=", 4) == 0)
502                                                 strncpy(cpu_arch, opt, sizeof(cpu_arch));
503                                 }
504                         } else if(option[0] == 'W') {
505                                 set_warning_opt(&option[1]);
506                         } else if(option[0] == 'm') {
507                                 const char *opt;
508                                 char arch_opt[64];
509
510                                 GET_ARG_AFTER(opt, "-m");
511                                 if(strncmp(opt, "arch=", 5) == 0) {
512                                         GET_ARG_AFTER(opt, "-march=");
513                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
514                                         int res = firm_be_option(arch_opt);
515                                         if (res == 0)
516                                                 argument_errors = true;
517                                         else {
518                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
519                                                 int res = firm_be_option(arch_opt);
520                                                 if (res == 0)
521                                                         argument_errors = true;
522                                         }
523                                 } else if(strncmp(opt, "tune=", 5) == 0) {
524                                         GET_ARG_AFTER(opt, "-mtune=");
525                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
526                                         int res = firm_be_option(arch_opt);
527                                         if (res == 0)
528                                                 argument_errors = true;
529                                 } else if(strncmp(opt, "cpu=", 4) == 0) {
530                                         GET_ARG_AFTER(opt, "-mcpu=");
531                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
532                                         int res = firm_be_option(arch_opt);
533                                         if (res == 0)
534                                                 argument_errors = true;
535                                 } else if(strncmp(opt, "fpu=", 4) == 0) {
536                                         GET_ARG_AFTER(opt, "-mfpu=");
537                                         snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
538                                         int res = firm_be_option(arch_opt);
539                                         if (res == 0)
540                                                 argument_errors = true;
541                                 } else {
542                                         char *endptr;
543                                         long int value = strtol(opt, &endptr, 10);
544                                         if (*endptr != '\0') {
545                                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
546                                                 argument_errors = true;
547                                         }
548                                         if (value != 16 && value != 32 && value != 64) {
549                                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
550                                                 argument_errors = true;
551                                         } else {
552                                                 machine_size = (unsigned int)value;
553                                         }
554                                 }
555                         } else if (option[0] == '\0') {
556                                 if(input != NULL) {
557                                         fprintf(stderr, "error: multiple input files specified\n");
558                                         argument_errors = true;
559                                 } else {
560                                         input = arg;
561                                 }
562                         } else if(strcmp(option, "pg") == 0) {
563                                 firm_be_option("-b gprof");
564                                 obstack_printf(&ldflags_obst, " -pg");
565                         } else if(strcmp(option, "pedantic") == 0) {
566                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
567                         } else if(strncmp(option, "std=", 4) == 0) {
568                                 if(strcmp(&option[4], "c99") == 0) {
569                                         c_mode = _C89|_C99;
570                                 } else if(strcmp(&option[4], "c89") == 0) {
571                                         c_mode = _C89;
572                                 } else if(strcmp(&option[4], "gnu99") == 0) {
573                                         c_mode = _C89|_C99|_GNUC;
574                                 } else if(strcmp(&option[4], "microsoft") == 0) {
575                                         c_mode = _C89|_C99|_MS;
576                                 } else
577                                         fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
578                         } else if (option[0] == '-') {
579                                 /* double dash option */
580                                 ++option;
581                                 if(strcmp(option, "gcc") == 0) {
582                                         c_mode |= _GNUC;
583                                 } else if(strcmp(option, "no-gcc") == 0) {
584                                         c_mode &= ~_GNUC;
585                                 } else if(strcmp(option, "ms") == 0) {
586                                         c_mode |= _MS;
587                                 } else if(strcmp(option, "no-ms") == 0) {
588                                         c_mode &= ~_MS;
589                                 } else if(strcmp(option, "signed-chars") == 0) {
590                                         char_is_signed = true;
591                                 } else if(strcmp(option, "unsigned-chars") == 0) {
592                                         char_is_signed = false;
593                                 } else if(strcmp(option, "strict") == 0) {
594                                         strict_mode = true;
595                                 } else if(strcmp(option, "lextest") == 0) {
596                                         mode = LexTest;
597                                 } else if(strcmp(option, "benchmark") == 0) {
598                                         mode = BenchmarkParser;
599                                 } else if(strcmp(option, "print-ast") == 0) {
600                                         mode = PrintAst;
601                                 } else if(strcmp(option, "print-implicit-cast") == 0) {
602                                         print_implicit_casts = true;
603                                 } else if(strcmp(option, "print-parenthesis") == 0) {
604                                         print_parenthesis = true;
605                                 } else if(strcmp(option, "print-fluffy") == 0) {
606                                         mode = PrintFluffy;
607                                 } else if(strcmp(option, "version") == 0) {
608                                         firm_version_t ver;
609                                         firm_get_version(&ver);
610                                         printf("cparser (%s) using libFirm (%u.%u",
611                                                cparser_REVISION, ver.major, ver.minor);
612                                         if(ver.revision[0] != 0) {
613                                                 putchar(' ');
614                                                 fputs(ver.revision, stdout);
615                                         }
616                                         if(ver.build[0] != 0) {
617                                                 putchar(' ');
618                                                 fputs(ver.build, stdout);
619                                         }
620                                         puts(")\n");
621                                         exit(EXIT_SUCCESS);
622                                 } else if(strcmp(option, "dump-function") == 0) {
623                                         ++i;
624                                         if(i >= argc) {
625                                                 fprintf(stderr, "error: "
626                                                         "expected argument after '--dump-function'\n");
627                                                 argument_errors = true;
628                                                 break;
629                                         }
630                                         dumpfunction = argv[i];
631                                         mode         = CompileDump;
632                                 } else {
633                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
634                                         argument_errors = true;
635                                 }
636                         } else {
637                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
638                                 argument_errors = true;
639                         }
640                 } else {
641                         if(input != NULL) {
642                                 fprintf(stderr, "error: multiple input files specified\n");
643                                 argument_errors = true;
644                         } else {
645                                 input = arg;
646                         }
647                 }
648         }
649
650         /* we do the lowering in ast2firm */
651         firm_opt.lower_bitfields = FALSE;
652
653         if(help_displayed) {
654                 return !argument_errors;
655         }
656         if(argument_errors) {
657                 usage(argv[0]);
658                 return 1;
659         }
660
661         gen_firm_init();
662         init_symbol_table();
663         init_tokens();
664         init_types();
665         init_typehash();
666         init_basic_types();
667         init_lexer();
668         init_ast();
669         init_parser();
670         init_ast2firm();
671
672         FILE *out = NULL;
673         char  outnamebuf[4096];
674         if(outname == NULL) {
675                 switch(mode) {
676                 case BenchmarkParser:
677                 case PrintAst:
678                 case PrintFluffy:
679                 case LexTest:
680                         if(outname == NULL)
681                                 outname = "-";
682                         break;
683                 case ParseOnly:
684                         break;
685                 case Compile:
686                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
687                         outname = outnamebuf;
688                         break;
689                 case CompileAssemble:
690                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
691                         outname = outnamebuf;
692                         break;
693                 case CompileDump:
694                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
695                                         ".vcg");
696                         outname = outnamebuf;
697                         break;
698                 case CompileAssembleLink:
699 #ifdef _WIN32
700                         /* Windows compiler typically derive the output name from
701                            the first source file */
702                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".exe");
703                         outname = outnamebuf;
704 #else
705                         outname = "a.out";
706 #endif
707                         break;
708                 }
709         }
710
711         if(outname != NULL) {
712                 if(strcmp(outname, "-") == 0) {
713                         out = stdout;
714                 } else {
715                         out = fopen(outname, "w");
716                         if(out == NULL) {
717                                 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
718                                         strerror(errno));
719                                 return 1;
720                         }
721                 }
722         }
723
724         FILE *in;
725         if(input == NULL) {
726                 fprintf(stderr, "%s: no input files\n", argv[0]);
727                 return 1;
728         } else if(strcmp(input, "-") == 0) {
729                 in    = stdin;
730                 input = "<stdin>";
731         } else {
732                 in = fopen(input, "r");
733                 if(in == NULL) {
734                         fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
735                         return 1;
736                 }
737         }
738
739         if(mode == LexTest) {
740                 lextest(in, input);
741                 fclose(in);
742                 return 0;
743         }
744
745         FILE *preprocessed_in = preprocess(in, input);
746         translation_unit_t *const unit = do_parsing(preprocessed_in, input);
747         int res = pclose(preprocessed_in);
748         if(res != 0) {
749                 return res;
750         }
751
752         if(error_count > 0) {
753                 /* parsing failed because of errors */
754                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
755                 result = EXIT_FAILURE;
756         } else if(warning_count > 0) {
757                 fprintf(stderr, "%u warning(s)\n", warning_count);
758         }
759
760         if(mode == BenchmarkParser) {
761                 return result;
762         }
763
764         /* prints the AST even if errors occurred */
765         if(mode == PrintAst) {
766                 type_set_output(out);
767                 ast_set_output(out);
768                 print_ast(unit);
769                 return result;
770         }
771
772         /* cannot handle other modes with errors */
773         if(result != EXIT_SUCCESS)
774                 return result;
775
776         if(mode == PrintFluffy) {
777                 type_set_output(out);
778                 ast_set_output(out);
779                 write_fluffy_decls(out, unit);
780         }
781
782         translation_unit_to_firm(unit);
783
784         if(mode == ParseOnly) {
785                 return 0;
786         }
787
788         FILE *asm_out;
789         char  asm_tempfile[1024];
790         if(mode == CompileDump) {
791                 asm_out = NULL;
792                 firm_be_opt.selection = BE_NONE;
793         } else if(mode == Compile) {
794                 asm_out = out;
795         } else {
796                 asm_out
797                         = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
798         }
799         gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
800
801         if(mode == CompileDump) {
802                 /* find irg */
803                 ident    *id     = new_id_from_str(dumpfunction);
804                 ir_graph *irg    = NULL;
805                 int       n_irgs = get_irp_n_irgs();
806                 for(int i = 0; i < n_irgs; ++i) {
807                         ir_graph *tirg   = get_irp_irg(i);
808                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
809                         if(irg_id == id) {
810                                 irg = tirg;
811                                 break;
812                         }
813                 }
814
815                 if(irg == NULL) {
816                         fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
817                         return 1;
818                 }
819
820                 dump_ir_block_graph_file(irg, out);
821                 fclose(out);
822                 return 0;
823         }
824
825         fclose(asm_out);
826
827         /* assemble assembler and create object file */
828         char obj_tfile[1024];
829         if(mode == CompileAssemble || mode == CompileAssembleLink) {
830                 const char *obj_outfile;
831                 if(mode == CompileAssemble) {
832                         fclose(out);
833                         obj_outfile = outname;
834                 } else {
835                         FILE *tempf
836                                 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
837                         fclose(tempf);
838                         obj_outfile = obj_tfile;
839                 }
840
841                 assemble(obj_outfile, asm_tempfile);
842         }
843
844         /* link object file */
845         if(mode == CompileAssembleLink) {
846                 do_link(outname, obj_tfile);
847         }
848
849         obstack_free(&cppflags_obst, NULL);
850         obstack_free(&ldflags_obst, NULL);
851
852         exit_ast2firm();
853         exit_parser();
854         exit_ast();
855         exit_lexer();
856         exit_typehash();
857         exit_types();
858         exit_tokens();
859         exit_symbol_table();
860         return 0;
861 }