Improve diagnostic handling: Add [-Wfoo] and -Werror-foo.
[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_timing.h"
74 #include "driver/firm_machine.h"
75 #include "adt/error.h"
76 #include "adt/strutil.h"
77 #include "wrappergen/write_fluffy.h"
78 #include "wrappergen/write_jna.h"
79 #include "revision.h"
80 #include "warning.h"
81 #include "help.h"
82 #include "mangle.h"
83 #include "printer.h"
84
85 #ifndef PREPROCESSOR
86 #ifndef __WIN32__
87 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
88 #else
89 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
90 #endif
91 #endif
92
93 #ifndef LINKER
94 #define LINKER    "gcc"
95 #endif
96
97 #ifndef ASSEMBLER
98 #define ASSEMBLER "gcc -c -xassembler"
99 #endif
100
101 unsigned int       c_mode                    = _C89 | _ANSI | _C99 | _GNUC;
102 unsigned int       machine_size              = 32;
103 bool               byte_order_big_endian     = false;
104 bool               char_is_signed            = true;
105 bool               strict_mode               = false;
106 atomic_type_kind_t wchar_atomic_kind         = ATOMIC_TYPE_INT;
107 unsigned           long_double_size          = 0;
108 bool               enable_main_collect2_hack = false;
109 bool               freestanding              = false;
110
111 static machine_triple_t *target_machine;
112 static const char       *target_triple;
113 static int               verbose;
114 static bool              use_builtins;
115 static struct obstack    cppflags_obst;
116 static struct obstack    ldflags_obst;
117 static struct obstack    asflags_obst;
118 static char              dep_target[1024];
119 static const char       *outname;
120 static bool              define_intmax_types;
121
122 typedef enum lang_standard_t {
123         STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
124         STANDARD_ANSI,    /* c89 (for C) or c++98 (for C++) */
125         STANDARD_C89,     /* ISO C90 (sic) */
126         STANDARD_C90,     /* ISO C90 as modified in amendment 1 */
127         STANDARD_C99,     /* ISO C99 */
128         STANDARD_GNU89,   /* ISO C90 plus GNU extensions (including some C99) */
129         STANDARD_GNU99,   /* ISO C99 plus GNU extensions */
130         STANDARD_CXX98,   /* ISO C++ 1998 plus amendments */
131         STANDARD_GNUXX98  /* ISO C++ 1998 plus amendments and GNU extensions */
132 } lang_standard_t;
133
134 static lang_standard_t standard;
135
136 typedef struct file_list_entry_t file_list_entry_t;
137
138 typedef enum filetype_t {
139         FILETYPE_AUTODETECT,
140         FILETYPE_C,
141         FILETYPE_PREPROCESSED_C,
142         FILETYPE_CXX,
143         FILETYPE_PREPROCESSED_CXX,
144         FILETYPE_ASSEMBLER,
145         FILETYPE_PREPROCESSED_ASSEMBLER,
146         FILETYPE_OBJECT,
147         FILETYPE_IR,
148         FILETYPE_UNKNOWN
149 } filetype_t;
150
151 struct file_list_entry_t {
152         const char  *name; /**< filename or NULL for stdin */
153         filetype_t   type;
154         file_list_entry_t *next;
155 };
156
157 static file_list_entry_t *temp_files;
158
159 static void get_output_name(char *buf, size_t buflen, const char *inputname,
160                             const char *newext)
161 {
162         if (inputname == NULL)
163                 inputname = "a";
164
165         char const *const last_slash = strrchr(inputname, '/');
166         char const *const filename   =
167                 last_slash != NULL ? last_slash + 1 : inputname;
168         char const *const last_dot   = strrchr(filename, '.');
169         char const *const name_end   =
170                 last_dot != NULL ? last_dot : strchr(filename, '\0');
171
172         int const len = snprintf(buf, buflen, "%.*s%s",
173                         (int)(name_end - filename), filename, newext);
174 #ifdef _WIN32
175         if (len < 0 || buflen <= (size_t)len)
176 #else
177         if (buflen <= (size_t)len)
178 #endif
179                 panic("filename too long");
180 }
181
182 #include "gen_builtins.h"
183
184 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
185 {
186         start_parsing();
187
188         if (use_builtins) {
189                 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
190                 parse();
191         }
192
193         lexer_open_stream(in, input_name);
194         parse();
195
196         translation_unit_t *unit = finish_parsing();
197         return unit;
198 }
199
200 static void lextest(FILE *in, const char *fname)
201 {
202         lexer_open_stream(in, fname);
203
204         do {
205                 lexer_next_preprocessing_token();
206                 print_token(stdout, &lexer_token);
207                 putchar('\n');
208         } while (lexer_token.type != T_EOF);
209 }
210
211 static void add_flag(struct obstack *obst, const char *format, ...)
212 {
213         char buf[65536];
214         va_list ap;
215
216         va_start(ap, format);
217 #ifdef _WIN32
218         int len =
219 #endif
220                 vsnprintf(buf, sizeof(buf), format, ap);
221         va_end(ap);
222
223         obstack_1grow(obst, ' ');
224 #ifdef _WIN32
225         obstack_1grow(obst, '"');
226         obstack_grow(obst, buf, len);
227         obstack_1grow(obst, '"');
228 #else
229         /* escape stuff... */
230         for (char *c = buf; *c != '\0'; ++c) {
231                 switch(*c) {
232                 case ' ':
233                 case '"':
234                 case '$':
235                 case '&':
236                 case '(':
237                 case ')':
238                 case ';':
239                 case '<':
240                 case '>':
241                 case '\'':
242                 case '\\':
243                 case '\n':
244                 case '\r':
245                 case '\t':
246                 case '`':
247                 case '|':
248                         obstack_1grow(obst, '\\');
249                         /* FALLTHROUGH */
250                 default:
251                         obstack_1grow(obst, *c);
252                         break;
253                 }
254         }
255 #endif
256 }
257
258 static const char *type_to_string(type_t *type)
259 {
260         assert(type->kind == TYPE_ATOMIC);
261         return get_atomic_kind_name(type->atomic.akind);
262 }
263
264 static FILE *preprocess(const char *fname, filetype_t filetype)
265 {
266         static const char *common_flags = NULL;
267
268         if (common_flags == NULL) {
269                 obstack_1grow(&cppflags_obst, '\0');
270                 const char *flags = obstack_finish(&cppflags_obst);
271
272                 /* setup default defines */
273                 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
274                 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
275                 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
276                 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
277
278                 add_flag(&cppflags_obst, "-U__VERSION__");
279                 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
280
281                 if (define_intmax_types) {
282                         add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
283                         add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
284                         add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
285                         add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
286                 }
287
288                 if (flags[0] != '\0') {
289                         size_t len = strlen(flags);
290                         obstack_1grow(&cppflags_obst, ' ');
291                         obstack_grow(&cppflags_obst, flags, len);
292                 }
293                 obstack_1grow(&cppflags_obst, '\0');
294                 common_flags = obstack_finish(&cppflags_obst);
295         }
296
297         assert(obstack_object_size(&cppflags_obst) == 0);
298
299         const char *preprocessor = getenv("CPARSER_PP");
300         if (preprocessor != NULL) {
301                 obstack_printf(&cppflags_obst, "%s ", preprocessor);
302         } else {
303                 if (target_triple != NULL)
304                         obstack_printf(&cppflags_obst, "%s-", target_triple);
305                 obstack_printf(&cppflags_obst, PREPROCESSOR);
306         }
307
308         switch (filetype) {
309         case FILETYPE_C:
310                 add_flag(&cppflags_obst, "-std=c99");
311                 break;
312         case FILETYPE_CXX:
313                 add_flag(&cppflags_obst, "-std=c++98");
314                 break;
315         case FILETYPE_ASSEMBLER:
316                 add_flag(&cppflags_obst, "-x");
317                 add_flag(&cppflags_obst, "assembler-with-cpp");
318                 break;
319         default:
320                 break;
321         }
322         obstack_printf(&cppflags_obst, "%s", common_flags);
323
324         /* handle dependency generation */
325         if (dep_target[0] != '\0') {
326                 add_flag(&cppflags_obst, "-MF");
327                 add_flag(&cppflags_obst, dep_target);
328                 if (outname != NULL) {
329                                 add_flag(&cppflags_obst, "-MQ");
330                                 add_flag(&cppflags_obst, outname);
331                 }
332         }
333         add_flag(&cppflags_obst, fname);
334         obstack_1grow(&cppflags_obst, '\0');
335
336         char *commandline = obstack_finish(&cppflags_obst);
337         if (verbose) {
338                 puts(commandline);
339         }
340         FILE *f = popen(commandline, "r");
341         if (f == NULL) {
342                 fprintf(stderr, "invoking preprocessor failed\n");
343                 exit(EXIT_FAILURE);
344         }
345         /* we don't really need that anymore */
346         obstack_free(&cppflags_obst, commandline);
347
348         return f;
349 }
350
351 static void assemble(const char *out, const char *in)
352 {
353         obstack_1grow(&asflags_obst, '\0');
354         const char *flags = obstack_finish(&asflags_obst);
355
356         const char *assembler = getenv("CPARSER_AS");
357         if (assembler != NULL) {
358                 obstack_printf(&asflags_obst, "%s", assembler);
359         } else {
360                 if (target_triple != NULL)
361                         obstack_printf(&asflags_obst, "%s-", target_triple);
362                 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
363         }
364         if (flags[0] != '\0')
365                 obstack_printf(&asflags_obst, " %s", flags);
366
367         obstack_printf(&asflags_obst, " %s -o %s", in, out);
368         obstack_1grow(&asflags_obst, '\0');
369
370         char *commandline = obstack_finish(&asflags_obst);
371         if (verbose) {
372                 puts(commandline);
373         }
374         int err = system(commandline);
375         if (err != EXIT_SUCCESS) {
376                 fprintf(stderr, "assembler reported an error\n");
377                 exit(EXIT_FAILURE);
378         }
379         obstack_free(&asflags_obst, commandline);
380 }
381
382 static void print_file_name(const char *file)
383 {
384         add_flag(&ldflags_obst, "-print-file-name=%s", file);
385
386         obstack_1grow(&ldflags_obst, '\0');
387         const char *flags = obstack_finish(&ldflags_obst);
388
389         /* construct commandline */
390         const char *linker = getenv("CPARSER_LINK");
391         if (linker != NULL) {
392                 obstack_printf(&ldflags_obst, "%s ", linker);
393         } else {
394                 if (target_triple != NULL)
395                         obstack_printf(&ldflags_obst, "%s-", target_triple);
396                 obstack_printf(&ldflags_obst, "%s ", LINKER);
397         }
398         obstack_printf(&ldflags_obst, "%s ", linker);
399         obstack_printf(&ldflags_obst, "%s", flags);
400         obstack_1grow(&ldflags_obst, '\0');
401
402         char *commandline = obstack_finish(&ldflags_obst);
403         if (verbose) {
404                 puts(commandline);
405         }
406         int err = system(commandline);
407         if (err != EXIT_SUCCESS) {
408                 fprintf(stderr, "linker reported an error\n");
409                 exit(EXIT_FAILURE);
410         }
411         obstack_free(&ldflags_obst, commandline);
412 }
413
414 static const char *try_dir(const char *dir)
415 {
416         if (dir == NULL)
417                 return dir;
418         if (access(dir, R_OK | W_OK | X_OK) == 0)
419                 return dir;
420         return NULL;
421 }
422
423 static const char *get_tempdir(void)
424 {
425         static const char *tmpdir = NULL;
426
427         if (tmpdir != NULL)
428                 return tmpdir;
429
430         if (tmpdir == NULL)
431                 tmpdir = try_dir(getenv("TMPDIR"));
432         if (tmpdir == NULL)
433                 tmpdir = try_dir(getenv("TMP"));
434         if (tmpdir == NULL)
435                 tmpdir = try_dir(getenv("TEMP"));
436
437 #ifdef P_tmpdir
438         if (tmpdir == NULL)
439                 tmpdir = try_dir(P_tmpdir);
440 #endif
441
442         if (tmpdir == NULL)
443                 tmpdir = try_dir("/var/tmp");
444         if (tmpdir == NULL)
445                 tmpdir = try_dir("/usr/tmp");
446         if (tmpdir == NULL)
447                 tmpdir = try_dir("/tmp");
448
449         if (tmpdir == NULL)
450                 tmpdir = ".";
451
452         return tmpdir;
453 }
454
455 #ifndef HAVE_MKSTEMP
456 /* cheap and nasty mkstemp replacement */
457 static int mkstemp(char *templ)
458 {
459         mktemp(templ);
460         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
461 }
462 #endif
463
464 /**
465  * an own version of tmpnam, which: writes in a buffer, emits no warnings
466  * during linking (like glibc/gnu ld do for tmpnam)...
467  */
468 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
469 {
470         const char *tempdir = get_tempdir();
471
472         snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
473
474         int fd = mkstemp(buffer);
475         if (fd == -1) {
476                 fprintf(stderr, "couldn't create temporary file: %s\n",
477                         strerror(errno));
478                 exit(EXIT_FAILURE);
479         }
480         FILE *out = fdopen(fd, "w");
481         if (out == NULL) {
482                 fprintf(stderr, "couldn't create temporary file FILE*\n");
483                 exit(EXIT_FAILURE);
484         }
485
486         file_list_entry_t *entry = xmalloc(sizeof(*entry));
487         memset(entry, 0, sizeof(*entry));
488
489         size_t  name_len = strlen(buffer) + 1;
490         char   *name     = malloc(name_len);
491         memcpy(name, buffer, name_len);
492         entry->name      = name;
493
494         entry->next = temp_files;
495         temp_files  = entry;
496
497         return out;
498 }
499
500 static void free_temp_files(void)
501 {
502         file_list_entry_t *entry = temp_files;
503         file_list_entry_t *next;
504         for ( ; entry != NULL; entry = next) {
505                 next = entry->next;
506
507                 unlink(entry->name);
508                 free((char*) entry->name);
509                 free(entry);
510         }
511         temp_files = NULL;
512 }
513
514 typedef enum compile_mode_t {
515         BenchmarkParser,
516         PreprocessOnly,
517         ParseOnly,
518         Compile,
519         CompileDump,
520         CompileExportIR,
521         CompileAssemble,
522         CompileAssembleLink,
523         LexTest,
524         PrintAst,
525         PrintFluffy,
526         PrintJna
527 } compile_mode_t;
528
529 static void usage(const char *argv0)
530 {
531         fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
532 }
533
534 static void print_cparser_version(void)
535 {
536         printf("cparser (%s) using libFirm (%u.%u",
537                cparser_REVISION, ir_get_version_major(),
538                ir_get_version_minor());
539
540         const char *revision = ir_get_version_revision();
541         if (revision[0] != 0) {
542                 putchar(' ');
543                 fputs(revision, stdout);
544         }
545
546         const char *build = ir_get_version_build();
547         if (build[0] != 0) {
548                 putchar(' ');
549                 fputs(build, stdout);
550         }
551         puts(")");
552         puts("This is free software; see the source for copying conditions.  There is NO\n"
553              "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
554 }
555
556 static void print_help_basic(const char *argv0)
557 {
558         usage(argv0);
559         puts("");
560         put_help("--help",                   "Display this information");
561         put_help("--version",                "Display compiler version");
562         put_help("--help-parser",            "Display information about parser options");
563         put_help("--help-warnings",          "Display information about warning options");
564         put_help("--help-codegen",           "Display information about code-generation options");
565         put_help("--help-optimization",      "Display information about optimization options");
566         put_help("--help-linker",            "Display information about linker options");
567         put_help("--help-language-tools",    "Display information about language tools options");
568         put_help("--help-debug",             "Display information about compiler debugging options");
569         put_help("--help-firm",              "Display information about direct firm options");
570         put_help("--help-all",               "Display information about all options");
571         put_help("-c",                       "Compile and assemble but do not link");
572         put_help("-E",                       "Preprocess only");
573         put_help("-S",                       "Compile but do not assembler or link");
574         put_help("-o",                       "Specify output file");
575         put_help("-v",                       "Verbose output (show invocation of sub-processes)");
576         put_help("-x",                       "Force input language:");
577         put_choice("c",                      "C");
578         put_choice("c++",                    "C++");
579         put_choice("assembler",              "Assembler (no preprocessing)");
580         put_choice("assembler-with-cpp",     "Assembler with preprocessing");
581         put_choice("none",                   "Autodetection");
582         put_help("-pipe",                    "ignored (gcc compatibility)");
583 }
584
585 static void print_help_preprocessor(void)
586 {
587         put_help("-nostdinc",                "Do not search standard system include directories");
588         put_help("-trigraphs",               "Support ISO C trigraphs");
589         put_help("-isystem",                 "");
590         put_help("-include",                 "");
591         put_help("-I PATH",                  "");
592         put_help("-D SYMBOL[=value]",        "");
593         put_help("-U SYMBOL",                "");
594         put_help("-Wp,OPTION",               "pass option directly to preprocessor");
595         put_help("-M",                       "");
596         put_help("-MD",                      "");
597         put_help("-MMD",                     "");
598         put_help("-MM",                      "");
599         put_help("-MP",                      "");
600         put_help("-MT",                      "");
601         put_help("-MQ",                      "");
602         put_help("-MF",                      "");
603 }
604
605 static void print_help_parser(void)
606 {
607         put_help("-finput-charset=CHARSET",  "Select encoding of input files");
608         put_help("-fmessage-length=LEN",     "ignored (gcc compatibility)");
609         put_help("-fshort-wchar",            "wchar_t is unsigned short instead of int");
610         put_help("-fshow-column",            "show the column number in diagnostic messages");
611         put_help("-funsigned-char",          "char is an unsigned type");
612         put_help("--ms",                     "Enable msvc extensions");
613         put_help("--no-ms",                  "Disable msvc extensions");
614         put_help("--gcc",                    "Enable gcc extensions");
615         put_help("--no-gcc",                 "Disable gcc extensions");
616         put_help("-std=STANDARD",            "Specify language standard:");
617         put_choice("c99",                    "ISO C99 standard");
618         put_choice("c89",                    "ISO C89 standard");
619         put_choice("c9x",                    "deprecated");
620         put_choice("c++",                    "ISO C++ 98");
621         put_choice("c++98",                  "ISO C++ 98");
622         put_choice("gnu99",                  "ISO C99 + GNU extensions (default)");
623         put_choice("gnu89",                  "ISO C89 + GNU extensions");
624         put_choice("gnu9x",                  "deprecated");
625         put_choice("iso9899:1990",           "ISO C89");
626         put_choice("iso9899:199409",         "ISO C90");
627         put_choice("iso9899:1999",           "ISO C99");
628         put_choice("iso9899:199x",           "deprecated");
629         put_help("-pedantic",                "ignored (gcc compatibility)");
630         put_help("-ansi",                    "ignored (gcc compatibility)");
631         put_help("--strict",                 "Enable strict conformance checking");
632 }
633
634 static void print_help_warnings(void)
635 {
636         put_help("-w",                       "disable all warnings");
637         put_help("-Wno-trigraphs",           "warn if input contains trigraphs");
638         put_help("-Wundef",                  "Warn if an undefined macro is used in an #if");
639         print_warning_opt_help();
640 }
641
642 static void print_help_optimization(void)
643 {
644         put_help("-O LEVEL",                 "select optimization level (0-4)");
645         firm_option_help(put_help);
646         put_help("-fexpensive-optimizations","ignored (gcc compatibility)");
647 }
648
649 static void print_help_codegeneration(void)
650 {
651         put_help("-g",                       "Generate debug information");
652         put_help("-pg",                      "Instrument code for gnu gprof");
653         put_help("-fomit-frame-pointer",     "Produce code without frame pointer where possible");
654         put_help("-ffreestanding",           "compile in freestanding mode (see ISO C standard)");
655         put_help("-fhosted",                 "compile in hosted (not freestanding) mode");
656         put_help("-fprofile-generate",       "Generate instrumented code to collect profile information");
657         put_help("-fprofile-use",            "Use profile information generated by instrumented binaries");
658         put_help("-ffp-precise",             "precise floating point model");
659         put_help("-ffp-fast",                "imprecise floating point model");
660         put_help("-ffp-strict",              "strict floating point model");
661         put_help("-ffp-precise",             "precise floating point model");
662         put_help("-pthread",                 "Use pthread threading library");
663         put_help("-mtarget=TARGET",          "Specify target architecture as CPU-manufacturer-OS triple");
664         put_help("-mtriple=TARGET",          "alias for -mtarget (clang compatibility)");
665         put_help("-march=ARCH",              "");
666         put_help("-mtune=ARCH",              "");
667         put_help("-mcpu=CPU",                "");
668         put_help("-mfpmath=",                "");
669         put_help("-mpreferred-stack-boundary=", "");
670         put_help("-mrtd",                    "");
671         put_help("-mregparm=",               "not supported yet");
672         put_help("-msoft-float",             "not supported yet");
673         put_help("-m32",                     "generate 32bit code");
674         put_help("-m64",                     "generate 64bit code");
675         put_help("-fverbose-asm",            "ignored (gcc compatibility)");
676         put_help("-fjump-tables",            "ignored (gcc compatibility)");
677         put_help("-fcommon",                 "ignored (gcc compatibility)");
678         put_help("-foptimize-sibling-calls", "ignored (gcc compatibility)");
679         put_help("-falign-loops",            "ignored (gcc compatibility)");
680         put_help("-falign-jumps",            "ignored (gcc compatibility)");
681         put_help("-falign-functions",        "ignored (gcc compatibility)");
682         put_help("-fPIC",                    "ignored (gcc compatibility)");
683         put_help("-ffast-math",              "same as fp-fast (gcc compatibility)");
684         puts("");
685         puts("\tMost of these options can be used with a no- prefix to disable them");
686         puts("\ti.e. -fno-signed-char");
687 }
688
689 static void print_help_linker(void)
690 {
691         put_help("-l LIBRARY",               "");
692         put_help("-L PATH",                  "");
693         put_help("-shared",                  "Produce a shared library");
694         put_help("-static",                  "Produce statically linked binary");
695         put_help("-Wl,OPTION",               "pass option directly to linker");
696 }
697
698 static void print_help_debug(void)
699 {
700         put_help("--lextest",                "Preprocess and tokenize only");
701         put_help("--print-ast",              "Preprocess, parse and print AST");
702         put_help("--print-implicit-cast",    "");
703         put_help("--print-parenthesis",      "");
704         put_help("--benchmark",              "Preprocess and parse, produces no output");
705         put_help("--time",                   "Measure time of compiler passes");
706         put_help("--dump-function func",     "Preprocess, parse and output vcg graph of func");
707         put_help("--export-ir",              "Preprocess, parse and output compiler intermediate representation");
708 }
709
710 static void print_help_language_tools(void)
711 {
712         put_help("--print-fluffy",           "Preprocess, parse and generate declarations for the fluffy language");
713         put_help("--print-jna",              "Preprocess, parse and generate declarations for JNA");
714         put_help("--jna-limit filename",     "");
715         put_help("--jna-libname name",       "");
716 }
717
718 static void print_help_firm(void)
719 {
720         put_help("-bOPTION",                 "directly pass option to libFirm backend");
721         int res = be_parse_arg("help");
722         (void) res;
723         assert(res);
724 }
725
726 typedef enum {
727         HELP_NONE          = 0,
728         HELP_BASIC         = 1u << 0,
729         HELP_PREPROCESSOR  = 1u << 1,
730         HELP_PARSER        = 1u << 2,
731         HELP_WARNINGS      = 1u << 3,
732         HELP_OPTIMIZATION  = 1u << 4,
733         HELP_CODEGEN       = 1u << 5,
734         HELP_LINKER        = 1u << 6,
735         HELP_LANGUAGETOOLS = 1u << 7,
736         HELP_DEBUG         = 1u << 8,
737         HELP_FIRM          = 1u << 9,
738
739         HELP_ALL           = (unsigned)-1
740 } help_sections_t;
741
742 static void print_help(const char *argv0, help_sections_t sections)
743 {
744         if (sections & HELP_BASIC)         print_help_basic(argv0);
745         if (sections & HELP_PREPROCESSOR)  print_help_preprocessor();
746         if (sections & HELP_PARSER)        print_help_parser();
747         if (sections & HELP_WARNINGS)      print_help_warnings();
748         if (sections & HELP_OPTIMIZATION)  print_help_optimization();
749         if (sections & HELP_CODEGEN)       print_help_codegeneration();
750         if (sections & HELP_LINKER)        print_help_linker();
751         if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
752         if (sections & HELP_DEBUG)         print_help_debug();
753         if (sections & HELP_FIRM)          print_help_firm();
754 }
755
756 static void set_be_option(const char *arg)
757 {
758         int res = be_parse_arg(arg);
759         (void) res;
760         assert(res);
761 }
762
763 static void copy_file(FILE *dest, FILE *input)
764 {
765         char buf[16384];
766
767         while (!feof(input) && !ferror(dest)) {
768                 size_t read = fread(buf, 1, sizeof(buf), input);
769                 if (fwrite(buf, 1, read, dest) != read) {
770                         perror("couldn't write output");
771                 }
772         }
773 }
774
775 static FILE *open_file(const char *filename)
776 {
777         if (streq(filename, "-")) {
778                 return stdin;
779         }
780
781         FILE *in = fopen(filename, "r");
782         if (in == NULL) {
783                 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
784                                 strerror(errno));
785                 exit(EXIT_FAILURE);
786         }
787
788         return in;
789 }
790
791 static filetype_t get_filetype_from_string(const char *string)
792 {
793         if (streq(string, "c") || streq(string, "c-header"))
794                 return FILETYPE_C;
795         if (streq(string, "c++") || streq(string, "c++-header"))
796                 return FILETYPE_CXX;
797         if (streq(string, "assembler"))
798                 return FILETYPE_PREPROCESSED_ASSEMBLER;
799         if (streq(string, "assembler-with-cpp"))
800                 return FILETYPE_ASSEMBLER;
801         if (streq(string, "none"))
802                 return FILETYPE_AUTODETECT;
803
804         return FILETYPE_UNKNOWN;
805 }
806
807 static bool init_os_support(void)
808 {
809         const char *os = target_machine->operating_system;
810         wchar_atomic_kind         = ATOMIC_TYPE_INT;
811         enable_main_collect2_hack = false;
812         define_intmax_types       = false;
813
814         if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
815                         || streq(os, "solaris")) {
816                 set_create_ld_ident(create_name_linux_elf);
817         } else if (streq(os, "darwin")) {
818                 long_double_size = 16;
819                 set_create_ld_ident(create_name_macho);
820                 define_intmax_types = true;
821         } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
822                 wchar_atomic_kind         = ATOMIC_TYPE_USHORT;
823                 enable_main_collect2_hack = true;
824                 set_create_ld_ident(create_name_win32);
825         } else {
826                 return false;
827         }
828
829         return true;
830 }
831
832 static bool parse_target_triple(const char *arg)
833 {
834         machine_triple_t *triple = firm_parse_machine_triple(arg);
835         if (triple == NULL) {
836                 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
837                 return false;
838         }
839         target_machine = triple;
840         return true;
841 }
842
843 static void setup_target_machine(void)
844 {
845         if (!setup_firm_for_machine(target_machine))
846                 exit(1);
847
848         const backend_params *be_params = be_get_backend_param();
849         if (be_params->long_double_size % 8 != 0) {
850                 fprintf(stderr, "firm-target long double size is not a multiple of 8, can't handle this\n");
851                 exit(1);
852         }
853
854         byte_order_big_endian = be_params->byte_order_big_endian;
855         machine_size          = be_params->machine_size;
856         long_double_size      = be_params->long_double_size / 8;
857
858         init_os_support();
859 }
860
861 int main(int argc, char **argv)
862 {
863         firm_early_init();
864
865         const char        *dumpfunction         = NULL;
866         const char        *print_file_name_file = NULL;
867         compile_mode_t     mode                 = CompileAssembleLink;
868         int                opt_level            = 1;
869         int                result               = EXIT_SUCCESS;
870         char               cpu_arch[16]         = "ia32";
871         file_list_entry_t *files                = NULL;
872         file_list_entry_t *last_file            = NULL;
873         bool               construct_dep_target = false;
874         bool               do_timing            = false;
875         bool               profile_generate     = false;
876         bool               profile_use          = false;
877         struct obstack     file_obst;
878
879         atexit(free_temp_files);
880
881         /* hack for now... */
882         if (strstr(argv[0], "pptest") != NULL) {
883                 extern int pptest_main(int argc, char **argv);
884                 return pptest_main(argc, argv);
885         }
886
887         obstack_init(&cppflags_obst);
888         obstack_init(&ldflags_obst);
889         obstack_init(&asflags_obst);
890         obstack_init(&file_obst);
891
892 #define GET_ARG_AFTER(def, args)                                             \
893         do {                                                                     \
894         def = &arg[sizeof(args)-1];                                              \
895         if (def[0] == '\0') {                                                    \
896                 ++i;                                                                 \
897                 if (i >= argc) {                                                     \
898                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
899                         argument_errors = true;                                          \
900                         break;                                                           \
901                 }                                                                    \
902                 def = argv[i];                                                       \
903                 if (def[0] == '-' && def[1] != '\0') {                               \
904                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
905                         argument_errors = true;                                          \
906                         continue;                                                        \
907                 }                                                                    \
908         }                                                                        \
909         } while (0)
910
911 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
912
913         /* early options parsing (find out optimization level and OS) */
914         for (int i = 1; i < argc; ++i) {
915                 const char *arg = argv[i];
916                 if (arg[0] != '-')
917                         continue;
918
919                 const char *option = &arg[1];
920                 if (option[0] == 'O') {
921                         sscanf(&option[1], "%d", &opt_level);
922                 }
923         }
924
925         const char *target = getenv("TARGET");
926         if (target != NULL)
927                 parse_target_triple(target);
928         if (target_machine == NULL) {
929                 target_machine = firm_get_host_machine();
930         }
931         choose_optimization_pack(opt_level);
932         setup_target_machine();
933
934         /* parse rest of options */
935                         standard        = STANDARD_DEFAULT;
936         unsigned        features_on     = 0;
937         unsigned        features_off    = 0;
938         filetype_t      forced_filetype = FILETYPE_AUTODETECT;
939         help_sections_t help            = HELP_NONE;
940         bool            argument_errors = false;
941         for (int i = 1; i < argc; ++i) {
942                 const char *arg = argv[i];
943                 if (arg[0] == '-' && arg[1] != '\0') {
944                         /* an option */
945                         const char *option = &arg[1];
946                         if (option[0] == 'o') {
947                                 GET_ARG_AFTER(outname, "-o");
948                         } else if (option[0] == 'g') {
949                                 set_be_option("debuginfo=stabs");
950                                 set_be_option("omitfp=no");
951                                 set_be_option("ia32-nooptcc=yes");
952                         } else if (SINGLE_OPTION('c')) {
953                                 mode = CompileAssemble;
954                         } else if (SINGLE_OPTION('E')) {
955                                 mode = PreprocessOnly;
956                         } else if (SINGLE_OPTION('S')) {
957                                 mode = Compile;
958                         } else if (option[0] == 'O') {
959                                 continue;
960                         } else if (option[0] == 'I') {
961                                 const char *opt;
962                                 GET_ARG_AFTER(opt, "-I");
963                                 add_flag(&cppflags_obst, "-I%s", opt);
964                         } else if (option[0] == 'D') {
965                                 const char *opt;
966                                 GET_ARG_AFTER(opt, "-D");
967                                 add_flag(&cppflags_obst, "-D%s", opt);
968                         } else if (option[0] == 'U') {
969                                 const char *opt;
970                                 GET_ARG_AFTER(opt, "-U");
971                                 add_flag(&cppflags_obst, "-U%s", opt);
972                         } else if (option[0] == 'l') {
973                                 const char *opt;
974                                 GET_ARG_AFTER(opt, "-l");
975                                 add_flag(&ldflags_obst, "-l%s", opt);
976                         } else if (option[0] == 'L') {
977                                 const char *opt;
978                                 GET_ARG_AFTER(opt, "-L");
979                                 add_flag(&ldflags_obst, "-L%s", opt);
980                         } else if (SINGLE_OPTION('v')) {
981                                 verbose = 1;
982                         } else if (SINGLE_OPTION('w')) {
983                                 disable_all_warnings();
984                         } else if (option[0] == 'x') {
985                                 const char *opt;
986                                 GET_ARG_AFTER(opt, "-x");
987                                 forced_filetype = get_filetype_from_string(opt);
988                                 if (forced_filetype == FILETYPE_UNKNOWN) {
989                                         fprintf(stderr, "Unknown language '%s'\n", opt);
990                                         argument_errors = true;
991                                 }
992                         } else if (streq(option, "M")) {
993                                 mode = PreprocessOnly;
994                                 add_flag(&cppflags_obst, "-M");
995                         } else if (streq(option, "MMD") ||
996                                    streq(option, "MD")) {
997                             construct_dep_target = true;
998                                 add_flag(&cppflags_obst, "-%s", option);
999                         } else if (streq(option, "MM")  ||
1000                                    streq(option, "MP")) {
1001                                 add_flag(&cppflags_obst, "-%s", option);
1002                         } else if (streq(option, "MT") ||
1003                                    streq(option, "MQ") ||
1004                                    streq(option, "MF")) {
1005                                 const char *opt;
1006                                 GET_ARG_AFTER(opt, "-MT");
1007                                 add_flag(&cppflags_obst, "-%s", option);
1008                                 add_flag(&cppflags_obst, "%s", opt);
1009                         } else if (streq(option, "include")) {
1010                                 const char *opt;
1011                                 GET_ARG_AFTER(opt, "-include");
1012                                 add_flag(&cppflags_obst, "-include");
1013                                 add_flag(&cppflags_obst, "%s", opt);
1014                         } else if (streq(option, "isystem")) {
1015                                 const char *opt;
1016                                 GET_ARG_AFTER(opt, "-isystem");
1017                                 add_flag(&cppflags_obst, "-isystem");
1018                                 add_flag(&cppflags_obst, "%s", opt);
1019 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1020                         } else if (streq(option, "pthread")) {
1021                                 /* set flags for the preprocessor */
1022                                 add_flag(&cppflags_obst, "-D_REENTRANT");
1023                                 /* set flags for the linker */
1024                                 add_flag(&ldflags_obst, "-lpthread");
1025 #endif
1026                         } else if (streq(option, "nostdinc")
1027                                         || streq(option, "trigraphs")) {
1028                                 /* pass these through to the preprocessor */
1029                                 add_flag(&cppflags_obst, "%s", arg);
1030                         } else if (streq(option, "pipe")) {
1031                                 /* here for gcc compatibility */
1032                         } else if (streq(option, "static")) {
1033                                 add_flag(&ldflags_obst, "-static");
1034                         } else if (streq(option, "shared")) {
1035                                 add_flag(&ldflags_obst, "-shared");
1036                         } else if (option[0] == 'f') {
1037                                 char const *orig_opt;
1038                                 GET_ARG_AFTER(orig_opt, "-f");
1039
1040                                 if (strstart(orig_opt, "input-charset=")) {
1041                                         char const* const encoding = strchr(orig_opt, '=') + 1;
1042                                         select_input_encoding(encoding);
1043                                 } else if (strstart(orig_opt, "align-loops=") ||
1044                                            strstart(orig_opt, "align-jumps=") ||
1045                                            strstart(orig_opt, "align-functions=")) {
1046                                         fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1047                                 } else if (strstart(orig_opt, "visibility=")) {
1048                                         const char *val = strchr(orig_opt, '=')+1;
1049                                         elf_visibility_tag_t visibility
1050                                                 = get_elf_visibility_from_string(val);
1051                                         if (visibility == ELF_VISIBILITY_ERROR) {
1052                                                 fprintf(stderr, "invalid visibility '%s' specified\n",
1053                                                         val);
1054                                                 argument_errors = true;
1055                                         } else {
1056                                                 set_default_visibility(visibility);
1057                                         }
1058                                 } else if (strstart(orig_opt, "message-length=")) {
1059                                         /* ignore: would only affect error message format */
1060                                 } else {
1061                                         /* -f options which have an -fno- variant */
1062                                         char const *opt         = orig_opt;
1063                                         bool        truth_value = true;
1064                                         if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1065                                                 truth_value = false;
1066                                                 opt += 3;
1067                                         }
1068
1069                                         if (streq(opt, "builtins")) {
1070                                                 use_builtins = truth_value;
1071                                         } else if (streq(opt, "dollars-in-identifiers")) {
1072                                                 allow_dollar_in_symbol = truth_value;
1073                                         } else if (streq(opt, "omit-frame-pointer")) {
1074                                                 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1075                                         } else if (streq(opt, "short-wchar")) {
1076                                                 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1077                                                         : ATOMIC_TYPE_INT;
1078                                         } else if (streq(opt, "show-column")) {
1079                                                 show_column = truth_value;
1080                                         } else if (streq(opt, "signed-char")) {
1081                                                 char_is_signed = truth_value;
1082                                         } else if (streq(opt, "strength-reduce")) {
1083                                                 /* does nothing, for gcc compatibility (even gcc does
1084                                                  * nothing for this switch anymore) */
1085                                         } else if (streq(opt, "syntax-only")) {
1086                                                 mode = truth_value ? ParseOnly : CompileAssembleLink;
1087                                         } else if (streq(opt, "unsigned-char")) {
1088                                                 char_is_signed = !truth_value;
1089                                         } else if (streq(opt, "freestanding")) {
1090                                                 freestanding = truth_value;
1091                                         } else if (streq(opt, "hosted")) {
1092                                                 freestanding = !truth_value;
1093                                         } else if (streq(opt, "profile-generate")) {
1094                                                 profile_generate = truth_value;
1095                                         } else if (streq(opt, "profile-use")) {
1096                                                 profile_use = truth_value;
1097                                         } else if (!truth_value &&
1098                                                    streq(opt, "asynchronous-unwind-tables")) {
1099                                             /* nothing todo, a gcc feature which we don't support
1100                                              * anyway was deactivated */
1101                                         } else if (streq(opt, "verbose-asm")) {
1102                                                 /* ignore: we always print verbose assembler */
1103                                         } else if (streq(opt, "fast-math") || streq(opt, "fp-fast")) {
1104                                                 firm_fp_model = fp_model_fast;
1105                                         } else if (streq(opt, "fp-precise")) {
1106                                                 firm_fp_model = fp_model_precise;
1107                                         } else if (streq(opt, "fp-strict")) {
1108                                                 firm_fp_model = fp_model_strict;
1109                                         } else if (streq(opt, "jump-tables")             ||
1110                                                    streq(opt, "expensive-optimizations") ||
1111                                                    streq(opt, "common")                  ||
1112                                                    streq(opt, "optimize-sibling-calls")  ||
1113                                                    streq(opt, "align-loops")             ||
1114                                                    streq(opt, "align-jumps")             ||
1115                                                    streq(opt, "align-functions")         ||
1116                                                    streq(opt, "PIC")) {
1117                                                 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1118                                         } else if (streq(opt, "help")) {
1119                                                 fprintf(stderr, "warning: -fhelp is deprecated\n");
1120                                                 help |= HELP_OPTIMIZATION;
1121                                         } else {
1122                                                 int res = firm_option(orig_opt);
1123                                                 if (res == 0) {
1124                                                         fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1125                                                                 orig_opt);
1126                                                         argument_errors = true;
1127                                                         continue;
1128                                                 }
1129                                         }
1130                                 }
1131                         } else if (option[0] == 'b') {
1132                                 const char *opt;
1133                                 GET_ARG_AFTER(opt, "-b");
1134
1135                                 if (streq(opt, "help")) {
1136                                         fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1137                                         help |= HELP_FIRM;
1138                                 } else {
1139                                         int res = be_parse_arg(opt);
1140                                         if (res == 0) {
1141                                                 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1142                                                                 opt);
1143                                                 argument_errors = true;
1144                                         } else if (strstart(opt, "isa=")) {
1145                                                 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1146                                         }
1147                                 }
1148                         } else if (option[0] == 'W') {
1149                                 if (strstart(option + 1, "p,")) {
1150                                         // pass options directly to the preprocessor
1151                                         const char *opt;
1152                                         GET_ARG_AFTER(opt, "-Wp,");
1153                                         add_flag(&cppflags_obst, "-Wp,%s", opt);
1154                                 } else if (strstart(option + 1, "l,")) {
1155                                         // pass options directly to the linker
1156                                         const char *opt;
1157                                         GET_ARG_AFTER(opt, "-Wl,");
1158                                         add_flag(&ldflags_obst, "-Wl,%s", opt);
1159                                 } else if (streq(option + 1, "no-trigraphs")
1160                                                         || streq(option + 1, "undef")) {
1161                                         add_flag(&cppflags_obst, "%s", arg);
1162                                 } else {
1163                                         set_warning_opt(&option[1]);
1164                                 }
1165                         } else if (option[0] == 'm') {
1166                                 /* -m options */
1167                                 const char *opt;
1168                                 char arch_opt[64];
1169
1170                                 GET_ARG_AFTER(opt, "-m");
1171                                 if (strstart(opt, "target=")) {
1172                                         GET_ARG_AFTER(opt, "-mtarget=");
1173                                         if (!parse_target_triple(opt)) {
1174                                                 argument_errors = true;
1175                                         } else {
1176                                                 setup_target_machine();
1177                                                 target_triple = opt;
1178                                         }
1179                                 } else if (strstart(opt, "triple=")) {
1180                                         GET_ARG_AFTER(opt, "-mtriple=");
1181                                         if (!parse_target_triple(opt)) {
1182                                                 argument_errors = true;
1183                                         } else {
1184                                                 setup_target_machine();
1185                                                 target_triple = opt;
1186                                         }
1187                                 } else if (strstart(opt, "arch=")) {
1188                                         GET_ARG_AFTER(opt, "-march=");
1189                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1190                                         int res = be_parse_arg(arch_opt);
1191                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1192                                         res &= be_parse_arg(arch_opt);
1193
1194                                         if (res == 0) {
1195                                                 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1196                                                 argument_errors = true;
1197                                         }
1198                                 } else if (strstart(opt, "tune=")) {
1199                                         GET_ARG_AFTER(opt, "-mtune=");
1200                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1201                                         int res = be_parse_arg(arch_opt);
1202                                         if (res == 0)
1203                                                 argument_errors = true;
1204                                 } else if (strstart(opt, "cpu=")) {
1205                                         GET_ARG_AFTER(opt, "-mcpu=");
1206                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1207                                         int res = be_parse_arg(arch_opt);
1208                                         if (res == 0)
1209                                                 argument_errors = true;
1210                                 } else if (strstart(opt, "fpmath=")) {
1211                                         GET_ARG_AFTER(opt, "-mfpmath=");
1212                                         if (streq(opt, "387"))
1213                                                 opt = "x87";
1214                                         else if (streq(opt, "sse"))
1215                                                 opt = "sse2";
1216                                         else {
1217                                                 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1218                                                 argument_errors = true;
1219                                         }
1220                                         if (!argument_errors) {
1221                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1222                                                 int res = be_parse_arg(arch_opt);
1223                                                 if (res == 0)
1224                                                         argument_errors = true;
1225                                         }
1226                                 } else if (strstart(opt, "preferred-stack-boundary=")) {
1227                                         GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1228                                         snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1229                                         int res = be_parse_arg(arch_opt);
1230                                         if (res == 0)
1231                                                 argument_errors = true;
1232                                 } else if (streq(opt, "rtd")) {
1233                                         default_calling_convention = CC_STDCALL;
1234                                 } else if (strstart(opt, "regparm=")) {
1235                                         fprintf(stderr, "error: regparm convention not supported yet\n");
1236                                         argument_errors = true;
1237                                 } else if (streq(opt, "soft-float")) {
1238                                         fprintf(stderr, "error: software floatingpoint not supported yet\n");
1239                                         argument_errors = true;
1240                                 } else {
1241                                         long int value = strtol(opt, NULL, 10);
1242                                         if (value == 0) {
1243                                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
1244                                                 argument_errors = true;
1245                                         } else if (value != 16 && value != 32 && value != 64) {
1246                                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1247                                                 argument_errors = true;
1248                                         } else {
1249                                                 add_flag(&cppflags_obst, "-m%u", machine_size);
1250                                                 add_flag(&asflags_obst, "-m%u", machine_size);
1251                                                 add_flag(&ldflags_obst, "-m%u", machine_size);
1252                                                 /* TODO: choose/change backend based on this */
1253                                         }
1254                                 }
1255                         } else if (streq(option, "pg")) {
1256                                 set_be_option("gprof");
1257                                 add_flag(&ldflags_obst, "-pg");
1258                         } else if (streq(option, "pedantic") ||
1259                                    streq(option, "ansi")) {
1260                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1261                         } else if (strstart(option, "std=")) {
1262                                 const char *const o = &option[4];
1263                                 standard =
1264                                         streq(o, "c++")            ? STANDARD_CXX98   :
1265                                         streq(o, "c++98")          ? STANDARD_CXX98   :
1266                                         streq(o, "c89")            ? STANDARD_C89     :
1267                                         streq(o, "c99")            ? STANDARD_C99     :
1268                                         streq(o, "c9x")            ? STANDARD_C99     : // deprecated
1269                                         streq(o, "gnu++98")        ? STANDARD_GNUXX98 :
1270                                         streq(o, "gnu89")          ? STANDARD_GNU89   :
1271                                         streq(o, "gnu99")          ? STANDARD_GNU99   :
1272                                         streq(o, "gnu9x")          ? STANDARD_GNU99   : // deprecated
1273                                         streq(o, "iso9899:1990")   ? STANDARD_C89     :
1274                                         streq(o, "iso9899:199409") ? STANDARD_C90     :
1275                                         streq(o, "iso9899:1999")   ? STANDARD_C99     :
1276                                         streq(o, "iso9899:199x")   ? STANDARD_C99     : // deprecated
1277                                         (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1278                         } else if (streq(option, "version")) {
1279                                 print_cparser_version();
1280                         } else if (strstart(option, "print-file-name=")) {
1281                                 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1282                         } else if (option[0] == '-') {
1283                                 /* double dash option */
1284                                 ++option;
1285                                 if (streq(option, "gcc")) {
1286                                         features_on  |=  _GNUC;
1287                                         features_off &= ~_GNUC;
1288                                 } else if (streq(option, "no-gcc")) {
1289                                         features_on  &= ~_GNUC;
1290                                         features_off |=  _GNUC;
1291                                 } else if (streq(option, "ms")) {
1292                                         features_on  |=  _MS;
1293                                         features_off &= ~_MS;
1294                                 } else if (streq(option, "no-ms")) {
1295                                         features_on  &= ~_MS;
1296                                         features_off |=  _MS;
1297                                 } else if (streq(option, "strict")) {
1298                                         strict_mode = true;
1299                                 } else if (streq(option, "lextest")) {
1300                                         mode = LexTest;
1301                                 } else if (streq(option, "benchmark")) {
1302                                         mode = BenchmarkParser;
1303                                 } else if (streq(option, "print-ast")) {
1304                                         mode = PrintAst;
1305                                 } else if (streq(option, "print-implicit-cast")) {
1306                                         print_implicit_casts = true;
1307                                 } else if (streq(option, "print-parenthesis")) {
1308                                         print_parenthesis = true;
1309                                 } else if (streq(option, "print-fluffy")) {
1310                                         mode = PrintFluffy;
1311                                 } else if (streq(option, "print-jna")) {
1312                                         mode = PrintJna;
1313                                 } else if (streq(option, "jna-limit")) {
1314                                         ++i;
1315                                         if (i >= argc) {
1316                                                 fprintf(stderr, "error: "
1317                                                         "expected argument after '--jna-limit'\n");
1318                                                 argument_errors = true;
1319                                                 break;
1320                                         }
1321                                         jna_limit_output(argv[i]);
1322                                 } else if (streq(option, "jna-libname")) {
1323                                         ++i;
1324                                         if (i >= argc) {
1325                                                 fprintf(stderr, "error: "
1326                                                         "expected argument after '--jna-libname'\n");
1327                                                 argument_errors = true;
1328                                                 break;
1329                                         }
1330                                         jna_set_libname(argv[i]);
1331                                 } else if (streq(option, "time")) {
1332                                         do_timing = true;
1333                                 } else if (streq(option, "version")) {
1334                                         print_cparser_version();
1335                                         return EXIT_SUCCESS;
1336                                 } else if (streq(option, "help")) {
1337                                         help |= HELP_BASIC;
1338                                 } else if (streq(option, "help-parser")) {
1339                                         help |= HELP_PARSER;
1340                                 } else if (streq(option, "help-warnings")) {
1341                                         help |= HELP_WARNINGS;
1342                                 } else if (streq(option, "help-codegen")) {
1343                                         help |= HELP_CODEGEN;
1344                                 } else if (streq(option, "help-linker")) {
1345                                         help |= HELP_LINKER;
1346                                 } else if (streq(option, "help-optimization")) {
1347                                         help |= HELP_OPTIMIZATION;
1348                                 } else if (streq(option, "help-language-tools")) {
1349                                         help |= HELP_LANGUAGETOOLS;
1350                                 } else if (streq(option, "help-debug")) {
1351                                         help |= HELP_DEBUG;
1352                                 } else if (streq(option, "help-firm")) {
1353                                         help |= HELP_FIRM;
1354                                 } else if (streq(option, "help-all")) {
1355                                         help |= HELP_ALL;
1356                                 } else if (streq(option, "dump-function")) {
1357                                         ++i;
1358                                         if (i >= argc) {
1359                                                 fprintf(stderr, "error: "
1360                                                         "expected argument after '--dump-function'\n");
1361                                                 argument_errors = true;
1362                                                 break;
1363                                         }
1364                                         dumpfunction = argv[i];
1365                                         mode         = CompileDump;
1366                                 } else if (streq(option, "export-ir")) {
1367                                         mode = CompileExportIR;
1368                                 } else {
1369                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
1370                                         argument_errors = true;
1371                                 }
1372                         } else {
1373                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1374                                 argument_errors = true;
1375                         }
1376                 } else {
1377                         filetype_t type = forced_filetype;
1378                         if (type == FILETYPE_AUTODETECT) {
1379                                 if (streq(arg, "-")) {
1380                                         /* - implicitly means C source file */
1381                                         type = FILETYPE_C;
1382                                 } else {
1383                                         const char *suffix = strrchr(arg, '.');
1384                                         /* Ensure there is at least one char before the suffix */
1385                                         if (suffix != NULL && suffix != arg) {
1386                                                 ++suffix;
1387                                                 type =
1388                                                         streq(suffix, "S")   ? FILETYPE_ASSEMBLER              :
1389                                                         streq(suffix, "a")   ? FILETYPE_OBJECT                 :
1390                                                         streq(suffix, "c")   ? FILETYPE_C                      :
1391                                                         streq(suffix, "i")   ? FILETYPE_PREPROCESSED_C         :
1392                                                         streq(suffix, "C")   ? FILETYPE_CXX                    :
1393                                                         streq(suffix, "cc")  ? FILETYPE_CXX                    :
1394                                                         streq(suffix, "cp")  ? FILETYPE_CXX                    :
1395                                                         streq(suffix, "cpp") ? FILETYPE_CXX                    :
1396                                                         streq(suffix, "CPP") ? FILETYPE_CXX                    :
1397                                                         streq(suffix, "cxx") ? FILETYPE_CXX                    :
1398                                                         streq(suffix, "c++") ? FILETYPE_CXX                    :
1399                                                         streq(suffix, "ii")  ? FILETYPE_PREPROCESSED_CXX       :
1400                                                         streq(suffix, "h")   ? FILETYPE_C                      :
1401                                                         streq(suffix, "ir")  ? FILETYPE_IR                     :
1402                                                         streq(suffix, "o")   ? FILETYPE_OBJECT                 :
1403                                                         streq(suffix, "s")   ? FILETYPE_PREPROCESSED_ASSEMBLER :
1404                                                         streq(suffix, "so")  ? FILETYPE_OBJECT                 :
1405                                                         FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1406                                         }
1407                                 }
1408                         }
1409
1410                         file_list_entry_t *entry
1411                                 = obstack_alloc(&file_obst, sizeof(entry[0]));
1412                         memset(entry, 0, sizeof(entry[0]));
1413                         entry->name = arg;
1414                         entry->type = type;
1415
1416                         if (last_file != NULL) {
1417                                 last_file->next = entry;
1418                         } else {
1419                                 files = entry;
1420                         }
1421                         last_file = entry;
1422                 }
1423         }
1424
1425         if (help != HELP_NONE) {
1426                 print_help(argv[0], help);
1427                 return !argument_errors;
1428         }
1429
1430         if (print_file_name_file != NULL) {
1431                 print_file_name(print_file_name_file);
1432                 return EXIT_SUCCESS;
1433         }
1434         if (files == NULL) {
1435                 fprintf(stderr, "error: no input files specified\n");
1436                 argument_errors = true;
1437         }
1438
1439         if (argument_errors) {
1440                 usage(argv[0]);
1441                 return EXIT_FAILURE;
1442         }
1443
1444         /* apply some effects from switches */
1445         c_mode |= features_on;
1446         c_mode &= ~features_off;
1447         if (profile_generate) {
1448                 add_flag(&ldflags_obst, "-lfirmprof");
1449                 set_be_option("profilegenerate");
1450         }
1451         if (profile_use) {
1452                 set_be_option("profileuse");
1453         }
1454
1455         gen_firm_init();
1456         init_symbol_table();
1457         init_types();
1458         init_typehash();
1459         init_basic_types();
1460         init_lexer();
1461         init_ast();
1462         init_parser();
1463         init_ast2firm();
1464         init_mangle();
1465
1466         if (do_timing)
1467                 timer_init();
1468
1469         if (construct_dep_target) {
1470                 if (outname != 0 && strlen(outname) >= 2) {
1471                         get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1472                 } else {
1473                         get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1474                 }
1475         } else {
1476                 dep_target[0] = '\0';
1477         }
1478
1479         char outnamebuf[4096];
1480         if (outname == NULL) {
1481                 const char *filename = files->name;
1482
1483                 switch(mode) {
1484                 case BenchmarkParser:
1485                 case PrintAst:
1486                 case PrintFluffy:
1487                 case PrintJna:
1488                 case LexTest:
1489                 case PreprocessOnly:
1490                 case ParseOnly:
1491                         outname = "-";
1492                         break;
1493                 case Compile:
1494                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1495                         outname = outnamebuf;
1496                         break;
1497                 case CompileAssemble:
1498                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1499                         outname = outnamebuf;
1500                         break;
1501                 case CompileDump:
1502                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1503                                         ".vcg");
1504                         outname = outnamebuf;
1505                         break;
1506                 case CompileExportIR:
1507                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1508                         outname = outnamebuf;
1509                         break;
1510                 case CompileAssembleLink:
1511 #ifdef _WIN32
1512                         outname = "a.exe";
1513 #else
1514                         outname = "a.out";
1515 #endif
1516                         break;
1517                 }
1518         }
1519
1520         assert(outname != NULL);
1521
1522         FILE *out;
1523         if (streq(outname, "-")) {
1524                 out = stdout;
1525         } else {
1526                 out = fopen(outname, "w");
1527                 if (out == NULL) {
1528                         fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1529                                         strerror(errno));
1530                         return EXIT_FAILURE;
1531                 }
1532         }
1533
1534         file_list_entry_t *file;
1535         bool               already_constructed_firm = false;
1536         for (file = files; file != NULL; file = file->next) {
1537                 char        asm_tempfile[1024];
1538                 const char *filename = file->name;
1539                 filetype_t  filetype = file->type;
1540
1541                 if (filetype == FILETYPE_OBJECT)
1542                         continue;
1543
1544                 FILE *in = NULL;
1545                 if (mode == LexTest) {
1546                         if (in == NULL)
1547                                 in = open_file(filename);
1548                         lextest(in, filename);
1549                         fclose(in);
1550                         return EXIT_SUCCESS;
1551                 }
1552
1553                 FILE *preprocessed_in = NULL;
1554                 filetype_t next_filetype = filetype;
1555                 switch (filetype) {
1556                         case FILETYPE_C:
1557                                 next_filetype = FILETYPE_PREPROCESSED_C;
1558                                 goto preprocess;
1559                         case FILETYPE_CXX:
1560                                 next_filetype = FILETYPE_PREPROCESSED_CXX;
1561                                 goto preprocess;
1562                         case FILETYPE_ASSEMBLER:
1563                                 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1564                                 goto preprocess;
1565 preprocess:
1566                                 /* no support for input on FILE* yet */
1567                                 if (in != NULL)
1568                                         panic("internal compiler error: in for preprocessor != NULL");
1569
1570                                 preprocessed_in = preprocess(filename, filetype);
1571                                 if (mode == PreprocessOnly) {
1572                                         copy_file(out, preprocessed_in);
1573                                         int pp_result = pclose(preprocessed_in);
1574                                         fclose(out);
1575                                         /* remove output file in case of error */
1576                                         if (out != stdout && pp_result != EXIT_SUCCESS) {
1577                                                 unlink(outname);
1578                                         }
1579                                         return pp_result;
1580                                 }
1581
1582                                 in = preprocessed_in;
1583                                 filetype = next_filetype;
1584                                 break;
1585
1586                         default:
1587                                 break;
1588                 }
1589
1590                 FILE *asm_out;
1591                 if (mode == Compile) {
1592                         asm_out = out;
1593                 } else {
1594                         asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1595                 }
1596
1597                 if (in == NULL)
1598                         in = open_file(filename);
1599
1600                 /* preprocess and compile */
1601                 if (filetype == FILETYPE_PREPROCESSED_C) {
1602                         char const* invalid_mode;
1603                         switch (standard) {
1604                                 case STANDARD_ANSI:
1605                                 case STANDARD_C89:   c_mode = _C89;                break;
1606                                 /* TODO determine difference between these two */
1607                                 case STANDARD_C90:   c_mode = _C89;                break;
1608                                 case STANDARD_C99:   c_mode = _C89 | _C99;         break;
1609                                 case STANDARD_GNU89: c_mode = _C89 |        _GNUC; break;
1610
1611 default_c_warn:
1612                                         fprintf(stderr,
1613                                                         "warning: command line option \"-std=%s\" is not valid for C\n",
1614                                                         invalid_mode);
1615                                         /* FALLTHROUGH */
1616                                 case STANDARD_DEFAULT:
1617                                 case STANDARD_GNU99:   c_mode = _C89 | _C99 | _GNUC; break;
1618
1619                                 case STANDARD_CXX98:   invalid_mode = "c++98"; goto default_c_warn;
1620                                 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1621                         }
1622                         goto do_parsing;
1623                 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1624                         char const* invalid_mode;
1625                         switch (standard) {
1626                                 case STANDARD_C89:   invalid_mode = "c89";   goto default_cxx_warn;
1627                                 case STANDARD_C90:   invalid_mode = "c90";   goto default_cxx_warn;
1628                                 case STANDARD_C99:   invalid_mode = "c99";   goto default_cxx_warn;
1629                                 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1630                                 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1631
1632                                 case STANDARD_ANSI:
1633                                 case STANDARD_CXX98: c_mode = _CXX; break;
1634
1635 default_cxx_warn:
1636                                         fprintf(stderr,
1637                                                         "warning: command line option \"-std=%s\" is not valid for C++\n",
1638                                                         invalid_mode);
1639                                 case STANDARD_DEFAULT:
1640                                 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1641                         }
1642
1643 do_parsing:
1644                         c_mode |= features_on;
1645                         c_mode &= ~features_off;
1646
1647                         /* do the actual parsing */
1648                         ir_timer_t *t_parsing = ir_timer_new();
1649                         timer_register(t_parsing, "Frontend: Parsing");
1650                         timer_push(t_parsing);
1651                         init_tokens();
1652                         translation_unit_t *const unit = do_parsing(in, filename);
1653                         timer_pop(t_parsing);
1654
1655                         /* prints the AST even if errors occurred */
1656                         if (mode == PrintAst) {
1657                                 print_to_file(out);
1658                                 print_ast(unit);
1659                         }
1660
1661                         if (error_count > 0) {
1662                                 /* parsing failed because of errors */
1663                                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1664                                         warning_count);
1665                                 result = EXIT_FAILURE;
1666                                 continue;
1667                         } else if (warning_count > 0) {
1668                                 fprintf(stderr, "%u warning(s)\n", warning_count);
1669                         }
1670
1671                         if (in == preprocessed_in) {
1672                                 int pp_result = pclose(preprocessed_in);
1673                                 if (pp_result != EXIT_SUCCESS) {
1674                                         /* remove output file */
1675                                         if (out != stdout)
1676                                                 unlink(outname);
1677                                         return EXIT_FAILURE;
1678                                 }
1679                         }
1680
1681                         if (mode == BenchmarkParser) {
1682                                 return result;
1683                         } else if (mode == PrintFluffy) {
1684                                 write_fluffy_decls(out, unit);
1685                                 continue;
1686                         } else if (mode == PrintJna) {
1687                                 write_jna_decls(out, unit);
1688                                 continue;
1689                         }
1690
1691                         /* build the firm graph */
1692                         ir_timer_t *t_construct = ir_timer_new();
1693                         timer_register(t_construct, "Frontend: Graph construction");
1694                         timer_push(t_construct);
1695                         if (already_constructed_firm) {
1696                                 panic("compiling multiple files/translation units not possible");
1697                         }
1698                         translation_unit_to_firm(unit);
1699                         already_constructed_firm = true;
1700                         timer_pop(t_construct);
1701
1702 graph_built:
1703                         if (mode == ParseOnly) {
1704                                 continue;
1705                         }
1706
1707                         if (mode == CompileDump) {
1708                                 /* find irg */
1709                                 ident    *id     = new_id_from_str(dumpfunction);
1710                                 ir_graph *irg    = NULL;
1711                                 int       n_irgs = get_irp_n_irgs();
1712                                 for (int i = 0; i < n_irgs; ++i) {
1713                                         ir_graph *tirg   = get_irp_irg(i);
1714                                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
1715                                         if (irg_id == id) {
1716                                                 irg = tirg;
1717                                                 break;
1718                                         }
1719                                 }
1720
1721                                 if (irg == NULL) {
1722                                         fprintf(stderr, "No graph for function '%s' found\n",
1723                                                 dumpfunction);
1724                                         return EXIT_FAILURE;
1725                                 }
1726
1727                                 dump_ir_graph_file(out, irg);
1728                                 fclose(out);
1729                                 return EXIT_SUCCESS;
1730                         }
1731
1732                         if (mode == CompileExportIR) {
1733                                 fclose(out);
1734                                 ir_export(outname);
1735                                 return EXIT_SUCCESS;
1736                         }
1737
1738                         gen_firm_finish(asm_out, filename);
1739                         if (asm_out != out) {
1740                                 fclose(asm_out);
1741                         }
1742                 } else if (filetype == FILETYPE_IR) {
1743                         fclose(in);
1744                         ir_import(filename);
1745                         goto graph_built;
1746                 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1747                         copy_file(asm_out, in);
1748                         if (in == preprocessed_in) {
1749                                 int pp_result = pclose(preprocessed_in);
1750                                 if (pp_result != EXIT_SUCCESS) {
1751                                         /* remove output in error case */
1752                                         if (out != stdout)
1753                                                 unlink(outname);
1754                                         return pp_result;
1755                                 }
1756                         }
1757                         if (asm_out != out) {
1758                                 fclose(asm_out);
1759                         }
1760                 }
1761
1762                 if (mode == Compile)
1763                         continue;
1764
1765                 /* if we're here then we have preprocessed assembly */
1766                 filename = asm_tempfile;
1767                 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1768
1769                 /* assemble */
1770                 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1771                         char        temp[1024];
1772                         const char *filename_o;
1773                         if (mode == CompileAssemble) {
1774                                 fclose(out);
1775                                 filename_o = outname;
1776                         } else {
1777                                 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1778                                 fclose(tempf);
1779                                 filename_o = temp;
1780                         }
1781
1782                         assemble(filename_o, filename);
1783
1784                         size_t len = strlen(filename_o) + 1;
1785                         filename = obstack_copy(&file_obst, filename_o, len);
1786                         filetype = FILETYPE_OBJECT;
1787                 }
1788
1789                 /* ok we're done here, process next file */
1790                 file->name = filename;
1791                 file->type = filetype;
1792         }
1793
1794         if (result != EXIT_SUCCESS) {
1795                 if (out != stdout)
1796                         unlink(outname);
1797                 return result;
1798         }
1799
1800         /* link program file */
1801         if (mode == CompileAssembleLink) {
1802                 obstack_1grow(&ldflags_obst, '\0');
1803                 const char *flags = obstack_finish(&ldflags_obst);
1804
1805                 /* construct commandline */
1806                 const char *linker = getenv("CPARSER_LINK");
1807                 if (linker != NULL) {
1808                         obstack_printf(&file_obst, "%s ", linker);
1809                 } else {
1810                         if (target_triple != NULL)
1811                                 obstack_printf(&file_obst, "%s-", target_triple);
1812                         obstack_printf(&file_obst, "%s ", LINKER);
1813                 }
1814
1815                 for (file_list_entry_t *entry = files; entry != NULL;
1816                                 entry = entry->next) {
1817                         if (entry->type != FILETYPE_OBJECT)
1818                                 continue;
1819
1820                         add_flag(&file_obst, "%s", entry->name);
1821                 }
1822
1823                 add_flag(&file_obst, "-o");
1824                 add_flag(&file_obst, outname);
1825                 obstack_printf(&file_obst, "%s", flags);
1826                 obstack_1grow(&file_obst, '\0');
1827
1828                 char *commandline = obstack_finish(&file_obst);
1829
1830                 if (verbose) {
1831                         puts(commandline);
1832                 }
1833                 int err = system(commandline);
1834                 if (err != EXIT_SUCCESS) {
1835                         fprintf(stderr, "linker reported an error\n");
1836                         return EXIT_FAILURE;
1837                 }
1838         }
1839
1840         if (do_timing)
1841                 timer_term(stderr);
1842
1843         obstack_free(&cppflags_obst, NULL);
1844         obstack_free(&ldflags_obst, NULL);
1845         obstack_free(&asflags_obst, NULL);
1846         obstack_free(&file_obst, NULL);
1847
1848         exit_mangle();
1849         exit_ast2firm();
1850         exit_parser();
1851         exit_ast();
1852         exit_lexer();
1853         exit_typehash();
1854         exit_types();
1855         exit_tokens();
1856         exit_symbol_table();
1857         return EXIT_SUCCESS;
1858 }