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