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