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