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) {
270 obstack_1grow(obst, '\\');
273 obstack_1grow(obst, *c);
280 static const char *type_to_string(type_t *type)
282 assert(type->kind == TYPE_ATOMIC);
283 return get_atomic_kind_name(type->atomic.akind);
286 static FILE *preprocess(const char *fname)
288 obstack_1grow(&cppflags_obst, '\0');
289 const char *flags = obstack_finish(&cppflags_obst);
291 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
293 /* setup default defines */
294 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
295 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
296 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
297 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
299 /* handle dependency generation */
300 if (dep_target[0] != '\0') {
301 add_flag(&cppflags_obst, "-MF");
302 add_flag(&cppflags_obst, dep_target);
303 if (outname != NULL) {
304 add_flag(&cppflags_obst, "-MQ");
305 add_flag(&cppflags_obst, outname);
308 if (flags[0] != '\0') {
309 obstack_printf(&cppflags_obst, " %s", flags);
311 add_flag(&cppflags_obst, fname);
313 obstack_1grow(&cppflags_obst, '\0');
314 const char *buf = obstack_finish(&cppflags_obst);
319 FILE *f = popen(buf, "r");
321 fprintf(stderr, "invoking preprocessor failed\n");
328 static void assemble(const char *out, const char *in)
332 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
337 int err = system(buf);
339 fprintf(stderr, "assembler reported an error\n");
344 static const char *try_dir(const char *dir)
348 if (access(dir, R_OK | W_OK | X_OK) == 0)
353 static const char *get_tempdir(void)
355 static const char *tmpdir = NULL;
361 tmpdir = try_dir(getenv("TMPDIR"));
363 tmpdir = try_dir(getenv("TMP"));
365 tmpdir = try_dir(getenv("TEMP"));
369 tmpdir = try_dir(P_tmpdir);
373 tmpdir = try_dir("/var/tmp");
375 tmpdir = try_dir("/usr/tmp");
377 tmpdir = try_dir("/tmp");
386 /* cheap and nasty mkstemp replacement */
387 static int mkstemp(char *templ)
390 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
395 * an own version of tmpnam, which: writes in a buffer, emits no warnings
396 * during linking (like glibc/gnu ld do for tmpnam)...
398 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
400 const char *tempdir = get_tempdir();
402 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
404 int fd = mkstemp(buffer);
406 fprintf(stderr, "couldn't create temporary file: %s\n",
410 FILE *out = fdopen(fd, "w");
412 fprintf(stderr, "couldn't create temporary file FILE*\n");
416 file_list_entry_t *entry = xmalloc(sizeof(*entry));
417 memset(entry, 0, sizeof(*entry));
419 size_t name_len = strlen(buffer) + 1;
420 char *name = malloc(name_len);
421 memcpy(name, buffer, name_len);
424 entry->next = temp_files;
430 static void free_temp_files(void)
432 file_list_entry_t *entry = temp_files;
433 file_list_entry_t *next;
434 for ( ; entry != NULL; entry = next) {
438 free((char*) entry->name);
445 * Do the necessary lowering for compound parameters.
447 void lower_compound_params(void)
449 lower_params_t params;
451 params.def_ptr_alignment = 4;
452 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
453 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
454 params.find_pointer_type = NULL;
455 params.ret_compound_in_regs = NULL;
456 lower_calls_with_compounds(¶ms);
459 typedef enum compile_mode_t {
473 static void usage(const char *argv0)
475 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
478 static void print_cparser_version(void) {
480 firm_get_version(&ver);
482 printf("cparser (%s) using libFirm (%u.%u",
483 cparser_REVISION, ver.major, ver.minor);
484 if (ver.revision[0] != 0) {
486 fputs(ver.revision, stdout);
488 if (ver.build[0] != 0) {
490 fputs(ver.build, stdout);
495 static void set_be_option(const char *arg)
497 int res = firm_be_option(arg);
502 static void set_option(const char *arg)
504 int res = firm_option(arg);
509 static void copy_file(FILE *dest, FILE *input)
513 while (!feof(input) && !ferror(dest)) {
514 size_t read = fread(buf, 1, sizeof(buf), input);
515 if (fwrite(buf, 1, read, dest) != read) {
516 perror("couldn't write output");
521 static inline bool streq(char const* a, char const* b)
523 return strcmp(a, b) == 0;
526 static inline bool strstart(char const* str, char const* start)
531 } while (*str++ == *start++);
535 static FILE *open_file(const char *filename)
537 if (streq(filename, "-")) {
541 FILE *in = fopen(filename, "r");
543 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
551 static filetype_t get_filetype_from_string(const char *string)
553 if (streq(string, "c") || streq(string, "c-header"))
555 if (streq(string, "c++") || streq(string, "c++-header"))
557 if (streq(string, "assembler"))
558 return FILETYPE_PREPROCESSED_ASSEMBLER;
559 if (streq(string, "assembler-with-cpp"))
560 return FILETYPE_ASSEMBLER;
561 if (streq(string, "none"))
562 return FILETYPE_AUTODETECT;
564 return FILETYPE_UNKNOWN;
567 static void init_os_support(void)
569 /* OS option must be set to the backend */
570 switch (firm_opt.os_support) {
571 case OS_SUPPORT_MINGW:
572 set_be_option("ia32-gasmode=mingw");
573 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
575 case OS_SUPPORT_LINUX:
576 set_be_option("ia32-gasmode=elf");
578 case OS_SUPPORT_MACHO:
579 set_be_option("ia32-gasmode=macho");
580 set_be_option("ia32-stackalign=4");
581 set_be_option("pic");
586 typedef enum lang_standard_t {
587 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
588 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
589 STANDARD_C89, /* ISO C90 (sic) */
590 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
591 STANDARD_C99, /* ISO C99 */
592 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
593 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
594 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
595 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
598 int main(int argc, char **argv)
602 const char *dumpfunction = NULL;
603 compile_mode_t mode = CompileAssembleLink;
605 int result = EXIT_SUCCESS;
606 char cpu_arch[16] = "ia32";
607 file_list_entry_t *files = NULL;
608 file_list_entry_t *last_file = NULL;
609 bool construct_dep_target = false;
610 struct obstack file_obst;
612 atexit(free_temp_files);
614 /* hack for now... */
615 if (strstr(argv[0], "pptest") != NULL) {
616 extern int pptest_main(int argc, char **argv);
617 return pptest_main(argc, argv);
620 obstack_init(&cppflags_obst);
621 obstack_init(&ldflags_obst);
622 obstack_init(&file_obst);
624 #define GET_ARG_AFTER(def, args) \
625 def = &arg[sizeof(args)-1]; \
626 if (def[0] == '\0') { \
629 fprintf(stderr, "error: expected argument after '" args "'\n"); \
630 argument_errors = true; \
634 if (def[0] == '-' && def[1] != '\0') { \
635 fprintf(stderr, "error: expected argument after '" args "'\n"); \
636 argument_errors = true; \
641 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
643 /* early options parsing (find out optimisation level and OS) */
644 for (int i = 1; i < argc; ++i) {
645 const char *arg = argv[i];
649 const char *option = &arg[1];
650 if (option[0] == 'O') {
651 sscanf(&option[1], "%d", &opt_level);
653 if (strcmp(arg, "-fwin32") == 0) {
654 firm_opt.os_support = OS_SUPPORT_MINGW;
655 } else if (strcmp(arg, "-fmac") == 0) {
656 firm_opt.os_support = OS_SUPPORT_MACHO;
657 } else if (strcmp(arg, "-flinux") == 0) {
658 firm_opt.os_support = OS_SUPPORT_LINUX;
662 /* set target/os specific stuff */
665 /* apply optimisation level */
668 set_option("no-opt");
671 set_option("no-inline");
675 set_option("strict-aliasing");
676 /* use_builtins = true; */
679 set_option("cond-eval");
680 set_option("if-conv");
683 set_option("inline");
684 set_option("deconv");
685 set_be_option("omitfp");
689 /* parse rest of options */
690 lang_standard_t standard = STANDARD_DEFAULT;
691 unsigned features_on = 0;
692 unsigned features_off = 0;
693 filetype_t forced_filetype = FILETYPE_AUTODETECT;
694 bool help_displayed = false;
695 bool argument_errors = false;
696 for (int i = 1; i < argc; ++i) {
697 const char *arg = argv[i];
698 if (arg[0] == '-' && arg[1] != '\0') {
700 const char *option = &arg[1];
701 if (option[0] == 'o') {
702 GET_ARG_AFTER(outname, "-o");
703 } else if (option[0] == 'g') {
704 set_be_option("debuginfo=stabs");
705 set_be_option("omitfp=no");
706 set_be_option("ia32-nooptcc=yes");
707 } else if (SINGLE_OPTION('c')) {
708 mode = CompileAssemble;
709 } else if (SINGLE_OPTION('E')) {
710 mode = PreprocessOnly;
711 } else if (SINGLE_OPTION('S')) {
713 } else if (option[0] == 'O') {
715 } else if (option[0] == 'I') {
717 GET_ARG_AFTER(opt, "-I");
718 add_flag(&cppflags_obst, "-I%s", opt);
719 } else if (option[0] == 'D') {
721 GET_ARG_AFTER(opt, "-D");
722 add_flag(&cppflags_obst, "-D%s", opt);
723 } else if (option[0] == 'U') {
725 GET_ARG_AFTER(opt, "-U");
726 add_flag(&cppflags_obst, "-U%s", opt);
727 } else if (option[0] == 'l') {
729 GET_ARG_AFTER(opt, "-l");
730 add_flag(&ldflags_obst, "-l%s", opt);
731 } else if (option[0] == 'L') {
733 GET_ARG_AFTER(opt, "-L");
734 add_flag(&ldflags_obst, "-L%s", opt);
735 } else if (SINGLE_OPTION('v')) {
737 } else if (SINGLE_OPTION('w')) {
738 memset(&warning, 0, sizeof(warning));
739 } else if (option[0] == 'x') {
741 GET_ARG_AFTER(opt, "-x");
742 forced_filetype = get_filetype_from_string(opt);
743 if (forced_filetype == FILETYPE_UNKNOWN) {
744 fprintf(stderr, "Unknown language '%s'\n", opt);
745 argument_errors = true;
747 } else if (streq(option, "M")) {
748 mode = PreprocessOnly;
749 add_flag(&cppflags_obst, "-M");
750 } else if (streq(option, "MMD") ||
751 streq(option, "MD")) {
752 construct_dep_target = true;
753 add_flag(&cppflags_obst, "-%s", option);
754 } else if (streq(option, "MM") ||
755 streq(option, "MP")) {
756 add_flag(&cppflags_obst, "-%s", option);
757 } else if (streq(option, "MT") ||
758 streq(option, "MQ") ||
759 streq(option, "MF")) {
761 GET_ARG_AFTER(opt, "-MT");
762 add_flag(&cppflags_obst, "-%s", option);
763 add_flag(&cppflags_obst, "%s", opt);
764 } else if (streq(option, "pipe")) {
765 /* here for gcc compatibility */
766 } else if (option[0] == 'f') {
767 char const *orig_opt;
768 GET_ARG_AFTER(orig_opt, "-f");
770 if (strstart(orig_opt, "align-loops=") ||
771 strstart(orig_opt, "align-jumps=") ||
772 strstart(orig_opt, "align-functions=")) {
773 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
775 char const *opt = orig_opt;
776 bool truth_value = true;
777 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
782 if (streq(opt, "dollars-in-identifiers")) {
783 allow_dollar_in_symbol = truth_value;
784 } if (streq(opt, "builtins")) {
785 use_builtins = truth_value;
786 } else if (streq(opt, "short-wchar")) {
787 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
789 } else if (streq(opt, "syntax-only")) {
790 mode = truth_value ? ParseOnly : CompileAssembleLink;
791 } else if (streq(opt, "omit-frame-pointer")) {
792 set_be_option(truth_value ? "omitfp" : "omitfp=no");
793 } else if (streq(opt, "strength-reduce")) {
794 firm_option(truth_value ? "strength-red" : "no-strength-red");
795 } else if (streq(opt, "fast-math") ||
796 streq(opt, "jump-tables") ||
797 streq(opt, "unroll-loops") ||
798 streq(opt, "expensive-optimizations") ||
799 streq(opt, "common") ||
801 streq(opt, "align-loops") ||
802 streq(opt, "align-jumps") ||
803 streq(opt, "align-functions")) {
804 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
806 int res = firm_option(orig_opt);
808 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
810 argument_errors = true;
812 } else if (res == -1) {
813 help_displayed = true;
817 } else if (option[0] == 'b') {
819 GET_ARG_AFTER(opt, "-b");
820 int res = firm_be_option(opt);
822 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
824 argument_errors = true;
825 } else if (res == -1) {
826 help_displayed = true;
827 } else if (strstart(opt, "isa=")) {
828 strncpy(cpu_arch, opt, sizeof(cpu_arch));
830 } else if (option[0] == 'W') {
831 if (strstart(option + 1, "l,")) // a gcc-style linker option
834 GET_ARG_AFTER(opt, "-Wl,");
835 add_flag(&ldflags_obst, "-Wl,%s", opt);
837 else set_warning_opt(&option[1]);
838 } else if (option[0] == 'm') {
843 GET_ARG_AFTER(opt, "-m");
844 if (strstart(opt, "arch=")) {
845 GET_ARG_AFTER(opt, "-march=");
846 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
847 int res = firm_be_option(arch_opt);
849 argument_errors = true;
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;
856 } else if (strstart(opt, "tune=")) {
857 GET_ARG_AFTER(opt, "-mtune=");
858 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
859 int res = firm_be_option(arch_opt);
861 argument_errors = true;
862 } else if (strstart(opt, "cpu=")) {
863 GET_ARG_AFTER(opt, "-mcpu=");
864 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
865 int res = firm_be_option(arch_opt);
867 argument_errors = true;
868 } else if (strstart(opt, "fpmath=")) {
869 GET_ARG_AFTER(opt, "-mfpmath=");
870 if (streq(opt, "387"))
872 else if (streq(opt, "sse"))
875 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
876 argument_errors = true;
878 if (!argument_errors) {
879 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
880 int res = firm_be_option(arch_opt);
882 argument_errors = true;
884 } else if (strstart(opt, "preferred-stack-boundary=")) {
885 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
886 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
887 int res = firm_be_option(arch_opt);
889 argument_errors = true;
890 } else if (streq(opt, "omit-leaf-frame-pointer")) {
891 set_be_option("omitleaffp=1");
892 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
893 set_be_option("omitleaffp=0");
896 long int value = strtol(opt, &endptr, 10);
897 if (*endptr != '\0') {
898 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
899 argument_errors = true;
901 if (value != 16 && value != 32 && value != 64) {
902 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
903 argument_errors = true;
905 machine_size = (unsigned int)value;
908 } else if (streq(option, "pg")) {
909 set_be_option("gprof");
910 add_flag(&ldflags_obst, "-pg");
911 } else if (streq(option, "pedantic") ||
912 streq(option, "ansi")) {
913 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
914 } else if (streq(option, "shared")) {
915 add_flag(&ldflags_obst, "-shared");
916 } else if (strstart(option, "std=")) {
917 const char *const o = &option[4];
919 streq(o, "c++") ? STANDARD_CXX98 :
920 streq(o, "c++98") ? STANDARD_CXX98 :
921 streq(o, "c89") ? STANDARD_C89 :
922 streq(o, "c99") ? STANDARD_C99 :
923 streq(o, "c9x") ? STANDARD_C99 : // deprecated
924 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
925 streq(o, "gnu89") ? STANDARD_GNU89 :
926 streq(o, "gnu99") ? STANDARD_GNU99 :
927 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
928 streq(o, "iso9899:1990") ? STANDARD_C89 :
929 streq(o, "iso9899:199409") ? STANDARD_C90 :
930 streq(o, "iso9899:1999") ? STANDARD_C99 :
931 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
932 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
933 } else if (streq(option, "version")) {
934 print_cparser_version();
935 } else if (option[0] == '-') {
936 /* double dash option */
938 if (streq(option, "gcc")) {
939 features_on |= _GNUC;
940 features_off &= ~_GNUC;
941 } else if (streq(option, "no-gcc")) {
942 features_on &= ~_GNUC;
943 features_off |= _GNUC;
944 } else if (streq(option, "ms")) {
946 features_off &= ~_MS;
947 } else if (streq(option, "no-ms")) {
950 } else if (streq(option, "signed-chars")) {
951 char_is_signed = true;
952 } else if (streq(option, "unsigned-chars")) {
953 char_is_signed = false;
954 } else if (streq(option, "strict")) {
956 } else if (streq(option, "lextest")) {
958 } else if (streq(option, "benchmark")) {
959 mode = BenchmarkParser;
960 } else if (streq(option, "print-ast")) {
962 } else if (streq(option, "print-implicit-cast")) {
963 print_implicit_casts = true;
964 } else if (streq(option, "print-parenthesis")) {
965 print_parenthesis = true;
966 } else if (streq(option, "print-fluffy")) {
968 } else if (streq(option, "print-caml")) {
970 } else if (streq(option, "version")) {
971 print_cparser_version();
973 } else if (streq(option, "dump-function")) {
976 fprintf(stderr, "error: "
977 "expected argument after '--dump-function'\n");
978 argument_errors = true;
981 dumpfunction = argv[i];
984 fprintf(stderr, "error: unknown argument '%s'\n", arg);
985 argument_errors = true;
988 fprintf(stderr, "error: unknown argument '%s'\n", arg);
989 argument_errors = true;
992 filetype_t type = forced_filetype;
993 if (type == FILETYPE_AUTODETECT) {
994 if (streq(arg, "-")) {
995 /* - implicitly means C source file */
998 const char *suffix = strrchr(arg, '.');
999 /* Ensure there is at least one char before the suffix */
1000 if (suffix != NULL && suffix != arg) {
1003 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1004 streq(suffix, "a") ? FILETYPE_OBJECT :
1005 streq(suffix, "c") ? FILETYPE_C :
1006 streq(suffix, "cc") ? FILETYPE_CXX :
1007 streq(suffix, "cpp") ? FILETYPE_CXX :
1008 streq(suffix, "cxx") ? FILETYPE_CXX :
1009 streq(suffix, "h") ? FILETYPE_C :
1010 streq(suffix, "o") ? FILETYPE_OBJECT :
1011 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1012 streq(suffix, "so") ? FILETYPE_OBJECT :
1013 FILETYPE_AUTODETECT;
1017 if (type == FILETYPE_AUTODETECT) {
1018 fprintf(stderr, "'%s': file format not recognized\n", arg);
1023 file_list_entry_t *entry
1024 = obstack_alloc(&file_obst, sizeof(entry[0]));
1025 memset(entry, 0, sizeof(entry[0]));
1029 if (last_file != NULL) {
1030 last_file->next = entry;
1038 if (files == NULL) {
1039 fprintf(stderr, "error: no input files specified\n");
1040 argument_errors = true;
1043 if (help_displayed) {
1044 return !argument_errors;
1046 if (argument_errors) {
1051 /* we do the lowering in ast2firm */
1052 firm_opt.lower_bitfields = FALSE;
1055 init_symbol_table();
1065 if (construct_dep_target) {
1066 if (outname != 0 && strlen(outname) >= 2) {
1067 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1069 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1072 dep_target[0] = '\0';
1075 char outnamebuf[4096];
1076 if (outname == NULL) {
1077 const char *filename = files->name;
1080 case BenchmarkParser:
1085 case PreprocessOnly:
1090 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1091 outname = outnamebuf;
1093 case CompileAssemble:
1094 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1095 outname = outnamebuf;
1098 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1100 outname = outnamebuf;
1102 case CompileAssembleLink:
1112 assert(outname != NULL);
1115 if (streq(outname, "-")) {
1118 out = fopen(outname, "w");
1120 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1126 file_list_entry_t *file;
1127 for (file = files; file != NULL; file = file->next) {
1128 char asm_tempfile[1024];
1129 const char *filename = file->name;
1130 filetype_t filetype = file->type;
1132 if (filetype == FILETYPE_OBJECT)
1136 if (mode == LexTest) {
1138 in = open_file(filename);
1139 lextest(in, filename);
1144 FILE *preprocessed_in = NULL;
1146 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1147 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1148 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1150 /* no support for input on FILE* yet */
1152 panic("internal compiler error: in for preprocessor != NULL");
1154 preprocessed_in = preprocess(filename);
1155 if (mode == PreprocessOnly) {
1156 copy_file(out, preprocessed_in);
1157 int result = pclose(preprocessed_in);
1162 in = preprocessed_in;
1170 if (mode == Compile) {
1173 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1177 in = open_file(filename);
1179 /* preprocess and compile */
1180 if (filetype == FILETYPE_PREPROCESSED_C) {
1181 char const* invalid_mode;
1184 case STANDARD_C89: c_mode = _C89; break;
1185 /* TODO ^v determine difference between these two */
1186 case STANDARD_C90: c_mode = _C89; break;
1187 case STANDARD_C99: c_mode = _C89 | _C99; break;
1188 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1192 "warning: command line option \"-std=%s\" is not valid for C\n",
1195 case STANDARD_DEFAULT:
1196 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1198 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1199 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1202 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1203 char const* invalid_mode;
1205 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1206 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1207 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1208 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1209 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1212 case STANDARD_CXX98: c_mode = _CXX; break;
1216 "warning: command line option \"-std=%s\" is not valid for C++\n",
1218 case STANDARD_DEFAULT:
1219 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1223 c_mode |= features_on;
1224 c_mode &= ~features_off;
1226 translation_unit_t *const unit = do_parsing(in, filename);
1228 /* prints the AST even if errors occurred */
1229 if (mode == PrintAst) {
1230 type_set_output(out);
1231 ast_set_output(out);
1235 if (error_count > 0) {
1236 /* parsing failed because of errors */
1237 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1239 result = EXIT_FAILURE;
1241 } else if (warning_count > 0) {
1242 fprintf(stderr, "%u warning(s)\n", warning_count);
1245 if (in == preprocessed_in) {
1246 int pp_result = pclose(preprocessed_in);
1247 if (pp_result != EXIT_SUCCESS) {
1252 if (mode == BenchmarkParser) {
1254 } else if (mode == PrintFluffy) {
1255 write_fluffy_decls(out, unit);
1257 } else if (mode == PrintCaml) {
1258 write_caml_decls(out, unit);
1262 translation_unit_to_firm(unit);
1264 if (mode == ParseOnly) {
1268 if (mode == CompileDump) {
1270 ident *id = new_id_from_str(dumpfunction);
1271 ir_graph *irg = NULL;
1272 int n_irgs = get_irp_n_irgs();
1273 for (int i = 0; i < n_irgs; ++i) {
1274 ir_graph *tirg = get_irp_irg(i);
1275 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1283 fprintf(stderr, "No graph for function '%s' found\n",
1288 dump_ir_block_graph_file(irg, out);
1293 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1294 have_const_functions);
1295 if (asm_out != out) {
1298 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1299 copy_file(asm_out, in);
1300 if (in == preprocessed_in) {
1301 int pp_result = pclose(preprocessed_in);
1302 if (pp_result != EXIT_SUCCESS) {
1306 if (asm_out != out) {
1311 if (mode == Compile)
1314 /* if we're here then we have preprocessed assembly */
1315 filename = asm_tempfile;
1316 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1319 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1321 const char *filename_o;
1322 if (mode == CompileAssemble) {
1324 filename_o = outname;
1326 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1331 assemble(filename_o, filename);
1333 size_t len = strlen(filename_o) + 1;
1334 filename = obstack_copy(&file_obst, filename_o, len);
1335 filetype = FILETYPE_OBJECT;
1338 /* ok we're done here, process next file */
1339 file->name = filename;
1340 file->type = filetype;
1343 if (result != EXIT_SUCCESS)
1346 /* link program file */
1347 if (mode == CompileAssembleLink) {
1348 obstack_1grow(&ldflags_obst, '\0');
1349 const char *flags = obstack_finish(&ldflags_obst);
1351 /* construct commandline */
1352 obstack_printf(&file_obst, "%s", LINKER);
1353 for (file_list_entry_t *entry = files; entry != NULL;
1354 entry = entry->next) {
1355 if (entry->type != FILETYPE_OBJECT)
1358 add_flag(&file_obst, "%s", entry->name);
1361 add_flag(&file_obst, "-o");
1362 add_flag(&file_obst, outname);
1363 obstack_printf(&file_obst, "%s", flags);
1364 obstack_1grow(&file_obst, '\0');
1366 char *commandline = obstack_finish(&file_obst);
1371 int err = system(commandline);
1372 if (err != EXIT_SUCCESS) {
1373 fprintf(stderr, "linker reported an error\n");
1378 obstack_free(&cppflags_obst, NULL);
1379 obstack_free(&ldflags_obst, NULL);
1380 obstack_free(&file_obst, NULL);
1390 exit_symbol_table();