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