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, ...)
243 va_start(ap, format);
247 vsnprintf(buf, sizeof(buf), format, ap);
250 obstack_1grow(obst, ' ');
252 obstack_1grow(obst, '"');
253 obstack_grow(obst, buf, len);
254 obstack_1grow(obst, '"');
256 /* escape stuff... */
257 for (char *c = buf; *c != '\0'; ++c) {
272 obstack_1grow(obst, '\\');
275 obstack_1grow(obst, *c);
282 static const char *type_to_string(type_t *type)
284 assert(type->kind == TYPE_ATOMIC);
285 return get_atomic_kind_name(type->atomic.akind);
288 static FILE *preprocess(const char *fname)
290 obstack_1grow(&cppflags_obst, '\0');
291 const char *flags = obstack_finish(&cppflags_obst);
293 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
295 /* setup default defines */
296 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
297 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
298 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
299 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
301 /* handle dependency generation */
302 if (dep_target[0] != '\0') {
303 add_flag(&cppflags_obst, "-MF");
304 add_flag(&cppflags_obst, dep_target);
305 if (outname != NULL) {
306 add_flag(&cppflags_obst, "-MQ");
307 add_flag(&cppflags_obst, outname);
310 if (flags[0] != '\0') {
311 obstack_printf(&cppflags_obst, " %s", flags);
313 add_flag(&cppflags_obst, fname);
315 obstack_1grow(&cppflags_obst, '\0');
316 const char *buf = obstack_finish(&cppflags_obst);
321 FILE *f = popen(buf, "r");
323 fprintf(stderr, "invoking preprocessor failed\n");
330 static void assemble(const char *out, const char *in)
334 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
339 int err = system(buf);
341 fprintf(stderr, "assembler reported an error\n");
346 static const char *try_dir(const char *dir)
350 if (access(dir, R_OK | W_OK | X_OK) == 0)
355 static const char *get_tempdir(void)
357 static const char *tmpdir = NULL;
363 tmpdir = try_dir(getenv("TMPDIR"));
365 tmpdir = try_dir(getenv("TMP"));
367 tmpdir = try_dir(getenv("TEMP"));
371 tmpdir = try_dir(P_tmpdir);
375 tmpdir = try_dir("/var/tmp");
377 tmpdir = try_dir("/usr/tmp");
379 tmpdir = try_dir("/tmp");
388 /* cheap and nasty mkstemp replacement */
389 static int mkstemp(char *templ)
392 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
397 * an own version of tmpnam, which: writes in a buffer, emits no warnings
398 * during linking (like glibc/gnu ld do for tmpnam)...
400 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
402 const char *tempdir = get_tempdir();
404 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
406 int fd = mkstemp(buffer);
408 fprintf(stderr, "couldn't create temporary file: %s\n",
412 FILE *out = fdopen(fd, "w");
414 fprintf(stderr, "couldn't create temporary file FILE*\n");
418 file_list_entry_t *entry = xmalloc(sizeof(*entry));
419 memset(entry, 0, sizeof(*entry));
421 size_t name_len = strlen(buffer) + 1;
422 char *name = malloc(name_len);
423 memcpy(name, buffer, name_len);
426 entry->next = temp_files;
432 static void free_temp_files(void)
434 file_list_entry_t *entry = temp_files;
435 file_list_entry_t *next;
436 for ( ; entry != NULL; entry = next) {
440 free((char*) entry->name);
447 * Do the necessary lowering for compound parameters.
449 void lower_compound_params(void)
451 lower_params_t params;
453 params.def_ptr_alignment = 4;
454 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
455 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
456 params.find_pointer_type = NULL;
457 params.ret_compound_in_regs = NULL;
458 lower_calls_with_compounds(¶ms);
461 typedef enum compile_mode_t {
475 static void usage(const char *argv0)
477 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
480 static void print_cparser_version(void) {
482 firm_get_version(&ver);
484 printf("cparser (%s) using libFirm (%u.%u",
485 cparser_REVISION, ver.major, ver.minor);
486 if (ver.revision[0] != 0) {
488 fputs(ver.revision, stdout);
490 if (ver.build[0] != 0) {
492 fputs(ver.build, stdout);
497 static void set_be_option(const char *arg)
499 int res = firm_be_option(arg);
504 static void set_option(const char *arg)
506 int res = firm_option(arg);
511 static void copy_file(FILE *dest, FILE *input)
515 while (!feof(input) && !ferror(dest)) {
516 size_t read = fread(buf, 1, sizeof(buf), input);
517 if (fwrite(buf, 1, read, dest) != read) {
518 perror("couldn't write output");
523 static inline bool streq(char const* a, char const* b)
525 return strcmp(a, b) == 0;
528 static inline bool strstart(char const* str, char const* start)
533 } while (*str++ == *start++);
537 static FILE *open_file(const char *filename)
539 if (streq(filename, "-")) {
543 FILE *in = fopen(filename, "r");
545 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
553 static filetype_t get_filetype_from_string(const char *string)
555 if (streq(string, "c") || streq(string, "c-header"))
557 if (streq(string, "c++") || streq(string, "c++-header"))
559 if (streq(string, "assembler"))
560 return FILETYPE_PREPROCESSED_ASSEMBLER;
561 if (streq(string, "assembler-with-cpp"))
562 return FILETYPE_ASSEMBLER;
563 if (streq(string, "none"))
564 return FILETYPE_AUTODETECT;
566 return FILETYPE_UNKNOWN;
569 static void init_os_support(void)
571 /* OS option must be set to the backend */
572 switch (firm_opt.os_support) {
573 case OS_SUPPORT_MINGW:
574 set_be_option("ia32-gasmode=mingw");
575 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
577 case OS_SUPPORT_LINUX:
578 set_be_option("ia32-gasmode=elf");
580 case OS_SUPPORT_MACHO:
581 set_be_option("ia32-gasmode=macho");
582 set_be_option("ia32-stackalign=4");
583 set_be_option("pic");
588 typedef enum lang_standard_t {
589 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
590 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
591 STANDARD_C89, /* ISO C90 (sic) */
592 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
593 STANDARD_C99, /* ISO C99 */
594 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
595 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
596 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
597 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
600 int main(int argc, char **argv)
604 const char *dumpfunction = NULL;
605 compile_mode_t mode = CompileAssembleLink;
607 int result = EXIT_SUCCESS;
608 char cpu_arch[16] = "ia32";
609 file_list_entry_t *files = NULL;
610 file_list_entry_t *last_file = NULL;
611 bool construct_dep_target = false;
612 struct obstack file_obst;
614 atexit(free_temp_files);
616 /* hack for now... */
617 if (strstr(argv[0], "pptest") != NULL) {
618 extern int pptest_main(int argc, char **argv);
619 return pptest_main(argc, argv);
622 obstack_init(&cppflags_obst);
623 obstack_init(&ldflags_obst);
624 obstack_init(&file_obst);
626 #define GET_ARG_AFTER(def, args) \
627 def = &arg[sizeof(args)-1]; \
628 if (def[0] == '\0') { \
631 fprintf(stderr, "error: expected argument after '" args "'\n"); \
632 argument_errors = true; \
636 if (def[0] == '-' && def[1] != '\0') { \
637 fprintf(stderr, "error: expected argument after '" args "'\n"); \
638 argument_errors = true; \
643 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
645 /* early options parsing (find out optimisation level and OS) */
646 for (int i = 1; i < argc; ++i) {
647 const char *arg = argv[i];
651 const char *option = &arg[1];
652 if (option[0] == 'O') {
653 sscanf(&option[1], "%d", &opt_level);
655 if (strcmp(arg, "-fwin32") == 0) {
656 firm_opt.os_support = OS_SUPPORT_MINGW;
657 } else if (strcmp(arg, "-fmac") == 0) {
658 firm_opt.os_support = OS_SUPPORT_MACHO;
659 } else if (strcmp(arg, "-flinux") == 0) {
660 firm_opt.os_support = OS_SUPPORT_LINUX;
664 /* set target/os specific stuff */
667 /* apply optimisation level */
670 set_option("no-opt");
673 set_option("no-inline");
677 set_option("strict-aliasing");
678 /* use_builtins = true; */
681 set_option("cond-eval");
682 set_option("if-conv");
685 set_option("inline");
686 set_option("deconv");
687 set_be_option("omitfp");
691 /* parse rest of options */
692 lang_standard_t standard = STANDARD_DEFAULT;
693 unsigned features_on = 0;
694 unsigned features_off = 0;
695 filetype_t forced_filetype = FILETYPE_AUTODETECT;
696 bool help_displayed = false;
697 bool argument_errors = false;
698 for (int i = 1; i < argc; ++i) {
699 const char *arg = argv[i];
700 if (arg[0] == '-' && arg[1] != '\0') {
702 const char *option = &arg[1];
703 if (option[0] == 'o') {
704 GET_ARG_AFTER(outname, "-o");
705 } else if (option[0] == 'g') {
706 set_be_option("debuginfo=stabs");
707 set_be_option("omitfp=no");
708 set_be_option("ia32-nooptcc=yes");
709 } else if (SINGLE_OPTION('c')) {
710 mode = CompileAssemble;
711 } else if (SINGLE_OPTION('E')) {
712 mode = PreprocessOnly;
713 } else if (SINGLE_OPTION('S')) {
715 } else if (option[0] == 'O') {
717 } else if (option[0] == 'I') {
719 GET_ARG_AFTER(opt, "-I");
720 add_flag(&cppflags_obst, "-I%s", opt);
721 } else if (option[0] == 'D') {
723 GET_ARG_AFTER(opt, "-D");
724 add_flag(&cppflags_obst, "-D%s", opt);
725 } else if (option[0] == 'U') {
727 GET_ARG_AFTER(opt, "-U");
728 add_flag(&cppflags_obst, "-U%s", opt);
729 } else if (option[0] == 'l') {
731 GET_ARG_AFTER(opt, "-l");
732 add_flag(&ldflags_obst, "-l%s", opt);
733 } else if (option[0] == 'L') {
735 GET_ARG_AFTER(opt, "-L");
736 add_flag(&ldflags_obst, "-L%s", opt);
737 } else if (SINGLE_OPTION('v')) {
739 } else if (SINGLE_OPTION('w')) {
740 memset(&warning, 0, sizeof(warning));
741 } else if (option[0] == 'x') {
743 GET_ARG_AFTER(opt, "-x");
744 forced_filetype = get_filetype_from_string(opt);
745 if (forced_filetype == FILETYPE_UNKNOWN) {
746 fprintf(stderr, "Unknown language '%s'\n", opt);
747 argument_errors = true;
749 } else if (streq(option, "M")) {
750 mode = PreprocessOnly;
751 add_flag(&cppflags_obst, "-M");
752 } else if (streq(option, "MMD") ||
753 streq(option, "MD")) {
754 construct_dep_target = true;
755 add_flag(&cppflags_obst, "-%s", option);
756 } else if (streq(option, "MM") ||
757 streq(option, "MP")) {
758 add_flag(&cppflags_obst, "-%s", option);
759 } else if (streq(option, "MT") ||
760 streq(option, "MQ") ||
761 streq(option, "MF")) {
763 GET_ARG_AFTER(opt, "-MT");
764 add_flag(&cppflags_obst, "-%s", option);
765 add_flag(&cppflags_obst, "%s", opt);
766 } else if (streq(option, "pipe")) {
767 /* here for gcc compatibility */
768 } else if (option[0] == 'f') {
769 char const *orig_opt;
770 GET_ARG_AFTER(orig_opt, "-f");
772 if (strstart(orig_opt, "align-loops=") ||
773 strstart(orig_opt, "align-jumps=") ||
774 strstart(orig_opt, "align-functions=")) {
775 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
777 char const *opt = orig_opt;
778 bool truth_value = true;
779 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
784 if (streq(opt, "dollars-in-identifiers")) {
785 allow_dollar_in_symbol = truth_value;
786 } if (streq(opt, "builtins")) {
787 use_builtins = truth_value;
788 } else if (streq(opt, "short-wchar")) {
789 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
791 } else if (streq(opt, "syntax-only")) {
792 mode = truth_value ? ParseOnly : CompileAssembleLink;
793 } else if (streq(opt, "omit-frame-pointer")) {
794 set_be_option(truth_value ? "omitfp" : "omitfp=no");
795 } else if (streq(opt, "strength-reduce")) {
796 firm_option(truth_value ? "strength-red" : "no-strength-red");
797 } else if (streq(opt, "fast-math") ||
798 streq(opt, "jump-tables") ||
799 streq(opt, "unroll-loops") ||
800 streq(opt, "expensive-optimizations") ||
801 streq(opt, "common") ||
803 streq(opt, "align-loops") ||
804 streq(opt, "align-jumps") ||
805 streq(opt, "align-functions")) {
806 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
808 int res = firm_option(orig_opt);
810 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
812 argument_errors = true;
814 } else if (res == -1) {
815 help_displayed = true;
819 } else if (option[0] == 'b') {
821 GET_ARG_AFTER(opt, "-b");
822 int res = firm_be_option(opt);
824 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
826 argument_errors = true;
827 } else if (res == -1) {
828 help_displayed = true;
829 } else if (strstart(opt, "isa=")) {
830 strncpy(cpu_arch, opt, sizeof(cpu_arch));
832 } else if (option[0] == 'W') {
833 if (option[1] == 'p') {
834 // pass options directly to the preprocessor
836 GET_ARG_AFTER(opt, "-Wp,");
837 add_flag(&cppflags_obst, "-Wp,%s", opt);
838 } else if (strstart(option + 1, "l,")) {
839 // pass options directly to the linker
841 GET_ARG_AFTER(opt, "-Wl,");
842 add_flag(&ldflags_obst, "-Wl,%s", opt);
844 set_warning_opt(&option[1]);
846 } else if (option[0] == 'm') {
851 GET_ARG_AFTER(opt, "-m");
852 if (strstart(opt, "arch=")) {
853 GET_ARG_AFTER(opt, "-march=");
854 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
855 int res = firm_be_option(arch_opt);
857 argument_errors = true;
859 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
860 int res = firm_be_option(arch_opt);
862 argument_errors = true;
864 } else if (strstart(opt, "tune=")) {
865 GET_ARG_AFTER(opt, "-mtune=");
866 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
867 int res = firm_be_option(arch_opt);
869 argument_errors = true;
870 } else if (strstart(opt, "cpu=")) {
871 GET_ARG_AFTER(opt, "-mcpu=");
872 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
873 int res = firm_be_option(arch_opt);
875 argument_errors = true;
876 } else if (strstart(opt, "fpmath=")) {
877 GET_ARG_AFTER(opt, "-mfpmath=");
878 if (streq(opt, "387"))
880 else if (streq(opt, "sse"))
883 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
884 argument_errors = true;
886 if (!argument_errors) {
887 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
888 int res = firm_be_option(arch_opt);
890 argument_errors = true;
892 } else if (strstart(opt, "preferred-stack-boundary=")) {
893 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
894 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
895 int res = firm_be_option(arch_opt);
897 argument_errors = true;
898 } else if (streq(opt, "omit-leaf-frame-pointer")) {
899 set_be_option("omitleaffp=1");
900 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
901 set_be_option("omitleaffp=0");
904 long int value = strtol(opt, &endptr, 10);
905 if (*endptr != '\0') {
906 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
907 argument_errors = true;
909 if (value != 16 && value != 32 && value != 64) {
910 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
911 argument_errors = true;
913 machine_size = (unsigned int)value;
916 } else if (streq(option, "pg")) {
917 set_be_option("gprof");
918 add_flag(&ldflags_obst, "-pg");
919 } else if (streq(option, "pedantic") ||
920 streq(option, "ansi")) {
921 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
922 } else if (streq(option, "shared")) {
923 add_flag(&ldflags_obst, "-shared");
924 } else if (strstart(option, "std=")) {
925 const char *const o = &option[4];
927 streq(o, "c++") ? STANDARD_CXX98 :
928 streq(o, "c++98") ? STANDARD_CXX98 :
929 streq(o, "c89") ? STANDARD_C89 :
930 streq(o, "c99") ? STANDARD_C99 :
931 streq(o, "c9x") ? STANDARD_C99 : // deprecated
932 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
933 streq(o, "gnu89") ? STANDARD_GNU89 :
934 streq(o, "gnu99") ? STANDARD_GNU99 :
935 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
936 streq(o, "iso9899:1990") ? STANDARD_C89 :
937 streq(o, "iso9899:199409") ? STANDARD_C90 :
938 streq(o, "iso9899:1999") ? STANDARD_C99 :
939 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
940 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
941 } else if (streq(option, "version")) {
942 print_cparser_version();
943 } else if (option[0] == '-') {
944 /* double dash option */
946 if (streq(option, "gcc")) {
947 features_on |= _GNUC;
948 features_off &= ~_GNUC;
949 } else if (streq(option, "no-gcc")) {
950 features_on &= ~_GNUC;
951 features_off |= _GNUC;
952 } else if (streq(option, "ms")) {
954 features_off &= ~_MS;
955 } else if (streq(option, "no-ms")) {
958 } else if (streq(option, "signed-chars")) {
959 char_is_signed = true;
960 } else if (streq(option, "unsigned-chars")) {
961 char_is_signed = false;
962 } else if (streq(option, "strict")) {
964 } else if (streq(option, "lextest")) {
966 } else if (streq(option, "benchmark")) {
967 mode = BenchmarkParser;
968 } else if (streq(option, "print-ast")) {
970 } else if (streq(option, "print-implicit-cast")) {
971 print_implicit_casts = true;
972 } else if (streq(option, "print-parenthesis")) {
973 print_parenthesis = true;
974 } else if (streq(option, "print-fluffy")) {
976 } else if (streq(option, "print-caml")) {
978 } else if (streq(option, "version")) {
979 print_cparser_version();
981 } else if (streq(option, "dump-function")) {
984 fprintf(stderr, "error: "
985 "expected argument after '--dump-function'\n");
986 argument_errors = true;
989 dumpfunction = argv[i];
992 fprintf(stderr, "error: unknown argument '%s'\n", arg);
993 argument_errors = true;
996 fprintf(stderr, "error: unknown argument '%s'\n", arg);
997 argument_errors = true;
1000 filetype_t type = forced_filetype;
1001 if (type == FILETYPE_AUTODETECT) {
1002 if (streq(arg, "-")) {
1003 /* - implicitly means C source file */
1006 const char *suffix = strrchr(arg, '.');
1007 /* Ensure there is at least one char before the suffix */
1008 if (suffix != NULL && suffix != arg) {
1011 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1012 streq(suffix, "a") ? FILETYPE_OBJECT :
1013 streq(suffix, "c") ? FILETYPE_C :
1014 streq(suffix, "cc") ? FILETYPE_CXX :
1015 streq(suffix, "cpp") ? FILETYPE_CXX :
1016 streq(suffix, "cxx") ? FILETYPE_CXX :
1017 streq(suffix, "h") ? FILETYPE_C :
1018 streq(suffix, "o") ? FILETYPE_OBJECT :
1019 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1020 streq(suffix, "so") ? FILETYPE_OBJECT :
1021 FILETYPE_AUTODETECT;
1025 if (type == FILETYPE_AUTODETECT) {
1026 fprintf(stderr, "'%s': file format not recognized\n", arg);
1031 file_list_entry_t *entry
1032 = obstack_alloc(&file_obst, sizeof(entry[0]));
1033 memset(entry, 0, sizeof(entry[0]));
1037 if (last_file != NULL) {
1038 last_file->next = entry;
1046 if (files == NULL) {
1047 fprintf(stderr, "error: no input files specified\n");
1048 argument_errors = true;
1051 if (help_displayed) {
1052 return !argument_errors;
1054 if (argument_errors) {
1059 /* we do the lowering in ast2firm */
1060 firm_opt.lower_bitfields = FALSE;
1063 init_symbol_table();
1073 if (construct_dep_target) {
1074 if (outname != 0 && strlen(outname) >= 2) {
1075 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1077 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1080 dep_target[0] = '\0';
1083 char outnamebuf[4096];
1084 if (outname == NULL) {
1085 const char *filename = files->name;
1088 case BenchmarkParser:
1093 case PreprocessOnly:
1098 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1099 outname = outnamebuf;
1101 case CompileAssemble:
1102 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1103 outname = outnamebuf;
1106 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1108 outname = outnamebuf;
1110 case CompileAssembleLink:
1120 assert(outname != NULL);
1123 if (streq(outname, "-")) {
1126 out = fopen(outname, "w");
1128 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1134 file_list_entry_t *file;
1135 for (file = files; file != NULL; file = file->next) {
1136 char asm_tempfile[1024];
1137 const char *filename = file->name;
1138 filetype_t filetype = file->type;
1140 if (filetype == FILETYPE_OBJECT)
1144 if (mode == LexTest) {
1146 in = open_file(filename);
1147 lextest(in, filename);
1152 FILE *preprocessed_in = NULL;
1154 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1155 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1156 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1158 /* no support for input on FILE* yet */
1160 panic("internal compiler error: in for preprocessor != NULL");
1162 preprocessed_in = preprocess(filename);
1163 if (mode == PreprocessOnly) {
1164 copy_file(out, preprocessed_in);
1165 int result = pclose(preprocessed_in);
1170 in = preprocessed_in;
1178 if (mode == Compile) {
1181 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1185 in = open_file(filename);
1187 /* preprocess and compile */
1188 if (filetype == FILETYPE_PREPROCESSED_C) {
1189 char const* invalid_mode;
1192 case STANDARD_C89: c_mode = _C89; break;
1193 /* TODO ^v determine difference between these two */
1194 case STANDARD_C90: c_mode = _C89; break;
1195 case STANDARD_C99: c_mode = _C89 | _C99; break;
1196 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1200 "warning: command line option \"-std=%s\" is not valid for C\n",
1203 case STANDARD_DEFAULT:
1204 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1206 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1207 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1210 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1211 char const* invalid_mode;
1213 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1214 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1215 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1216 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1217 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1220 case STANDARD_CXX98: c_mode = _CXX; break;
1224 "warning: command line option \"-std=%s\" is not valid for C++\n",
1226 case STANDARD_DEFAULT:
1227 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1231 c_mode |= features_on;
1232 c_mode &= ~features_off;
1234 translation_unit_t *const unit = do_parsing(in, filename);
1236 /* prints the AST even if errors occurred */
1237 if (mode == PrintAst) {
1238 type_set_output(out);
1239 ast_set_output(out);
1243 if (error_count > 0) {
1244 /* parsing failed because of errors */
1245 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1247 result = EXIT_FAILURE;
1249 } else if (warning_count > 0) {
1250 fprintf(stderr, "%u warning(s)\n", warning_count);
1253 if (in == preprocessed_in) {
1254 int pp_result = pclose(preprocessed_in);
1255 if (pp_result != EXIT_SUCCESS) {
1260 if (mode == BenchmarkParser) {
1262 } else if (mode == PrintFluffy) {
1263 write_fluffy_decls(out, unit);
1265 } else if (mode == PrintCaml) {
1266 write_caml_decls(out, unit);
1270 translation_unit_to_firm(unit);
1272 if (mode == ParseOnly) {
1276 if (mode == CompileDump) {
1278 ident *id = new_id_from_str(dumpfunction);
1279 ir_graph *irg = NULL;
1280 int n_irgs = get_irp_n_irgs();
1281 for (int i = 0; i < n_irgs; ++i) {
1282 ir_graph *tirg = get_irp_irg(i);
1283 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1291 fprintf(stderr, "No graph for function '%s' found\n",
1296 dump_ir_block_graph_file(irg, out);
1301 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1302 have_const_functions);
1303 if (asm_out != out) {
1306 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1307 copy_file(asm_out, in);
1308 if (in == preprocessed_in) {
1309 int pp_result = pclose(preprocessed_in);
1310 if (pp_result != EXIT_SUCCESS) {
1314 if (asm_out != out) {
1319 if (mode == Compile)
1322 /* if we're here then we have preprocessed assembly */
1323 filename = asm_tempfile;
1324 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1327 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1329 const char *filename_o;
1330 if (mode == CompileAssemble) {
1332 filename_o = outname;
1334 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1339 assemble(filename_o, filename);
1341 size_t len = strlen(filename_o) + 1;
1342 filename = obstack_copy(&file_obst, filename_o, len);
1343 filetype = FILETYPE_OBJECT;
1346 /* ok we're done here, process next file */
1347 file->name = filename;
1348 file->type = filetype;
1351 if (result != EXIT_SUCCESS)
1354 /* link program file */
1355 if (mode == CompileAssembleLink) {
1356 obstack_1grow(&ldflags_obst, '\0');
1357 const char *flags = obstack_finish(&ldflags_obst);
1359 /* construct commandline */
1360 obstack_printf(&file_obst, "%s", LINKER);
1361 for (file_list_entry_t *entry = files; entry != NULL;
1362 entry = entry->next) {
1363 if (entry->type != FILETYPE_OBJECT)
1366 add_flag(&file_obst, "%s", entry->name);
1369 add_flag(&file_obst, "-o");
1370 add_flag(&file_obst, outname);
1371 obstack_printf(&file_obst, "%s", flags);
1372 obstack_1grow(&file_obst, '\0');
1374 char *commandline = obstack_finish(&file_obst);
1379 int err = system(commandline);
1380 if (err != EXIT_SUCCESS) {
1381 fprintf(stderr, "linker reported an error\n");
1386 obstack_free(&cppflags_obst, NULL);
1387 obstack_free(&ldflags_obst, NULL);
1388 obstack_free(&file_obst, NULL);
1398 exit_symbol_table();