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)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "adt/error.h"
75 #include "write_fluffy.h"
76 #include "write_caml.h"
83 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
85 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
90 #define LINKER "gcc -m32"
95 #define ASSEMBLER "gcc -c -xassembler"
97 #define ASSEMBLER "as --32"
101 /** The current c mode/dialect. */
102 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
104 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
105 unsigned int machine_size = 32;
107 /** true if the char type is signed. */
108 bool char_is_signed = true;
110 /** true for strict language checking. */
111 bool strict_mode = false;
113 /** use builtins for some libc functions */
114 bool use_builtins = false;
116 /** we have extern function with const attribute. */
117 bool have_const_functions = false;
119 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
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 static file_list_entry_t *temp_files;
154 #if defined(_DEBUG) || defined(FIRM_DEBUG)
156 * Debug printf implementation.
158 * @param fmt printf style format parameter
160 void dbg_printf(const char *fmt, ...)
164 if (firm_dump.debug_print) {
170 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
172 static void initialize_firm(void)
176 dump_consts_local(1);
177 dump_keepalive_edges(1);
180 static void get_output_name(char *buf, size_t buflen, const char *inputname,
183 size_t last_dot = 0xffffffff;
186 if(inputname == NULL) {
187 snprintf(buf, buflen, "a%s", newext);
191 for(const char *c = inputname; *c != 0; ++c) {
196 if(last_dot == 0xffffffff)
199 if(last_dot >= buflen)
200 panic("filename too long");
201 memcpy(buf, inputname, last_dot);
203 size_t extlen = strlen(newext) + 1;
204 if(extlen + last_dot >= buflen)
205 panic("filename too long");
206 memcpy(buf+last_dot, newext, extlen);
209 #include "builtins.h"
211 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
216 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
220 lexer_open_stream(in, input_name);
223 translation_unit_t *unit = finish_parsing();
227 static void lextest(FILE *in, const char *fname)
229 lexer_open_stream(in, fname);
232 lexer_next_preprocessing_token();
233 print_token(stdout, &lexer_token);
235 } while(lexer_token.type != T_EOF);
238 static void add_flag(struct obstack *obst, const char *format, ...)
241 va_start(ap, format);
244 vsnprintf(buf, sizeof(buf), format, ap);
246 /* escape stuff... */
247 obstack_1grow(obst, ' ');
248 for (char *c = buf; *c != '\0'; ++c) {
261 obstack_1grow(obst, '\\');
264 obstack_1grow(obst, *c);
272 static const char *type_to_string(type_t *type)
274 assert(type->kind == TYPE_ATOMIC);
275 return get_atomic_kind_name(type->atomic.akind);
278 static FILE *preprocess(const char *fname)
280 obstack_1grow(&cppflags_obst, '\0');
281 const char *flags = obstack_finish(&cppflags_obst);
283 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
285 /* setup default defines */
286 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
287 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s",
288 type_to_string(type_wchar_t));
289 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
290 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
292 /* handle dependency generation */
293 if (dep_target[0] != '\0') {
294 add_flag(&cppflags_obst, "-MF");
295 add_flag(&cppflags_obst, "%s", dep_target);
296 if (outname != NULL) {
297 add_flag(&cppflags_obst, "-MQ");
298 add_flag(&cppflags_obst, "%s", outname);
301 if (flags[0] != '\0') {
302 obstack_printf(&cppflags_obst, " %s", flags);
304 add_flag(&cppflags_obst, "%s", fname);
306 obstack_1grow(&cppflags_obst, '\0');
307 const char *buf = obstack_finish(&cppflags_obst);
312 FILE *f = popen(buf, "r");
314 fprintf(stderr, "invoking preprocessor failed\n");
321 static void assemble(const char *out, const char *in)
325 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
330 int err = system(buf);
332 fprintf(stderr, "assembler reported an error\n");
337 static const char *try_dir(const char *dir)
341 if(access(dir, R_OK | W_OK | X_OK) == 0)
346 static const char *get_tempdir(void)
348 static const char *tmpdir = NULL;
354 tmpdir = try_dir(getenv("TMPDIR"));
356 tmpdir = try_dir(getenv("TMP"));
358 tmpdir = try_dir(getenv("TEMP"));
362 tmpdir = try_dir(P_tmpdir);
366 tmpdir = try_dir("/var/tmp");
368 tmpdir = try_dir("/usr/tmp");
370 tmpdir = try_dir("/tmp");
379 /* cheap and nasty mkstemp replacement */
380 static int mkstemp(char *templ)
383 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
388 * an own version of tmpnam, which: writes in a buffer, emits no warnings
389 * during linking (like glibc/gnu ld do for tmpnam)...
391 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
393 const char *tempdir = get_tempdir();
395 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
397 int fd = mkstemp(buffer);
399 fprintf(stderr, "couldn't create temporary file: %s\n",
403 FILE *out = fdopen(fd, "w");
405 fprintf(stderr, "couldn't create temporary file FILE*\n");
409 file_list_entry_t *entry = xmalloc(sizeof(*entry));
410 memset(entry, 0, sizeof(*entry));
412 size_t name_len = strlen(buffer) + 1;
413 char *name = malloc(name_len);
414 memcpy(name, buffer, name_len);
417 entry->next = temp_files;
423 static void free_temp_files(void)
425 file_list_entry_t *entry = temp_files;
426 file_list_entry_t *next;
427 for ( ; entry != NULL; entry = next) {
431 free((char*) entry->name);
438 * Do the necessary lowering for compound parameters.
440 void lower_compound_params(void)
442 lower_params_t params;
444 params.def_ptr_alignment = 4;
445 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
446 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
447 params.find_pointer_type = NULL;
448 params.ret_compound_in_regs = NULL;
449 lower_calls_with_compounds(¶ms);
452 typedef enum compile_mode_t {
466 static void usage(const char *argv0)
468 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
471 static void print_cparser_version(void) {
473 firm_get_version(&ver);
475 printf("cparser (%s) using libFirm (%u.%u",
476 cparser_REVISION, ver.major, ver.minor);
477 if(ver.revision[0] != 0) {
479 fputs(ver.revision, stdout);
481 if(ver.build[0] != 0) {
483 fputs(ver.build, stdout);
488 static void set_be_option(const char *arg)
490 int res = firm_be_option(arg);
495 static void set_option(const char *arg)
497 int res = firm_option(arg);
502 static void copy_file(FILE *dest, FILE *input)
506 while (!feof(input) && !ferror(dest)) {
507 size_t read = fread(buf, 1, sizeof(buf), input);
508 if(fwrite(buf, 1, read, dest) != read) {
509 perror("couldn't write output");
514 static inline bool streq(char const* a, char const* b)
516 return strcmp(a, b) == 0;
519 static inline bool strstart(char const* str, char const* start)
524 } while (*str++ == *start++);
528 static FILE *open_file(const char *filename)
530 if (streq(filename, "-")) {
534 FILE *in = fopen(filename, "r");
536 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
544 static filetype_t get_filetype_from_string(const char *string)
546 if (streq(string, "c") || streq(string, "c-header"))
548 if (streq(string, "c++") || streq(string, "c++-header"))
550 if (streq(string, "assembler"))
551 return FILETYPE_PREPROCESSED_ASSEMBLER;
552 if (streq(string, "assembler-with-cpp"))
553 return FILETYPE_ASSEMBLER;
554 if (streq(string, "none"))
555 return FILETYPE_AUTODETECT;
557 return FILETYPE_UNKNOWN;
560 static void init_os_support(void)
562 /* OS option must be set to the backend */
563 switch (firm_opt.os_support) {
564 case OS_SUPPORT_MINGW:
565 set_be_option("ia32-gasmode=mingw");
566 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
568 case OS_SUPPORT_LINUX:
569 set_be_option("ia32-gasmode=elf");
571 case OS_SUPPORT_MACHO:
572 set_be_option("ia32-gasmode=macho");
573 set_be_option("ia32-stackalign=4");
574 set_be_option("pic");
579 typedef enum lang_standard_t {
580 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
581 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
582 STANDARD_C89, /* ISO C90 (sic) */
583 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
584 STANDARD_C99, /* ISO C99 */
585 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
586 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
587 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
588 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
591 int main(int argc, char **argv)
595 const char *dumpfunction = NULL;
596 compile_mode_t mode = CompileAssembleLink;
598 int result = EXIT_SUCCESS;
599 char cpu_arch[16] = "ia32";
600 file_list_entry_t *files = NULL;
601 file_list_entry_t *last_file = NULL;
602 bool construct_dep_target = false;
603 struct obstack file_obst;
605 atexit(free_temp_files);
607 /* hack for now... */
608 if (strstr(argv[0], "pptest") != NULL) {
609 extern int pptest_main(int argc, char **argv);
610 return pptest_main(argc, argv);
613 obstack_init(&cppflags_obst);
614 obstack_init(&ldflags_obst);
615 obstack_init(&file_obst);
617 #define GET_ARG_AFTER(def, args) \
618 def = &arg[sizeof(args)-1]; \
619 if(def[0] == '\0') { \
622 fprintf(stderr, "error: expected argument after '" args "'\n"); \
623 argument_errors = true; \
627 if(def[0] == '-' && def[1] != '\0') { \
628 fprintf(stderr, "error: expected argument after '" args "'\n"); \
629 argument_errors = true; \
634 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
636 /* early options parsing (find out optimisation level and OS) */
637 for (int i = 1; i < argc; ++i) {
638 const char *arg = argv[i];
642 const char *option = &arg[1];
643 if (option[0] == 'O') {
644 sscanf(&option[1], "%d", &opt_level);
646 if (strcmp(arg, "-fwin32") == 0) {
647 firm_opt.os_support = OS_SUPPORT_MINGW;
648 } else if (strcmp(arg, "-fmac") == 0) {
649 firm_opt.os_support = OS_SUPPORT_MACHO;
650 } else if (strcmp(arg, "-flinux") == 0) {
651 firm_opt.os_support = OS_SUPPORT_LINUX;
655 /* set target/os specific stuff */
658 /* apply optimisation level */
661 set_option("no-opt");
664 set_option("no-inline");
668 set_option("strict-aliasing");
669 /* use_builtins = true; */
672 set_option("cond-eval");
673 set_option("if-conv");
676 set_option("inline");
677 set_option("deconv");
678 set_be_option("omitfp");
682 /* parse rest of options */
683 lang_standard_t standard = STANDARD_DEFAULT;
684 unsigned features_on = 0;
685 unsigned features_off = 0;
686 filetype_t forced_filetype = FILETYPE_AUTODETECT;
687 bool help_displayed = false;
688 bool argument_errors = false;
689 for(int i = 1; i < argc; ++i) {
690 const char *arg = argv[i];
691 if (arg[0] == '-' && arg[1] != '\0') {
693 const char *option = &arg[1];
694 if(option[0] == 'o') {
695 GET_ARG_AFTER(outname, "-o");
696 } else if(option[0] == 'g') {
697 set_be_option("debuginfo=stabs");
698 set_be_option("omitfp=no");
699 set_be_option("ia32-nooptcc=yes");
700 } else if(SINGLE_OPTION('c')) {
701 mode = CompileAssemble;
702 } else if(SINGLE_OPTION('E')) {
703 mode = PreprocessOnly;
704 } else if(SINGLE_OPTION('S')) {
706 } else if(option[0] == 'O') {
708 } else if(option[0] == 'I') {
710 GET_ARG_AFTER(opt, "-I");
711 add_flag(&cppflags_obst, "-I%s", opt);
712 } else if(option[0] == 'D') {
714 GET_ARG_AFTER(opt, "-D");
715 add_flag(&cppflags_obst, "-D%s", opt);
716 } else if(option[0] == 'U') {
718 GET_ARG_AFTER(opt, "-U");
719 add_flag(&cppflags_obst, "-U%s", opt);
720 } else if(option[0] == 'l') {
722 GET_ARG_AFTER(opt, "-l");
723 add_flag(&ldflags_obst, "-l%s", opt);
724 } else if(option[0] == 'L') {
726 GET_ARG_AFTER(opt, "-L");
727 add_flag(&ldflags_obst, "-L%s", opt);
728 } else if(SINGLE_OPTION('v')) {
730 } else if(SINGLE_OPTION('w')) {
731 memset(&warning, 0, sizeof(warning));
732 } else if(option[0] == 'x') {
734 GET_ARG_AFTER(opt, "-x");
735 forced_filetype = get_filetype_from_string(opt);
736 if (forced_filetype == FILETYPE_UNKNOWN) {
737 fprintf(stderr, "Unknown language '%s'\n", opt);
738 argument_errors = true;
740 } else if (streq(option, "M")) {
741 mode = PreprocessOnly;
742 add_flag(&cppflags_obst, "-M");
743 } else if (streq(option, "MMD") ||
744 streq(option, "MD")) {
745 construct_dep_target = true;
746 add_flag(&cppflags_obst, "-%s", option);
747 } else if(streq(option, "MM") ||
748 streq(option, "MP")) {
749 add_flag(&cppflags_obst, "-%s", option);
750 } else if (streq(option, "MT") ||
751 streq(option, "MQ") ||
752 streq(option, "MF")) {
754 GET_ARG_AFTER(opt, "-MT");
755 add_flag(&cppflags_obst, "-%s", option);
756 add_flag(&cppflags_obst, "%s", opt);
757 } else if (streq(option, "pipe")) {
758 /* here for gcc compatibility */
759 } else if (option[0] == 'f') {
760 char const *orig_opt;
761 GET_ARG_AFTER(orig_opt, "-f");
763 if (strstart(orig_opt, "align-loops=") ||
764 strstart(orig_opt, "align-jumps=") ||
765 strstart(orig_opt, "align-functions=")) {
766 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
768 char const *opt = orig_opt;
769 bool truth_value = true;
770 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
775 if (streq(opt, "dollars-in-identifiers")) {
776 allow_dollar_in_symbol = truth_value;
777 } if (streq(opt, "builtins")) {
778 use_builtins = truth_value;
779 } else if (streq(opt, "short-wchar")) {
780 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
782 } else if (streq(opt, "syntax-only")) {
783 mode = truth_value ? ParseOnly : CompileAssembleLink;
784 } else if (streq(opt, "omit-frame-pointer")) {
785 set_be_option(truth_value ? "omitfp" : "omitfp=no");
786 } else if (streq(opt, "strength-reduce")) {
787 firm_option(truth_value ? "strength-red" : "no-strength-red");
788 } else if (streq(opt, "fast-math") ||
789 streq(opt, "jump-tables") ||
790 streq(opt, "unroll-loops") ||
791 streq(opt, "expensive-optimizations") ||
792 streq(opt, "common") ||
794 streq(opt, "align-loops") ||
795 streq(opt, "align-jumps") ||
796 streq(opt, "align-functions")) {
797 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
799 int res = firm_option(orig_opt);
801 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
803 argument_errors = true;
805 } else if (res == -1) {
806 help_displayed = true;
810 } else if (option[0] == 'b') {
812 GET_ARG_AFTER(opt, "-b");
813 int res = firm_be_option(opt);
815 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
817 argument_errors = true;
818 } else if (res == -1) {
819 help_displayed = true;
820 } else if (strstart(opt, "isa=")) {
821 strncpy(cpu_arch, opt, sizeof(cpu_arch));
823 } else if (option[0] == 'W') {
824 if (strstart(option + 1, "l,")) // a gcc-style linker option
827 GET_ARG_AFTER(opt, "-Wl,");
828 add_flag(&ldflags_obst, "-Wl,%s", opt);
830 else set_warning_opt(&option[1]);
831 } else if(option[0] == 'm') {
836 GET_ARG_AFTER(opt, "-m");
837 if (strstart(opt, "arch=")) {
838 GET_ARG_AFTER(opt, "-march=");
839 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
840 int res = firm_be_option(arch_opt);
842 argument_errors = true;
844 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
845 int res = firm_be_option(arch_opt);
847 argument_errors = true;
849 } else if (strstart(opt, "tune=")) {
850 GET_ARG_AFTER(opt, "-mtune=");
851 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
852 int res = firm_be_option(arch_opt);
854 argument_errors = true;
855 } else if (strstart(opt, "cpu=")) {
856 GET_ARG_AFTER(opt, "-mcpu=");
857 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
858 int res = firm_be_option(arch_opt);
860 argument_errors = true;
861 } else if (strstart(opt, "fpmath=")) {
862 GET_ARG_AFTER(opt, "-mfpmath=");
863 if (streq(opt, "387"))
865 else if (streq(opt, "sse"))
868 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
869 argument_errors = true;
871 if(!argument_errors) {
872 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
873 int res = firm_be_option(arch_opt);
875 argument_errors = true;
877 } else if (strstart(opt, "preferred-stack-boundary=")) {
878 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
879 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
880 int res = firm_be_option(arch_opt);
882 argument_errors = true;
883 } else if (streq(opt, "omit-leaf-frame-pointer")) {
884 set_be_option("omitleaffp=1");
885 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
886 set_be_option("omitleaffp=0");
889 long int value = strtol(opt, &endptr, 10);
890 if (*endptr != '\0') {
891 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
892 argument_errors = true;
894 if (value != 16 && value != 32 && value != 64) {
895 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
896 argument_errors = true;
898 machine_size = (unsigned int)value;
901 } else if (streq(option, "pg")) {
902 set_be_option("gprof");
903 add_flag(&ldflags_obst, "-pg");
904 } else if (streq(option, "pedantic") ||
905 streq(option, "ansi")) {
906 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
907 } else if (streq(option, "shared")) {
908 add_flag(&ldflags_obst, "-shared");
909 } else if (strstart(option, "std=")) {
910 const char *const o = &option[4];
912 streq(o, "c++") ? STANDARD_CXX98 :
913 streq(o, "c++98") ? STANDARD_CXX98 :
914 streq(o, "c89") ? STANDARD_C89 :
915 streq(o, "c99") ? STANDARD_C99 :
916 streq(o, "c9x") ? STANDARD_C99 : // deprecated
917 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
918 streq(o, "gnu89") ? STANDARD_GNU89 :
919 streq(o, "gnu99") ? STANDARD_GNU99 :
920 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
921 streq(o, "iso9899:1990") ? STANDARD_C89 :
922 streq(o, "iso9899:199409") ? STANDARD_C90 :
923 streq(o, "iso9899:1999") ? STANDARD_C99 :
924 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
925 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
926 } else if (streq(option, "version")) {
927 print_cparser_version();
928 } else if (option[0] == '-') {
929 /* double dash option */
931 if (streq(option, "gcc")) {
932 features_on |= _GNUC;
933 features_off &= ~_GNUC;
934 } else if (streq(option, "no-gcc")) {
935 features_on &= ~_GNUC;
936 features_off |= _GNUC;
937 } else if (streq(option, "ms")) {
939 features_off &= ~_MS;
940 } else if (streq(option, "no-ms")) {
943 } else if (streq(option, "signed-chars")) {
944 char_is_signed = true;
945 } else if (streq(option, "unsigned-chars")) {
946 char_is_signed = false;
947 } else if (streq(option, "strict")) {
949 } else if (streq(option, "lextest")) {
951 } else if (streq(option, "benchmark")) {
952 mode = BenchmarkParser;
953 } else if (streq(option, "print-ast")) {
955 } else if (streq(option, "print-implicit-cast")) {
956 print_implicit_casts = true;
957 } else if (streq(option, "print-parenthesis")) {
958 print_parenthesis = true;
959 } else if (streq(option, "print-fluffy")) {
961 } else if (streq(option, "print-caml")) {
963 } else if (streq(option, "version")) {
964 print_cparser_version();
966 } else if (streq(option, "dump-function")) {
969 fprintf(stderr, "error: "
970 "expected argument after '--dump-function'\n");
971 argument_errors = true;
974 dumpfunction = argv[i];
977 fprintf(stderr, "error: unknown argument '%s'\n", arg);
978 argument_errors = true;
981 fprintf(stderr, "error: unknown argument '%s'\n", arg);
982 argument_errors = true;
985 filetype_t type = forced_filetype;
986 if (type == FILETYPE_AUTODETECT) {
987 if (streq(arg, "-")) {
988 /* - implicitly means C source file */
991 const char *suffix = strrchr(arg, '.');
992 /* Ensure there is at least one char before the suffix */
993 if (suffix != NULL && suffix != arg) {
996 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
997 streq(suffix, "a") ? FILETYPE_OBJECT :
998 streq(suffix, "c") ? FILETYPE_C :
999 streq(suffix, "cc") ? FILETYPE_CXX :
1000 streq(suffix, "cpp") ? FILETYPE_CXX :
1001 streq(suffix, "cxx") ? FILETYPE_CXX :
1002 streq(suffix, "h") ? FILETYPE_C :
1003 streq(suffix, "o") ? FILETYPE_OBJECT :
1004 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1005 streq(suffix, "so") ? FILETYPE_OBJECT :
1006 FILETYPE_AUTODETECT;
1010 if (type == FILETYPE_AUTODETECT) {
1011 fprintf(stderr, "'%s': file format not recognized\n", arg);
1016 file_list_entry_t *entry
1017 = obstack_alloc(&file_obst, sizeof(entry[0]));
1018 memset(entry, 0, sizeof(entry[0]));
1022 if (last_file != NULL) {
1023 last_file->next = entry;
1031 if (files == NULL) {
1032 fprintf(stderr, "error: no input files specified\n");
1033 argument_errors = true;
1036 if (help_displayed) {
1037 return !argument_errors;
1039 if (argument_errors) {
1044 /* we do the lowering in ast2firm */
1045 firm_opt.lower_bitfields = FALSE;
1048 init_symbol_table();
1058 if (construct_dep_target) {
1059 if (outname != 0 && strlen(outname) >= 2) {
1060 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1062 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1065 dep_target[0] = '\0';
1068 char outnamebuf[4096];
1069 if (outname == NULL) {
1070 const char *filename = files->name;
1073 case BenchmarkParser:
1078 case PreprocessOnly:
1083 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1084 outname = outnamebuf;
1086 case CompileAssemble:
1087 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1088 outname = outnamebuf;
1091 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1093 outname = outnamebuf;
1095 case CompileAssembleLink:
1105 assert(outname != NULL);
1108 if (streq(outname, "-")) {
1111 out = fopen(outname, "w");
1113 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1119 file_list_entry_t *file;
1120 for (file = files; file != NULL; file = file->next) {
1121 char asm_tempfile[1024];
1122 const char *filename = file->name;
1123 filetype_t filetype = file->type;
1125 if (filetype == FILETYPE_OBJECT)
1129 if (mode == LexTest) {
1131 in = open_file(filename);
1132 lextest(in, filename);
1137 FILE *preprocessed_in = NULL;
1139 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1140 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1141 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1143 /* no support for input on FILE* yet */
1145 panic("internal compiler error: in for preprocessor != NULL");
1147 preprocessed_in = preprocess(filename);
1148 if (mode == PreprocessOnly) {
1149 copy_file(out, preprocessed_in);
1150 int result = pclose(preprocessed_in);
1155 in = preprocessed_in;
1163 if(mode == Compile) {
1166 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1170 in = open_file(filename);
1172 /* preprocess and compile */
1173 if (filetype == FILETYPE_PREPROCESSED_C) {
1174 char const* invalid_mode;
1177 case STANDARD_C89: c_mode = _C89; break;
1178 /* TODO ^v determine difference between these two */
1179 case STANDARD_C90: c_mode = _C89; break;
1180 case STANDARD_C99: c_mode = _C89 | _C99; break;
1181 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1185 "warning: command line option \"-std=%s\" is not valid for C\n",
1188 case STANDARD_DEFAULT:
1189 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1191 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1192 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1195 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1196 char const* invalid_mode;
1198 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1199 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1200 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1201 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1202 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1205 case STANDARD_CXX98: c_mode = _CXX; break;
1209 "warning: command line option \"-std=%s\" is not valid for C++\n",
1211 case STANDARD_DEFAULT:
1212 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1216 c_mode |= features_on;
1217 c_mode &= ~features_off;
1219 translation_unit_t *const unit = do_parsing(in, filename);
1221 /* prints the AST even if errors occurred */
1222 if (mode == PrintAst) {
1223 type_set_output(out);
1224 ast_set_output(out);
1228 if(error_count > 0) {
1229 /* parsing failed because of errors */
1230 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1232 result = EXIT_FAILURE;
1234 } else if(warning_count > 0) {
1235 fprintf(stderr, "%u warning(s)\n", warning_count);
1238 if (in == preprocessed_in) {
1239 int pp_result = pclose(preprocessed_in);
1240 if (pp_result != EXIT_SUCCESS) {
1245 if(mode == BenchmarkParser) {
1247 } else if(mode == PrintFluffy) {
1248 write_fluffy_decls(out, unit);
1250 } else if (mode == PrintCaml) {
1251 write_caml_decls(out, unit);
1255 translation_unit_to_firm(unit);
1257 if (mode == ParseOnly) {
1261 if (mode == CompileDump) {
1263 ident *id = new_id_from_str(dumpfunction);
1264 ir_graph *irg = NULL;
1265 int n_irgs = get_irp_n_irgs();
1266 for(int i = 0; i < n_irgs; ++i) {
1267 ir_graph *tirg = get_irp_irg(i);
1268 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1276 fprintf(stderr, "No graph for function '%s' found\n",
1281 dump_ir_block_graph_file(irg, out);
1286 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1287 have_const_functions);
1288 if (asm_out != out) {
1291 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1292 copy_file(asm_out, in);
1293 if (in == preprocessed_in) {
1294 int pp_result = pclose(preprocessed_in);
1295 if (pp_result != EXIT_SUCCESS) {
1299 if(asm_out != out) {
1304 if (mode == Compile)
1307 /* if we're here then we have preprocessed assembly */
1308 filename = asm_tempfile;
1309 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1312 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1314 const char *filename_o;
1315 if(mode == CompileAssemble) {
1317 filename_o = outname;
1319 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1324 assemble(filename_o, filename);
1326 size_t len = strlen(filename_o) + 1;
1327 filename = obstack_copy(&file_obst, filename_o, len);
1328 filetype = FILETYPE_OBJECT;
1331 /* ok we're done here, process next file */
1332 file->name = filename;
1333 file->type = filetype;
1336 if (result != EXIT_SUCCESS)
1339 /* link program file */
1340 if(mode == CompileAssembleLink) {
1341 obstack_1grow(&ldflags_obst, '\0');
1342 const char *flags = obstack_finish(&ldflags_obst);
1344 /* construct commandline */
1345 obstack_printf(&file_obst, "%s", LINKER);
1346 for (file_list_entry_t *entry = files; entry != NULL;
1347 entry = entry->next) {
1348 if (entry->type != FILETYPE_OBJECT)
1351 add_flag(&file_obst, "%s", entry->name);
1354 add_flag(&file_obst, "-o");
1355 add_flag(&file_obst, outname);
1356 obstack_printf(&file_obst, "%s", flags);
1357 obstack_1grow(&file_obst, '\0');
1359 char *commandline = obstack_finish(&file_obst);
1364 int err = system(commandline);
1365 if(err != EXIT_SUCCESS) {
1366 fprintf(stderr, "linker reported an error\n");
1371 obstack_free(&cppflags_obst, NULL);
1372 obstack_free(&ldflags_obst, NULL);
1373 obstack_free(&file_obst, NULL);
1383 exit_symbol_table();