2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
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.
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.
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
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
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)
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
69 #include "type_hash.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 "write_caml.h"
83 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=__SIZE_TYPE__ -m32 -U__STRICT_ANSI__"
87 #define LINKER "gcc -m32"
91 #define ASSEMBLER "as --32"
94 /** The current c mode/dialect. */
95 unsigned int c_mode = _C89|_C99|_GNUC;
97 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
98 unsigned int machine_size = 32;
100 /** true if the char type is signed. */
101 bool char_is_signed = true;
103 /** true for strict language checking. */
104 bool strict_mode = false;
106 /** use builtins for some libc functions */
107 bool use_builtins = false;
109 /* to switch on printing of implicit casts */
110 extern bool print_implicit_casts;
112 /* to switch on printing of parenthesis to indicate operator precedence */
113 extern bool print_parenthesis;
116 static struct obstack cppflags_obst, ldflags_obst;
118 typedef struct file_list_entry_t file_list_entry_t;
120 typedef enum filetype_t {
123 FILETYPE_PREPROCESSED_C,
125 FILETYPE_PREPROCESSED_ASSEMBLER,
130 struct file_list_entry_t {
131 const char *name; /**< filename or NULL for stdin */
133 file_list_entry_t *next;
136 #if defined(_DEBUG) || defined(FIRM_DEBUG)
138 * Debug printf implementation.
140 * @param fmt printf style format parameter
142 void dbg_printf(const char *fmt, ...)
146 if (firm_dump.debug_print) {
152 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
154 static void initialize_firm(void)
158 dump_consts_local(1);
159 dump_keepalive_edges(1);
162 static void get_output_name(char *buf, size_t buflen, const char *inputname,
165 size_t last_dot = 0xffffffff;
168 if(inputname == NULL) {
169 snprintf(buf, buflen, "a%s", newext);
173 for(const char *c = inputname; *c != 0; ++c) {
178 if(last_dot == 0xffffffff)
181 if(last_dot >= buflen)
182 panic("filename too long");
183 memcpy(buf, inputname, last_dot);
185 size_t extlen = strlen(newext) + 1;
186 if(extlen + last_dot >= buflen)
187 panic("filename too long");
188 memcpy(buf+last_dot, newext, extlen);
191 #include "builtins.h"
193 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
198 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
202 lexer_open_stream(in, input_name);
205 translation_unit_t *unit = finish_parsing();
209 static void lextest(FILE *in, const char *fname)
211 lexer_open_stream(in, fname);
214 lexer_next_preprocessing_token();
215 print_token(stdout, &lexer_token);
217 } while(lexer_token.type != T_EOF);
220 static void add_flag(struct obstack *obst, const char *format, ...)
223 va_start(ap, format);
226 vsnprintf(buf, sizeof(buf), format, ap);
228 /* escape stuff... */
229 obstack_1grow(obst, ' ');
230 for (char *c = buf; *c != '\0'; ++c) {
243 obstack_1grow(obst, '\\');
246 obstack_1grow(obst, *c);
254 static FILE *preprocess(const char *fname)
257 obstack_1grow(&cppflags_obst, '\0');
258 const char *flags = obstack_finish(&cppflags_obst);
260 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
265 FILE *f = popen(buf, "r");
267 fprintf(stderr, "invoking preprocessor failed\n");
273 static void assemble(const char *out, const char *in)
277 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
282 int err = system(buf);
284 fprintf(stderr, "assembler reported an error\n");
289 static const char *try_dir(const char *dir)
293 if(access(dir, R_OK | W_OK | X_OK) == 0)
298 static const char *get_tempdir(void)
300 static const char *tmpdir = NULL;
306 tmpdir = try_dir(getenv("TMPDIR"));
308 tmpdir = try_dir(getenv("TMP"));
310 tmpdir = try_dir(getenv("TEMP"));
314 tmpdir = try_dir(P_tmpdir);
318 tmpdir = try_dir("/var/tmp");
320 tmpdir = try_dir("/usr/tmp");
322 tmpdir = try_dir("/tmp");
331 /* cheap and nasty mkstemp replacement */
332 static int mkstemp(char *templ)
335 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
340 * an own version of tmpnam, which: writes in a buffer, emits no warnings
341 * during linking (like glibc/gnu ld do for tmpnam)...
343 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
345 const char *tempdir = get_tempdir();
347 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
349 int fd = mkstemp(buffer);
351 fprintf(stderr, "couldn't create temporary file: %s\n",
355 FILE *out = fdopen(fd, "w");
357 fprintf(stderr, "couldn't create temporary file FILE*\n");
365 * Do the necessary lowering for compound parameters.
367 void lower_compound_params(void)
369 lower_params_t params;
371 params.def_ptr_alignment = 4;
372 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
373 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
374 params.find_pointer_type = NULL;
375 params.ret_compound_in_regs = NULL;
376 lower_calls_with_compounds(¶ms);
379 typedef enum compile_mode_t {
393 static void usage(const char *argv0)
395 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
398 static void print_cparser_version(void) {
400 firm_get_version(&ver);
402 printf("cparser (%s) using libFirm (%u.%u",
403 cparser_REVISION, ver.major, ver.minor);
404 if(ver.revision[0] != 0) {
406 fputs(ver.revision, stdout);
408 if(ver.build[0] != 0) {
410 fputs(ver.build, stdout);
415 static void set_be_option(const char *arg)
417 int res = firm_be_option(arg);
422 static void set_option(const char *arg)
424 int res = firm_option(arg);
429 static void copy_file(FILE *dest, FILE *input)
433 while (!feof(input) && !ferror(dest)) {
434 size_t read = fread(buf, 1, sizeof(buf), input);
435 if(fwrite(buf, 1, read, dest) != read) {
436 perror("couldn't write output");
441 static FILE *open_file(const char *filename)
443 if (strcmp(filename, "-") == 0) {
447 FILE *in = fopen(filename, "r");
449 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
457 static filetype_t get_filetype_from_string(const char *string)
459 if (strcmp(string, "c") == 0 || strcmp(string, "c-header") == 0)
461 if (strcmp(string, "assembler") == 0)
462 return FILETYPE_PREPROCESSED_ASSEMBLER;
463 if (strcmp(string, "assembler-with-cpp") == 0)
464 return FILETYPE_ASSEMBLER;
465 if (strcmp(string, "none") == 0)
466 return FILETYPE_AUTODETECT;
468 return FILETYPE_UNKNOWN;
471 int main(int argc, char **argv)
475 const char *outname = NULL;
476 const char *dumpfunction = NULL;
477 compile_mode_t mode = CompileAssembleLink;
479 int result = EXIT_SUCCESS;
480 char cpu_arch[16] = "ia32";
481 file_list_entry_t *files = NULL;
482 file_list_entry_t *last_file = NULL;
483 struct obstack file_obst;
485 obstack_init(&cppflags_obst);
486 obstack_init(&ldflags_obst);
487 obstack_init(&file_obst);
489 #define GET_ARG_AFTER(def, args) \
490 def = &arg[sizeof(args)-1]; \
491 if(def[0] == '\0') { \
494 fprintf(stderr, "error: expected argument after '" args "'\n"); \
495 argument_errors = true; \
499 if(def[0] == '-' && def[1] != '\0') { \
500 fprintf(stderr, "error: expected argument after '" args "'\n"); \
501 argument_errors = true; \
506 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
508 /* early options parsing (find out optimisation level) */
509 for(int i = 1; i < argc; ++i) {
510 const char *arg = argv[i];
514 const char *option = &arg[1];
515 if(option[0] == 'O') {
516 sscanf(&option[1], "%d", &opt_level);
520 /* apply optimisation level */
523 set_option("no-opt");
526 set_option("no-inline");
530 set_option("strict-aliasing");
533 set_option("cond-eval");
534 set_option("if-conv");
538 set_option("inline");
539 set_option("deconv");
540 set_be_option("omitfp");
544 /* parse rest of options */
545 filetype_t forced_filetype = FILETYPE_AUTODETECT;
546 bool help_displayed = false;
547 bool argument_errors = false;
548 for(int i = 1; i < argc; ++i) {
549 const char *arg = argv[i];
550 if(arg[0] == '-' && arg[1] != 0) {
552 const char *option = &arg[1];
553 if(option[0] == 'o') {
554 GET_ARG_AFTER(outname, "-o");
555 } else if(option[0] == 'g') {
556 set_be_option("debuginfo=stabs");
557 set_be_option("omitfp=no");
558 set_be_option("ia32-nooptcc=yes");
559 } else if(SINGLE_OPTION('c')) {
560 mode = CompileAssemble;
561 } else if(SINGLE_OPTION('E')) {
562 mode = PreprocessOnly;
563 } else if(SINGLE_OPTION('S')) {
565 } else if(option[0] == 'O') {
567 } else if(option[0] == 'I') {
569 GET_ARG_AFTER(opt, "-I");
570 add_flag(&cppflags_obst, "-I%s", opt);
571 } else if(option[0] == 'D') {
573 GET_ARG_AFTER(opt, "-D");
574 add_flag(&cppflags_obst, "-D%s", opt);
575 } else if(option[0] == 'U') {
577 GET_ARG_AFTER(opt, "-U");
578 add_flag(&cppflags_obst, "-U%s", opt);
579 } else if(option[0] == 'l') {
581 GET_ARG_AFTER(opt, "-l");
582 add_flag(&ldflags_obst, "-l%s", opt);
583 } else if(option[0] == 'L') {
585 GET_ARG_AFTER(opt, "-L");
586 add_flag(&ldflags_obst, "-L%s", opt);
587 } else if(SINGLE_OPTION('v')) {
589 } else if(SINGLE_OPTION('w')) {
590 inhibit_all_warnings = true;
591 } else if(option[0] == 'x') {
593 GET_ARG_AFTER(opt, "-x");
594 forced_filetype = get_filetype_from_string(opt);
595 if (forced_filetype == FILETYPE_UNKNOWN) {
596 fprintf(stderr, "Unknown language '%s'\n", opt);
597 argument_errors = true;
599 } else if(strcmp(option, "M") == 0) {
600 mode = PreprocessOnly;
601 add_flag(&cppflags_obst, "-M");
602 } else if (strcmp(option, "MMD") == 0 ||
603 strcmp(option, "MD") == 0 ||
604 strcmp(option, "MM") == 0 ||
605 strcmp(option, "MP") == 0) {
606 add_flag(&cppflags_obst, "-%s", option);
607 } else if(strcmp(option, "MT") == 0
608 || strcmp(option, "MQ") == 0
609 || strcmp(option, "MF") == 0) {
611 GET_ARG_AFTER(opt, "-MT");
612 add_flag(&cppflags_obst, "-%s", option);
613 add_flag(&cppflags_obst, "%s", opt);
614 } else if(strcmp(option, "pipe") == 0) {
615 /* here for gcc compatibility */
616 } else if(option[0] == 'f') {
618 GET_ARG_AFTER(opt, "-f");
620 if (strcmp(opt, "dollars-in-identifiers") == 0) {
621 allow_dollar_in_symbol = true;
622 } else if (strcmp(opt, "no-dollars-in-identifiers") == 0) {
623 allow_dollar_in_symbol = false;
624 } else if (strcmp(opt, "syntax-only") == 0) {
626 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
627 set_be_option("omitfp");
628 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
629 set_be_option("omitfp=no");
630 } else if(strcmp(opt, "strength-reduce") == 0) {
631 firm_option("strength-red");
632 } else if(strcmp(opt, "fast-math") == 0
633 || strcmp(opt, "unroll-loops") == 0
634 || strcmp(opt, "expensive-optimizations") == 0
635 || strcmp(opt, "no-common") == 0
636 || strcmp(opt, "PIC") == 0
637 || strncmp(opt, "align-loops=", sizeof("align-loops=")-1) == 0
638 || strncmp(opt, "align-jumps=", sizeof("align-jumps=")-1) == 0
639 || strncmp(opt, "align-functions=", sizeof("align-functions=")-1) == 0) {
640 fprintf(stderr, "ignoring gcc option '-f %s'\n", opt);
642 int res = firm_option(opt);
644 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
646 argument_errors = true;
648 } else if (res == -1) {
649 help_displayed = true;
652 } else if(option[0] == 'b') {
654 GET_ARG_AFTER(opt, "-b");
655 int res = firm_be_option(opt);
657 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
659 argument_errors = true;
660 } else if (res == -1) {
661 help_displayed = true;
663 if (strncmp(opt, "isa=", 4) == 0)
664 strncpy(cpu_arch, opt, sizeof(cpu_arch));
666 } else if(option[0] == 'W') {
667 if(strncmp(option + 1, "l,", 2) == 0) // a gcc-style linker option
670 GET_ARG_AFTER(opt, "-Wl,");
671 add_flag(&ldflags_obst, "-Wl,%s", opt);
673 else set_warning_opt(&option[1]);
674 } else if(option[0] == 'm') {
679 GET_ARG_AFTER(opt, "-m");
680 if(strncmp(opt, "arch=", 5) == 0) {
681 GET_ARG_AFTER(opt, "-march=");
682 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
683 int res = firm_be_option(arch_opt);
685 argument_errors = true;
687 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
688 int res = firm_be_option(arch_opt);
690 argument_errors = true;
692 } else if(strncmp(opt, "tune=", 5) == 0) {
693 GET_ARG_AFTER(opt, "-mtune=");
694 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
695 int res = firm_be_option(arch_opt);
697 argument_errors = true;
698 } else if(strncmp(opt, "cpu=", 4) == 0) {
699 GET_ARG_AFTER(opt, "-mcpu=");
700 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
701 int res = firm_be_option(arch_opt);
703 argument_errors = true;
704 } else if(strncmp(opt, "fpmath=", 7) == 0) {
705 GET_ARG_AFTER(opt, "-mfpmath=");
706 if(strcmp(opt, "387") == 0)
708 else if(strcmp(opt, "sse") == 0)
711 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
712 argument_errors = true;
714 if(!argument_errors) {
715 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
716 int res = firm_be_option(arch_opt);
718 argument_errors = true;
720 } else if(strncmp(opt, "preferred-stack-boundary=", 25) == 0) {
721 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
722 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
723 int res = firm_be_option(arch_opt);
725 argument_errors = true;
726 } else if(strcmp(opt, "omit-leaf-frame-pointer") == 0) {
727 set_be_option("omitleaffp=1");
728 } else if(strcmp(opt, "no-omit-leaf-frame-pointer") == 0) {
729 set_be_option("omitleaffp=0");
732 long int value = strtol(opt, &endptr, 10);
733 if (*endptr != '\0') {
734 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
735 argument_errors = true;
737 if (value != 16 && value != 32 && value != 64) {
738 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
739 argument_errors = true;
741 machine_size = (unsigned int)value;
744 } else if(strcmp(option, "pg") == 0) {
745 set_be_option("gprof");
746 add_flag(&ldflags_obst, "-pg");
747 } else if(strcmp(option, "pedantic") == 0
748 || strcmp(option, "ansi") == 0) {
749 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
750 } else if(strcmp(option, "shared") == 0) {
751 add_flag(&ldflags_obst, "-shared");
752 } else if(strncmp(option, "std=", 4) == 0) {
753 if(strcmp(&option[4], "c99") == 0) {
755 } else if(strcmp(&option[4], "c89") == 0) {
757 } else if(strcmp(&option[4], "gnu99") == 0) {
758 c_mode = _C89|_C99|_GNUC;
759 } else if(strcmp(&option[4], "microsoft") == 0) {
760 c_mode = _C89|_C99|_MS;
762 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
763 } else if(strcmp(option, "version") == 0) {
764 print_cparser_version();
765 } else if (option[0] == '-') {
766 /* double dash option */
768 if(strcmp(option, "gcc") == 0) {
770 } else if(strcmp(option, "no-gcc") == 0) {
772 } else if(strcmp(option, "ms") == 0) {
774 } else if(strcmp(option, "no-ms") == 0) {
776 } else if(strcmp(option, "signed-chars") == 0) {
777 char_is_signed = true;
778 } else if(strcmp(option, "unsigned-chars") == 0) {
779 char_is_signed = false;
780 } else if(strcmp(option, "strict") == 0) {
782 } else if(strcmp(option, "lextest") == 0) {
784 } else if(strcmp(option, "benchmark") == 0) {
785 mode = BenchmarkParser;
786 } else if(strcmp(option, "print-ast") == 0) {
788 } else if(strcmp(option, "print-implicit-cast") == 0) {
789 print_implicit_casts = true;
790 } else if(strcmp(option, "print-parenthesis") == 0) {
791 print_parenthesis = true;
792 } else if(strcmp(option, "print-fluffy") == 0) {
794 } else if(strcmp(option, "print-caml") == 0) {
796 } else if(strcmp(option, "version") == 0) {
797 print_cparser_version();
799 } else if(strcmp(option, "dump-function") == 0) {
802 fprintf(stderr, "error: "
803 "expected argument after '--dump-function'\n");
804 argument_errors = true;
807 dumpfunction = argv[i];
810 fprintf(stderr, "error: unknown argument '%s'\n", arg);
811 argument_errors = true;
814 fprintf(stderr, "error: unknown argument '%s'\n", arg);
815 argument_errors = true;
818 filetype_t type = forced_filetype;
819 const char *filename = arg;
820 if (type == FILETYPE_AUTODETECT) {
821 size_t len = strlen(arg);
822 if (len < 2 && arg[0] == '-') {
823 /* - implicitely means C source file */
826 } else if (len > 2 && arg[len-2] == '.') {
828 case 'c': type = FILETYPE_C; break;
829 case 'h': type = FILETYPE_C; break;
830 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
831 case 'S': type = FILETYPE_ASSEMBLER; break;
834 case 'o': type = FILETYPE_OBJECT; break;
836 } else if (len > 3 && arg[len-3] == '.') {
837 if(strcmp(arg + len - 2, "so") == 0) {
838 type = FILETYPE_OBJECT;
842 if (type == FILETYPE_AUTODETECT) {
843 fprintf(stderr, "'%s': file format not recognized\n", arg);
848 file_list_entry_t *entry
849 = obstack_alloc(&file_obst, sizeof(entry[0]));
850 memset(entry, 0, sizeof(entry[0]));
854 if (last_file != NULL) {
855 last_file->next = entry;
864 fprintf(stderr, "error: no input files specified\n");
865 argument_errors = true;
868 if (help_displayed) {
869 return !argument_errors;
871 if (argument_errors) {
876 /* we do the lowering in ast2firm */
877 firm_opt.lower_bitfields = FALSE;
890 char outnamebuf[4096];
891 if (outname == NULL) {
892 const char *filename = files->name;
895 case BenchmarkParser:
906 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
907 outname = outnamebuf;
909 case CompileAssemble:
910 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
911 outname = outnamebuf;
914 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
916 outname = outnamebuf;
918 case CompileAssembleLink:
928 assert(outname != NULL);
931 if(strcmp(outname, "-") == 0) {
934 out = fopen(outname, "w");
936 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
942 file_list_entry_t *file;
943 for (file = files; file != NULL; file = file->next) {
944 char asm_tempfile[1024];
945 const char *filename = file->name;
946 filetype_t filetype = file->type;
948 if (file->type == FILETYPE_OBJECT)
952 if (mode == LexTest) {
954 in = open_file(filename);
955 lextest(in, filename);
960 FILE *preprocessed_in = NULL;
961 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
962 /* no support for input on FILE* yet */
964 panic("internal compiler error: in for preprocessor != NULL");
966 preprocessed_in = preprocess(filename);
967 if (mode == PreprocessOnly) {
968 copy_file(out, preprocessed_in);
969 int result = pclose(preprocessed_in);
974 if (filetype == FILETYPE_C) {
975 filetype = FILETYPE_PREPROCESSED_C;
976 } else if (filetype == FILETYPE_ASSEMBLER) {
977 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
979 panic("internal compiler error: unknown filetype at preproc");
982 in = preprocessed_in;
986 if(mode == Compile) {
989 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
994 in = open_file(filename);
996 /* preprocess and compile */
997 if (filetype == FILETYPE_PREPROCESSED_C) {
998 translation_unit_t *const unit = do_parsing(in, filename);
999 if (in == preprocessed_in) {
1000 int pp_result = pclose(preprocessed_in);
1001 if (pp_result != EXIT_SUCCESS) {
1006 /* prints the AST even if errors occurred */
1007 if (mode == PrintAst) {
1008 type_set_output(out);
1009 ast_set_output(out);
1013 if(error_count > 0) {
1014 /* parsing failed because of errors */
1015 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1017 result = EXIT_FAILURE;
1019 } else if(warning_count > 0) {
1020 fprintf(stderr, "%u warning(s)\n", warning_count);
1023 if(mode == BenchmarkParser) {
1025 } else if(mode == PrintFluffy) {
1026 write_fluffy_decls(out, unit);
1028 } else if (mode == PrintCaml) {
1029 write_caml_decls(out, unit);
1033 translation_unit_to_firm(unit);
1035 if (mode == ParseOnly) {
1039 if (mode == CompileDump) {
1041 ident *id = new_id_from_str(dumpfunction);
1042 ir_graph *irg = NULL;
1043 int n_irgs = get_irp_n_irgs();
1044 for(int i = 0; i < n_irgs; ++i) {
1045 ir_graph *tirg = get_irp_irg(i);
1046 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1054 fprintf(stderr, "No graph for function '%s' found\n",
1059 dump_ir_block_graph_file(irg, out);
1064 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1065 /*firm_const_exists=*/0);
1066 if (asm_out != out) {
1070 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1071 copy_file(asm_out, in);
1072 if (in == preprocessed_in) {
1073 int pp_result = pclose(preprocessed_in);
1074 if (pp_result != EXIT_SUCCESS) {
1078 if(asm_out != out) {
1083 if (mode == Compile)
1086 /* if we're here then we have preprocessed assembly */
1087 filename = asm_tempfile;
1088 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1091 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1093 const char *filename_o;
1094 if(mode == CompileAssemble) {
1096 filename_o = outname;
1098 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1103 assemble(filename_o, filename);
1105 size_t len = strlen(filename_o) + 1;
1106 filename = obstack_copy(&file_obst, filename_o, len);
1107 filetype = FILETYPE_OBJECT;
1110 /* ok we're done here, process next file */
1111 file->name = filename;
1112 file->type = filetype;
1115 if (result != EXIT_SUCCESS)
1118 /* link program file */
1119 if(mode == CompileAssembleLink) {
1120 obstack_1grow(&ldflags_obst, '\0');
1121 const char *flags = obstack_finish(&ldflags_obst);
1123 /* construct commandline */
1124 obstack_printf(&file_obst, "%s", LINKER);
1125 for (file_list_entry_t *entry = files; entry != NULL;
1126 entry = entry->next) {
1127 if (entry->type != FILETYPE_OBJECT)
1130 add_flag(&file_obst, "%s", entry->name);
1133 add_flag(&file_obst, "-o");
1134 add_flag(&file_obst, outname);
1135 obstack_printf(&file_obst, "%s", flags);
1136 obstack_1grow(&file_obst, '\0');
1138 char *commandline = obstack_finish(&file_obst);
1143 int err = system(commandline);
1144 if(err != EXIT_SUCCESS) {
1145 fprintf(stderr, "linker reported an error\n");
1150 obstack_free(&cppflags_obst, NULL);
1151 obstack_free(&ldflags_obst, NULL);
1152 obstack_free(&file_obst, NULL);
1161 exit_symbol_table();