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) {
274 obstack_1grow(obst, '\\');
277 obstack_1grow(obst, *c);
284 static const char *type_to_string(type_t *type)
286 assert(type->kind == TYPE_ATOMIC);
287 return get_atomic_kind_name(type->atomic.akind);
290 static FILE *preprocess(const char *fname)
292 obstack_1grow(&cppflags_obst, '\0');
293 const char *flags = obstack_finish(&cppflags_obst);
295 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
297 /* setup default defines */
298 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
299 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
300 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
301 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
304 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
306 /* handle dependency generation */
307 if (dep_target[0] != '\0') {
308 add_flag(&cppflags_obst, "-MF");
309 add_flag(&cppflags_obst, dep_target);
310 if (outname != NULL) {
311 add_flag(&cppflags_obst, "-MQ");
312 add_flag(&cppflags_obst, outname);
315 if (flags[0] != '\0') {
316 obstack_printf(&cppflags_obst, " %s", flags);
318 add_flag(&cppflags_obst, fname);
320 obstack_1grow(&cppflags_obst, '\0');
321 const char *buf = obstack_finish(&cppflags_obst);
326 FILE *f = popen(buf, "r");
328 fprintf(stderr, "invoking preprocessor failed\n");
335 static void assemble(const char *out, const char *in)
339 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
344 int err = system(buf);
346 fprintf(stderr, "assembler reported an error\n");
351 static void print_file_name(const char *file)
353 add_flag(&ldflags_obst, "-print-file-name=%s", file);
355 obstack_1grow(&ldflags_obst, '\0');
356 const char *flags = obstack_finish(&ldflags_obst);
358 /* construct commandline */
359 obstack_printf(&ldflags_obst, "%s ", LINKER);
360 obstack_printf(&ldflags_obst, "%s", flags);
361 obstack_1grow(&ldflags_obst, '\0');
363 char *commandline = obstack_finish(&ldflags_obst);
368 int err = system(commandline);
369 if (err != EXIT_SUCCESS) {
370 fprintf(stderr, "linker reported an error\n");
375 static const char *try_dir(const char *dir)
379 if (access(dir, R_OK | W_OK | X_OK) == 0)
384 static const char *get_tempdir(void)
386 static const char *tmpdir = NULL;
392 tmpdir = try_dir(getenv("TMPDIR"));
394 tmpdir = try_dir(getenv("TMP"));
396 tmpdir = try_dir(getenv("TEMP"));
400 tmpdir = try_dir(P_tmpdir);
404 tmpdir = try_dir("/var/tmp");
406 tmpdir = try_dir("/usr/tmp");
408 tmpdir = try_dir("/tmp");
417 /* cheap and nasty mkstemp replacement */
418 static int mkstemp(char *templ)
421 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
426 * an own version of tmpnam, which: writes in a buffer, emits no warnings
427 * during linking (like glibc/gnu ld do for tmpnam)...
429 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
431 const char *tempdir = get_tempdir();
433 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
435 int fd = mkstemp(buffer);
437 fprintf(stderr, "couldn't create temporary file: %s\n",
441 FILE *out = fdopen(fd, "w");
443 fprintf(stderr, "couldn't create temporary file FILE*\n");
447 file_list_entry_t *entry = xmalloc(sizeof(*entry));
448 memset(entry, 0, sizeof(*entry));
450 size_t name_len = strlen(buffer) + 1;
451 char *name = malloc(name_len);
452 memcpy(name, buffer, name_len);
455 entry->next = temp_files;
461 static void free_temp_files(void)
463 file_list_entry_t *entry = temp_files;
464 file_list_entry_t *next;
465 for ( ; entry != NULL; entry = next) {
469 free((char*) entry->name);
476 * Do the necessary lowering for compound parameters.
478 void lower_compound_params(void)
480 lower_params_t params;
482 params.def_ptr_alignment = 4;
483 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
484 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
485 params.find_pointer_type = NULL;
486 params.ret_compound_in_regs = NULL;
487 lower_calls_with_compounds(¶ms);
490 typedef enum compile_mode_t {
504 static void usage(const char *argv0)
506 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
509 static void print_cparser_version(void) {
511 firm_get_version(&ver);
513 printf("cparser (%s) using libFirm (%u.%u",
514 cparser_REVISION, ver.major, ver.minor);
515 if (ver.revision[0] != 0) {
517 fputs(ver.revision, stdout);
519 if (ver.build[0] != 0) {
521 fputs(ver.build, stdout);
526 static void set_be_option(const char *arg)
528 int res = firm_be_option(arg);
533 static void set_option(const char *arg)
535 int res = firm_option(arg);
540 static void copy_file(FILE *dest, FILE *input)
544 while (!feof(input) && !ferror(dest)) {
545 size_t read = fread(buf, 1, sizeof(buf), input);
546 if (fwrite(buf, 1, read, dest) != read) {
547 perror("couldn't write output");
552 static inline bool streq(char const* a, char const* b)
554 return strcmp(a, b) == 0;
557 static inline bool strstart(char const* str, char const* start)
562 } while (*str++ == *start++);
566 static FILE *open_file(const char *filename)
568 if (streq(filename, "-")) {
572 FILE *in = fopen(filename, "r");
574 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
582 static filetype_t get_filetype_from_string(const char *string)
584 if (streq(string, "c") || streq(string, "c-header"))
586 if (streq(string, "c++") || streq(string, "c++-header"))
588 if (streq(string, "assembler"))
589 return FILETYPE_PREPROCESSED_ASSEMBLER;
590 if (streq(string, "assembler-with-cpp"))
591 return FILETYPE_ASSEMBLER;
592 if (streq(string, "none"))
593 return FILETYPE_AUTODETECT;
595 return FILETYPE_UNKNOWN;
598 static void init_os_support(void)
600 /* OS option must be set to the backend */
601 switch (firm_opt.os_support) {
602 case OS_SUPPORT_MINGW:
603 set_be_option("ia32-gasmode=mingw");
604 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
606 case OS_SUPPORT_LINUX:
607 set_be_option("ia32-gasmode=elf");
609 case OS_SUPPORT_MACHO:
610 set_be_option("ia32-gasmode=macho");
611 set_be_option("ia32-stackalign=4");
612 set_be_option("pic");
617 typedef enum lang_standard_t {
618 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
619 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
620 STANDARD_C89, /* ISO C90 (sic) */
621 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
622 STANDARD_C99, /* ISO C99 */
623 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
624 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
625 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
626 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
629 int main(int argc, char **argv)
633 const char *dumpfunction = NULL;
634 const char *print_file_name_file = NULL;
635 compile_mode_t mode = CompileAssembleLink;
637 int result = EXIT_SUCCESS;
638 char cpu_arch[16] = "ia32";
639 file_list_entry_t *files = NULL;
640 file_list_entry_t *last_file = NULL;
641 bool construct_dep_target = false;
642 struct obstack file_obst;
644 atexit(free_temp_files);
646 /* hack for now... */
647 if (strstr(argv[0], "pptest") != NULL) {
648 extern int pptest_main(int argc, char **argv);
649 return pptest_main(argc, argv);
652 obstack_init(&cppflags_obst);
653 obstack_init(&ldflags_obst);
654 obstack_init(&file_obst);
656 #define GET_ARG_AFTER(def, args) \
657 def = &arg[sizeof(args)-1]; \
658 if (def[0] == '\0') { \
661 fprintf(stderr, "error: expected argument after '" args "'\n"); \
662 argument_errors = true; \
666 if (def[0] == '-' && def[1] != '\0') { \
667 fprintf(stderr, "error: expected argument after '" args "'\n"); \
668 argument_errors = true; \
673 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
675 /* early options parsing (find out optimisation level and OS) */
676 for (int i = 1; i < argc; ++i) {
677 const char *arg = argv[i];
681 const char *option = &arg[1];
682 if (option[0] == 'O') {
683 sscanf(&option[1], "%d", &opt_level);
685 if (strcmp(arg, "-fwin32") == 0) {
686 firm_opt.os_support = OS_SUPPORT_MINGW;
687 } else if (strcmp(arg, "-fmac") == 0) {
688 firm_opt.os_support = OS_SUPPORT_MACHO;
689 } else if (strcmp(arg, "-flinux") == 0) {
690 firm_opt.os_support = OS_SUPPORT_LINUX;
694 /* set target/os specific stuff */
697 /* apply optimisation level */
700 set_option("no-opt");
703 set_option("no-inline");
707 set_option("strict-aliasing");
708 /* use_builtins = true; */
711 set_option("cond-eval");
712 set_option("if-conv");
715 set_option("inline");
716 set_option("deconv");
717 set_be_option("omitfp");
721 /* parse rest of options */
722 lang_standard_t standard = STANDARD_DEFAULT;
723 unsigned features_on = 0;
724 unsigned features_off = 0;
725 filetype_t forced_filetype = FILETYPE_AUTODETECT;
726 bool help_displayed = false;
727 bool argument_errors = false;
728 for (int i = 1; i < argc; ++i) {
729 const char *arg = argv[i];
730 if (arg[0] == '-' && arg[1] != '\0') {
732 const char *option = &arg[1];
733 if (option[0] == 'o') {
734 GET_ARG_AFTER(outname, "-o");
735 } else if (option[0] == 'g') {
736 set_be_option("debuginfo=stabs");
737 set_be_option("omitfp=no");
738 set_be_option("ia32-nooptcc=yes");
739 } else if (SINGLE_OPTION('c')) {
740 mode = CompileAssemble;
741 } else if (SINGLE_OPTION('E')) {
742 mode = PreprocessOnly;
743 } else if (SINGLE_OPTION('S')) {
745 } else if (option[0] == 'O') {
747 } else if (option[0] == 'I') {
749 GET_ARG_AFTER(opt, "-I");
750 add_flag(&cppflags_obst, "-I%s", opt);
751 } else if (option[0] == 'D') {
753 GET_ARG_AFTER(opt, "-D");
754 add_flag(&cppflags_obst, "-D%s", opt);
755 } else if (option[0] == 'U') {
757 GET_ARG_AFTER(opt, "-U");
758 add_flag(&cppflags_obst, "-U%s", opt);
759 } else if (option[0] == 'l') {
761 GET_ARG_AFTER(opt, "-l");
762 add_flag(&ldflags_obst, "-l%s", opt);
763 } else if (option[0] == 'L') {
765 GET_ARG_AFTER(opt, "-L");
766 add_flag(&ldflags_obst, "-L%s", opt);
767 } else if (SINGLE_OPTION('v')) {
769 } else if (SINGLE_OPTION('w')) {
770 memset(&warning, 0, sizeof(warning));
771 } else if (option[0] == 'x') {
773 GET_ARG_AFTER(opt, "-x");
774 forced_filetype = get_filetype_from_string(opt);
775 if (forced_filetype == FILETYPE_UNKNOWN) {
776 fprintf(stderr, "Unknown language '%s'\n", opt);
777 argument_errors = true;
779 } else if (streq(option, "M")) {
780 mode = PreprocessOnly;
781 add_flag(&cppflags_obst, "-M");
782 } else if (streq(option, "MMD") ||
783 streq(option, "MD")) {
784 construct_dep_target = true;
785 add_flag(&cppflags_obst, "-%s", option);
786 } else if (streq(option, "MM") ||
787 streq(option, "MP")) {
788 add_flag(&cppflags_obst, "-%s", option);
789 } else if (streq(option, "MT") ||
790 streq(option, "MQ") ||
791 streq(option, "MF")) {
793 GET_ARG_AFTER(opt, "-MT");
794 add_flag(&cppflags_obst, "-%s", option);
795 add_flag(&cppflags_obst, "%s", opt);
796 } else if (streq(option, "include")) {
798 GET_ARG_AFTER(opt, "-include");
799 add_flag(&cppflags_obst, "-include");
800 add_flag(&cppflags_obst, "%s", opt);
801 } else if (streq(option, "isystem")) {
803 GET_ARG_AFTER(opt, "-isystem");
804 add_flag(&cppflags_obst, "-isystem");
805 add_flag(&cppflags_obst, "%s", opt);
806 } else if (streq(option, "nostdinc")
807 || streq(option, "trigraphs")) {
808 /* pass these through to the preprocessor */
809 add_flag(&cppflags_obst, "%s", arg);
810 } else if (streq(option, "pipe")) {
811 /* here for gcc compatibility */
812 } else if (option[0] == 'f') {
813 char const *orig_opt;
814 GET_ARG_AFTER(orig_opt, "-f");
816 if (strstart(orig_opt, "align-loops=") ||
817 strstart(orig_opt, "align-jumps=") ||
818 strstart(orig_opt, "align-functions=")) {
819 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
820 } else if (streq(orig_opt, "verbose-asm")) {
821 /* ignore: we always print verbose assembler */
823 char const *opt = orig_opt;
824 bool truth_value = true;
825 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
830 if (streq(opt, "dollars-in-identifiers")) {
831 allow_dollar_in_symbol = truth_value;
832 } if (streq(opt, "builtins")) {
833 use_builtins = truth_value;
834 } else if (streq(opt, "short-wchar")) {
835 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
837 } else if (streq(opt, "syntax-only")) {
838 mode = truth_value ? ParseOnly : CompileAssembleLink;
839 } else if (streq(opt, "omit-frame-pointer")) {
840 set_be_option(truth_value ? "omitfp" : "omitfp=no");
841 } else if (streq(opt, "strength-reduce")) {
842 firm_option(truth_value ? "strength-red" : "no-strength-red");
843 } else if (streq(opt, "fast-math") ||
844 streq(opt, "jump-tables") ||
845 streq(opt, "unroll-loops") ||
846 streq(opt, "expensive-optimizations") ||
847 streq(opt, "common") ||
849 streq(opt, "align-loops") ||
850 streq(opt, "align-jumps") ||
851 streq(opt, "align-functions")) {
852 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
854 int res = firm_option(orig_opt);
856 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
858 argument_errors = true;
860 } else if (res == -1) {
861 help_displayed = true;
865 } else if (option[0] == 'b') {
867 GET_ARG_AFTER(opt, "-b");
868 int res = firm_be_option(opt);
870 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
872 argument_errors = true;
873 } else if (res == -1) {
874 help_displayed = true;
875 } else if (strstart(opt, "isa=")) {
876 strncpy(cpu_arch, opt, sizeof(cpu_arch));
878 } else if (option[0] == 'W') {
879 if (strstart(option + 1, "p,")) {
880 // pass options directly to the preprocessor
882 GET_ARG_AFTER(opt, "-Wp,");
883 add_flag(&cppflags_obst, "-Wp,%s", opt);
884 } else if (strstart(option + 1, "l,")) {
885 // pass options directly to the linker
887 GET_ARG_AFTER(opt, "-Wl,");
888 add_flag(&ldflags_obst, "-Wl,%s", opt);
889 } else if (streq(option + 1, "no-trigraphs")
890 || streq(option + 1, "undef")) {
891 add_flag(&cppflags_obst, "%s", arg);
893 set_warning_opt(&option[1]);
895 } else if (option[0] == 'm') {
900 GET_ARG_AFTER(opt, "-m");
901 if (strstart(opt, "arch=")) {
902 GET_ARG_AFTER(opt, "-march=");
903 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
904 int res = firm_be_option(arch_opt);
906 argument_errors = true;
908 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
909 int res = firm_be_option(arch_opt);
911 argument_errors = true;
913 } else if (strstart(opt, "tune=")) {
914 GET_ARG_AFTER(opt, "-mtune=");
915 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
916 int res = firm_be_option(arch_opt);
918 argument_errors = true;
919 } else if (strstart(opt, "cpu=")) {
920 GET_ARG_AFTER(opt, "-mcpu=");
921 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
922 int res = firm_be_option(arch_opt);
924 argument_errors = true;
925 } else if (strstart(opt, "fpmath=")) {
926 GET_ARG_AFTER(opt, "-mfpmath=");
927 if (streq(opt, "387"))
929 else if (streq(opt, "sse"))
932 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
933 argument_errors = true;
935 if (!argument_errors) {
936 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
937 int res = firm_be_option(arch_opt);
939 argument_errors = true;
941 } else if (strstart(opt, "preferred-stack-boundary=")) {
942 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
943 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
944 int res = firm_be_option(arch_opt);
946 argument_errors = true;
947 } else if (streq(opt, "omit-leaf-frame-pointer")) {
948 set_be_option("omitleaffp=1");
949 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
950 set_be_option("omitleaffp=0");
953 long int value = strtol(opt, &endptr, 10);
954 if (*endptr != '\0') {
955 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
956 argument_errors = true;
958 if (value != 16 && value != 32 && value != 64) {
959 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
960 argument_errors = true;
962 machine_size = (unsigned int)value;
965 } else if (streq(option, "pg")) {
966 set_be_option("gprof");
967 add_flag(&ldflags_obst, "-pg");
968 } else if (streq(option, "pedantic") ||
969 streq(option, "ansi")) {
970 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
971 } else if (streq(option, "shared")) {
972 add_flag(&ldflags_obst, "-shared");
973 } else if (strstart(option, "std=")) {
974 const char *const o = &option[4];
976 streq(o, "c++") ? STANDARD_CXX98 :
977 streq(o, "c++98") ? STANDARD_CXX98 :
978 streq(o, "c89") ? STANDARD_C89 :
979 streq(o, "c99") ? STANDARD_C99 :
980 streq(o, "c9x") ? STANDARD_C99 : // deprecated
981 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
982 streq(o, "gnu89") ? STANDARD_GNU89 :
983 streq(o, "gnu99") ? STANDARD_GNU99 :
984 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
985 streq(o, "iso9899:1990") ? STANDARD_C89 :
986 streq(o, "iso9899:199409") ? STANDARD_C90 :
987 streq(o, "iso9899:1999") ? STANDARD_C99 :
988 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
989 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
990 } else if (streq(option, "version")) {
991 print_cparser_version();
992 } else if (strstart(option, "print-file-name=")) {
993 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
994 } else if (option[0] == '-') {
995 /* double dash option */
997 if (streq(option, "gcc")) {
998 features_on |= _GNUC;
999 features_off &= ~_GNUC;
1000 } else if (streq(option, "no-gcc")) {
1001 features_on &= ~_GNUC;
1002 features_off |= _GNUC;
1003 } else if (streq(option, "ms")) {
1005 features_off &= ~_MS;
1006 } else if (streq(option, "no-ms")) {
1007 features_on &= ~_MS;
1008 features_off |= _MS;
1009 } else if (streq(option, "signed-chars")) {
1010 char_is_signed = true;
1011 } else if (streq(option, "unsigned-chars")) {
1012 char_is_signed = false;
1013 } else if (streq(option, "strict")) {
1015 } else if (streq(option, "lextest")) {
1017 } else if (streq(option, "benchmark")) {
1018 mode = BenchmarkParser;
1019 } else if (streq(option, "print-ast")) {
1021 } else if (streq(option, "print-implicit-cast")) {
1022 print_implicit_casts = true;
1023 } else if (streq(option, "print-parenthesis")) {
1024 print_parenthesis = true;
1025 } else if (streq(option, "print-fluffy")) {
1027 } else if (streq(option, "print-caml")) {
1029 } else if (streq(option, "version")) {
1030 print_cparser_version();
1032 } else if (streq(option, "dump-function")) {
1035 fprintf(stderr, "error: "
1036 "expected argument after '--dump-function'\n");
1037 argument_errors = true;
1040 dumpfunction = argv[i];
1043 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1044 argument_errors = true;
1047 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1048 argument_errors = true;
1051 filetype_t type = forced_filetype;
1052 if (type == FILETYPE_AUTODETECT) {
1053 if (streq(arg, "-")) {
1054 /* - implicitly means C source file */
1057 const char *suffix = strrchr(arg, '.');
1058 /* Ensure there is at least one char before the suffix */
1059 if (suffix != NULL && suffix != arg) {
1062 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1063 streq(suffix, "a") ? FILETYPE_OBJECT :
1064 streq(suffix, "c") ? FILETYPE_C :
1065 streq(suffix, "cc") ? FILETYPE_CXX :
1066 streq(suffix, "cpp") ? FILETYPE_CXX :
1067 streq(suffix, "cxx") ? FILETYPE_CXX :
1068 streq(suffix, "h") ? FILETYPE_C :
1069 streq(suffix, "o") ? FILETYPE_OBJECT :
1070 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1071 streq(suffix, "so") ? FILETYPE_OBJECT :
1072 FILETYPE_AUTODETECT;
1076 if (type == FILETYPE_AUTODETECT) {
1077 fprintf(stderr, "'%s': file format not recognized\n", arg);
1082 file_list_entry_t *entry
1083 = obstack_alloc(&file_obst, sizeof(entry[0]));
1084 memset(entry, 0, sizeof(entry[0]));
1088 if (last_file != NULL) {
1089 last_file->next = entry;
1097 if (print_file_name_file != NULL) {
1098 print_file_name(print_file_name_file);
1102 if (files == NULL) {
1103 fprintf(stderr, "error: no input files specified\n");
1104 argument_errors = true;
1107 if (help_displayed) {
1108 return !argument_errors;
1110 if (argument_errors) {
1115 /* we do the lowering in ast2firm */
1116 firm_opt.lower_bitfields = FALSE;
1119 init_symbol_table();
1129 if (construct_dep_target) {
1130 if (outname != 0 && strlen(outname) >= 2) {
1131 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1133 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1136 dep_target[0] = '\0';
1139 char outnamebuf[4096];
1140 if (outname == NULL) {
1141 const char *filename = files->name;
1144 case BenchmarkParser:
1149 case PreprocessOnly:
1154 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1155 outname = outnamebuf;
1157 case CompileAssemble:
1158 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1159 outname = outnamebuf;
1162 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1164 outname = outnamebuf;
1166 case CompileAssembleLink:
1176 assert(outname != NULL);
1179 if (streq(outname, "-")) {
1182 out = fopen(outname, "w");
1184 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1190 file_list_entry_t *file;
1191 for (file = files; file != NULL; file = file->next) {
1192 char asm_tempfile[1024];
1193 const char *filename = file->name;
1194 filetype_t filetype = file->type;
1196 if (filetype == FILETYPE_OBJECT)
1200 if (mode == LexTest) {
1202 in = open_file(filename);
1203 lextest(in, filename);
1208 FILE *preprocessed_in = NULL;
1211 filetype = FILETYPE_PREPROCESSED_C;
1214 filetype = FILETYPE_PREPROCESSED_CXX;
1216 case FILETYPE_ASSEMBLER:
1217 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1218 add_flag(&cppflags_obst, "-x");
1219 add_flag(&cppflags_obst, "assembler-with-cpp");
1222 /* no support for input on FILE* yet */
1224 panic("internal compiler error: in for preprocessor != NULL");
1226 preprocessed_in = preprocess(filename);
1227 if (mode == PreprocessOnly) {
1228 copy_file(out, preprocessed_in);
1229 int result = pclose(preprocessed_in);
1234 in = preprocessed_in;
1242 if (mode == Compile) {
1245 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1249 in = open_file(filename);
1251 /* preprocess and compile */
1252 if (filetype == FILETYPE_PREPROCESSED_C) {
1253 char const* invalid_mode;
1256 case STANDARD_C89: c_mode = _C89; break;
1257 /* TODO ^v determine difference between these two */
1258 case STANDARD_C90: c_mode = _C89; break;
1259 case STANDARD_C99: c_mode = _C89 | _C99; break;
1260 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1264 "warning: command line option \"-std=%s\" is not valid for C\n",
1267 case STANDARD_DEFAULT:
1268 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1270 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1271 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1274 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1275 char const* invalid_mode;
1277 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1278 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1279 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1280 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1281 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1284 case STANDARD_CXX98: c_mode = _CXX; break;
1288 "warning: command line option \"-std=%s\" is not valid for C++\n",
1290 case STANDARD_DEFAULT:
1291 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1295 c_mode |= features_on;
1296 c_mode &= ~features_off;
1298 translation_unit_t *const unit = do_parsing(in, filename);
1300 /* prints the AST even if errors occurred */
1301 if (mode == PrintAst) {
1302 type_set_output(out);
1303 ast_set_output(out);
1307 if (error_count > 0) {
1308 /* parsing failed because of errors */
1309 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1311 result = EXIT_FAILURE;
1313 } else if (warning_count > 0) {
1314 fprintf(stderr, "%u warning(s)\n", warning_count);
1317 if (in == preprocessed_in) {
1318 int pp_result = pclose(preprocessed_in);
1319 if (pp_result != EXIT_SUCCESS) {
1324 if (mode == BenchmarkParser) {
1326 } else if (mode == PrintFluffy) {
1327 write_fluffy_decls(out, unit);
1329 } else if (mode == PrintCaml) {
1330 write_caml_decls(out, unit);
1334 translation_unit_to_firm(unit);
1336 if (mode == ParseOnly) {
1340 if (mode == CompileDump) {
1342 ident *id = new_id_from_str(dumpfunction);
1343 ir_graph *irg = NULL;
1344 int n_irgs = get_irp_n_irgs();
1345 for (int i = 0; i < n_irgs; ++i) {
1346 ir_graph *tirg = get_irp_irg(i);
1347 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1355 fprintf(stderr, "No graph for function '%s' found\n",
1360 dump_ir_block_graph_file(irg, out);
1365 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1366 have_const_functions);
1367 if (asm_out != out) {
1370 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1371 copy_file(asm_out, in);
1372 if (in == preprocessed_in) {
1373 int pp_result = pclose(preprocessed_in);
1374 if (pp_result != EXIT_SUCCESS) {
1378 if (asm_out != out) {
1383 if (mode == Compile)
1386 /* if we're here then we have preprocessed assembly */
1387 filename = asm_tempfile;
1388 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1391 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1393 const char *filename_o;
1394 if (mode == CompileAssemble) {
1396 filename_o = outname;
1398 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1403 assemble(filename_o, filename);
1405 size_t len = strlen(filename_o) + 1;
1406 filename = obstack_copy(&file_obst, filename_o, len);
1407 filetype = FILETYPE_OBJECT;
1410 /* ok we're done here, process next file */
1411 file->name = filename;
1412 file->type = filetype;
1415 if (result != EXIT_SUCCESS)
1418 /* link program file */
1419 if (mode == CompileAssembleLink) {
1420 obstack_1grow(&ldflags_obst, '\0');
1421 const char *flags = obstack_finish(&ldflags_obst);
1423 /* construct commandline */
1424 obstack_printf(&file_obst, "%s", LINKER);
1425 for (file_list_entry_t *entry = files; entry != NULL;
1426 entry = entry->next) {
1427 if (entry->type != FILETYPE_OBJECT)
1430 add_flag(&file_obst, "%s", entry->name);
1433 add_flag(&file_obst, "-o");
1434 add_flag(&file_obst, outname);
1435 obstack_printf(&file_obst, "%s", flags);
1436 obstack_1grow(&file_obst, '\0');
1438 char *commandline = obstack_finish(&file_obst);
1443 int err = system(commandline);
1444 if (err != EXIT_SUCCESS) {
1445 fprintf(stderr, "linker reported an error\n");
1450 obstack_free(&cppflags_obst, NULL);
1451 obstack_free(&ldflags_obst, NULL);
1452 obstack_free(&file_obst, NULL);
1462 exit_symbol_table();