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