2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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 "driver/firm_timing.h"
75 #include "adt/error.h"
76 #include "wrappergen/write_fluffy.h"
77 #include "wrappergen/write_caml.h"
78 #include "wrappergen/write_jna.h"
86 #define PREPROCESSOR "gcc -E -m32 -U__STRICT_ANSI__"
88 #define PREPROCESSOR "cpp -m32 -U__STRICT_ANSI__"
93 #define LINKER "gcc -m32"
98 #define ASSEMBLER "gcc -m32 -c -xassembler"
100 #define ASSEMBLER "as --32"
104 /** The current c mode/dialect. */
105 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
107 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
108 unsigned int machine_size = 32;
110 /** true if the char type is signed. */
111 bool char_is_signed = true;
113 /** true for strict language checking. */
114 bool strict_mode = false;
116 /** use builtins for some libc functions */
117 bool use_builtins = false;
119 /** we have extern function with const attribute. */
120 bool have_const_functions = false;
122 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
124 /* to switch on printing of implicit casts */
125 extern bool print_implicit_casts;
127 /* to switch on printing of parenthesis to indicate operator precedence */
128 extern bool print_parenthesis;
131 static struct obstack cppflags_obst, ldflags_obst;
132 static char dep_target[1024];
133 static const char *outname;
135 typedef enum lang_standard_t {
136 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
137 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
138 STANDARD_C89, /* ISO C90 (sic) */
139 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
140 STANDARD_C99, /* ISO C99 */
141 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
142 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
143 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
144 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
147 static lang_standard_t standard;
149 typedef struct file_list_entry_t file_list_entry_t;
151 typedef enum filetype_t {
154 FILETYPE_PREPROCESSED_C,
156 FILETYPE_PREPROCESSED_CXX,
158 FILETYPE_PREPROCESSED_ASSEMBLER,
164 struct file_list_entry_t {
165 const char *name; /**< filename or NULL for stdin */
167 file_list_entry_t *next;
170 static file_list_entry_t *temp_files;
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 if (inputname == NULL)
186 char const *const last_slash = strrchr(inputname, '/');
187 char const *const filename =
188 last_slash != NULL ? last_slash + 1 : inputname;
189 char const *const last_dot = strrchr(filename, '.');
190 char const *const name_end =
191 last_dot != NULL ? last_dot : strchr(filename, '\0');
193 int const len = snprintf(buf, buflen, "%.*s%s",
194 (int)(name_end - filename), filename, newext);
196 if (len < 0 || buflen <= (size_t)len)
198 if (buflen <= (size_t)len)
200 panic("filename too long");
203 #include "gen_builtins.h"
205 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
210 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
214 lexer_open_stream(in, input_name);
217 translation_unit_t *unit = finish_parsing();
221 static void lextest(FILE *in, const char *fname)
223 lexer_open_stream(in, fname);
226 lexer_next_preprocessing_token();
227 print_token(stdout, &lexer_token);
229 } while (lexer_token.type != T_EOF);
232 static void add_flag(struct obstack *obst, const char *format, ...)
237 va_start(ap, format);
241 vsnprintf(buf, sizeof(buf), format, ap);
244 obstack_1grow(obst, ' ');
246 obstack_1grow(obst, '"');
247 obstack_grow(obst, buf, len);
248 obstack_1grow(obst, '"');
250 /* escape stuff... */
251 for (char *c = buf; *c != '\0'; ++c) {
269 obstack_1grow(obst, '\\');
272 obstack_1grow(obst, *c);
279 static const char *type_to_string(type_t *type)
281 assert(type->kind == TYPE_ATOMIC);
282 return get_atomic_kind_name(type->atomic.akind);
285 static FILE *preprocess(const char *fname, filetype_t filetype)
287 static const char *common_flags = NULL;
289 if (common_flags == NULL) {
290 obstack_1grow(&cppflags_obst, '\0');
291 const char *flags = obstack_finish(&cppflags_obst);
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));
299 add_flag(&cppflags_obst, "-U__VERSION__");
300 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
302 if (flags[0] != '\0') {
303 size_t len = strlen(flags);
304 obstack_1grow(&cppflags_obst, ' ');
305 obstack_grow(&cppflags_obst, flags, len);
307 obstack_1grow(&cppflags_obst, '\0');
308 common_flags = obstack_finish(&cppflags_obst);
311 assert(obstack_object_size(&cppflags_obst) == 0);
313 const char *preprocessor = getenv("CPARSER_PP");
314 if (preprocessor == NULL)
315 preprocessor = PREPROCESSOR;
317 obstack_printf(&cppflags_obst, "%s ", preprocessor);
320 add_flag(&cppflags_obst, "-std=c99");
323 add_flag(&cppflags_obst, "-std=c++98");
325 case FILETYPE_ASSEMBLER:
326 add_flag(&cppflags_obst, "-x");
327 add_flag(&cppflags_obst, "assembler-with-cpp");
332 obstack_printf(&cppflags_obst, "%s", common_flags);
334 /* handle dependency generation */
335 if (dep_target[0] != '\0') {
336 add_flag(&cppflags_obst, "-MF");
337 add_flag(&cppflags_obst, dep_target);
338 if (outname != NULL) {
339 add_flag(&cppflags_obst, "-MQ");
340 add_flag(&cppflags_obst, outname);
343 add_flag(&cppflags_obst, fname);
345 obstack_1grow(&cppflags_obst, '\0');
346 char *buf = obstack_finish(&cppflags_obst);
351 FILE *f = popen(buf, "r");
353 fprintf(stderr, "invoking preprocessor failed\n");
357 /* we don't really need that anymore */
358 obstack_free(&cppflags_obst, buf);
363 static void assemble(const char *out, const char *in)
367 const char *assembler = getenv("CPARSER_AS");
368 if (assembler == NULL)
369 assembler = ASSEMBLER;
371 snprintf(buf, sizeof(buf), "%s %s -o %s", assembler, in, out);
376 int err = system(buf);
378 fprintf(stderr, "assembler reported an error\n");
383 static void print_file_name(const char *file)
385 add_flag(&ldflags_obst, "-print-file-name=%s", file);
387 obstack_1grow(&ldflags_obst, '\0');
388 const char *flags = obstack_finish(&ldflags_obst);
390 /* construct commandline */
391 const char *linker = getenv("CPARSER_LINK");
394 obstack_printf(&ldflags_obst, "%s ", linker);
395 obstack_printf(&ldflags_obst, "%s", flags);
396 obstack_1grow(&ldflags_obst, '\0');
398 char *commandline = obstack_finish(&ldflags_obst);
403 int err = system(commandline);
404 if (err != EXIT_SUCCESS) {
405 fprintf(stderr, "linker reported an error\n");
410 static const char *try_dir(const char *dir)
414 if (access(dir, R_OK | W_OK | X_OK) == 0)
419 static const char *get_tempdir(void)
421 static const char *tmpdir = NULL;
427 tmpdir = try_dir(getenv("TMPDIR"));
429 tmpdir = try_dir(getenv("TMP"));
431 tmpdir = try_dir(getenv("TEMP"));
435 tmpdir = try_dir(P_tmpdir);
439 tmpdir = try_dir("/var/tmp");
441 tmpdir = try_dir("/usr/tmp");
443 tmpdir = try_dir("/tmp");
452 /* cheap and nasty mkstemp replacement */
453 static int mkstemp(char *templ)
456 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
461 * an own version of tmpnam, which: writes in a buffer, emits no warnings
462 * during linking (like glibc/gnu ld do for tmpnam)...
464 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
466 const char *tempdir = get_tempdir();
468 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
470 int fd = mkstemp(buffer);
472 fprintf(stderr, "couldn't create temporary file: %s\n",
476 FILE *out = fdopen(fd, "w");
478 fprintf(stderr, "couldn't create temporary file FILE*\n");
482 file_list_entry_t *entry = xmalloc(sizeof(*entry));
483 memset(entry, 0, sizeof(*entry));
485 size_t name_len = strlen(buffer) + 1;
486 char *name = malloc(name_len);
487 memcpy(name, buffer, name_len);
490 entry->next = temp_files;
496 static void free_temp_files(void)
498 file_list_entry_t *entry = temp_files;
499 file_list_entry_t *next;
500 for ( ; entry != NULL; entry = next) {
504 free((char*) entry->name);
511 * Do the necessary lowering for compound parameters.
513 void lower_compound_params(void)
515 lower_params_t params;
517 params.def_ptr_alignment = 4;
518 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
519 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
520 params.find_pointer_type = NULL;
521 params.ret_compound_in_regs = NULL;
522 lower_calls_with_compounds(¶ms);
525 typedef enum compile_mode_t {
541 static void usage(const char *argv0)
543 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
546 static void print_cparser_version(void)
548 printf("cparser (%s) using libFirm (%u.%u",
549 cparser_REVISION, ir_get_version_major(),
550 ir_get_version_minor());
552 const char *revision = ir_get_version_revision();
553 if (revision[0] != 0) {
555 fputs(revision, stdout);
558 const char *build = ir_get_version_build();
561 fputs(build, stdout);
564 puts("This is free software; see the source for copying conditions. There is NO\n"
565 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
568 static void set_be_option(const char *arg)
570 int res = be_parse_arg(arg);
575 static void set_option(const char *arg)
577 int res = firm_option(arg);
582 static void copy_file(FILE *dest, FILE *input)
586 while (!feof(input) && !ferror(dest)) {
587 size_t read = fread(buf, 1, sizeof(buf), input);
588 if (fwrite(buf, 1, read, dest) != read) {
589 perror("couldn't write output");
594 static inline bool streq(char const* a, char const* b)
596 return strcmp(a, b) == 0;
599 static inline bool strstart(char const* str, char const* start)
604 } while (*str++ == *start++);
608 static FILE *open_file(const char *filename)
610 if (streq(filename, "-")) {
614 FILE *in = fopen(filename, "r");
616 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
624 static filetype_t get_filetype_from_string(const char *string)
626 if (streq(string, "c") || streq(string, "c-header"))
628 if (streq(string, "c++") || streq(string, "c++-header"))
630 if (streq(string, "assembler"))
631 return FILETYPE_PREPROCESSED_ASSEMBLER;
632 if (streq(string, "assembler-with-cpp"))
633 return FILETYPE_ASSEMBLER;
634 if (streq(string, "none"))
635 return FILETYPE_AUTODETECT;
637 return FILETYPE_UNKNOWN;
640 static void init_os_support(void)
642 /* OS option must be set to the backend */
643 switch (firm_opt.os_support) {
644 case OS_SUPPORT_MINGW:
645 set_be_option("ia32-gasmode=mingw");
646 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
648 case OS_SUPPORT_LINUX:
649 set_be_option("ia32-gasmode=elf");
651 case OS_SUPPORT_MACHO:
652 set_be_option("ia32-gasmode=macho");
653 set_be_option("ia32-stackalign=4");
654 set_be_option("pic");
659 int main(int argc, char **argv)
663 const char *dumpfunction = NULL;
664 const char *print_file_name_file = NULL;
665 compile_mode_t mode = CompileAssembleLink;
667 int result = EXIT_SUCCESS;
668 char cpu_arch[16] = "ia32";
669 file_list_entry_t *files = NULL;
670 file_list_entry_t *last_file = NULL;
671 bool construct_dep_target = false;
672 bool do_timing = false;
673 struct obstack file_obst;
675 atexit(free_temp_files);
677 /* hack for now... */
678 if (strstr(argv[0], "pptest") != NULL) {
679 extern int pptest_main(int argc, char **argv);
680 return pptest_main(argc, argv);
683 obstack_init(&cppflags_obst);
684 obstack_init(&ldflags_obst);
685 obstack_init(&file_obst);
687 #define GET_ARG_AFTER(def, args) \
688 def = &arg[sizeof(args)-1]; \
689 if (def[0] == '\0') { \
692 fprintf(stderr, "error: expected argument after '" args "'\n"); \
693 argument_errors = true; \
697 if (def[0] == '-' && def[1] != '\0') { \
698 fprintf(stderr, "error: expected argument after '" args "'\n"); \
699 argument_errors = true; \
704 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
706 /* early options parsing (find out optimisation level and OS) */
707 for (int i = 1; i < argc; ++i) {
708 const char *arg = argv[i];
712 const char *option = &arg[1];
713 if (option[0] == 'O') {
714 sscanf(&option[1], "%d", &opt_level);
716 if (strcmp(arg, "-fwin32") == 0) {
717 firm_opt.os_support = OS_SUPPORT_MINGW;
718 } else if (strcmp(arg, "-fmac") == 0) {
719 firm_opt.os_support = OS_SUPPORT_MACHO;
720 } else if (strcmp(arg, "-flinux") == 0) {
721 firm_opt.os_support = OS_SUPPORT_LINUX;
725 /* set target/os specific stuff */
728 /* apply optimisation level */
731 set_option("no-opt");
734 set_option("no-inline");
738 /* use_builtins = true; */
741 set_option("thread-jumps");
742 set_option("if-conversion");
745 set_option("strict-aliasing");
746 set_option("inline");
747 set_option("deconv");
748 set_be_option("omitfp");
752 /* parse rest of options */
753 standard = STANDARD_DEFAULT;
754 unsigned features_on = 0;
755 unsigned features_off = 0;
756 filetype_t forced_filetype = FILETYPE_AUTODETECT;
757 bool help_displayed = false;
758 bool argument_errors = false;
759 for (int i = 1; i < argc; ++i) {
760 const char *arg = argv[i];
761 if (arg[0] == '-' && arg[1] != '\0') {
763 const char *option = &arg[1];
764 if (option[0] == 'o') {
765 GET_ARG_AFTER(outname, "-o");
766 } else if (option[0] == 'g') {
767 set_be_option("debuginfo=stabs");
768 set_be_option("omitfp=no");
769 set_be_option("ia32-nooptcc=yes");
770 } else if (SINGLE_OPTION('c')) {
771 mode = CompileAssemble;
772 } else if (SINGLE_OPTION('E')) {
773 mode = PreprocessOnly;
774 } else if (SINGLE_OPTION('S')) {
776 } else if (option[0] == 'O') {
778 } else if (option[0] == 'I') {
780 GET_ARG_AFTER(opt, "-I");
781 add_flag(&cppflags_obst, "-I%s", opt);
782 } else if (option[0] == 'D') {
784 GET_ARG_AFTER(opt, "-D");
785 add_flag(&cppflags_obst, "-D%s", opt);
786 } else if (option[0] == 'U') {
788 GET_ARG_AFTER(opt, "-U");
789 add_flag(&cppflags_obst, "-U%s", opt);
790 } else if (option[0] == 'l') {
792 GET_ARG_AFTER(opt, "-l");
793 add_flag(&ldflags_obst, "-l%s", opt);
794 } else if (option[0] == 'L') {
796 GET_ARG_AFTER(opt, "-L");
797 add_flag(&ldflags_obst, "-L%s", opt);
798 } else if (SINGLE_OPTION('v')) {
800 } else if (SINGLE_OPTION('w')) {
801 memset(&warning, 0, sizeof(warning));
802 } else if (option[0] == 'x') {
804 GET_ARG_AFTER(opt, "-x");
805 forced_filetype = get_filetype_from_string(opt);
806 if (forced_filetype == FILETYPE_UNKNOWN) {
807 fprintf(stderr, "Unknown language '%s'\n", opt);
808 argument_errors = true;
810 } else if (streq(option, "M")) {
811 mode = PreprocessOnly;
812 add_flag(&cppflags_obst, "-M");
813 } else if (streq(option, "MMD") ||
814 streq(option, "MD")) {
815 construct_dep_target = true;
816 add_flag(&cppflags_obst, "-%s", option);
817 } else if (streq(option, "MM") ||
818 streq(option, "MP")) {
819 add_flag(&cppflags_obst, "-%s", option);
820 } else if (streq(option, "MT") ||
821 streq(option, "MQ") ||
822 streq(option, "MF")) {
824 GET_ARG_AFTER(opt, "-MT");
825 add_flag(&cppflags_obst, "-%s", option);
826 add_flag(&cppflags_obst, "%s", opt);
827 } else if (streq(option, "include")) {
829 GET_ARG_AFTER(opt, "-include");
830 add_flag(&cppflags_obst, "-include");
831 add_flag(&cppflags_obst, "%s", opt);
832 } else if (streq(option, "isystem")) {
834 GET_ARG_AFTER(opt, "-isystem");
835 add_flag(&cppflags_obst, "-isystem");
836 add_flag(&cppflags_obst, "%s", opt);
837 } else if (streq(option, "nostdinc")
838 || streq(option, "trigraphs")) {
839 /* pass these through to the preprocessor */
840 add_flag(&cppflags_obst, "%s", arg);
841 } else if (streq(option, "pipe")) {
842 /* here for gcc compatibility */
843 } else if (option[0] == 'f') {
844 char const *orig_opt;
845 GET_ARG_AFTER(orig_opt, "-f");
847 if (strstart(orig_opt, "input-charset=")) {
848 char const* const encoding = strchr(orig_opt, '=') + 1;
849 select_input_encoding(encoding);
850 } else if (streq(orig_opt, "verbose-asm")) {
851 /* ignore: we always print verbose assembler */
853 char const *opt = orig_opt;
854 bool truth_value = true;
855 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
860 if (streq(opt, "builtins")) {
861 use_builtins = truth_value;
862 } else if (streq(opt, "dollars-in-identifiers")) {
863 allow_dollar_in_symbol = truth_value;
864 } else if (streq(opt, "omit-frame-pointer")) {
865 set_be_option(truth_value ? "omitfp" : "omitfp=no");
866 } else if (streq(opt, "short-wchar")) {
867 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
869 } else if (streq(opt, "signed-char")) {
870 char_is_signed = truth_value;
871 } else if (streq(opt, "strength-reduce")) {
872 firm_option(truth_value ? "strength-red" : "no-strength-red");
873 } else if (streq(opt, "syntax-only")) {
874 mode = truth_value ? ParseOnly : CompileAssembleLink;
875 } else if (streq(opt, "unsigned-char")) {
876 char_is_signed = !truth_value;
877 } else if (truth_value == false &&
878 streq(opt, "asynchronous-unwind-tables")) {
879 /* nothing todo, a gcc feature which we don't support
880 * anyway was deactivated */
881 } else if (strstart(orig_opt, "align-loops=") ||
882 strstart(orig_opt, "align-jumps=") ||
883 strstart(orig_opt, "align-functions=")) {
884 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
885 } else if (streq(opt, "fast-math") ||
886 streq(opt, "jump-tables") ||
887 streq(opt, "expensive-optimizations") ||
888 streq(opt, "common") ||
889 streq(opt, "optimize-sibling-calls") ||
890 streq(opt, "align-loops") ||
891 streq(opt, "align-jumps") ||
892 streq(opt, "align-functions") ||
894 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
896 int res = firm_option(orig_opt);
898 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
900 argument_errors = true;
902 } else if (res == -1) {
903 help_displayed = true;
907 } else if (option[0] == 'b') {
909 GET_ARG_AFTER(opt, "-b");
910 int res = be_parse_arg(opt);
912 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
914 argument_errors = true;
915 } else if (res == -1) {
916 help_displayed = true;
917 } else if (strstart(opt, "isa=")) {
918 strncpy(cpu_arch, opt, sizeof(cpu_arch));
920 } else if (option[0] == 'W') {
921 if (option[1] == '\0') {
922 /* ignore -W, out defaults are already quiet verbose */
923 } else if (strstart(option + 1, "p,")) {
924 // pass options directly to the preprocessor
926 GET_ARG_AFTER(opt, "-Wp,");
927 add_flag(&cppflags_obst, "-Wp,%s", opt);
928 } else if (strstart(option + 1, "l,")) {
929 // pass options directly to the linker
931 GET_ARG_AFTER(opt, "-Wl,");
932 add_flag(&ldflags_obst, "-Wl,%s", opt);
933 } else if (streq(option + 1, "no-trigraphs")
934 || streq(option + 1, "undef")) {
935 add_flag(&cppflags_obst, "%s", arg);
937 set_warning_opt(&option[1]);
939 } else if (option[0] == 'm') {
944 GET_ARG_AFTER(opt, "-m");
945 if (strstart(opt, "arch=")) {
946 GET_ARG_AFTER(opt, "-march=");
947 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
948 int res = be_parse_arg(arch_opt);
950 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
951 argument_errors = true;
953 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
954 int res = be_parse_arg(arch_opt);
956 argument_errors = true;
958 } else if (strstart(opt, "tune=")) {
959 GET_ARG_AFTER(opt, "-mtune=");
960 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
961 int res = be_parse_arg(arch_opt);
963 argument_errors = true;
964 } else if (strstart(opt, "cpu=")) {
965 GET_ARG_AFTER(opt, "-mcpu=");
966 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
967 int res = be_parse_arg(arch_opt);
969 argument_errors = true;
970 } else if (strstart(opt, "fpmath=")) {
971 GET_ARG_AFTER(opt, "-mfpmath=");
972 if (streq(opt, "387"))
974 else if (streq(opt, "sse"))
977 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
978 argument_errors = true;
980 if (!argument_errors) {
981 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
982 int res = be_parse_arg(arch_opt);
984 argument_errors = true;
986 } else if (strstart(opt, "preferred-stack-boundary=")) {
987 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
988 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
989 int res = be_parse_arg(arch_opt);
991 argument_errors = true;
992 } else if (streq(opt, "omit-leaf-frame-pointer")) {
993 set_be_option("omitleaffp=1");
994 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
995 set_be_option("omitleaffp=0");
996 } else if (streq(opt, "rtd")) {
997 default_calling_convention = CC_STDCALL;
998 } else if (strstart(opt, "regparm=")) {
999 fprintf(stderr, "error: regparm convention not supported yet\n");
1000 argument_errors = true;
1001 } else if (streq(opt, "soft-float")) {
1002 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1003 argument_errors = true;
1006 long int value = strtol(opt, &endptr, 10);
1007 if (*endptr != '\0') {
1008 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1009 argument_errors = true;
1011 if (value != 16 && value != 32 && value != 64) {
1012 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1013 argument_errors = true;
1015 machine_size = (unsigned int)value;
1018 } else if (streq(option, "pg")) {
1019 set_be_option("gprof");
1020 add_flag(&ldflags_obst, "-pg");
1021 } else if (streq(option, "pedantic") ||
1022 streq(option, "ansi")) {
1023 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1024 } else if (streq(option, "shared")) {
1025 add_flag(&ldflags_obst, "-shared");
1026 } else if (strstart(option, "std=")) {
1027 const char *const o = &option[4];
1029 streq(o, "c++") ? STANDARD_CXX98 :
1030 streq(o, "c++98") ? STANDARD_CXX98 :
1031 streq(o, "c89") ? STANDARD_C89 :
1032 streq(o, "c99") ? STANDARD_C99 :
1033 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1034 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1035 streq(o, "gnu89") ? STANDARD_GNU89 :
1036 streq(o, "gnu99") ? STANDARD_GNU99 :
1037 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1038 streq(o, "iso9899:1990") ? STANDARD_C89 :
1039 streq(o, "iso9899:199409") ? STANDARD_C90 :
1040 streq(o, "iso9899:1999") ? STANDARD_C99 :
1041 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1042 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1043 } else if (streq(option, "version")) {
1044 print_cparser_version();
1045 } else if (strstart(option, "print-file-name=")) {
1046 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1047 } else if (option[0] == '-') {
1048 /* double dash option */
1050 if (streq(option, "gcc")) {
1051 features_on |= _GNUC;
1052 features_off &= ~_GNUC;
1053 } else if (streq(option, "no-gcc")) {
1054 features_on &= ~_GNUC;
1055 features_off |= _GNUC;
1056 } else if (streq(option, "ms")) {
1058 features_off &= ~_MS;
1059 } else if (streq(option, "no-ms")) {
1060 features_on &= ~_MS;
1061 features_off |= _MS;
1062 } else if (streq(option, "strict")) {
1064 } else if (streq(option, "lextest")) {
1066 } else if (streq(option, "benchmark")) {
1067 mode = BenchmarkParser;
1068 } else if (streq(option, "print-ast")) {
1070 } else if (streq(option, "print-implicit-cast")) {
1071 print_implicit_casts = true;
1072 } else if (streq(option, "print-parenthesis")) {
1073 print_parenthesis = true;
1074 } else if (streq(option, "print-fluffy")) {
1076 } else if (streq(option, "print-caml")) {
1078 } else if (streq(option, "print-jna")) {
1080 } else if (streq(option, "time")) {
1082 } else if (streq(option, "version")) {
1083 print_cparser_version();
1085 } else if (streq(option, "dump-function")) {
1088 fprintf(stderr, "error: "
1089 "expected argument after '--dump-function'\n");
1090 argument_errors = true;
1093 dumpfunction = argv[i];
1095 } else if (streq(option, "export-ir")) {
1096 mode = CompileExportIR;
1098 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1099 argument_errors = true;
1102 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1103 argument_errors = true;
1106 filetype_t type = forced_filetype;
1107 if (type == FILETYPE_AUTODETECT) {
1108 if (streq(arg, "-")) {
1109 /* - implicitly means C source file */
1112 const char *suffix = strrchr(arg, '.');
1113 /* Ensure there is at least one char before the suffix */
1114 if (suffix != NULL && suffix != arg) {
1117 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1118 streq(suffix, "a") ? FILETYPE_OBJECT :
1119 streq(suffix, "c") ? FILETYPE_C :
1120 streq(suffix, "cc") ? FILETYPE_CXX :
1121 streq(suffix, "cpp") ? FILETYPE_CXX :
1122 streq(suffix, "cxx") ? FILETYPE_CXX :
1123 streq(suffix, "h") ? FILETYPE_C :
1124 streq(suffix, "ir") ? FILETYPE_IR :
1125 streq(suffix, "o") ? FILETYPE_OBJECT :
1126 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1127 streq(suffix, "so") ? FILETYPE_OBJECT :
1128 FILETYPE_AUTODETECT;
1132 if (type == FILETYPE_AUTODETECT) {
1133 fprintf(stderr, "'%s': file format not recognized\n", arg);
1138 file_list_entry_t *entry
1139 = obstack_alloc(&file_obst, sizeof(entry[0]));
1140 memset(entry, 0, sizeof(entry[0]));
1144 if (last_file != NULL) {
1145 last_file->next = entry;
1153 if (print_file_name_file != NULL) {
1154 print_file_name(print_file_name_file);
1158 if (files == NULL) {
1159 fprintf(stderr, "error: no input files specified\n");
1160 argument_errors = true;
1163 if (help_displayed) {
1164 return !argument_errors;
1166 if (argument_errors) {
1171 /* we do the lowering in ast2firm */
1172 firm_opt.lower_bitfields = FALSE;
1174 /* set the c_mode here, types depends on it */
1175 c_mode |= features_on;
1176 c_mode &= ~features_off;
1179 init_symbol_table();
1192 if (construct_dep_target) {
1193 if (outname != 0 && strlen(outname) >= 2) {
1194 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1196 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1199 dep_target[0] = '\0';
1202 char outnamebuf[4096];
1203 if (outname == NULL) {
1204 const char *filename = files->name;
1207 case BenchmarkParser:
1213 case PreprocessOnly:
1218 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1219 outname = outnamebuf;
1221 case CompileAssemble:
1222 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1223 outname = outnamebuf;
1226 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1228 outname = outnamebuf;
1230 case CompileExportIR:
1231 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1232 outname = outnamebuf;
1234 case CompileAssembleLink:
1244 assert(outname != NULL);
1247 if (streq(outname, "-")) {
1250 out = fopen(outname, "w");
1252 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1258 file_list_entry_t *file;
1259 bool already_constructed_firm = false;
1260 for (file = files; file != NULL; file = file->next) {
1261 char asm_tempfile[1024];
1262 const char *filename = file->name;
1263 filetype_t filetype = file->type;
1265 if (filetype == FILETYPE_OBJECT)
1269 if (mode == LexTest) {
1271 in = open_file(filename);
1272 lextest(in, filename);
1277 FILE *preprocessed_in = NULL;
1278 filetype_t next_filetype = filetype;
1281 next_filetype = FILETYPE_PREPROCESSED_C;
1284 next_filetype = FILETYPE_PREPROCESSED_CXX;
1286 case FILETYPE_ASSEMBLER:
1287 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1290 /* no support for input on FILE* yet */
1292 panic("internal compiler error: in for preprocessor != NULL");
1294 preprocessed_in = preprocess(filename, filetype);
1295 if (mode == PreprocessOnly) {
1296 copy_file(out, preprocessed_in);
1297 int result = pclose(preprocessed_in);
1299 /* remove output file in case of error */
1300 if (out != stdout && result != EXIT_SUCCESS) {
1306 in = preprocessed_in;
1307 filetype = next_filetype;
1315 if (mode == Compile) {
1318 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1322 in = open_file(filename);
1324 /* preprocess and compile */
1325 if (filetype == FILETYPE_PREPROCESSED_C) {
1326 char const* invalid_mode;
1329 case STANDARD_C89: c_mode = _C89; break;
1330 /* TODO determine difference between these two */
1331 case STANDARD_C90: c_mode = _C89; break;
1332 case STANDARD_C99: c_mode = _C89 | _C99; break;
1333 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1337 "warning: command line option \"-std=%s\" is not valid for C\n",
1340 case STANDARD_DEFAULT:
1341 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1343 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1344 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1347 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1348 char const* invalid_mode;
1350 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1351 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1352 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1353 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1354 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1357 case STANDARD_CXX98: c_mode = _CXX; break;
1361 "warning: command line option \"-std=%s\" is not valid for C++\n",
1363 case STANDARD_DEFAULT:
1364 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1368 c_mode |= features_on;
1369 c_mode &= ~features_off;
1371 /* do the actual parsing */
1372 ir_timer_t *t_parsing = ir_timer_new();
1373 timer_register(t_parsing, "Frontend: Parsing");
1374 timer_push(t_parsing);
1376 translation_unit_t *const unit = do_parsing(in, filename);
1377 timer_pop(t_parsing);
1379 /* prints the AST even if errors occurred */
1380 if (mode == PrintAst) {
1385 if (error_count > 0) {
1386 /* parsing failed because of errors */
1387 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1389 result = EXIT_FAILURE;
1391 } else if (warning_count > 0) {
1392 fprintf(stderr, "%u warning(s)\n", warning_count);
1395 if (in == preprocessed_in) {
1396 int pp_result = pclose(preprocessed_in);
1397 if (pp_result != EXIT_SUCCESS) {
1398 /* remove output file */
1405 if (mode == BenchmarkParser) {
1407 } else if (mode == PrintFluffy) {
1408 write_fluffy_decls(out, unit);
1410 } else if (mode == PrintCaml) {
1411 write_caml_decls(out, unit);
1413 } else if (mode == PrintJna) {
1414 write_jna_decls(out, unit);
1418 /* build the firm graph */
1419 ir_timer_t *t_construct = ir_timer_new();
1420 timer_register(t_construct, "Frontend: Graph construction");
1421 timer_push(t_construct);
1422 if (already_constructed_firm) {
1423 panic("compiling multiple files/translation units not possible");
1425 translation_unit_to_firm(unit);
1426 already_constructed_firm = true;
1427 timer_pop(t_construct);
1430 if (mode == ParseOnly) {
1434 if (mode == CompileDump) {
1436 ident *id = new_id_from_str(dumpfunction);
1437 ir_graph *irg = NULL;
1438 int n_irgs = get_irp_n_irgs();
1439 for (int i = 0; i < n_irgs; ++i) {
1440 ir_graph *tirg = get_irp_irg(i);
1441 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1449 fprintf(stderr, "No graph for function '%s' found\n",
1454 dump_ir_block_graph_file(irg, out);
1459 if (mode == CompileExportIR) {
1465 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1466 have_const_functions);
1467 if (asm_out != out) {
1470 } else if (filetype == FILETYPE_IR) {
1472 ir_import(filename);
1474 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1475 copy_file(asm_out, in);
1476 if (in == preprocessed_in) {
1477 int pp_result = pclose(preprocessed_in);
1478 if (pp_result != EXIT_SUCCESS) {
1479 /* remove output in error case */
1485 if (asm_out != out) {
1490 if (mode == Compile)
1493 /* if we're here then we have preprocessed assembly */
1494 filename = asm_tempfile;
1495 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1498 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1500 const char *filename_o;
1501 if (mode == CompileAssemble) {
1503 filename_o = outname;
1505 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1510 assemble(filename_o, filename);
1512 size_t len = strlen(filename_o) + 1;
1513 filename = obstack_copy(&file_obst, filename_o, len);
1514 filetype = FILETYPE_OBJECT;
1517 /* ok we're done here, process next file */
1518 file->name = filename;
1519 file->type = filetype;
1522 if (result != EXIT_SUCCESS) {
1528 /* link program file */
1529 if (mode == CompileAssembleLink) {
1530 obstack_1grow(&ldflags_obst, '\0');
1531 const char *flags = obstack_finish(&ldflags_obst);
1533 /* construct commandline */
1534 const char *linker = getenv("CPARSER_LINK");
1537 obstack_printf(&file_obst, "%s", linker);
1538 for (file_list_entry_t *entry = files; entry != NULL;
1539 entry = entry->next) {
1540 if (entry->type != FILETYPE_OBJECT)
1543 add_flag(&file_obst, "%s", entry->name);
1546 add_flag(&file_obst, "-o");
1547 add_flag(&file_obst, outname);
1548 obstack_printf(&file_obst, "%s", flags);
1549 obstack_1grow(&file_obst, '\0');
1551 char *commandline = obstack_finish(&file_obst);
1556 int err = system(commandline);
1557 if (err != EXIT_SUCCESS) {
1558 fprintf(stderr, "linker reported an error\n");
1566 obstack_free(&cppflags_obst, NULL);
1567 obstack_free(&ldflags_obst, NULL);
1568 obstack_free(&file_obst, NULL);
1578 exit_symbol_table();