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