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);
271 static void add_quoted_string(struct obstack *obst, const char *s)
274 obstack_1grow(obst, '"');
275 obstack_grow(obst, s, strlen(s));
276 obstack_1grow(obst, '"');
278 add_flag(obst, "%s", s);
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__=");
298 add_quoted_string(&cppflags_obst, type_to_string(type_wchar_t));
299 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
300 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=");
301 add_quoted_string(&cppflags_obst, type_to_string(type_size_t));
303 /* handle dependency generation */
304 if (dep_target[0] != '\0') {
305 add_flag(&cppflags_obst, "-MF");
306 add_quoted_string(&cppflags_obst, dep_target);
307 if (outname != NULL) {
308 add_flag(&cppflags_obst, "-MQ");
309 add_quoted_string(&cppflags_obst, outname);
312 if (flags[0] != '\0') {
313 obstack_printf(&cppflags_obst, " %s", flags);
315 obstack_1grow(&cppflags_obst, ' ');
316 add_quoted_string(&cppflags_obst, fname);
318 obstack_1grow(&cppflags_obst, '\0');
319 const char *buf = obstack_finish(&cppflags_obst);
324 FILE *f = popen(buf, "r");
326 fprintf(stderr, "invoking preprocessor failed\n");
333 static void assemble(const char *out, const char *in)
337 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
342 int err = system(buf);
344 fprintf(stderr, "assembler reported an error\n");
349 static const char *try_dir(const char *dir)
353 if (access(dir, R_OK | W_OK | X_OK) == 0)
358 static const char *get_tempdir(void)
360 static const char *tmpdir = NULL;
366 tmpdir = try_dir(getenv("TMPDIR"));
368 tmpdir = try_dir(getenv("TMP"));
370 tmpdir = try_dir(getenv("TEMP"));
374 tmpdir = try_dir(P_tmpdir);
378 tmpdir = try_dir("/var/tmp");
380 tmpdir = try_dir("/usr/tmp");
382 tmpdir = try_dir("/tmp");
391 /* cheap and nasty mkstemp replacement */
392 static int mkstemp(char *templ)
395 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
400 * an own version of tmpnam, which: writes in a buffer, emits no warnings
401 * during linking (like glibc/gnu ld do for tmpnam)...
403 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
405 const char *tempdir = get_tempdir();
407 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
409 int fd = mkstemp(buffer);
411 fprintf(stderr, "couldn't create temporary file: %s\n",
415 FILE *out = fdopen(fd, "w");
417 fprintf(stderr, "couldn't create temporary file FILE*\n");
421 file_list_entry_t *entry = xmalloc(sizeof(*entry));
422 memset(entry, 0, sizeof(*entry));
424 size_t name_len = strlen(buffer) + 1;
425 char *name = malloc(name_len);
426 memcpy(name, buffer, name_len);
429 entry->next = temp_files;
435 static void free_temp_files(void)
437 file_list_entry_t *entry = temp_files;
438 file_list_entry_t *next;
439 for ( ; entry != NULL; entry = next) {
443 free((char*) entry->name);
450 * Do the necessary lowering for compound parameters.
452 void lower_compound_params(void)
454 lower_params_t params;
456 params.def_ptr_alignment = 4;
457 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
458 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
459 params.find_pointer_type = NULL;
460 params.ret_compound_in_regs = NULL;
461 lower_calls_with_compounds(¶ms);
464 typedef enum compile_mode_t {
478 static void usage(const char *argv0)
480 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
483 static void print_cparser_version(void) {
485 firm_get_version(&ver);
487 printf("cparser (%s) using libFirm (%u.%u",
488 cparser_REVISION, ver.major, ver.minor);
489 if (ver.revision[0] != 0) {
491 fputs(ver.revision, stdout);
493 if (ver.build[0] != 0) {
495 fputs(ver.build, stdout);
500 static void set_be_option(const char *arg)
502 int res = firm_be_option(arg);
507 static void set_option(const char *arg)
509 int res = firm_option(arg);
514 static void copy_file(FILE *dest, FILE *input)
518 while (!feof(input) && !ferror(dest)) {
519 size_t read = fread(buf, 1, sizeof(buf), input);
520 if (fwrite(buf, 1, read, dest) != read) {
521 perror("couldn't write output");
526 static inline bool streq(char const* a, char const* b)
528 return strcmp(a, b) == 0;
531 static inline bool strstart(char const* str, char const* start)
536 } while (*str++ == *start++);
540 static FILE *open_file(const char *filename)
542 if (streq(filename, "-")) {
546 FILE *in = fopen(filename, "r");
548 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
556 static filetype_t get_filetype_from_string(const char *string)
558 if (streq(string, "c") || streq(string, "c-header"))
560 if (streq(string, "c++") || streq(string, "c++-header"))
562 if (streq(string, "assembler"))
563 return FILETYPE_PREPROCESSED_ASSEMBLER;
564 if (streq(string, "assembler-with-cpp"))
565 return FILETYPE_ASSEMBLER;
566 if (streq(string, "none"))
567 return FILETYPE_AUTODETECT;
569 return FILETYPE_UNKNOWN;
572 static void init_os_support(void)
574 /* OS option must be set to the backend */
575 switch (firm_opt.os_support) {
576 case OS_SUPPORT_MINGW:
577 set_be_option("ia32-gasmode=mingw");
578 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
580 case OS_SUPPORT_LINUX:
581 set_be_option("ia32-gasmode=elf");
583 case OS_SUPPORT_MACHO:
584 set_be_option("ia32-gasmode=macho");
585 set_be_option("ia32-stackalign=4");
586 set_be_option("pic");
591 typedef enum lang_standard_t {
592 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
593 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
594 STANDARD_C89, /* ISO C90 (sic) */
595 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
596 STANDARD_C99, /* ISO C99 */
597 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
598 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
599 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
600 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
603 int main(int argc, char **argv)
607 const char *dumpfunction = NULL;
608 compile_mode_t mode = CompileAssembleLink;
610 int result = EXIT_SUCCESS;
611 char cpu_arch[16] = "ia32";
612 file_list_entry_t *files = NULL;
613 file_list_entry_t *last_file = NULL;
614 bool construct_dep_target = false;
615 struct obstack file_obst;
617 atexit(free_temp_files);
619 /* hack for now... */
620 if (strstr(argv[0], "pptest") != NULL) {
621 extern int pptest_main(int argc, char **argv);
622 return pptest_main(argc, argv);
625 obstack_init(&cppflags_obst);
626 obstack_init(&ldflags_obst);
627 obstack_init(&file_obst);
629 #define GET_ARG_AFTER(def, args) \
630 def = &arg[sizeof(args)-1]; \
631 if (def[0] == '\0') { \
634 fprintf(stderr, "error: expected argument after '" args "'\n"); \
635 argument_errors = true; \
639 if (def[0] == '-' && def[1] != '\0') { \
640 fprintf(stderr, "error: expected argument after '" args "'\n"); \
641 argument_errors = true; \
646 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
648 /* early options parsing (find out optimisation level and OS) */
649 for (int i = 1; i < argc; ++i) {
650 const char *arg = argv[i];
654 const char *option = &arg[1];
655 if (option[0] == 'O') {
656 sscanf(&option[1], "%d", &opt_level);
658 if (strcmp(arg, "-fwin32") == 0) {
659 firm_opt.os_support = OS_SUPPORT_MINGW;
660 } else if (strcmp(arg, "-fmac") == 0) {
661 firm_opt.os_support = OS_SUPPORT_MACHO;
662 } else if (strcmp(arg, "-flinux") == 0) {
663 firm_opt.os_support = OS_SUPPORT_LINUX;
667 /* set target/os specific stuff */
670 /* apply optimisation level */
673 set_option("no-opt");
676 set_option("no-inline");
680 set_option("strict-aliasing");
681 /* use_builtins = true; */
684 set_option("cond-eval");
685 set_option("if-conv");
688 set_option("inline");
689 set_option("deconv");
690 set_be_option("omitfp");
694 /* parse rest of options */
695 lang_standard_t standard = STANDARD_DEFAULT;
696 unsigned features_on = 0;
697 unsigned features_off = 0;
698 filetype_t forced_filetype = FILETYPE_AUTODETECT;
699 bool help_displayed = false;
700 bool argument_errors = false;
701 for (int i = 1; i < argc; ++i) {
702 const char *arg = argv[i];
703 if (arg[0] == '-' && arg[1] != '\0') {
705 const char *option = &arg[1];
706 if (option[0] == 'o') {
707 GET_ARG_AFTER(outname, "-o");
708 } else if (option[0] == 'g') {
709 set_be_option("debuginfo=stabs");
710 set_be_option("omitfp=no");
711 set_be_option("ia32-nooptcc=yes");
712 } else if (SINGLE_OPTION('c')) {
713 mode = CompileAssemble;
714 } else if (SINGLE_OPTION('E')) {
715 mode = PreprocessOnly;
716 } else if (SINGLE_OPTION('S')) {
718 } else if (option[0] == 'O') {
720 } else if (option[0] == 'I') {
722 GET_ARG_AFTER(opt, "-I");
723 add_flag(&cppflags_obst, "-I%s", opt);
724 } else if (option[0] == 'D') {
726 GET_ARG_AFTER(opt, "-D");
727 add_flag(&cppflags_obst, "-D%s", opt);
728 } else if (option[0] == 'U') {
730 GET_ARG_AFTER(opt, "-U");
731 add_flag(&cppflags_obst, "-U%s", opt);
732 } else if (option[0] == 'l') {
734 GET_ARG_AFTER(opt, "-l");
735 add_flag(&ldflags_obst, "-l%s", opt);
736 } else if (option[0] == 'L') {
738 GET_ARG_AFTER(opt, "-L");
739 add_flag(&ldflags_obst, "-L%s", opt);
740 } else if (SINGLE_OPTION('v')) {
742 } else if (SINGLE_OPTION('w')) {
743 memset(&warning, 0, sizeof(warning));
744 } else if (option[0] == 'x') {
746 GET_ARG_AFTER(opt, "-x");
747 forced_filetype = get_filetype_from_string(opt);
748 if (forced_filetype == FILETYPE_UNKNOWN) {
749 fprintf(stderr, "Unknown language '%s'\n", opt);
750 argument_errors = true;
752 } else if (streq(option, "M")) {
753 mode = PreprocessOnly;
754 add_flag(&cppflags_obst, "-M");
755 } else if (streq(option, "MMD") ||
756 streq(option, "MD")) {
757 construct_dep_target = true;
758 add_flag(&cppflags_obst, "-%s", option);
759 } else if (streq(option, "MM") ||
760 streq(option, "MP")) {
761 add_flag(&cppflags_obst, "-%s", option);
762 } else if (streq(option, "MT") ||
763 streq(option, "MQ") ||
764 streq(option, "MF")) {
766 GET_ARG_AFTER(opt, "-MT");
767 add_flag(&cppflags_obst, "-%s", option);
768 add_flag(&cppflags_obst, "%s", opt);
769 } else if (streq(option, "pipe")) {
770 /* here for gcc compatibility */
771 } else if (option[0] == 'f') {
772 char const *orig_opt;
773 GET_ARG_AFTER(orig_opt, "-f");
775 if (strstart(orig_opt, "align-loops=") ||
776 strstart(orig_opt, "align-jumps=") ||
777 strstart(orig_opt, "align-functions=")) {
778 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
780 char const *opt = orig_opt;
781 bool truth_value = true;
782 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
787 if (streq(opt, "dollars-in-identifiers")) {
788 allow_dollar_in_symbol = truth_value;
789 } if (streq(opt, "builtins")) {
790 use_builtins = truth_value;
791 } else if (streq(opt, "short-wchar")) {
792 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
794 } else if (streq(opt, "syntax-only")) {
795 mode = truth_value ? ParseOnly : CompileAssembleLink;
796 } else if (streq(opt, "omit-frame-pointer")) {
797 set_be_option(truth_value ? "omitfp" : "omitfp=no");
798 } else if (streq(opt, "strength-reduce")) {
799 firm_option(truth_value ? "strength-red" : "no-strength-red");
800 } else if (streq(opt, "fast-math") ||
801 streq(opt, "jump-tables") ||
802 streq(opt, "unroll-loops") ||
803 streq(opt, "expensive-optimizations") ||
804 streq(opt, "common") ||
806 streq(opt, "align-loops") ||
807 streq(opt, "align-jumps") ||
808 streq(opt, "align-functions")) {
809 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
811 int res = firm_option(orig_opt);
813 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
815 argument_errors = true;
817 } else if (res == -1) {
818 help_displayed = true;
822 } else if (option[0] == 'b') {
824 GET_ARG_AFTER(opt, "-b");
825 int res = firm_be_option(opt);
827 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
829 argument_errors = true;
830 } else if (res == -1) {
831 help_displayed = true;
832 } else if (strstart(opt, "isa=")) {
833 strncpy(cpu_arch, opt, sizeof(cpu_arch));
835 } else if (option[0] == 'W') {
836 if (strstart(option + 1, "l,")) // a gcc-style linker option
839 GET_ARG_AFTER(opt, "-Wl,");
840 add_flag(&ldflags_obst, "-Wl,%s", opt);
842 else set_warning_opt(&option[1]);
843 } else if (option[0] == 'm') {
848 GET_ARG_AFTER(opt, "-m");
849 if (strstart(opt, "arch=")) {
850 GET_ARG_AFTER(opt, "-march=");
851 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
852 int res = firm_be_option(arch_opt);
854 argument_errors = true;
856 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
857 int res = firm_be_option(arch_opt);
859 argument_errors = true;
861 } else if (strstart(opt, "tune=")) {
862 GET_ARG_AFTER(opt, "-mtune=");
863 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
864 int res = firm_be_option(arch_opt);
866 argument_errors = true;
867 } else if (strstart(opt, "cpu=")) {
868 GET_ARG_AFTER(opt, "-mcpu=");
869 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
870 int res = firm_be_option(arch_opt);
872 argument_errors = true;
873 } else if (strstart(opt, "fpmath=")) {
874 GET_ARG_AFTER(opt, "-mfpmath=");
875 if (streq(opt, "387"))
877 else if (streq(opt, "sse"))
880 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
881 argument_errors = true;
883 if (!argument_errors) {
884 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
885 int res = firm_be_option(arch_opt);
887 argument_errors = true;
889 } else if (strstart(opt, "preferred-stack-boundary=")) {
890 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
891 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
892 int res = firm_be_option(arch_opt);
894 argument_errors = true;
895 } else if (streq(opt, "omit-leaf-frame-pointer")) {
896 set_be_option("omitleaffp=1");
897 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
898 set_be_option("omitleaffp=0");
901 long int value = strtol(opt, &endptr, 10);
902 if (*endptr != '\0') {
903 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
904 argument_errors = true;
906 if (value != 16 && value != 32 && value != 64) {
907 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
908 argument_errors = true;
910 machine_size = (unsigned int)value;
913 } else if (streq(option, "pg")) {
914 set_be_option("gprof");
915 add_flag(&ldflags_obst, "-pg");
916 } else if (streq(option, "pedantic") ||
917 streq(option, "ansi")) {
918 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
919 } else if (streq(option, "shared")) {
920 add_flag(&ldflags_obst, "-shared");
921 } else if (strstart(option, "std=")) {
922 const char *const o = &option[4];
924 streq(o, "c++") ? STANDARD_CXX98 :
925 streq(o, "c++98") ? STANDARD_CXX98 :
926 streq(o, "c89") ? STANDARD_C89 :
927 streq(o, "c99") ? STANDARD_C99 :
928 streq(o, "c9x") ? STANDARD_C99 : // deprecated
929 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
930 streq(o, "gnu89") ? STANDARD_GNU89 :
931 streq(o, "gnu99") ? STANDARD_GNU99 :
932 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
933 streq(o, "iso9899:1990") ? STANDARD_C89 :
934 streq(o, "iso9899:199409") ? STANDARD_C90 :
935 streq(o, "iso9899:1999") ? STANDARD_C99 :
936 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
937 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
938 } else if (streq(option, "version")) {
939 print_cparser_version();
940 } else if (option[0] == '-') {
941 /* double dash option */
943 if (streq(option, "gcc")) {
944 features_on |= _GNUC;
945 features_off &= ~_GNUC;
946 } else if (streq(option, "no-gcc")) {
947 features_on &= ~_GNUC;
948 features_off |= _GNUC;
949 } else if (streq(option, "ms")) {
951 features_off &= ~_MS;
952 } else if (streq(option, "no-ms")) {
955 } else if (streq(option, "signed-chars")) {
956 char_is_signed = true;
957 } else if (streq(option, "unsigned-chars")) {
958 char_is_signed = false;
959 } else if (streq(option, "strict")) {
961 } else if (streq(option, "lextest")) {
963 } else if (streq(option, "benchmark")) {
964 mode = BenchmarkParser;
965 } else if (streq(option, "print-ast")) {
967 } else if (streq(option, "print-implicit-cast")) {
968 print_implicit_casts = true;
969 } else if (streq(option, "print-parenthesis")) {
970 print_parenthesis = true;
971 } else if (streq(option, "print-fluffy")) {
973 } else if (streq(option, "print-caml")) {
975 } else if (streq(option, "version")) {
976 print_cparser_version();
978 } else if (streq(option, "dump-function")) {
981 fprintf(stderr, "error: "
982 "expected argument after '--dump-function'\n");
983 argument_errors = true;
986 dumpfunction = argv[i];
989 fprintf(stderr, "error: unknown argument '%s'\n", arg);
990 argument_errors = true;
993 fprintf(stderr, "error: unknown argument '%s'\n", arg);
994 argument_errors = true;
997 filetype_t type = forced_filetype;
998 if (type == FILETYPE_AUTODETECT) {
999 if (streq(arg, "-")) {
1000 /* - implicitly means C source file */
1003 const char *suffix = strrchr(arg, '.');
1004 /* Ensure there is at least one char before the suffix */
1005 if (suffix != NULL && suffix != arg) {
1008 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1009 streq(suffix, "a") ? FILETYPE_OBJECT :
1010 streq(suffix, "c") ? FILETYPE_C :
1011 streq(suffix, "cc") ? FILETYPE_CXX :
1012 streq(suffix, "cpp") ? FILETYPE_CXX :
1013 streq(suffix, "cxx") ? FILETYPE_CXX :
1014 streq(suffix, "h") ? FILETYPE_C :
1015 streq(suffix, "o") ? FILETYPE_OBJECT :
1016 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1017 streq(suffix, "so") ? FILETYPE_OBJECT :
1018 FILETYPE_AUTODETECT;
1022 if (type == FILETYPE_AUTODETECT) {
1023 fprintf(stderr, "'%s': file format not recognized\n", arg);
1028 file_list_entry_t *entry
1029 = obstack_alloc(&file_obst, sizeof(entry[0]));
1030 memset(entry, 0, sizeof(entry[0]));
1034 if (last_file != NULL) {
1035 last_file->next = entry;
1043 if (files == NULL) {
1044 fprintf(stderr, "error: no input files specified\n");
1045 argument_errors = true;
1048 if (help_displayed) {
1049 return !argument_errors;
1051 if (argument_errors) {
1056 /* we do the lowering in ast2firm */
1057 firm_opt.lower_bitfields = FALSE;
1060 init_symbol_table();
1070 if (construct_dep_target) {
1071 if (outname != 0 && strlen(outname) >= 2) {
1072 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1074 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1077 dep_target[0] = '\0';
1080 char outnamebuf[4096];
1081 if (outname == NULL) {
1082 const char *filename = files->name;
1085 case BenchmarkParser:
1090 case PreprocessOnly:
1095 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1096 outname = outnamebuf;
1098 case CompileAssemble:
1099 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1100 outname = outnamebuf;
1103 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1105 outname = outnamebuf;
1107 case CompileAssembleLink:
1117 assert(outname != NULL);
1120 if (streq(outname, "-")) {
1123 out = fopen(outname, "w");
1125 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1131 file_list_entry_t *file;
1132 for (file = files; file != NULL; file = file->next) {
1133 char asm_tempfile[1024];
1134 const char *filename = file->name;
1135 filetype_t filetype = file->type;
1137 if (filetype == FILETYPE_OBJECT)
1141 if (mode == LexTest) {
1143 in = open_file(filename);
1144 lextest(in, filename);
1149 FILE *preprocessed_in = NULL;
1151 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1152 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1153 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1155 /* no support for input on FILE* yet */
1157 panic("internal compiler error: in for preprocessor != NULL");
1159 preprocessed_in = preprocess(filename);
1160 if (mode == PreprocessOnly) {
1161 copy_file(out, preprocessed_in);
1162 int result = pclose(preprocessed_in);
1167 in = preprocessed_in;
1175 if (mode == Compile) {
1178 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1182 in = open_file(filename);
1184 /* preprocess and compile */
1185 if (filetype == FILETYPE_PREPROCESSED_C) {
1186 char const* invalid_mode;
1189 case STANDARD_C89: c_mode = _C89; break;
1190 /* TODO ^v determine difference between these two */
1191 case STANDARD_C90: c_mode = _C89; break;
1192 case STANDARD_C99: c_mode = _C89 | _C99; break;
1193 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1197 "warning: command line option \"-std=%s\" is not valid for C\n",
1200 case STANDARD_DEFAULT:
1201 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1203 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1204 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1207 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1208 char const* invalid_mode;
1210 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1211 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1212 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1213 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1214 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1217 case STANDARD_CXX98: c_mode = _CXX; break;
1221 "warning: command line option \"-std=%s\" is not valid for C++\n",
1223 case STANDARD_DEFAULT:
1224 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1228 c_mode |= features_on;
1229 c_mode &= ~features_off;
1231 translation_unit_t *const unit = do_parsing(in, filename);
1233 /* prints the AST even if errors occurred */
1234 if (mode == PrintAst) {
1235 type_set_output(out);
1236 ast_set_output(out);
1240 if (error_count > 0) {
1241 /* parsing failed because of errors */
1242 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1244 result = EXIT_FAILURE;
1246 } else if (warning_count > 0) {
1247 fprintf(stderr, "%u warning(s)\n", warning_count);
1250 if (in == preprocessed_in) {
1251 int pp_result = pclose(preprocessed_in);
1252 if (pp_result != EXIT_SUCCESS) {
1257 if (mode == BenchmarkParser) {
1259 } else if (mode == PrintFluffy) {
1260 write_fluffy_decls(out, unit);
1262 } else if (mode == PrintCaml) {
1263 write_caml_decls(out, unit);
1267 translation_unit_to_firm(unit);
1269 if (mode == ParseOnly) {
1273 if (mode == CompileDump) {
1275 ident *id = new_id_from_str(dumpfunction);
1276 ir_graph *irg = NULL;
1277 int n_irgs = get_irp_n_irgs();
1278 for (int i = 0; i < n_irgs; ++i) {
1279 ir_graph *tirg = get_irp_irg(i);
1280 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1288 fprintf(stderr, "No graph for function '%s' found\n",
1293 dump_ir_block_graph_file(irg, out);
1298 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1299 have_const_functions);
1300 if (asm_out != out) {
1303 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1304 copy_file(asm_out, in);
1305 if (in == preprocessed_in) {
1306 int pp_result = pclose(preprocessed_in);
1307 if (pp_result != EXIT_SUCCESS) {
1311 if (asm_out != out) {
1316 if (mode == Compile)
1319 /* if we're here then we have preprocessed assembly */
1320 filename = asm_tempfile;
1321 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1324 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1326 const char *filename_o;
1327 if (mode == CompileAssemble) {
1329 filename_o = outname;
1331 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1336 assemble(filename_o, filename);
1338 size_t len = strlen(filename_o) + 1;
1339 filename = obstack_copy(&file_obst, filename_o, len);
1340 filetype = FILETYPE_OBJECT;
1343 /* ok we're done here, process next file */
1344 file->name = filename;
1345 file->type = filetype;
1348 if (result != EXIT_SUCCESS)
1351 /* link program file */
1352 if (mode == CompileAssembleLink) {
1353 obstack_1grow(&ldflags_obst, '\0');
1354 const char *flags = obstack_finish(&ldflags_obst);
1356 /* construct commandline */
1357 obstack_printf(&file_obst, "%s", LINKER);
1358 for (file_list_entry_t *entry = files; entry != NULL;
1359 entry = entry->next) {
1360 if (entry->type != FILETYPE_OBJECT)
1363 add_flag(&file_obst, "%s", entry->name);
1366 add_flag(&file_obst, "-o");
1367 add_flag(&file_obst, outname);
1368 obstack_printf(&file_obst, "%s", flags);
1369 obstack_1grow(&file_obst, '\0');
1371 char *commandline = obstack_finish(&file_obst);
1376 int err = system(commandline);
1377 if (err != EXIT_SUCCESS) {
1378 fprintf(stderr, "linker reported an error\n");
1383 obstack_free(&cppflags_obst, NULL);
1384 obstack_free(&ldflags_obst, NULL);
1385 obstack_free(&file_obst, NULL);
1395 exit_symbol_table();