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 "wrappergen/write_fluffy.h"
76 #include "wrappergen/write_caml.h"
77 #include "wrappergen/write_jna.h"
84 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
86 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
91 #define LINKER "gcc -m32"
96 #define ASSEMBLER "gcc -c -xassembler"
98 #define ASSEMBLER "as --32"
102 /** The current c mode/dialect. */
103 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
105 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
106 unsigned int machine_size = 32;
108 /** true if the char type is signed. */
109 bool char_is_signed = true;
111 /** true for strict language checking. */
112 bool strict_mode = false;
114 /** use builtins for some libc functions */
115 bool use_builtins = false;
117 /** we have extern function with const attribute. */
118 bool have_const_functions = false;
120 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
122 /* to switch on printing of implicit casts */
123 extern bool print_implicit_casts;
125 /* to switch on printing of parenthesis to indicate operator precedence */
126 extern bool print_parenthesis;
129 static struct obstack cppflags_obst, ldflags_obst;
130 static char dep_target[1024];
131 static const char *outname;
133 typedef struct file_list_entry_t file_list_entry_t;
135 typedef enum filetype_t {
138 FILETYPE_PREPROCESSED_C,
140 FILETYPE_PREPROCESSED_CXX,
142 FILETYPE_PREPROCESSED_ASSEMBLER,
147 struct file_list_entry_t {
148 const char *name; /**< filename or NULL for stdin */
150 file_list_entry_t *next;
153 static file_list_entry_t *temp_files;
155 #if defined(_DEBUG) || defined(FIRM_DEBUG)
157 * Debug printf implementation.
159 * @param fmt printf style format parameter
161 void dbg_printf(const char *fmt, ...)
165 if (firm_dump.debug_print) {
171 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
173 static void initialize_firm(void)
177 dump_consts_local(1);
178 dump_keepalive_edges(1);
181 static void get_output_name(char *buf, size_t buflen, const char *inputname,
184 if (inputname == NULL)
187 char const *const last_slash = strrchr(inputname, '/');
188 char const *const filename =
189 last_slash != NULL ? last_slash + 1 : inputname;
190 char const *const last_dot = strrchr(filename, '.');
191 char const *const name_end =
192 last_dot != NULL ? last_dot : strchr(filename, '\0');
194 int const len = snprintf(buf, buflen, "%.*s%s",
195 (int)(name_end - filename), filename, newext);
197 if (len < 0 || buflen <= (size_t)len)
199 if (buflen <= (size_t)len)
201 panic("filename too long");
204 #include "builtins.h"
206 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
211 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
215 lexer_open_stream(in, input_name);
218 translation_unit_t *unit = finish_parsing();
222 static void lextest(FILE *in, const char *fname)
224 lexer_open_stream(in, fname);
227 lexer_next_preprocessing_token();
228 print_token(stdout, &lexer_token);
230 } while (lexer_token.type != T_EOF);
233 static void add_flag(struct obstack *obst, const char *format, ...)
238 va_start(ap, format);
242 vsnprintf(buf, sizeof(buf), format, ap);
245 obstack_1grow(obst, ' ');
247 obstack_1grow(obst, '"');
248 obstack_grow(obst, buf, len);
249 obstack_1grow(obst, '"');
251 /* escape stuff... */
252 for (char *c = buf; *c != '\0'; ++c) {
270 obstack_1grow(obst, '\\');
273 obstack_1grow(obst, *c);
280 static const char *type_to_string(type_t *type)
282 assert(type->kind == TYPE_ATOMIC);
283 return get_atomic_kind_name(type->atomic.akind);
286 static FILE *preprocess(const char *fname)
288 obstack_1grow(&cppflags_obst, '\0');
289 const char *flags = obstack_finish(&cppflags_obst);
291 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
293 /* setup default defines */
294 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
295 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
296 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
297 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
300 add_flag(&cppflags_obst, "-D__builtin_abort=abort");
301 add_flag(&cppflags_obst, "-D__builtin_abs=abs");
302 add_flag(&cppflags_obst, "-D__builtin_exit=exit");
303 add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
304 add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
305 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
306 add_flag(&cppflags_obst, "-D__builtin_memset=memset");
307 add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
308 add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
309 add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
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 size_t len = strlen(flags);
322 obstack_1grow(&cppflags_obst, ' ');
323 obstack_grow(&cppflags_obst, flags, len);
325 add_flag(&cppflags_obst, fname);
327 obstack_1grow(&cppflags_obst, '\0');
328 const char *buf = obstack_finish(&cppflags_obst);
333 FILE *f = popen(buf, "r");
335 fprintf(stderr, "invoking preprocessor failed\n");
342 static void assemble(const char *out, const char *in)
346 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
351 int err = system(buf);
353 fprintf(stderr, "assembler reported an error\n");
358 static void print_file_name(const char *file)
360 add_flag(&ldflags_obst, "-print-file-name=%s", file);
362 obstack_1grow(&ldflags_obst, '\0');
363 const char *flags = obstack_finish(&ldflags_obst);
365 /* construct commandline */
366 obstack_printf(&ldflags_obst, "%s ", LINKER);
367 obstack_printf(&ldflags_obst, "%s", flags);
368 obstack_1grow(&ldflags_obst, '\0');
370 char *commandline = obstack_finish(&ldflags_obst);
375 int err = system(commandline);
376 if (err != EXIT_SUCCESS) {
377 fprintf(stderr, "linker reported an error\n");
382 static const char *try_dir(const char *dir)
386 if (access(dir, R_OK | W_OK | X_OK) == 0)
391 static const char *get_tempdir(void)
393 static const char *tmpdir = NULL;
399 tmpdir = try_dir(getenv("TMPDIR"));
401 tmpdir = try_dir(getenv("TMP"));
403 tmpdir = try_dir(getenv("TEMP"));
407 tmpdir = try_dir(P_tmpdir);
411 tmpdir = try_dir("/var/tmp");
413 tmpdir = try_dir("/usr/tmp");
415 tmpdir = try_dir("/tmp");
424 /* cheap and nasty mkstemp replacement */
425 static int mkstemp(char *templ)
428 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
433 * an own version of tmpnam, which: writes in a buffer, emits no warnings
434 * during linking (like glibc/gnu ld do for tmpnam)...
436 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
438 const char *tempdir = get_tempdir();
440 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
442 int fd = mkstemp(buffer);
444 fprintf(stderr, "couldn't create temporary file: %s\n",
448 FILE *out = fdopen(fd, "w");
450 fprintf(stderr, "couldn't create temporary file FILE*\n");
454 file_list_entry_t *entry = xmalloc(sizeof(*entry));
455 memset(entry, 0, sizeof(*entry));
457 size_t name_len = strlen(buffer) + 1;
458 char *name = malloc(name_len);
459 memcpy(name, buffer, name_len);
462 entry->next = temp_files;
468 static void free_temp_files(void)
470 file_list_entry_t *entry = temp_files;
471 file_list_entry_t *next;
472 for ( ; entry != NULL; entry = next) {
476 free((char*) entry->name);
483 * Do the necessary lowering for compound parameters.
485 void lower_compound_params(void)
487 lower_params_t params;
489 params.def_ptr_alignment = 4;
490 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
491 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
492 params.find_pointer_type = NULL;
493 params.ret_compound_in_regs = NULL;
494 lower_calls_with_compounds(¶ms);
497 typedef enum compile_mode_t {
512 static void usage(const char *argv0)
514 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
517 static void print_cparser_version(void)
519 printf("cparser (%s) using libFirm (%u.%u",
520 cparser_REVISION, ir_get_version_major(),
521 ir_get_version_minor());
523 const char *revision = ir_get_version_revision();
524 if (revision[0] != 0) {
526 fputs(revision, stdout);
529 const char *build = ir_get_version_build();
532 fputs(build, stdout);
537 static void set_be_option(const char *arg)
539 int res = firm_be_option(arg);
544 static void set_option(const char *arg)
546 int res = firm_option(arg);
551 static void copy_file(FILE *dest, FILE *input)
555 while (!feof(input) && !ferror(dest)) {
556 size_t read = fread(buf, 1, sizeof(buf), input);
557 if (fwrite(buf, 1, read, dest) != read) {
558 perror("couldn't write output");
563 static inline bool streq(char const* a, char const* b)
565 return strcmp(a, b) == 0;
568 static inline bool strstart(char const* str, char const* start)
573 } while (*str++ == *start++);
577 static FILE *open_file(const char *filename)
579 if (streq(filename, "-")) {
583 FILE *in = fopen(filename, "r");
585 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
593 static filetype_t get_filetype_from_string(const char *string)
595 if (streq(string, "c") || streq(string, "c-header"))
597 if (streq(string, "c++") || streq(string, "c++-header"))
599 if (streq(string, "assembler"))
600 return FILETYPE_PREPROCESSED_ASSEMBLER;
601 if (streq(string, "assembler-with-cpp"))
602 return FILETYPE_ASSEMBLER;
603 if (streq(string, "none"))
604 return FILETYPE_AUTODETECT;
606 return FILETYPE_UNKNOWN;
609 static void init_os_support(void)
611 /* OS option must be set to the backend */
612 switch (firm_opt.os_support) {
613 case OS_SUPPORT_MINGW:
614 set_be_option("ia32-gasmode=mingw");
615 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
617 case OS_SUPPORT_LINUX:
618 set_be_option("ia32-gasmode=elf");
620 case OS_SUPPORT_MACHO:
621 set_be_option("ia32-gasmode=macho");
622 set_be_option("ia32-stackalign=4");
623 set_be_option("pic");
628 typedef enum lang_standard_t {
629 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
630 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
631 STANDARD_C89, /* ISO C90 (sic) */
632 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
633 STANDARD_C99, /* ISO C99 */
634 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
635 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
636 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
637 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
640 int main(int argc, char **argv)
644 const char *dumpfunction = NULL;
645 const char *print_file_name_file = NULL;
646 compile_mode_t mode = CompileAssembleLink;
648 int result = EXIT_SUCCESS;
649 char cpu_arch[16] = "ia32";
650 file_list_entry_t *files = NULL;
651 file_list_entry_t *last_file = NULL;
652 bool construct_dep_target = false;
653 struct obstack file_obst;
655 atexit(free_temp_files);
657 /* hack for now... */
658 if (strstr(argv[0], "pptest") != NULL) {
659 extern int pptest_main(int argc, char **argv);
660 return pptest_main(argc, argv);
663 obstack_init(&cppflags_obst);
664 obstack_init(&ldflags_obst);
665 obstack_init(&file_obst);
667 #define GET_ARG_AFTER(def, args) \
668 def = &arg[sizeof(args)-1]; \
669 if (def[0] == '\0') { \
672 fprintf(stderr, "error: expected argument after '" args "'\n"); \
673 argument_errors = true; \
677 if (def[0] == '-' && def[1] != '\0') { \
678 fprintf(stderr, "error: expected argument after '" args "'\n"); \
679 argument_errors = true; \
684 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
686 /* early options parsing (find out optimisation level and OS) */
687 for (int i = 1; i < argc; ++i) {
688 const char *arg = argv[i];
692 const char *option = &arg[1];
693 if (option[0] == 'O') {
694 sscanf(&option[1], "%d", &opt_level);
696 if (strcmp(arg, "-fwin32") == 0) {
697 firm_opt.os_support = OS_SUPPORT_MINGW;
698 } else if (strcmp(arg, "-fmac") == 0) {
699 firm_opt.os_support = OS_SUPPORT_MACHO;
700 } else if (strcmp(arg, "-flinux") == 0) {
701 firm_opt.os_support = OS_SUPPORT_LINUX;
705 /* set target/os specific stuff */
708 /* apply optimisation level */
711 set_option("no-opt");
714 set_option("no-inline");
718 set_option("strict-aliasing");
719 /* use_builtins = true; */
722 set_option("cond-eval");
723 set_option("if-conv");
726 set_option("inline");
727 set_option("deconv");
728 set_be_option("omitfp");
732 /* parse rest of options */
733 lang_standard_t standard = STANDARD_DEFAULT;
734 unsigned features_on = 0;
735 unsigned features_off = 0;
736 filetype_t forced_filetype = FILETYPE_AUTODETECT;
737 bool help_displayed = false;
738 bool argument_errors = false;
739 for (int i = 1; i < argc; ++i) {
740 const char *arg = argv[i];
741 if (arg[0] == '-' && arg[1] != '\0') {
743 const char *option = &arg[1];
744 if (option[0] == 'o') {
745 GET_ARG_AFTER(outname, "-o");
746 } else if (option[0] == 'g') {
747 set_be_option("debuginfo=stabs");
748 set_be_option("omitfp=no");
749 set_be_option("ia32-nooptcc=yes");
750 } else if (SINGLE_OPTION('c')) {
751 mode = CompileAssemble;
752 } else if (SINGLE_OPTION('E')) {
753 mode = PreprocessOnly;
754 } else if (SINGLE_OPTION('S')) {
756 } else if (option[0] == 'O') {
758 } else if (option[0] == 'I') {
760 GET_ARG_AFTER(opt, "-I");
761 add_flag(&cppflags_obst, "-I%s", opt);
762 } else if (option[0] == 'D') {
764 GET_ARG_AFTER(opt, "-D");
765 add_flag(&cppflags_obst, "-D%s", opt);
766 } else if (option[0] == 'U') {
768 GET_ARG_AFTER(opt, "-U");
769 add_flag(&cppflags_obst, "-U%s", opt);
770 } else if (option[0] == 'l') {
772 GET_ARG_AFTER(opt, "-l");
773 add_flag(&ldflags_obst, "-l%s", opt);
774 } else if (option[0] == 'L') {
776 GET_ARG_AFTER(opt, "-L");
777 add_flag(&ldflags_obst, "-L%s", opt);
778 } else if (SINGLE_OPTION('v')) {
780 } else if (SINGLE_OPTION('w')) {
781 memset(&warning, 0, sizeof(warning));
782 } else if (option[0] == 'x') {
784 GET_ARG_AFTER(opt, "-x");
785 forced_filetype = get_filetype_from_string(opt);
786 if (forced_filetype == FILETYPE_UNKNOWN) {
787 fprintf(stderr, "Unknown language '%s'\n", opt);
788 argument_errors = true;
790 } else if (streq(option, "M")) {
791 mode = PreprocessOnly;
792 add_flag(&cppflags_obst, "-M");
793 } else if (streq(option, "MMD") ||
794 streq(option, "MD")) {
795 construct_dep_target = true;
796 add_flag(&cppflags_obst, "-%s", option);
797 } else if (streq(option, "MM") ||
798 streq(option, "MP")) {
799 add_flag(&cppflags_obst, "-%s", option);
800 } else if (streq(option, "MT") ||
801 streq(option, "MQ") ||
802 streq(option, "MF")) {
804 GET_ARG_AFTER(opt, "-MT");
805 add_flag(&cppflags_obst, "-%s", option);
806 add_flag(&cppflags_obst, "%s", opt);
807 } else if (streq(option, "include")) {
809 GET_ARG_AFTER(opt, "-include");
810 add_flag(&cppflags_obst, "-include");
811 add_flag(&cppflags_obst, "%s", opt);
812 } else if (streq(option, "isystem")) {
814 GET_ARG_AFTER(opt, "-isystem");
815 add_flag(&cppflags_obst, "-isystem");
816 add_flag(&cppflags_obst, "%s", opt);
817 } else if (streq(option, "nostdinc")
818 || streq(option, "trigraphs")) {
819 /* pass these through to the preprocessor */
820 add_flag(&cppflags_obst, "%s", arg);
821 } else if (streq(option, "pipe")) {
822 /* here for gcc compatibility */
823 } else if (option[0] == 'f') {
824 char const *orig_opt;
825 GET_ARG_AFTER(orig_opt, "-f");
827 if (strstart(orig_opt, "align-loops=") ||
828 strstart(orig_opt, "align-jumps=") ||
829 strstart(orig_opt, "align-functions=")) {
830 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
831 } else if (strstart(orig_opt, "input-charset=")) {
832 char const* const encoding = strchr(orig_opt, '=') + 1;
833 select_input_encoding(encoding);
834 } else if (streq(orig_opt, "verbose-asm")) {
835 /* ignore: we always print verbose assembler */
837 char const *opt = orig_opt;
838 bool truth_value = true;
839 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
844 if (streq(opt, "builtins")) {
845 use_builtins = truth_value;
846 } else if (streq(opt, "dollars-in-identifiers")) {
847 allow_dollar_in_symbol = truth_value;
848 } else if (streq(opt, "omit-frame-pointer")) {
849 set_be_option(truth_value ? "omitfp" : "omitfp=no");
850 } else if (streq(opt, "short-wchar")) {
851 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
853 } else if (streq(opt, "signed-char")) {
854 char_is_signed = truth_value;
855 } else if (streq(opt, "strength-reduce")) {
856 firm_option(truth_value ? "strength-red" : "no-strength-red");
857 } else if (streq(opt, "syntax-only")) {
858 mode = truth_value ? ParseOnly : CompileAssembleLink;
859 } else if (streq(opt, "unsigned-char")) {
860 char_is_signed = !truth_value;
861 } else if (streq(opt, "fast-math") ||
862 streq(opt, "jump-tables") ||
863 streq(opt, "unroll-loops") ||
864 streq(opt, "expensive-optimizations") ||
865 streq(opt, "common") ||
867 streq(opt, "align-loops") ||
868 streq(opt, "align-jumps") ||
869 streq(opt, "align-functions")) {
870 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
872 int res = firm_option(orig_opt);
874 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
876 argument_errors = true;
878 } else if (res == -1) {
879 help_displayed = true;
883 } else if (option[0] == 'b') {
885 GET_ARG_AFTER(opt, "-b");
886 int res = firm_be_option(opt);
888 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
890 argument_errors = true;
891 } else if (res == -1) {
892 help_displayed = true;
893 } else if (strstart(opt, "isa=")) {
894 strncpy(cpu_arch, opt, sizeof(cpu_arch));
896 } else if (option[0] == 'W') {
897 if (strstart(option + 1, "p,")) {
898 // pass options directly to the preprocessor
900 GET_ARG_AFTER(opt, "-Wp,");
901 add_flag(&cppflags_obst, "-Wp,%s", opt);
902 } else if (strstart(option + 1, "l,")) {
903 // pass options directly to the linker
905 GET_ARG_AFTER(opt, "-Wl,");
906 add_flag(&ldflags_obst, "-Wl,%s", opt);
907 } else if (streq(option + 1, "no-trigraphs")
908 || streq(option + 1, "undef")) {
909 add_flag(&cppflags_obst, "%s", arg);
911 set_warning_opt(&option[1]);
913 } else if (option[0] == 'm') {
918 GET_ARG_AFTER(opt, "-m");
919 if (strstart(opt, "arch=")) {
920 GET_ARG_AFTER(opt, "-march=");
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;
926 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
927 int res = firm_be_option(arch_opt);
929 argument_errors = true;
931 } else if (strstart(opt, "tune=")) {
932 GET_ARG_AFTER(opt, "-mtune=");
933 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
934 int res = firm_be_option(arch_opt);
936 argument_errors = true;
937 } else if (strstart(opt, "cpu=")) {
938 GET_ARG_AFTER(opt, "-mcpu=");
939 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
940 int res = firm_be_option(arch_opt);
942 argument_errors = true;
943 } else if (strstart(opt, "fpmath=")) {
944 GET_ARG_AFTER(opt, "-mfpmath=");
945 if (streq(opt, "387"))
947 else if (streq(opt, "sse"))
950 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
951 argument_errors = true;
953 if (!argument_errors) {
954 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
955 int res = firm_be_option(arch_opt);
957 argument_errors = true;
959 } else if (strstart(opt, "preferred-stack-boundary=")) {
960 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
961 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
962 int res = firm_be_option(arch_opt);
964 argument_errors = true;
965 } else if (streq(opt, "omit-leaf-frame-pointer")) {
966 set_be_option("omitleaffp=1");
967 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
968 set_be_option("omitleaffp=0");
971 long int value = strtol(opt, &endptr, 10);
972 if (*endptr != '\0') {
973 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
974 argument_errors = true;
976 if (value != 16 && value != 32 && value != 64) {
977 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
978 argument_errors = true;
980 machine_size = (unsigned int)value;
983 } else if (streq(option, "pg")) {
984 set_be_option("gprof");
985 add_flag(&ldflags_obst, "-pg");
986 } else if (streq(option, "pedantic") ||
987 streq(option, "ansi")) {
988 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
989 } else if (streq(option, "shared")) {
990 add_flag(&ldflags_obst, "-shared");
991 } else if (strstart(option, "std=")) {
992 const char *const o = &option[4];
994 streq(o, "c++") ? STANDARD_CXX98 :
995 streq(o, "c++98") ? STANDARD_CXX98 :
996 streq(o, "c89") ? STANDARD_C89 :
997 streq(o, "c99") ? STANDARD_C99 :
998 streq(o, "c9x") ? STANDARD_C99 : // deprecated
999 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1000 streq(o, "gnu89") ? STANDARD_GNU89 :
1001 streq(o, "gnu99") ? STANDARD_GNU99 :
1002 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1003 streq(o, "iso9899:1990") ? STANDARD_C89 :
1004 streq(o, "iso9899:199409") ? STANDARD_C90 :
1005 streq(o, "iso9899:1999") ? STANDARD_C99 :
1006 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1007 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1008 } else if (streq(option, "version")) {
1009 print_cparser_version();
1010 } else if (strstart(option, "print-file-name=")) {
1011 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1012 } else if (option[0] == '-') {
1013 /* double dash option */
1015 if (streq(option, "gcc")) {
1016 features_on |= _GNUC;
1017 features_off &= ~_GNUC;
1018 } else if (streq(option, "no-gcc")) {
1019 features_on &= ~_GNUC;
1020 features_off |= _GNUC;
1021 } else if (streq(option, "ms")) {
1023 features_off &= ~_MS;
1024 } else if (streq(option, "no-ms")) {
1025 features_on &= ~_MS;
1026 features_off |= _MS;
1027 } else if (streq(option, "strict")) {
1029 } else if (streq(option, "lextest")) {
1031 } else if (streq(option, "benchmark")) {
1032 mode = BenchmarkParser;
1033 } else if (streq(option, "print-ast")) {
1035 } else if (streq(option, "print-implicit-cast")) {
1036 print_implicit_casts = true;
1037 } else if (streq(option, "print-parenthesis")) {
1038 print_parenthesis = true;
1039 } else if (streq(option, "print-fluffy")) {
1041 } else if (streq(option, "print-caml")) {
1043 } else if (streq(option, "print-jna")) {
1045 } else if (streq(option, "version")) {
1046 print_cparser_version();
1048 } else if (streq(option, "dump-function")) {
1051 fprintf(stderr, "error: "
1052 "expected argument after '--dump-function'\n");
1053 argument_errors = true;
1056 dumpfunction = argv[i];
1059 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1060 argument_errors = true;
1063 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1064 argument_errors = true;
1067 filetype_t type = forced_filetype;
1068 if (type == FILETYPE_AUTODETECT) {
1069 if (streq(arg, "-")) {
1070 /* - implicitly means C source file */
1073 const char *suffix = strrchr(arg, '.');
1074 /* Ensure there is at least one char before the suffix */
1075 if (suffix != NULL && suffix != arg) {
1078 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1079 streq(suffix, "a") ? FILETYPE_OBJECT :
1080 streq(suffix, "c") ? FILETYPE_C :
1081 streq(suffix, "cc") ? FILETYPE_CXX :
1082 streq(suffix, "cpp") ? FILETYPE_CXX :
1083 streq(suffix, "cxx") ? FILETYPE_CXX :
1084 streq(suffix, "h") ? FILETYPE_C :
1085 streq(suffix, "o") ? FILETYPE_OBJECT :
1086 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1087 streq(suffix, "so") ? FILETYPE_OBJECT :
1088 FILETYPE_AUTODETECT;
1092 if (type == FILETYPE_AUTODETECT) {
1093 fprintf(stderr, "'%s': file format not recognized\n", arg);
1098 file_list_entry_t *entry
1099 = obstack_alloc(&file_obst, sizeof(entry[0]));
1100 memset(entry, 0, sizeof(entry[0]));
1104 if (last_file != NULL) {
1105 last_file->next = entry;
1113 if (print_file_name_file != NULL) {
1114 print_file_name(print_file_name_file);
1118 if (files == NULL) {
1119 fprintf(stderr, "error: no input files specified\n");
1120 argument_errors = true;
1123 if (help_displayed) {
1124 return !argument_errors;
1126 if (argument_errors) {
1131 /* we do the lowering in ast2firm */
1132 firm_opt.lower_bitfields = FALSE;
1134 /* set the c_mode here, types depends on it */
1135 c_mode |= features_on;
1136 c_mode &= ~features_off;
1139 init_symbol_table();
1149 if (construct_dep_target) {
1150 if (outname != 0 && strlen(outname) >= 2) {
1151 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1153 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1156 dep_target[0] = '\0';
1159 char outnamebuf[4096];
1160 if (outname == NULL) {
1161 const char *filename = files->name;
1164 case BenchmarkParser:
1170 case PreprocessOnly:
1175 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1176 outname = outnamebuf;
1178 case CompileAssemble:
1179 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1180 outname = outnamebuf;
1183 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1185 outname = outnamebuf;
1187 case CompileAssembleLink:
1197 assert(outname != NULL);
1200 if (streq(outname, "-")) {
1203 out = fopen(outname, "w");
1205 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1211 file_list_entry_t *file;
1212 for (file = files; file != NULL; file = file->next) {
1213 char asm_tempfile[1024];
1214 const char *filename = file->name;
1215 filetype_t filetype = file->type;
1217 if (filetype == FILETYPE_OBJECT)
1221 if (mode == LexTest) {
1223 in = open_file(filename);
1224 lextest(in, filename);
1229 FILE *preprocessed_in = NULL;
1232 filetype = FILETYPE_PREPROCESSED_C;
1235 filetype = FILETYPE_PREPROCESSED_CXX;
1237 case FILETYPE_ASSEMBLER:
1238 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1239 add_flag(&cppflags_obst, "-x");
1240 add_flag(&cppflags_obst, "assembler-with-cpp");
1243 /* no support for input on FILE* yet */
1245 panic("internal compiler error: in for preprocessor != NULL");
1247 preprocessed_in = preprocess(filename);
1248 if (mode == PreprocessOnly) {
1249 copy_file(out, preprocessed_in);
1250 int result = pclose(preprocessed_in);
1252 /* remove output file in case of error */
1253 if (out != stdout && result != EXIT_SUCCESS) {
1259 in = preprocessed_in;
1267 if (mode == Compile) {
1270 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1274 in = open_file(filename);
1276 /* preprocess and compile */
1277 if (filetype == FILETYPE_PREPROCESSED_C) {
1278 char const* invalid_mode;
1281 case STANDARD_C89: c_mode = _C89; break;
1282 /* TODO ^v determine difference between these two */
1283 case STANDARD_C90: c_mode = _C89; break;
1284 case STANDARD_C99: c_mode = _C89 | _C99; break;
1285 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1289 "warning: command line option \"-std=%s\" is not valid for C\n",
1292 case STANDARD_DEFAULT:
1293 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1295 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1296 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1299 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1300 char const* invalid_mode;
1302 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1303 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1304 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1305 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1306 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1309 case STANDARD_CXX98: c_mode = _CXX; break;
1313 "warning: command line option \"-std=%s\" is not valid for C++\n",
1315 case STANDARD_DEFAULT:
1316 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1320 c_mode |= features_on;
1321 c_mode &= ~features_off;
1324 translation_unit_t *const unit = do_parsing(in, filename);
1326 /* prints the AST even if errors occurred */
1327 if (mode == PrintAst) {
1328 type_set_output(out);
1329 ast_set_output(out);
1333 if (error_count > 0) {
1334 /* parsing failed because of errors */
1335 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1337 result = EXIT_FAILURE;
1339 } else if (warning_count > 0) {
1340 fprintf(stderr, "%u warning(s)\n", warning_count);
1343 if (in == preprocessed_in) {
1344 int pp_result = pclose(preprocessed_in);
1345 if (pp_result != EXIT_SUCCESS) {
1346 /* remove output file */
1353 if (mode == BenchmarkParser) {
1355 } else if (mode == PrintFluffy) {
1356 write_fluffy_decls(out, unit);
1358 } else if (mode == PrintCaml) {
1359 write_caml_decls(out, unit);
1361 } else if (mode == PrintJna) {
1362 write_jna_decls(out, unit);
1366 translation_unit_to_firm(unit);
1368 if (mode == ParseOnly) {
1372 if (mode == CompileDump) {
1374 ident *id = new_id_from_str(dumpfunction);
1375 ir_graph *irg = NULL;
1376 int n_irgs = get_irp_n_irgs();
1377 for (int i = 0; i < n_irgs; ++i) {
1378 ir_graph *tirg = get_irp_irg(i);
1379 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1387 fprintf(stderr, "No graph for function '%s' found\n",
1392 dump_ir_block_graph_file(irg, out);
1397 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1398 have_const_functions);
1399 if (asm_out != out) {
1402 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1403 copy_file(asm_out, in);
1404 if (in == preprocessed_in) {
1405 int pp_result = pclose(preprocessed_in);
1406 if (pp_result != EXIT_SUCCESS) {
1407 /* remove output in error case */
1413 if (asm_out != out) {
1418 if (mode == Compile)
1421 /* if we're here then we have preprocessed assembly */
1422 filename = asm_tempfile;
1423 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1426 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1428 const char *filename_o;
1429 if (mode == CompileAssemble) {
1431 filename_o = outname;
1433 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1438 assemble(filename_o, filename);
1440 size_t len = strlen(filename_o) + 1;
1441 filename = obstack_copy(&file_obst, filename_o, len);
1442 filetype = FILETYPE_OBJECT;
1445 /* ok we're done here, process next file */
1446 file->name = filename;
1447 file->type = filetype;
1450 if (result != EXIT_SUCCESS) {
1456 /* link program file */
1457 if (mode == CompileAssembleLink) {
1458 obstack_1grow(&ldflags_obst, '\0');
1459 const char *flags = obstack_finish(&ldflags_obst);
1461 /* construct commandline */
1462 obstack_printf(&file_obst, "%s", LINKER);
1463 for (file_list_entry_t *entry = files; entry != NULL;
1464 entry = entry->next) {
1465 if (entry->type != FILETYPE_OBJECT)
1468 add_flag(&file_obst, "%s", entry->name);
1471 add_flag(&file_obst, "-o");
1472 add_flag(&file_obst, outname);
1473 obstack_printf(&file_obst, "%s", flags);
1474 obstack_1grow(&file_obst, '\0');
1476 char *commandline = obstack_finish(&file_obst);
1481 int err = system(commandline);
1482 if (err != EXIT_SUCCESS) {
1483 fprintf(stderr, "linker reported an error\n");
1488 obstack_free(&cppflags_obst, NULL);
1489 obstack_free(&ldflags_obst, NULL);
1490 obstack_free(&file_obst, NULL);
1500 exit_symbol_table();