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