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");
572 set_option("cond-eval");
573 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 } else if (streq(opt, "short-wchar")) {
681 opt_short_wchar_t = truth_value;
682 } else if (streq(opt, "syntax-only")) {
683 mode = truth_value ? ParseOnly : CompileAssembleLink;
684 } else if (streq(opt, "omit-frame-pointer")) {
685 set_be_option(truth_value ? "omitfp" : "omitfp=no");
686 } else if (streq(opt, "strength-reduce")) {
687 firm_option(truth_value ? "strength-red" : "no-strength-red");
688 } else if (streq(opt, "fast-math") ||
689 streq(opt, "jump-tables") ||
690 streq(opt, "unroll-loops") ||
691 streq(opt, "expensive-optimizations") ||
692 streq(opt, "common") ||
694 streq(opt, "align-loops") ||
695 streq(opt, "align-jumps") ||
696 streq(opt, "align-functions")) {
697 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
699 int res = firm_option(orig_opt);
701 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
703 argument_errors = true;
705 } else if (res == -1) {
706 help_displayed = true;
710 } else if (option[0] == 'b') {
712 GET_ARG_AFTER(opt, "-b");
713 int res = firm_be_option(opt);
715 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
717 argument_errors = true;
718 } else if (res == -1) {
719 help_displayed = true;
720 } else if (strstart(opt, "isa=")) {
721 strncpy(cpu_arch, opt, sizeof(cpu_arch));
723 } else if (option[0] == 'W') {
724 if (strstart(option + 1, "l,")) // a gcc-style linker option
727 GET_ARG_AFTER(opt, "-Wl,");
728 add_flag(&ldflags_obst, "-Wl,%s", opt);
730 else set_warning_opt(&option[1]);
731 } else if(option[0] == 'm') {
736 GET_ARG_AFTER(opt, "-m");
737 if (strstart(opt, "arch=")) {
738 GET_ARG_AFTER(opt, "-march=");
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;
744 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
745 int res = firm_be_option(arch_opt);
747 argument_errors = true;
749 } else if (strstart(opt, "tune=")) {
750 GET_ARG_AFTER(opt, "-mtune=");
751 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
752 int res = firm_be_option(arch_opt);
754 argument_errors = true;
755 } else if (strstart(opt, "cpu=")) {
756 GET_ARG_AFTER(opt, "-mcpu=");
757 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
758 int res = firm_be_option(arch_opt);
760 argument_errors = true;
761 } else if (strstart(opt, "fpmath=")) {
762 GET_ARG_AFTER(opt, "-mfpmath=");
763 if (streq(opt, "387"))
765 else if (streq(opt, "sse"))
768 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
769 argument_errors = true;
771 if(!argument_errors) {
772 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
773 int res = firm_be_option(arch_opt);
775 argument_errors = true;
777 } else if (strstart(opt, "preferred-stack-boundary=")) {
778 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
779 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
780 int res = firm_be_option(arch_opt);
782 argument_errors = true;
783 } else if (streq(opt, "omit-leaf-frame-pointer")) {
784 set_be_option("omitleaffp=1");
785 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
786 set_be_option("omitleaffp=0");
789 long int value = strtol(opt, &endptr, 10);
790 if (*endptr != '\0') {
791 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
792 argument_errors = true;
794 if (value != 16 && value != 32 && value != 64) {
795 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
796 argument_errors = true;
798 machine_size = (unsigned int)value;
801 } else if (streq(option, "pg")) {
802 set_be_option("gprof");
803 add_flag(&ldflags_obst, "-pg");
804 } else if (streq(option, "pedantic") ||
805 streq(option, "ansi")) {
806 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
807 } else if (streq(option, "shared")) {
808 add_flag(&ldflags_obst, "-shared");
809 } else if (strstart(option, "std=")) {
810 if (streq(&option[4], "c99")) {
812 } else if (streq(&option[4], "c89")) {
814 } else if (streq(&option[4], "gnu99")) {
815 c_mode = _C89|_C99|_GNUC;
816 } else if (streq(&option[4], "microsoft")) {
817 c_mode = _C89|_C99|_MS;
819 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
820 } else if (streq(option, "version")) {
821 print_cparser_version();
822 } else if (option[0] == '-') {
823 /* double dash option */
825 if (streq(option, "gcc")) {
827 } else if (streq(option, "no-gcc")) {
829 } else if (streq(option, "ms")) {
831 } else if (streq(option, "no-ms")) {
833 } else if (streq(option, "signed-chars")) {
834 char_is_signed = true;
835 } else if (streq(option, "unsigned-chars")) {
836 char_is_signed = false;
837 } else if (streq(option, "strict")) {
839 } else if (streq(option, "lextest")) {
841 } else if (streq(option, "benchmark")) {
842 mode = BenchmarkParser;
843 } else if (streq(option, "print-ast")) {
845 } else if (streq(option, "print-implicit-cast")) {
846 print_implicit_casts = true;
847 } else if (streq(option, "print-parenthesis")) {
848 print_parenthesis = true;
849 } else if (streq(option, "print-fluffy")) {
851 } else if (streq(option, "print-caml")) {
853 } else if (streq(option, "version")) {
854 print_cparser_version();
856 } else if (streq(option, "dump-function")) {
859 fprintf(stderr, "error: "
860 "expected argument after '--dump-function'\n");
861 argument_errors = true;
864 dumpfunction = argv[i];
867 fprintf(stderr, "error: unknown argument '%s'\n", arg);
868 argument_errors = true;
871 fprintf(stderr, "error: unknown argument '%s'\n", arg);
872 argument_errors = true;
875 filetype_t type = forced_filetype;
876 const char *filename = arg;
877 if (type == FILETYPE_AUTODETECT) {
878 size_t const len = strlen(arg);
879 if (len < 2 && arg[0] == '-') {
880 /* - implicitly means C source file */
883 } else if (len > 2 && arg[len - 2] == '.') {
884 switch (arg[len - 1]) {
885 case 'c': type = FILETYPE_C; break;
886 case 'h': type = FILETYPE_C; break;
887 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
888 case 'S': type = FILETYPE_ASSEMBLER; break;
891 case 'o': type = FILETYPE_OBJECT; break;
893 } else if (len > 3 && arg[len - 3] == '.') {
894 if (streq(arg + len - 2, "so")) {
895 type = FILETYPE_OBJECT;
899 if (type == FILETYPE_AUTODETECT) {
900 fprintf(stderr, "'%s': file format not recognized\n", arg);
905 file_list_entry_t *entry
906 = obstack_alloc(&file_obst, sizeof(entry[0]));
907 memset(entry, 0, sizeof(entry[0]));
911 if (last_file != NULL) {
912 last_file->next = entry;
921 fprintf(stderr, "error: no input files specified\n");
922 argument_errors = true;
925 if (help_displayed) {
926 return !argument_errors;
928 if (argument_errors) {
933 /* we do the lowering in ast2firm */
934 firm_opt.lower_bitfields = FALSE;
947 if (construct_dep_target) {
948 if (outname != 0 && strlen(outname) >= 2) {
949 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
951 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
954 dep_target[0] = '\0';
957 char outnamebuf[4096];
958 if (outname == NULL) {
959 const char *filename = files->name;
962 case BenchmarkParser:
973 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
974 outname = outnamebuf;
976 case CompileAssemble:
977 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
978 outname = outnamebuf;
981 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
983 outname = outnamebuf;
985 case CompileAssembleLink:
995 assert(outname != NULL);
998 if (streq(outname, "-")) {
1001 out = fopen(outname, "w");
1003 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1009 file_list_entry_t *file;
1010 for (file = files; file != NULL; file = file->next) {
1011 char asm_tempfile[1024];
1012 const char *filename = file->name;
1013 filetype_t filetype = file->type;
1015 if (file->type == FILETYPE_OBJECT)
1019 if (mode == LexTest) {
1021 in = open_file(filename);
1022 lextest(in, filename);
1027 FILE *preprocessed_in = NULL;
1028 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
1029 /* no support for input on FILE* yet */
1031 panic("internal compiler error: in for preprocessor != NULL");
1033 preprocessed_in = preprocess(filename);
1034 if (mode == PreprocessOnly) {
1035 copy_file(out, preprocessed_in);
1036 int result = pclose(preprocessed_in);
1041 if (filetype == FILETYPE_C) {
1042 filetype = FILETYPE_PREPROCESSED_C;
1043 } else if (filetype == FILETYPE_ASSEMBLER) {
1044 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1046 panic("internal compiler error: unknown filetype at preproc");
1049 in = preprocessed_in;
1053 if(mode == Compile) {
1056 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1061 in = open_file(filename);
1063 /* preprocess and compile */
1064 if (filetype == FILETYPE_PREPROCESSED_C) {
1065 translation_unit_t *const unit = do_parsing(in, filename);
1066 if (in == preprocessed_in) {
1067 int pp_result = pclose(preprocessed_in);
1068 if (pp_result != EXIT_SUCCESS) {
1073 /* prints the AST even if errors occurred */
1074 if (mode == PrintAst) {
1075 type_set_output(out);
1076 ast_set_output(out);
1080 if(error_count > 0) {
1081 /* parsing failed because of errors */
1082 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1084 result = EXIT_FAILURE;
1086 } else if(warning_count > 0) {
1087 fprintf(stderr, "%u warning(s)\n", warning_count);
1090 if(mode == BenchmarkParser) {
1092 } else if(mode == PrintFluffy) {
1093 write_fluffy_decls(out, unit);
1095 } else if (mode == PrintCaml) {
1096 write_caml_decls(out, unit);
1100 translation_unit_to_firm(unit);
1102 if (mode == ParseOnly) {
1106 if (mode == CompileDump) {
1108 ident *id = new_id_from_str(dumpfunction);
1109 ir_graph *irg = NULL;
1110 int n_irgs = get_irp_n_irgs();
1111 for(int i = 0; i < n_irgs; ++i) {
1112 ir_graph *tirg = get_irp_irg(i);
1113 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1121 fprintf(stderr, "No graph for function '%s' found\n",
1126 dump_ir_block_graph_file(irg, out);
1131 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1132 /*firm_const_exists=*/0);
1133 if (asm_out != out) {
1137 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1138 copy_file(asm_out, in);
1139 if (in == preprocessed_in) {
1140 int pp_result = pclose(preprocessed_in);
1141 if (pp_result != EXIT_SUCCESS) {
1145 if(asm_out != out) {
1150 if (mode == Compile)
1153 /* if we're here then we have preprocessed assembly */
1154 filename = asm_tempfile;
1155 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1158 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1160 const char *filename_o;
1161 if(mode == CompileAssemble) {
1163 filename_o = outname;
1165 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1170 assemble(filename_o, filename);
1172 size_t len = strlen(filename_o) + 1;
1173 filename = obstack_copy(&file_obst, filename_o, len);
1174 filetype = FILETYPE_OBJECT;
1177 /* ok we're done here, process next file */
1178 file->name = filename;
1179 file->type = filetype;
1182 if (result != EXIT_SUCCESS)
1185 /* link program file */
1186 if(mode == CompileAssembleLink) {
1187 obstack_1grow(&ldflags_obst, '\0');
1188 const char *flags = obstack_finish(&ldflags_obst);
1190 /* construct commandline */
1191 obstack_printf(&file_obst, "%s", LINKER);
1192 for (file_list_entry_t *entry = files; entry != NULL;
1193 entry = entry->next) {
1194 if (entry->type != FILETYPE_OBJECT)
1197 add_flag(&file_obst, "%s", entry->name);
1200 add_flag(&file_obst, "-o");
1201 add_flag(&file_obst, outname);
1202 obstack_printf(&file_obst, "%s", flags);
1203 obstack_1grow(&file_obst, '\0');
1205 char *commandline = obstack_finish(&file_obst);
1210 int err = system(commandline);
1211 if(err != EXIT_SUCCESS) {
1212 fprintf(stderr, "linker reported an error\n");
1217 obstack_free(&cppflags_obst, NULL);
1218 obstack_free(&ldflags_obst, NULL);
1219 obstack_free(&file_obst, NULL);
1228 exit_symbol_table();