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') {
617 bool truth_value = true;
619 GET_ARG_AFTER(opt, "-f");
620 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
625 if (strcmp(opt, "dollars-in-identifiers") == 0) {
626 allow_dollar_in_symbol = truth_value;
627 } else if (strcmp(opt, "short-wchar") == 0) {
628 opt_short_wchar_t = truth_value;
629 } else if (strcmp(opt, "syntax-only") == 0) {
631 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
632 set_be_option(truth_value ? "omitfp" : "omitfp=no");
633 } else if(strcmp(opt, "strength-reduce") == 0) {
634 firm_option("strength-red");
635 } else if (strcmp(opt, "fast-math") == 0
636 || strcmp(opt, "jump-tables") == 0
637 || strcmp(opt, "unroll-loops") == 0
638 || strcmp(opt, "expensive-optimizations") == 0
639 || strcmp(opt, "no-common") == 0
640 || strcmp(opt, "PIC") == 0
641 || strncmp(opt, "align-loops=", sizeof("align-loops=")-1) == 0
642 || strncmp(opt, "align-jumps=", sizeof("align-jumps=")-1) == 0
643 || strncmp(opt, "align-functions=", sizeof("align-functions=")-1) == 0) {
644 fprintf(stderr, "ignoring gcc option '-f %s'\n", truth_value ? opt : opt - 3);
648 int res = firm_option(opt);
650 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
652 argument_errors = true;
654 } else if (res == -1) {
655 help_displayed = true;
658 } else if (option[0] == 'b') {
660 GET_ARG_AFTER(opt, "-b");
661 int res = firm_be_option(opt);
663 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
665 argument_errors = true;
666 } else if (res == -1) {
667 help_displayed = true;
669 if (strncmp(opt, "isa=", 4) == 0)
670 strncpy(cpu_arch, opt, sizeof(cpu_arch));
672 } else if (option[0] == 'W') {
673 if (strncmp(option + 1, "l,", 2) == 0) // a gcc-style linker option
676 GET_ARG_AFTER(opt, "-Wl,");
677 add_flag(&ldflags_obst, "-Wl,%s", opt);
679 else set_warning_opt(&option[1]);
680 } else if(option[0] == 'm') {
685 GET_ARG_AFTER(opt, "-m");
686 if (strncmp(opt, "arch=", 5) == 0) {
687 GET_ARG_AFTER(opt, "-march=");
688 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
689 int res = firm_be_option(arch_opt);
691 argument_errors = true;
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;
698 } else if(strncmp(opt, "tune=", 5) == 0) {
699 GET_ARG_AFTER(opt, "-mtune=");
700 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
701 int res = firm_be_option(arch_opt);
703 argument_errors = true;
704 } else if(strncmp(opt, "cpu=", 4) == 0) {
705 GET_ARG_AFTER(opt, "-mcpu=");
706 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
707 int res = firm_be_option(arch_opt);
709 argument_errors = true;
710 } else if(strncmp(opt, "fpmath=", 7) == 0) {
711 GET_ARG_AFTER(opt, "-mfpmath=");
712 if(strcmp(opt, "387") == 0)
714 else if(strcmp(opt, "sse") == 0)
717 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
718 argument_errors = true;
720 if(!argument_errors) {
721 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
722 int res = firm_be_option(arch_opt);
724 argument_errors = true;
726 } else if(strncmp(opt, "preferred-stack-boundary=", 25) == 0) {
727 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
728 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
729 int res = firm_be_option(arch_opt);
731 argument_errors = true;
732 } else if(strcmp(opt, "omit-leaf-frame-pointer") == 0) {
733 set_be_option("omitleaffp=1");
734 } else if(strcmp(opt, "no-omit-leaf-frame-pointer") == 0) {
735 set_be_option("omitleaffp=0");
738 long int value = strtol(opt, &endptr, 10);
739 if (*endptr != '\0') {
740 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
741 argument_errors = true;
743 if (value != 16 && value != 32 && value != 64) {
744 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
745 argument_errors = true;
747 machine_size = (unsigned int)value;
750 } else if(strcmp(option, "pg") == 0) {
751 set_be_option("gprof");
752 add_flag(&ldflags_obst, "-pg");
753 } else if(strcmp(option, "pedantic") == 0
754 || strcmp(option, "ansi") == 0) {
755 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
756 } else if(strcmp(option, "shared") == 0) {
757 add_flag(&ldflags_obst, "-shared");
758 } else if(strncmp(option, "std=", 4) == 0) {
759 if(strcmp(&option[4], "c99") == 0) {
761 } else if(strcmp(&option[4], "c89") == 0) {
763 } else if(strcmp(&option[4], "gnu99") == 0) {
764 c_mode = _C89|_C99|_GNUC;
765 } else if(strcmp(&option[4], "microsoft") == 0) {
766 c_mode = _C89|_C99|_MS;
768 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
769 } else if(strcmp(option, "version") == 0) {
770 print_cparser_version();
771 } else if (option[0] == '-') {
772 /* double dash option */
774 if(strcmp(option, "gcc") == 0) {
776 } else if(strcmp(option, "no-gcc") == 0) {
778 } else if(strcmp(option, "ms") == 0) {
780 } else if(strcmp(option, "no-ms") == 0) {
782 } else if(strcmp(option, "signed-chars") == 0) {
783 char_is_signed = true;
784 } else if(strcmp(option, "unsigned-chars") == 0) {
785 char_is_signed = false;
786 } else if(strcmp(option, "strict") == 0) {
788 } else if(strcmp(option, "lextest") == 0) {
790 } else if(strcmp(option, "benchmark") == 0) {
791 mode = BenchmarkParser;
792 } else if(strcmp(option, "print-ast") == 0) {
794 } else if(strcmp(option, "print-implicit-cast") == 0) {
795 print_implicit_casts = true;
796 } else if(strcmp(option, "print-parenthesis") == 0) {
797 print_parenthesis = true;
798 } else if(strcmp(option, "print-fluffy") == 0) {
800 } else if(strcmp(option, "print-caml") == 0) {
802 } else if(strcmp(option, "version") == 0) {
803 print_cparser_version();
805 } else if(strcmp(option, "dump-function") == 0) {
808 fprintf(stderr, "error: "
809 "expected argument after '--dump-function'\n");
810 argument_errors = true;
813 dumpfunction = argv[i];
816 fprintf(stderr, "error: unknown argument '%s'\n", arg);
817 argument_errors = true;
820 fprintf(stderr, "error: unknown argument '%s'\n", arg);
821 argument_errors = true;
824 filetype_t type = forced_filetype;
825 const char *filename = arg;
826 if (type == FILETYPE_AUTODETECT) {
827 size_t const len = strlen(arg);
828 if (len < 2 && arg[0] == '-') {
829 /* - implicitly means C source file */
832 } else if (len > 2 && arg[len - 2] == '.') {
833 switch (arg[len - 1]) {
834 case 'c': type = FILETYPE_C; break;
835 case 'h': type = FILETYPE_C; break;
836 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
837 case 'S': type = FILETYPE_ASSEMBLER; break;
840 case 'o': type = FILETYPE_OBJECT; break;
842 } else if (len > 3 && arg[len - 3] == '.') {
843 if (strcmp(arg + len - 2, "so") == 0) {
844 type = FILETYPE_OBJECT;
848 if (type == FILETYPE_AUTODETECT) {
849 fprintf(stderr, "'%s': file format not recognized\n", arg);
854 file_list_entry_t *entry
855 = obstack_alloc(&file_obst, sizeof(entry[0]));
856 memset(entry, 0, sizeof(entry[0]));
860 if (last_file != NULL) {
861 last_file->next = entry;
870 fprintf(stderr, "error: no input files specified\n");
871 argument_errors = true;
874 if (help_displayed) {
875 return !argument_errors;
877 if (argument_errors) {
882 /* we do the lowering in ast2firm */
883 firm_opt.lower_bitfields = FALSE;
896 char outnamebuf[4096];
897 if (outname == NULL) {
898 const char *filename = files->name;
901 case BenchmarkParser:
912 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
913 outname = outnamebuf;
915 case CompileAssemble:
916 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
917 outname = outnamebuf;
920 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
922 outname = outnamebuf;
924 case CompileAssembleLink:
934 assert(outname != NULL);
937 if(strcmp(outname, "-") == 0) {
940 out = fopen(outname, "w");
942 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
948 file_list_entry_t *file;
949 for (file = files; file != NULL; file = file->next) {
950 char asm_tempfile[1024];
951 const char *filename = file->name;
952 filetype_t filetype = file->type;
954 if (file->type == FILETYPE_OBJECT)
958 if (mode == LexTest) {
960 in = open_file(filename);
961 lextest(in, filename);
966 FILE *preprocessed_in = NULL;
967 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
968 /* no support for input on FILE* yet */
970 panic("internal compiler error: in for preprocessor != NULL");
972 preprocessed_in = preprocess(filename);
973 if (mode == PreprocessOnly) {
974 copy_file(out, preprocessed_in);
975 int result = pclose(preprocessed_in);
980 if (filetype == FILETYPE_C) {
981 filetype = FILETYPE_PREPROCESSED_C;
982 } else if (filetype == FILETYPE_ASSEMBLER) {
983 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
985 panic("internal compiler error: unknown filetype at preproc");
988 in = preprocessed_in;
992 if(mode == Compile) {
995 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1000 in = open_file(filename);
1002 /* preprocess and compile */
1003 if (filetype == FILETYPE_PREPROCESSED_C) {
1004 translation_unit_t *const unit = do_parsing(in, filename);
1005 if (in == preprocessed_in) {
1006 int pp_result = pclose(preprocessed_in);
1007 if (pp_result != EXIT_SUCCESS) {
1012 /* prints the AST even if errors occurred */
1013 if (mode == PrintAst) {
1014 type_set_output(out);
1015 ast_set_output(out);
1019 if(error_count > 0) {
1020 /* parsing failed because of errors */
1021 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1023 result = EXIT_FAILURE;
1025 } else if(warning_count > 0) {
1026 fprintf(stderr, "%u warning(s)\n", warning_count);
1029 if(mode == BenchmarkParser) {
1031 } else if(mode == PrintFluffy) {
1032 write_fluffy_decls(out, unit);
1034 } else if (mode == PrintCaml) {
1035 write_caml_decls(out, unit);
1039 translation_unit_to_firm(unit);
1041 if (mode == ParseOnly) {
1045 if (mode == CompileDump) {
1047 ident *id = new_id_from_str(dumpfunction);
1048 ir_graph *irg = NULL;
1049 int n_irgs = get_irp_n_irgs();
1050 for(int i = 0; i < n_irgs; ++i) {
1051 ir_graph *tirg = get_irp_irg(i);
1052 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1060 fprintf(stderr, "No graph for function '%s' found\n",
1065 dump_ir_block_graph_file(irg, out);
1070 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1071 /*firm_const_exists=*/0);
1072 if (asm_out != out) {
1076 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1077 copy_file(asm_out, in);
1078 if (in == preprocessed_in) {
1079 int pp_result = pclose(preprocessed_in);
1080 if (pp_result != EXIT_SUCCESS) {
1084 if(asm_out != out) {
1089 if (mode == Compile)
1092 /* if we're here then we have preprocessed assembly */
1093 filename = asm_tempfile;
1094 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1097 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1099 const char *filename_o;
1100 if(mode == CompileAssemble) {
1102 filename_o = outname;
1104 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1109 assemble(filename_o, filename);
1111 size_t len = strlen(filename_o) + 1;
1112 filename = obstack_copy(&file_obst, filename_o, len);
1113 filetype = FILETYPE_OBJECT;
1116 /* ok we're done here, process next file */
1117 file->name = filename;
1118 file->type = filetype;
1121 if (result != EXIT_SUCCESS)
1124 /* link program file */
1125 if(mode == CompileAssembleLink) {
1126 obstack_1grow(&ldflags_obst, '\0');
1127 const char *flags = obstack_finish(&ldflags_obst);
1129 /* construct commandline */
1130 obstack_printf(&file_obst, "%s", LINKER);
1131 for (file_list_entry_t *entry = files; entry != NULL;
1132 entry = entry->next) {
1133 if (entry->type != FILETYPE_OBJECT)
1136 add_flag(&file_obst, "%s", entry->name);
1139 add_flag(&file_obst, "-o");
1140 add_flag(&file_obst, outname);
1141 obstack_printf(&file_obst, "%s", flags);
1142 obstack_1grow(&file_obst, '\0');
1144 char *commandline = obstack_finish(&file_obst);
1149 int err = system(commandline);
1150 if(err != EXIT_SUCCESS) {
1151 fprintf(stderr, "linker reported an error\n");
1156 obstack_free(&cppflags_obst, NULL);
1157 obstack_free(&ldflags_obst, NULL);
1158 obstack_free(&file_obst, NULL);
1167 exit_symbol_table();