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"
84 #define PREPROCESSOR "gcc -E -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__"
86 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__"
91 #define LINKER "gcc -m32"
96 #define ASSEMBLER "gcc -c -xassembler"
98 #define ASSEMBLER "as --32"
102 /** The current c mode/dialect. */
103 unsigned int c_mode = _C89|_C99|_GNUC;
105 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
106 unsigned int machine_size = 32;
108 /** true if the char type is signed. */
109 bool char_is_signed = true;
111 /** true for strict language checking. */
112 bool strict_mode = false;
114 /** use builtins for some libc functions */
115 bool use_builtins = false;
117 /* to switch on printing of implicit casts */
118 extern bool print_implicit_casts;
120 /* to switch on printing of parenthesis to indicate operator precedence */
121 extern bool print_parenthesis;
124 static struct obstack cppflags_obst, ldflags_obst;
125 static char dep_target[1024];
126 static const char *outname;
128 typedef struct file_list_entry_t file_list_entry_t;
130 typedef enum filetype_t {
133 FILETYPE_PREPROCESSED_C,
135 FILETYPE_PREPROCESSED_ASSEMBLER,
140 struct file_list_entry_t {
141 const char *name; /**< filename or NULL for stdin */
143 file_list_entry_t *next;
146 #if defined(_DEBUG) || defined(FIRM_DEBUG)
148 * Debug printf implementation.
150 * @param fmt printf style format parameter
152 void dbg_printf(const char *fmt, ...)
156 if (firm_dump.debug_print) {
162 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
164 static void initialize_firm(void)
168 dump_consts_local(1);
169 dump_keepalive_edges(1);
172 static void get_output_name(char *buf, size_t buflen, const char *inputname,
175 size_t last_dot = 0xffffffff;
178 if(inputname == NULL) {
179 snprintf(buf, buflen, "a%s", newext);
183 for(const char *c = inputname; *c != 0; ++c) {
188 if(last_dot == 0xffffffff)
191 if(last_dot >= buflen)
192 panic("filename too long");
193 memcpy(buf, inputname, last_dot);
195 size_t extlen = strlen(newext) + 1;
196 if(extlen + last_dot >= buflen)
197 panic("filename too long");
198 memcpy(buf+last_dot, newext, extlen);
201 #include "builtins.h"
203 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
208 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
212 lexer_open_stream(in, input_name);
215 translation_unit_t *unit = finish_parsing();
219 static void lextest(FILE *in, const char *fname)
221 lexer_open_stream(in, fname);
224 lexer_next_preprocessing_token();
225 print_token(stdout, &lexer_token);
227 } while(lexer_token.type != T_EOF);
230 static void add_flag(struct obstack *obst, const char *format, ...)
233 va_start(ap, format);
236 vsnprintf(buf, sizeof(buf), format, ap);
238 /* escape stuff... */
239 obstack_1grow(obst, ' ');
240 for (char *c = buf; *c != '\0'; ++c) {
253 obstack_1grow(obst, '\\');
256 obstack_1grow(obst, *c);
264 static FILE *preprocess(const char *fname)
266 obstack_1grow(&cppflags_obst, '\0');
267 const char *flags = obstack_finish(&cppflags_obst);
269 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
270 if (dep_target[0] != '\0') {
271 add_flag(&cppflags_obst, "-MF");
272 add_flag(&cppflags_obst, "%s", dep_target);
273 if (outname != NULL) {
274 add_flag(&cppflags_obst, "-MQ");
275 add_flag(&cppflags_obst, "%s", outname);
278 if (flags[0] != '\0') {
279 obstack_printf(&cppflags_obst, " %s", flags);
281 add_flag(&cppflags_obst, "%s", fname);
283 obstack_1grow(&cppflags_obst, '\0');
284 const char *buf = obstack_finish(&cppflags_obst);
289 FILE *f = popen(buf, "r");
291 fprintf(stderr, "invoking preprocessor failed\n");
298 static void assemble(const char *out, const char *in)
302 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
307 int err = system(buf);
309 fprintf(stderr, "assembler reported an error\n");
314 static const char *try_dir(const char *dir)
318 if(access(dir, R_OK | W_OK | X_OK) == 0)
323 static const char *get_tempdir(void)
325 static const char *tmpdir = NULL;
331 tmpdir = try_dir(getenv("TMPDIR"));
333 tmpdir = try_dir(getenv("TMP"));
335 tmpdir = try_dir(getenv("TEMP"));
339 tmpdir = try_dir(P_tmpdir);
343 tmpdir = try_dir("/var/tmp");
345 tmpdir = try_dir("/usr/tmp");
347 tmpdir = try_dir("/tmp");
356 /* cheap and nasty mkstemp replacement */
357 static int mkstemp(char *templ)
360 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
365 * an own version of tmpnam, which: writes in a buffer, emits no warnings
366 * during linking (like glibc/gnu ld do for tmpnam)...
368 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
370 const char *tempdir = get_tempdir();
372 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
374 int fd = mkstemp(buffer);
376 fprintf(stderr, "couldn't create temporary file: %s\n",
380 FILE *out = fdopen(fd, "w");
382 fprintf(stderr, "couldn't create temporary file FILE*\n");
390 * Do the necessary lowering for compound parameters.
392 void lower_compound_params(void)
394 lower_params_t params;
396 params.def_ptr_alignment = 4;
397 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
398 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
399 params.find_pointer_type = NULL;
400 params.ret_compound_in_regs = NULL;
401 lower_calls_with_compounds(¶ms);
404 typedef enum compile_mode_t {
418 static void usage(const char *argv0)
420 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
423 static void print_cparser_version(void) {
425 firm_get_version(&ver);
427 printf("cparser (%s) using libFirm (%u.%u",
428 cparser_REVISION, ver.major, ver.minor);
429 if(ver.revision[0] != 0) {
431 fputs(ver.revision, stdout);
433 if(ver.build[0] != 0) {
435 fputs(ver.build, stdout);
440 static void set_be_option(const char *arg)
442 int res = firm_be_option(arg);
447 static void set_option(const char *arg)
449 int res = firm_option(arg);
454 static void copy_file(FILE *dest, FILE *input)
458 while (!feof(input) && !ferror(dest)) {
459 size_t read = fread(buf, 1, sizeof(buf), input);
460 if(fwrite(buf, 1, read, dest) != read) {
461 perror("couldn't write output");
466 static inline bool streq(char const* a, char const* b)
468 return strcmp(a, b) == 0;
471 static inline bool strstart(char const* str, char const* start)
476 } while (*str++ == *start++);
480 static FILE *open_file(const char *filename)
482 if (streq(filename, "-")) {
486 FILE *in = fopen(filename, "r");
488 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
496 static filetype_t get_filetype_from_string(const char *string)
498 if (streq(string, "c") || streq(string, "c-header"))
500 if (streq(string, "assembler"))
501 return FILETYPE_PREPROCESSED_ASSEMBLER;
502 if (streq(string, "assembler-with-cpp"))
503 return FILETYPE_ASSEMBLER;
504 if (streq(string, "none"))
505 return FILETYPE_AUTODETECT;
507 return FILETYPE_UNKNOWN;
510 int main(int argc, char **argv)
514 const char *dumpfunction = NULL;
515 compile_mode_t mode = CompileAssembleLink;
517 int result = EXIT_SUCCESS;
518 char cpu_arch[16] = "ia32";
519 file_list_entry_t *files = NULL;
520 file_list_entry_t *last_file = NULL;
521 bool construct_dep_target = false;
522 struct obstack file_obst;
524 obstack_init(&cppflags_obst);
525 obstack_init(&ldflags_obst);
526 obstack_init(&file_obst);
528 #define GET_ARG_AFTER(def, args) \
529 def = &arg[sizeof(args)-1]; \
530 if(def[0] == '\0') { \
533 fprintf(stderr, "error: expected argument after '" args "'\n"); \
534 argument_errors = true; \
538 if(def[0] == '-' && def[1] != '\0') { \
539 fprintf(stderr, "error: expected argument after '" args "'\n"); \
540 argument_errors = true; \
545 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
547 /* early options parsing (find out optimisation level) */
548 for(int i = 1; i < argc; ++i) {
549 const char *arg = argv[i];
553 const char *option = &arg[1];
554 if(option[0] == 'O') {
555 sscanf(&option[1], "%d", &opt_level);
559 /* apply optimisation level */
562 set_option("no-opt");
565 set_option("no-inline");
569 set_option("strict-aliasing");
570 /* use_builtins = true; */
573 set_option("cond-eval");
574 set_option("if-conv");
577 set_option("inline");
578 set_option("deconv");
579 set_be_option("omitfp");
584 /* Darwin expects the stack to be aligned to 16byte boundary */
585 firm_be_option("ia32-stackalign=4");
588 /* parse rest of options */
589 filetype_t forced_filetype = FILETYPE_AUTODETECT;
590 bool help_displayed = false;
591 bool argument_errors = false;
592 for(int i = 1; i < argc; ++i) {
593 const char *arg = argv[i];
594 if(arg[0] == '-' && arg[1] != 0) {
596 const char *option = &arg[1];
597 if(option[0] == 'o') {
598 GET_ARG_AFTER(outname, "-o");
599 } else if(option[0] == 'g') {
600 set_be_option("debuginfo=stabs");
601 set_be_option("omitfp=no");
602 set_be_option("ia32-nooptcc=yes");
603 } else if(SINGLE_OPTION('c')) {
604 mode = CompileAssemble;
605 } else if(SINGLE_OPTION('E')) {
606 mode = PreprocessOnly;
607 } else if(SINGLE_OPTION('S')) {
609 } else if(option[0] == 'O') {
611 } else if(option[0] == 'I') {
613 GET_ARG_AFTER(opt, "-I");
614 add_flag(&cppflags_obst, "-I%s", opt);
615 } else if(option[0] == 'D') {
617 GET_ARG_AFTER(opt, "-D");
618 add_flag(&cppflags_obst, "-D%s", opt);
619 } else if(option[0] == 'U') {
621 GET_ARG_AFTER(opt, "-U");
622 add_flag(&cppflags_obst, "-U%s", opt);
623 } else if(option[0] == 'l') {
625 GET_ARG_AFTER(opt, "-l");
626 add_flag(&ldflags_obst, "-l%s", opt);
627 } else if(option[0] == 'L') {
629 GET_ARG_AFTER(opt, "-L");
630 add_flag(&ldflags_obst, "-L%s", opt);
631 } else if(SINGLE_OPTION('v')) {
633 } else if(SINGLE_OPTION('w')) {
634 inhibit_all_warnings = true;
635 } else if(option[0] == 'x') {
637 GET_ARG_AFTER(opt, "-x");
638 forced_filetype = get_filetype_from_string(opt);
639 if (forced_filetype == FILETYPE_UNKNOWN) {
640 fprintf(stderr, "Unknown language '%s'\n", opt);
641 argument_errors = true;
643 } else if (streq(option, "M")) {
644 mode = PreprocessOnly;
645 add_flag(&cppflags_obst, "-M");
646 } else if (streq(option, "MMD") ||
647 streq(option, "MD")) {
648 construct_dep_target = true;
649 add_flag(&cppflags_obst, "-%s", option);
650 } else if(streq(option, "MM") ||
651 streq(option, "MP")) {
652 add_flag(&cppflags_obst, "-%s", option);
653 } else if (streq(option, "MT") ||
654 streq(option, "MQ") ||
655 streq(option, "MF")) {
657 GET_ARG_AFTER(opt, "-MT");
658 add_flag(&cppflags_obst, "-%s", option);
659 add_flag(&cppflags_obst, "%s", opt);
660 } else if (streq(option, "pipe")) {
661 /* here for gcc compatibility */
662 } else if (option[0] == 'f') {
663 char const *orig_opt;
664 GET_ARG_AFTER(orig_opt, "-f");
666 if (strstart(orig_opt, "align-loops=") ||
667 strstart(orig_opt, "align-jumps=") ||
668 strstart(orig_opt, "align-functions=")) {
669 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
671 char const *opt = orig_opt;
672 bool truth_value = true;
673 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
678 if (streq(opt, "dollars-in-identifiers")) {
679 allow_dollar_in_symbol = truth_value;
680 } if (streq(opt, "builtins")) {
681 use_builtins = truth_value;
682 } else if (streq(opt, "short-wchar")) {
683 opt_short_wchar_t = truth_value;
684 } else if (streq(opt, "syntax-only")) {
685 mode = truth_value ? ParseOnly : CompileAssembleLink;
686 } else if (streq(opt, "omit-frame-pointer")) {
687 set_be_option(truth_value ? "omitfp" : "omitfp=no");
688 } else if (streq(opt, "strength-reduce")) {
689 firm_option(truth_value ? "strength-red" : "no-strength-red");
690 } else if (streq(opt, "fast-math") ||
691 streq(opt, "jump-tables") ||
692 streq(opt, "unroll-loops") ||
693 streq(opt, "expensive-optimizations") ||
694 streq(opt, "common") ||
696 streq(opt, "align-loops") ||
697 streq(opt, "align-jumps") ||
698 streq(opt, "align-functions")) {
699 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
701 int res = firm_option(orig_opt);
703 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
705 argument_errors = true;
707 } else if (res == -1) {
708 help_displayed = true;
712 } else if (option[0] == 'b') {
714 GET_ARG_AFTER(opt, "-b");
715 int res = firm_be_option(opt);
717 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
719 argument_errors = true;
720 } else if (res == -1) {
721 help_displayed = true;
722 } else if (strstart(opt, "isa=")) {
723 strncpy(cpu_arch, opt, sizeof(cpu_arch));
725 } else if (option[0] == 'W') {
726 if (strstart(option + 1, "l,")) // a gcc-style linker option
729 GET_ARG_AFTER(opt, "-Wl,");
730 add_flag(&ldflags_obst, "-Wl,%s", opt);
732 else set_warning_opt(&option[1]);
733 } else if(option[0] == 'm') {
738 GET_ARG_AFTER(opt, "-m");
739 if (strstart(opt, "arch=")) {
740 GET_ARG_AFTER(opt, "-march=");
741 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
742 int res = firm_be_option(arch_opt);
744 argument_errors = true;
746 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
747 int res = firm_be_option(arch_opt);
749 argument_errors = true;
751 } else if (strstart(opt, "tune=")) {
752 GET_ARG_AFTER(opt, "-mtune=");
753 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
754 int res = firm_be_option(arch_opt);
756 argument_errors = true;
757 } else if (strstart(opt, "cpu=")) {
758 GET_ARG_AFTER(opt, "-mcpu=");
759 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
760 int res = firm_be_option(arch_opt);
762 argument_errors = true;
763 } else if (strstart(opt, "fpmath=")) {
764 GET_ARG_AFTER(opt, "-mfpmath=");
765 if (streq(opt, "387"))
767 else if (streq(opt, "sse"))
770 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
771 argument_errors = true;
773 if(!argument_errors) {
774 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
775 int res = firm_be_option(arch_opt);
777 argument_errors = true;
779 } else if (strstart(opt, "preferred-stack-boundary=")) {
780 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
781 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
782 int res = firm_be_option(arch_opt);
784 argument_errors = true;
785 } else if (streq(opt, "omit-leaf-frame-pointer")) {
786 set_be_option("omitleaffp=1");
787 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
788 set_be_option("omitleaffp=0");
791 long int value = strtol(opt, &endptr, 10);
792 if (*endptr != '\0') {
793 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
794 argument_errors = true;
796 if (value != 16 && value != 32 && value != 64) {
797 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
798 argument_errors = true;
800 machine_size = (unsigned int)value;
803 } else if (streq(option, "pg")) {
804 set_be_option("gprof");
805 add_flag(&ldflags_obst, "-pg");
806 } else if (streq(option, "pedantic") ||
807 streq(option, "ansi")) {
808 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
809 } else if (streq(option, "shared")) {
810 add_flag(&ldflags_obst, "-shared");
811 } else if (strstart(option, "std=")) {
812 if (streq(&option[4], "c99")) {
814 } else if (streq(&option[4], "c89")) {
816 } else if (streq(&option[4], "gnu99")) {
817 c_mode = _C89|_C99|_GNUC;
818 } else if (streq(&option[4], "microsoft")) {
819 c_mode = _C89|_C99|_MS;
821 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
822 } else if (streq(option, "version")) {
823 print_cparser_version();
824 } else if (option[0] == '-') {
825 /* double dash option */
827 if (streq(option, "gcc")) {
829 } else if (streq(option, "no-gcc")) {
831 } else if (streq(option, "ms")) {
833 } else if (streq(option, "no-ms")) {
835 } else if (streq(option, "signed-chars")) {
836 char_is_signed = true;
837 } else if (streq(option, "unsigned-chars")) {
838 char_is_signed = false;
839 } else if (streq(option, "strict")) {
841 } else if (streq(option, "lextest")) {
843 } else if (streq(option, "benchmark")) {
844 mode = BenchmarkParser;
845 } else if (streq(option, "print-ast")) {
847 } else if (streq(option, "print-implicit-cast")) {
848 print_implicit_casts = true;
849 } else if (streq(option, "print-parenthesis")) {
850 print_parenthesis = true;
851 } else if (streq(option, "print-fluffy")) {
853 } else if (streq(option, "print-caml")) {
855 } else if (streq(option, "version")) {
856 print_cparser_version();
858 } else if (streq(option, "dump-function")) {
861 fprintf(stderr, "error: "
862 "expected argument after '--dump-function'\n");
863 argument_errors = true;
866 dumpfunction = argv[i];
869 fprintf(stderr, "error: unknown argument '%s'\n", arg);
870 argument_errors = true;
873 fprintf(stderr, "error: unknown argument '%s'\n", arg);
874 argument_errors = true;
877 filetype_t type = forced_filetype;
878 const char *filename = arg;
879 if (type == FILETYPE_AUTODETECT) {
880 size_t const len = strlen(arg);
881 if (len < 2 && arg[0] == '-') {
882 /* - implicitly means C source file */
885 } else if (len > 2 && arg[len - 2] == '.') {
886 switch (arg[len - 1]) {
887 case 'c': type = FILETYPE_C; break;
888 case 'h': type = FILETYPE_C; break;
889 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
890 case 'S': type = FILETYPE_ASSEMBLER; break;
893 case 'o': type = FILETYPE_OBJECT; break;
895 } else if (len > 3 && arg[len - 3] == '.') {
896 if (streq(arg + len - 2, "so")) {
897 type = FILETYPE_OBJECT;
901 if (type == FILETYPE_AUTODETECT) {
902 fprintf(stderr, "'%s': file format not recognized\n", arg);
907 file_list_entry_t *entry
908 = obstack_alloc(&file_obst, sizeof(entry[0]));
909 memset(entry, 0, sizeof(entry[0]));
913 if (last_file != NULL) {
914 last_file->next = entry;
923 fprintf(stderr, "error: no input files specified\n");
924 argument_errors = true;
927 if (help_displayed) {
928 return !argument_errors;
930 if (argument_errors) {
935 /* we do the lowering in ast2firm */
936 firm_opt.lower_bitfields = FALSE;
949 if (construct_dep_target) {
950 if (outname != 0 && strlen(outname) >= 2) {
951 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
953 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
956 dep_target[0] = '\0';
959 char outnamebuf[4096];
960 if (outname == NULL) {
961 const char *filename = files->name;
964 case BenchmarkParser:
975 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
976 outname = outnamebuf;
978 case CompileAssemble:
979 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
980 outname = outnamebuf;
983 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
985 outname = outnamebuf;
987 case CompileAssembleLink:
997 assert(outname != NULL);
1000 if (streq(outname, "-")) {
1003 out = fopen(outname, "w");
1005 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1011 file_list_entry_t *file;
1012 for (file = files; file != NULL; file = file->next) {
1013 char asm_tempfile[1024];
1014 const char *filename = file->name;
1015 filetype_t filetype = file->type;
1017 if (file->type == FILETYPE_OBJECT)
1021 if (mode == LexTest) {
1023 in = open_file(filename);
1024 lextest(in, filename);
1029 FILE *preprocessed_in = NULL;
1030 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
1031 /* no support for input on FILE* yet */
1033 panic("internal compiler error: in for preprocessor != NULL");
1035 preprocessed_in = preprocess(filename);
1036 if (mode == PreprocessOnly) {
1037 copy_file(out, preprocessed_in);
1038 int result = pclose(preprocessed_in);
1043 if (filetype == FILETYPE_C) {
1044 filetype = FILETYPE_PREPROCESSED_C;
1045 } else if (filetype == FILETYPE_ASSEMBLER) {
1046 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1048 panic("internal compiler error: unknown filetype at preproc");
1051 in = preprocessed_in;
1055 if(mode == Compile) {
1058 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1063 in = open_file(filename);
1065 /* preprocess and compile */
1066 if (filetype == FILETYPE_PREPROCESSED_C) {
1067 translation_unit_t *const unit = do_parsing(in, filename);
1068 if (in == preprocessed_in) {
1069 int pp_result = pclose(preprocessed_in);
1070 if (pp_result != EXIT_SUCCESS) {
1075 /* prints the AST even if errors occurred */
1076 if (mode == PrintAst) {
1077 type_set_output(out);
1078 ast_set_output(out);
1082 if(error_count > 0) {
1083 /* parsing failed because of errors */
1084 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1086 result = EXIT_FAILURE;
1088 } else if(warning_count > 0) {
1089 fprintf(stderr, "%u warning(s)\n", warning_count);
1092 if(mode == BenchmarkParser) {
1094 } else if(mode == PrintFluffy) {
1095 write_fluffy_decls(out, unit);
1097 } else if (mode == PrintCaml) {
1098 write_caml_decls(out, unit);
1102 translation_unit_to_firm(unit);
1104 if (mode == ParseOnly) {
1108 if (mode == CompileDump) {
1110 ident *id = new_id_from_str(dumpfunction);
1111 ir_graph *irg = NULL;
1112 int n_irgs = get_irp_n_irgs();
1113 for(int i = 0; i < n_irgs; ++i) {
1114 ir_graph *tirg = get_irp_irg(i);
1115 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1123 fprintf(stderr, "No graph for function '%s' found\n",
1128 dump_ir_block_graph_file(irg, out);
1133 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1134 /*firm_const_exists=*/0);
1135 if (asm_out != out) {
1139 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1140 copy_file(asm_out, in);
1141 if (in == preprocessed_in) {
1142 int pp_result = pclose(preprocessed_in);
1143 if (pp_result != EXIT_SUCCESS) {
1147 if(asm_out != out) {
1152 if (mode == Compile)
1155 /* if we're here then we have preprocessed assembly */
1156 filename = asm_tempfile;
1157 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1160 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1162 const char *filename_o;
1163 if(mode == CompileAssemble) {
1165 filename_o = outname;
1167 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1172 assemble(filename_o, filename);
1174 size_t len = strlen(filename_o) + 1;
1175 filename = obstack_copy(&file_obst, filename_o, len);
1176 filetype = FILETYPE_OBJECT;
1179 /* ok we're done here, process next file */
1180 file->name = filename;
1181 file->type = filetype;
1184 if (result != EXIT_SUCCESS)
1187 /* link program file */
1188 if(mode == CompileAssembleLink) {
1189 obstack_1grow(&ldflags_obst, '\0');
1190 const char *flags = obstack_finish(&ldflags_obst);
1192 /* construct commandline */
1193 obstack_printf(&file_obst, "%s", LINKER);
1194 for (file_list_entry_t *entry = files; entry != NULL;
1195 entry = entry->next) {
1196 if (entry->type != FILETYPE_OBJECT)
1199 add_flag(&file_obst, "%s", entry->name);
1202 add_flag(&file_obst, "-o");
1203 add_flag(&file_obst, outname);
1204 obstack_printf(&file_obst, "%s", flags);
1205 obstack_1grow(&file_obst, '\0');
1207 char *commandline = obstack_finish(&file_obst);
1212 int err = system(commandline);
1213 if(err != EXIT_SUCCESS) {
1214 fprintf(stderr, "linker reported an error\n");
1219 obstack_free(&cppflags_obst, NULL);
1220 obstack_free(&ldflags_obst, NULL);
1221 obstack_free(&file_obst, NULL);
1230 exit_symbol_table();