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