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) {
273 obstack_1grow(obst, '\\');
276 obstack_1grow(obst, *c);
283 static const char *type_to_string(type_t *type)
285 assert(type->kind == TYPE_ATOMIC);
286 return get_atomic_kind_name(type->atomic.akind);
289 static FILE *preprocess(const char *fname)
291 obstack_1grow(&cppflags_obst, '\0');
292 const char *flags = obstack_finish(&cppflags_obst);
294 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
296 /* setup default defines */
297 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
298 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
299 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
300 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
303 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
305 /* handle dependency generation */
306 if (dep_target[0] != '\0') {
307 add_flag(&cppflags_obst, "-MF");
308 add_flag(&cppflags_obst, dep_target);
309 if (outname != NULL) {
310 add_flag(&cppflags_obst, "-MQ");
311 add_flag(&cppflags_obst, outname);
314 if (flags[0] != '\0') {
315 obstack_printf(&cppflags_obst, " %s", flags);
317 add_flag(&cppflags_obst, fname);
319 obstack_1grow(&cppflags_obst, '\0');
320 const char *buf = obstack_finish(&cppflags_obst);
325 FILE *f = popen(buf, "r");
327 fprintf(stderr, "invoking preprocessor failed\n");
334 static void assemble(const char *out, const char *in)
338 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
343 int err = system(buf);
345 fprintf(stderr, "assembler reported an error\n");
350 static void print_file_name(const char *file)
352 add_flag(&ldflags_obst, "-print-file-name=%s", file);
354 obstack_1grow(&ldflags_obst, '\0');
355 const char *flags = obstack_finish(&ldflags_obst);
357 /* construct commandline */
358 obstack_printf(&ldflags_obst, "%s ", LINKER);
359 obstack_printf(&ldflags_obst, "%s", flags);
360 obstack_1grow(&ldflags_obst, '\0');
362 char *commandline = obstack_finish(&ldflags_obst);
367 int err = system(commandline);
368 if (err != EXIT_SUCCESS) {
369 fprintf(stderr, "linker reported an error\n");
374 static const char *try_dir(const char *dir)
378 if (access(dir, R_OK | W_OK | X_OK) == 0)
383 static const char *get_tempdir(void)
385 static const char *tmpdir = NULL;
391 tmpdir = try_dir(getenv("TMPDIR"));
393 tmpdir = try_dir(getenv("TMP"));
395 tmpdir = try_dir(getenv("TEMP"));
399 tmpdir = try_dir(P_tmpdir);
403 tmpdir = try_dir("/var/tmp");
405 tmpdir = try_dir("/usr/tmp");
407 tmpdir = try_dir("/tmp");
416 /* cheap and nasty mkstemp replacement */
417 static int mkstemp(char *templ)
420 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
425 * an own version of tmpnam, which: writes in a buffer, emits no warnings
426 * during linking (like glibc/gnu ld do for tmpnam)...
428 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
430 const char *tempdir = get_tempdir();
432 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
434 int fd = mkstemp(buffer);
436 fprintf(stderr, "couldn't create temporary file: %s\n",
440 FILE *out = fdopen(fd, "w");
442 fprintf(stderr, "couldn't create temporary file FILE*\n");
446 file_list_entry_t *entry = xmalloc(sizeof(*entry));
447 memset(entry, 0, sizeof(*entry));
449 size_t name_len = strlen(buffer) + 1;
450 char *name = malloc(name_len);
451 memcpy(name, buffer, name_len);
454 entry->next = temp_files;
460 static void free_temp_files(void)
462 file_list_entry_t *entry = temp_files;
463 file_list_entry_t *next;
464 for ( ; entry != NULL; entry = next) {
468 free((char*) entry->name);
475 * Do the necessary lowering for compound parameters.
477 void lower_compound_params(void)
479 lower_params_t params;
481 params.def_ptr_alignment = 4;
482 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
483 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
484 params.find_pointer_type = NULL;
485 params.ret_compound_in_regs = NULL;
486 lower_calls_with_compounds(¶ms);
489 typedef enum compile_mode_t {
503 static void usage(const char *argv0)
505 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
508 static void print_cparser_version(void) {
510 firm_get_version(&ver);
512 printf("cparser (%s) using libFirm (%u.%u",
513 cparser_REVISION, ver.major, ver.minor);
514 if (ver.revision[0] != 0) {
516 fputs(ver.revision, stdout);
518 if (ver.build[0] != 0) {
520 fputs(ver.build, stdout);
525 static void set_be_option(const char *arg)
527 int res = firm_be_option(arg);
532 static void set_option(const char *arg)
534 int res = firm_option(arg);
539 static void copy_file(FILE *dest, FILE *input)
543 while (!feof(input) && !ferror(dest)) {
544 size_t read = fread(buf, 1, sizeof(buf), input);
545 if (fwrite(buf, 1, read, dest) != read) {
546 perror("couldn't write output");
551 static inline bool streq(char const* a, char const* b)
553 return strcmp(a, b) == 0;
556 static inline bool strstart(char const* str, char const* start)
561 } while (*str++ == *start++);
565 static FILE *open_file(const char *filename)
567 if (streq(filename, "-")) {
571 FILE *in = fopen(filename, "r");
573 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
581 static filetype_t get_filetype_from_string(const char *string)
583 if (streq(string, "c") || streq(string, "c-header"))
585 if (streq(string, "c++") || streq(string, "c++-header"))
587 if (streq(string, "assembler"))
588 return FILETYPE_PREPROCESSED_ASSEMBLER;
589 if (streq(string, "assembler-with-cpp"))
590 return FILETYPE_ASSEMBLER;
591 if (streq(string, "none"))
592 return FILETYPE_AUTODETECT;
594 return FILETYPE_UNKNOWN;
597 static void init_os_support(void)
599 /* OS option must be set to the backend */
600 switch (firm_opt.os_support) {
601 case OS_SUPPORT_MINGW:
602 set_be_option("ia32-gasmode=mingw");
603 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
605 case OS_SUPPORT_LINUX:
606 set_be_option("ia32-gasmode=elf");
608 case OS_SUPPORT_MACHO:
609 set_be_option("ia32-gasmode=macho");
610 set_be_option("ia32-stackalign=4");
611 set_be_option("pic");
616 typedef enum lang_standard_t {
617 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
618 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
619 STANDARD_C89, /* ISO C90 (sic) */
620 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
621 STANDARD_C99, /* ISO C99 */
622 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
623 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
624 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
625 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
628 int main(int argc, char **argv)
632 const char *dumpfunction = NULL;
633 const char *print_file_name_file = NULL;
634 compile_mode_t mode = CompileAssembleLink;
636 int result = EXIT_SUCCESS;
637 char cpu_arch[16] = "ia32";
638 file_list_entry_t *files = NULL;
639 file_list_entry_t *last_file = NULL;
640 bool construct_dep_target = false;
641 struct obstack file_obst;
643 atexit(free_temp_files);
645 /* hack for now... */
646 if (strstr(argv[0], "pptest") != NULL) {
647 extern int pptest_main(int argc, char **argv);
648 return pptest_main(argc, argv);
651 obstack_init(&cppflags_obst);
652 obstack_init(&ldflags_obst);
653 obstack_init(&file_obst);
655 #define GET_ARG_AFTER(def, args) \
656 def = &arg[sizeof(args)-1]; \
657 if (def[0] == '\0') { \
660 fprintf(stderr, "error: expected argument after '" args "'\n"); \
661 argument_errors = true; \
665 if (def[0] == '-' && def[1] != '\0') { \
666 fprintf(stderr, "error: expected argument after '" args "'\n"); \
667 argument_errors = true; \
672 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
674 /* early options parsing (find out optimisation level and OS) */
675 for (int i = 1; i < argc; ++i) {
676 const char *arg = argv[i];
680 const char *option = &arg[1];
681 if (option[0] == 'O') {
682 sscanf(&option[1], "%d", &opt_level);
684 if (strcmp(arg, "-fwin32") == 0) {
685 firm_opt.os_support = OS_SUPPORT_MINGW;
686 } else if (strcmp(arg, "-fmac") == 0) {
687 firm_opt.os_support = OS_SUPPORT_MACHO;
688 } else if (strcmp(arg, "-flinux") == 0) {
689 firm_opt.os_support = OS_SUPPORT_LINUX;
693 /* set target/os specific stuff */
696 /* apply optimisation level */
699 set_option("no-opt");
702 set_option("no-inline");
706 set_option("strict-aliasing");
707 /* use_builtins = true; */
710 set_option("cond-eval");
711 set_option("if-conv");
714 set_option("inline");
715 set_option("deconv");
716 set_be_option("omitfp");
720 /* parse rest of options */
721 lang_standard_t standard = STANDARD_DEFAULT;
722 unsigned features_on = 0;
723 unsigned features_off = 0;
724 filetype_t forced_filetype = FILETYPE_AUTODETECT;
725 bool help_displayed = false;
726 bool argument_errors = false;
727 for (int i = 1; i < argc; ++i) {
728 const char *arg = argv[i];
729 if (arg[0] == '-' && arg[1] != '\0') {
731 const char *option = &arg[1];
732 if (option[0] == 'o') {
733 GET_ARG_AFTER(outname, "-o");
734 } else if (option[0] == 'g') {
735 set_be_option("debuginfo=stabs");
736 set_be_option("omitfp=no");
737 set_be_option("ia32-nooptcc=yes");
738 } else if (SINGLE_OPTION('c')) {
739 mode = CompileAssemble;
740 } else if (SINGLE_OPTION('E')) {
741 mode = PreprocessOnly;
742 } else if (SINGLE_OPTION('S')) {
744 } else if (option[0] == 'O') {
746 } else if (option[0] == 'I') {
748 GET_ARG_AFTER(opt, "-I");
749 add_flag(&cppflags_obst, "-I%s", opt);
750 } else if (option[0] == 'D') {
752 GET_ARG_AFTER(opt, "-D");
753 add_flag(&cppflags_obst, "-D%s", opt);
754 } else if (option[0] == 'U') {
756 GET_ARG_AFTER(opt, "-U");
757 add_flag(&cppflags_obst, "-U%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 (option[0] == 'L') {
764 GET_ARG_AFTER(opt, "-L");
765 add_flag(&ldflags_obst, "-L%s", opt);
766 } else if (SINGLE_OPTION('v')) {
768 } else if (SINGLE_OPTION('w')) {
769 memset(&warning, 0, sizeof(warning));
770 } else if (option[0] == 'x') {
772 GET_ARG_AFTER(opt, "-x");
773 forced_filetype = get_filetype_from_string(opt);
774 if (forced_filetype == FILETYPE_UNKNOWN) {
775 fprintf(stderr, "Unknown language '%s'\n", opt);
776 argument_errors = true;
778 } else if (streq(option, "M")) {
779 mode = PreprocessOnly;
780 add_flag(&cppflags_obst, "-M");
781 } else if (streq(option, "MMD") ||
782 streq(option, "MD")) {
783 construct_dep_target = true;
784 add_flag(&cppflags_obst, "-%s", option);
785 } else if (streq(option, "MM") ||
786 streq(option, "MP")) {
787 add_flag(&cppflags_obst, "-%s", option);
788 } else if (streq(option, "MT") ||
789 streq(option, "MQ") ||
790 streq(option, "MF")) {
792 GET_ARG_AFTER(opt, "-MT");
793 add_flag(&cppflags_obst, "-%s", option);
794 add_flag(&cppflags_obst, "%s", opt);
795 } else if (streq(option, "include")) {
797 GET_ARG_AFTER(opt, "-include");
798 add_flag(&cppflags_obst, "-include");
799 add_flag(&cppflags_obst, "%s", opt);
800 } else if (streq(option, "isystem")) {
802 GET_ARG_AFTER(opt, "-isystem");
803 add_flag(&cppflags_obst, "-isystem");
804 add_flag(&cppflags_obst, "%s", opt);
805 } else if (streq(option, "nostdinc")
806 || streq(option, "trigraphs")) {
807 /* pass these through to the preprocessor */
808 add_flag(&cppflags_obst, "%s", arg);
809 } else if (streq(option, "pipe")) {
810 /* here for gcc compatibility */
811 } else if (option[0] == 'f') {
812 char const *orig_opt;
813 GET_ARG_AFTER(orig_opt, "-f");
815 if (strstart(orig_opt, "align-loops=") ||
816 strstart(orig_opt, "align-jumps=") ||
817 strstart(orig_opt, "align-functions=")) {
818 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
819 } else if (streq(orig_opt, "verbose-asm")) {
820 /* ignore: we always print verbose assembler */
822 char const *opt = orig_opt;
823 bool truth_value = true;
824 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
829 if (streq(opt, "dollars-in-identifiers")) {
830 allow_dollar_in_symbol = truth_value;
831 } if (streq(opt, "builtins")) {
832 use_builtins = truth_value;
833 } else if (streq(opt, "short-wchar")) {
834 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
836 } else if (streq(opt, "syntax-only")) {
837 mode = truth_value ? ParseOnly : CompileAssembleLink;
838 } else if (streq(opt, "omit-frame-pointer")) {
839 set_be_option(truth_value ? "omitfp" : "omitfp=no");
840 } else if (streq(opt, "strength-reduce")) {
841 firm_option(truth_value ? "strength-red" : "no-strength-red");
842 } else if (streq(opt, "fast-math") ||
843 streq(opt, "jump-tables") ||
844 streq(opt, "unroll-loops") ||
845 streq(opt, "expensive-optimizations") ||
846 streq(opt, "common") ||
848 streq(opt, "align-loops") ||
849 streq(opt, "align-jumps") ||
850 streq(opt, "align-functions")) {
851 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
853 int res = firm_option(orig_opt);
855 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
857 argument_errors = true;
859 } else if (res == -1) {
860 help_displayed = true;
864 } else if (option[0] == 'b') {
866 GET_ARG_AFTER(opt, "-b");
867 int res = firm_be_option(opt);
869 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
871 argument_errors = true;
872 } else if (res == -1) {
873 help_displayed = true;
874 } else if (strstart(opt, "isa=")) {
875 strncpy(cpu_arch, opt, sizeof(cpu_arch));
877 } else if (option[0] == 'W') {
878 if (strstart(option + 1, "p,")) {
879 // pass options directly to the preprocessor
881 GET_ARG_AFTER(opt, "-Wp,");
882 add_flag(&cppflags_obst, "-Wp,%s", opt);
883 } else if (strstart(option + 1, "l,")) {
884 // pass options directly to the linker
886 GET_ARG_AFTER(opt, "-Wl,");
887 add_flag(&ldflags_obst, "-Wl,%s", opt);
888 } else if (streq(option + 1, "no-trigraphs")
889 || streq(option + 1, "undef")) {
890 add_flag(&cppflags_obst, "%s", arg);
892 set_warning_opt(&option[1]);
894 } else if (option[0] == 'm') {
899 GET_ARG_AFTER(opt, "-m");
900 if (strstart(opt, "arch=")) {
901 GET_ARG_AFTER(opt, "-march=");
902 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
903 int res = firm_be_option(arch_opt);
905 argument_errors = true;
907 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
908 int res = firm_be_option(arch_opt);
910 argument_errors = true;
912 } else if (strstart(opt, "tune=")) {
913 GET_ARG_AFTER(opt, "-mtune=");
914 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
915 int res = firm_be_option(arch_opt);
917 argument_errors = true;
918 } else if (strstart(opt, "cpu=")) {
919 GET_ARG_AFTER(opt, "-mcpu=");
920 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
921 int res = firm_be_option(arch_opt);
923 argument_errors = true;
924 } else if (strstart(opt, "fpmath=")) {
925 GET_ARG_AFTER(opt, "-mfpmath=");
926 if (streq(opt, "387"))
928 else if (streq(opt, "sse"))
931 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
932 argument_errors = true;
934 if (!argument_errors) {
935 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
936 int res = firm_be_option(arch_opt);
938 argument_errors = true;
940 } else if (strstart(opt, "preferred-stack-boundary=")) {
941 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
942 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
943 int res = firm_be_option(arch_opt);
945 argument_errors = true;
946 } else if (streq(opt, "omit-leaf-frame-pointer")) {
947 set_be_option("omitleaffp=1");
948 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
949 set_be_option("omitleaffp=0");
952 long int value = strtol(opt, &endptr, 10);
953 if (*endptr != '\0') {
954 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
955 argument_errors = true;
957 if (value != 16 && value != 32 && value != 64) {
958 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
959 argument_errors = true;
961 machine_size = (unsigned int)value;
964 } else if (streq(option, "pg")) {
965 set_be_option("gprof");
966 add_flag(&ldflags_obst, "-pg");
967 } else if (streq(option, "pedantic") ||
968 streq(option, "ansi")) {
969 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
970 } else if (streq(option, "shared")) {
971 add_flag(&ldflags_obst, "-shared");
972 } else if (strstart(option, "std=")) {
973 const char *const o = &option[4];
975 streq(o, "c++") ? STANDARD_CXX98 :
976 streq(o, "c++98") ? STANDARD_CXX98 :
977 streq(o, "c89") ? STANDARD_C89 :
978 streq(o, "c99") ? STANDARD_C99 :
979 streq(o, "c9x") ? STANDARD_C99 : // deprecated
980 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
981 streq(o, "gnu89") ? STANDARD_GNU89 :
982 streq(o, "gnu99") ? STANDARD_GNU99 :
983 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
984 streq(o, "iso9899:1990") ? STANDARD_C89 :
985 streq(o, "iso9899:199409") ? STANDARD_C90 :
986 streq(o, "iso9899:1999") ? STANDARD_C99 :
987 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
988 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
989 } else if (streq(option, "version")) {
990 print_cparser_version();
991 } else if (strstart(option, "print-file-name=")) {
992 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
993 } else if (option[0] == '-') {
994 /* double dash option */
996 if (streq(option, "gcc")) {
997 features_on |= _GNUC;
998 features_off &= ~_GNUC;
999 } else if (streq(option, "no-gcc")) {
1000 features_on &= ~_GNUC;
1001 features_off |= _GNUC;
1002 } else if (streq(option, "ms")) {
1004 features_off &= ~_MS;
1005 } else if (streq(option, "no-ms")) {
1006 features_on &= ~_MS;
1007 features_off |= _MS;
1008 } else if (streq(option, "signed-chars")) {
1009 char_is_signed = true;
1010 } else if (streq(option, "unsigned-chars")) {
1011 char_is_signed = false;
1012 } else if (streq(option, "strict")) {
1014 } else if (streq(option, "lextest")) {
1016 } else if (streq(option, "benchmark")) {
1017 mode = BenchmarkParser;
1018 } else if (streq(option, "print-ast")) {
1020 } else if (streq(option, "print-implicit-cast")) {
1021 print_implicit_casts = true;
1022 } else if (streq(option, "print-parenthesis")) {
1023 print_parenthesis = true;
1024 } else if (streq(option, "print-fluffy")) {
1026 } else if (streq(option, "print-caml")) {
1028 } else if (streq(option, "version")) {
1029 print_cparser_version();
1031 } else if (streq(option, "dump-function")) {
1034 fprintf(stderr, "error: "
1035 "expected argument after '--dump-function'\n");
1036 argument_errors = true;
1039 dumpfunction = argv[i];
1042 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1043 argument_errors = true;
1046 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1047 argument_errors = true;
1050 filetype_t type = forced_filetype;
1051 if (type == FILETYPE_AUTODETECT) {
1052 if (streq(arg, "-")) {
1053 /* - implicitly means C source file */
1056 const char *suffix = strrchr(arg, '.');
1057 /* Ensure there is at least one char before the suffix */
1058 if (suffix != NULL && suffix != arg) {
1061 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1062 streq(suffix, "a") ? FILETYPE_OBJECT :
1063 streq(suffix, "c") ? FILETYPE_C :
1064 streq(suffix, "cc") ? FILETYPE_CXX :
1065 streq(suffix, "cpp") ? FILETYPE_CXX :
1066 streq(suffix, "cxx") ? FILETYPE_CXX :
1067 streq(suffix, "h") ? FILETYPE_C :
1068 streq(suffix, "o") ? FILETYPE_OBJECT :
1069 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1070 streq(suffix, "so") ? FILETYPE_OBJECT :
1071 FILETYPE_AUTODETECT;
1075 if (type == FILETYPE_AUTODETECT) {
1076 fprintf(stderr, "'%s': file format not recognized\n", arg);
1081 file_list_entry_t *entry
1082 = obstack_alloc(&file_obst, sizeof(entry[0]));
1083 memset(entry, 0, sizeof(entry[0]));
1087 if (last_file != NULL) {
1088 last_file->next = entry;
1096 if (print_file_name_file != NULL) {
1097 print_file_name(print_file_name_file);
1101 if (files == NULL) {
1102 fprintf(stderr, "error: no input files specified\n");
1103 argument_errors = true;
1106 if (help_displayed) {
1107 return !argument_errors;
1109 if (argument_errors) {
1114 /* we do the lowering in ast2firm */
1115 firm_opt.lower_bitfields = FALSE;
1118 init_symbol_table();
1128 if (construct_dep_target) {
1129 if (outname != 0 && strlen(outname) >= 2) {
1130 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1132 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1135 dep_target[0] = '\0';
1138 char outnamebuf[4096];
1139 if (outname == NULL) {
1140 const char *filename = files->name;
1143 case BenchmarkParser:
1148 case PreprocessOnly:
1153 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1154 outname = outnamebuf;
1156 case CompileAssemble:
1157 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1158 outname = outnamebuf;
1161 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1163 outname = outnamebuf;
1165 case CompileAssembleLink:
1175 assert(outname != NULL);
1178 if (streq(outname, "-")) {
1181 out = fopen(outname, "w");
1183 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1189 file_list_entry_t *file;
1190 for (file = files; file != NULL; file = file->next) {
1191 char asm_tempfile[1024];
1192 const char *filename = file->name;
1193 filetype_t filetype = file->type;
1195 if (filetype == FILETYPE_OBJECT)
1199 if (mode == LexTest) {
1201 in = open_file(filename);
1202 lextest(in, filename);
1207 FILE *preprocessed_in = NULL;
1209 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1210 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1211 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1213 /* no support for input on FILE* yet */
1215 panic("internal compiler error: in for preprocessor != NULL");
1217 preprocessed_in = preprocess(filename);
1218 if (mode == PreprocessOnly) {
1219 copy_file(out, preprocessed_in);
1220 int result = pclose(preprocessed_in);
1225 in = preprocessed_in;
1233 if (mode == Compile) {
1236 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1240 in = open_file(filename);
1242 /* preprocess and compile */
1243 if (filetype == FILETYPE_PREPROCESSED_C) {
1244 char const* invalid_mode;
1247 case STANDARD_C89: c_mode = _C89; break;
1248 /* TODO ^v determine difference between these two */
1249 case STANDARD_C90: c_mode = _C89; break;
1250 case STANDARD_C99: c_mode = _C89 | _C99; break;
1251 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1255 "warning: command line option \"-std=%s\" is not valid for C\n",
1258 case STANDARD_DEFAULT:
1259 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1261 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1262 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1265 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1266 char const* invalid_mode;
1268 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1269 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1270 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1271 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1272 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1275 case STANDARD_CXX98: c_mode = _CXX; break;
1279 "warning: command line option \"-std=%s\" is not valid for C++\n",
1281 case STANDARD_DEFAULT:
1282 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1286 c_mode |= features_on;
1287 c_mode &= ~features_off;
1289 translation_unit_t *const unit = do_parsing(in, filename);
1291 /* prints the AST even if errors occurred */
1292 if (mode == PrintAst) {
1293 type_set_output(out);
1294 ast_set_output(out);
1298 if (error_count > 0) {
1299 /* parsing failed because of errors */
1300 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1302 result = EXIT_FAILURE;
1304 } else if (warning_count > 0) {
1305 fprintf(stderr, "%u warning(s)\n", warning_count);
1308 if (in == preprocessed_in) {
1309 int pp_result = pclose(preprocessed_in);
1310 if (pp_result != EXIT_SUCCESS) {
1315 if (mode == BenchmarkParser) {
1317 } else if (mode == PrintFluffy) {
1318 write_fluffy_decls(out, unit);
1320 } else if (mode == PrintCaml) {
1321 write_caml_decls(out, unit);
1325 translation_unit_to_firm(unit);
1327 if (mode == ParseOnly) {
1331 if (mode == CompileDump) {
1333 ident *id = new_id_from_str(dumpfunction);
1334 ir_graph *irg = NULL;
1335 int n_irgs = get_irp_n_irgs();
1336 for (int i = 0; i < n_irgs; ++i) {
1337 ir_graph *tirg = get_irp_irg(i);
1338 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1346 fprintf(stderr, "No graph for function '%s' found\n",
1351 dump_ir_block_graph_file(irg, out);
1356 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1357 have_const_functions);
1358 if (asm_out != out) {
1361 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1362 copy_file(asm_out, in);
1363 if (in == preprocessed_in) {
1364 int pp_result = pclose(preprocessed_in);
1365 if (pp_result != EXIT_SUCCESS) {
1369 if (asm_out != out) {
1374 if (mode == Compile)
1377 /* if we're here then we have preprocessed assembly */
1378 filename = asm_tempfile;
1379 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1382 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1384 const char *filename_o;
1385 if (mode == CompileAssemble) {
1387 filename_o = outname;
1389 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1394 assemble(filename_o, filename);
1396 size_t len = strlen(filename_o) + 1;
1397 filename = obstack_copy(&file_obst, filename_o, len);
1398 filetype = FILETYPE_OBJECT;
1401 /* ok we're done here, process next file */
1402 file->name = filename;
1403 file->type = filetype;
1406 if (result != EXIT_SUCCESS)
1409 /* link program file */
1410 if (mode == CompileAssembleLink) {
1411 obstack_1grow(&ldflags_obst, '\0');
1412 const char *flags = obstack_finish(&ldflags_obst);
1414 /* construct commandline */
1415 obstack_printf(&file_obst, "%s", LINKER);
1416 for (file_list_entry_t *entry = files; entry != NULL;
1417 entry = entry->next) {
1418 if (entry->type != FILETYPE_OBJECT)
1421 add_flag(&file_obst, "%s", entry->name);
1424 add_flag(&file_obst, "-o");
1425 add_flag(&file_obst, outname);
1426 obstack_printf(&file_obst, "%s", flags);
1427 obstack_1grow(&file_obst, '\0');
1429 char *commandline = obstack_finish(&file_obst);
1434 int err = system(commandline);
1435 if (err != EXIT_SUCCESS) {
1436 fprintf(stderr, "linker reported an error\n");
1441 obstack_free(&cppflags_obst, NULL);
1442 obstack_free(&ldflags_obst, NULL);
1443 obstack_free(&file_obst, NULL);
1453 exit_symbol_table();