some refactoring in preparation for a preprocessor
[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
54 #else
55 #include <unistd.h>
56 #define HAVE_MKSTEMP
57 #endif
58
59 #ifndef WITH_LIBCORE
60 #define WITH_LIBCORE
61 #endif
62
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
65
66 #include "lexer.h"
67 #include "token_t.h"
68 #include "types.h"
69 #include "type_hash.h"
70 #include "parser.h"
71 #include "ast2firm.h"
72 #include "diagnostic.h"
73 #include "lang_features.h"
74 #include "driver/firm_opt.h"
75 #include "driver/firm_cmdline.h"
76 #include "adt/error.h"
77 #include "write_fluffy.h"
78 #include "revision.h"
79 #include "warning.h"
80
81 #ifndef PREPROCESSOR
82 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
83 #endif
84
85 #ifndef LINKER
86 #define LINKER    "gcc -m32"
87 #endif
88
89 #ifndef ASSEMBLER
90 #define ASSEMBLER "as --32"
91 #endif
92
93 /** The current c mode/dialect. */
94 unsigned int c_mode = _C89|_C99|_GNUC;
95
96 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
97 unsigned int machine_size = 32;
98
99 /** true if the char type is signed. */
100 bool char_is_signed = true;
101
102 /** true for strict language checking. */
103 bool strict_mode = false;
104
105 /* to switch on printing of implicit casts */
106 extern bool print_implicit_casts;
107
108 /* to switch on printing of  srenthesis to indicate operator precedence */
109 extern bool print_parenthesis;
110
111 static int            verbose;
112 static struct obstack cppflags_obst, ldflags_obst;
113
114 #if defined(_DEBUG) || defined(FIRM_DEBUG)
115 /**
116  * Debug printf implementation.
117  *
118  * @param fmt  printf style format parameter
119  */
120 void dbg_printf(const char *fmt, ...)
121 {
122         va_list list;
123
124         if (firm_dump.debug_print) {
125                 va_start(list, fmt);
126                 vprintf(fmt, list);
127                 va_end(list);
128         }  /* if */
129 }
130 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
131
132 static void initialize_firm(void)
133 {
134         firm_early_init();
135
136         dump_consts_local(1);
137         dump_keepalive_edges(1);
138 }
139
140 static void get_output_name(char *buf, size_t buflen, const char *inputname,
141                             const char *newext)
142 {
143         size_t last_dot = 0xffffffff;
144         size_t i = 0;
145
146         if(inputname == NULL) {
147                 snprintf(buf, buflen, "a%s", newext);
148                 return;
149         }
150
151         for(const char *c = inputname; *c != 0; ++c) {
152                 if(*c == '.')
153                         last_dot = i;
154                 ++i;
155         }
156         if(last_dot == 0xffffffff)
157                 last_dot = i;
158
159         if(last_dot >= buflen)
160                 panic("filename too long");
161         memcpy(buf, inputname, last_dot);
162
163         size_t extlen = strlen(newext) + 1;
164         if(extlen + last_dot >= buflen)
165                 panic("filename too long");
166         memcpy(buf+last_dot, newext, extlen);
167 }
168
169 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
170 {
171         lexer_open_stream(in, input);
172         translation_unit_t *unit = parse();
173         return unit;
174 }
175
176 static void lextest(FILE *in, const char *fname)
177 {
178         lexer_open_stream(in, fname);
179
180         do {
181                 lexer_next_preprocessing_token();
182                 print_token(stdout, &lexer_token);
183                 puts("");
184         } while(lexer_token.type != T_EOF);
185 }
186
187 static FILE* preprocess(FILE* in, const char *fname)
188 {
189         char buf[4096];
190         obstack_1grow(&cppflags_obst, '\0');
191         const char *flags = obstack_finish(&cppflags_obst);
192
193         if(in != stdin) {
194                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
195         } else {
196                 /* read from stdin */
197                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
198         }
199
200         if(verbose) {
201                 puts(buf);
202         }
203         FILE* f = popen(buf, "r");
204         if (f == NULL) {
205                 fprintf(stderr, "invoking preprocessor failed\n");
206                 exit(1);
207         }
208         return f;
209 }
210
211 static void do_link(const char *out, const char *in)
212 {
213         char buf[4096];
214         obstack_1grow(&ldflags_obst, '\0');
215         const char *flags = obstack_finish(&ldflags_obst);
216
217         snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
218         if(verbose) {
219                 puts(buf);
220         }
221         int err = system(buf);
222         if(err != 0) {
223                 fprintf(stderr, "linker reported an error\n");
224                 exit(1);
225         }
226 }
227
228 static void assemble(const char *out, const char *in)
229 {
230         char buf[4096];
231
232         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
233         if(verbose) {
234                 puts(buf);
235         }
236
237         int err = system(buf);
238         if(err != 0) {
239                 fprintf(stderr, "assembler reported an error\n");
240                 exit(1);
241         }
242 }
243
244 static const char *try_dir(const char *dir)
245 {
246         if(dir == NULL)
247                 return dir;
248         if(access(dir, R_OK | W_OK | X_OK) == 0)
249                 return dir;
250         return NULL;
251 }
252
253 static const char *get_tempdir(void)
254 {
255         static const char *tmpdir = NULL;
256
257         if(tmpdir != NULL)
258                 return tmpdir;
259
260         if(tmpdir == NULL)
261                 tmpdir = try_dir(getenv("TMPDIR"));
262         if(tmpdir == NULL)
263                 tmpdir = try_dir(getenv("TMP"));
264         if(tmpdir == NULL)
265                 tmpdir = try_dir(getenv("TEMP"));
266
267 #ifdef P_tmpdir
268         if(tmpdir == NULL)
269                 tmpdir = try_dir(P_tmpdir);
270 #endif
271
272         if(tmpdir == NULL)
273                 tmpdir = try_dir("/var/tmp");
274         if(tmpdir == NULL)
275                 tmpdir = try_dir("/usr/tmp");
276         if(tmpdir == NULL)
277                 tmpdir = try_dir("/tmp");
278
279         if(tmpdir == NULL)
280                 tmpdir = ".";
281
282         return tmpdir;
283 }
284
285 #ifndef HAVE_MKSTEMP
286 /* cheap and nasty mkstemp replacement */
287 static int mkstemp(char *templ)
288 {
289         mktemp(templ);
290         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
291 }
292 #endif
293
294 /**
295  * an own version of tmpnam, which: writes in a buffer, appends a user specified
296  * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
297  */
298 static FILE *make_temp_file(char *buffer, size_t buflen,
299                             const char *prefix, const char *suffix)
300 {
301         const char *tempdir = get_tempdir();
302
303         /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
304         (void) suffix;
305         suffix = "";
306
307         snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
308
309         int fd = mkstemp(buffer);
310         if(fd == -1) {
311                 fprintf(stderr, "couldn't create temporary file: %s\n",
312                         strerror(errno));
313                 exit(1);
314         }
315         FILE *out = fdopen(fd, "w");
316         if(out == NULL) {
317                 fprintf(stderr, "couldn't create temporary file FILE*\n");
318                 exit(1);
319         }
320
321         return out;
322 }
323
324 /**
325  * Do the necessary lowering for compound parameters.
326  */
327 void lower_compound_params(void)
328 {
329         lower_params_t params;
330
331         params.def_ptr_alignment    = 4;
332         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
333         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
334         params.find_pointer_type    = NULL;
335         params.ret_compound_in_regs = NULL;
336         lower_calls_with_compounds(&params);
337 }
338
339 typedef enum compile_mode_t {
340         BenchmarkParser,
341         ParseOnly,
342         Compile,
343         CompileDump,
344         CompileAssemble,
345         CompileAssembleLink,
346         LexTest,
347         PrintAst,
348         PrintFluffy
349 } compile_mode_t;
350
351 static void usage(const char *argv0)
352 {
353         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
354 }
355
356 int main(int argc, char **argv)
357 {
358         initialize_firm();
359
360         const char     *input        = NULL;
361         const char     *outname      = NULL;
362         const char     *dumpfunction = NULL;
363         compile_mode_t  mode         = CompileAssembleLink;
364         int             opt_level    = 1;
365
366         obstack_init(&cppflags_obst);
367         obstack_init(&ldflags_obst);
368
369 #define GET_ARG_AFTER(def, args)                                             \
370         def = &arg[sizeof(args)-1];                                              \
371         if(def[0] == '\0') {                                                     \
372                 ++i;                                                                 \
373                 if(i >= argc) {                                                      \
374                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
375                         argument_errors = true;                                          \
376                         break;                                                           \
377                 }                                                                    \
378                 def = argv[i];                                                       \
379                 if(def[0] == '-' && def[1] != '\0') {                                \
380                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
381                         argument_errors = true;                                          \
382                         continue;                                                        \
383                 }                                                                    \
384         }
385
386 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
387
388         /* early options parsing (find out optimisation level) */
389         for(int i = 1; i < argc; ++i) {
390                 const char *arg = argv[i];
391                 if(arg[0] != '-')
392                         continue;
393
394                 const char *option = &arg[1];
395                 if(option[0] == 'O') {
396                         sscanf(&option[1], "%d", &opt_level);
397                 }
398         }
399
400         /* apply optimisation level */
401         switch(opt_level) {
402         case 0:
403                 firm_option("no-opt");
404                 break;
405         case 1:
406                 firm_option("no-inline");
407                 break;
408         default:
409         case 4:
410                 firm_option("strict-aliasing");
411                 /* fallthrough */
412         case 3:
413                 firm_option("cond-eval");
414                 firm_option("if-conv");
415                 /* fallthrough */
416         case 2:
417                 firm_option("inline");
418                 firm_option("no-strength-red");
419                 firm_option("deconv");
420                 firm_be_option("omitfp");
421                 break;
422         }
423
424         /* parse rest of options */
425         bool help_displayed  = false;
426         bool argument_errors = false;
427         for(int i = 1; i < argc; ++i) {
428                 const char *arg = argv[i];
429                 if(arg[0] == '-') {
430                         /* an option */
431                         const char *option = &arg[1];
432                         if(option[0] == 'o') {
433                                 GET_ARG_AFTER(outname, "-o");
434                         } else if(option[0] == 'g') {
435                                 firm_be_option("stabs=yes");
436                                 firm_be_option("omitfp=no");
437                                 firm_be_option("ia32-nooptcc=yes");
438                         } else if(SINGLE_OPTION('c')) {
439                                 mode = CompileAssemble;
440                         } else if(SINGLE_OPTION('S')) {
441                                 mode = Compile;
442                         } else if(option[0] == 'O') {
443                                 continue;
444                         } else if(option[0] == 'I') {
445                                 const char *opt;
446                                 GET_ARG_AFTER(opt, "-I");
447                                 obstack_printf(&cppflags_obst, " -I%s", opt);
448                         } else if(option[0] == 'D') {
449                                 const char *opt;
450                                 GET_ARG_AFTER(opt, "-D");
451                                 obstack_printf(&cppflags_obst, " -D%s", opt);
452                         } else if(option[0] == 'U') {
453                                 const char *opt;
454                                 GET_ARG_AFTER(opt, "-U");
455                                 obstack_printf(&cppflags_obst, " -U%s", opt);
456                         } else if(option[0] == 'l') {
457                                 const char *opt;
458                                 GET_ARG_AFTER(opt, "-l");
459                                 obstack_printf(&ldflags_obst, " -l%s", opt);
460                         } else if(option[0] == 'L') {
461                                 const char *opt;
462                                 GET_ARG_AFTER(opt, "-L");
463                                 obstack_printf(&ldflags_obst, " -L%s", opt);
464                         } else if(SINGLE_OPTION('v')) {
465                                 verbose = 1;
466                         } else if(SINGLE_OPTION('w')) {
467                                 inhibit_all_warnings = true;
468                         } else if(option[0] == 'f') {
469                                 const char *opt;
470                                 GET_ARG_AFTER(opt, "-f");
471
472                                 if(strcmp(opt, "syntax-only") == 0) {
473                                         mode = ParseOnly;
474                                 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
475                                         firm_be_option("omitfp");
476                                 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
477                                         firm_be_option("omitfp=no");
478                                 } else {
479                                         int res = firm_option(opt);
480                                         if (res == 0) {
481                                                 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
482                                                         opt);
483                                                 argument_errors = true;
484                                                 continue;
485                                         } else if (res == -1) {
486                                                 help_displayed = true;
487                                         }
488                                 }
489                         } else if(option[0] == 'b') {
490                                 const char *opt;
491                                 GET_ARG_AFTER(opt, "-b");
492                                 int res = firm_be_option(opt);
493                                 if (res == 0) {
494                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
495                                                 opt);
496                                         argument_errors = true;
497                                 } else if (res == -1) {
498                                         help_displayed = true;
499                                 }
500                         } else if(option[0] == 'W') {
501                                 set_warning_opt(&option[1]);
502                         } else if(option[0] == 'm') {
503                                 const char *opt;
504                                 GET_ARG_AFTER(opt, "-m");
505                                 char *endptr;
506                                 long int value = strtol(opt, &endptr, 10);
507                                 if (*endptr != '\0') {
508                                         fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
509                                         argument_errors = true;
510                                 }
511                                 if (value != 16 && value != 32 && value != 64) {
512                                         fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
513                                         argument_errors = true;
514                                 } else {
515                                         machine_size = (unsigned int)value;
516                                 }
517                         } else if (option[0] == '\0') {
518                                 if(input != NULL) {
519                                         fprintf(stderr, "error: multiple input files specified\n");
520                                         argument_errors = true;
521                                 } else {
522                                         input = arg;
523                                 }
524                         } else if(strcmp(option, "pedantic") == 0) {
525                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
526                         } else if(strncmp(option, "std=", 4) == 0) {
527                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
528                         } else if (option[0] == '-') {
529                                 /* double dash option */
530                                 ++option;
531                                 if(strcmp(option, "gcc") == 0) {
532                                         c_mode |= _GNUC;
533                                 } else if(strcmp(option, "no-gcc") == 0) {
534                                         c_mode &= ~_GNUC;
535                                 } else if(strcmp(option, "ms") == 0) {
536                                         c_mode |= _MS;
537                                 } else if(strcmp(option, "no-ms") == 0) {
538                                         c_mode &= ~_MS;
539                                 } else if(strcmp(option, "signed-chars") == 0) {
540                                         char_is_signed = true;
541                                 } else if(strcmp(option, "unsigned-chars") == 0) {
542                                         char_is_signed = false;
543                                 } else if(strcmp(option, "strict") == 0) {
544                                         strict_mode = true;
545                                 } else if(strcmp(option, "lextest") == 0) {
546                                         mode = LexTest;
547                                 } else if(strcmp(option, "benchmark") == 0) {
548                                         mode = BenchmarkParser;
549                                 } else if(strcmp(option, "print-ast") == 0) {
550                                         mode = PrintAst;
551                                 } else if(strcmp(option, "print-implicit-cast") == 0) {
552                                         print_implicit_casts = true;
553                                 } else if(strcmp(option, "print-parenthesis") == 0) {
554                                         print_parenthesis = true;
555                                 } else if(strcmp(option, "print-fluffy") == 0) {
556                                         mode = PrintFluffy;
557                                 } else if(strcmp(option, "version") == 0) {
558                                         firm_version_t ver;
559                                         firm_get_version(&ver);
560                                         printf("cparser (%s) using libFirm (%u.%u",
561                                                cparser_REVISION, ver.major, ver.minor);
562                                         if(ver.revision[0] != 0) {
563                                                 putchar(' ');
564                                                 fputs(ver.revision, stdout);
565                                         }
566                                         if(ver.build[0] != 0) {
567                                                 putchar(' ');
568                                                 fputs(ver.build, stdout);
569                                         }
570                                         puts(")\n");
571                                         exit(EXIT_SUCCESS);
572                                 } else if(strcmp(option, "dump-function") == 0) {
573                                         ++i;
574                                         if(i >= argc) {
575                                                 fprintf(stderr, "error: "
576                                                         "expected argument after '--dump-function'\n");
577                                                 argument_errors = true;
578                                                 break;
579                                         }
580                                         dumpfunction = argv[i];
581                                         mode         = CompileDump;
582                                 } else {
583                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
584                                         argument_errors = true;
585                                 }
586                         } else {
587                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
588                                 argument_errors = true;
589                         }
590                 } else {
591                         if(input != NULL) {
592                                 fprintf(stderr, "error: multiple input files specified\n");
593                                 argument_errors = true;
594                         } else {
595                                 input = arg;
596                         }
597                 }
598         }
599
600         /* we do the lowering in ast2firm */
601         firm_opt.lower_bitfields = FALSE;
602
603         if(help_displayed) {
604                 return !argument_errors;
605         }
606         if(argument_errors) {
607                 usage(argv[0]);
608                 return 1;
609         }
610
611         gen_firm_init();
612         init_symbol_table();
613         init_tokens();
614         init_types();
615         init_typehash();
616         init_basic_types();
617         init_lexer();
618         init_ast();
619         init_parser();
620         init_ast2firm();
621
622         FILE *out = NULL;
623         char  outnamebuf[4096];
624         if(outname == NULL) {
625                 switch(mode) {
626                 case BenchmarkParser:
627                 case PrintAst:
628                 case PrintFluffy:
629                 case LexTest:
630                         if(outname == NULL)
631                                 outname = "-";
632                         break;
633                 case ParseOnly:
634                         break;
635                 case Compile:
636                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
637                         outname = outnamebuf;
638                         break;
639                 case CompileAssemble:
640                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
641                         outname = outnamebuf;
642                         break;
643                 case CompileDump:
644                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
645                                         ".vcg");
646                         outname = outnamebuf;
647                         break;
648                 case CompileAssembleLink:
649                         outname = "a.out";
650                         break;
651                 }
652         }
653
654         if(outname != NULL) {
655                 if(strcmp(outname, "-") == 0) {
656                         out = stdout;
657                 } else {
658                         out = fopen(outname, "w");
659                         if(out == NULL) {
660                                 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
661                                         strerror(errno));
662                                 return 1;
663                         }
664                 }
665         }
666
667         FILE *in;
668         if(input == NULL) {
669                 fprintf(stderr, "%s: no input files\n", argv[0]);
670                 return 1;
671         } else if(strcmp(input, "-") == 0) {
672                 in    = stdin;
673                 input = "<stdin>";
674         } else {
675                 in = fopen(input, "r");
676                 if(in == NULL) {
677                         fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
678                         return 1;
679                 }
680         }
681
682         if(mode == LexTest) {
683                 lextest(in, input);
684                 fclose(in);
685                 return 0;
686         }
687
688         FILE *preprocessed_in = preprocess(in, input);
689         translation_unit_t *const unit = do_parsing(preprocessed_in, input);
690         int result = pclose(preprocessed_in);
691         if(result != 0) {
692                 return result;
693         }
694         if(unit == NULL) {
695                 /* parsing failed because of errors */
696                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
697                 return EXIT_FAILURE;
698         }
699         if (warning_count > 0) {
700                 fprintf(stderr, "%u warning(s)\n", warning_count);
701         }
702
703         if(mode == BenchmarkParser) {
704                 return 0;
705         }
706
707         if(mode == PrintAst) {
708                 type_set_output(out);
709                 ast_set_output(out);
710                 print_ast(unit);
711                 return 0;
712         }
713         if(mode == PrintFluffy) {
714                 type_set_output(out);
715                 ast_set_output(out);
716                 write_fluffy_decls(out, unit);
717         }
718
719         translation_unit_to_firm(unit);
720
721         if(mode == ParseOnly) {
722                 return 0;
723         }
724
725         FILE *asm_out;
726         char  asm_tempfile[1024];
727         if(mode == CompileDump) {
728                 asm_out = NULL;
729                 firm_be_opt.selection = BE_NONE;
730         } else if(mode == Compile) {
731                 asm_out = out;
732         } else {
733                 asm_out
734                         = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
735         }
736         gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
737
738         if(mode == CompileDump) {
739                 /* find irg */
740                 ident    *id     = new_id_from_str(dumpfunction);
741                 ir_graph *irg    = NULL;
742                 int       n_irgs = get_irp_n_irgs();
743                 for(int i = 0; i < n_irgs; ++i) {
744                         ir_graph *tirg   = get_irp_irg(i);
745                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
746                         if(irg_id == id) {
747                                 irg = tirg;
748                                 break;
749                         }
750                 }
751
752                 if(irg == NULL) {
753                         fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
754                         return 1;
755                 }
756
757                 dump_ir_block_graph_file(irg, out);
758                 fclose(out);
759                 return 0;
760         }
761
762         fclose(asm_out);
763
764         /* assemble assembler and create object file */
765         char obj_tfile[1024];
766         if(mode == CompileAssemble || mode == CompileAssembleLink) {
767                 const char *obj_outfile;
768                 if(mode == CompileAssemble) {
769                         fclose(out);
770                         obj_outfile = outname;
771                 } else {
772                         FILE *tempf
773                                 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
774                         fclose(tempf);
775                         obj_outfile = obj_tfile;
776                 }
777
778                 assemble(obj_outfile, asm_tempfile);
779         }
780
781         /* link object file */
782         if(mode == CompileAssembleLink) {
783                 do_link(outname, obj_tfile);
784         }
785
786         obstack_free(&cppflags_obst, NULL);
787         obstack_free(&ldflags_obst, NULL);
788
789         exit_ast2firm();
790         exit_parser();
791         exit_ast();
792         exit_lexer();
793         exit_typehash();
794         exit_types();
795         exit_tokens();
796         exit_symbol_table();
797         return 0;
798 }