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 /** allow dollar signs in symbols */
110 extern bool allow_dollar_in_symbols;
112 /* to switch on printing of implicit casts */
113 extern bool print_implicit_casts;
115 /* to switch on printing of parenthesis to indicate operator precedence */
116 extern bool print_parenthesis;
119 static struct obstack cppflags_obst, ldflags_obst;
121 typedef struct file_list_entry_t file_list_entry_t;
123 typedef enum filetype_t {
126 FILETYPE_PREPROCESSED_C,
128 FILETYPE_PREPROCESSED_ASSEMBLER,
133 struct file_list_entry_t {
134 const char *name; /**< filename or NULL for stdin */
136 file_list_entry_t *next;
139 #if defined(_DEBUG) || defined(FIRM_DEBUG)
141 * Debug printf implementation.
143 * @param fmt printf style format parameter
145 void dbg_printf(const char *fmt, ...)
149 if (firm_dump.debug_print) {
155 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
157 static void initialize_firm(void)
161 dump_consts_local(1);
162 dump_keepalive_edges(1);
165 static void get_output_name(char *buf, size_t buflen, const char *inputname,
168 size_t last_dot = 0xffffffff;
171 if(inputname == NULL) {
172 snprintf(buf, buflen, "a%s", newext);
176 for(const char *c = inputname; *c != 0; ++c) {
181 if(last_dot == 0xffffffff)
184 if(last_dot >= buflen)
185 panic("filename too long");
186 memcpy(buf, inputname, last_dot);
188 size_t extlen = strlen(newext) + 1;
189 if(extlen + last_dot >= buflen)
190 panic("filename too long");
191 memcpy(buf+last_dot, newext, extlen);
194 #include "builtins.h"
196 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
201 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
205 lexer_open_stream(in, input_name);
208 translation_unit_t *unit = finish_parsing();
212 static void lextest(FILE *in, const char *fname)
214 lexer_open_stream(in, fname);
217 lexer_next_preprocessing_token();
218 print_token(stdout, &lexer_token);
220 } while(lexer_token.type != T_EOF);
223 static void add_flag(struct obstack *obst, const char *format, ...)
226 va_start(ap, format);
229 vsnprintf(buf, sizeof(buf), format, ap);
231 /* escape stuff... */
232 obstack_1grow(obst, ' ');
233 for (char *c = buf; *c != '\0'; ++c) {
246 obstack_1grow(obst, '\\');
249 obstack_1grow(obst, *c);
257 static FILE *preprocess(const char *fname)
260 obstack_1grow(&cppflags_obst, '\0');
261 const char *flags = obstack_finish(&cppflags_obst);
263 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
268 FILE *f = popen(buf, "r");
270 fprintf(stderr, "invoking preprocessor failed\n");
276 static void assemble(const char *out, const char *in)
280 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
285 int err = system(buf);
287 fprintf(stderr, "assembler reported an error\n");
292 static const char *try_dir(const char *dir)
296 if(access(dir, R_OK | W_OK | X_OK) == 0)
301 static const char *get_tempdir(void)
303 static const char *tmpdir = NULL;
309 tmpdir = try_dir(getenv("TMPDIR"));
311 tmpdir = try_dir(getenv("TMP"));
313 tmpdir = try_dir(getenv("TEMP"));
317 tmpdir = try_dir(P_tmpdir);
321 tmpdir = try_dir("/var/tmp");
323 tmpdir = try_dir("/usr/tmp");
325 tmpdir = try_dir("/tmp");
334 /* cheap and nasty mkstemp replacement */
335 static int mkstemp(char *templ)
338 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
343 * an own version of tmpnam, which: writes in a buffer, emits no warnings
344 * during linking (like glibc/gnu ld do for tmpnam)...
346 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
348 const char *tempdir = get_tempdir();
350 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
352 int fd = mkstemp(buffer);
354 fprintf(stderr, "couldn't create temporary file: %s\n",
358 FILE *out = fdopen(fd, "w");
360 fprintf(stderr, "couldn't create temporary file FILE*\n");
368 * Do the necessary lowering for compound parameters.
370 void lower_compound_params(void)
372 lower_params_t params;
374 params.def_ptr_alignment = 4;
375 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
376 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
377 params.find_pointer_type = NULL;
378 params.ret_compound_in_regs = NULL;
379 lower_calls_with_compounds(¶ms);
382 typedef enum compile_mode_t {
396 static void usage(const char *argv0)
398 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
401 static void print_cparser_version(void) {
403 firm_get_version(&ver);
405 printf("cparser (%s) using libFirm (%u.%u",
406 cparser_REVISION, ver.major, ver.minor);
407 if(ver.revision[0] != 0) {
409 fputs(ver.revision, stdout);
411 if(ver.build[0] != 0) {
413 fputs(ver.build, stdout);
418 static void set_be_option(const char *arg)
420 int res = firm_be_option(arg);
425 static void set_option(const char *arg)
427 int res = firm_option(arg);
432 static void copy_file(FILE *dest, FILE *input)
436 while (!feof(input) && !ferror(dest)) {
437 size_t read = fread(buf, 1, sizeof(buf), input);
438 if(fwrite(buf, 1, read, dest) != read) {
439 perror("couldn't write output");
444 static FILE *open_file(const char *filename)
446 if (strcmp(filename, "-") == 0) {
450 FILE *in = fopen(filename, "r");
452 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
460 static filetype_t get_filetype_from_string(const char *string)
462 if (strcmp(string, "c") == 0 || strcmp(string, "c-header") == 0)
464 if (strcmp(string, "assembler") == 0)
465 return FILETYPE_PREPROCESSED_ASSEMBLER;
466 if (strcmp(string, "assembler-with-cpp") == 0)
467 return FILETYPE_ASSEMBLER;
468 if (strcmp(string, "none") == 0)
469 return FILETYPE_AUTODETECT;
471 return FILETYPE_UNKNOWN;
474 int main(int argc, char **argv)
478 const char *outname = NULL;
479 const char *dumpfunction = NULL;
480 compile_mode_t mode = CompileAssembleLink;
482 int result = EXIT_SUCCESS;
483 char cpu_arch[16] = "ia32";
484 file_list_entry_t *files = NULL;
485 file_list_entry_t *last_file = NULL;
486 struct obstack file_obst;
488 obstack_init(&cppflags_obst);
489 obstack_init(&ldflags_obst);
490 obstack_init(&file_obst);
492 #define GET_ARG_AFTER(def, args) \
493 def = &arg[sizeof(args)-1]; \
494 if(def[0] == '\0') { \
497 fprintf(stderr, "error: expected argument after '" args "'\n"); \
498 argument_errors = true; \
502 if(def[0] == '-' && def[1] != '\0') { \
503 fprintf(stderr, "error: expected argument after '" args "'\n"); \
504 argument_errors = true; \
509 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
511 /* early options parsing (find out optimisation level) */
512 for(int i = 1; i < argc; ++i) {
513 const char *arg = argv[i];
517 const char *option = &arg[1];
518 if(option[0] == 'O') {
519 sscanf(&option[1], "%d", &opt_level);
523 /* apply optimisation level */
526 set_option("no-opt");
529 set_option("no-inline");
533 set_option("strict-aliasing");
536 set_option("cond-eval");
537 set_option("if-conv");
541 set_option("inline");
542 set_option("deconv");
543 set_be_option("omitfp");
547 /* parse rest of options */
548 filetype_t forced_filetype = FILETYPE_AUTODETECT;
549 bool help_displayed = false;
550 bool argument_errors = false;
551 for(int i = 1; i < argc; ++i) {
552 const char *arg = argv[i];
553 if(arg[0] == '-' && arg[1] != 0) {
555 const char *option = &arg[1];
556 if(option[0] == 'o') {
557 GET_ARG_AFTER(outname, "-o");
558 } else if(option[0] == 'g') {
559 set_be_option("debuginfo=stabs");
560 set_be_option("omitfp=no");
561 set_be_option("ia32-nooptcc=yes");
562 } else if(SINGLE_OPTION('c')) {
563 mode = CompileAssemble;
564 } else if(SINGLE_OPTION('E')) {
565 mode = PreprocessOnly;
566 } else if(SINGLE_OPTION('S')) {
568 } else if(option[0] == 'O') {
570 } else if(option[0] == 'I') {
572 GET_ARG_AFTER(opt, "-I");
573 add_flag(&cppflags_obst, "-I%s", opt);
574 } else if(option[0] == 'D') {
576 GET_ARG_AFTER(opt, "-D");
577 add_flag(&cppflags_obst, "-D%s", opt);
578 } else if(option[0] == 'U') {
580 GET_ARG_AFTER(opt, "-U");
581 add_flag(&cppflags_obst, "-U%s", opt);
582 } else if(option[0] == 'l') {
584 GET_ARG_AFTER(opt, "-l");
585 add_flag(&ldflags_obst, "-l%s", opt);
586 } else if(option[0] == 'L') {
588 GET_ARG_AFTER(opt, "-L");
589 add_flag(&ldflags_obst, "-L%s", opt);
590 } else if(SINGLE_OPTION('v')) {
592 } else if(SINGLE_OPTION('w')) {
593 inhibit_all_warnings = true;
594 } else if(option[0] == 'x') {
596 GET_ARG_AFTER(opt, "-x");
597 forced_filetype = get_filetype_from_string(opt);
598 if (forced_filetype == FILETYPE_UNKNOWN) {
599 fprintf(stderr, "Unknown language '%s'\n", opt);
600 argument_errors = true;
602 } else if(strcmp(option, "M") == 0) {
603 mode = PreprocessOnly;
604 add_flag(&cppflags_obst, "-M");
605 } else if (strcmp(option, "MMD") == 0 ||
606 strcmp(option, "MD") == 0 ||
607 strcmp(option, "MM") == 0 ||
608 strcmp(option, "MP") == 0) {
609 add_flag(&cppflags_obst, "-%s", option);
610 } else if(strcmp(option, "MT") == 0
611 || strcmp(option, "MQ") == 0
612 || strcmp(option, "MF") == 0) {
614 GET_ARG_AFTER(opt, "-MT");
615 add_flag(&cppflags_obst, "-%s", option);
616 add_flag(&cppflags_obst, "%s", opt);
617 } else if(strcmp(option, "pipe") == 0) {
618 /* here for gcc compatibility */
619 } else if(option[0] == 'f') {
621 GET_ARG_AFTER(opt, "-f");
623 if(strcmp(opt, "syntax-only") == 0) {
625 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
626 set_be_option("omitfp");
627 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
628 set_be_option("omitfp=no");
629 } else if(strcmp(opt, "strength-reduce") == 0) {
630 firm_option("strength-red");
631 } else if(strcmp(opt, "fast-math") == 0
632 || strcmp(opt, "unroll-loops") == 0
633 || strcmp(opt, "expensive-optimizations") == 0
634 || strcmp(opt, "no-common") == 0
635 || strcmp(opt, "PIC") == 0
636 || strncmp(opt, "align-loops=", sizeof("align-loops=")-1) == 0
637 || strncmp(opt, "align-jumps=", sizeof("align-jumps=")-1) == 0
638 || strncmp(opt, "align-functions=", sizeof("align-functions=")-1) == 0) {
639 fprintf(stderr, "ignoring gcc option '-f %s'\n", opt);
641 int res = firm_option(opt);
643 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
645 argument_errors = true;
647 } else if (res == -1) {
648 help_displayed = true;
651 } else if(option[0] == 'b') {
653 GET_ARG_AFTER(opt, "-b");
654 int res = firm_be_option(opt);
656 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
658 argument_errors = true;
659 } else if (res == -1) {
660 help_displayed = true;
662 if (strncmp(opt, "isa=", 4) == 0)
663 strncpy(cpu_arch, opt, sizeof(cpu_arch));
665 } else if(option[0] == 'W') {
666 if(strncmp(option + 1, "l,", 2) == 0) // a gcc-style linker option
669 GET_ARG_AFTER(opt, "-Wl,");
670 add_flag(&ldflags_obst, "-Wl,%s", opt);
672 else set_warning_opt(&option[1]);
673 } else if(option[0] == 'm') {
678 GET_ARG_AFTER(opt, "-m");
679 if(strncmp(opt, "arch=", 5) == 0) {
680 GET_ARG_AFTER(opt, "-march=");
681 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
682 int res = firm_be_option(arch_opt);
684 argument_errors = true;
686 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
687 int res = firm_be_option(arch_opt);
689 argument_errors = true;
691 } else if(strncmp(opt, "tune=", 5) == 0) {
692 GET_ARG_AFTER(opt, "-mtune=");
693 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
694 int res = firm_be_option(arch_opt);
696 argument_errors = true;
697 } else if(strncmp(opt, "cpu=", 4) == 0) {
698 GET_ARG_AFTER(opt, "-mcpu=");
699 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
700 int res = firm_be_option(arch_opt);
702 argument_errors = true;
703 } else if(strncmp(opt, "fpmath=", 7) == 0) {
704 GET_ARG_AFTER(opt, "-mfpmath=");
705 if(strcmp(opt, "387") == 0)
707 else if(strcmp(opt, "sse") == 0)
710 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
711 argument_errors = true;
713 if(!argument_errors) {
714 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
715 int res = firm_be_option(arch_opt);
717 argument_errors = true;
719 } else if(strncmp(opt, "preferred-stack-boundary=", 25) == 0) {
720 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
721 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
722 int res = firm_be_option(arch_opt);
724 argument_errors = true;
725 } else if(strcmp(opt, "omit-leaf-frame-pointer") == 0) {
726 set_be_option("omitleaffp=1");
727 } else if(strcmp(opt, "no-omit-leaf-frame-pointer") == 0) {
728 set_be_option("omitleaffp=0");
731 long int value = strtol(opt, &endptr, 10);
732 if (*endptr != '\0') {
733 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
734 argument_errors = true;
736 if (value != 16 && value != 32 && value != 64) {
737 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
738 argument_errors = true;
740 machine_size = (unsigned int)value;
743 } else if(strcmp(option, "pg") == 0) {
744 set_be_option("gprof");
745 add_flag(&ldflags_obst, "-pg");
746 } else if(strcmp(option, "pedantic") == 0
747 || strcmp(option, "ansi") == 0) {
748 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
749 } else if(strcmp(option, "shared") == 0) {
750 add_flag(&ldflags_obst, "-shared");
751 } else if(strncmp(option, "std=", 4) == 0) {
752 if(strcmp(&option[4], "c99") == 0) {
754 } else if(strcmp(&option[4], "c89") == 0) {
756 } else if(strcmp(&option[4], "gnu99") == 0) {
757 c_mode = _C89|_C99|_GNUC;
758 } else if(strcmp(&option[4], "microsoft") == 0) {
759 c_mode = _C89|_C99|_MS;
761 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
762 } else if(strcmp(option, "version") == 0) {
763 print_cparser_version();
764 } else if (option[0] == '-') {
765 /* double dash option */
767 if(strcmp(option, "gcc") == 0) {
769 } else if(strcmp(option, "no-gcc") == 0) {
771 } else if(strcmp(option, "ms") == 0) {
773 } else if(strcmp(option, "no-ms") == 0) {
775 } else if(strcmp(option, "signed-chars") == 0) {
776 char_is_signed = true;
777 } else if(strcmp(option, "unsigned-chars") == 0) {
778 char_is_signed = false;
779 } else if(strcmp(option, "strict") == 0) {
781 } else if(strcmp(option, "lextest") == 0) {
783 } else if(strcmp(option, "benchmark") == 0) {
784 mode = BenchmarkParser;
785 } else if(strcmp(option, "print-ast") == 0) {
787 } else if(strcmp(option, "print-implicit-cast") == 0) {
788 print_implicit_casts = true;
789 } else if(strcmp(option, "print-parenthesis") == 0) {
790 print_parenthesis = true;
791 } else if(strcmp(option, "print-fluffy") == 0) {
793 } else if(strcmp(option, "print-caml") == 0) {
795 } else if(strcmp(option, "version") == 0) {
796 print_cparser_version();
798 } else if(strcmp(option, "dump-function") == 0) {
801 fprintf(stderr, "error: "
802 "expected argument after '--dump-function'\n");
803 argument_errors = true;
806 dumpfunction = argv[i];
809 fprintf(stderr, "error: unknown argument '%s'\n", arg);
810 argument_errors = true;
813 fprintf(stderr, "error: unknown argument '%s'\n", arg);
814 argument_errors = true;
817 filetype_t type = forced_filetype;
818 const char *filename = arg;
819 if (type == FILETYPE_AUTODETECT) {
820 size_t len = strlen(arg);
821 if (len < 2 && arg[0] == '-') {
822 /* - implicitely means C source file */
825 } else if (len > 2 && arg[len-2] == '.') {
827 case 'c': type = FILETYPE_C; break;
828 case 'h': type = FILETYPE_C; break;
829 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
830 case 'S': type = FILETYPE_ASSEMBLER; break;
833 case 'o': type = FILETYPE_OBJECT; break;
835 } else if (len > 3 && arg[len-3] == '.') {
836 if(strcmp(arg + len - 2, "so") == 0) {
837 type = FILETYPE_OBJECT;
841 if (type == FILETYPE_AUTODETECT) {
842 fprintf(stderr, "'%s': file format not recognized\n", arg);
847 file_list_entry_t *entry
848 = obstack_alloc(&file_obst, sizeof(entry[0]));
849 memset(entry, 0, sizeof(entry[0]));
853 if (last_file != NULL) {
854 last_file->next = entry;
863 fprintf(stderr, "error: no input files specified\n");
864 argument_errors = true;
867 if (help_displayed) {
868 return !argument_errors;
870 if (argument_errors) {
875 /* we do the lowering in ast2firm */
876 firm_opt.lower_bitfields = FALSE;
889 char outnamebuf[4096];
890 if (outname == NULL) {
891 const char *filename = files->name;
894 case BenchmarkParser:
905 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
906 outname = outnamebuf;
908 case CompileAssemble:
909 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
910 outname = outnamebuf;
913 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
915 outname = outnamebuf;
917 case CompileAssembleLink:
927 assert(outname != NULL);
930 if(strcmp(outname, "-") == 0) {
933 out = fopen(outname, "w");
935 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
941 file_list_entry_t *file;
942 for (file = files; file != NULL; file = file->next) {
943 char asm_tempfile[1024];
944 const char *filename = file->name;
945 filetype_t filetype = file->type;
947 if (file->type == FILETYPE_OBJECT)
951 if (mode == LexTest) {
953 in = open_file(filename);
954 lextest(in, filename);
959 FILE *preprocessed_in = NULL;
960 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
961 /* no support for input on FILE* yet */
963 panic("internal compiler error: in for preprocessor != NULL");
965 preprocessed_in = preprocess(filename);
966 if (mode == PreprocessOnly) {
967 copy_file(out, preprocessed_in);
968 int result = pclose(preprocessed_in);
973 if (filetype == FILETYPE_C) {
974 filetype = FILETYPE_PREPROCESSED_C;
975 } else if (filetype == FILETYPE_ASSEMBLER) {
976 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
978 panic("internal compiler error: unknown filetype at preproc");
981 in = preprocessed_in;
985 if(mode == Compile) {
988 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
993 in = open_file(filename);
995 /* preprocess and compile */
996 if (filetype == FILETYPE_PREPROCESSED_C) {
997 translation_unit_t *const unit = do_parsing(in, filename);
998 if (in == preprocessed_in) {
999 int pp_result = pclose(preprocessed_in);
1000 if (pp_result != EXIT_SUCCESS) {
1005 /* prints the AST even if errors occurred */
1006 if (mode == PrintAst) {
1007 type_set_output(out);
1008 ast_set_output(out);
1012 if(error_count > 0) {
1013 /* parsing failed because of errors */
1014 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1016 result = EXIT_FAILURE;
1018 } else if(warning_count > 0) {
1019 fprintf(stderr, "%u warning(s)\n", warning_count);
1022 if(mode == BenchmarkParser) {
1024 } else if(mode == PrintFluffy) {
1025 write_fluffy_decls(out, unit);
1027 } else if (mode == PrintCaml) {
1028 write_caml_decls(out, unit);
1032 translation_unit_to_firm(unit);
1034 if (mode == ParseOnly) {
1038 if (mode == CompileDump) {
1040 ident *id = new_id_from_str(dumpfunction);
1041 ir_graph *irg = NULL;
1042 int n_irgs = get_irp_n_irgs();
1043 for(int i = 0; i < n_irgs; ++i) {
1044 ir_graph *tirg = get_irp_irg(i);
1045 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1053 fprintf(stderr, "No graph for function '%s' found\n",
1058 dump_ir_block_graph_file(irg, out);
1063 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1064 /*firm_const_exists=*/0);
1065 if (asm_out != out) {
1069 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1070 copy_file(asm_out, in);
1071 if (in == preprocessed_in) {
1072 int pp_result = pclose(preprocessed_in);
1073 if (pp_result != EXIT_SUCCESS) {
1077 if(asm_out != out) {
1082 if (mode == Compile)
1085 /* if we're here then we have preprocessed assembly */
1086 filename = asm_tempfile;
1087 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1090 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1092 const char *filename_o;
1093 if(mode == CompileAssemble) {
1095 filename_o = outname;
1097 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1102 assemble(filename_o, filename);
1104 size_t len = strlen(filename_o) + 1;
1105 filename = obstack_copy(&file_obst, filename_o, len);
1106 filetype = FILETYPE_OBJECT;
1109 /* ok we're done here, process next file */
1110 file->name = filename;
1111 file->type = filetype;
1114 if (result != EXIT_SUCCESS)
1117 /* link program file */
1118 if(mode == CompileAssembleLink) {
1119 obstack_1grow(&ldflags_obst, '\0');
1120 const char *flags = obstack_finish(&ldflags_obst);
1122 /* construct commandline */
1123 obstack_printf(&file_obst, "%s", LINKER);
1124 for (file_list_entry_t *entry = files; entry != NULL;
1125 entry = entry->next) {
1126 if (entry->type != FILETYPE_OBJECT)
1129 add_flag(&file_obst, "%s", entry->name);
1132 add_flag(&file_obst, "-o");
1133 add_flag(&file_obst, outname);
1134 obstack_printf(&file_obst, "%s", flags);
1135 obstack_1grow(&file_obst, '\0');
1137 char *commandline = obstack_finish(&file_obst);
1142 int err = system(commandline);
1143 if(err != EXIT_SUCCESS) {
1144 fprintf(stderr, "linker reported an error\n");
1149 obstack_free(&cppflags_obst, NULL);
1150 obstack_free(&ldflags_obst, NULL);
1151 obstack_free(&file_obst, NULL);
1160 exit_symbol_table();