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