2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
69 #include "type_hash.h"
72 #include "diagnostic.h"
73 #include "lang_features.h"
74 #include "driver/firm_opt.h"
75 #include "driver/firm_cmdline.h"
76 #include "adt/error.h"
77 #include "write_fluffy.h"
78 #include "write_caml.h"
83 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=__SIZE_TYPE__ -m32 -U__STRICT_ANSI__"
87 #define LINKER "gcc -m32"
91 #define ASSEMBLER "as --32"
94 /** The current c mode/dialect. */
95 unsigned int c_mode = _C89|_C99|_GNUC;
97 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
98 unsigned int machine_size = 32;
100 /** true if the char type is signed. */
101 bool char_is_signed = true;
103 /** true for strict language checking. */
104 bool strict_mode = false;
106 /** use builtins for some libc functions */
107 bool use_builtins = false;
109 /* to switch on printing of implicit casts */
110 extern bool print_implicit_casts;
112 /* to switch on printing of parenthesis to indicate operator precedence */
113 extern bool print_parenthesis;
116 static struct obstack cppflags_obst, ldflags_obst;
118 typedef struct file_list_entry_t file_list_entry_t;
120 typedef enum filetype_t {
123 FILETYPE_PREPROCESSED_C,
125 FILETYPE_PREPROCESSED_ASSEMBLER,
130 struct file_list_entry_t {
131 const char *name; /**< filename or NULL for stdin */
133 file_list_entry_t *next;
136 #if defined(_DEBUG) || defined(FIRM_DEBUG)
138 * Debug printf implementation.
140 * @param fmt printf style format parameter
142 void dbg_printf(const char *fmt, ...)
146 if (firm_dump.debug_print) {
152 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
154 static void initialize_firm(void)
158 dump_consts_local(1);
159 dump_keepalive_edges(1);
162 static void get_output_name(char *buf, size_t buflen, const char *inputname,
165 size_t last_dot = 0xffffffff;
168 if(inputname == NULL) {
169 snprintf(buf, buflen, "a%s", newext);
173 for(const char *c = inputname; *c != 0; ++c) {
178 if(last_dot == 0xffffffff)
181 if(last_dot >= buflen)
182 panic("filename too long");
183 memcpy(buf, inputname, last_dot);
185 size_t extlen = strlen(newext) + 1;
186 if(extlen + last_dot >= buflen)
187 panic("filename too long");
188 memcpy(buf+last_dot, newext, extlen);
191 #include "builtins.h"
193 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
198 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
202 lexer_open_stream(in, input_name);
205 translation_unit_t *unit = finish_parsing();
209 static void lextest(FILE *in, const char *fname)
211 lexer_open_stream(in, fname);
214 lexer_next_preprocessing_token();
215 print_token(stdout, &lexer_token);
217 } while(lexer_token.type != T_EOF);
220 static void add_flag(struct obstack *obst, const char *format, ...)
223 va_start(ap, format);
226 vsnprintf(buf, sizeof(buf), format, ap);
228 /* escape stuff... */
229 obstack_1grow(obst, ' ');
230 for (char *c = buf; *c != '\0'; ++c) {
243 obstack_1grow(obst, '\\');
246 obstack_1grow(obst, *c);
254 static FILE *preprocess(const char *fname)
257 obstack_1grow(&cppflags_obst, '\0');
258 const char *flags = obstack_finish(&cppflags_obst);
260 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
265 FILE *f = popen(buf, "r");
267 fprintf(stderr, "invoking preprocessor failed\n");
273 static void assemble(const char *out, const char *in)
277 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
282 int err = system(buf);
284 fprintf(stderr, "assembler reported an error\n");
289 static const char *try_dir(const char *dir)
293 if(access(dir, R_OK | W_OK | X_OK) == 0)
298 static const char *get_tempdir(void)
300 static const char *tmpdir = NULL;
306 tmpdir = try_dir(getenv("TMPDIR"));
308 tmpdir = try_dir(getenv("TMP"));
310 tmpdir = try_dir(getenv("TEMP"));
314 tmpdir = try_dir(P_tmpdir);
318 tmpdir = try_dir("/var/tmp");
320 tmpdir = try_dir("/usr/tmp");
322 tmpdir = try_dir("/tmp");
331 /* cheap and nasty mkstemp replacement */
332 static int mkstemp(char *templ)
335 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
340 * an own version of tmpnam, which: writes in a buffer, emits no warnings
341 * during linking (like glibc/gnu ld do for tmpnam)...
343 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
345 const char *tempdir = get_tempdir();
347 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
349 int fd = mkstemp(buffer);
351 fprintf(stderr, "couldn't create temporary file: %s\n",
355 FILE *out = fdopen(fd, "w");
357 fprintf(stderr, "couldn't create temporary file FILE*\n");
365 * Do the necessary lowering for compound parameters.
367 void lower_compound_params(void)
369 lower_params_t params;
371 params.def_ptr_alignment = 4;
372 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
373 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
374 params.find_pointer_type = NULL;
375 params.ret_compound_in_regs = NULL;
376 lower_calls_with_compounds(¶ms);
379 typedef enum compile_mode_t {
393 static void usage(const char *argv0)
395 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
398 static void print_cparser_version(void) {
400 firm_get_version(&ver);
402 printf("cparser (%s) using libFirm (%u.%u",
403 cparser_REVISION, ver.major, ver.minor);
404 if(ver.revision[0] != 0) {
406 fputs(ver.revision, stdout);
408 if(ver.build[0] != 0) {
410 fputs(ver.build, stdout);
415 static void set_be_option(const char *arg)
417 int res = firm_be_option(arg);
422 static void set_option(const char *arg)
424 int res = firm_option(arg);
429 static void copy_file(FILE *dest, FILE *input)
433 while (!feof(input) && !ferror(dest)) {
434 size_t read = fread(buf, 1, sizeof(buf), input);
435 if(fwrite(buf, 1, read, dest) != read) {
436 perror("couldn't write output");
441 static inline bool streq(char const* a, char const* b)
443 return strcmp(a, b) == 0;
446 static inline bool strstart(char const* str, char const* start)
451 } while (*str++ == *start++);
455 static FILE *open_file(const char *filename)
457 if (streq(filename, "-")) {
461 FILE *in = fopen(filename, "r");
463 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
471 static filetype_t get_filetype_from_string(const char *string)
473 if (streq(string, "c") || streq(string, "c-header"))
475 if (streq(string, "assembler"))
476 return FILETYPE_PREPROCESSED_ASSEMBLER;
477 if (streq(string, "assembler-with-cpp"))
478 return FILETYPE_ASSEMBLER;
479 if (streq(string, "none"))
480 return FILETYPE_AUTODETECT;
482 return FILETYPE_UNKNOWN;
485 int main(int argc, char **argv)
489 const char *outname = NULL;
490 const char *dumpfunction = NULL;
491 compile_mode_t mode = CompileAssembleLink;
493 int result = EXIT_SUCCESS;
494 char cpu_arch[16] = "ia32";
495 file_list_entry_t *files = NULL;
496 file_list_entry_t *last_file = NULL;
497 struct obstack file_obst;
499 obstack_init(&cppflags_obst);
500 obstack_init(&ldflags_obst);
501 obstack_init(&file_obst);
503 #define GET_ARG_AFTER(def, args) \
504 def = &arg[sizeof(args)-1]; \
505 if(def[0] == '\0') { \
508 fprintf(stderr, "error: expected argument after '" args "'\n"); \
509 argument_errors = true; \
513 if(def[0] == '-' && def[1] != '\0') { \
514 fprintf(stderr, "error: expected argument after '" args "'\n"); \
515 argument_errors = true; \
520 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
522 /* early options parsing (find out optimisation level) */
523 for(int i = 1; i < argc; ++i) {
524 const char *arg = argv[i];
528 const char *option = &arg[1];
529 if(option[0] == 'O') {
530 sscanf(&option[1], "%d", &opt_level);
534 /* apply optimisation level */
537 set_option("no-opt");
540 set_option("no-inline");
544 set_option("strict-aliasing");
547 set_option("cond-eval");
548 set_option("if-conv");
552 set_option("inline");
553 set_option("deconv");
554 set_be_option("omitfp");
558 /* parse rest of options */
559 filetype_t forced_filetype = FILETYPE_AUTODETECT;
560 bool help_displayed = false;
561 bool argument_errors = false;
562 for(int i = 1; i < argc; ++i) {
563 const char *arg = argv[i];
564 if(arg[0] == '-' && arg[1] != 0) {
566 const char *option = &arg[1];
567 if(option[0] == 'o') {
568 GET_ARG_AFTER(outname, "-o");
569 } else if(option[0] == 'g') {
570 set_be_option("debuginfo=stabs");
571 set_be_option("omitfp=no");
572 set_be_option("ia32-nooptcc=yes");
573 } else if(SINGLE_OPTION('c')) {
574 mode = CompileAssemble;
575 } else if(SINGLE_OPTION('E')) {
576 mode = PreprocessOnly;
577 } else if(SINGLE_OPTION('S')) {
579 } else if(option[0] == 'O') {
581 } else if(option[0] == 'I') {
583 GET_ARG_AFTER(opt, "-I");
584 add_flag(&cppflags_obst, "-I%s", opt);
585 } else if(option[0] == 'D') {
587 GET_ARG_AFTER(opt, "-D");
588 add_flag(&cppflags_obst, "-D%s", opt);
589 } else if(option[0] == 'U') {
591 GET_ARG_AFTER(opt, "-U");
592 add_flag(&cppflags_obst, "-U%s", opt);
593 } else if(option[0] == 'l') {
595 GET_ARG_AFTER(opt, "-l");
596 add_flag(&ldflags_obst, "-l%s", opt);
597 } else if(option[0] == 'L') {
599 GET_ARG_AFTER(opt, "-L");
600 add_flag(&ldflags_obst, "-L%s", opt);
601 } else if(SINGLE_OPTION('v')) {
603 } else if(SINGLE_OPTION('w')) {
604 inhibit_all_warnings = true;
605 } else if(option[0] == 'x') {
607 GET_ARG_AFTER(opt, "-x");
608 forced_filetype = get_filetype_from_string(opt);
609 if (forced_filetype == FILETYPE_UNKNOWN) {
610 fprintf(stderr, "Unknown language '%s'\n", opt);
611 argument_errors = true;
613 } else if (streq(option, "M")) {
614 mode = PreprocessOnly;
615 add_flag(&cppflags_obst, "-M");
616 } else if (streq(option, "MMD") ||
617 streq(option, "MD") ||
618 streq(option, "MM") ||
619 streq(option, "MP")) {
620 add_flag(&cppflags_obst, "-%s", option);
621 } else if (streq(option, "MT") ||
622 streq(option, "MQ") ||
623 streq(option, "MF")) {
625 GET_ARG_AFTER(opt, "-MT");
626 add_flag(&cppflags_obst, "-%s", option);
627 add_flag(&cppflags_obst, "%s", opt);
628 } else if (streq(option, "pipe")) {
629 /* here for gcc compatibility */
630 } else if (option[0] == 'f') {
631 bool truth_value = true;
633 GET_ARG_AFTER(opt, "-f");
634 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
639 if (streq(opt, "dollars-in-identifiers")) {
640 allow_dollar_in_symbol = truth_value;
641 } else if (streq(opt, "short-wchar")) {
642 opt_short_wchar_t = truth_value;
643 } else if (streq(opt, "syntax-only")) {
645 } else if (streq(opt, "omit-frame-pointer")) {
646 set_be_option(truth_value ? "omitfp" : "omitfp=no");
647 } else if (streq(opt, "strength-reduce")) {
648 firm_option("strength-red");
649 } else if (streq(opt, "fast-math") ||
650 streq(opt, "jump-tables") ||
651 streq(opt, "unroll-loops") ||
652 streq(opt, "expensive-optimizations") ||
653 streq(opt, "common") ||
655 strstart(opt, "align-loops=") ||
656 strstart(opt, "align-jumps=") ||
657 strstart(opt, "align-functions=")) {
658 fprintf(stderr, "ignoring gcc option '-f%s'\n", truth_value ? opt : opt - 3);
662 int res = firm_option(opt);
664 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
666 argument_errors = true;
668 } else if (res == -1) {
669 help_displayed = true;
672 } else if (option[0] == 'b') {
674 GET_ARG_AFTER(opt, "-b");
675 int res = firm_be_option(opt);
677 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
679 argument_errors = true;
680 } else if (res == -1) {
681 help_displayed = true;
682 } else if (strstart(opt, "isa=")) {
683 strncpy(cpu_arch, opt, sizeof(cpu_arch));
685 } else if (option[0] == 'W') {
686 if (strstart(option + 1, "l,")) // a gcc-style linker option
689 GET_ARG_AFTER(opt, "-Wl,");
690 add_flag(&ldflags_obst, "-Wl,%s", opt);
692 else set_warning_opt(&option[1]);
693 } else if(option[0] == 'm') {
698 GET_ARG_AFTER(opt, "-m");
699 if (strstart(opt, "arch=")) {
700 GET_ARG_AFTER(opt, "-march=");
701 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
702 int res = firm_be_option(arch_opt);
704 argument_errors = true;
706 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
707 int res = firm_be_option(arch_opt);
709 argument_errors = true;
711 } else if (strstart(opt, "tune=")) {
712 GET_ARG_AFTER(opt, "-mtune=");
713 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
714 int res = firm_be_option(arch_opt);
716 argument_errors = true;
717 } else if (strstart(opt, "cpu=")) {
718 GET_ARG_AFTER(opt, "-mcpu=");
719 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
720 int res = firm_be_option(arch_opt);
722 argument_errors = true;
723 } else if (strstart(opt, "fpmath=")) {
724 GET_ARG_AFTER(opt, "-mfpmath=");
725 if (streq(opt, "387"))
727 else if (streq(opt, "sse"))
730 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
731 argument_errors = true;
733 if(!argument_errors) {
734 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
735 int res = firm_be_option(arch_opt);
737 argument_errors = true;
739 } else if (strstart(opt, "preferred-stack-boundary=")) {
740 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
741 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
742 int res = firm_be_option(arch_opt);
744 argument_errors = true;
745 } else if (streq(opt, "omit-leaf-frame-pointer")) {
746 set_be_option("omitleaffp=1");
747 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
748 set_be_option("omitleaffp=0");
751 long int value = strtol(opt, &endptr, 10);
752 if (*endptr != '\0') {
753 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
754 argument_errors = true;
756 if (value != 16 && value != 32 && value != 64) {
757 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
758 argument_errors = true;
760 machine_size = (unsigned int)value;
763 } else if (streq(option, "pg")) {
764 set_be_option("gprof");
765 add_flag(&ldflags_obst, "-pg");
766 } else if (streq(option, "pedantic") ||
767 streq(option, "ansi")) {
768 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
769 } else if (streq(option, "shared")) {
770 add_flag(&ldflags_obst, "-shared");
771 } else if (strstart(option, "std=")) {
772 if (streq(&option[4], "c99")) {
774 } else if (streq(&option[4], "c89")) {
776 } else if (streq(&option[4], "gnu99")) {
777 c_mode = _C89|_C99|_GNUC;
778 } else if (streq(&option[4], "microsoft")) {
779 c_mode = _C89|_C99|_MS;
781 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
782 } else if (streq(option, "version")) {
783 print_cparser_version();
784 } else if (option[0] == '-') {
785 /* double dash option */
787 if (streq(option, "gcc")) {
789 } else if (streq(option, "no-gcc")) {
791 } else if (streq(option, "ms")) {
793 } else if (streq(option, "no-ms")) {
795 } else if (streq(option, "signed-chars")) {
796 char_is_signed = true;
797 } else if (streq(option, "unsigned-chars")) {
798 char_is_signed = false;
799 } else if (streq(option, "strict")) {
801 } else if (streq(option, "lextest")) {
803 } else if (streq(option, "benchmark")) {
804 mode = BenchmarkParser;
805 } else if (streq(option, "print-ast")) {
807 } else if (streq(option, "print-implicit-cast")) {
808 print_implicit_casts = true;
809 } else if (streq(option, "print-parenthesis")) {
810 print_parenthesis = true;
811 } else if (streq(option, "print-fluffy")) {
813 } else if (streq(option, "print-caml")) {
815 } else if (streq(option, "version")) {
816 print_cparser_version();
818 } else if (streq(option, "dump-function")) {
821 fprintf(stderr, "error: "
822 "expected argument after '--dump-function'\n");
823 argument_errors = true;
826 dumpfunction = argv[i];
829 fprintf(stderr, "error: unknown argument '%s'\n", arg);
830 argument_errors = true;
833 fprintf(stderr, "error: unknown argument '%s'\n", arg);
834 argument_errors = true;
837 filetype_t type = forced_filetype;
838 const char *filename = arg;
839 if (type == FILETYPE_AUTODETECT) {
840 size_t const len = strlen(arg);
841 if (len < 2 && arg[0] == '-') {
842 /* - implicitly means C source file */
845 } else if (len > 2 && arg[len - 2] == '.') {
846 switch (arg[len - 1]) {
847 case 'c': type = FILETYPE_C; break;
848 case 'h': type = FILETYPE_C; break;
849 case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break;
850 case 'S': type = FILETYPE_ASSEMBLER; break;
853 case 'o': type = FILETYPE_OBJECT; break;
855 } else if (len > 3 && arg[len - 3] == '.') {
856 if (streq(arg + len - 2, "so")) {
857 type = FILETYPE_OBJECT;
861 if (type == FILETYPE_AUTODETECT) {
862 fprintf(stderr, "'%s': file format not recognized\n", arg);
867 file_list_entry_t *entry
868 = obstack_alloc(&file_obst, sizeof(entry[0]));
869 memset(entry, 0, sizeof(entry[0]));
873 if (last_file != NULL) {
874 last_file->next = entry;
883 fprintf(stderr, "error: no input files specified\n");
884 argument_errors = true;
887 if (help_displayed) {
888 return !argument_errors;
890 if (argument_errors) {
895 /* we do the lowering in ast2firm */
896 firm_opt.lower_bitfields = FALSE;
909 char outnamebuf[4096];
910 if (outname == NULL) {
911 const char *filename = files->name;
914 case BenchmarkParser:
925 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
926 outname = outnamebuf;
928 case CompileAssemble:
929 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
930 outname = outnamebuf;
933 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
935 outname = outnamebuf;
937 case CompileAssembleLink:
947 assert(outname != NULL);
950 if (streq(outname, "-")) {
953 out = fopen(outname, "w");
955 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
961 file_list_entry_t *file;
962 for (file = files; file != NULL; file = file->next) {
963 char asm_tempfile[1024];
964 const char *filename = file->name;
965 filetype_t filetype = file->type;
967 if (file->type == FILETYPE_OBJECT)
971 if (mode == LexTest) {
973 in = open_file(filename);
974 lextest(in, filename);
979 FILE *preprocessed_in = NULL;
980 if (filetype == FILETYPE_C || filetype == FILETYPE_ASSEMBLER) {
981 /* no support for input on FILE* yet */
983 panic("internal compiler error: in for preprocessor != NULL");
985 preprocessed_in = preprocess(filename);
986 if (mode == PreprocessOnly) {
987 copy_file(out, preprocessed_in);
988 int result = pclose(preprocessed_in);
993 if (filetype == FILETYPE_C) {
994 filetype = FILETYPE_PREPROCESSED_C;
995 } else if (filetype == FILETYPE_ASSEMBLER) {
996 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
998 panic("internal compiler error: unknown filetype at preproc");
1001 in = preprocessed_in;
1005 if(mode == Compile) {
1008 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile),
1013 in = open_file(filename);
1015 /* preprocess and compile */
1016 if (filetype == FILETYPE_PREPROCESSED_C) {
1017 translation_unit_t *const unit = do_parsing(in, filename);
1018 if (in == preprocessed_in) {
1019 int pp_result = pclose(preprocessed_in);
1020 if (pp_result != EXIT_SUCCESS) {
1025 /* prints the AST even if errors occurred */
1026 if (mode == PrintAst) {
1027 type_set_output(out);
1028 ast_set_output(out);
1032 if(error_count > 0) {
1033 /* parsing failed because of errors */
1034 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1036 result = EXIT_FAILURE;
1038 } else if(warning_count > 0) {
1039 fprintf(stderr, "%u warning(s)\n", warning_count);
1042 if(mode == BenchmarkParser) {
1044 } else if(mode == PrintFluffy) {
1045 write_fluffy_decls(out, unit);
1047 } else if (mode == PrintCaml) {
1048 write_caml_decls(out, unit);
1052 translation_unit_to_firm(unit);
1054 if (mode == ParseOnly) {
1058 if (mode == CompileDump) {
1060 ident *id = new_id_from_str(dumpfunction);
1061 ir_graph *irg = NULL;
1062 int n_irgs = get_irp_n_irgs();
1063 for(int i = 0; i < n_irgs; ++i) {
1064 ir_graph *tirg = get_irp_irg(i);
1065 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1073 fprintf(stderr, "No graph for function '%s' found\n",
1078 dump_ir_block_graph_file(irg, out);
1083 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1084 /*firm_const_exists=*/0);
1085 if (asm_out != out) {
1089 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1090 copy_file(asm_out, in);
1091 if (in == preprocessed_in) {
1092 int pp_result = pclose(preprocessed_in);
1093 if (pp_result != EXIT_SUCCESS) {
1097 if(asm_out != out) {
1102 if (mode == Compile)
1105 /* if we're here then we have preprocessed assembly */
1106 filename = asm_tempfile;
1107 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1110 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1112 const char *filename_o;
1113 if(mode == CompileAssemble) {
1115 filename_o = outname;
1117 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1122 assemble(filename_o, filename);
1124 size_t len = strlen(filename_o) + 1;
1125 filename = obstack_copy(&file_obst, filename_o, len);
1126 filetype = FILETYPE_OBJECT;
1129 /* ok we're done here, process next file */
1130 file->name = filename;
1131 file->type = filetype;
1134 if (result != EXIT_SUCCESS)
1137 /* link program file */
1138 if(mode == CompileAssembleLink) {
1139 obstack_1grow(&ldflags_obst, '\0');
1140 const char *flags = obstack_finish(&ldflags_obst);
1142 /* construct commandline */
1143 obstack_printf(&file_obst, "%s", LINKER);
1144 for (file_list_entry_t *entry = files; entry != NULL;
1145 entry = entry->next) {
1146 if (entry->type != FILETYPE_OBJECT)
1149 add_flag(&file_obst, "%s", entry->name);
1152 add_flag(&file_obst, "-o");
1153 add_flag(&file_obst, outname);
1154 obstack_printf(&file_obst, "%s", flags);
1155 obstack_1grow(&file_obst, '\0');
1157 char *commandline = obstack_finish(&file_obst);
1162 int err = system(commandline);
1163 if(err != EXIT_SUCCESS) {
1164 fprintf(stderr, "linker reported an error\n");
1169 obstack_free(&cppflags_obst, NULL);
1170 obstack_free(&ldflags_obst, NULL);
1171 obstack_free(&file_obst, NULL);
1180 exit_symbol_table();