c2e0bf0307e9dcdec948677f5158bd898f6fb3b2
[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("deconv");
419                 firm_be_option("omitfp");
420                 break;
421         }
422
423         /* parse rest of options */
424         bool help_displayed  = false;
425         bool argument_errors = false;
426         for(int i = 1; i < argc; ++i) {
427                 const char *arg = argv[i];
428                 if(arg[0] == '-') {
429                         /* an option */
430                         const char *option = &arg[1];
431                         if(option[0] == 'o') {
432                                 GET_ARG_AFTER(outname, "-o");
433                         } else if(option[0] == 'g') {
434                                 firm_be_option("stabs=yes");
435                                 firm_be_option("omitfp=no");
436                                 firm_be_option("ia32-nooptcc=yes");
437                         } else if(SINGLE_OPTION('c')) {
438                                 mode = CompileAssemble;
439                         } else if(SINGLE_OPTION('S')) {
440                                 mode = Compile;
441                         } else if(option[0] == 'O') {
442                                 continue;
443                         } else if(option[0] == 'I') {
444                                 const char *opt;
445                                 GET_ARG_AFTER(opt, "-I");
446                                 obstack_printf(&cppflags_obst, " -I%s", opt);
447                         } else if(option[0] == 'D') {
448                                 const char *opt;
449                                 GET_ARG_AFTER(opt, "-D");
450                                 obstack_printf(&cppflags_obst, " -D%s", opt);
451                         } else if(option[0] == 'U') {
452                                 const char *opt;
453                                 GET_ARG_AFTER(opt, "-U");
454                                 obstack_printf(&cppflags_obst, " -U%s", opt);
455                         } else if(option[0] == 'l') {
456                                 const char *opt;
457                                 GET_ARG_AFTER(opt, "-l");
458                                 obstack_printf(&ldflags_obst, " -l%s", opt);
459                         } else if(option[0] == 'L') {
460                                 const char *opt;
461                                 GET_ARG_AFTER(opt, "-L");
462                                 obstack_printf(&ldflags_obst, " -L%s", opt);
463                         } else if(SINGLE_OPTION('v')) {
464                                 verbose = 1;
465                         } else if(SINGLE_OPTION('w')) {
466                                 inhibit_all_warnings = true;
467                         } else if(option[0] == 'f') {
468                                 const char *opt;
469                                 GET_ARG_AFTER(opt, "-f");
470
471                                 if(strcmp(opt, "syntax-only") == 0) {
472                                         mode = ParseOnly;
473                                 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
474                                         firm_be_option("omitfp");
475                                 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
476                                         firm_be_option("omitfp=no");
477                                 } else {
478                                         int res = firm_option(opt);
479                                         if (res == 0) {
480                                                 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
481                                                         opt);
482                                                 argument_errors = true;
483                                                 continue;
484                                         } else if (res == -1) {
485                                                 help_displayed = true;
486                                         }
487                                 }
488                         } else if(option[0] == 'b') {
489                                 const char *opt;
490                                 GET_ARG_AFTER(opt, "-b");
491                                 int res = firm_be_option(opt);
492                                 if (res == 0) {
493                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
494                                                 opt);
495                                         argument_errors = true;
496                                 } else if (res == -1) {
497                                         help_displayed = true;
498                                 }
499                         } else if(option[0] == 'W') {
500                                 set_warning_opt(&option[1]);
501                         } else if(option[0] == 'm') {
502                                 const char *opt;
503                                 GET_ARG_AFTER(opt, "-m");
504                                 char *endptr;
505                                 long int value = strtol(opt, &endptr, 10);
506                                 if (*endptr != '\0') {
507                                         fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
508                                         argument_errors = true;
509                                 }
510                                 if (value != 16 && value != 32 && value != 64) {
511                                         fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
512                                         argument_errors = true;
513                                 } else {
514                                         machine_size = (unsigned int)value;
515                                 }
516                         } else if (option[0] == '\0') {
517                                 if(input != NULL) {
518                                         fprintf(stderr, "error: multiple input files specified\n");
519                                         argument_errors = true;
520                                 } else {
521                                         input = arg;
522                                 }
523                         } else if(strcmp(option, "pedantic") == 0) {
524                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
525                         } else if(strncmp(option, "std=", 4) == 0) {
526                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
527                         } else if (option[0] == '-') {
528                                 /* double dash option */
529                                 ++option;
530                                 if(strcmp(option, "gcc") == 0) {
531                                         c_mode |= _GNUC;
532                                 } else if(strcmp(option, "no-gcc") == 0) {
533                                         c_mode &= ~_GNUC;
534                                 } else if(strcmp(option, "ms") == 0) {
535                                         c_mode |= _MS;
536                                 } else if(strcmp(option, "no-ms") == 0) {
537                                         c_mode &= ~_MS;
538                                 } else if(strcmp(option, "signed-chars") == 0) {
539                                         char_is_signed = true;
540                                 } else if(strcmp(option, "unsigned-chars") == 0) {
541                                         char_is_signed = false;
542                                 } else if(strcmp(option, "strict") == 0) {
543                                         strict_mode = true;
544                                 } else if(strcmp(option, "lextest") == 0) {
545                                         mode = LexTest;
546                                 } else if(strcmp(option, "benchmark") == 0) {
547                                         mode = BenchmarkParser;
548                                 } else if(strcmp(option, "print-ast") == 0) {
549                                         mode = PrintAst;
550                                 } else if(strcmp(option, "print-implicit-cast") == 0) {
551                                         print_implicit_casts = true;
552                                 } else if(strcmp(option, "print-parenthesis") == 0) {
553                                         print_parenthesis = true;
554                                 } else if(strcmp(option, "print-fluffy") == 0) {
555                                         mode = PrintFluffy;
556                                 } else if(strcmp(option, "version") == 0) {
557                                         firm_version_t ver;
558                                         firm_get_version(&ver);
559                                         printf("cparser (%s) using libFirm (%u.%u",
560                                                cparser_REVISION, ver.major, ver.minor);
561                                         if(ver.revision[0] != 0) {
562                                                 putchar(' ');
563                                                 fputs(ver.revision, stdout);
564                                         }
565                                         if(ver.build[0] != 0) {
566                                                 putchar(' ');
567                                                 fputs(ver.build, stdout);
568                                         }
569                                         puts(")\n");
570                                         exit(EXIT_SUCCESS);
571                                 } else if(strcmp(option, "dump-function") == 0) {
572                                         ++i;
573                                         if(i >= argc) {
574                                                 fprintf(stderr, "error: "
575                                                         "expected argument after '--dump-function'\n");
576                                                 argument_errors = true;
577                                                 break;
578                                         }
579                                         dumpfunction = argv[i];
580                                         mode         = CompileDump;
581                                 } else {
582                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
583                                         argument_errors = true;
584                                 }
585                         } else {
586                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
587                                 argument_errors = true;
588                         }
589                 } else {
590                         if(input != NULL) {
591                                 fprintf(stderr, "error: multiple input files specified\n");
592                                 argument_errors = true;
593                         } else {
594                                 input = arg;
595                         }
596                 }
597         }
598
599         /* we do the lowering in ast2firm */
600         firm_opt.lower_bitfields = FALSE;
601
602         if(help_displayed) {
603                 return !argument_errors;
604         }
605         if(argument_errors) {
606                 usage(argv[0]);
607                 return 1;
608         }
609
610         gen_firm_init();
611         init_symbol_table();
612         init_tokens();
613         init_types();
614         init_typehash();
615         init_basic_types();
616         init_lexer();
617         init_ast();
618         init_parser();
619         init_ast2firm();
620
621         FILE *out = NULL;
622         char  outnamebuf[4096];
623         if(outname == NULL) {
624                 switch(mode) {
625                 case BenchmarkParser:
626                 case PrintAst:
627                 case PrintFluffy:
628                 case LexTest:
629                         if(outname == NULL)
630                                 outname = "-";
631                         break;
632                 case ParseOnly:
633                         break;
634                 case Compile:
635                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
636                         outname = outnamebuf;
637                         break;
638                 case CompileAssemble:
639                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
640                         outname = outnamebuf;
641                         break;
642                 case CompileDump:
643                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
644                                         ".vcg");
645                         outname = outnamebuf;
646                         break;
647                 case CompileAssembleLink:
648                         outname = "a.out";
649                         break;
650                 }
651         }
652
653         if(outname != NULL) {
654                 if(strcmp(outname, "-") == 0) {
655                         out = stdout;
656                 } else {
657                         out = fopen(outname, "w");
658                         if(out == NULL) {
659                                 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
660                                         strerror(errno));
661                                 return 1;
662                         }
663                 }
664         }
665
666         FILE *in;
667         if(input == NULL) {
668                 fprintf(stderr, "%s: no input files\n", argv[0]);
669                 return 1;
670         } else if(strcmp(input, "-") == 0) {
671                 in    = stdin;
672                 input = "<stdin>";
673         } else {
674                 in = fopen(input, "r");
675                 if(in == NULL) {
676                         fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
677                         return 1;
678                 }
679         }
680
681         if(mode == LexTest) {
682                 lextest(in, input);
683                 fclose(in);
684                 return 0;
685         }
686
687         FILE *preprocessed_in = preprocess(in, input);
688         translation_unit_t *const unit = do_parsing(preprocessed_in, input);
689         int result = pclose(preprocessed_in);
690         if(result != 0) {
691                 return result;
692         }
693         if(unit == NULL) {
694                 /* parsing failed because of errors */
695                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
696                 return EXIT_FAILURE;
697         }
698         if (warning_count > 0) {
699                 fprintf(stderr, "%u warning(s)\n", warning_count);
700         }
701
702         if(mode == BenchmarkParser) {
703                 return 0;
704         }
705
706         if(mode == PrintAst) {
707                 type_set_output(out);
708                 ast_set_output(out);
709                 print_ast(unit);
710                 return 0;
711         }
712         if(mode == PrintFluffy) {
713                 type_set_output(out);
714                 ast_set_output(out);
715                 write_fluffy_decls(out, unit);
716         }
717
718         translation_unit_to_firm(unit);
719
720         if(mode == ParseOnly) {
721                 return 0;
722         }
723
724         FILE *asm_out;
725         char  asm_tempfile[1024];
726         if(mode == CompileDump) {
727                 asm_out = NULL;
728                 firm_be_opt.selection = BE_NONE;
729         } else if(mode == Compile) {
730                 asm_out = out;
731         } else {
732                 asm_out
733                         = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
734         }
735         gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
736
737         if(mode == CompileDump) {
738                 /* find irg */
739                 ident    *id     = new_id_from_str(dumpfunction);
740                 ir_graph *irg    = NULL;
741                 int       n_irgs = get_irp_n_irgs();
742                 for(int i = 0; i < n_irgs; ++i) {
743                         ir_graph *tirg   = get_irp_irg(i);
744                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
745                         if(irg_id == id) {
746                                 irg = tirg;
747                                 break;
748                         }
749                 }
750
751                 if(irg == NULL) {
752                         fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
753                         return 1;
754                 }
755
756                 dump_ir_block_graph_file(irg, out);
757                 fclose(out);
758                 return 0;
759         }
760
761         fclose(asm_out);
762
763         /* assemble assembler and create object file */
764         char obj_tfile[1024];
765         if(mode == CompileAssemble || mode == CompileAssembleLink) {
766                 const char *obj_outfile;
767                 if(mode == CompileAssemble) {
768                         fclose(out);
769                         obj_outfile = outname;
770                 } else {
771                         FILE *tempf
772                                 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
773                         fclose(tempf);
774                         obj_outfile = obj_tfile;
775                 }
776
777                 assemble(obj_outfile, asm_tempfile);
778         }
779
780         /* link object file */
781         if(mode == CompileAssembleLink) {
782                 do_link(outname, obj_tfile);
783         }
784
785         obstack_free(&cppflags_obst, NULL);
786         obstack_free(&ldflags_obst, NULL);
787
788         exit_ast2firm();
789         exit_parser();
790         exit_ast();
791         exit_lexer();
792         exit_typehash();
793         exit_types();
794         exit_tokens();
795         exit_symbol_table();
796         return 0;
797 }