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