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 /** we have extern function with const attribute. */
118 bool have_const_functions = false;
120 /* to switch on printing of implicit casts */
121 extern bool print_implicit_casts;
123 /* to switch on printing of parenthesis to indicate operator precedence */
124 extern bool print_parenthesis;
127 static struct obstack cppflags_obst, ldflags_obst;
128 static char dep_target[1024];
129 static const char *outname;
131 typedef struct file_list_entry_t file_list_entry_t;
133 typedef enum filetype_t {
136 FILETYPE_PREPROCESSED_C,
138 FILETYPE_PREPROCESSED_ASSEMBLER,
143 struct file_list_entry_t {
144 const char *name; /**< filename or NULL for stdin */
146 file_list_entry_t *next;
149 #if defined(_DEBUG) || defined(FIRM_DEBUG)
151 * Debug printf implementation.
153 * @param fmt printf style format parameter
155 void dbg_printf(const char *fmt, ...)
159 if (firm_dump.debug_print) {
165 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
167 static void initialize_firm(void)
171 dump_consts_local(1);
172 dump_keepalive_edges(1);
175 static void get_output_name(char *buf, size_t buflen, const char *inputname,
178 size_t last_dot = 0xffffffff;
181 if(inputname == NULL) {
182 snprintf(buf, buflen, "a%s", newext);
186 for(const char *c = inputname; *c != 0; ++c) {
191 if(last_dot == 0xffffffff)
194 if(last_dot >= buflen)
195 panic("filename too long");
196 memcpy(buf, inputname, last_dot);
198 size_t extlen = strlen(newext) + 1;
199 if(extlen + last_dot >= buflen)
200 panic("filename too long");
201 memcpy(buf+last_dot, newext, extlen);
204 #include "builtins.h"
206 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
211 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
215 lexer_open_stream(in, input_name);
218 translation_unit_t *unit = finish_parsing();
222 static void lextest(FILE *in, const char *fname)
224 lexer_open_stream(in, fname);
227 lexer_next_preprocessing_token();
228 print_token(stdout, &lexer_token);
230 } while(lexer_token.type != T_EOF);
233 static void add_flag(struct obstack *obst, const char *format, ...)
236 va_start(ap, format);
239 vsnprintf(buf, sizeof(buf), format, ap);
241 /* escape stuff... */
242 obstack_1grow(obst, ' ');
243 for (char *c = buf; *c != '\0'; ++c) {
256 obstack_1grow(obst, '\\');
259 obstack_1grow(obst, *c);
267 static FILE *preprocess(const char *fname)
269 obstack_1grow(&cppflags_obst, '\0');
270 const char *flags = obstack_finish(&cppflags_obst);
272 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
273 if (dep_target[0] != '\0') {
274 add_flag(&cppflags_obst, "-MF");
275 add_flag(&cppflags_obst, "%s", dep_target);
276 if (outname != NULL) {
277 add_flag(&cppflags_obst, "-MQ");
278 add_flag(&cppflags_obst, "%s", outname);
281 if (flags[0] != '\0') {
282 obstack_printf(&cppflags_obst, " %s", flags);
284 add_flag(&cppflags_obst, "%s", fname);
286 obstack_1grow(&cppflags_obst, '\0');
287 const char *buf = obstack_finish(&cppflags_obst);
292 FILE *f = popen(buf, "r");
294 fprintf(stderr, "invoking preprocessor failed\n");
301 static void assemble(const char *out, const char *in)
305 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
310 int err = system(buf);
312 fprintf(stderr, "assembler reported an error\n");
317 static const char *try_dir(const char *dir)
321 if(access(dir, R_OK | W_OK | X_OK) == 0)
326 static const char *get_tempdir(void)
328 static const char *tmpdir = NULL;
334 tmpdir = try_dir(getenv("TMPDIR"));
336 tmpdir = try_dir(getenv("TMP"));
338 tmpdir = try_dir(getenv("TEMP"));
342 tmpdir = try_dir(P_tmpdir);
346 tmpdir = try_dir("/var/tmp");
348 tmpdir = try_dir("/usr/tmp");
350 tmpdir = try_dir("/tmp");
359 /* cheap and nasty mkstemp replacement */
360 static int mkstemp(char *templ)
363 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
368 * an own version of tmpnam, which: writes in a buffer, emits no warnings
369 * during linking (like glibc/gnu ld do for tmpnam)...
371 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
373 const char *tempdir = get_tempdir();
375 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
377 int fd = mkstemp(buffer);
379 fprintf(stderr, "couldn't create temporary file: %s\n",
383 FILE *out = fdopen(fd, "w");
385 fprintf(stderr, "couldn't create temporary file FILE*\n");
393 * Do the necessary lowering for compound parameters.
395 void lower_compound_params(void)
397 lower_params_t params;
399 params.def_ptr_alignment = 4;
400 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
401 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
402 params.find_pointer_type = NULL;
403 params.ret_compound_in_regs = NULL;
404 lower_calls_with_compounds(¶ms);
407 typedef enum compile_mode_t {
421 static void usage(const char *argv0)
423 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
426 static void print_cparser_version(void) {
428 firm_get_version(&ver);
430 printf("cparser (%s) using libFirm (%u.%u",
431 cparser_REVISION, ver.major, ver.minor);
432 if(ver.revision[0] != 0) {
434 fputs(ver.revision, stdout);
436 if(ver.build[0] != 0) {
438 fputs(ver.build, stdout);
443 static void set_be_option(const char *arg)
445 int res = firm_be_option(arg);
450 static void set_option(const char *arg)
452 int res = firm_option(arg);
457 static void copy_file(FILE *dest, FILE *input)
461 while (!feof(input) && !ferror(dest)) {
462 size_t read = fread(buf, 1, sizeof(buf), input);
463 if(fwrite(buf, 1, read, dest) != read) {
464 perror("couldn't write output");
469 static inline bool streq(char const* a, char const* b)
471 return strcmp(a, b) == 0;
474 static inline bool strstart(char const* str, char const* start)
479 } while (*str++ == *start++);
483 static FILE *open_file(const char *filename)
485 if (streq(filename, "-")) {
489 FILE *in = fopen(filename, "r");
491 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
499 static filetype_t get_filetype_from_string(const char *string)
501 if (streq(string, "c") || streq(string, "c-header"))
503 if (streq(string, "assembler"))
504 return FILETYPE_PREPROCESSED_ASSEMBLER;
505 if (streq(string, "assembler-with-cpp"))
506 return FILETYPE_ASSEMBLER;
507 if (streq(string, "none"))
508 return FILETYPE_AUTODETECT;
510 return FILETYPE_UNKNOWN;
513 int main(int argc, char **argv)
517 const char *dumpfunction = NULL;
518 compile_mode_t mode = CompileAssembleLink;
520 int result = EXIT_SUCCESS;
521 char cpu_arch[16] = "ia32";
522 file_list_entry_t *files = NULL;
523 file_list_entry_t *last_file = NULL;
524 bool construct_dep_target = false;
525 struct obstack file_obst;
527 obstack_init(&cppflags_obst);
528 obstack_init(&ldflags_obst);
529 obstack_init(&file_obst);
531 #define GET_ARG_AFTER(def, args) \
532 def = &arg[sizeof(args)-1]; \
533 if(def[0] == '\0') { \
536 fprintf(stderr, "error: expected argument after '" args "'\n"); \
537 argument_errors = true; \
541 if(def[0] == '-' && def[1] != '\0') { \
542 fprintf(stderr, "error: expected argument after '" args "'\n"); \
543 argument_errors = true; \
548 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
550 /* early options parsing (find out optimisation level) */
551 for(int i = 1; i < argc; ++i) {
552 const char *arg = argv[i];
556 const char *option = &arg[1];
557 if(option[0] == 'O') {
558 sscanf(&option[1], "%d", &opt_level);
562 /* apply optimisation level */
565 set_option("no-opt");
568 set_option("no-inline");
572 set_option("strict-aliasing");
573 /* use_builtins = true; */
576 set_option("cond-eval");
577 set_option("if-conv");
580 set_option("inline");
581 set_option("deconv");
582 set_be_option("omitfp");
587 /* Darwin expects the stack to be aligned to 16byte boundary */
588 firm_be_option("ia32-stackalign=4");
591 /* parse rest of options */
592 filetype_t forced_filetype = FILETYPE_AUTODETECT;
593 bool help_displayed = false;
594 bool argument_errors = false;
595 for(int i = 1; i < argc; ++i) {
596 const char *arg = argv[i];
597 if(arg[0] == '-' && arg[1] != 0) {
599 const char *option = &arg[1];
600 if(option[0] == 'o') {
601 GET_ARG_AFTER(outname, "-o");
602 } else if(option[0] == 'g') {
603 set_be_option("debuginfo=stabs");
604 set_be_option("omitfp=no");
605 set_be_option("ia32-nooptcc=yes");
606 } else if(SINGLE_OPTION('c')) {
607 mode = CompileAssemble;
608 } else if(SINGLE_OPTION('E')) {
609 mode = PreprocessOnly;
610 } else if(SINGLE_OPTION('S')) {
612 } else if(option[0] == 'O') {
614 } else if(option[0] == 'I') {
616 GET_ARG_AFTER(opt, "-I");
617 add_flag(&cppflags_obst, "-I%s", opt);
618 } else if(option[0] == 'D') {
620 GET_ARG_AFTER(opt, "-D");
621 add_flag(&cppflags_obst, "-D%s", opt);
622 } else if(option[0] == 'U') {
624 GET_ARG_AFTER(opt, "-U");
625 add_flag(&cppflags_obst, "-U%s", opt);
626 } else if(option[0] == 'l') {
628 GET_ARG_AFTER(opt, "-l");
629 add_flag(&ldflags_obst, "-l%s", opt);
630 } else if(option[0] == 'L') {
632 GET_ARG_AFTER(opt, "-L");
633 add_flag(&ldflags_obst, "-L%s", opt);
634 } else if(SINGLE_OPTION('v')) {
636 } else if(SINGLE_OPTION('w')) {
637 inhibit_all_warnings = true;
638 } else if(option[0] == 'x') {
640 GET_ARG_AFTER(opt, "-x");
641 forced_filetype = get_filetype_from_string(opt);
642 if (forced_filetype == FILETYPE_UNKNOWN) {
643 fprintf(stderr, "Unknown language '%s'\n", opt);
644 argument_errors = true;
646 } else if (streq(option, "M")) {
647 mode = PreprocessOnly;
648 add_flag(&cppflags_obst, "-M");
649 } else if (streq(option, "MMD") ||
650 streq(option, "MD")) {
651 construct_dep_target = true;
652 add_flag(&cppflags_obst, "-%s", option);
653 } else if(streq(option, "MM") ||
654 streq(option, "MP")) {
655 add_flag(&cppflags_obst, "-%s", option);
656 } else if (streq(option, "MT") ||
657 streq(option, "MQ") ||
658 streq(option, "MF")) {
660 GET_ARG_AFTER(opt, "-MT");
661 add_flag(&cppflags_obst, "-%s", option);
662 add_flag(&cppflags_obst, "%s", opt);
663 } else if (streq(option, "pipe")) {
664 /* here for gcc compatibility */
665 } else if (option[0] == 'f') {
666 char const *orig_opt;
667 GET_ARG_AFTER(orig_opt, "-f");
669 if (strstart(orig_opt, "align-loops=") ||
670 strstart(orig_opt, "align-jumps=") ||
671 strstart(orig_opt, "align-functions=")) {
672 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
674 char const *opt = orig_opt;
675 bool truth_value = true;
676 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
681 if (streq(opt, "dollars-in-identifiers")) {
682 allow_dollar_in_symbol = truth_value;
683 } if (streq(opt, "builtins")) {
684 use_builtins = truth_value;
685 } else if (streq(opt, "short-wchar")) {
686 opt_short_wchar_t = truth_value;
687 } else if (streq(opt, "syntax-only")) {
688 mode = truth_value ? ParseOnly : CompileAssembleLink;
689 } else if (streq(opt, "omit-frame-pointer")) {
690 set_be_option(truth_value ? "omitfp" : "omitfp=no");
691 } else if (streq(opt, "strength-reduce")) {
692 firm_option(truth_value ? "strength-red" : "no-strength-red");
693 } else if (streq(opt, "fast-math") ||
694 streq(opt, "jump-tables") ||
695 streq(opt, "unroll-loops") ||
696 streq(opt, "expensive-optimizations") ||
697 streq(opt, "common") ||
699 streq(opt, "align-loops") ||
700 streq(opt, "align-jumps") ||
701 streq(opt, "align-functions")) {
702 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
704 int res = firm_option(orig_opt);
706 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
708 argument_errors = true;
710 } else if (res == -1) {
711 help_displayed = true;
715 } else if (option[0] == 'b') {
717 GET_ARG_AFTER(opt, "-b");
718 int res = firm_be_option(opt);
720 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
722 argument_errors = true;
723 } else if (res == -1) {
724 help_displayed = true;
725 } else if (strstart(opt, "isa=")) {
726 strncpy(cpu_arch, opt, sizeof(cpu_arch));
728 } else if (option[0] == 'W') {
729 if (strstart(option + 1, "l,")) // a gcc-style linker option
732 GET_ARG_AFTER(opt, "-Wl,");
733 add_flag(&ldflags_obst, "-Wl,%s", opt);
735 else set_warning_opt(&option[1]);
736 } else if(option[0] == 'm') {
741 GET_ARG_AFTER(opt, "-m");
742 if (strstart(opt, "arch=")) {
743 GET_ARG_AFTER(opt, "-march=");
744 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
745 int res = firm_be_option(arch_opt);
747 argument_errors = true;
749 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
750 int res = firm_be_option(arch_opt);
752 argument_errors = true;
754 } else if (strstart(opt, "tune=")) {
755 GET_ARG_AFTER(opt, "-mtune=");
756 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
757 int res = firm_be_option(arch_opt);
759 argument_errors = true;
760 } else if (strstart(opt, "cpu=")) {
761 GET_ARG_AFTER(opt, "-mcpu=");
762 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
763 int res = firm_be_option(arch_opt);
765 argument_errors = true;
766 } else if (strstart(opt, "fpmath=")) {
767 GET_ARG_AFTER(opt, "-mfpmath=");
768 if (streq(opt, "387"))
770 else if (streq(opt, "sse"))
773 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
774 argument_errors = true;
776 if(!argument_errors) {
777 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
778 int res = firm_be_option(arch_opt);
780 argument_errors = true;
782 } else if (strstart(opt, "preferred-stack-boundary=")) {
783 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
784 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
785 int res = firm_be_option(arch_opt);
787 argument_errors = true;
788 } else if (streq(opt, "omit-leaf-frame-pointer")) {
789 set_be_option("omitleaffp=1");
790 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
791 set_be_option("omitleaffp=0");
794 long int value = strtol(opt, &endptr, 10);
795 if (*endptr != '\0') {
796 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
797 argument_errors = true;
799 if (value != 16 && value != 32 && value != 64) {
800 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
801 argument_errors = true;
803 machine_size = (unsigned int)value;
806 } else if (streq(option, "pg")) {
807 set_be_option("gprof");
808 add_flag(&ldflags_obst, "-pg");
809 } else if (streq(option, "pedantic") ||
810 streq(option, "ansi")) {
811 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
812 } else if (streq(option, "shared")) {
813 add_flag(&ldflags_obst, "-shared");
814 } else if (strstart(option, "std=")) {
815 if (streq(&option[4], "c99")) {
817 } else if (streq(&option[4], "c89")) {
819 } else if (streq(&option[4], "gnu99")) {
820 c_mode = _C89|_C99|_GNUC;
821 } else if (streq(&option[4], "microsoft")) {
822 c_mode = _C89|_C99|_MS;
824 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
825 } else if (streq(option, "version")) {
826 print_cparser_version();
827 } else if (option[0] == '-') {
828 /* double dash option */
830 if (streq(option, "gcc")) {
832 } else if (streq(option, "no-gcc")) {
834 } else if (streq(option, "ms")) {
836 } else if (streq(option, "no-ms")) {
838 } else if (streq(option, "signed-chars")) {
839 char_is_signed = true;
840 } else if (streq(option, "unsigned-chars")) {
841 char_is_signed = false;
842 } else if (streq(option, "strict")) {
844 } else if (streq(option, "lextest")) {
846 } else if (streq(option, "benchmark")) {
847 mode = BenchmarkParser;
848 } else if (streq(option, "print-ast")) {
850 } else if (streq(option, "print-implicit-cast")) {
851 print_implicit_casts = true;
852 } else if (streq(option, "print-parenthesis")) {
853 print_parenthesis = true;
854 } else if (streq(option, "print-fluffy")) {
856 } else if (streq(option, "print-caml")) {
858 } else if (streq(option, "version")) {
859 print_cparser_version();
861 } else if (streq(option, "dump-function")) {
864 fprintf(stderr, "error: "
865 "expected argument after '--dump-function'\n");
866 argument_errors = true;
869 dumpfunction = argv[i];
872 fprintf(stderr, "error: unknown argument '%s'\n", arg);
873 argument_errors = true;
876 fprintf(stderr, "error: unknown argument '%s'\n", arg);
877 argument_errors = true;
880 filetype_t type = forced_filetype;
881 const char *filename = arg;
882 if (type == FILETYPE_AUTODETECT) {
883 size_t const len = strlen(arg);
884 if (len < 2 && arg[0] == '-') {
885 /* - implicitly means C source file */
888 } else if (len > 2 && arg[len - 2] == '.') {
889 switch (arg[len - 1]) {
890 case 'c': type = FILETYPE_C; break;
891 case 'h': type = FILETYPE_C; break;
892 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
893 case 'S': type = FILETYPE_ASSEMBLER; break;
896 case 'o': type = FILETYPE_OBJECT; break;
898 } else if (len > 3 && arg[len - 3] == '.') {
899 if (streq(arg + len - 2, "so")) {
900 type = FILETYPE_OBJECT;
904 if (type == FILETYPE_AUTODETECT) {
905 fprintf(stderr, "'%s': file format not recognized\n", arg);
910 file_list_entry_t *entry
911 = obstack_alloc(&file_obst, sizeof(entry[0]));
912 memset(entry, 0, sizeof(entry[0]));
916 if (last_file != NULL) {
917 last_file->next = entry;
926 fprintf(stderr, "error: no input files specified\n");
927 argument_errors = true;
930 if (help_displayed) {
931 return !argument_errors;
933 if (argument_errors) {
938 /* we do the lowering in ast2firm */
939 firm_opt.lower_bitfields = FALSE;
952 if (construct_dep_target) {
953 if (outname != 0 && strlen(outname) >= 2) {
954 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
956 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
959 dep_target[0] = '\0';
962 char outnamebuf[4096];
963 if (outname == NULL) {
964 const char *filename = files->name;
967 case BenchmarkParser:
977 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
978 outname = outnamebuf;
980 case CompileAssemble:
981 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
982 outname = outnamebuf;
985 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
987 outname = outnamebuf;
989 case CompileAssembleLink:
999 assert(outname != NULL);
1002 if (streq(outname, "-")) {
1005 out = fopen(outname, "w");
1007 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1013 file_list_entry_t *file;
1014 for (file = files; file != NULL; file = file->next) {
1015 char asm_tempfile[1024];
1016 const char *filename = file->name;
1017 filetype_t filetype = file->type;
1019 if (file->type == FILETYPE_OBJECT)
1023 if (mode == LexTest) {
1025 in = open_file(filename);
1026 lextest(in, filename);
1031 FILE *preprocessed_in = NULL;
1032 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
1033 /* no support for input on FILE* yet */
1035 panic("internal compiler error: in for preprocessor != NULL");
1037 preprocessed_in = preprocess(filename);
1038 if (mode == PreprocessOnly) {
1039 copy_file(out, preprocessed_in);
1040 int result = pclose(preprocessed_in);
1045 if (filetype == FILETYPE_C) {
1046 filetype = FILETYPE_PREPROCESSED_C;
1047 } else if (filetype == FILETYPE_ASSEMBLER) {
1048 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1050 panic("internal compiler error: unknown filetype at preproc");
1053 in = preprocessed_in;
1057 if(mode == Compile) {
1060 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1065 in = open_file(filename);
1067 /* preprocess and compile */
1068 if (filetype == FILETYPE_PREPROCESSED_C) {
1069 translation_unit_t *const unit = do_parsing(in, filename);
1071 /* prints the AST even if errors occurred */
1072 if (mode == PrintAst) {
1073 type_set_output(out);
1074 ast_set_output(out);
1078 if(error_count > 0) {
1079 /* parsing failed because of errors */
1080 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1082 result = EXIT_FAILURE;
1084 } else if(warning_count > 0) {
1085 fprintf(stderr, "%u warning(s)\n", warning_count);
1088 if (in == preprocessed_in) {
1089 int pp_result = pclose(preprocessed_in);
1090 if (pp_result != EXIT_SUCCESS) {
1095 if(mode == BenchmarkParser) {
1097 } else if(mode == PrintFluffy) {
1098 write_fluffy_decls(out, unit);
1100 } else if (mode == PrintCaml) {
1101 write_caml_decls(out, unit);
1105 translation_unit_to_firm(unit);
1107 if (mode == ParseOnly) {
1111 if (mode == CompileDump) {
1113 ident *id = new_id_from_str(dumpfunction);
1114 ir_graph *irg = NULL;
1115 int n_irgs = get_irp_n_irgs();
1116 for(int i = 0; i < n_irgs; ++i) {
1117 ir_graph *tirg = get_irp_irg(i);
1118 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1126 fprintf(stderr, "No graph for function '%s' found\n",
1131 dump_ir_block_graph_file(irg, out);
1136 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1137 have_const_functions);
1138 if (asm_out != out) {
1142 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1143 copy_file(asm_out, in);
1144 if (in == preprocessed_in) {
1145 int pp_result = pclose(preprocessed_in);
1146 if (pp_result != EXIT_SUCCESS) {
1150 if(asm_out != out) {
1155 if (mode == Compile)
1158 /* if we're here then we have preprocessed assembly */
1159 filename = asm_tempfile;
1160 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1163 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1165 const char *filename_o;
1166 if(mode == CompileAssemble) {
1168 filename_o = outname;
1170 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1175 assemble(filename_o, filename);
1177 size_t len = strlen(filename_o) + 1;
1178 filename = obstack_copy(&file_obst, filename_o, len);
1179 filetype = FILETYPE_OBJECT;
1182 /* ok we're done here, process next file */
1183 file->name = filename;
1184 file->type = filetype;
1187 if (result != EXIT_SUCCESS)
1190 /* link program file */
1191 if(mode == CompileAssembleLink) {
1192 obstack_1grow(&ldflags_obst, '\0');
1193 const char *flags = obstack_finish(&ldflags_obst);
1195 /* construct commandline */
1196 obstack_printf(&file_obst, "%s", LINKER);
1197 for (file_list_entry_t *entry = files; entry != NULL;
1198 entry = entry->next) {
1199 if (entry->type != FILETYPE_OBJECT)
1202 add_flag(&file_obst, "%s", entry->name);
1205 add_flag(&file_obst, "-o");
1206 add_flag(&file_obst, outname);
1207 obstack_printf(&file_obst, "%s", flags);
1208 obstack_1grow(&file_obst, '\0');
1210 char *commandline = obstack_finish(&file_obst);
1215 int err = system(commandline);
1216 if(err != EXIT_SUCCESS) {
1217 fprintf(stderr, "linker reported an error\n");
1222 obstack_free(&cppflags_obst, NULL);
1223 obstack_free(&ldflags_obst, NULL);
1224 obstack_free(&file_obst, NULL);
1233 exit_symbol_table();