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 void print_file_name(const char *file)
348 add_flag(&ldflags_obst, "-print-file-name=%s", file);
350 obstack_1grow(&ldflags_obst, '\0');
351 const char *flags = obstack_finish(&ldflags_obst);
353 /* construct commandline */
354 obstack_printf(&ldflags_obst, "%s ", LINKER);
355 obstack_printf(&ldflags_obst, "%s", flags);
356 obstack_1grow(&ldflags_obst, '\0');
358 char *commandline = obstack_finish(&ldflags_obst);
363 int err = system(commandline);
364 if (err != EXIT_SUCCESS) {
365 fprintf(stderr, "linker reported an error\n");
370 static const char *try_dir(const char *dir)
374 if (access(dir, R_OK | W_OK | X_OK) == 0)
379 static const char *get_tempdir(void)
381 static const char *tmpdir = NULL;
387 tmpdir = try_dir(getenv("TMPDIR"));
389 tmpdir = try_dir(getenv("TMP"));
391 tmpdir = try_dir(getenv("TEMP"));
395 tmpdir = try_dir(P_tmpdir);
399 tmpdir = try_dir("/var/tmp");
401 tmpdir = try_dir("/usr/tmp");
403 tmpdir = try_dir("/tmp");
412 /* cheap and nasty mkstemp replacement */
413 static int mkstemp(char *templ)
416 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
421 * an own version of tmpnam, which: writes in a buffer, emits no warnings
422 * during linking (like glibc/gnu ld do for tmpnam)...
424 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
426 const char *tempdir = get_tempdir();
428 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
430 int fd = mkstemp(buffer);
432 fprintf(stderr, "couldn't create temporary file: %s\n",
436 FILE *out = fdopen(fd, "w");
438 fprintf(stderr, "couldn't create temporary file FILE*\n");
442 file_list_entry_t *entry = xmalloc(sizeof(*entry));
443 memset(entry, 0, sizeof(*entry));
445 size_t name_len = strlen(buffer) + 1;
446 char *name = malloc(name_len);
447 memcpy(name, buffer, name_len);
450 entry->next = temp_files;
456 static void free_temp_files(void)
458 file_list_entry_t *entry = temp_files;
459 file_list_entry_t *next;
460 for ( ; entry != NULL; entry = next) {
464 free((char*) entry->name);
471 * Do the necessary lowering for compound parameters.
473 void lower_compound_params(void)
475 lower_params_t params;
477 params.def_ptr_alignment = 4;
478 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
479 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
480 params.find_pointer_type = NULL;
481 params.ret_compound_in_regs = NULL;
482 lower_calls_with_compounds(¶ms);
485 typedef enum compile_mode_t {
499 static void usage(const char *argv0)
501 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
504 static void print_cparser_version(void) {
506 firm_get_version(&ver);
508 printf("cparser (%s) using libFirm (%u.%u",
509 cparser_REVISION, ver.major, ver.minor);
510 if (ver.revision[0] != 0) {
512 fputs(ver.revision, stdout);
514 if (ver.build[0] != 0) {
516 fputs(ver.build, stdout);
521 static void set_be_option(const char *arg)
523 int res = firm_be_option(arg);
528 static void set_option(const char *arg)
530 int res = firm_option(arg);
535 static void copy_file(FILE *dest, FILE *input)
539 while (!feof(input) && !ferror(dest)) {
540 size_t read = fread(buf, 1, sizeof(buf), input);
541 if (fwrite(buf, 1, read, dest) != read) {
542 perror("couldn't write output");
547 static inline bool streq(char const* a, char const* b)
549 return strcmp(a, b) == 0;
552 static inline bool strstart(char const* str, char const* start)
557 } while (*str++ == *start++);
561 static FILE *open_file(const char *filename)
563 if (streq(filename, "-")) {
567 FILE *in = fopen(filename, "r");
569 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
577 static filetype_t get_filetype_from_string(const char *string)
579 if (streq(string, "c") || streq(string, "c-header"))
581 if (streq(string, "c++") || streq(string, "c++-header"))
583 if (streq(string, "assembler"))
584 return FILETYPE_PREPROCESSED_ASSEMBLER;
585 if (streq(string, "assembler-with-cpp"))
586 return FILETYPE_ASSEMBLER;
587 if (streq(string, "none"))
588 return FILETYPE_AUTODETECT;
590 return FILETYPE_UNKNOWN;
593 static void init_os_support(void)
595 /* OS option must be set to the backend */
596 switch (firm_opt.os_support) {
597 case OS_SUPPORT_MINGW:
598 set_be_option("ia32-gasmode=mingw");
599 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
601 case OS_SUPPORT_LINUX:
602 set_be_option("ia32-gasmode=elf");
604 case OS_SUPPORT_MACHO:
605 set_be_option("ia32-gasmode=macho");
606 set_be_option("ia32-stackalign=4");
607 set_be_option("pic");
612 typedef enum lang_standard_t {
613 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
614 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
615 STANDARD_C89, /* ISO C90 (sic) */
616 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
617 STANDARD_C99, /* ISO C99 */
618 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
619 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
620 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
621 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
624 int main(int argc, char **argv)
628 const char *dumpfunction = NULL;
629 const char *print_file_name_file = NULL;
630 compile_mode_t mode = CompileAssembleLink;
632 int result = EXIT_SUCCESS;
633 char cpu_arch[16] = "ia32";
634 file_list_entry_t *files = NULL;
635 file_list_entry_t *last_file = NULL;
636 bool construct_dep_target = false;
637 struct obstack file_obst;
639 atexit(free_temp_files);
641 /* hack for now... */
642 if (strstr(argv[0], "pptest") != NULL) {
643 extern int pptest_main(int argc, char **argv);
644 return pptest_main(argc, argv);
647 obstack_init(&cppflags_obst);
648 obstack_init(&ldflags_obst);
649 obstack_init(&file_obst);
651 #define GET_ARG_AFTER(def, args) \
652 def = &arg[sizeof(args)-1]; \
653 if (def[0] == '\0') { \
656 fprintf(stderr, "error: expected argument after '" args "'\n"); \
657 argument_errors = true; \
661 if (def[0] == '-' && def[1] != '\0') { \
662 fprintf(stderr, "error: expected argument after '" args "'\n"); \
663 argument_errors = true; \
668 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
670 /* early options parsing (find out optimisation level and OS) */
671 for (int i = 1; i < argc; ++i) {
672 const char *arg = argv[i];
676 const char *option = &arg[1];
677 if (option[0] == 'O') {
678 sscanf(&option[1], "%d", &opt_level);
680 if (strcmp(arg, "-fwin32") == 0) {
681 firm_opt.os_support = OS_SUPPORT_MINGW;
682 } else if (strcmp(arg, "-fmac") == 0) {
683 firm_opt.os_support = OS_SUPPORT_MACHO;
684 } else if (strcmp(arg, "-flinux") == 0) {
685 firm_opt.os_support = OS_SUPPORT_LINUX;
689 /* set target/os specific stuff */
692 /* apply optimisation level */
695 set_option("no-opt");
698 set_option("no-inline");
702 set_option("strict-aliasing");
703 /* use_builtins = true; */
706 set_option("cond-eval");
707 set_option("if-conv");
710 set_option("inline");
711 set_option("deconv");
712 set_be_option("omitfp");
716 /* parse rest of options */
717 lang_standard_t standard = STANDARD_DEFAULT;
718 unsigned features_on = 0;
719 unsigned features_off = 0;
720 filetype_t forced_filetype = FILETYPE_AUTODETECT;
721 bool help_displayed = false;
722 bool argument_errors = false;
723 for (int i = 1; i < argc; ++i) {
724 const char *arg = argv[i];
725 if (arg[0] == '-' && arg[1] != '\0') {
727 const char *option = &arg[1];
728 if (option[0] == 'o') {
729 GET_ARG_AFTER(outname, "-o");
730 } else if (option[0] == 'g') {
731 set_be_option("debuginfo=stabs");
732 set_be_option("omitfp=no");
733 set_be_option("ia32-nooptcc=yes");
734 } else if (SINGLE_OPTION('c')) {
735 mode = CompileAssemble;
736 } else if (SINGLE_OPTION('E')) {
737 mode = PreprocessOnly;
738 } else if (SINGLE_OPTION('S')) {
740 } else if (option[0] == 'O') {
742 } else if (option[0] == 'I') {
744 GET_ARG_AFTER(opt, "-I");
745 add_flag(&cppflags_obst, "-I%s", opt);
746 } else if (option[0] == 'D') {
748 GET_ARG_AFTER(opt, "-D");
749 add_flag(&cppflags_obst, "-D%s", opt);
750 } else if (option[0] == 'U') {
752 GET_ARG_AFTER(opt, "-U");
753 add_flag(&cppflags_obst, "-U%s", opt);
754 } else if (option[0] == 'l') {
756 GET_ARG_AFTER(opt, "-l");
757 add_flag(&ldflags_obst, "-l%s", opt);
758 } else if (option[0] == 'L') {
760 GET_ARG_AFTER(opt, "-L");
761 add_flag(&ldflags_obst, "-L%s", opt);
762 } else if (SINGLE_OPTION('v')) {
764 } else if (SINGLE_OPTION('w')) {
765 memset(&warning, 0, sizeof(warning));
766 } else if (option[0] == 'x') {
768 GET_ARG_AFTER(opt, "-x");
769 forced_filetype = get_filetype_from_string(opt);
770 if (forced_filetype == FILETYPE_UNKNOWN) {
771 fprintf(stderr, "Unknown language '%s'\n", opt);
772 argument_errors = true;
774 } else if (streq(option, "M")) {
775 mode = PreprocessOnly;
776 add_flag(&cppflags_obst, "-M");
777 } else if (streq(option, "MMD") ||
778 streq(option, "MD")) {
779 construct_dep_target = true;
780 add_flag(&cppflags_obst, "-%s", option);
781 } else if (streq(option, "MM") ||
782 streq(option, "MP")) {
783 add_flag(&cppflags_obst, "-%s", option);
784 } else if (streq(option, "MT") ||
785 streq(option, "MQ") ||
786 streq(option, "MF")) {
788 GET_ARG_AFTER(opt, "-MT");
789 add_flag(&cppflags_obst, "-%s", option);
790 add_flag(&cppflags_obst, "%s", opt);
791 } else if (streq(option, "pipe")) {
792 /* here for gcc compatibility */
793 } else if (option[0] == 'f') {
794 char const *orig_opt;
795 GET_ARG_AFTER(orig_opt, "-f");
797 if (strstart(orig_opt, "align-loops=") ||
798 strstart(orig_opt, "align-jumps=") ||
799 strstart(orig_opt, "align-functions=")) {
800 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
802 char const *opt = orig_opt;
803 bool truth_value = true;
804 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
809 if (streq(opt, "dollars-in-identifiers")) {
810 allow_dollar_in_symbol = truth_value;
811 } if (streq(opt, "builtins")) {
812 use_builtins = truth_value;
813 } else if (streq(opt, "short-wchar")) {
814 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
816 } else if (streq(opt, "syntax-only")) {
817 mode = truth_value ? ParseOnly : CompileAssembleLink;
818 } else if (streq(opt, "omit-frame-pointer")) {
819 set_be_option(truth_value ? "omitfp" : "omitfp=no");
820 } else if (streq(opt, "strength-reduce")) {
821 firm_option(truth_value ? "strength-red" : "no-strength-red");
822 } else if (streq(opt, "fast-math") ||
823 streq(opt, "jump-tables") ||
824 streq(opt, "unroll-loops") ||
825 streq(opt, "expensive-optimizations") ||
826 streq(opt, "common") ||
828 streq(opt, "align-loops") ||
829 streq(opt, "align-jumps") ||
830 streq(opt, "align-functions")) {
831 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
833 int res = firm_option(orig_opt);
835 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
837 argument_errors = true;
839 } else if (res == -1) {
840 help_displayed = true;
844 } else if (option[0] == 'b') {
846 GET_ARG_AFTER(opt, "-b");
847 int res = firm_be_option(opt);
849 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
851 argument_errors = true;
852 } else if (res == -1) {
853 help_displayed = true;
854 } else if (strstart(opt, "isa=")) {
855 strncpy(cpu_arch, opt, sizeof(cpu_arch));
857 } else if (option[0] == 'W') {
858 if (strstart(option + 1, "p,")) {
859 // pass options directly to the preprocessor
861 GET_ARG_AFTER(opt, "-Wp,");
862 add_flag(&cppflags_obst, "-Wp,%s", opt);
863 } else if (strstart(option + 1, "l,")) {
864 // pass options directly to the linker
866 GET_ARG_AFTER(opt, "-Wl,");
867 add_flag(&ldflags_obst, "-Wl,%s", opt);
869 set_warning_opt(&option[1]);
871 } else if (option[0] == 'm') {
876 GET_ARG_AFTER(opt, "-m");
877 if (strstart(opt, "arch=")) {
878 GET_ARG_AFTER(opt, "-march=");
879 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
880 int res = firm_be_option(arch_opt);
882 argument_errors = true;
884 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
885 int res = firm_be_option(arch_opt);
887 argument_errors = true;
889 } else if (strstart(opt, "tune=")) {
890 GET_ARG_AFTER(opt, "-mtune=");
891 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
892 int res = firm_be_option(arch_opt);
894 argument_errors = true;
895 } else if (strstart(opt, "cpu=")) {
896 GET_ARG_AFTER(opt, "-mcpu=");
897 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
898 int res = firm_be_option(arch_opt);
900 argument_errors = true;
901 } else if (strstart(opt, "fpmath=")) {
902 GET_ARG_AFTER(opt, "-mfpmath=");
903 if (streq(opt, "387"))
905 else if (streq(opt, "sse"))
908 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
909 argument_errors = true;
911 if (!argument_errors) {
912 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
913 int res = firm_be_option(arch_opt);
915 argument_errors = true;
917 } else if (strstart(opt, "preferred-stack-boundary=")) {
918 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
919 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
920 int res = firm_be_option(arch_opt);
922 argument_errors = true;
923 } else if (streq(opt, "omit-leaf-frame-pointer")) {
924 set_be_option("omitleaffp=1");
925 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
926 set_be_option("omitleaffp=0");
929 long int value = strtol(opt, &endptr, 10);
930 if (*endptr != '\0') {
931 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
932 argument_errors = true;
934 if (value != 16 && value != 32 && value != 64) {
935 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
936 argument_errors = true;
938 machine_size = (unsigned int)value;
941 } else if (streq(option, "pg")) {
942 set_be_option("gprof");
943 add_flag(&ldflags_obst, "-pg");
944 } else if (streq(option, "pedantic") ||
945 streq(option, "ansi")) {
946 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
947 } else if (streq(option, "shared")) {
948 add_flag(&ldflags_obst, "-shared");
949 } else if (strstart(option, "std=")) {
950 const char *const o = &option[4];
952 streq(o, "c++") ? STANDARD_CXX98 :
953 streq(o, "c++98") ? STANDARD_CXX98 :
954 streq(o, "c89") ? STANDARD_C89 :
955 streq(o, "c99") ? STANDARD_C99 :
956 streq(o, "c9x") ? STANDARD_C99 : // deprecated
957 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
958 streq(o, "gnu89") ? STANDARD_GNU89 :
959 streq(o, "gnu99") ? STANDARD_GNU99 :
960 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
961 streq(o, "iso9899:1990") ? STANDARD_C89 :
962 streq(o, "iso9899:199409") ? STANDARD_C90 :
963 streq(o, "iso9899:1999") ? STANDARD_C99 :
964 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
965 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
966 } else if (streq(option, "version")) {
967 print_cparser_version();
968 } else if (strstart(option, "print-file-name=")) {
969 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
970 } else if (option[0] == '-') {
971 /* double dash option */
973 if (streq(option, "gcc")) {
974 features_on |= _GNUC;
975 features_off &= ~_GNUC;
976 } else if (streq(option, "no-gcc")) {
977 features_on &= ~_GNUC;
978 features_off |= _GNUC;
979 } else if (streq(option, "ms")) {
981 features_off &= ~_MS;
982 } else if (streq(option, "no-ms")) {
985 } else if (streq(option, "signed-chars")) {
986 char_is_signed = true;
987 } else if (streq(option, "unsigned-chars")) {
988 char_is_signed = false;
989 } else if (streq(option, "strict")) {
991 } else if (streq(option, "lextest")) {
993 } else if (streq(option, "benchmark")) {
994 mode = BenchmarkParser;
995 } else if (streq(option, "print-ast")) {
997 } else if (streq(option, "print-implicit-cast")) {
998 print_implicit_casts = true;
999 } else if (streq(option, "print-parenthesis")) {
1000 print_parenthesis = true;
1001 } else if (streq(option, "print-fluffy")) {
1003 } else if (streq(option, "print-caml")) {
1005 } else if (streq(option, "version")) {
1006 print_cparser_version();
1008 } else if (streq(option, "dump-function")) {
1011 fprintf(stderr, "error: "
1012 "expected argument after '--dump-function'\n");
1013 argument_errors = true;
1016 dumpfunction = argv[i];
1019 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1020 argument_errors = true;
1023 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1024 argument_errors = true;
1027 filetype_t type = forced_filetype;
1028 if (type == FILETYPE_AUTODETECT) {
1029 if (streq(arg, "-")) {
1030 /* - implicitly means C source file */
1033 const char *suffix = strrchr(arg, '.');
1034 /* Ensure there is at least one char before the suffix */
1035 if (suffix != NULL && suffix != arg) {
1038 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1039 streq(suffix, "a") ? FILETYPE_OBJECT :
1040 streq(suffix, "c") ? FILETYPE_C :
1041 streq(suffix, "cc") ? FILETYPE_CXX :
1042 streq(suffix, "cpp") ? FILETYPE_CXX :
1043 streq(suffix, "cxx") ? FILETYPE_CXX :
1044 streq(suffix, "h") ? FILETYPE_C :
1045 streq(suffix, "o") ? FILETYPE_OBJECT :
1046 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1047 streq(suffix, "so") ? FILETYPE_OBJECT :
1048 FILETYPE_AUTODETECT;
1052 if (type == FILETYPE_AUTODETECT) {
1053 fprintf(stderr, "'%s': file format not recognized\n", arg);
1058 file_list_entry_t *entry
1059 = obstack_alloc(&file_obst, sizeof(entry[0]));
1060 memset(entry, 0, sizeof(entry[0]));
1064 if (last_file != NULL) {
1065 last_file->next = entry;
1073 if (print_file_name_file != NULL) {
1074 print_file_name(print_file_name_file);
1078 if (files == NULL) {
1079 fprintf(stderr, "error: no input files specified\n");
1080 argument_errors = true;
1083 if (help_displayed) {
1084 return !argument_errors;
1086 if (argument_errors) {
1091 /* we do the lowering in ast2firm */
1092 firm_opt.lower_bitfields = FALSE;
1095 init_symbol_table();
1105 if (construct_dep_target) {
1106 if (outname != 0 && strlen(outname) >= 2) {
1107 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1109 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1112 dep_target[0] = '\0';
1115 char outnamebuf[4096];
1116 if (outname == NULL) {
1117 const char *filename = files->name;
1120 case BenchmarkParser:
1125 case PreprocessOnly:
1130 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1131 outname = outnamebuf;
1133 case CompileAssemble:
1134 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1135 outname = outnamebuf;
1138 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1140 outname = outnamebuf;
1142 case CompileAssembleLink:
1152 assert(outname != NULL);
1155 if (streq(outname, "-")) {
1158 out = fopen(outname, "w");
1160 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1166 file_list_entry_t *file;
1167 for (file = files; file != NULL; file = file->next) {
1168 char asm_tempfile[1024];
1169 const char *filename = file->name;
1170 filetype_t filetype = file->type;
1172 if (filetype == FILETYPE_OBJECT)
1176 if (mode == LexTest) {
1178 in = open_file(filename);
1179 lextest(in, filename);
1184 FILE *preprocessed_in = NULL;
1186 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1187 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1188 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1190 /* no support for input on FILE* yet */
1192 panic("internal compiler error: in for preprocessor != NULL");
1194 preprocessed_in = preprocess(filename);
1195 if (mode == PreprocessOnly) {
1196 copy_file(out, preprocessed_in);
1197 int result = pclose(preprocessed_in);
1202 in = preprocessed_in;
1210 if (mode == Compile) {
1213 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1217 in = open_file(filename);
1219 /* preprocess and compile */
1220 if (filetype == FILETYPE_PREPROCESSED_C) {
1221 char const* invalid_mode;
1224 case STANDARD_C89: c_mode = _C89; break;
1225 /* TODO ^v determine difference between these two */
1226 case STANDARD_C90: c_mode = _C89; break;
1227 case STANDARD_C99: c_mode = _C89 | _C99; break;
1228 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1232 "warning: command line option \"-std=%s\" is not valid for C\n",
1235 case STANDARD_DEFAULT:
1236 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1238 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1239 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1242 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1243 char const* invalid_mode;
1245 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1246 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1247 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1248 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1249 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1252 case STANDARD_CXX98: c_mode = _CXX; break;
1256 "warning: command line option \"-std=%s\" is not valid for C++\n",
1258 case STANDARD_DEFAULT:
1259 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1263 c_mode |= features_on;
1264 c_mode &= ~features_off;
1266 translation_unit_t *const unit = do_parsing(in, filename);
1268 /* prints the AST even if errors occurred */
1269 if (mode == PrintAst) {
1270 type_set_output(out);
1271 ast_set_output(out);
1275 if (error_count > 0) {
1276 /* parsing failed because of errors */
1277 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1279 result = EXIT_FAILURE;
1281 } else if (warning_count > 0) {
1282 fprintf(stderr, "%u warning(s)\n", warning_count);
1285 if (in == preprocessed_in) {
1286 int pp_result = pclose(preprocessed_in);
1287 if (pp_result != EXIT_SUCCESS) {
1292 if (mode == BenchmarkParser) {
1294 } else if (mode == PrintFluffy) {
1295 write_fluffy_decls(out, unit);
1297 } else if (mode == PrintCaml) {
1298 write_caml_decls(out, unit);
1302 translation_unit_to_firm(unit);
1304 if (mode == ParseOnly) {
1308 if (mode == CompileDump) {
1310 ident *id = new_id_from_str(dumpfunction);
1311 ir_graph *irg = NULL;
1312 int n_irgs = get_irp_n_irgs();
1313 for (int i = 0; i < n_irgs; ++i) {
1314 ir_graph *tirg = get_irp_irg(i);
1315 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1323 fprintf(stderr, "No graph for function '%s' found\n",
1328 dump_ir_block_graph_file(irg, out);
1333 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1334 have_const_functions);
1335 if (asm_out != out) {
1338 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1339 copy_file(asm_out, in);
1340 if (in == preprocessed_in) {
1341 int pp_result = pclose(preprocessed_in);
1342 if (pp_result != EXIT_SUCCESS) {
1346 if (asm_out != out) {
1351 if (mode == Compile)
1354 /* if we're here then we have preprocessed assembly */
1355 filename = asm_tempfile;
1356 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1359 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1361 const char *filename_o;
1362 if (mode == CompileAssemble) {
1364 filename_o = outname;
1366 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1371 assemble(filename_o, filename);
1373 size_t len = strlen(filename_o) + 1;
1374 filename = obstack_copy(&file_obst, filename_o, len);
1375 filetype = FILETYPE_OBJECT;
1378 /* ok we're done here, process next file */
1379 file->name = filename;
1380 file->type = filetype;
1383 if (result != EXIT_SUCCESS)
1386 /* link program file */
1387 if (mode == CompileAssembleLink) {
1388 obstack_1grow(&ldflags_obst, '\0');
1389 const char *flags = obstack_finish(&ldflags_obst);
1391 /* construct commandline */
1392 obstack_printf(&file_obst, "%s", LINKER);
1393 for (file_list_entry_t *entry = files; entry != NULL;
1394 entry = entry->next) {
1395 if (entry->type != FILETYPE_OBJECT)
1398 add_flag(&file_obst, "%s", entry->name);
1401 add_flag(&file_obst, "-o");
1402 add_flag(&file_obst, outname);
1403 obstack_printf(&file_obst, "%s", flags);
1404 obstack_1grow(&file_obst, '\0');
1406 char *commandline = obstack_finish(&file_obst);
1411 int err = system(commandline);
1412 if (err != EXIT_SUCCESS) {
1413 fprintf(stderr, "linker reported an error\n");
1418 obstack_free(&cppflags_obst, NULL);
1419 obstack_free(&ldflags_obst, NULL);
1420 obstack_free(&file_obst, NULL);
1430 exit_symbol_table();