s/puts("")/putchar(n)/.
[cparser] / main.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #ifdef _WIN32
32
33 #include <fcntl.h>
34 #include <io.h>
35
36 /* no eXecute on Win32 */
37 #define X_OK 0
38 #define W_OK 2
39 #define R_OK 4
40
41 #define O_RDWR          _O_RDWR
42 #define O_CREAT         _O_CREAT
43 #define O_EXCL          _O_EXCL
44 #define O_BINARY        _O_BINARY
45
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode)      _access(fname, mode)
48 #define mktemp(tmpl)             _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode)         _fdopen(fd, mode)
51 #define popen(cmd, mode)         _popen(cmd, mode)
52 #define pclose(file)             _pclose(file)
53 #define unlink(filename)         _unlink(filename)
54
55 #else
56 #include <unistd.h>
57 #define HAVE_MKSTEMP
58 #endif
59
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
62
63 #include "lexer.h"
64 #include "token_t.h"
65 #include "types.h"
66 #include "type_hash.h"
67 #include "parser.h"
68 #include "type_t.h"
69 #include "ast2firm.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "adt/error.h"
75 #include "write_fluffy.h"
76 #include "write_caml.h"
77 #include "revision.h"
78 #include "warning.h"
79 #include "mangle.h"
80
81 #ifndef PREPROCESSOR
82 #ifdef __APPLE__
83 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
84 #else
85 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
86 #endif
87 #endif
88
89 #ifndef LINKER
90 #define LINKER    "gcc -m32"
91 #endif
92
93 #ifndef ASSEMBLER
94 #ifdef __APPLE__
95 #define ASSEMBLER "gcc -c -xassembler"
96 #else
97 #define ASSEMBLER "as --32"
98 #endif
99 #endif
100
101 /** The current c mode/dialect. */
102 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
103
104 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
105 unsigned int machine_size = 32;
106
107 /** true if the char type is signed. */
108 bool char_is_signed = true;
109
110 /** true for strict language checking. */
111 bool strict_mode = false;
112
113 /** use builtins for some libc functions */
114 bool use_builtins = false;
115
116 /** we have extern function with const attribute. */
117 bool have_const_functions = false;
118
119 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
120
121 /* to switch on printing of implicit casts */
122 extern bool print_implicit_casts;
123
124 /* to switch on printing of parenthesis to indicate operator precedence */
125 extern bool print_parenthesis;
126
127 static int             verbose;
128 static struct obstack  cppflags_obst, ldflags_obst;
129 static char            dep_target[1024];
130 static const char     *outname;
131
132 typedef struct file_list_entry_t file_list_entry_t;
133
134 typedef enum filetype_t {
135         FILETYPE_AUTODETECT,
136         FILETYPE_C,
137         FILETYPE_PREPROCESSED_C,
138         FILETYPE_CXX,
139         FILETYPE_PREPROCESSED_CXX,
140         FILETYPE_ASSEMBLER,
141         FILETYPE_PREPROCESSED_ASSEMBLER,
142         FILETYPE_OBJECT,
143         FILETYPE_UNKNOWN
144 } filetype_t;
145
146 struct file_list_entry_t {
147         const char  *name; /**< filename or NULL for stdin */
148         filetype_t   type;
149         file_list_entry_t *next;
150 };
151
152 static file_list_entry_t *temp_files;
153
154 #if defined(_DEBUG) || defined(FIRM_DEBUG)
155 /**
156  * Debug printf implementation.
157  *
158  * @param fmt  printf style format parameter
159  */
160 void dbg_printf(const char *fmt, ...)
161 {
162         va_list list;
163
164         if (firm_dump.debug_print) {
165                 va_start(list, fmt);
166                 vprintf(fmt, list);
167                 va_end(list);
168         }  /* if */
169 }
170 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
171
172 static void initialize_firm(void)
173 {
174         firm_early_init();
175
176         dump_consts_local(1);
177         dump_keepalive_edges(1);
178 }
179
180 static void get_output_name(char *buf, size_t buflen, const char *inputname,
181                             const char *newext)
182 {
183         size_t last_dot = 0xffffffff;
184         size_t i = 0;
185
186         if (inputname == NULL) {
187                 snprintf(buf, buflen, "a%s", newext);
188                 return;
189         }
190
191         for (const char *c = inputname; *c != 0; ++c) {
192                 if (*c == '.')
193                         last_dot = i;
194                 ++i;
195         }
196         if (last_dot == 0xffffffff)
197                 last_dot = i;
198
199         if (last_dot >= buflen)
200                 panic("filename too long");
201         memcpy(buf, inputname, last_dot);
202
203         size_t extlen = strlen(newext) + 1;
204         if (extlen + last_dot >= buflen)
205                 panic("filename too long");
206         memcpy(buf+last_dot, newext, extlen);
207 }
208
209 #include "builtins.h"
210
211 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
212 {
213         start_parsing();
214
215         if (use_builtins) {
216                 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
217                 parse();
218         }
219
220         lexer_open_stream(in, input_name);
221         parse();
222
223         translation_unit_t *unit = finish_parsing();
224         return unit;
225 }
226
227 static void lextest(FILE *in, const char *fname)
228 {
229         lexer_open_stream(in, fname);
230
231         do {
232                 lexer_next_preprocessing_token();
233                 print_token(stdout, &lexer_token);
234                 putchar('\n');
235         } while (lexer_token.type != T_EOF);
236 }
237
238 static void add_flag(struct obstack *obst, const char *format, ...)
239 {
240         obstack_1grow(obst, ' ');
241 #ifdef _WIN32
242         obstack_1grow(obst, '"');
243         obstack_vprintf(obst, format, ap);
244         obstack_1grow(obst, '"');
245 #else
246         char buf[4096];
247         va_list ap;
248         va_start(ap, format);
249         vsnprintf(buf, sizeof(buf), format, ap);
250         va_end(ap);
251
252         /* escape stuff... */
253         for (char *c = buf; *c != '\0'; ++c) {
254                 switch(*c) {
255                 case '"':
256                 case '\'':
257                 case '`':
258                 case ' ':
259                 case '\t':
260                 case '\n':
261                 case '\r':
262                 case '\\':
263                 case '$':
264                 case '(':
265                 case ')':
266                         obstack_1grow(obst, '\\');
267                         /* FALLTHROUGH */
268                 default:
269                         obstack_1grow(obst, *c);
270                         break;
271                 }
272         }
273 #endif
274 }
275
276 static const char *type_to_string(type_t *type)
277 {
278         assert(type->kind == TYPE_ATOMIC);
279         return get_atomic_kind_name(type->atomic.akind);
280 }
281
282 static FILE *preprocess(const char *fname)
283 {
284         obstack_1grow(&cppflags_obst, '\0');
285         const char *flags = obstack_finish(&cppflags_obst);
286
287         obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
288
289         /* setup default defines */
290         add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
291         add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
292         add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
293         add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
294
295         /* handle dependency generation */
296         if (dep_target[0] != '\0') {
297                 add_flag(&cppflags_obst, "-MF");
298                 add_flag(&cppflags_obst, dep_target);
299                 if (outname != NULL) {
300                                 add_flag(&cppflags_obst, "-MQ");
301                                 add_flag(&cppflags_obst, outname);
302                 }
303         }
304         if (flags[0] != '\0') {
305                 obstack_printf(&cppflags_obst, " %s", flags);
306         }
307         add_flag(&cppflags_obst, fname);
308
309         obstack_1grow(&cppflags_obst, '\0');
310         const char *buf = obstack_finish(&cppflags_obst);
311         if (verbose) {
312                 puts(buf);
313         }
314
315         FILE *f = popen(buf, "r");
316         if (f == NULL) {
317                 fprintf(stderr, "invoking preprocessor failed\n");
318                 exit(1);
319         }
320
321         return f;
322 }
323
324 static void assemble(const char *out, const char *in)
325 {
326         char buf[4096];
327
328         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
329         if (verbose) {
330                 puts(buf);
331         }
332
333         int err = system(buf);
334         if (err != 0) {
335                 fprintf(stderr, "assembler reported an error\n");
336                 exit(1);
337         }
338 }
339
340 static const char *try_dir(const char *dir)
341 {
342         if (dir == NULL)
343                 return dir;
344         if (access(dir, R_OK | W_OK | X_OK) == 0)
345                 return dir;
346         return NULL;
347 }
348
349 static const char *get_tempdir(void)
350 {
351         static const char *tmpdir = NULL;
352
353         if (tmpdir != NULL)
354                 return tmpdir;
355
356         if (tmpdir == NULL)
357                 tmpdir = try_dir(getenv("TMPDIR"));
358         if (tmpdir == NULL)
359                 tmpdir = try_dir(getenv("TMP"));
360         if (tmpdir == NULL)
361                 tmpdir = try_dir(getenv("TEMP"));
362
363 #ifdef P_tmpdir
364         if (tmpdir == NULL)
365                 tmpdir = try_dir(P_tmpdir);
366 #endif
367
368         if (tmpdir == NULL)
369                 tmpdir = try_dir("/var/tmp");
370         if (tmpdir == NULL)
371                 tmpdir = try_dir("/usr/tmp");
372         if (tmpdir == NULL)
373                 tmpdir = try_dir("/tmp");
374
375         if (tmpdir == NULL)
376                 tmpdir = ".";
377
378         return tmpdir;
379 }
380
381 #ifndef HAVE_MKSTEMP
382 /* cheap and nasty mkstemp replacement */
383 static int mkstemp(char *templ)
384 {
385         mktemp(templ);
386         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
387 }
388 #endif
389
390 /**
391  * an own version of tmpnam, which: writes in a buffer, emits no warnings
392  * during linking (like glibc/gnu ld do for tmpnam)...
393  */
394 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
395 {
396         const char *tempdir = get_tempdir();
397
398         snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
399
400         int fd = mkstemp(buffer);
401         if (fd == -1) {
402                 fprintf(stderr, "couldn't create temporary file: %s\n",
403                         strerror(errno));
404                 exit(1);
405         }
406         FILE *out = fdopen(fd, "w");
407         if (out == NULL) {
408                 fprintf(stderr, "couldn't create temporary file FILE*\n");
409                 exit(1);
410         }
411
412         file_list_entry_t *entry = xmalloc(sizeof(*entry));
413         memset(entry, 0, sizeof(*entry));
414
415         size_t  name_len = strlen(buffer) + 1;
416         char   *name     = malloc(name_len);
417         memcpy(name, buffer, name_len);
418         entry->name      = name;
419
420         entry->next = temp_files;
421         temp_files  = entry;
422
423         return out;
424 }
425
426 static void free_temp_files(void)
427 {
428         file_list_entry_t *entry = temp_files;
429         file_list_entry_t *next;
430         for ( ; entry != NULL; entry = next) {
431                 next = entry->next;
432
433                 unlink(entry->name);
434                 free((char*) entry->name);
435                 free(entry);
436         }
437         temp_files = NULL;
438 }
439
440 /**
441  * Do the necessary lowering for compound parameters.
442  */
443 void lower_compound_params(void)
444 {
445         lower_params_t params;
446
447         params.def_ptr_alignment    = 4;
448         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
449         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
450         params.find_pointer_type    = NULL;
451         params.ret_compound_in_regs = NULL;
452         lower_calls_with_compounds(&params);
453 }
454
455 typedef enum compile_mode_t {
456         BenchmarkParser,
457         PreprocessOnly,
458         ParseOnly,
459         Compile,
460         CompileDump,
461         CompileAssemble,
462         CompileAssembleLink,
463         LexTest,
464         PrintAst,
465         PrintFluffy,
466         PrintCaml
467 } compile_mode_t;
468
469 static void usage(const char *argv0)
470 {
471         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
472 }
473
474 static void print_cparser_version(void) {
475         firm_version_t ver;
476         firm_get_version(&ver);
477
478         printf("cparser (%s) using libFirm (%u.%u",
479                 cparser_REVISION, ver.major, ver.minor);
480         if (ver.revision[0] != 0) {
481                 putchar(' ');
482                 fputs(ver.revision, stdout);
483         }
484         if (ver.build[0] != 0) {
485                 putchar(' ');
486                 fputs(ver.build, stdout);
487         }
488         puts(")\n");
489 }
490
491 static void set_be_option(const char *arg)
492 {
493         int res = firm_be_option(arg);
494         (void) res;
495         assert(res);
496 }
497
498 static void set_option(const char *arg)
499 {
500         int res = firm_option(arg);
501         (void) res;
502         assert(res);
503 }
504
505 static void copy_file(FILE *dest, FILE *input)
506 {
507         char buf[16384];
508
509         while (!feof(input) && !ferror(dest)) {
510                 size_t read = fread(buf, 1, sizeof(buf), input);
511                 if (fwrite(buf, 1, read, dest) != read) {
512                         perror("couldn't write output");
513                 }
514         }
515 }
516
517 static inline bool streq(char const* a, char const* b)
518 {
519         return strcmp(a, b) == 0;
520 }
521
522 static inline bool strstart(char const* str, char const* start)
523 {
524         do {
525                 if (*start == '\0')
526                         return true;
527         } while (*str++ == *start++);
528         return false;
529 }
530
531 static FILE *open_file(const char *filename)
532 {
533         if (streq(filename, "-")) {
534                 return stdin;
535         }
536
537         FILE *in = fopen(filename, "r");
538         if (in == NULL) {
539                 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
540                                 strerror(errno));
541                 exit(1);
542         }
543
544         return in;
545 }
546
547 static filetype_t get_filetype_from_string(const char *string)
548 {
549         if (streq(string, "c") || streq(string, "c-header"))
550                 return FILETYPE_C;
551         if (streq(string, "c++") || streq(string, "c++-header"))
552                 return FILETYPE_CXX;
553         if (streq(string, "assembler"))
554                 return FILETYPE_PREPROCESSED_ASSEMBLER;
555         if (streq(string, "assembler-with-cpp"))
556                 return FILETYPE_ASSEMBLER;
557         if (streq(string, "none"))
558                 return FILETYPE_AUTODETECT;
559
560         return FILETYPE_UNKNOWN;
561 }
562
563 static void init_os_support(void)
564 {
565         /* OS option must be set to the backend */
566         switch (firm_opt.os_support) {
567         case OS_SUPPORT_MINGW:
568                 set_be_option("ia32-gasmode=mingw");
569                 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
570                 break;
571         case OS_SUPPORT_LINUX:
572                 set_be_option("ia32-gasmode=elf");
573                 break;
574         case OS_SUPPORT_MACHO:
575                 set_be_option("ia32-gasmode=macho");
576                 set_be_option("ia32-stackalign=4");
577                 set_be_option("pic");
578                 break;
579         }
580 }
581
582 typedef enum lang_standard_t {
583         STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
584         STANDARD_ANSI,    /* c89 (for C) or c++98 (for C++) */
585         STANDARD_C89,     /* ISO C90 (sic) */
586         STANDARD_C90,     /* ISO C90 as modified in amendment 1 */
587         STANDARD_C99,     /* ISO C99 */
588         STANDARD_GNU89,   /* ISO C90 plus GNU extensions (including some C99) */
589         STANDARD_GNU99,   /* ISO C99 plus GNU extensions */
590         STANDARD_CXX98,   /* ISO C++ 1998 plus amendments */
591         STANDARD_GNUXX98  /* ISO C++ 1998 plus amendments and GNU extensions */
592 } lang_standard_t;
593
594 int main(int argc, char **argv)
595 {
596         initialize_firm();
597
598         const char        *dumpfunction         = NULL;
599         compile_mode_t     mode                 = CompileAssembleLink;
600         int                opt_level            = 1;
601         int                result               = EXIT_SUCCESS;
602         char               cpu_arch[16]         = "ia32";
603         file_list_entry_t *files                = NULL;
604         file_list_entry_t *last_file            = NULL;
605         bool               construct_dep_target = false;
606         struct obstack     file_obst;
607
608         atexit(free_temp_files);
609
610         /* hack for now... */
611         if (strstr(argv[0], "pptest") != NULL) {
612                 extern int pptest_main(int argc, char **argv);
613                 return pptest_main(argc, argv);
614         }
615
616         obstack_init(&cppflags_obst);
617         obstack_init(&ldflags_obst);
618         obstack_init(&file_obst);
619
620 #define GET_ARG_AFTER(def, args)                                             \
621         def = &arg[sizeof(args)-1];                                              \
622         if (def[0] == '\0') {                                                     \
623                 ++i;                                                                 \
624                 if (i >= argc) {                                                      \
625                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
626                         argument_errors = true;                                          \
627                         break;                                                           \
628                 }                                                                    \
629                 def = argv[i];                                                       \
630                 if (def[0] == '-' && def[1] != '\0') {                                \
631                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
632                         argument_errors = true;                                          \
633                         continue;                                                        \
634                 }                                                                    \
635         }
636
637 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
638
639         /* early options parsing (find out optimisation level and OS) */
640         for (int i = 1; i < argc; ++i) {
641                 const char *arg = argv[i];
642                 if (arg[0] != '-')
643                         continue;
644
645                 const char *option = &arg[1];
646                 if (option[0] == 'O') {
647                         sscanf(&option[1], "%d", &opt_level);
648                 }
649                 if (strcmp(arg, "-fwin32") == 0) {
650                         firm_opt.os_support = OS_SUPPORT_MINGW;
651                 } else if (strcmp(arg, "-fmac") == 0) {
652                         firm_opt.os_support = OS_SUPPORT_MACHO;
653                 } else if (strcmp(arg, "-flinux") == 0) {
654                         firm_opt.os_support = OS_SUPPORT_LINUX;
655                 }
656         }
657
658         /* set target/os specific stuff */
659         init_os_support();
660
661         /* apply optimisation level */
662         switch(opt_level) {
663         case 0:
664                 set_option("no-opt");
665                 break;
666         case 1:
667                 set_option("no-inline");
668                 break;
669         default:
670         case 4:
671                 set_option("strict-aliasing");
672                 /* use_builtins = true; */
673                 /* fallthrough */
674         case 3:
675                 set_option("cond-eval");
676                 set_option("if-conv");
677                 /* fallthrough */
678         case 2:
679                 set_option("inline");
680                 set_option("deconv");
681                 set_be_option("omitfp");
682                 break;
683         }
684
685         /* parse rest of options */
686         lang_standard_t standard        = STANDARD_DEFAULT;
687         unsigned        features_on     = 0;
688         unsigned        features_off    = 0;
689         filetype_t      forced_filetype = FILETYPE_AUTODETECT;
690         bool            help_displayed  = false;
691         bool            argument_errors = false;
692         for (int i = 1; i < argc; ++i) {
693                 const char *arg = argv[i];
694                 if (arg[0] == '-' && arg[1] != '\0') {
695                         /* an option */
696                         const char *option = &arg[1];
697                         if (option[0] == 'o') {
698                                 GET_ARG_AFTER(outname, "-o");
699                         } else if (option[0] == 'g') {
700                                 set_be_option("debuginfo=stabs");
701                                 set_be_option("omitfp=no");
702                                 set_be_option("ia32-nooptcc=yes");
703                         } else if (SINGLE_OPTION('c')) {
704                                 mode = CompileAssemble;
705                         } else if (SINGLE_OPTION('E')) {
706                                 mode = PreprocessOnly;
707                         } else if (SINGLE_OPTION('S')) {
708                                 mode = Compile;
709                         } else if (option[0] == 'O') {
710                                 continue;
711                         } else if (option[0] == 'I') {
712                                 const char *opt;
713                                 GET_ARG_AFTER(opt, "-I");
714                                 add_flag(&cppflags_obst, "-I%s", opt);
715                         } else if (option[0] == 'D') {
716                                 const char *opt;
717                                 GET_ARG_AFTER(opt, "-D");
718                                 add_flag(&cppflags_obst, "-D%s", opt);
719                         } else if (option[0] == 'U') {
720                                 const char *opt;
721                                 GET_ARG_AFTER(opt, "-U");
722                                 add_flag(&cppflags_obst, "-U%s", opt);
723                         } else if (option[0] == 'l') {
724                                 const char *opt;
725                                 GET_ARG_AFTER(opt, "-l");
726                                 add_flag(&ldflags_obst, "-l%s", opt);
727                         } else if (option[0] == 'L') {
728                                 const char *opt;
729                                 GET_ARG_AFTER(opt, "-L");
730                                 add_flag(&ldflags_obst, "-L%s", opt);
731                         } else if (SINGLE_OPTION('v')) {
732                                 verbose = 1;
733                         } else if (SINGLE_OPTION('w')) {
734                                 memset(&warning, 0, sizeof(warning));
735                         } else if (option[0] == 'x') {
736                                 const char *opt;
737                                 GET_ARG_AFTER(opt, "-x");
738                                 forced_filetype = get_filetype_from_string(opt);
739                                 if (forced_filetype == FILETYPE_UNKNOWN) {
740                                         fprintf(stderr, "Unknown language '%s'\n", opt);
741                                         argument_errors = true;
742                                 }
743                         } else if (streq(option, "M")) {
744                                 mode = PreprocessOnly;
745                                 add_flag(&cppflags_obst, "-M");
746                         } else if (streq(option, "MMD") ||
747                                    streq(option, "MD")) {
748                             construct_dep_target = true;
749                                 add_flag(&cppflags_obst, "-%s", option);
750                         } else if (streq(option, "MM")  ||
751                                    streq(option, "MP")) {
752                                 add_flag(&cppflags_obst, "-%s", option);
753                         } else if (streq(option, "MT") ||
754                                    streq(option, "MQ") ||
755                                    streq(option, "MF")) {
756                                 const char *opt;
757                                 GET_ARG_AFTER(opt, "-MT");
758                                 add_flag(&cppflags_obst, "-%s", option);
759                                 add_flag(&cppflags_obst, "%s", opt);
760                         } else if (streq(option, "pipe")) {
761                                 /* here for gcc compatibility */
762                         } else if (option[0] == 'f') {
763                                 char const *orig_opt;
764                                 GET_ARG_AFTER(orig_opt, "-f");
765
766                                 if (strstart(orig_opt, "align-loops=") ||
767                                     strstart(orig_opt, "align-jumps=") ||
768                                     strstart(orig_opt, "align-functions=")) {
769                                         fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
770                                 } else {
771                                         char const *opt         = orig_opt;
772                                         bool        truth_value = true;
773                                         if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
774                                                 truth_value = false;
775                                                 opt += 3;
776                                         }
777
778                                         if (streq(opt, "dollars-in-identifiers")) {
779                                                 allow_dollar_in_symbol = truth_value;
780                                         } if (streq(opt, "builtins")) {
781                                                 use_builtins = truth_value;
782                                         } else if (streq(opt, "short-wchar")) {
783                                                 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
784                                                         : ATOMIC_TYPE_INT;
785                                         } else if (streq(opt, "syntax-only")) {
786                                                 mode = truth_value ? ParseOnly : CompileAssembleLink;
787                                         } else if (streq(opt, "omit-frame-pointer")) {
788                                                 set_be_option(truth_value ? "omitfp" : "omitfp=no");
789                                         } else if (streq(opt, "strength-reduce")) {
790                                                 firm_option(truth_value ? "strength-red" : "no-strength-red");
791                                         } else if (streq(opt, "fast-math")               ||
792                                                    streq(opt, "jump-tables")             ||
793                                                    streq(opt, "unroll-loops")            ||
794                                                    streq(opt, "expensive-optimizations") ||
795                                                    streq(opt, "common")                  ||
796                                                    streq(opt, "PIC")                     ||
797                                                    streq(opt, "align-loops")             ||
798                                                    streq(opt, "align-jumps")             ||
799                                                    streq(opt, "align-functions")) {
800                                                 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
801                                         } else {
802                                                 int res = firm_option(orig_opt);
803                                                 if (res == 0) {
804                                                         fprintf(stderr, "error: unknown Firm option '-f%s'\n",
805                                                                 orig_opt);
806                                                         argument_errors = true;
807                                                         continue;
808                                                 } else if (res == -1) {
809                                                         help_displayed = true;
810                                                 }
811                                         }
812                                 }
813                         } else if (option[0] == 'b') {
814                                 const char *opt;
815                                 GET_ARG_AFTER(opt, "-b");
816                                 int res = firm_be_option(opt);
817                                 if (res == 0) {
818                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
819                                                 opt);
820                                         argument_errors = true;
821                                 } else if (res == -1) {
822                                         help_displayed = true;
823                                 } else if (strstart(opt, "isa=")) {
824                                         strncpy(cpu_arch, opt, sizeof(cpu_arch));
825                                 }
826                         } else if (option[0] == 'W') {
827                                 if (strstart(option + 1, "l,")) // a gcc-style linker option
828                                 {
829                                         const char *opt;
830                                         GET_ARG_AFTER(opt, "-Wl,");
831                                         add_flag(&ldflags_obst, "-Wl,%s", opt);
832                                 }
833                                 else set_warning_opt(&option[1]);
834                         } else if (option[0] == 'm') {
835                                 /* -m options */
836                                 const char *opt;
837                                 char arch_opt[64];
838
839                                 GET_ARG_AFTER(opt, "-m");
840                                 if (strstart(opt, "arch=")) {
841                                         GET_ARG_AFTER(opt, "-march=");
842                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
843                                         int res = firm_be_option(arch_opt);
844                                         if (res == 0)
845                                                 argument_errors = true;
846                                         else {
847                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
848                                                 int res = firm_be_option(arch_opt);
849                                                 if (res == 0)
850                                                         argument_errors = true;
851                                         }
852                                 } else if (strstart(opt, "tune=")) {
853                                         GET_ARG_AFTER(opt, "-mtune=");
854                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
855                                         int res = firm_be_option(arch_opt);
856                                         if (res == 0)
857                                                 argument_errors = true;
858                                 } else if (strstart(opt, "cpu=")) {
859                                         GET_ARG_AFTER(opt, "-mcpu=");
860                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
861                                         int res = firm_be_option(arch_opt);
862                                         if (res == 0)
863                                                 argument_errors = true;
864                                 } else if (strstart(opt, "fpmath=")) {
865                                         GET_ARG_AFTER(opt, "-mfpmath=");
866                                         if (streq(opt, "387"))
867                                                 opt = "x87";
868                                         else if (streq(opt, "sse"))
869                                                 opt = "sse2";
870                                         else {
871                                                 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
872                                                 argument_errors = true;
873                                         }
874                                         if (!argument_errors) {
875                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
876                                                 int res = firm_be_option(arch_opt);
877                                                 if (res == 0)
878                                                         argument_errors = true;
879                                         }
880                                 } else if (strstart(opt, "preferred-stack-boundary=")) {
881                                         GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
882                                         snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
883                                         int res = firm_be_option(arch_opt);
884                                         if (res == 0)
885                                                 argument_errors = true;
886                                 } else if (streq(opt, "omit-leaf-frame-pointer")) {
887                                         set_be_option("omitleaffp=1");
888                                 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
889                                         set_be_option("omitleaffp=0");
890                                 } else {
891                                         char *endptr;
892                                         long int value = strtol(opt, &endptr, 10);
893                                         if (*endptr != '\0') {
894                                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
895                                                 argument_errors = true;
896                                         }
897                                         if (value != 16 && value != 32 && value != 64) {
898                                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
899                                                 argument_errors = true;
900                                         } else {
901                                                 machine_size = (unsigned int)value;
902                                         }
903                                 }
904                         } else if (streq(option, "pg")) {
905                                 set_be_option("gprof");
906                                 add_flag(&ldflags_obst, "-pg");
907                         } else if (streq(option, "pedantic") ||
908                                    streq(option, "ansi")) {
909                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
910                         } else if (streq(option, "shared")) {
911                                 add_flag(&ldflags_obst, "-shared");
912                         } else if (strstart(option, "std=")) {
913                                 const char *const o = &option[4];
914                                 standard =
915                                         streq(o, "c++")            ? STANDARD_CXX98   :
916                                         streq(o, "c++98")          ? STANDARD_CXX98   :
917                                         streq(o, "c89")            ? STANDARD_C89     :
918                                         streq(o, "c99")            ? STANDARD_C99     :
919                                         streq(o, "c9x")            ? STANDARD_C99     : // deprecated
920                                         streq(o, "gnu++98")        ? STANDARD_GNUXX98 :
921                                         streq(o, "gnu89")          ? STANDARD_GNU89   :
922                                         streq(o, "gnu99")          ? STANDARD_GNU99   :
923                                         streq(o, "gnu9x")          ? STANDARD_GNU99   : // deprecated
924                                         streq(o, "iso9899:1990")   ? STANDARD_C89     :
925                                         streq(o, "iso9899:199409") ? STANDARD_C90     :
926                                         streq(o, "iso9899:1999")   ? STANDARD_C99     :
927                                         streq(o, "iso9899:199x")   ? STANDARD_C99     : // deprecated
928                                         (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
929                         } else if (streq(option, "version")) {
930                                 print_cparser_version();
931                         } else if (option[0] == '-') {
932                                 /* double dash option */
933                                 ++option;
934                                 if (streq(option, "gcc")) {
935                                         features_on  |=  _GNUC;
936                                         features_off &= ~_GNUC;
937                                 } else if (streq(option, "no-gcc")) {
938                                         features_on  &= ~_GNUC;
939                                         features_off |=  _GNUC;
940                                 } else if (streq(option, "ms")) {
941                                         features_on  |=  _MS;
942                                         features_off &= ~_MS;
943                                 } else if (streq(option, "no-ms")) {
944                                         features_on  &= ~_MS;
945                                         features_off |=  _MS;
946                                 } else if (streq(option, "signed-chars")) {
947                                         char_is_signed = true;
948                                 } else if (streq(option, "unsigned-chars")) {
949                                         char_is_signed = false;
950                                 } else if (streq(option, "strict")) {
951                                         strict_mode = true;
952                                 } else if (streq(option, "lextest")) {
953                                         mode = LexTest;
954                                 } else if (streq(option, "benchmark")) {
955                                         mode = BenchmarkParser;
956                                 } else if (streq(option, "print-ast")) {
957                                         mode = PrintAst;
958                                 } else if (streq(option, "print-implicit-cast")) {
959                                         print_implicit_casts = true;
960                                 } else if (streq(option, "print-parenthesis")) {
961                                         print_parenthesis = true;
962                                 } else if (streq(option, "print-fluffy")) {
963                                         mode = PrintFluffy;
964                                 } else if (streq(option, "print-caml")) {
965                                         mode = PrintCaml;
966                                 } else if (streq(option, "version")) {
967                                         print_cparser_version();
968                                         exit(EXIT_SUCCESS);
969                                 } else if (streq(option, "dump-function")) {
970                                         ++i;
971                                         if (i >= argc) {
972                                                 fprintf(stderr, "error: "
973                                                         "expected argument after '--dump-function'\n");
974                                                 argument_errors = true;
975                                                 break;
976                                         }
977                                         dumpfunction = argv[i];
978                                         mode         = CompileDump;
979                                 } else {
980                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
981                                         argument_errors = true;
982                                 }
983                         } else {
984                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
985                                 argument_errors = true;
986                         }
987                 } else {
988                         filetype_t type = forced_filetype;
989                         if (type == FILETYPE_AUTODETECT) {
990                                 if (streq(arg, "-")) {
991                                         /* - implicitly means C source file */
992                                         type = FILETYPE_C;
993                                 } else {
994                                         const char *suffix = strrchr(arg, '.');
995                                         /* Ensure there is at least one char before the suffix */
996                                         if (suffix != NULL && suffix != arg) {
997                                                 ++suffix;
998                                                 type =
999                                                         streq(suffix, "S")   ? FILETYPE_ASSEMBLER              :
1000                                                         streq(suffix, "a")   ? FILETYPE_OBJECT                 :
1001                                                         streq(suffix, "c")   ? FILETYPE_C                      :
1002                                                         streq(suffix, "cc")  ? FILETYPE_CXX                    :
1003                                                         streq(suffix, "cpp") ? FILETYPE_CXX                    :
1004                                                         streq(suffix, "cxx") ? FILETYPE_CXX                    :
1005                                                         streq(suffix, "h")   ? FILETYPE_C                      :
1006                                                         streq(suffix, "o")   ? FILETYPE_OBJECT                 :
1007                                                         streq(suffix, "s")   ? FILETYPE_PREPROCESSED_ASSEMBLER :
1008                                                         streq(suffix, "so")  ? FILETYPE_OBJECT                 :
1009                                                         FILETYPE_AUTODETECT;
1010                                         }
1011                                 }
1012
1013                                 if (type == FILETYPE_AUTODETECT) {
1014                                         fprintf(stderr, "'%s': file format not recognized\n", arg);
1015                                         continue;
1016                                 }
1017                         }
1018
1019                         file_list_entry_t *entry
1020                                 = obstack_alloc(&file_obst, sizeof(entry[0]));
1021                         memset(entry, 0, sizeof(entry[0]));
1022                         entry->name = arg;
1023                         entry->type = type;
1024
1025                         if (last_file != NULL) {
1026                                 last_file->next = entry;
1027                         } else {
1028                                 files = entry;
1029                         }
1030                         last_file = entry;
1031                 }
1032         }
1033
1034         if (files == NULL) {
1035                 fprintf(stderr, "error: no input files specified\n");
1036                 argument_errors = true;
1037         }
1038
1039         if (help_displayed) {
1040                 return !argument_errors;
1041         }
1042         if (argument_errors) {
1043                 usage(argv[0]);
1044                 return 1;
1045         }
1046
1047         /* we do the lowering in ast2firm */
1048         firm_opt.lower_bitfields = FALSE;
1049
1050         gen_firm_init();
1051         init_symbol_table();
1052         init_types();
1053         init_typehash();
1054         init_basic_types();
1055         init_lexer();
1056         init_ast();
1057         init_parser();
1058         init_ast2firm();
1059         init_mangle();
1060
1061         if (construct_dep_target) {
1062                 if (outname != 0 && strlen(outname) >= 2) {
1063                         get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1064                 } else {
1065                         get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1066                 }
1067         } else {
1068                 dep_target[0] = '\0';
1069         }
1070
1071         char outnamebuf[4096];
1072         if (outname == NULL) {
1073                 const char *filename = files->name;
1074
1075                 switch(mode) {
1076                 case BenchmarkParser:
1077                 case PrintAst:
1078                 case PrintFluffy:
1079                 case PrintCaml:
1080                 case LexTest:
1081                 case PreprocessOnly:
1082                 case ParseOnly:
1083                         outname = "-";
1084                         break;
1085                 case Compile:
1086                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1087                         outname = outnamebuf;
1088                         break;
1089                 case CompileAssemble:
1090                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1091                         outname = outnamebuf;
1092                         break;
1093                 case CompileDump:
1094                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1095                                         ".vcg");
1096                         outname = outnamebuf;
1097                         break;
1098                 case CompileAssembleLink:
1099 #ifdef _WIN32
1100                         outname = "a.exe";
1101 #else
1102                         outname = "a.out";
1103 #endif
1104                         break;
1105                 }
1106         }
1107
1108         assert(outname != NULL);
1109
1110         FILE *out;
1111         if (streq(outname, "-")) {
1112                 out = stdout;
1113         } else {
1114                 out = fopen(outname, "w");
1115                 if (out == NULL) {
1116                         fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1117                                         strerror(errno));
1118                         return 1;
1119                 }
1120         }
1121
1122         file_list_entry_t *file;
1123         for (file = files; file != NULL; file = file->next) {
1124                 char        asm_tempfile[1024];
1125                 const char *filename = file->name;
1126                 filetype_t  filetype = file->type;
1127
1128                 if (filetype == FILETYPE_OBJECT)
1129                         continue;
1130
1131                 FILE *in = NULL;
1132                 if (mode == LexTest) {
1133                         if (in == NULL)
1134                                 in = open_file(filename);
1135                         lextest(in, filename);
1136                         fclose(in);
1137                         exit(EXIT_SUCCESS);
1138                 }
1139
1140                 FILE *preprocessed_in = NULL;
1141                 switch (filetype) {
1142                         case FILETYPE_C:         filetype = FILETYPE_PREPROCESSED_C;         goto preprocess;
1143                         case FILETYPE_CXX:       filetype = FILETYPE_PREPROCESSED_CXX;       goto preprocess;
1144                         case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1145 preprocess:
1146                                 /* no support for input on FILE* yet */
1147                                 if (in != NULL)
1148                                         panic("internal compiler error: in for preprocessor != NULL");
1149
1150                                 preprocessed_in = preprocess(filename);
1151                                 if (mode == PreprocessOnly) {
1152                                         copy_file(out, preprocessed_in);
1153                                         int result = pclose(preprocessed_in);
1154                                         fclose(out);
1155                                         return result;
1156                                 }
1157
1158                                 in = preprocessed_in;
1159                                 break;
1160
1161                         default:
1162                                 break;
1163                 }
1164
1165                 FILE *asm_out;
1166                 if (mode == Compile) {
1167                         asm_out = out;
1168                 } else {
1169                         asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1170                 }
1171
1172                 if (in == NULL)
1173                         in = open_file(filename);
1174
1175                 /* preprocess and compile */
1176                 if (filetype == FILETYPE_PREPROCESSED_C) {
1177                         char const* invalid_mode;
1178                         switch (standard) {
1179                                 case STANDARD_ANSI:
1180                                 case STANDARD_C89:   c_mode = _C89;                break;
1181                                 /* TODO ^v determine difference between these two */
1182                                 case STANDARD_C90:   c_mode = _C89;                break;
1183                                 case STANDARD_C99:   c_mode = _C89 | _C99;         break;
1184                                 case STANDARD_GNU89: c_mode = _C89 |        _GNUC; break;
1185
1186 default_c_warn:
1187                                         fprintf(stderr,
1188                                                         "warning: command line option \"-std=%s\" is not valid for C\n",
1189                                                         invalid_mode);
1190                                         /* FALLTHROUGH */
1191                                 case STANDARD_DEFAULT:
1192                                 case STANDARD_GNU99:   c_mode = _C89 | _C99 | _GNUC; break;
1193
1194                                 case STANDARD_CXX98:   invalid_mode = "c++98"; goto default_c_warn;
1195                                 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1196                         }
1197                         goto do_parsing;
1198                 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1199                         char const* invalid_mode;
1200                         switch (standard) {
1201                                 case STANDARD_C89:   invalid_mode = "c89";   goto default_cxx_warn;
1202                                 case STANDARD_C90:   invalid_mode = "c90";   goto default_cxx_warn;
1203                                 case STANDARD_C99:   invalid_mode = "c99";   goto default_cxx_warn;
1204                                 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1205                                 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1206
1207                                 case STANDARD_ANSI:
1208                                 case STANDARD_CXX98: c_mode = _CXX; break;
1209
1210 default_cxx_warn:
1211                                         fprintf(stderr,
1212                                                         "warning: command line option \"-std=%s\" is not valid for C++\n",
1213                                                         invalid_mode);
1214                                 case STANDARD_DEFAULT:
1215                                 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1216                         }
1217
1218 do_parsing:
1219                         c_mode |= features_on;
1220                         c_mode &= ~features_off;
1221                         init_tokens();
1222                         translation_unit_t *const unit = do_parsing(in, filename);
1223
1224                         /* prints the AST even if errors occurred */
1225                         if (mode == PrintAst) {
1226                                 type_set_output(out);
1227                                 ast_set_output(out);
1228                                 print_ast(unit);
1229                         }
1230
1231                         if (error_count > 0) {
1232                                 /* parsing failed because of errors */
1233                                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1234                                         warning_count);
1235                                 result = EXIT_FAILURE;
1236                                 continue;
1237                         } else if (warning_count > 0) {
1238                                 fprintf(stderr, "%u warning(s)\n", warning_count);
1239                         }
1240
1241                         if (in == preprocessed_in) {
1242                                 int pp_result = pclose(preprocessed_in);
1243                                 if (pp_result != EXIT_SUCCESS) {
1244                                         exit(EXIT_FAILURE);
1245                                 }
1246                         }
1247
1248                         if (mode == BenchmarkParser) {
1249                                 return result;
1250                         } else if (mode == PrintFluffy) {
1251                                 write_fluffy_decls(out, unit);
1252                                 continue;
1253                         } else if (mode == PrintCaml) {
1254                                 write_caml_decls(out, unit);
1255                                 continue;
1256                         }
1257
1258                         translation_unit_to_firm(unit);
1259
1260                         if (mode == ParseOnly) {
1261                                 continue;
1262                         }
1263
1264                         if (mode == CompileDump) {
1265                                 /* find irg */
1266                                 ident    *id     = new_id_from_str(dumpfunction);
1267                                 ir_graph *irg    = NULL;
1268                                 int       n_irgs = get_irp_n_irgs();
1269                                 for (int i = 0; i < n_irgs; ++i) {
1270                                         ir_graph *tirg   = get_irp_irg(i);
1271                                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
1272                                         if (irg_id == id) {
1273                                                 irg = tirg;
1274                                                 break;
1275                                         }
1276                                 }
1277
1278                                 if (irg == NULL) {
1279                                         fprintf(stderr, "No graph for function '%s' found\n",
1280                                                 dumpfunction);
1281                                         exit(1);
1282                                 }
1283
1284                                 dump_ir_block_graph_file(irg, out);
1285                                 fclose(out);
1286                                 exit(0);
1287                         }
1288
1289                         gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1290                                         have_const_functions);
1291                         if (asm_out != out) {
1292                                 fclose(asm_out);
1293                         }
1294                 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1295                         copy_file(asm_out, in);
1296                         if (in == preprocessed_in) {
1297                                 int pp_result = pclose(preprocessed_in);
1298                                 if (pp_result != EXIT_SUCCESS) {
1299                                         return pp_result;
1300                                 }
1301                         }
1302                         if (asm_out != out) {
1303                                 fclose(asm_out);
1304                         }
1305                 }
1306
1307                 if (mode == Compile)
1308                         continue;
1309
1310                 /* if we're here then we have preprocessed assembly */
1311                 filename = asm_tempfile;
1312                 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1313
1314                 /* assemble */
1315                 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1316                         char        temp[1024];
1317                         const char *filename_o;
1318                         if (mode == CompileAssemble) {
1319                                 fclose(out);
1320                                 filename_o = outname;
1321                         } else {
1322                                 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1323                                 fclose(tempf);
1324                                 filename_o = temp;
1325                         }
1326
1327                         assemble(filename_o, filename);
1328
1329                         size_t len = strlen(filename_o) + 1;
1330                         filename = obstack_copy(&file_obst, filename_o, len);
1331                         filetype = FILETYPE_OBJECT;
1332                 }
1333
1334                 /* ok we're done here, process next file */
1335                 file->name = filename;
1336                 file->type = filetype;
1337         }
1338
1339         if (result != EXIT_SUCCESS)
1340                 return result;
1341
1342         /* link program file */
1343         if (mode == CompileAssembleLink) {
1344                 obstack_1grow(&ldflags_obst, '\0');
1345                 const char *flags = obstack_finish(&ldflags_obst);
1346
1347                 /* construct commandline */
1348                 obstack_printf(&file_obst, "%s", LINKER);
1349                 for (file_list_entry_t *entry = files; entry != NULL;
1350                                 entry = entry->next) {
1351                         if (entry->type != FILETYPE_OBJECT)
1352                                 continue;
1353
1354                         add_flag(&file_obst, "%s", entry->name);
1355                 }
1356
1357                 add_flag(&file_obst, "-o");
1358                 add_flag(&file_obst, outname);
1359                 obstack_printf(&file_obst, "%s", flags);
1360                 obstack_1grow(&file_obst, '\0');
1361
1362                 char *commandline = obstack_finish(&file_obst);
1363
1364                 if (verbose) {
1365                         puts(commandline);
1366                 }
1367                 int err = system(commandline);
1368                 if (err != EXIT_SUCCESS) {
1369                         fprintf(stderr, "linker reported an error\n");
1370                         exit(1);
1371                 }
1372         }
1373
1374         obstack_free(&cppflags_obst, NULL);
1375         obstack_free(&ldflags_obst, NULL);
1376         obstack_free(&file_obst, NULL);
1377
1378         exit_mangle();
1379         exit_ast2firm();
1380         exit_parser();
1381         exit_ast();
1382         exit_lexer();
1383         exit_typehash();
1384         exit_types();
1385         exit_tokens();
1386         exit_symbol_table();
1387         return 0;
1388 }