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"
73 #include "diagnostic.h"
74 #include "lang_features.h"
75 #include "driver/firm_opt.h"
76 #include "driver/firm_cmdline.h"
77 #include "adt/error.h"
78 #include "write_fluffy.h"
79 #include "write_caml.h"
85 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
87 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
92 #define LINKER "gcc -m32"
97 #define ASSEMBLER "gcc -c -xassembler"
99 #define ASSEMBLER "as --32"
103 /** The current c mode/dialect. */
104 lang_features_t c_mode = _C89 | _ANSI | _C99 | _GNUC;
106 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
107 unsigned int machine_size = 32;
109 /** true if the char type is signed. */
110 bool char_is_signed = true;
112 /** true for strict language checking. */
113 bool strict_mode = false;
115 /** use builtins for some libc functions */
116 bool use_builtins = false;
118 /** we have extern function with const attribute. */
119 bool have_const_functions = false;
121 /* to switch on printing of implicit casts */
122 extern bool print_implicit_casts;
124 /* to switch on printing of parenthesis to indicate operator precedence */
125 extern bool print_parenthesis;
128 static struct obstack cppflags_obst, ldflags_obst;
129 static char dep_target[1024];
130 static const char *outname;
132 typedef struct file_list_entry_t file_list_entry_t;
134 typedef enum filetype_t {
137 FILETYPE_PREPROCESSED_C,
139 FILETYPE_PREPROCESSED_CXX,
141 FILETYPE_PREPROCESSED_ASSEMBLER,
146 struct file_list_entry_t {
147 const char *name; /**< filename or NULL for stdin */
149 file_list_entry_t *next;
152 #if defined(_DEBUG) || defined(FIRM_DEBUG)
154 * Debug printf implementation.
156 * @param fmt printf style format parameter
158 void dbg_printf(const char *fmt, ...)
162 if (firm_dump.debug_print) {
168 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
170 static void initialize_firm(void)
174 dump_consts_local(1);
175 dump_keepalive_edges(1);
178 static void get_output_name(char *buf, size_t buflen, const char *inputname,
181 size_t last_dot = 0xffffffff;
184 if(inputname == NULL) {
185 snprintf(buf, buflen, "a%s", newext);
189 for(const char *c = inputname; *c != 0; ++c) {
194 if(last_dot == 0xffffffff)
197 if(last_dot >= buflen)
198 panic("filename too long");
199 memcpy(buf, inputname, last_dot);
201 size_t extlen = strlen(newext) + 1;
202 if(extlen + last_dot >= buflen)
203 panic("filename too long");
204 memcpy(buf+last_dot, newext, extlen);
207 #include "builtins.h"
209 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
214 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
218 lexer_open_stream(in, input_name);
221 translation_unit_t *unit = finish_parsing();
225 static void lextest(FILE *in, const char *fname)
227 lexer_open_stream(in, fname);
230 lexer_next_preprocessing_token();
231 print_token(stdout, &lexer_token);
233 } while(lexer_token.type != T_EOF);
236 static void add_flag(struct obstack *obst, const char *format, ...)
239 va_start(ap, format);
242 vsnprintf(buf, sizeof(buf), format, ap);
244 /* escape stuff... */
245 obstack_1grow(obst, ' ');
246 for (char *c = buf; *c != '\0'; ++c) {
259 obstack_1grow(obst, '\\');
262 obstack_1grow(obst, *c);
270 static const char *type_to_string(type_t *type)
272 assert(type->kind == TYPE_ATOMIC);
273 return get_atomic_kind_name(type->atomic.akind);
276 static FILE *preprocess(const char *fname)
278 obstack_1grow(&cppflags_obst, '\0');
279 const char *flags = obstack_finish(&cppflags_obst);
281 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
283 /* setup default defines */
284 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
285 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s",
286 type_to_string(type_wchar_t));
287 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
288 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
290 /* handle dependency generation */
291 if (dep_target[0] != '\0') {
292 add_flag(&cppflags_obst, "-MF");
293 add_flag(&cppflags_obst, "%s", dep_target);
294 if (outname != NULL) {
295 add_flag(&cppflags_obst, "-MQ");
296 add_flag(&cppflags_obst, "%s", outname);
299 if (flags[0] != '\0') {
300 obstack_printf(&cppflags_obst, " %s", flags);
302 add_flag(&cppflags_obst, "%s", fname);
304 obstack_1grow(&cppflags_obst, '\0');
305 const char *buf = obstack_finish(&cppflags_obst);
310 FILE *f = popen(buf, "r");
312 fprintf(stderr, "invoking preprocessor failed\n");
319 static void assemble(const char *out, const char *in)
323 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
328 int err = system(buf);
330 fprintf(stderr, "assembler reported an error\n");
335 static const char *try_dir(const char *dir)
339 if(access(dir, R_OK | W_OK | X_OK) == 0)
344 static const char *get_tempdir(void)
346 static const char *tmpdir = NULL;
352 tmpdir = try_dir(getenv("TMPDIR"));
354 tmpdir = try_dir(getenv("TMP"));
356 tmpdir = try_dir(getenv("TEMP"));
360 tmpdir = try_dir(P_tmpdir);
364 tmpdir = try_dir("/var/tmp");
366 tmpdir = try_dir("/usr/tmp");
368 tmpdir = try_dir("/tmp");
377 /* cheap and nasty mkstemp replacement */
378 static int mkstemp(char *templ)
381 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
386 * an own version of tmpnam, which: writes in a buffer, emits no warnings
387 * during linking (like glibc/gnu ld do for tmpnam)...
389 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
391 const char *tempdir = get_tempdir();
393 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
395 int fd = mkstemp(buffer);
397 fprintf(stderr, "couldn't create temporary file: %s\n",
401 FILE *out = fdopen(fd, "w");
403 fprintf(stderr, "couldn't create temporary file FILE*\n");
411 * Do the necessary lowering for compound parameters.
413 void lower_compound_params(void)
415 lower_params_t params;
417 params.def_ptr_alignment = 4;
418 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
419 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
420 params.find_pointer_type = NULL;
421 params.ret_compound_in_regs = NULL;
422 lower_calls_with_compounds(¶ms);
425 typedef enum compile_mode_t {
439 static void usage(const char *argv0)
441 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
444 static void print_cparser_version(void) {
446 firm_get_version(&ver);
448 printf("cparser (%s) using libFirm (%u.%u",
449 cparser_REVISION, ver.major, ver.minor);
450 if(ver.revision[0] != 0) {
452 fputs(ver.revision, stdout);
454 if(ver.build[0] != 0) {
456 fputs(ver.build, stdout);
461 static void set_be_option(const char *arg)
463 int res = firm_be_option(arg);
468 static void set_option(const char *arg)
470 int res = firm_option(arg);
475 static void copy_file(FILE *dest, FILE *input)
479 while (!feof(input) && !ferror(dest)) {
480 size_t read = fread(buf, 1, sizeof(buf), input);
481 if(fwrite(buf, 1, read, dest) != read) {
482 perror("couldn't write output");
487 static inline bool streq(char const* a, char const* b)
489 return strcmp(a, b) == 0;
492 static inline bool strstart(char const* str, char const* start)
497 } while (*str++ == *start++);
501 static FILE *open_file(const char *filename)
503 if (streq(filename, "-")) {
507 FILE *in = fopen(filename, "r");
509 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
517 static filetype_t get_filetype_from_string(const char *string)
519 if (streq(string, "c") || streq(string, "c-header"))
521 if (streq(string, "c++") || streq(string, "c++-header"))
523 if (streq(string, "assembler"))
524 return FILETYPE_PREPROCESSED_ASSEMBLER;
525 if (streq(string, "assembler-with-cpp"))
526 return FILETYPE_ASSEMBLER;
527 if (streq(string, "none"))
528 return FILETYPE_AUTODETECT;
530 return FILETYPE_UNKNOWN;
533 static void init_os_support(void)
535 /* OS option must be set to the backend */
536 switch (firm_opt.os_support) {
537 case OS_SUPPORT_MINGW:
538 set_be_option("ia32-gasmode=mingw");
539 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
541 case OS_SUPPORT_LINUX:
542 set_be_option("ia32-gasmode=elf");
544 case OS_SUPPORT_MACHO:
545 set_be_option("ia32-gasmode=macho");
546 set_be_option("ia32-stackalign=4");
547 set_be_option("pic");
552 typedef enum lang_standard_t {
553 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
554 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
555 STANDARD_C89, /* ISO C90 (sic) */
556 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
557 STANDARD_C99, /* ISO C99 */
558 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
559 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
560 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
561 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
564 int main(int argc, char **argv)
568 const char *dumpfunction = NULL;
569 compile_mode_t mode = CompileAssembleLink;
571 int result = EXIT_SUCCESS;
572 char cpu_arch[16] = "ia32";
573 file_list_entry_t *files = NULL;
574 file_list_entry_t *last_file = NULL;
575 bool construct_dep_target = false;
576 struct obstack file_obst;
578 /* hack for now... */
579 if (strstr(argv[0], "pptest") != NULL) {
580 extern int pptest_main(int argc, char **argv);
581 return pptest_main(argc, argv);
584 obstack_init(&cppflags_obst);
585 obstack_init(&ldflags_obst);
586 obstack_init(&file_obst);
588 #define GET_ARG_AFTER(def, args) \
589 def = &arg[sizeof(args)-1]; \
590 if(def[0] == '\0') { \
593 fprintf(stderr, "error: expected argument after '" args "'\n"); \
594 argument_errors = true; \
598 if(def[0] == '-' && def[1] != '\0') { \
599 fprintf(stderr, "error: expected argument after '" args "'\n"); \
600 argument_errors = true; \
605 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
607 /* early options parsing (find out optimisation level and OS) */
608 for (int i = 1; i < argc; ++i) {
609 const char *arg = argv[i];
613 const char *option = &arg[1];
614 if (option[0] == 'O') {
615 sscanf(&option[1], "%d", &opt_level);
617 if (strcmp(arg, "-fwin32") == 0) {
618 firm_opt.os_support = OS_SUPPORT_MINGW;
619 } else if (strcmp(arg, "-fmac") == 0) {
620 firm_opt.os_support = OS_SUPPORT_MACHO;
621 } else if (strcmp(arg, "-flinux") == 0) {
622 firm_opt.os_support = OS_SUPPORT_LINUX;
626 /* set target/os specific stuff */
629 /* apply optimisation level */
632 set_option("no-opt");
635 set_option("no-inline");
639 set_option("strict-aliasing");
640 /* use_builtins = true; */
643 set_option("cond-eval");
644 set_option("if-conv");
647 set_option("inline");
648 set_option("deconv");
649 set_be_option("omitfp");
653 /* parse rest of options */
654 lang_standard_t standard = STANDARD_DEFAULT;
655 lang_features_t features_on = 0;
656 lang_features_t features_off = 0;
657 filetype_t forced_filetype = FILETYPE_AUTODETECT;
658 bool help_displayed = false;
659 bool argument_errors = false;
660 for(int i = 1; i < argc; ++i) {
661 const char *arg = argv[i];
662 if (arg[0] == '-' && arg[1] != '\0') {
664 const char *option = &arg[1];
665 if(option[0] == 'o') {
666 GET_ARG_AFTER(outname, "-o");
667 } else if(option[0] == 'g') {
668 set_be_option("debuginfo=stabs");
669 set_be_option("omitfp=no");
670 set_be_option("ia32-nooptcc=yes");
671 } else if(SINGLE_OPTION('c')) {
672 mode = CompileAssemble;
673 } else if(SINGLE_OPTION('E')) {
674 mode = PreprocessOnly;
675 } else if(SINGLE_OPTION('S')) {
677 } else if(option[0] == 'O') {
679 } else if(option[0] == 'I') {
681 GET_ARG_AFTER(opt, "-I");
682 add_flag(&cppflags_obst, "-I%s", opt);
683 } else if(option[0] == 'D') {
685 GET_ARG_AFTER(opt, "-D");
686 add_flag(&cppflags_obst, "-D%s", opt);
687 } else if(option[0] == 'U') {
689 GET_ARG_AFTER(opt, "-U");
690 add_flag(&cppflags_obst, "-U%s", opt);
691 } else if(option[0] == 'l') {
693 GET_ARG_AFTER(opt, "-l");
694 add_flag(&ldflags_obst, "-l%s", opt);
695 } else if(option[0] == 'L') {
697 GET_ARG_AFTER(opt, "-L");
698 add_flag(&ldflags_obst, "-L%s", opt);
699 } else if(SINGLE_OPTION('v')) {
701 } else if(SINGLE_OPTION('w')) {
702 memset(&warning, 0, sizeof(warning));
703 } else if(option[0] == 'x') {
705 GET_ARG_AFTER(opt, "-x");
706 forced_filetype = get_filetype_from_string(opt);
707 if (forced_filetype == FILETYPE_UNKNOWN) {
708 fprintf(stderr, "Unknown language '%s'\n", opt);
709 argument_errors = true;
711 } else if (streq(option, "M")) {
712 mode = PreprocessOnly;
713 add_flag(&cppflags_obst, "-M");
714 } else if (streq(option, "MMD") ||
715 streq(option, "MD")) {
716 construct_dep_target = true;
717 add_flag(&cppflags_obst, "-%s", option);
718 } else if(streq(option, "MM") ||
719 streq(option, "MP")) {
720 add_flag(&cppflags_obst, "-%s", option);
721 } else if (streq(option, "MT") ||
722 streq(option, "MQ") ||
723 streq(option, "MF")) {
725 GET_ARG_AFTER(opt, "-MT");
726 add_flag(&cppflags_obst, "-%s", option);
727 add_flag(&cppflags_obst, "%s", opt);
728 } else if (streq(option, "pipe")) {
729 /* here for gcc compatibility */
730 } else if (option[0] == 'f') {
731 char const *orig_opt;
732 GET_ARG_AFTER(orig_opt, "-f");
734 if (strstart(orig_opt, "align-loops=") ||
735 strstart(orig_opt, "align-jumps=") ||
736 strstart(orig_opt, "align-functions=")) {
737 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
739 char const *opt = orig_opt;
740 bool truth_value = true;
741 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
746 if (streq(opt, "dollars-in-identifiers")) {
747 allow_dollar_in_symbol = truth_value;
748 } if (streq(opt, "builtins")) {
749 use_builtins = truth_value;
750 } else if (streq(opt, "short-wchar")) {
751 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
753 } else if (streq(opt, "syntax-only")) {
754 mode = truth_value ? ParseOnly : CompileAssembleLink;
755 } else if (streq(opt, "omit-frame-pointer")) {
756 set_be_option(truth_value ? "omitfp" : "omitfp=no");
757 } else if (streq(opt, "strength-reduce")) {
758 firm_option(truth_value ? "strength-red" : "no-strength-red");
759 } else if (streq(opt, "fast-math") ||
760 streq(opt, "jump-tables") ||
761 streq(opt, "unroll-loops") ||
762 streq(opt, "expensive-optimizations") ||
763 streq(opt, "common") ||
765 streq(opt, "align-loops") ||
766 streq(opt, "align-jumps") ||
767 streq(opt, "align-functions")) {
768 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
770 int res = firm_option(orig_opt);
772 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
774 argument_errors = true;
776 } else if (res == -1) {
777 help_displayed = true;
781 } else if (option[0] == 'b') {
783 GET_ARG_AFTER(opt, "-b");
784 int res = firm_be_option(opt);
786 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
788 argument_errors = true;
789 } else if (res == -1) {
790 help_displayed = true;
791 } else if (strstart(opt, "isa=")) {
792 strncpy(cpu_arch, opt, sizeof(cpu_arch));
794 } else if (option[0] == 'W') {
795 if (strstart(option + 1, "l,")) // a gcc-style linker option
798 GET_ARG_AFTER(opt, "-Wl,");
799 add_flag(&ldflags_obst, "-Wl,%s", opt);
801 else set_warning_opt(&option[1]);
802 } else if(option[0] == 'm') {
807 GET_ARG_AFTER(opt, "-m");
808 if (strstart(opt, "arch=")) {
809 GET_ARG_AFTER(opt, "-march=");
810 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
811 int res = firm_be_option(arch_opt);
813 argument_errors = true;
815 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
816 int res = firm_be_option(arch_opt);
818 argument_errors = true;
820 } else if (strstart(opt, "tune=")) {
821 GET_ARG_AFTER(opt, "-mtune=");
822 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
823 int res = firm_be_option(arch_opt);
825 argument_errors = true;
826 } else if (strstart(opt, "cpu=")) {
827 GET_ARG_AFTER(opt, "-mcpu=");
828 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
829 int res = firm_be_option(arch_opt);
831 argument_errors = true;
832 } else if (strstart(opt, "fpmath=")) {
833 GET_ARG_AFTER(opt, "-mfpmath=");
834 if (streq(opt, "387"))
836 else if (streq(opt, "sse"))
839 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
840 argument_errors = true;
842 if(!argument_errors) {
843 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
844 int res = firm_be_option(arch_opt);
846 argument_errors = true;
848 } else if (strstart(opt, "preferred-stack-boundary=")) {
849 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
850 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
851 int res = firm_be_option(arch_opt);
853 argument_errors = true;
854 } else if (streq(opt, "omit-leaf-frame-pointer")) {
855 set_be_option("omitleaffp=1");
856 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
857 set_be_option("omitleaffp=0");
860 long int value = strtol(opt, &endptr, 10);
861 if (*endptr != '\0') {
862 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
863 argument_errors = true;
865 if (value != 16 && value != 32 && value != 64) {
866 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
867 argument_errors = true;
869 machine_size = (unsigned int)value;
872 } else if (streq(option, "pg")) {
873 set_be_option("gprof");
874 add_flag(&ldflags_obst, "-pg");
875 } else if (streq(option, "pedantic") ||
876 streq(option, "ansi")) {
877 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
878 } else if (streq(option, "shared")) {
879 add_flag(&ldflags_obst, "-shared");
880 } else if (strstart(option, "std=")) {
881 const char *const o = &option[4];
883 streq(o, "c++") ? STANDARD_CXX98 :
884 streq(o, "c++98") ? STANDARD_CXX98 :
885 streq(o, "c89") ? STANDARD_C89 :
886 streq(o, "c99") ? STANDARD_C99 :
887 streq(o, "c9x") ? STANDARD_C99 : // deprecated
888 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
889 streq(o, "gnu89") ? STANDARD_GNU89 :
890 streq(o, "gnu99") ? STANDARD_GNU99 :
891 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
892 streq(o, "iso9899:1990") ? STANDARD_C89 :
893 streq(o, "iso9899:199409") ? STANDARD_C90 :
894 streq(o, "iso9899:1999") ? STANDARD_C99 :
895 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
896 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
897 } else if (streq(option, "version")) {
898 print_cparser_version();
899 } else if (option[0] == '-') {
900 /* double dash option */
902 if (streq(option, "gcc")) {
903 features_on |= _GNUC;
904 features_off &= ~_GNUC;
905 } else if (streq(option, "no-gcc")) {
906 features_on &= ~_GNUC;
907 features_off |= _GNUC;
908 } else if (streq(option, "ms")) {
910 features_off &= ~_MS;
911 } else if (streq(option, "no-ms")) {
914 } else if (streq(option, "signed-chars")) {
915 char_is_signed = true;
916 } else if (streq(option, "unsigned-chars")) {
917 char_is_signed = false;
918 } else if (streq(option, "strict")) {
920 } else if (streq(option, "lextest")) {
922 } else if (streq(option, "benchmark")) {
923 mode = BenchmarkParser;
924 } else if (streq(option, "print-ast")) {
926 } else if (streq(option, "print-implicit-cast")) {
927 print_implicit_casts = true;
928 } else if (streq(option, "print-parenthesis")) {
929 print_parenthesis = true;
930 } else if (streq(option, "print-fluffy")) {
932 } else if (streq(option, "print-caml")) {
934 } else if (streq(option, "version")) {
935 print_cparser_version();
937 } else if (streq(option, "dump-function")) {
940 fprintf(stderr, "error: "
941 "expected argument after '--dump-function'\n");
942 argument_errors = true;
945 dumpfunction = argv[i];
948 fprintf(stderr, "error: unknown argument '%s'\n", arg);
949 argument_errors = true;
952 fprintf(stderr, "error: unknown argument '%s'\n", arg);
953 argument_errors = true;
956 filetype_t type = forced_filetype;
957 if (type == FILETYPE_AUTODETECT) {
958 if (streq(arg, "-")) {
959 /* - implicitly means C source file */
962 const char *suffix = strrchr(arg, '.');
963 /* Ensure there is at least one char before the suffix */
964 if (suffix != NULL && suffix != arg) {
967 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
968 streq(suffix, "a") ? FILETYPE_OBJECT :
969 streq(suffix, "c") ? FILETYPE_C :
970 streq(suffix, "cc") ? FILETYPE_CXX :
971 streq(suffix, "cpp") ? FILETYPE_CXX :
972 streq(suffix, "cxx") ? FILETYPE_CXX :
973 streq(suffix, "h") ? FILETYPE_C :
974 streq(suffix, "o") ? FILETYPE_OBJECT :
975 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
976 streq(suffix, "so") ? FILETYPE_OBJECT :
981 if (type == FILETYPE_AUTODETECT) {
982 fprintf(stderr, "'%s': file format not recognized\n", arg);
987 file_list_entry_t *entry
988 = obstack_alloc(&file_obst, sizeof(entry[0]));
989 memset(entry, 0, sizeof(entry[0]));
993 if (last_file != NULL) {
994 last_file->next = entry;
1002 if (files == NULL) {
1003 fprintf(stderr, "error: no input files specified\n");
1004 argument_errors = true;
1007 if (help_displayed) {
1008 return !argument_errors;
1010 if (argument_errors) {
1015 /* we do the lowering in ast2firm */
1016 firm_opt.lower_bitfields = FALSE;
1019 init_symbol_table();
1028 if (construct_dep_target) {
1029 if (outname != 0 && strlen(outname) >= 2) {
1030 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1032 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1035 dep_target[0] = '\0';
1038 char outnamebuf[4096];
1039 if (outname == NULL) {
1040 const char *filename = files->name;
1043 case BenchmarkParser:
1048 case PreprocessOnly:
1053 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1054 outname = outnamebuf;
1056 case CompileAssemble:
1057 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1058 outname = outnamebuf;
1061 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1063 outname = outnamebuf;
1065 case CompileAssembleLink:
1075 assert(outname != NULL);
1078 if (streq(outname, "-")) {
1081 out = fopen(outname, "w");
1083 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1089 file_list_entry_t *file;
1090 for (file = files; file != NULL; file = file->next) {
1091 char asm_tempfile[1024];
1092 const char *filename = file->name;
1093 filetype_t filetype = file->type;
1095 if (filetype == FILETYPE_OBJECT)
1099 if (mode == LexTest) {
1101 in = open_file(filename);
1102 lextest(in, filename);
1107 FILE *preprocessed_in = NULL;
1109 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1110 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1111 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1113 /* no support for input on FILE* yet */
1115 panic("internal compiler error: in for preprocessor != NULL");
1117 preprocessed_in = preprocess(filename);
1118 if (mode == PreprocessOnly) {
1119 copy_file(out, preprocessed_in);
1120 int result = pclose(preprocessed_in);
1125 in = preprocessed_in;
1133 if(mode == Compile) {
1136 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1140 in = open_file(filename);
1142 /* preprocess and compile */
1143 if (filetype == FILETYPE_PREPROCESSED_C) {
1144 char const* invalid_mode;
1147 case STANDARD_C89: c_mode = _C89; break;
1148 /* TODO ^v determine difference between these two */
1149 case STANDARD_C90: c_mode = _C89; break;
1150 case STANDARD_C99: c_mode = _C89 | _C99; break;
1151 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1155 "warning: command line option \"-std=%s\" is not valid for C\n",
1158 case STANDARD_DEFAULT:
1159 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1161 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1162 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1165 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1166 char const* invalid_mode;
1168 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1169 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1170 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1171 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1172 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1175 case STANDARD_CXX98: c_mode = _CXX; break;
1179 "warning: command line option \"-std=%s\" is not valid for C++\n",
1181 case STANDARD_DEFAULT:
1182 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1186 c_mode |= features_on;
1187 c_mode &= ~features_off;
1189 translation_unit_t *const unit = do_parsing(in, filename);
1191 /* prints the AST even if errors occurred */
1192 if (mode == PrintAst) {
1193 type_set_output(out);
1194 ast_set_output(out);
1198 if(error_count > 0) {
1199 /* parsing failed because of errors */
1200 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1202 result = EXIT_FAILURE;
1204 } else if(warning_count > 0) {
1205 fprintf(stderr, "%u warning(s)\n", warning_count);
1208 if (in == preprocessed_in) {
1209 int pp_result = pclose(preprocessed_in);
1210 if (pp_result != EXIT_SUCCESS) {
1215 if(mode == BenchmarkParser) {
1217 } else if(mode == PrintFluffy) {
1218 write_fluffy_decls(out, unit);
1220 } else if (mode == PrintCaml) {
1221 write_caml_decls(out, unit);
1225 translation_unit_to_firm(unit);
1227 if (mode == ParseOnly) {
1231 if (mode == CompileDump) {
1233 ident *id = new_id_from_str(dumpfunction);
1234 ir_graph *irg = NULL;
1235 int n_irgs = get_irp_n_irgs();
1236 for(int i = 0; i < n_irgs; ++i) {
1237 ir_graph *tirg = get_irp_irg(i);
1238 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1246 fprintf(stderr, "No graph for function '%s' found\n",
1251 dump_ir_block_graph_file(irg, out);
1256 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1257 have_const_functions);
1258 if (asm_out != out) {
1261 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1262 copy_file(asm_out, in);
1263 if (in == preprocessed_in) {
1264 int pp_result = pclose(preprocessed_in);
1265 if (pp_result != EXIT_SUCCESS) {
1269 if(asm_out != out) {
1274 if (mode == Compile)
1277 /* if we're here then we have preprocessed assembly */
1278 filename = asm_tempfile;
1279 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1282 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1284 const char *filename_o;
1285 if(mode == CompileAssemble) {
1287 filename_o = outname;
1289 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1294 assemble(filename_o, filename);
1296 size_t len = strlen(filename_o) + 1;
1297 filename = obstack_copy(&file_obst, filename_o, len);
1298 filetype = FILETYPE_OBJECT;
1301 /* ok we're done here, process next file */
1302 file->name = filename;
1303 file->type = filetype;
1306 if (result != EXIT_SUCCESS)
1309 /* link program file */
1310 if(mode == CompileAssembleLink) {
1311 obstack_1grow(&ldflags_obst, '\0');
1312 const char *flags = obstack_finish(&ldflags_obst);
1314 /* construct commandline */
1315 obstack_printf(&file_obst, "%s", LINKER);
1316 for (file_list_entry_t *entry = files; entry != NULL;
1317 entry = entry->next) {
1318 if (entry->type != FILETYPE_OBJECT)
1321 add_flag(&file_obst, "%s", entry->name);
1324 add_flag(&file_obst, "-o");
1325 add_flag(&file_obst, outname);
1326 obstack_printf(&file_obst, "%s", flags);
1327 obstack_1grow(&file_obst, '\0');
1329 char *commandline = obstack_finish(&file_obst);
1334 int err = system(commandline);
1335 if(err != EXIT_SUCCESS) {
1336 fprintf(stderr, "linker reported an error\n");
1341 obstack_free(&cppflags_obst, NULL);
1342 obstack_free(&ldflags_obst, NULL);
1343 obstack_free(&file_obst, NULL);
1352 exit_symbol_table();