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