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