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;
117 static char dep_target[1024];
119 typedef struct file_list_entry_t file_list_entry_t;
121 typedef enum filetype_t {
124 FILETYPE_PREPROCESSED_C,
126 FILETYPE_PREPROCESSED_ASSEMBLER,
131 struct file_list_entry_t {
132 const char *name; /**< filename or NULL for stdin */
134 file_list_entry_t *next;
137 #if defined(_DEBUG) || defined(FIRM_DEBUG)
139 * Debug printf implementation.
141 * @param fmt printf style format parameter
143 void dbg_printf(const char *fmt, ...)
147 if (firm_dump.debug_print) {
153 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
155 static void initialize_firm(void)
159 dump_consts_local(1);
160 dump_keepalive_edges(1);
163 static void get_output_name(char *buf, size_t buflen, const char *inputname,
166 size_t last_dot = 0xffffffff;
169 if(inputname == NULL) {
170 snprintf(buf, buflen, "a%s", newext);
174 for(const char *c = inputname; *c != 0; ++c) {
179 if(last_dot == 0xffffffff)
182 if(last_dot >= buflen)
183 panic("filename too long");
184 memcpy(buf, inputname, last_dot);
186 size_t extlen = strlen(newext) + 1;
187 if(extlen + last_dot >= buflen)
188 panic("filename too long");
189 memcpy(buf+last_dot, newext, extlen);
192 #include "builtins.h"
194 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
199 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
203 lexer_open_stream(in, input_name);
206 translation_unit_t *unit = finish_parsing();
210 static void lextest(FILE *in, const char *fname)
212 lexer_open_stream(in, fname);
215 lexer_next_preprocessing_token();
216 print_token(stdout, &lexer_token);
218 } while(lexer_token.type != T_EOF);
221 static void add_flag(struct obstack *obst, const char *format, ...)
224 va_start(ap, format);
227 vsnprintf(buf, sizeof(buf), format, ap);
229 /* escape stuff... */
230 obstack_1grow(obst, ' ');
231 for (char *c = buf; *c != '\0'; ++c) {
244 obstack_1grow(obst, '\\');
247 obstack_1grow(obst, *c);
255 static FILE *preprocess(const char *fname)
257 obstack_1grow(&cppflags_obst, '\0');
258 const char *flags = obstack_finish(&cppflags_obst);
260 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
261 if (dep_target[0] != '\0') {
262 obstack_printf(&cppflags_obst, " -MF %s", dep_target);
264 if (flags[0] != '\0') {
265 obstack_printf(&cppflags_obst, " %s", flags);
267 obstack_printf(&cppflags_obst, " %s", fname);
269 obstack_1grow(&cppflags_obst, '\0');
270 const char *buf = obstack_finish(&cppflags_obst);
275 FILE *f = popen(buf, "r");
277 fprintf(stderr, "invoking preprocessor failed\n");
284 static void assemble(const char *out, const char *in)
288 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
293 int err = system(buf);
295 fprintf(stderr, "assembler reported an error\n");
300 static const char *try_dir(const char *dir)
304 if(access(dir, R_OK | W_OK | X_OK) == 0)
309 static const char *get_tempdir(void)
311 static const char *tmpdir = NULL;
317 tmpdir = try_dir(getenv("TMPDIR"));
319 tmpdir = try_dir(getenv("TMP"));
321 tmpdir = try_dir(getenv("TEMP"));
325 tmpdir = try_dir(P_tmpdir);
329 tmpdir = try_dir("/var/tmp");
331 tmpdir = try_dir("/usr/tmp");
333 tmpdir = try_dir("/tmp");
342 /* cheap and nasty mkstemp replacement */
343 static int mkstemp(char *templ)
346 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
351 * an own version of tmpnam, which: writes in a buffer, emits no warnings
352 * during linking (like glibc/gnu ld do for tmpnam)...
354 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
356 const char *tempdir = get_tempdir();
358 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
360 int fd = mkstemp(buffer);
362 fprintf(stderr, "couldn't create temporary file: %s\n",
366 FILE *out = fdopen(fd, "w");
368 fprintf(stderr, "couldn't create temporary file FILE*\n");
376 * Do the necessary lowering for compound parameters.
378 void lower_compound_params(void)
380 lower_params_t params;
382 params.def_ptr_alignment = 4;
383 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
384 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
385 params.find_pointer_type = NULL;
386 params.ret_compound_in_regs = NULL;
387 lower_calls_with_compounds(¶ms);
390 typedef enum compile_mode_t {
404 static void usage(const char *argv0)
406 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
409 static void print_cparser_version(void) {
411 firm_get_version(&ver);
413 printf("cparser (%s) using libFirm (%u.%u",
414 cparser_REVISION, ver.major, ver.minor);
415 if(ver.revision[0] != 0) {
417 fputs(ver.revision, stdout);
419 if(ver.build[0] != 0) {
421 fputs(ver.build, stdout);
426 static void set_be_option(const char *arg)
428 int res = firm_be_option(arg);
433 static void set_option(const char *arg)
435 int res = firm_option(arg);
440 static void copy_file(FILE *dest, FILE *input)
444 while (!feof(input) && !ferror(dest)) {
445 size_t read = fread(buf, 1, sizeof(buf), input);
446 if(fwrite(buf, 1, read, dest) != read) {
447 perror("couldn't write output");
452 static inline bool streq(char const* a, char const* b)
454 return strcmp(a, b) == 0;
457 static inline bool strstart(char const* str, char const* start)
462 } while (*str++ == *start++);
466 static FILE *open_file(const char *filename)
468 if (streq(filename, "-")) {
472 FILE *in = fopen(filename, "r");
474 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
482 static filetype_t get_filetype_from_string(const char *string)
484 if (streq(string, "c") || streq(string, "c-header"))
486 if (streq(string, "assembler"))
487 return FILETYPE_PREPROCESSED_ASSEMBLER;
488 if (streq(string, "assembler-with-cpp"))
489 return FILETYPE_ASSEMBLER;
490 if (streq(string, "none"))
491 return FILETYPE_AUTODETECT;
493 return FILETYPE_UNKNOWN;
496 int main(int argc, char **argv)
500 const char *outname = NULL;
501 const char *dumpfunction = NULL;
502 compile_mode_t mode = CompileAssembleLink;
504 int result = EXIT_SUCCESS;
505 char cpu_arch[16] = "ia32";
506 file_list_entry_t *files = NULL;
507 file_list_entry_t *last_file = NULL;
508 bool construct_dep_target = false;
509 struct obstack file_obst;
511 obstack_init(&cppflags_obst);
512 obstack_init(&ldflags_obst);
513 obstack_init(&file_obst);
515 #define GET_ARG_AFTER(def, args) \
516 def = &arg[sizeof(args)-1]; \
517 if(def[0] == '\0') { \
520 fprintf(stderr, "error: expected argument after '" args "'\n"); \
521 argument_errors = true; \
525 if(def[0] == '-' && def[1] != '\0') { \
526 fprintf(stderr, "error: expected argument after '" args "'\n"); \
527 argument_errors = true; \
532 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
534 /* early options parsing (find out optimisation level) */
535 for(int i = 1; i < argc; ++i) {
536 const char *arg = argv[i];
540 const char *option = &arg[1];
541 if(option[0] == 'O') {
542 sscanf(&option[1], "%d", &opt_level);
546 /* apply optimisation level */
549 set_option("no-opt");
552 set_option("no-inline");
556 set_option("strict-aliasing");
559 set_option("cond-eval");
560 set_option("if-conv");
564 set_option("inline");
565 set_option("deconv");
566 set_be_option("omitfp");
570 /* parse rest of options */
571 filetype_t forced_filetype = FILETYPE_AUTODETECT;
572 bool help_displayed = false;
573 bool argument_errors = false;
574 for(int i = 1; i < argc; ++i) {
575 const char *arg = argv[i];
576 if(arg[0] == '-' && arg[1] != 0) {
578 const char *option = &arg[1];
579 if(option[0] == 'o') {
580 GET_ARG_AFTER(outname, "-o");
581 } else if(option[0] == 'g') {
582 set_be_option("debuginfo=stabs");
583 set_be_option("omitfp=no");
584 set_be_option("ia32-nooptcc=yes");
585 } else if(SINGLE_OPTION('c')) {
586 mode = CompileAssemble;
587 } else if(SINGLE_OPTION('E')) {
588 mode = PreprocessOnly;
589 } else if(SINGLE_OPTION('S')) {
591 } else if(option[0] == 'O') {
593 } else if(option[0] == 'I') {
595 GET_ARG_AFTER(opt, "-I");
596 add_flag(&cppflags_obst, "-I%s", opt);
597 } else if(option[0] == 'D') {
599 GET_ARG_AFTER(opt, "-D");
600 add_flag(&cppflags_obst, "-D%s", opt);
601 } else if(option[0] == 'U') {
603 GET_ARG_AFTER(opt, "-U");
604 add_flag(&cppflags_obst, "-U%s", opt);
605 } else if(option[0] == 'l') {
607 GET_ARG_AFTER(opt, "-l");
608 add_flag(&ldflags_obst, "-l%s", opt);
609 } else if(option[0] == 'L') {
611 GET_ARG_AFTER(opt, "-L");
612 add_flag(&ldflags_obst, "-L%s", opt);
613 } else if(SINGLE_OPTION('v')) {
615 } else if(SINGLE_OPTION('w')) {
616 inhibit_all_warnings = true;
617 } else if(option[0] == 'x') {
619 GET_ARG_AFTER(opt, "-x");
620 forced_filetype = get_filetype_from_string(opt);
621 if (forced_filetype == FILETYPE_UNKNOWN) {
622 fprintf(stderr, "Unknown language '%s'\n", opt);
623 argument_errors = true;
625 } else if (streq(option, "M")) {
626 mode = PreprocessOnly;
627 add_flag(&cppflags_obst, "-M");
628 } else if (streq(option, "MMD") ||
629 streq(option, "MD")) {
630 construct_dep_target = true;
631 add_flag(&cppflags_obst, "-%s", option);
632 } else if(streq(option, "MM") ||
633 streq(option, "MP")) {
634 add_flag(&cppflags_obst, "-%s", option);
635 } else if (streq(option, "MT") ||
636 streq(option, "MQ") ||
637 streq(option, "MF")) {
639 GET_ARG_AFTER(opt, "-MT");
640 add_flag(&cppflags_obst, "-%s", option);
641 add_flag(&cppflags_obst, "%s", opt);
642 } else if (streq(option, "pipe")) {
643 /* here for gcc compatibility */
644 } else if (option[0] == 'f') {
645 char const *orig_opt;
646 GET_ARG_AFTER(orig_opt, "-f");
648 if (strstart(orig_opt, "align-loops=") ||
649 strstart(orig_opt, "align-jumps=") ||
650 strstart(orig_opt, "align-functions=")) {
651 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
653 char const *opt = orig_opt;
654 bool truth_value = true;
655 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
660 if (streq(opt, "dollars-in-identifiers")) {
661 allow_dollar_in_symbol = truth_value;
662 } else if (streq(opt, "short-wchar")) {
663 opt_short_wchar_t = truth_value;
664 } else if (streq(opt, "syntax-only")) {
665 mode = truth_value ? ParseOnly : CompileAssembleLink;
666 } else if (streq(opt, "omit-frame-pointer")) {
667 set_be_option(truth_value ? "omitfp" : "omitfp=no");
668 } else if (streq(opt, "strength-reduce")) {
669 firm_option(truth_value ? "strength-red" : "no-strength-red");
670 } else if (streq(opt, "fast-math") ||
671 streq(opt, "jump-tables") ||
672 streq(opt, "unroll-loops") ||
673 streq(opt, "expensive-optimizations") ||
674 streq(opt, "common") ||
676 streq(opt, "align-loops") ||
677 streq(opt, "align-jumps") ||
678 streq(opt, "align-functions")) {
679 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
681 int res = firm_option(orig_opt);
683 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
685 argument_errors = true;
687 } else if (res == -1) {
688 help_displayed = true;
692 } else if (option[0] == 'b') {
694 GET_ARG_AFTER(opt, "-b");
695 int res = firm_be_option(opt);
697 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
699 argument_errors = true;
700 } else if (res == -1) {
701 help_displayed = true;
702 } else if (strstart(opt, "isa=")) {
703 strncpy(cpu_arch, opt, sizeof(cpu_arch));
705 } else if (option[0] == 'W') {
706 if (strstart(option + 1, "l,")) // a gcc-style linker option
709 GET_ARG_AFTER(opt, "-Wl,");
710 add_flag(&ldflags_obst, "-Wl,%s", opt);
712 else set_warning_opt(&option[1]);
713 } else if(option[0] == 'm') {
718 GET_ARG_AFTER(opt, "-m");
719 if (strstart(opt, "arch=")) {
720 GET_ARG_AFTER(opt, "-march=");
721 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
722 int res = firm_be_option(arch_opt);
724 argument_errors = true;
726 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
727 int res = firm_be_option(arch_opt);
729 argument_errors = true;
731 } else if (strstart(opt, "tune=")) {
732 GET_ARG_AFTER(opt, "-mtune=");
733 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
734 int res = firm_be_option(arch_opt);
736 argument_errors = true;
737 } else if (strstart(opt, "cpu=")) {
738 GET_ARG_AFTER(opt, "-mcpu=");
739 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
740 int res = firm_be_option(arch_opt);
742 argument_errors = true;
743 } else if (strstart(opt, "fpmath=")) {
744 GET_ARG_AFTER(opt, "-mfpmath=");
745 if (streq(opt, "387"))
747 else if (streq(opt, "sse"))
750 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
751 argument_errors = true;
753 if(!argument_errors) {
754 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
755 int res = firm_be_option(arch_opt);
757 argument_errors = true;
759 } else if (strstart(opt, "preferred-stack-boundary=")) {
760 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
761 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
762 int res = firm_be_option(arch_opt);
764 argument_errors = true;
765 } else if (streq(opt, "omit-leaf-frame-pointer")) {
766 set_be_option("omitleaffp=1");
767 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
768 set_be_option("omitleaffp=0");
771 long int value = strtol(opt, &endptr, 10);
772 if (*endptr != '\0') {
773 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
774 argument_errors = true;
776 if (value != 16 && value != 32 && value != 64) {
777 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
778 argument_errors = true;
780 machine_size = (unsigned int)value;
783 } else if (streq(option, "pg")) {
784 set_be_option("gprof");
785 add_flag(&ldflags_obst, "-pg");
786 } else if (streq(option, "pedantic") ||
787 streq(option, "ansi")) {
788 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
789 } else if (streq(option, "shared")) {
790 add_flag(&ldflags_obst, "-shared");
791 } else if (strstart(option, "std=")) {
792 if (streq(&option[4], "c99")) {
794 } else if (streq(&option[4], "c89")) {
796 } else if (streq(&option[4], "gnu99")) {
797 c_mode = _C89|_C99|_GNUC;
798 } else if (streq(&option[4], "microsoft")) {
799 c_mode = _C89|_C99|_MS;
801 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
802 } else if (streq(option, "version")) {
803 print_cparser_version();
804 } else if (option[0] == '-') {
805 /* double dash option */
807 if (streq(option, "gcc")) {
809 } else if (streq(option, "no-gcc")) {
811 } else if (streq(option, "ms")) {
813 } else if (streq(option, "no-ms")) {
815 } else if (streq(option, "signed-chars")) {
816 char_is_signed = true;
817 } else if (streq(option, "unsigned-chars")) {
818 char_is_signed = false;
819 } else if (streq(option, "strict")) {
821 } else if (streq(option, "lextest")) {
823 } else if (streq(option, "benchmark")) {
824 mode = BenchmarkParser;
825 } else if (streq(option, "print-ast")) {
827 } else if (streq(option, "print-implicit-cast")) {
828 print_implicit_casts = true;
829 } else if (streq(option, "print-parenthesis")) {
830 print_parenthesis = true;
831 } else if (streq(option, "print-fluffy")) {
833 } else if (streq(option, "print-caml")) {
835 } else if (streq(option, "version")) {
836 print_cparser_version();
838 } else if (streq(option, "dump-function")) {
841 fprintf(stderr, "error: "
842 "expected argument after '--dump-function'\n");
843 argument_errors = true;
846 dumpfunction = argv[i];
849 fprintf(stderr, "error: unknown argument '%s'\n", arg);
850 argument_errors = true;
853 fprintf(stderr, "error: unknown argument '%s'\n", arg);
854 argument_errors = true;
857 filetype_t type = forced_filetype;
858 const char *filename = arg;
859 if (type == FILETYPE_AUTODETECT) {
860 size_t const len = strlen(arg);
861 if (len < 2 && arg[0] == '-') {
862 /* - implicitly means C source file */
865 } else if (len > 2 && arg[len - 2] == '.') {
866 switch (arg[len - 1]) {
867 case 'c': type = FILETYPE_C; break;
868 case 'h': type = FILETYPE_C; break;
869 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
870 case 'S': type = FILETYPE_ASSEMBLER; break;
873 case 'o': type = FILETYPE_OBJECT; break;
875 } else if (len > 3 && arg[len - 3] == '.') {
876 if (streq(arg + len - 2, "so")) {
877 type = FILETYPE_OBJECT;
881 if (type == FILETYPE_AUTODETECT) {
882 fprintf(stderr, "'%s': file format not recognized\n", arg);
887 file_list_entry_t *entry
888 = obstack_alloc(&file_obst, sizeof(entry[0]));
889 memset(entry, 0, sizeof(entry[0]));
893 if (last_file != NULL) {
894 last_file->next = entry;
903 fprintf(stderr, "error: no input files specified\n");
904 argument_errors = true;
907 if (help_displayed) {
908 return !argument_errors;
910 if (argument_errors) {
915 /* we do the lowering in ast2firm */
916 firm_opt.lower_bitfields = FALSE;
929 if (construct_dep_target) {
930 if (outname != 0 && strlen(outname) >= 2) {
931 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
933 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
936 dep_target[0] = '\0';
939 char outnamebuf[4096];
940 if (outname == NULL) {
941 const char *filename = files->name;
944 case BenchmarkParser:
955 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
956 outname = outnamebuf;
958 case CompileAssemble:
959 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
960 outname = outnamebuf;
963 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
965 outname = outnamebuf;
967 case CompileAssembleLink:
977 assert(outname != NULL);
980 if (streq(outname, "-")) {
983 out = fopen(outname, "w");
985 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
991 file_list_entry_t *file;
992 for (file = files; file != NULL; file = file->next) {
993 char asm_tempfile[1024];
994 const char *filename = file->name;
995 filetype_t filetype = file->type;
997 if (file->type == FILETYPE_OBJECT)
1001 if (mode == LexTest) {
1003 in = open_file(filename);
1004 lextest(in, filename);
1009 FILE *preprocessed_in = NULL;
1010 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
1011 /* no support for input on FILE* yet */
1013 panic("internal compiler error: in for preprocessor != NULL");
1015 preprocessed_in = preprocess(filename);
1016 if (mode == PreprocessOnly) {
1017 copy_file(out, preprocessed_in);
1018 int result = pclose(preprocessed_in);
1023 if (filetype == FILETYPE_C) {
1024 filetype = FILETYPE_PREPROCESSED_C;
1025 } else if (filetype == FILETYPE_ASSEMBLER) {
1026 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1028 panic("internal compiler error: unknown filetype at preproc");
1031 in = preprocessed_in;
1035 if(mode == Compile) {
1038 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1043 in = open_file(filename);
1045 /* preprocess and compile */
1046 if (filetype == FILETYPE_PREPROCESSED_C) {
1047 translation_unit_t *const unit = do_parsing(in, filename);
1048 if (in == preprocessed_in) {
1049 int pp_result = pclose(preprocessed_in);
1050 if (pp_result != EXIT_SUCCESS) {
1055 /* prints the AST even if errors occurred */
1056 if (mode == PrintAst) {
1057 type_set_output(out);
1058 ast_set_output(out);
1062 if(error_count > 0) {
1063 /* parsing failed because of errors */
1064 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1066 result = EXIT_FAILURE;
1068 } else if(warning_count > 0) {
1069 fprintf(stderr, "%u warning(s)\n", warning_count);
1072 if(mode == BenchmarkParser) {
1074 } else if(mode == PrintFluffy) {
1075 write_fluffy_decls(out, unit);
1077 } else if (mode == PrintCaml) {
1078 write_caml_decls(out, unit);
1082 translation_unit_to_firm(unit);
1084 if (mode == ParseOnly) {
1088 if (mode == CompileDump) {
1090 ident *id = new_id_from_str(dumpfunction);
1091 ir_graph *irg = NULL;
1092 int n_irgs = get_irp_n_irgs();
1093 for(int i = 0; i < n_irgs; ++i) {
1094 ir_graph *tirg = get_irp_irg(i);
1095 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1103 fprintf(stderr, "No graph for function '%s' found\n",
1108 dump_ir_block_graph_file(irg, out);
1113 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1114 /*firm_const_exists=*/0);
1115 if (asm_out != out) {
1119 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1120 copy_file(asm_out, in);
1121 if (in == preprocessed_in) {
1122 int pp_result = pclose(preprocessed_in);
1123 if (pp_result != EXIT_SUCCESS) {
1127 if(asm_out != out) {
1132 if (mode == Compile)
1135 /* if we're here then we have preprocessed assembly */
1136 filename = asm_tempfile;
1137 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1140 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1142 const char *filename_o;
1143 if(mode == CompileAssemble) {
1145 filename_o = outname;
1147 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1152 assemble(filename_o, filename);
1154 size_t len = strlen(filename_o) + 1;
1155 filename = obstack_copy(&file_obst, filename_o, len);
1156 filetype = FILETYPE_OBJECT;
1159 /* ok we're done here, process next file */
1160 file->name = filename;
1161 file->type = filetype;
1164 if (result != EXIT_SUCCESS)
1167 /* link program file */
1168 if(mode == CompileAssembleLink) {
1169 obstack_1grow(&ldflags_obst, '\0');
1170 const char *flags = obstack_finish(&ldflags_obst);
1172 /* construct commandline */
1173 obstack_printf(&file_obst, "%s", LINKER);
1174 for (file_list_entry_t *entry = files; entry != NULL;
1175 entry = entry->next) {
1176 if (entry->type != FILETYPE_OBJECT)
1179 add_flag(&file_obst, "%s", entry->name);
1182 add_flag(&file_obst, "-o");
1183 add_flag(&file_obst, outname);
1184 obstack_printf(&file_obst, "%s", flags);
1185 obstack_1grow(&file_obst, '\0');
1187 char *commandline = obstack_finish(&file_obst);
1192 int err = system(commandline);
1193 if(err != EXIT_SUCCESS) {
1194 fprintf(stderr, "linker reported an error\n");
1199 obstack_free(&cppflags_obst, NULL);
1200 obstack_free(&ldflags_obst, NULL);
1201 obstack_free(&file_obst, NULL);
1210 exit_symbol_table();