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) {
275 obstack_1grow(obst, '\\');
278 obstack_1grow(obst, *c);
285 static const char *type_to_string(type_t *type)
287 assert(type->kind == TYPE_ATOMIC);
288 return get_atomic_kind_name(type->atomic.akind);
291 static FILE *preprocess(const char *fname)
293 obstack_1grow(&cppflags_obst, '\0');
294 const char *flags = obstack_finish(&cppflags_obst);
296 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
298 /* setup default defines */
299 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
300 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
301 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
302 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
305 add_flag(&cppflags_obst, "-D__builtin_abort=abort");
306 add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
307 add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
308 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
309 add_flag(&cppflags_obst, "-D__builtin_memset=memset");
311 /* handle dependency generation */
312 if (dep_target[0] != '\0') {
313 add_flag(&cppflags_obst, "-MF");
314 add_flag(&cppflags_obst, dep_target);
315 if (outname != NULL) {
316 add_flag(&cppflags_obst, "-MQ");
317 add_flag(&cppflags_obst, outname);
320 if (flags[0] != '\0') {
321 obstack_printf(&cppflags_obst, " %s", flags);
323 add_flag(&cppflags_obst, fname);
325 obstack_1grow(&cppflags_obst, '\0');
326 const char *buf = obstack_finish(&cppflags_obst);
331 FILE *f = popen(buf, "r");
333 fprintf(stderr, "invoking preprocessor failed\n");
340 static void assemble(const char *out, const char *in)
344 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
349 int err = system(buf);
351 fprintf(stderr, "assembler reported an error\n");
356 static void print_file_name(const char *file)
358 add_flag(&ldflags_obst, "-print-file-name=%s", file);
360 obstack_1grow(&ldflags_obst, '\0');
361 const char *flags = obstack_finish(&ldflags_obst);
363 /* construct commandline */
364 obstack_printf(&ldflags_obst, "%s ", LINKER);
365 obstack_printf(&ldflags_obst, "%s", flags);
366 obstack_1grow(&ldflags_obst, '\0');
368 char *commandline = obstack_finish(&ldflags_obst);
373 int err = system(commandline);
374 if (err != EXIT_SUCCESS) {
375 fprintf(stderr, "linker reported an error\n");
380 static const char *try_dir(const char *dir)
384 if (access(dir, R_OK | W_OK | X_OK) == 0)
389 static const char *get_tempdir(void)
391 static const char *tmpdir = NULL;
397 tmpdir = try_dir(getenv("TMPDIR"));
399 tmpdir = try_dir(getenv("TMP"));
401 tmpdir = try_dir(getenv("TEMP"));
405 tmpdir = try_dir(P_tmpdir);
409 tmpdir = try_dir("/var/tmp");
411 tmpdir = try_dir("/usr/tmp");
413 tmpdir = try_dir("/tmp");
422 /* cheap and nasty mkstemp replacement */
423 static int mkstemp(char *templ)
426 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
431 * an own version of tmpnam, which: writes in a buffer, emits no warnings
432 * during linking (like glibc/gnu ld do for tmpnam)...
434 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
436 const char *tempdir = get_tempdir();
438 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
440 int fd = mkstemp(buffer);
442 fprintf(stderr, "couldn't create temporary file: %s\n",
446 FILE *out = fdopen(fd, "w");
448 fprintf(stderr, "couldn't create temporary file FILE*\n");
452 file_list_entry_t *entry = xmalloc(sizeof(*entry));
453 memset(entry, 0, sizeof(*entry));
455 size_t name_len = strlen(buffer) + 1;
456 char *name = malloc(name_len);
457 memcpy(name, buffer, name_len);
460 entry->next = temp_files;
466 static void free_temp_files(void)
468 file_list_entry_t *entry = temp_files;
469 file_list_entry_t *next;
470 for ( ; entry != NULL; entry = next) {
474 free((char*) entry->name);
481 * Do the necessary lowering for compound parameters.
483 void lower_compound_params(void)
485 lower_params_t params;
487 params.def_ptr_alignment = 4;
488 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
489 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
490 params.find_pointer_type = NULL;
491 params.ret_compound_in_regs = NULL;
492 lower_calls_with_compounds(¶ms);
495 typedef enum compile_mode_t {
509 static void usage(const char *argv0)
511 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
514 static void print_cparser_version(void) {
516 firm_get_version(&ver);
518 printf("cparser (%s) using libFirm (%u.%u",
519 cparser_REVISION, ver.major, ver.minor);
520 if (ver.revision[0] != 0) {
522 fputs(ver.revision, stdout);
524 if (ver.build[0] != 0) {
526 fputs(ver.build, stdout);
531 static void set_be_option(const char *arg)
533 int res = firm_be_option(arg);
538 static void set_option(const char *arg)
540 int res = firm_option(arg);
545 static void copy_file(FILE *dest, FILE *input)
549 while (!feof(input) && !ferror(dest)) {
550 size_t read = fread(buf, 1, sizeof(buf), input);
551 if (fwrite(buf, 1, read, dest) != read) {
552 perror("couldn't write output");
557 static inline bool streq(char const* a, char const* b)
559 return strcmp(a, b) == 0;
562 static inline bool strstart(char const* str, char const* start)
567 } while (*str++ == *start++);
571 static FILE *open_file(const char *filename)
573 if (streq(filename, "-")) {
577 FILE *in = fopen(filename, "r");
579 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
587 static filetype_t get_filetype_from_string(const char *string)
589 if (streq(string, "c") || streq(string, "c-header"))
591 if (streq(string, "c++") || streq(string, "c++-header"))
593 if (streq(string, "assembler"))
594 return FILETYPE_PREPROCESSED_ASSEMBLER;
595 if (streq(string, "assembler-with-cpp"))
596 return FILETYPE_ASSEMBLER;
597 if (streq(string, "none"))
598 return FILETYPE_AUTODETECT;
600 return FILETYPE_UNKNOWN;
603 static void init_os_support(void)
605 /* OS option must be set to the backend */
606 switch (firm_opt.os_support) {
607 case OS_SUPPORT_MINGW:
608 set_be_option("ia32-gasmode=mingw");
609 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
611 case OS_SUPPORT_LINUX:
612 set_be_option("ia32-gasmode=elf");
614 case OS_SUPPORT_MACHO:
615 set_be_option("ia32-gasmode=macho");
616 set_be_option("ia32-stackalign=4");
617 set_be_option("pic");
622 typedef enum lang_standard_t {
623 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
624 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
625 STANDARD_C89, /* ISO C90 (sic) */
626 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
627 STANDARD_C99, /* ISO C99 */
628 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
629 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
630 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
631 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
634 int main(int argc, char **argv)
638 const char *dumpfunction = NULL;
639 const char *print_file_name_file = NULL;
640 compile_mode_t mode = CompileAssembleLink;
642 int result = EXIT_SUCCESS;
643 char cpu_arch[16] = "ia32";
644 file_list_entry_t *files = NULL;
645 file_list_entry_t *last_file = NULL;
646 bool construct_dep_target = false;
647 struct obstack file_obst;
649 atexit(free_temp_files);
651 /* hack for now... */
652 if (strstr(argv[0], "pptest") != NULL) {
653 extern int pptest_main(int argc, char **argv);
654 return pptest_main(argc, argv);
657 obstack_init(&cppflags_obst);
658 obstack_init(&ldflags_obst);
659 obstack_init(&file_obst);
661 #define GET_ARG_AFTER(def, args) \
662 def = &arg[sizeof(args)-1]; \
663 if (def[0] == '\0') { \
666 fprintf(stderr, "error: expected argument after '" args "'\n"); \
667 argument_errors = true; \
671 if (def[0] == '-' && def[1] != '\0') { \
672 fprintf(stderr, "error: expected argument after '" args "'\n"); \
673 argument_errors = true; \
678 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
680 /* early options parsing (find out optimisation level and OS) */
681 for (int i = 1; i < argc; ++i) {
682 const char *arg = argv[i];
686 const char *option = &arg[1];
687 if (option[0] == 'O') {
688 sscanf(&option[1], "%d", &opt_level);
690 if (strcmp(arg, "-fwin32") == 0) {
691 firm_opt.os_support = OS_SUPPORT_MINGW;
692 } else if (strcmp(arg, "-fmac") == 0) {
693 firm_opt.os_support = OS_SUPPORT_MACHO;
694 } else if (strcmp(arg, "-flinux") == 0) {
695 firm_opt.os_support = OS_SUPPORT_LINUX;
699 /* set target/os specific stuff */
702 /* apply optimisation level */
705 set_option("no-opt");
708 set_option("no-inline");
712 set_option("strict-aliasing");
713 /* use_builtins = true; */
716 set_option("cond-eval");
717 set_option("if-conv");
720 set_option("inline");
721 set_option("deconv");
722 set_be_option("omitfp");
726 /* parse rest of options */
727 lang_standard_t standard = STANDARD_DEFAULT;
728 unsigned features_on = 0;
729 unsigned features_off = 0;
730 filetype_t forced_filetype = FILETYPE_AUTODETECT;
731 bool help_displayed = false;
732 bool argument_errors = false;
733 for (int i = 1; i < argc; ++i) {
734 const char *arg = argv[i];
735 if (arg[0] == '-' && arg[1] != '\0') {
737 const char *option = &arg[1];
738 if (option[0] == 'o') {
739 GET_ARG_AFTER(outname, "-o");
740 } else if (option[0] == 'g') {
741 set_be_option("debuginfo=stabs");
742 set_be_option("omitfp=no");
743 set_be_option("ia32-nooptcc=yes");
744 } else if (SINGLE_OPTION('c')) {
745 mode = CompileAssemble;
746 } else if (SINGLE_OPTION('E')) {
747 mode = PreprocessOnly;
748 } else if (SINGLE_OPTION('S')) {
750 } else if (option[0] == 'O') {
752 } else if (option[0] == 'I') {
754 GET_ARG_AFTER(opt, "-I");
755 add_flag(&cppflags_obst, "-I%s", opt);
756 } else if (option[0] == 'D') {
758 GET_ARG_AFTER(opt, "-D");
759 add_flag(&cppflags_obst, "-D%s", opt);
760 } else if (option[0] == 'U') {
762 GET_ARG_AFTER(opt, "-U");
763 add_flag(&cppflags_obst, "-U%s", opt);
764 } else if (option[0] == 'l') {
766 GET_ARG_AFTER(opt, "-l");
767 add_flag(&ldflags_obst, "-l%s", opt);
768 } else if (option[0] == 'L') {
770 GET_ARG_AFTER(opt, "-L");
771 add_flag(&ldflags_obst, "-L%s", opt);
772 } else if (SINGLE_OPTION('v')) {
774 } else if (SINGLE_OPTION('w')) {
775 memset(&warning, 0, sizeof(warning));
776 } else if (option[0] == 'x') {
778 GET_ARG_AFTER(opt, "-x");
779 forced_filetype = get_filetype_from_string(opt);
780 if (forced_filetype == FILETYPE_UNKNOWN) {
781 fprintf(stderr, "Unknown language '%s'\n", opt);
782 argument_errors = true;
784 } else if (streq(option, "M")) {
785 mode = PreprocessOnly;
786 add_flag(&cppflags_obst, "-M");
787 } else if (streq(option, "MMD") ||
788 streq(option, "MD")) {
789 construct_dep_target = true;
790 add_flag(&cppflags_obst, "-%s", option);
791 } else if (streq(option, "MM") ||
792 streq(option, "MP")) {
793 add_flag(&cppflags_obst, "-%s", option);
794 } else if (streq(option, "MT") ||
795 streq(option, "MQ") ||
796 streq(option, "MF")) {
798 GET_ARG_AFTER(opt, "-MT");
799 add_flag(&cppflags_obst, "-%s", option);
800 add_flag(&cppflags_obst, "%s", opt);
801 } else if (streq(option, "include")) {
803 GET_ARG_AFTER(opt, "-include");
804 add_flag(&cppflags_obst, "-include");
805 add_flag(&cppflags_obst, "%s", opt);
806 } else if (streq(option, "isystem")) {
808 GET_ARG_AFTER(opt, "-isystem");
809 add_flag(&cppflags_obst, "-isystem");
810 add_flag(&cppflags_obst, "%s", opt);
811 } else if (streq(option, "nostdinc")
812 || streq(option, "trigraphs")) {
813 /* pass these through to the preprocessor */
814 add_flag(&cppflags_obst, "%s", arg);
815 } else if (streq(option, "pipe")) {
816 /* here for gcc compatibility */
817 } else if (option[0] == 'f') {
818 char const *orig_opt;
819 GET_ARG_AFTER(orig_opt, "-f");
821 if (strstart(orig_opt, "align-loops=") ||
822 strstart(orig_opt, "align-jumps=") ||
823 strstart(orig_opt, "align-functions=")) {
824 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
825 } else if (streq(orig_opt, "verbose-asm")) {
826 /* ignore: we always print verbose assembler */
828 char const *opt = orig_opt;
829 bool truth_value = true;
830 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
835 if (streq(opt, "dollars-in-identifiers")) {
836 allow_dollar_in_symbol = truth_value;
837 } if (streq(opt, "builtins")) {
838 use_builtins = truth_value;
839 } else if (streq(opt, "short-wchar")) {
840 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
842 } else if (streq(opt, "syntax-only")) {
843 mode = truth_value ? ParseOnly : CompileAssembleLink;
844 } else if (streq(opt, "omit-frame-pointer")) {
845 set_be_option(truth_value ? "omitfp" : "omitfp=no");
846 } else if (streq(opt, "strength-reduce")) {
847 firm_option(truth_value ? "strength-red" : "no-strength-red");
848 } else if (streq(opt, "fast-math") ||
849 streq(opt, "jump-tables") ||
850 streq(opt, "unroll-loops") ||
851 streq(opt, "expensive-optimizations") ||
852 streq(opt, "common") ||
854 streq(opt, "align-loops") ||
855 streq(opt, "align-jumps") ||
856 streq(opt, "align-functions")) {
857 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
859 int res = firm_option(orig_opt);
861 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
863 argument_errors = true;
865 } else if (res == -1) {
866 help_displayed = true;
870 } else if (option[0] == 'b') {
872 GET_ARG_AFTER(opt, "-b");
873 int res = firm_be_option(opt);
875 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
877 argument_errors = true;
878 } else if (res == -1) {
879 help_displayed = true;
880 } else if (strstart(opt, "isa=")) {
881 strncpy(cpu_arch, opt, sizeof(cpu_arch));
883 } else if (option[0] == 'W') {
884 if (strstart(option + 1, "p,")) {
885 // pass options directly to the preprocessor
887 GET_ARG_AFTER(opt, "-Wp,");
888 add_flag(&cppflags_obst, "-Wp,%s", opt);
889 } else if (strstart(option + 1, "l,")) {
890 // pass options directly to the linker
892 GET_ARG_AFTER(opt, "-Wl,");
893 add_flag(&ldflags_obst, "-Wl,%s", opt);
894 } else if (streq(option + 1, "no-trigraphs")
895 || streq(option + 1, "undef")) {
896 add_flag(&cppflags_obst, "%s", arg);
898 set_warning_opt(&option[1]);
900 } else if (option[0] == 'm') {
905 GET_ARG_AFTER(opt, "-m");
906 if (strstart(opt, "arch=")) {
907 GET_ARG_AFTER(opt, "-march=");
908 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
909 int res = firm_be_option(arch_opt);
911 argument_errors = true;
913 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
914 int res = firm_be_option(arch_opt);
916 argument_errors = true;
918 } else if (strstart(opt, "tune=")) {
919 GET_ARG_AFTER(opt, "-mtune=");
920 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
921 int res = firm_be_option(arch_opt);
923 argument_errors = true;
924 } else if (strstart(opt, "cpu=")) {
925 GET_ARG_AFTER(opt, "-mcpu=");
926 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
927 int res = firm_be_option(arch_opt);
929 argument_errors = true;
930 } else if (strstart(opt, "fpmath=")) {
931 GET_ARG_AFTER(opt, "-mfpmath=");
932 if (streq(opt, "387"))
934 else if (streq(opt, "sse"))
937 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
938 argument_errors = true;
940 if (!argument_errors) {
941 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
942 int res = firm_be_option(arch_opt);
944 argument_errors = true;
946 } else if (strstart(opt, "preferred-stack-boundary=")) {
947 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
948 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
949 int res = firm_be_option(arch_opt);
951 argument_errors = true;
952 } else if (streq(opt, "omit-leaf-frame-pointer")) {
953 set_be_option("omitleaffp=1");
954 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
955 set_be_option("omitleaffp=0");
958 long int value = strtol(opt, &endptr, 10);
959 if (*endptr != '\0') {
960 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
961 argument_errors = true;
963 if (value != 16 && value != 32 && value != 64) {
964 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
965 argument_errors = true;
967 machine_size = (unsigned int)value;
970 } else if (streq(option, "pg")) {
971 set_be_option("gprof");
972 add_flag(&ldflags_obst, "-pg");
973 } else if (streq(option, "pedantic") ||
974 streq(option, "ansi")) {
975 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
976 } else if (streq(option, "shared")) {
977 add_flag(&ldflags_obst, "-shared");
978 } else if (strstart(option, "std=")) {
979 const char *const o = &option[4];
981 streq(o, "c++") ? STANDARD_CXX98 :
982 streq(o, "c++98") ? STANDARD_CXX98 :
983 streq(o, "c89") ? STANDARD_C89 :
984 streq(o, "c99") ? STANDARD_C99 :
985 streq(o, "c9x") ? STANDARD_C99 : // deprecated
986 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
987 streq(o, "gnu89") ? STANDARD_GNU89 :
988 streq(o, "gnu99") ? STANDARD_GNU99 :
989 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
990 streq(o, "iso9899:1990") ? STANDARD_C89 :
991 streq(o, "iso9899:199409") ? STANDARD_C90 :
992 streq(o, "iso9899:1999") ? STANDARD_C99 :
993 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
994 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
995 } else if (streq(option, "version")) {
996 print_cparser_version();
997 } else if (strstart(option, "print-file-name=")) {
998 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
999 } else if (option[0] == '-') {
1000 /* double dash option */
1002 if (streq(option, "gcc")) {
1003 features_on |= _GNUC;
1004 features_off &= ~_GNUC;
1005 } else if (streq(option, "no-gcc")) {
1006 features_on &= ~_GNUC;
1007 features_off |= _GNUC;
1008 } else if (streq(option, "ms")) {
1010 features_off &= ~_MS;
1011 } else if (streq(option, "no-ms")) {
1012 features_on &= ~_MS;
1013 features_off |= _MS;
1014 } else if (streq(option, "signed-chars")) {
1015 char_is_signed = true;
1016 } else if (streq(option, "unsigned-chars")) {
1017 char_is_signed = false;
1018 } else if (streq(option, "strict")) {
1020 } else if (streq(option, "lextest")) {
1022 } else if (streq(option, "benchmark")) {
1023 mode = BenchmarkParser;
1024 } else if (streq(option, "print-ast")) {
1026 } else if (streq(option, "print-implicit-cast")) {
1027 print_implicit_casts = true;
1028 } else if (streq(option, "print-parenthesis")) {
1029 print_parenthesis = true;
1030 } else if (streq(option, "print-fluffy")) {
1032 } else if (streq(option, "print-caml")) {
1034 } else if (streq(option, "version")) {
1035 print_cparser_version();
1037 } else if (streq(option, "dump-function")) {
1040 fprintf(stderr, "error: "
1041 "expected argument after '--dump-function'\n");
1042 argument_errors = true;
1045 dumpfunction = argv[i];
1048 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1049 argument_errors = true;
1052 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1053 argument_errors = true;
1056 filetype_t type = forced_filetype;
1057 if (type == FILETYPE_AUTODETECT) {
1058 if (streq(arg, "-")) {
1059 /* - implicitly means C source file */
1062 const char *suffix = strrchr(arg, '.');
1063 /* Ensure there is at least one char before the suffix */
1064 if (suffix != NULL && suffix != arg) {
1067 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1068 streq(suffix, "a") ? FILETYPE_OBJECT :
1069 streq(suffix, "c") ? FILETYPE_C :
1070 streq(suffix, "cc") ? FILETYPE_CXX :
1071 streq(suffix, "cpp") ? FILETYPE_CXX :
1072 streq(suffix, "cxx") ? FILETYPE_CXX :
1073 streq(suffix, "h") ? FILETYPE_C :
1074 streq(suffix, "o") ? FILETYPE_OBJECT :
1075 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1076 streq(suffix, "so") ? FILETYPE_OBJECT :
1077 FILETYPE_AUTODETECT;
1081 if (type == FILETYPE_AUTODETECT) {
1082 fprintf(stderr, "'%s': file format not recognized\n", arg);
1087 file_list_entry_t *entry
1088 = obstack_alloc(&file_obst, sizeof(entry[0]));
1089 memset(entry, 0, sizeof(entry[0]));
1093 if (last_file != NULL) {
1094 last_file->next = entry;
1102 if (print_file_name_file != NULL) {
1103 print_file_name(print_file_name_file);
1107 if (files == NULL) {
1108 fprintf(stderr, "error: no input files specified\n");
1109 argument_errors = true;
1112 if (help_displayed) {
1113 return !argument_errors;
1115 if (argument_errors) {
1120 /* we do the lowering in ast2firm */
1121 firm_opt.lower_bitfields = FALSE;
1124 init_symbol_table();
1134 if (construct_dep_target) {
1135 if (outname != 0 && strlen(outname) >= 2) {
1136 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1138 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1141 dep_target[0] = '\0';
1144 char outnamebuf[4096];
1145 if (outname == NULL) {
1146 const char *filename = files->name;
1149 case BenchmarkParser:
1154 case PreprocessOnly:
1159 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1160 outname = outnamebuf;
1162 case CompileAssemble:
1163 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1164 outname = outnamebuf;
1167 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1169 outname = outnamebuf;
1171 case CompileAssembleLink:
1181 assert(outname != NULL);
1184 if (streq(outname, "-")) {
1187 out = fopen(outname, "w");
1189 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1195 file_list_entry_t *file;
1196 for (file = files; file != NULL; file = file->next) {
1197 char asm_tempfile[1024];
1198 const char *filename = file->name;
1199 filetype_t filetype = file->type;
1201 if (filetype == FILETYPE_OBJECT)
1205 if (mode == LexTest) {
1207 in = open_file(filename);
1208 lextest(in, filename);
1213 FILE *preprocessed_in = NULL;
1216 filetype = FILETYPE_PREPROCESSED_C;
1219 filetype = FILETYPE_PREPROCESSED_CXX;
1221 case FILETYPE_ASSEMBLER:
1222 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1223 add_flag(&cppflags_obst, "-x");
1224 add_flag(&cppflags_obst, "assembler-with-cpp");
1227 /* no support for input on FILE* yet */
1229 panic("internal compiler error: in for preprocessor != NULL");
1231 preprocessed_in = preprocess(filename);
1232 if (mode == PreprocessOnly) {
1233 copy_file(out, preprocessed_in);
1234 int result = pclose(preprocessed_in);
1236 /* remove output file in case of error */
1237 if (out != stdout && result != EXIT_SUCCESS) {
1243 in = preprocessed_in;
1251 if (mode == Compile) {
1254 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1258 in = open_file(filename);
1260 /* preprocess and compile */
1261 if (filetype == FILETYPE_PREPROCESSED_C) {
1262 char const* invalid_mode;
1265 case STANDARD_C89: c_mode = _C89; break;
1266 /* TODO ^v determine difference between these two */
1267 case STANDARD_C90: c_mode = _C89; break;
1268 case STANDARD_C99: c_mode = _C89 | _C99; break;
1269 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1273 "warning: command line option \"-std=%s\" is not valid for C\n",
1276 case STANDARD_DEFAULT:
1277 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1279 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1280 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1283 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1284 char const* invalid_mode;
1286 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1287 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1288 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1289 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1290 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1293 case STANDARD_CXX98: c_mode = _CXX; break;
1297 "warning: command line option \"-std=%s\" is not valid for C++\n",
1299 case STANDARD_DEFAULT:
1300 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1304 c_mode |= features_on;
1305 c_mode &= ~features_off;
1307 translation_unit_t *const unit = do_parsing(in, filename);
1309 /* prints the AST even if errors occurred */
1310 if (mode == PrintAst) {
1311 type_set_output(out);
1312 ast_set_output(out);
1316 if (error_count > 0) {
1317 /* parsing failed because of errors */
1318 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1320 result = EXIT_FAILURE;
1322 } else if (warning_count > 0) {
1323 fprintf(stderr, "%u warning(s)\n", warning_count);
1326 if (in == preprocessed_in) {
1327 int pp_result = pclose(preprocessed_in);
1328 if (pp_result != EXIT_SUCCESS) {
1329 /* remove output file */
1336 if (mode == BenchmarkParser) {
1338 } else if (mode == PrintFluffy) {
1339 write_fluffy_decls(out, unit);
1341 } else if (mode == PrintCaml) {
1342 write_caml_decls(out, unit);
1346 translation_unit_to_firm(unit);
1348 if (mode == ParseOnly) {
1352 if (mode == CompileDump) {
1354 ident *id = new_id_from_str(dumpfunction);
1355 ir_graph *irg = NULL;
1356 int n_irgs = get_irp_n_irgs();
1357 for (int i = 0; i < n_irgs; ++i) {
1358 ir_graph *tirg = get_irp_irg(i);
1359 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1367 fprintf(stderr, "No graph for function '%s' found\n",
1372 dump_ir_block_graph_file(irg, out);
1377 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1378 have_const_functions);
1379 if (asm_out != out) {
1382 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1383 copy_file(asm_out, in);
1384 if (in == preprocessed_in) {
1385 int pp_result = pclose(preprocessed_in);
1386 if (pp_result != EXIT_SUCCESS) {
1387 /* remove output in error case */
1393 if (asm_out != out) {
1398 if (mode == Compile)
1401 /* if we're here then we have preprocessed assembly */
1402 filename = asm_tempfile;
1403 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1406 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1408 const char *filename_o;
1409 if (mode == CompileAssemble) {
1411 filename_o = outname;
1413 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1418 assemble(filename_o, filename);
1420 size_t len = strlen(filename_o) + 1;
1421 filename = obstack_copy(&file_obst, filename_o, len);
1422 filetype = FILETYPE_OBJECT;
1425 /* ok we're done here, process next file */
1426 file->name = filename;
1427 file->type = filetype;
1430 if (result != EXIT_SUCCESS) {
1436 /* link program file */
1437 if (mode == CompileAssembleLink) {
1438 obstack_1grow(&ldflags_obst, '\0');
1439 const char *flags = obstack_finish(&ldflags_obst);
1441 /* construct commandline */
1442 obstack_printf(&file_obst, "%s", LINKER);
1443 for (file_list_entry_t *entry = files; entry != NULL;
1444 entry = entry->next) {
1445 if (entry->type != FILETYPE_OBJECT)
1448 add_flag(&file_obst, "%s", entry->name);
1451 add_flag(&file_obst, "-o");
1452 add_flag(&file_obst, outname);
1453 obstack_printf(&file_obst, "%s", flags);
1454 obstack_1grow(&file_obst, '\0');
1456 char *commandline = obstack_finish(&file_obst);
1461 int err = system(commandline);
1462 if (err != EXIT_SUCCESS) {
1463 fprintf(stderr, "linker reported an error\n");
1468 obstack_free(&cppflags_obst, NULL);
1469 obstack_free(&ldflags_obst, NULL);
1470 obstack_free(&file_obst, NULL);
1480 exit_symbol_table();