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