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