add -pg support, globals removed
[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
367         obstack_init(&cppflags_obst);
368         obstack_init(&ldflags_obst);
369
370 #define GET_ARG_AFTER(def, args)                                             \
371         def = &arg[sizeof(args)-1];                                              \
372         if(def[0] == '\0') {                                                     \
373                 ++i;                                                                 \
374                 if(i >= argc) {                                                      \
375                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
376                         argument_errors = true;                                          \
377                         break;                                                           \
378                 }                                                                    \
379                 def = argv[i];                                                       \
380                 if(def[0] == '-' && def[1] != '\0') {                                \
381                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
382                         argument_errors = true;                                          \
383                         continue;                                                        \
384                 }                                                                    \
385         }
386
387 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
388
389         /* early options parsing (find out optimisation level) */
390         for(int i = 1; i < argc; ++i) {
391                 const char *arg = argv[i];
392                 if(arg[0] != '-')
393                         continue;
394
395                 const char *option = &arg[1];
396                 if(option[0] == 'O') {
397                         sscanf(&option[1], "%d", &opt_level);
398                 }
399         }
400
401         /* apply optimisation level */
402         switch(opt_level) {
403         case 0:
404                 firm_option("no-opt");
405                 break;
406         case 1:
407                 firm_option("no-inline");
408                 break;
409         default:
410         case 4:
411                 firm_option("strict-aliasing");
412                 /* fallthrough */
413         case 3:
414                 firm_option("cond-eval");
415                 firm_option("if-conv");
416                 /* fallthrough */
417         case 2:
418                 firm_option("inline");
419                 firm_option("deconv");
420                 firm_be_option("omitfp");
421                 break;
422         }
423
424         /* parse rest of options */
425         bool help_displayed  = false;
426         bool argument_errors = false;
427         for(int i = 1; i < argc; ++i) {
428                 const char *arg = argv[i];
429                 if(arg[0] == '-') {
430                         /* an option */
431                         const char *option = &arg[1];
432                         if(option[0] == 'o') {
433                                 GET_ARG_AFTER(outname, "-o");
434                         } else if(option[0] == 'g') {
435                                 firm_be_option("stabs=yes");
436                                 firm_be_option("omitfp=no");
437                                 firm_be_option("ia32-nooptcc=yes");
438                         } else if(SINGLE_OPTION('c')) {
439                                 mode = CompileAssemble;
440                         } else if(SINGLE_OPTION('S')) {
441                                 mode = Compile;
442                         } else if(option[0] == 'O') {
443                                 continue;
444                         } else if(option[0] == 'I') {
445                                 const char *opt;
446                                 GET_ARG_AFTER(opt, "-I");
447                                 obstack_printf(&cppflags_obst, " -I%s", opt);
448                         } else if(option[0] == 'D') {
449                                 const char *opt;
450                                 GET_ARG_AFTER(opt, "-D");
451                                 obstack_printf(&cppflags_obst, " -D%s", opt);
452                         } else if(option[0] == 'U') {
453                                 const char *opt;
454                                 GET_ARG_AFTER(opt, "-U");
455                                 obstack_printf(&cppflags_obst, " -U%s", opt);
456                         } else if(option[0] == 'l') {
457                                 const char *opt;
458                                 GET_ARG_AFTER(opt, "-l");
459                                 obstack_printf(&ldflags_obst, " -l%s", opt);
460                         } else if(option[0] == 'L') {
461                                 const char *opt;
462                                 GET_ARG_AFTER(opt, "-L");
463                                 obstack_printf(&ldflags_obst, " -L%s", opt);
464                         } else if(SINGLE_OPTION('v')) {
465                                 verbose = 1;
466                         } else if(SINGLE_OPTION('w')) {
467                                 inhibit_all_warnings = true;
468                         } else if(option[0] == 'f') {
469                                 const char *opt;
470                                 GET_ARG_AFTER(opt, "-f");
471
472                                 if(strcmp(opt, "syntax-only") == 0) {
473                                         mode = ParseOnly;
474                                 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
475                                         firm_be_option("omitfp");
476                                 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
477                                         firm_be_option("omitfp=no");
478                                 } else {
479                                         int res = firm_option(opt);
480                                         if (res == 0) {
481                                                 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
482                                                         opt);
483                                                 argument_errors = true;
484                                                 continue;
485                                         } else if (res == -1) {
486                                                 help_displayed = true;
487                                         }
488                                 }
489                         } else if(option[0] == 'b') {
490                                 const char *opt;
491                                 GET_ARG_AFTER(opt, "-b");
492                                 int res = firm_be_option(opt);
493                                 if (res == 0) {
494                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
495                                                 opt);
496                                         argument_errors = true;
497                                 } else if (res == -1) {
498                                         help_displayed = true;
499                                 }
500                         } else if(option[0] == 'W') {
501                                 set_warning_opt(&option[1]);
502                         } else if(option[0] == 'm') {
503                                 const char *opt;
504                                 GET_ARG_AFTER(opt, "-m");
505                                 char *endptr;
506                                 long int value = strtol(opt, &endptr, 10);
507                                 if (*endptr != '\0') {
508                                         fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
509                                         argument_errors = true;
510                                 }
511                                 if (value != 16 && value != 32 && value != 64) {
512                                         fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
513                                         argument_errors = true;
514                                 } else {
515                                         machine_size = (unsigned int)value;
516                                 }
517                         } else if (option[0] == '\0') {
518                                 if(input != NULL) {
519                                         fprintf(stderr, "error: multiple input files specified\n");
520                                         argument_errors = true;
521                                 } else {
522                                         input = arg;
523                                 }
524                         } else if(strcmp(option, "pg") == 0) {
525                                 firm_be_option("-b gprof");
526                                 obstack_printf(&ldflags_obst, " -pg");
527                         } else if(strcmp(option, "pedantic") == 0) {
528                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
529                         } else if(strncmp(option, "std=", 4) == 0) {
530                                 if(strcmp(&option[4], "c99") == 0) {
531                                         c_mode = _C89|_C99;
532                                 } else if(strcmp(&option[4], "c89") == 0) {
533                                         c_mode = _C89;
534                                 } else if(strcmp(&option[4], "gnu99") == 0) {
535                                         c_mode = _C89|_C99|_GNUC;
536                                 } else if(strcmp(&option[4], "microsoft") == 0) {
537                                         c_mode = _C89|_C99|_MS;
538                                 } else
539                                         fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
540                         } else if (option[0] == '-') {
541                                 /* double dash option */
542                                 ++option;
543                                 if(strcmp(option, "gcc") == 0) {
544                                         c_mode |= _GNUC;
545                                 } else if(strcmp(option, "no-gcc") == 0) {
546                                         c_mode &= ~_GNUC;
547                                 } else if(strcmp(option, "ms") == 0) {
548                                         c_mode |= _MS;
549                                 } else if(strcmp(option, "no-ms") == 0) {
550                                         c_mode &= ~_MS;
551                                 } else if(strcmp(option, "signed-chars") == 0) {
552                                         char_is_signed = true;
553                                 } else if(strcmp(option, "unsigned-chars") == 0) {
554                                         char_is_signed = false;
555                                 } else if(strcmp(option, "strict") == 0) {
556                                         strict_mode = true;
557                                 } else if(strcmp(option, "lextest") == 0) {
558                                         mode = LexTest;
559                                 } else if(strcmp(option, "benchmark") == 0) {
560                                         mode = BenchmarkParser;
561                                 } else if(strcmp(option, "print-ast") == 0) {
562                                         mode = PrintAst;
563                                 } else if(strcmp(option, "print-implicit-cast") == 0) {
564                                         print_implicit_casts = true;
565                                 } else if(strcmp(option, "print-parenthesis") == 0) {
566                                         print_parenthesis = true;
567                                 } else if(strcmp(option, "print-fluffy") == 0) {
568                                         mode = PrintFluffy;
569                                 } else if(strcmp(option, "version") == 0) {
570                                         firm_version_t ver;
571                                         firm_get_version(&ver);
572                                         printf("cparser (%s) using libFirm (%u.%u",
573                                                cparser_REVISION, ver.major, ver.minor);
574                                         if(ver.revision[0] != 0) {
575                                                 putchar(' ');
576                                                 fputs(ver.revision, stdout);
577                                         }
578                                         if(ver.build[0] != 0) {
579                                                 putchar(' ');
580                                                 fputs(ver.build, stdout);
581                                         }
582                                         puts(")\n");
583                                         exit(EXIT_SUCCESS);
584                                 } else if(strcmp(option, "dump-function") == 0) {
585                                         ++i;
586                                         if(i >= argc) {
587                                                 fprintf(stderr, "error: "
588                                                         "expected argument after '--dump-function'\n");
589                                                 argument_errors = true;
590                                                 break;
591                                         }
592                                         dumpfunction = argv[i];
593                                         mode         = CompileDump;
594                                 } else {
595                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
596                                         argument_errors = true;
597                                 }
598                         } else {
599                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
600                                 argument_errors = true;
601                         }
602                 } else {
603                         if(input != NULL) {
604                                 fprintf(stderr, "error: multiple input files specified\n");
605                                 argument_errors = true;
606                         } else {
607                                 input = arg;
608                         }
609                 }
610         }
611
612         /* we do the lowering in ast2firm */
613         firm_opt.lower_bitfields = FALSE;
614
615         if(help_displayed) {
616                 return !argument_errors;
617         }
618         if(argument_errors) {
619                 usage(argv[0]);
620                 return 1;
621         }
622
623         gen_firm_init();
624         init_symbol_table();
625         init_tokens();
626         init_types();
627         init_typehash();
628         init_basic_types();
629         init_lexer();
630         init_ast();
631         init_parser();
632         init_ast2firm();
633
634         FILE *out = NULL;
635         char  outnamebuf[4096];
636         if(outname == NULL) {
637                 switch(mode) {
638                 case BenchmarkParser:
639                 case PrintAst:
640                 case PrintFluffy:
641                 case LexTest:
642                         if(outname == NULL)
643                                 outname = "-";
644                         break;
645                 case ParseOnly:
646                         break;
647                 case Compile:
648                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
649                         outname = outnamebuf;
650                         break;
651                 case CompileAssemble:
652                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
653                         outname = outnamebuf;
654                         break;
655                 case CompileDump:
656                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
657                                         ".vcg");
658                         outname = outnamebuf;
659                         break;
660                 case CompileAssembleLink:
661 #ifdef _WIN32
662                         /* Windows compiler typically derive the output name from
663                            the first source file */
664                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".exe");
665                         outname = outnamebuf;
666 #else
667                         outname = "a.out";
668 #endif
669                         break;
670                 }
671         }
672
673         if(outname != NULL) {
674                 if(strcmp(outname, "-") == 0) {
675                         out = stdout;
676                 } else {
677                         out = fopen(outname, "w");
678                         if(out == NULL) {
679                                 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
680                                         strerror(errno));
681                                 return 1;
682                         }
683                 }
684         }
685
686         FILE *in;
687         if(input == NULL) {
688                 fprintf(stderr, "%s: no input files\n", argv[0]);
689                 return 1;
690         } else if(strcmp(input, "-") == 0) {
691                 in    = stdin;
692                 input = "<stdin>";
693         } else {
694                 in = fopen(input, "r");
695                 if(in == NULL) {
696                         fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
697                         return 1;
698                 }
699         }
700
701         if(mode == LexTest) {
702                 lextest(in, input);
703                 fclose(in);
704                 return 0;
705         }
706
707         FILE *preprocessed_in = preprocess(in, input);
708         translation_unit_t *const unit = do_parsing(preprocessed_in, input);
709         int res = pclose(preprocessed_in);
710         if(res != 0) {
711                 return res;
712         }
713
714         if(error_count > 0) {
715                 /* parsing failed because of errors */
716                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
717                 result = EXIT_FAILURE;
718         } else if(warning_count > 0) {
719                 fprintf(stderr, "%u warning(s)\n", warning_count);
720         }
721
722         if(mode == BenchmarkParser) {
723                 return result;
724         }
725
726         /* prints the AST even if errors occurred */
727         if(mode == PrintAst) {
728                 type_set_output(out);
729                 ast_set_output(out);
730                 print_ast(unit);
731                 return result;
732         }
733
734         /* cannot handle other modes with errors */
735         if(result != EXIT_SUCCESS)
736                 return result;
737
738         if(mode == PrintFluffy) {
739                 type_set_output(out);
740                 ast_set_output(out);
741                 write_fluffy_decls(out, unit);
742         }
743
744         translation_unit_to_firm(unit);
745
746         if(mode == ParseOnly) {
747                 return 0;
748         }
749
750         FILE *asm_out;
751         char  asm_tempfile[1024];
752         if(mode == CompileDump) {
753                 asm_out = NULL;
754                 firm_be_opt.selection = BE_NONE;
755         } else if(mode == Compile) {
756                 asm_out = out;
757         } else {
758                 asm_out
759                         = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
760         }
761         gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
762
763         if(mode == CompileDump) {
764                 /* find irg */
765                 ident    *id     = new_id_from_str(dumpfunction);
766                 ir_graph *irg    = NULL;
767                 int       n_irgs = get_irp_n_irgs();
768                 for(int i = 0; i < n_irgs; ++i) {
769                         ir_graph *tirg   = get_irp_irg(i);
770                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
771                         if(irg_id == id) {
772                                 irg = tirg;
773                                 break;
774                         }
775                 }
776
777                 if(irg == NULL) {
778                         fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
779                         return 1;
780                 }
781
782                 dump_ir_block_graph_file(irg, out);
783                 fclose(out);
784                 return 0;
785         }
786
787         fclose(asm_out);
788
789         /* assemble assembler and create object file */
790         char obj_tfile[1024];
791         if(mode == CompileAssemble || mode == CompileAssembleLink) {
792                 const char *obj_outfile;
793                 if(mode == CompileAssemble) {
794                         fclose(out);
795                         obj_outfile = outname;
796                 } else {
797                         FILE *tempf
798                                 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
799                         fclose(tempf);
800                         obj_outfile = obj_tfile;
801                 }
802
803                 assemble(obj_outfile, asm_tempfile);
804         }
805
806         /* link object file */
807         if(mode == CompileAssembleLink) {
808                 do_link(outname, obj_tfile);
809         }
810
811         obstack_free(&cppflags_obst, NULL);
812         obstack_free(&ldflags_obst, NULL);
813
814         exit_ast2firm();
815         exit_parser();
816         exit_ast();
817         exit_lexer();
818         exit_typehash();
819         exit_types();
820         exit_tokens();
821         exit_symbol_table();
822         return 0;
823 }