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