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