adapt for latest firm changes
[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 #define unlink(filename)         _unlink(filename)
54
55 #else
56 #include <unistd.h>
57 #define HAVE_MKSTEMP
58 #endif
59
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
62
63 #include "lexer.h"
64 #include "token_t.h"
65 #include "types.h"
66 #include "type_hash.h"
67 #include "parser.h"
68 #include "type_t.h"
69 #include "ast2firm.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "adt/error.h"
75 #include "write_fluffy.h"
76 #include "write_caml.h"
77 #include "revision.h"
78 #include "warning.h"
79 #include "mangle.h"
80
81 #ifndef PREPROCESSOR
82 #ifndef __WIN32__
83 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
84 #else
85 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
86 #endif
87 #endif
88
89 #ifndef LINKER
90 #define LINKER    "gcc -m32"
91 #endif
92
93 #ifndef ASSEMBLER
94 #ifdef __APPLE__
95 #define ASSEMBLER "gcc -c -xassembler"
96 #else
97 #define ASSEMBLER "as --32"
98 #endif
99 #endif
100
101 /** The current c mode/dialect. */
102 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
103
104 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
105 unsigned int machine_size = 32;
106
107 /** true if the char type is signed. */
108 bool char_is_signed = true;
109
110 /** true for strict language checking. */
111 bool strict_mode = false;
112
113 /** use builtins for some libc functions */
114 bool use_builtins = false;
115
116 /** we have extern function with const attribute. */
117 bool have_const_functions = false;
118
119 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
120
121 /* to switch on printing of implicit casts */
122 extern bool print_implicit_casts;
123
124 /* to switch on printing of parenthesis to indicate operator precedence */
125 extern bool print_parenthesis;
126
127 static int             verbose;
128 static struct obstack  cppflags_obst, ldflags_obst;
129 static char            dep_target[1024];
130 static const char     *outname;
131
132 typedef struct file_list_entry_t file_list_entry_t;
133
134 typedef enum filetype_t {
135         FILETYPE_AUTODETECT,
136         FILETYPE_C,
137         FILETYPE_PREPROCESSED_C,
138         FILETYPE_CXX,
139         FILETYPE_PREPROCESSED_CXX,
140         FILETYPE_ASSEMBLER,
141         FILETYPE_PREPROCESSED_ASSEMBLER,
142         FILETYPE_OBJECT,
143         FILETYPE_UNKNOWN
144 } filetype_t;
145
146 struct file_list_entry_t {
147         const char  *name; /**< filename or NULL for stdin */
148         filetype_t   type;
149         file_list_entry_t *next;
150 };
151
152 static file_list_entry_t *temp_files;
153
154 #if defined(_DEBUG) || defined(FIRM_DEBUG)
155 /**
156  * Debug printf implementation.
157  *
158  * @param fmt  printf style format parameter
159  */
160 void dbg_printf(const char *fmt, ...)
161 {
162         va_list list;
163
164         if (firm_dump.debug_print) {
165                 va_start(list, fmt);
166                 vprintf(fmt, list);
167                 va_end(list);
168         }  /* if */
169 }
170 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
171
172 static void initialize_firm(void)
173 {
174         firm_early_init();
175
176         dump_consts_local(1);
177         dump_keepalive_edges(1);
178 }
179
180 static void get_output_name(char *buf, size_t buflen, const char *inputname,
181                             const char *newext)
182 {
183         size_t last_dot = 0xffffffff;
184         size_t i = 0;
185
186         if (inputname == NULL) {
187                 snprintf(buf, buflen, "a%s", newext);
188                 return;
189         }
190
191         for (const char *c = inputname; *c != 0; ++c) {
192                 if (*c == '.')
193                         last_dot = i;
194                 ++i;
195         }
196         if (last_dot == 0xffffffff)
197                 last_dot = i;
198
199         if (last_dot >= buflen)
200                 panic("filename too long");
201         memcpy(buf, inputname, last_dot);
202
203         size_t extlen = strlen(newext) + 1;
204         if (extlen + last_dot >= buflen)
205                 panic("filename too long");
206         memcpy(buf+last_dot, newext, extlen);
207 }
208
209 #include "builtins.h"
210
211 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
212 {
213         start_parsing();
214
215         if (use_builtins) {
216                 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
217                 parse();
218         }
219
220         lexer_open_stream(in, input_name);
221         parse();
222
223         translation_unit_t *unit = finish_parsing();
224         return unit;
225 }
226
227 static void lextest(FILE *in, const char *fname)
228 {
229         lexer_open_stream(in, fname);
230
231         do {
232                 lexer_next_preprocessing_token();
233                 print_token(stdout, &lexer_token);
234                 putchar('\n');
235         } while (lexer_token.type != T_EOF);
236 }
237
238 static void add_flag(struct obstack *obst, const char *format, ...)
239 {
240         char buf[4096];
241         va_list ap;
242
243         va_start(ap, format);
244 #ifdef _WIN32
245         int len =
246 #endif
247                 vsnprintf(buf, sizeof(buf), format, ap);
248         va_end(ap);
249
250         obstack_1grow(obst, ' ');
251 #ifdef _WIN32
252         obstack_1grow(obst, '"');
253         obstack_grow(obst, buf, len);
254         obstack_1grow(obst, '"');
255 #else
256         /* escape stuff... */
257         for (char *c = buf; *c != '\0'; ++c) {
258                 switch(*c) {
259                 case ' ':
260                 case '"':
261                 case '$':
262                 case '&':
263                 case '(':
264                 case ')':
265                 case ';':
266                 case '<':
267                 case '>':
268                 case '\'':
269                 case '\\':
270                 case '\n':
271                 case '\r':
272                 case '\t':
273                 case '`':
274                 case '|':
275                         obstack_1grow(obst, '\\');
276                         /* FALLTHROUGH */
277                 default:
278                         obstack_1grow(obst, *c);
279                         break;
280                 }
281         }
282 #endif
283 }
284
285 static const char *type_to_string(type_t *type)
286 {
287         assert(type->kind == TYPE_ATOMIC);
288         return get_atomic_kind_name(type->atomic.akind);
289 }
290
291 static FILE *preprocess(const char *fname)
292 {
293         obstack_1grow(&cppflags_obst, '\0');
294         const char *flags = obstack_finish(&cppflags_obst);
295
296         obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
297
298         /* setup default defines */
299         add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
300         add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
301         add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
302         add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
303
304         /* TODO hack... */
305         add_flag(&cppflags_obst, "-D__builtin_abort=abort");
306         add_flag(&cppflags_obst, "-D__builtin_abs=abs");
307         add_flag(&cppflags_obst, "-D__builtin_exit=exit");
308         add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
309         add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
310         add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
311         add_flag(&cppflags_obst, "-D__builtin_memset=memset");
312         add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
313         add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
314         add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
315
316         /* handle dependency generation */
317         if (dep_target[0] != '\0') {
318                 add_flag(&cppflags_obst, "-MF");
319                 add_flag(&cppflags_obst, dep_target);
320                 if (outname != NULL) {
321                                 add_flag(&cppflags_obst, "-MQ");
322                                 add_flag(&cppflags_obst, outname);
323                 }
324         }
325         if (flags[0] != '\0') {
326                 obstack_printf(&cppflags_obst, " %s", flags);
327         }
328         add_flag(&cppflags_obst, fname);
329
330         obstack_1grow(&cppflags_obst, '\0');
331         const char *buf = obstack_finish(&cppflags_obst);
332         if (verbose) {
333                 puts(buf);
334         }
335
336         FILE *f = popen(buf, "r");
337         if (f == NULL) {
338                 fprintf(stderr, "invoking preprocessor failed\n");
339                 exit(1);
340         }
341
342         return f;
343 }
344
345 static void assemble(const char *out, const char *in)
346 {
347         char buf[4096];
348
349         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
350         if (verbose) {
351                 puts(buf);
352         }
353
354         int err = system(buf);
355         if (err != 0) {
356                 fprintf(stderr, "assembler reported an error\n");
357                 exit(1);
358         }
359 }
360
361 static void print_file_name(const char *file)
362 {
363         add_flag(&ldflags_obst, "-print-file-name=%s", file);
364
365         obstack_1grow(&ldflags_obst, '\0');
366         const char *flags = obstack_finish(&ldflags_obst);
367
368         /* construct commandline */
369         obstack_printf(&ldflags_obst, "%s ", LINKER);
370         obstack_printf(&ldflags_obst, "%s", flags);
371         obstack_1grow(&ldflags_obst, '\0');
372
373         char *commandline = obstack_finish(&ldflags_obst);
374
375         if (verbose) {
376                 puts(commandline);
377         }
378         int err = system(commandline);
379         if (err != EXIT_SUCCESS) {
380                 fprintf(stderr, "linker reported an error\n");
381                 exit(1);
382         }
383 }
384
385 static const char *try_dir(const char *dir)
386 {
387         if (dir == NULL)
388                 return dir;
389         if (access(dir, R_OK | W_OK | X_OK) == 0)
390                 return dir;
391         return NULL;
392 }
393
394 static const char *get_tempdir(void)
395 {
396         static const char *tmpdir = NULL;
397
398         if (tmpdir != NULL)
399                 return tmpdir;
400
401         if (tmpdir == NULL)
402                 tmpdir = try_dir(getenv("TMPDIR"));
403         if (tmpdir == NULL)
404                 tmpdir = try_dir(getenv("TMP"));
405         if (tmpdir == NULL)
406                 tmpdir = try_dir(getenv("TEMP"));
407
408 #ifdef P_tmpdir
409         if (tmpdir == NULL)
410                 tmpdir = try_dir(P_tmpdir);
411 #endif
412
413         if (tmpdir == NULL)
414                 tmpdir = try_dir("/var/tmp");
415         if (tmpdir == NULL)
416                 tmpdir = try_dir("/usr/tmp");
417         if (tmpdir == NULL)
418                 tmpdir = try_dir("/tmp");
419
420         if (tmpdir == NULL)
421                 tmpdir = ".";
422
423         return tmpdir;
424 }
425
426 #ifndef HAVE_MKSTEMP
427 /* cheap and nasty mkstemp replacement */
428 static int mkstemp(char *templ)
429 {
430         mktemp(templ);
431         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
432 }
433 #endif
434
435 /**
436  * an own version of tmpnam, which: writes in a buffer, emits no warnings
437  * during linking (like glibc/gnu ld do for tmpnam)...
438  */
439 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
440 {
441         const char *tempdir = get_tempdir();
442
443         snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
444
445         int fd = mkstemp(buffer);
446         if (fd == -1) {
447                 fprintf(stderr, "couldn't create temporary file: %s\n",
448                         strerror(errno));
449                 exit(1);
450         }
451         FILE *out = fdopen(fd, "w");
452         if (out == NULL) {
453                 fprintf(stderr, "couldn't create temporary file FILE*\n");
454                 exit(1);
455         }
456
457         file_list_entry_t *entry = xmalloc(sizeof(*entry));
458         memset(entry, 0, sizeof(*entry));
459
460         size_t  name_len = strlen(buffer) + 1;
461         char   *name     = malloc(name_len);
462         memcpy(name, buffer, name_len);
463         entry->name      = name;
464
465         entry->next = temp_files;
466         temp_files  = entry;
467
468         return out;
469 }
470
471 static void free_temp_files(void)
472 {
473         file_list_entry_t *entry = temp_files;
474         file_list_entry_t *next;
475         for ( ; entry != NULL; entry = next) {
476                 next = entry->next;
477
478                 unlink(entry->name);
479                 free((char*) entry->name);
480                 free(entry);
481         }
482         temp_files = NULL;
483 }
484
485 /**
486  * Do the necessary lowering for compound parameters.
487  */
488 void lower_compound_params(void)
489 {
490         lower_params_t params;
491
492         params.def_ptr_alignment    = 4;
493         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
494         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
495         params.find_pointer_type    = NULL;
496         params.ret_compound_in_regs = NULL;
497         lower_calls_with_compounds(&params);
498 }
499
500 typedef enum compile_mode_t {
501         BenchmarkParser,
502         PreprocessOnly,
503         ParseOnly,
504         Compile,
505         CompileDump,
506         CompileAssemble,
507         CompileAssembleLink,
508         LexTest,
509         PrintAst,
510         PrintFluffy,
511         PrintCaml
512 } compile_mode_t;
513
514 static void usage(const char *argv0)
515 {
516         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
517 }
518
519 static void print_cparser_version(void) {
520         printf("cparser (%s) using libFirm (%u.%u",
521                cparser_REVISION, firm_get_version_major(),
522                firm_get_version_minor());
523
524         const char *revision = firm_get_version_revision();
525         if (revision[0] != 0) {
526                 putchar(' ');
527                 fputs(revision, stdout);
528         }
529
530         const char *build = firm_get_version_build();
531         if (build[0] != 0) {
532                 putchar(' ');
533                 fputs(build, stdout);
534         }
535         puts(")\n");
536 }
537
538 static void set_be_option(const char *arg)
539 {
540         int res = firm_be_option(arg);
541         (void) res;
542         assert(res);
543 }
544
545 static void set_option(const char *arg)
546 {
547         int res = firm_option(arg);
548         (void) res;
549         assert(res);
550 }
551
552 static void copy_file(FILE *dest, FILE *input)
553 {
554         char buf[16384];
555
556         while (!feof(input) && !ferror(dest)) {
557                 size_t read = fread(buf, 1, sizeof(buf), input);
558                 if (fwrite(buf, 1, read, dest) != read) {
559                         perror("couldn't write output");
560                 }
561         }
562 }
563
564 static inline bool streq(char const* a, char const* b)
565 {
566         return strcmp(a, b) == 0;
567 }
568
569 static inline bool strstart(char const* str, char const* start)
570 {
571         do {
572                 if (*start == '\0')
573                         return true;
574         } while (*str++ == *start++);
575         return false;
576 }
577
578 static FILE *open_file(const char *filename)
579 {
580         if (streq(filename, "-")) {
581                 return stdin;
582         }
583
584         FILE *in = fopen(filename, "r");
585         if (in == NULL) {
586                 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
587                                 strerror(errno));
588                 exit(1);
589         }
590
591         return in;
592 }
593
594 static filetype_t get_filetype_from_string(const char *string)
595 {
596         if (streq(string, "c") || streq(string, "c-header"))
597                 return FILETYPE_C;
598         if (streq(string, "c++") || streq(string, "c++-header"))
599                 return FILETYPE_CXX;
600         if (streq(string, "assembler"))
601                 return FILETYPE_PREPROCESSED_ASSEMBLER;
602         if (streq(string, "assembler-with-cpp"))
603                 return FILETYPE_ASSEMBLER;
604         if (streq(string, "none"))
605                 return FILETYPE_AUTODETECT;
606
607         return FILETYPE_UNKNOWN;
608 }
609
610 static void init_os_support(void)
611 {
612         /* OS option must be set to the backend */
613         switch (firm_opt.os_support) {
614         case OS_SUPPORT_MINGW:
615                 set_be_option("ia32-gasmode=mingw");
616                 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
617                 break;
618         case OS_SUPPORT_LINUX:
619                 set_be_option("ia32-gasmode=elf");
620                 break;
621         case OS_SUPPORT_MACHO:
622                 set_be_option("ia32-gasmode=macho");
623                 set_be_option("ia32-stackalign=4");
624                 set_be_option("pic");
625                 break;
626         }
627 }
628
629 typedef enum lang_standard_t {
630         STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
631         STANDARD_ANSI,    /* c89 (for C) or c++98 (for C++) */
632         STANDARD_C89,     /* ISO C90 (sic) */
633         STANDARD_C90,     /* ISO C90 as modified in amendment 1 */
634         STANDARD_C99,     /* ISO C99 */
635         STANDARD_GNU89,   /* ISO C90 plus GNU extensions (including some C99) */
636         STANDARD_GNU99,   /* ISO C99 plus GNU extensions */
637         STANDARD_CXX98,   /* ISO C++ 1998 plus amendments */
638         STANDARD_GNUXX98  /* ISO C++ 1998 plus amendments and GNU extensions */
639 } lang_standard_t;
640
641 int main(int argc, char **argv)
642 {
643         initialize_firm();
644
645         const char        *dumpfunction         = NULL;
646         const char        *print_file_name_file = NULL;
647         compile_mode_t     mode                 = CompileAssembleLink;
648         int                opt_level            = 1;
649         int                result               = EXIT_SUCCESS;
650         char               cpu_arch[16]         = "ia32";
651         file_list_entry_t *files                = NULL;
652         file_list_entry_t *last_file            = NULL;
653         bool               construct_dep_target = false;
654         struct obstack     file_obst;
655
656         atexit(free_temp_files);
657
658         /* hack for now... */
659         if (strstr(argv[0], "pptest") != NULL) {
660                 extern int pptest_main(int argc, char **argv);
661                 return pptest_main(argc, argv);
662         }
663
664         obstack_init(&cppflags_obst);
665         obstack_init(&ldflags_obst);
666         obstack_init(&file_obst);
667
668 #define GET_ARG_AFTER(def, args)                                             \
669         def = &arg[sizeof(args)-1];                                              \
670         if (def[0] == '\0') {                                                     \
671                 ++i;                                                                 \
672                 if (i >= argc) {                                                      \
673                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
674                         argument_errors = true;                                          \
675                         break;                                                           \
676                 }                                                                    \
677                 def = argv[i];                                                       \
678                 if (def[0] == '-' && def[1] != '\0') {                                \
679                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
680                         argument_errors = true;                                          \
681                         continue;                                                        \
682                 }                                                                    \
683         }
684
685 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
686
687         /* early options parsing (find out optimisation level and OS) */
688         for (int i = 1; i < argc; ++i) {
689                 const char *arg = argv[i];
690                 if (arg[0] != '-')
691                         continue;
692
693                 const char *option = &arg[1];
694                 if (option[0] == 'O') {
695                         sscanf(&option[1], "%d", &opt_level);
696                 }
697                 if (strcmp(arg, "-fwin32") == 0) {
698                         firm_opt.os_support = OS_SUPPORT_MINGW;
699                 } else if (strcmp(arg, "-fmac") == 0) {
700                         firm_opt.os_support = OS_SUPPORT_MACHO;
701                 } else if (strcmp(arg, "-flinux") == 0) {
702                         firm_opt.os_support = OS_SUPPORT_LINUX;
703                 }
704         }
705
706         /* set target/os specific stuff */
707         init_os_support();
708
709         /* apply optimisation level */
710         switch(opt_level) {
711         case 0:
712                 set_option("no-opt");
713                 break;
714         case 1:
715                 set_option("no-inline");
716                 break;
717         default:
718         case 4:
719                 set_option("strict-aliasing");
720                 /* use_builtins = true; */
721                 /* fallthrough */
722         case 3:
723                 set_option("cond-eval");
724                 set_option("if-conv");
725                 /* fallthrough */
726         case 2:
727                 set_option("inline");
728                 set_option("deconv");
729                 set_be_option("omitfp");
730                 break;
731         }
732
733         /* parse rest of options */
734         lang_standard_t standard        = STANDARD_DEFAULT;
735         unsigned        features_on     = 0;
736         unsigned        features_off    = 0;
737         filetype_t      forced_filetype = FILETYPE_AUTODETECT;
738         bool            help_displayed  = false;
739         bool            argument_errors = false;
740         for (int i = 1; i < argc; ++i) {
741                 const char *arg = argv[i];
742                 if (arg[0] == '-' && arg[1] != '\0') {
743                         /* an option */
744                         const char *option = &arg[1];
745                         if (option[0] == 'o') {
746                                 GET_ARG_AFTER(outname, "-o");
747                         } else if (option[0] == 'g') {
748                                 set_be_option("debuginfo=stabs");
749                                 set_be_option("omitfp=no");
750                                 set_be_option("ia32-nooptcc=yes");
751                         } else if (SINGLE_OPTION('c')) {
752                                 mode = CompileAssemble;
753                         } else if (SINGLE_OPTION('E')) {
754                                 mode = PreprocessOnly;
755                         } else if (SINGLE_OPTION('S')) {
756                                 mode = Compile;
757                         } else if (option[0] == 'O') {
758                                 continue;
759                         } else if (option[0] == 'I') {
760                                 const char *opt;
761                                 GET_ARG_AFTER(opt, "-I");
762                                 add_flag(&cppflags_obst, "-I%s", opt);
763                         } else if (option[0] == 'D') {
764                                 const char *opt;
765                                 GET_ARG_AFTER(opt, "-D");
766                                 add_flag(&cppflags_obst, "-D%s", opt);
767                         } else if (option[0] == 'U') {
768                                 const char *opt;
769                                 GET_ARG_AFTER(opt, "-U");
770                                 add_flag(&cppflags_obst, "-U%s", opt);
771                         } else if (option[0] == 'l') {
772                                 const char *opt;
773                                 GET_ARG_AFTER(opt, "-l");
774                                 add_flag(&ldflags_obst, "-l%s", opt);
775                         } else if (option[0] == 'L') {
776                                 const char *opt;
777                                 GET_ARG_AFTER(opt, "-L");
778                                 add_flag(&ldflags_obst, "-L%s", opt);
779                         } else if (SINGLE_OPTION('v')) {
780                                 verbose = 1;
781                         } else if (SINGLE_OPTION('w')) {
782                                 memset(&warning, 0, sizeof(warning));
783                         } else if (option[0] == 'x') {
784                                 const char *opt;
785                                 GET_ARG_AFTER(opt, "-x");
786                                 forced_filetype = get_filetype_from_string(opt);
787                                 if (forced_filetype == FILETYPE_UNKNOWN) {
788                                         fprintf(stderr, "Unknown language '%s'\n", opt);
789                                         argument_errors = true;
790                                 }
791                         } else if (streq(option, "M")) {
792                                 mode = PreprocessOnly;
793                                 add_flag(&cppflags_obst, "-M");
794                         } else if (streq(option, "MMD") ||
795                                    streq(option, "MD")) {
796                             construct_dep_target = true;
797                                 add_flag(&cppflags_obst, "-%s", option);
798                         } else if (streq(option, "MM")  ||
799                                    streq(option, "MP")) {
800                                 add_flag(&cppflags_obst, "-%s", option);
801                         } else if (streq(option, "MT") ||
802                                    streq(option, "MQ") ||
803                                    streq(option, "MF")) {
804                                 const char *opt;
805                                 GET_ARG_AFTER(opt, "-MT");
806                                 add_flag(&cppflags_obst, "-%s", option);
807                                 add_flag(&cppflags_obst, "%s", opt);
808                         } else if (streq(option, "include")) {
809                                 const char *opt;
810                                 GET_ARG_AFTER(opt, "-include");
811                                 add_flag(&cppflags_obst, "-include");
812                                 add_flag(&cppflags_obst, "%s", opt);
813                         } else if (streq(option, "isystem")) {
814                                 const char *opt;
815                                 GET_ARG_AFTER(opt, "-isystem");
816                                 add_flag(&cppflags_obst, "-isystem");
817                                 add_flag(&cppflags_obst, "%s", opt);
818                         } else if (streq(option, "nostdinc")
819                                         || streq(option, "trigraphs")) {
820                                 /* pass these through to the preprocessor */
821                                 add_flag(&cppflags_obst, "%s", arg);
822                         } else if (streq(option, "pipe")) {
823                                 /* here for gcc compatibility */
824                         } else if (option[0] == 'f') {
825                                 char const *orig_opt;
826                                 GET_ARG_AFTER(orig_opt, "-f");
827
828                                 if (strstart(orig_opt, "align-loops=") ||
829                                     strstart(orig_opt, "align-jumps=") ||
830                                     strstart(orig_opt, "align-functions=")) {
831                                         fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
832                                 } else if (streq(orig_opt, "verbose-asm")) {
833                                         /* ignore: we always print verbose assembler */
834                                 } else {
835                                         char const *opt         = orig_opt;
836                                         bool        truth_value = true;
837                                         if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
838                                                 truth_value = false;
839                                                 opt += 3;
840                                         }
841
842                                         if (streq(opt, "dollars-in-identifiers")) {
843                                                 allow_dollar_in_symbol = truth_value;
844                                         } if (streq(opt, "builtins")) {
845                                                 use_builtins = truth_value;
846                                         } else if (streq(opt, "short-wchar")) {
847                                                 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
848                                                         : ATOMIC_TYPE_INT;
849                                         } else if (streq(opt, "syntax-only")) {
850                                                 mode = truth_value ? ParseOnly : CompileAssembleLink;
851                                         } else if (streq(opt, "omit-frame-pointer")) {
852                                                 set_be_option(truth_value ? "omitfp" : "omitfp=no");
853                                         } else if (streq(opt, "strength-reduce")) {
854                                                 firm_option(truth_value ? "strength-red" : "no-strength-red");
855                                         } else if (streq(opt, "fast-math")               ||
856                                                    streq(opt, "jump-tables")             ||
857                                                    streq(opt, "unroll-loops")            ||
858                                                    streq(opt, "expensive-optimizations") ||
859                                                    streq(opt, "common")                  ||
860                                                    streq(opt, "PIC")                     ||
861                                                    streq(opt, "align-loops")             ||
862                                                    streq(opt, "align-jumps")             ||
863                                                    streq(opt, "align-functions")) {
864                                                 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
865                                         } else {
866                                                 int res = firm_option(orig_opt);
867                                                 if (res == 0) {
868                                                         fprintf(stderr, "error: unknown Firm option '-f%s'\n",
869                                                                 orig_opt);
870                                                         argument_errors = true;
871                                                         continue;
872                                                 } else if (res == -1) {
873                                                         help_displayed = true;
874                                                 }
875                                         }
876                                 }
877                         } else if (option[0] == 'b') {
878                                 const char *opt;
879                                 GET_ARG_AFTER(opt, "-b");
880                                 int res = firm_be_option(opt);
881                                 if (res == 0) {
882                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
883                                                 opt);
884                                         argument_errors = true;
885                                 } else if (res == -1) {
886                                         help_displayed = true;
887                                 } else if (strstart(opt, "isa=")) {
888                                         strncpy(cpu_arch, opt, sizeof(cpu_arch));
889                                 }
890                         } else if (option[0] == 'W') {
891                                 if (strstart(option + 1, "p,")) {
892                                         // pass options directly to the preprocessor
893                                         const char *opt;
894                                         GET_ARG_AFTER(opt, "-Wp,");
895                                         add_flag(&cppflags_obst, "-Wp,%s", opt);
896                                 } else if (strstart(option + 1, "l,")) {
897                                         // pass options directly to the linker
898                                         const char *opt;
899                                         GET_ARG_AFTER(opt, "-Wl,");
900                                         add_flag(&ldflags_obst, "-Wl,%s", opt);
901                                 } else if (streq(option + 1, "no-trigraphs")
902                                                         || streq(option + 1, "undef")) {
903                                         add_flag(&cppflags_obst, "%s", arg);
904                                 } else {
905                                         set_warning_opt(&option[1]);
906                                 }
907                         } else if (option[0] == 'm') {
908                                 /* -m options */
909                                 const char *opt;
910                                 char arch_opt[64];
911
912                                 GET_ARG_AFTER(opt, "-m");
913                                 if (strstart(opt, "arch=")) {
914                                         GET_ARG_AFTER(opt, "-march=");
915                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
916                                         int res = firm_be_option(arch_opt);
917                                         if (res == 0)
918                                                 argument_errors = true;
919                                         else {
920                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
921                                                 int res = firm_be_option(arch_opt);
922                                                 if (res == 0)
923                                                         argument_errors = true;
924                                         }
925                                 } else if (strstart(opt, "tune=")) {
926                                         GET_ARG_AFTER(opt, "-mtune=");
927                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
928                                         int res = firm_be_option(arch_opt);
929                                         if (res == 0)
930                                                 argument_errors = true;
931                                 } else if (strstart(opt, "cpu=")) {
932                                         GET_ARG_AFTER(opt, "-mcpu=");
933                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
934                                         int res = firm_be_option(arch_opt);
935                                         if (res == 0)
936                                                 argument_errors = true;
937                                 } else if (strstart(opt, "fpmath=")) {
938                                         GET_ARG_AFTER(opt, "-mfpmath=");
939                                         if (streq(opt, "387"))
940                                                 opt = "x87";
941                                         else if (streq(opt, "sse"))
942                                                 opt = "sse2";
943                                         else {
944                                                 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
945                                                 argument_errors = true;
946                                         }
947                                         if (!argument_errors) {
948                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
949                                                 int res = firm_be_option(arch_opt);
950                                                 if (res == 0)
951                                                         argument_errors = true;
952                                         }
953                                 } else if (strstart(opt, "preferred-stack-boundary=")) {
954                                         GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
955                                         snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
956                                         int res = firm_be_option(arch_opt);
957                                         if (res == 0)
958                                                 argument_errors = true;
959                                 } else if (streq(opt, "omit-leaf-frame-pointer")) {
960                                         set_be_option("omitleaffp=1");
961                                 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
962                                         set_be_option("omitleaffp=0");
963                                 } else {
964                                         char *endptr;
965                                         long int value = strtol(opt, &endptr, 10);
966                                         if (*endptr != '\0') {
967                                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
968                                                 argument_errors = true;
969                                         }
970                                         if (value != 16 && value != 32 && value != 64) {
971                                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
972                                                 argument_errors = true;
973                                         } else {
974                                                 machine_size = (unsigned int)value;
975                                         }
976                                 }
977                         } else if (streq(option, "pg")) {
978                                 set_be_option("gprof");
979                                 add_flag(&ldflags_obst, "-pg");
980                         } else if (streq(option, "pedantic") ||
981                                    streq(option, "ansi")) {
982                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
983                         } else if (streq(option, "shared")) {
984                                 add_flag(&ldflags_obst, "-shared");
985                         } else if (strstart(option, "std=")) {
986                                 const char *const o = &option[4];
987                                 standard =
988                                         streq(o, "c++")            ? STANDARD_CXX98   :
989                                         streq(o, "c++98")          ? STANDARD_CXX98   :
990                                         streq(o, "c89")            ? STANDARD_C89     :
991                                         streq(o, "c99")            ? STANDARD_C99     :
992                                         streq(o, "c9x")            ? STANDARD_C99     : // deprecated
993                                         streq(o, "gnu++98")        ? STANDARD_GNUXX98 :
994                                         streq(o, "gnu89")          ? STANDARD_GNU89   :
995                                         streq(o, "gnu99")          ? STANDARD_GNU99   :
996                                         streq(o, "gnu9x")          ? STANDARD_GNU99   : // deprecated
997                                         streq(o, "iso9899:1990")   ? STANDARD_C89     :
998                                         streq(o, "iso9899:199409") ? STANDARD_C90     :
999                                         streq(o, "iso9899:1999")   ? STANDARD_C99     :
1000                                         streq(o, "iso9899:199x")   ? STANDARD_C99     : // deprecated
1001                                         (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1002                         } else if (streq(option, "version")) {
1003                                 print_cparser_version();
1004                         } else if (strstart(option, "print-file-name=")) {
1005                                 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1006                         } else if (option[0] == '-') {
1007                                 /* double dash option */
1008                                 ++option;
1009                                 if (streq(option, "gcc")) {
1010                                         features_on  |=  _GNUC;
1011                                         features_off &= ~_GNUC;
1012                                 } else if (streq(option, "no-gcc")) {
1013                                         features_on  &= ~_GNUC;
1014                                         features_off |=  _GNUC;
1015                                 } else if (streq(option, "ms")) {
1016                                         features_on  |=  _MS;
1017                                         features_off &= ~_MS;
1018                                 } else if (streq(option, "no-ms")) {
1019                                         features_on  &= ~_MS;
1020                                         features_off |=  _MS;
1021                                 } else if (streq(option, "signed-chars")) {
1022                                         char_is_signed = true;
1023                                 } else if (streq(option, "unsigned-chars")) {
1024                                         char_is_signed = false;
1025                                 } else if (streq(option, "strict")) {
1026                                         strict_mode = true;
1027                                 } else if (streq(option, "lextest")) {
1028                                         mode = LexTest;
1029                                 } else if (streq(option, "benchmark")) {
1030                                         mode = BenchmarkParser;
1031                                 } else if (streq(option, "print-ast")) {
1032                                         mode = PrintAst;
1033                                 } else if (streq(option, "print-implicit-cast")) {
1034                                         print_implicit_casts = true;
1035                                 } else if (streq(option, "print-parenthesis")) {
1036                                         print_parenthesis = true;
1037                                 } else if (streq(option, "print-fluffy")) {
1038                                         mode = PrintFluffy;
1039                                 } else if (streq(option, "print-caml")) {
1040                                         mode = PrintCaml;
1041                                 } else if (streq(option, "version")) {
1042                                         print_cparser_version();
1043                                         exit(EXIT_SUCCESS);
1044                                 } else if (streq(option, "dump-function")) {
1045                                         ++i;
1046                                         if (i >= argc) {
1047                                                 fprintf(stderr, "error: "
1048                                                         "expected argument after '--dump-function'\n");
1049                                                 argument_errors = true;
1050                                                 break;
1051                                         }
1052                                         dumpfunction = argv[i];
1053                                         mode         = CompileDump;
1054                                 } else {
1055                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
1056                                         argument_errors = true;
1057                                 }
1058                         } else {
1059                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1060                                 argument_errors = true;
1061                         }
1062                 } else {
1063                         filetype_t type = forced_filetype;
1064                         if (type == FILETYPE_AUTODETECT) {
1065                                 if (streq(arg, "-")) {
1066                                         /* - implicitly means C source file */
1067                                         type = FILETYPE_C;
1068                                 } else {
1069                                         const char *suffix = strrchr(arg, '.');
1070                                         /* Ensure there is at least one char before the suffix */
1071                                         if (suffix != NULL && suffix != arg) {
1072                                                 ++suffix;
1073                                                 type =
1074                                                         streq(suffix, "S")   ? FILETYPE_ASSEMBLER              :
1075                                                         streq(suffix, "a")   ? FILETYPE_OBJECT                 :
1076                                                         streq(suffix, "c")   ? FILETYPE_C                      :
1077                                                         streq(suffix, "cc")  ? FILETYPE_CXX                    :
1078                                                         streq(suffix, "cpp") ? FILETYPE_CXX                    :
1079                                                         streq(suffix, "cxx") ? FILETYPE_CXX                    :
1080                                                         streq(suffix, "h")   ? FILETYPE_C                      :
1081                                                         streq(suffix, "o")   ? FILETYPE_OBJECT                 :
1082                                                         streq(suffix, "s")   ? FILETYPE_PREPROCESSED_ASSEMBLER :
1083                                                         streq(suffix, "so")  ? FILETYPE_OBJECT                 :
1084                                                         FILETYPE_AUTODETECT;
1085                                         }
1086                                 }
1087
1088                                 if (type == FILETYPE_AUTODETECT) {
1089                                         fprintf(stderr, "'%s': file format not recognized\n", arg);
1090                                         continue;
1091                                 }
1092                         }
1093
1094                         file_list_entry_t *entry
1095                                 = obstack_alloc(&file_obst, sizeof(entry[0]));
1096                         memset(entry, 0, sizeof(entry[0]));
1097                         entry->name = arg;
1098                         entry->type = type;
1099
1100                         if (last_file != NULL) {
1101                                 last_file->next = entry;
1102                         } else {
1103                                 files = entry;
1104                         }
1105                         last_file = entry;
1106                 }
1107         }
1108
1109         if (print_file_name_file != NULL) {
1110                 print_file_name(print_file_name_file);
1111                 return 0;
1112         }
1113
1114         if (files == NULL) {
1115                 fprintf(stderr, "error: no input files specified\n");
1116                 argument_errors = true;
1117         }
1118
1119         if (help_displayed) {
1120                 return !argument_errors;
1121         }
1122         if (argument_errors) {
1123                 usage(argv[0]);
1124                 return 1;
1125         }
1126
1127         /* we do the lowering in ast2firm */
1128         firm_opt.lower_bitfields = FALSE;
1129
1130         gen_firm_init();
1131         init_symbol_table();
1132         init_types();
1133         init_typehash();
1134         init_basic_types();
1135         init_lexer();
1136         init_ast();
1137         init_parser();
1138         init_ast2firm();
1139         init_mangle();
1140
1141         if (construct_dep_target) {
1142                 if (outname != 0 && strlen(outname) >= 2) {
1143                         get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1144                 } else {
1145                         get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1146                 }
1147         } else {
1148                 dep_target[0] = '\0';
1149         }
1150
1151         char outnamebuf[4096];
1152         if (outname == NULL) {
1153                 const char *filename = files->name;
1154
1155                 switch(mode) {
1156                 case BenchmarkParser:
1157                 case PrintAst:
1158                 case PrintFluffy:
1159                 case PrintCaml:
1160                 case LexTest:
1161                 case PreprocessOnly:
1162                 case ParseOnly:
1163                         outname = "-";
1164                         break;
1165                 case Compile:
1166                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1167                         outname = outnamebuf;
1168                         break;
1169                 case CompileAssemble:
1170                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1171                         outname = outnamebuf;
1172                         break;
1173                 case CompileDump:
1174                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1175                                         ".vcg");
1176                         outname = outnamebuf;
1177                         break;
1178                 case CompileAssembleLink:
1179 #ifdef _WIN32
1180                         outname = "a.exe";
1181 #else
1182                         outname = "a.out";
1183 #endif
1184                         break;
1185                 }
1186         }
1187
1188         assert(outname != NULL);
1189
1190         FILE *out;
1191         if (streq(outname, "-")) {
1192                 out = stdout;
1193         } else {
1194                 out = fopen(outname, "w");
1195                 if (out == NULL) {
1196                         fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1197                                         strerror(errno));
1198                         return 1;
1199                 }
1200         }
1201
1202         file_list_entry_t *file;
1203         for (file = files; file != NULL; file = file->next) {
1204                 char        asm_tempfile[1024];
1205                 const char *filename = file->name;
1206                 filetype_t  filetype = file->type;
1207
1208                 if (filetype == FILETYPE_OBJECT)
1209                         continue;
1210
1211                 FILE *in = NULL;
1212                 if (mode == LexTest) {
1213                         if (in == NULL)
1214                                 in = open_file(filename);
1215                         lextest(in, filename);
1216                         fclose(in);
1217                         exit(EXIT_SUCCESS);
1218                 }
1219
1220                 FILE *preprocessed_in = NULL;
1221                 switch (filetype) {
1222                         case FILETYPE_C:
1223                                 filetype = FILETYPE_PREPROCESSED_C;
1224                                 goto preprocess;
1225                         case FILETYPE_CXX:
1226                                 filetype = FILETYPE_PREPROCESSED_CXX;
1227                                 goto preprocess;
1228                         case FILETYPE_ASSEMBLER:
1229                                 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1230                                 add_flag(&cppflags_obst, "-x");
1231                                 add_flag(&cppflags_obst, "assembler-with-cpp");
1232                                 goto preprocess;
1233 preprocess:
1234                                 /* no support for input on FILE* yet */
1235                                 if (in != NULL)
1236                                         panic("internal compiler error: in for preprocessor != NULL");
1237
1238                                 preprocessed_in = preprocess(filename);
1239                                 if (mode == PreprocessOnly) {
1240                                         copy_file(out, preprocessed_in);
1241                                         int result = pclose(preprocessed_in);
1242                                         fclose(out);
1243                                         /* remove output file in case of error */
1244                                         if (out != stdout && result != EXIT_SUCCESS) {
1245                                                 unlink(outname);
1246                                         }
1247                                         return result;
1248                                 }
1249
1250                                 in = preprocessed_in;
1251                                 break;
1252
1253                         default:
1254                                 break;
1255                 }
1256
1257                 FILE *asm_out;
1258                 if (mode == Compile) {
1259                         asm_out = out;
1260                 } else {
1261                         asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1262                 }
1263
1264                 if (in == NULL)
1265                         in = open_file(filename);
1266
1267                 /* preprocess and compile */
1268                 if (filetype == FILETYPE_PREPROCESSED_C) {
1269                         char const* invalid_mode;
1270                         switch (standard) {
1271                                 case STANDARD_ANSI:
1272                                 case STANDARD_C89:   c_mode = _C89;                break;
1273                                 /* TODO ^v determine difference between these two */
1274                                 case STANDARD_C90:   c_mode = _C89;                break;
1275                                 case STANDARD_C99:   c_mode = _C89 | _C99;         break;
1276                                 case STANDARD_GNU89: c_mode = _C89 |        _GNUC; break;
1277
1278 default_c_warn:
1279                                         fprintf(stderr,
1280                                                         "warning: command line option \"-std=%s\" is not valid for C\n",
1281                                                         invalid_mode);
1282                                         /* FALLTHROUGH */
1283                                 case STANDARD_DEFAULT:
1284                                 case STANDARD_GNU99:   c_mode = _C89 | _C99 | _GNUC; break;
1285
1286                                 case STANDARD_CXX98:   invalid_mode = "c++98"; goto default_c_warn;
1287                                 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1288                         }
1289                         goto do_parsing;
1290                 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1291                         char const* invalid_mode;
1292                         switch (standard) {
1293                                 case STANDARD_C89:   invalid_mode = "c89";   goto default_cxx_warn;
1294                                 case STANDARD_C90:   invalid_mode = "c90";   goto default_cxx_warn;
1295                                 case STANDARD_C99:   invalid_mode = "c99";   goto default_cxx_warn;
1296                                 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1297                                 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1298
1299                                 case STANDARD_ANSI:
1300                                 case STANDARD_CXX98: c_mode = _CXX; break;
1301
1302 default_cxx_warn:
1303                                         fprintf(stderr,
1304                                                         "warning: command line option \"-std=%s\" is not valid for C++\n",
1305                                                         invalid_mode);
1306                                 case STANDARD_DEFAULT:
1307                                 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1308                         }
1309
1310 do_parsing:
1311                         c_mode |= features_on;
1312                         c_mode &= ~features_off;
1313                         init_tokens();
1314                         translation_unit_t *const unit = do_parsing(in, filename);
1315
1316                         /* prints the AST even if errors occurred */
1317                         if (mode == PrintAst) {
1318                                 type_set_output(out);
1319                                 ast_set_output(out);
1320                                 print_ast(unit);
1321                         }
1322
1323                         if (error_count > 0) {
1324                                 /* parsing failed because of errors */
1325                                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1326                                         warning_count);
1327                                 result = EXIT_FAILURE;
1328                                 continue;
1329                         } else if (warning_count > 0) {
1330                                 fprintf(stderr, "%u warning(s)\n", warning_count);
1331                         }
1332
1333                         if (in == preprocessed_in) {
1334                                 int pp_result = pclose(preprocessed_in);
1335                                 if (pp_result != EXIT_SUCCESS) {
1336                                         /* remove output file */
1337                                         if (out != stdout)
1338                                                 unlink(outname);
1339                                         exit(EXIT_FAILURE);
1340                                 }
1341                         }
1342
1343                         if (mode == BenchmarkParser) {
1344                                 return result;
1345                         } else if (mode == PrintFluffy) {
1346                                 write_fluffy_decls(out, unit);
1347                                 continue;
1348                         } else if (mode == PrintCaml) {
1349                                 write_caml_decls(out, unit);
1350                                 continue;
1351                         }
1352
1353                         translation_unit_to_firm(unit);
1354
1355                         if (mode == ParseOnly) {
1356                                 continue;
1357                         }
1358
1359                         if (mode == CompileDump) {
1360                                 /* find irg */
1361                                 ident    *id     = new_id_from_str(dumpfunction);
1362                                 ir_graph *irg    = NULL;
1363                                 int       n_irgs = get_irp_n_irgs();
1364                                 for (int i = 0; i < n_irgs; ++i) {
1365                                         ir_graph *tirg   = get_irp_irg(i);
1366                                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
1367                                         if (irg_id == id) {
1368                                                 irg = tirg;
1369                                                 break;
1370                                         }
1371                                 }
1372
1373                                 if (irg == NULL) {
1374                                         fprintf(stderr, "No graph for function '%s' found\n",
1375                                                 dumpfunction);
1376                                         exit(1);
1377                                 }
1378
1379                                 dump_ir_block_graph_file(irg, out);
1380                                 fclose(out);
1381                                 exit(0);
1382                         }
1383
1384                         gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1385                                         have_const_functions);
1386                         if (asm_out != out) {
1387                                 fclose(asm_out);
1388                         }
1389                 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1390                         copy_file(asm_out, in);
1391                         if (in == preprocessed_in) {
1392                                 int pp_result = pclose(preprocessed_in);
1393                                 if (pp_result != EXIT_SUCCESS) {
1394                                         /* remove output in error case */
1395                                         if (out != stdout)
1396                                                 unlink(outname);
1397                                         return pp_result;
1398                                 }
1399                         }
1400                         if (asm_out != out) {
1401                                 fclose(asm_out);
1402                         }
1403                 }
1404
1405                 if (mode == Compile)
1406                         continue;
1407
1408                 /* if we're here then we have preprocessed assembly */
1409                 filename = asm_tempfile;
1410                 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1411
1412                 /* assemble */
1413                 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1414                         char        temp[1024];
1415                         const char *filename_o;
1416                         if (mode == CompileAssemble) {
1417                                 fclose(out);
1418                                 filename_o = outname;
1419                         } else {
1420                                 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1421                                 fclose(tempf);
1422                                 filename_o = temp;
1423                         }
1424
1425                         assemble(filename_o, filename);
1426
1427                         size_t len = strlen(filename_o) + 1;
1428                         filename = obstack_copy(&file_obst, filename_o, len);
1429                         filetype = FILETYPE_OBJECT;
1430                 }
1431
1432                 /* ok we're done here, process next file */
1433                 file->name = filename;
1434                 file->type = filetype;
1435         }
1436
1437         if (result != EXIT_SUCCESS) {
1438                 if (out != stdout)
1439                         unlink(outname);
1440                 return result;
1441         }
1442
1443         /* link program file */
1444         if (mode == CompileAssembleLink) {
1445                 obstack_1grow(&ldflags_obst, '\0');
1446                 const char *flags = obstack_finish(&ldflags_obst);
1447
1448                 /* construct commandline */
1449                 obstack_printf(&file_obst, "%s", LINKER);
1450                 for (file_list_entry_t *entry = files; entry != NULL;
1451                                 entry = entry->next) {
1452                         if (entry->type != FILETYPE_OBJECT)
1453                                 continue;
1454
1455                         add_flag(&file_obst, "%s", entry->name);
1456                 }
1457
1458                 add_flag(&file_obst, "-o");
1459                 add_flag(&file_obst, outname);
1460                 obstack_printf(&file_obst, "%s", flags);
1461                 obstack_1grow(&file_obst, '\0');
1462
1463                 char *commandline = obstack_finish(&file_obst);
1464
1465                 if (verbose) {
1466                         puts(commandline);
1467                 }
1468                 int err = system(commandline);
1469                 if (err != EXIT_SUCCESS) {
1470                         fprintf(stderr, "linker reported an error\n");
1471                         exit(1);
1472                 }
1473         }
1474
1475         obstack_free(&cppflags_obst, NULL);
1476         obstack_free(&ldflags_obst, NULL);
1477         obstack_free(&file_obst, NULL);
1478
1479         exit_mangle();
1480         exit_ast2firm();
1481         exit_parser();
1482         exit_ast();
1483         exit_lexer();
1484         exit_typehash();
1485         exit_types();
1486         exit_tokens();
1487         exit_symbol_table();
1488         return 0;
1489 }