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