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 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
105 unsigned int machine_size = 32;
106 bool byte_order_big_endian = false;
107 bool char_is_signed = true;
108 bool strict_mode = false;
109 bool use_builtins = false;
110 bool have_const_functions = false;
111 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
113 /* to switch on printing of implicit casts */
114 extern bool print_implicit_casts;
116 /* to switch on printing of parenthesis to indicate operator precedence */
117 extern bool print_parenthesis;
120 static struct obstack cppflags_obst, ldflags_obst;
121 static char dep_target[1024];
122 static const char *outname;
124 typedef enum lang_standard_t {
125 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
126 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
127 STANDARD_C89, /* ISO C90 (sic) */
128 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
129 STANDARD_C99, /* ISO C99 */
130 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
131 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
132 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
133 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
136 static lang_standard_t standard;
138 typedef struct file_list_entry_t file_list_entry_t;
140 typedef enum filetype_t {
143 FILETYPE_PREPROCESSED_C,
145 FILETYPE_PREPROCESSED_CXX,
147 FILETYPE_PREPROCESSED_ASSEMBLER,
153 struct file_list_entry_t {
154 const char *name; /**< filename or NULL for stdin */
156 file_list_entry_t *next;
159 static file_list_entry_t *temp_files;
161 static void initialize_firm(void)
166 static void get_output_name(char *buf, size_t buflen, const char *inputname,
169 if (inputname == NULL)
172 char const *const last_slash = strrchr(inputname, '/');
173 char const *const filename =
174 last_slash != NULL ? last_slash + 1 : inputname;
175 char const *const last_dot = strrchr(filename, '.');
176 char const *const name_end =
177 last_dot != NULL ? last_dot : strchr(filename, '\0');
179 int const len = snprintf(buf, buflen, "%.*s%s",
180 (int)(name_end - filename), filename, newext);
182 if (len < 0 || buflen <= (size_t)len)
184 if (buflen <= (size_t)len)
186 panic("filename too long");
189 #include "gen_builtins.h"
191 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
196 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
200 lexer_open_stream(in, input_name);
203 translation_unit_t *unit = finish_parsing();
207 static void lextest(FILE *in, const char *fname)
209 lexer_open_stream(in, fname);
212 lexer_next_preprocessing_token();
213 print_token(stdout, &lexer_token);
215 } while (lexer_token.type != T_EOF);
218 static void add_flag(struct obstack *obst, const char *format, ...)
223 va_start(ap, format);
227 vsnprintf(buf, sizeof(buf), format, ap);
230 obstack_1grow(obst, ' ');
232 obstack_1grow(obst, '"');
233 obstack_grow(obst, buf, len);
234 obstack_1grow(obst, '"');
236 /* escape stuff... */
237 for (char *c = buf; *c != '\0'; ++c) {
255 obstack_1grow(obst, '\\');
258 obstack_1grow(obst, *c);
265 static const char *type_to_string(type_t *type)
267 assert(type->kind == TYPE_ATOMIC);
268 return get_atomic_kind_name(type->atomic.akind);
271 static FILE *preprocess(const char *fname, filetype_t filetype)
273 static const char *common_flags = NULL;
275 if (common_flags == NULL) {
276 obstack_1grow(&cppflags_obst, '\0');
277 const char *flags = obstack_finish(&cppflags_obst);
279 /* setup default defines */
280 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
281 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
282 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
283 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
285 add_flag(&cppflags_obst, "-U__VERSION__");
286 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
288 if (flags[0] != '\0') {
289 size_t len = strlen(flags);
290 obstack_1grow(&cppflags_obst, ' ');
291 obstack_grow(&cppflags_obst, flags, len);
293 obstack_1grow(&cppflags_obst, '\0');
294 common_flags = obstack_finish(&cppflags_obst);
297 assert(obstack_object_size(&cppflags_obst) == 0);
299 const char *preprocessor = getenv("CPARSER_PP");
300 if (preprocessor == NULL)
301 preprocessor = PREPROCESSOR;
303 obstack_printf(&cppflags_obst, "%s ", preprocessor);
306 add_flag(&cppflags_obst, "-std=c99");
309 add_flag(&cppflags_obst, "-std=c++98");
311 case FILETYPE_ASSEMBLER:
312 add_flag(&cppflags_obst, "-x");
313 add_flag(&cppflags_obst, "assembler-with-cpp");
318 obstack_printf(&cppflags_obst, "%s", common_flags);
320 /* handle dependency generation */
321 if (dep_target[0] != '\0') {
322 add_flag(&cppflags_obst, "-MF");
323 add_flag(&cppflags_obst, dep_target);
324 if (outname != NULL) {
325 add_flag(&cppflags_obst, "-MQ");
326 add_flag(&cppflags_obst, outname);
329 add_flag(&cppflags_obst, fname);
331 obstack_1grow(&cppflags_obst, '\0');
332 char *buf = obstack_finish(&cppflags_obst);
337 FILE *f = popen(buf, "r");
339 fprintf(stderr, "invoking preprocessor failed\n");
343 /* we don't really need that anymore */
344 obstack_free(&cppflags_obst, buf);
349 static void assemble(const char *out, const char *in)
353 const char *assembler = getenv("CPARSER_AS");
354 if (assembler == NULL)
355 assembler = ASSEMBLER;
357 snprintf(buf, sizeof(buf), "%s %s -o %s", assembler, in, out);
362 int err = system(buf);
364 fprintf(stderr, "assembler reported an error\n");
369 static void print_file_name(const char *file)
371 add_flag(&ldflags_obst, "-print-file-name=%s", file);
373 obstack_1grow(&ldflags_obst, '\0');
374 const char *flags = obstack_finish(&ldflags_obst);
376 /* construct commandline */
377 const char *linker = getenv("CPARSER_LINK");
380 obstack_printf(&ldflags_obst, "%s ", linker);
381 obstack_printf(&ldflags_obst, "%s", flags);
382 obstack_1grow(&ldflags_obst, '\0');
384 char *commandline = obstack_finish(&ldflags_obst);
389 int err = system(commandline);
390 if (err != EXIT_SUCCESS) {
391 fprintf(stderr, "linker reported an error\n");
396 static const char *try_dir(const char *dir)
400 if (access(dir, R_OK | W_OK | X_OK) == 0)
405 static const char *get_tempdir(void)
407 static const char *tmpdir = NULL;
413 tmpdir = try_dir(getenv("TMPDIR"));
415 tmpdir = try_dir(getenv("TMP"));
417 tmpdir = try_dir(getenv("TEMP"));
421 tmpdir = try_dir(P_tmpdir);
425 tmpdir = try_dir("/var/tmp");
427 tmpdir = try_dir("/usr/tmp");
429 tmpdir = try_dir("/tmp");
438 /* cheap and nasty mkstemp replacement */
439 static int mkstemp(char *templ)
442 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
447 * an own version of tmpnam, which: writes in a buffer, emits no warnings
448 * during linking (like glibc/gnu ld do for tmpnam)...
450 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
452 const char *tempdir = get_tempdir();
454 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
456 int fd = mkstemp(buffer);
458 fprintf(stderr, "couldn't create temporary file: %s\n",
462 FILE *out = fdopen(fd, "w");
464 fprintf(stderr, "couldn't create temporary file FILE*\n");
468 file_list_entry_t *entry = xmalloc(sizeof(*entry));
469 memset(entry, 0, sizeof(*entry));
471 size_t name_len = strlen(buffer) + 1;
472 char *name = malloc(name_len);
473 memcpy(name, buffer, name_len);
476 entry->next = temp_files;
482 static void free_temp_files(void)
484 file_list_entry_t *entry = temp_files;
485 file_list_entry_t *next;
486 for ( ; entry != NULL; entry = next) {
490 free((char*) entry->name);
497 * Do the necessary lowering for compound parameters.
499 void lower_compound_params(void)
501 lower_params_t params;
503 params.def_ptr_alignment = 4;
504 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
505 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
506 params.find_pointer_type = NULL;
507 params.ret_compound_in_regs = NULL;
508 lower_calls_with_compounds(¶ms);
511 typedef enum compile_mode_t {
527 static void usage(const char *argv0)
529 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
532 static void print_cparser_version(void)
534 printf("cparser (%s) using libFirm (%u.%u",
535 cparser_REVISION, ir_get_version_major(),
536 ir_get_version_minor());
538 const char *revision = ir_get_version_revision();
539 if (revision[0] != 0) {
541 fputs(revision, stdout);
544 const char *build = ir_get_version_build();
547 fputs(build, stdout);
550 puts("This is free software; see the source for copying conditions. There is NO\n"
551 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
554 static void print_help(const char *argv0)
558 puts("\t-fhelp Display help about firm optimisation options");
559 puts("\t-bhelp Display help about firm backend options");
560 puts("A big number of gcc flags is also supported");
563 static void set_be_option(const char *arg)
565 int res = be_parse_arg(arg);
570 static void set_option(const char *arg)
572 int res = firm_option(arg);
577 static void copy_file(FILE *dest, FILE *input)
581 while (!feof(input) && !ferror(dest)) {
582 size_t read = fread(buf, 1, sizeof(buf), input);
583 if (fwrite(buf, 1, read, dest) != read) {
584 perror("couldn't write output");
589 static inline bool streq(char const* a, char const* b)
591 return strcmp(a, b) == 0;
594 static inline bool strstart(char const* str, char const* start)
599 } while (*str++ == *start++);
603 static FILE *open_file(const char *filename)
605 if (streq(filename, "-")) {
609 FILE *in = fopen(filename, "r");
611 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
619 static filetype_t get_filetype_from_string(const char *string)
621 if (streq(string, "c") || streq(string, "c-header"))
623 if (streq(string, "c++") || streq(string, "c++-header"))
625 if (streq(string, "assembler"))
626 return FILETYPE_PREPROCESSED_ASSEMBLER;
627 if (streq(string, "assembler-with-cpp"))
628 return FILETYPE_ASSEMBLER;
629 if (streq(string, "none"))
630 return FILETYPE_AUTODETECT;
632 return FILETYPE_UNKNOWN;
635 static void init_os_support(void)
637 /* OS option must be set to the backend */
638 switch (firm_opt.os_support) {
639 case OS_SUPPORT_MINGW:
640 set_be_option("ia32-gasmode=mingw");
641 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
643 case OS_SUPPORT_LINUX:
644 set_be_option("ia32-gasmode=elf");
646 case OS_SUPPORT_MACHO:
647 set_be_option("ia32-gasmode=macho");
648 set_be_option("ia32-stackalign=4");
649 set_be_option("pic");
654 int main(int argc, char **argv)
658 const char *dumpfunction = NULL;
659 const char *print_file_name_file = NULL;
660 compile_mode_t mode = CompileAssembleLink;
662 int result = EXIT_SUCCESS;
663 char cpu_arch[16] = "ia32";
664 file_list_entry_t *files = NULL;
665 file_list_entry_t *last_file = NULL;
666 bool construct_dep_target = false;
667 bool do_timing = false;
668 struct obstack file_obst;
670 atexit(free_temp_files);
672 /* hack for now... */
673 if (strstr(argv[0], "pptest") != NULL) {
674 extern int pptest_main(int argc, char **argv);
675 return pptest_main(argc, argv);
678 obstack_init(&cppflags_obst);
679 obstack_init(&ldflags_obst);
680 obstack_init(&file_obst);
682 #define GET_ARG_AFTER(def, args) \
683 def = &arg[sizeof(args)-1]; \
684 if (def[0] == '\0') { \
687 fprintf(stderr, "error: expected argument after '" args "'\n"); \
688 argument_errors = true; \
692 if (def[0] == '-' && def[1] != '\0') { \
693 fprintf(stderr, "error: expected argument after '" args "'\n"); \
694 argument_errors = true; \
699 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
701 /* early options parsing (find out optimisation level and OS) */
702 for (int i = 1; i < argc; ++i) {
703 const char *arg = argv[i];
707 const char *option = &arg[1];
708 if (option[0] == 'O') {
709 sscanf(&option[1], "%d", &opt_level);
711 if (strcmp(arg, "-fwin32") == 0) {
712 firm_opt.os_support = OS_SUPPORT_MINGW;
713 } else if (strcmp(arg, "-fmac") == 0) {
714 firm_opt.os_support = OS_SUPPORT_MACHO;
715 } else if (strcmp(arg, "-flinux") == 0) {
716 firm_opt.os_support = OS_SUPPORT_LINUX;
720 /* set target/os specific stuff */
723 /* apply optimisation level */
726 set_option("no-opt");
729 set_option("no-inline");
733 /* use_builtins = true; */
736 set_option("thread-jumps");
737 set_option("if-conversion");
740 set_option("strict-aliasing");
741 set_option("inline");
742 set_option("deconv");
743 set_be_option("omitfp");
747 /* parse rest of options */
748 standard = STANDARD_DEFAULT;
749 unsigned features_on = 0;
750 unsigned features_off = 0;
751 filetype_t forced_filetype = FILETYPE_AUTODETECT;
752 bool help_displayed = false;
753 bool argument_errors = false;
754 for (int i = 1; i < argc; ++i) {
755 const char *arg = argv[i];
756 if (arg[0] == '-' && arg[1] != '\0') {
758 const char *option = &arg[1];
759 if (option[0] == 'o') {
760 GET_ARG_AFTER(outname, "-o");
761 } else if (option[0] == 'g') {
762 set_be_option("debuginfo=stabs");
763 set_be_option("omitfp=no");
764 set_be_option("ia32-nooptcc=yes");
765 } else if (SINGLE_OPTION('c')) {
766 mode = CompileAssemble;
767 } else if (SINGLE_OPTION('E')) {
768 mode = PreprocessOnly;
769 } else if (SINGLE_OPTION('S')) {
771 } else if (option[0] == 'O') {
773 } else if (option[0] == 'I') {
775 GET_ARG_AFTER(opt, "-I");
776 add_flag(&cppflags_obst, "-I%s", opt);
777 } else if (option[0] == 'D') {
779 GET_ARG_AFTER(opt, "-D");
780 add_flag(&cppflags_obst, "-D%s", opt);
781 } else if (option[0] == 'U') {
783 GET_ARG_AFTER(opt, "-U");
784 add_flag(&cppflags_obst, "-U%s", opt);
785 } else if (option[0] == 'l') {
787 GET_ARG_AFTER(opt, "-l");
788 add_flag(&ldflags_obst, "-l%s", opt);
789 } else if (option[0] == 'L') {
791 GET_ARG_AFTER(opt, "-L");
792 add_flag(&ldflags_obst, "-L%s", opt);
793 } else if (SINGLE_OPTION('v')) {
795 } else if (SINGLE_OPTION('w')) {
796 memset(&warning, 0, sizeof(warning));
797 } else if (option[0] == 'x') {
799 GET_ARG_AFTER(opt, "-x");
800 forced_filetype = get_filetype_from_string(opt);
801 if (forced_filetype == FILETYPE_UNKNOWN) {
802 fprintf(stderr, "Unknown language '%s'\n", opt);
803 argument_errors = true;
805 } else if (streq(option, "M")) {
806 mode = PreprocessOnly;
807 add_flag(&cppflags_obst, "-M");
808 } else if (streq(option, "MMD") ||
809 streq(option, "MD")) {
810 construct_dep_target = true;
811 add_flag(&cppflags_obst, "-%s", option);
812 } else if (streq(option, "MM") ||
813 streq(option, "MP")) {
814 add_flag(&cppflags_obst, "-%s", option);
815 } else if (streq(option, "MT") ||
816 streq(option, "MQ") ||
817 streq(option, "MF")) {
819 GET_ARG_AFTER(opt, "-MT");
820 add_flag(&cppflags_obst, "-%s", option);
821 add_flag(&cppflags_obst, "%s", opt);
822 } else if (streq(option, "include")) {
824 GET_ARG_AFTER(opt, "-include");
825 add_flag(&cppflags_obst, "-include");
826 add_flag(&cppflags_obst, "%s", opt);
827 } else if (streq(option, "isystem")) {
829 GET_ARG_AFTER(opt, "-isystem");
830 add_flag(&cppflags_obst, "-isystem");
831 add_flag(&cppflags_obst, "%s", opt);
832 } else if (streq(option, "nostdinc")
833 || streq(option, "trigraphs")) {
834 /* pass these through to the preprocessor */
835 add_flag(&cppflags_obst, "%s", arg);
836 } else if (streq(option, "pipe")) {
837 /* here for gcc compatibility */
838 } else if (option[0] == 'f') {
839 char const *orig_opt;
840 GET_ARG_AFTER(orig_opt, "-f");
842 if (strstart(orig_opt, "input-charset=")) {
843 char const* const encoding = strchr(orig_opt, '=') + 1;
844 select_input_encoding(encoding);
845 } else if (streq(orig_opt, "verbose-asm")) {
846 /* ignore: we always print verbose assembler */
848 char const *opt = orig_opt;
849 bool truth_value = true;
850 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
855 if (streq(opt, "builtins")) {
856 use_builtins = truth_value;
857 } else if (streq(opt, "dollars-in-identifiers")) {
858 allow_dollar_in_symbol = truth_value;
859 } else if (streq(opt, "omit-frame-pointer")) {
860 set_be_option(truth_value ? "omitfp" : "omitfp=no");
861 } else if (streq(opt, "short-wchar")) {
862 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
864 } else if (streq(opt, "signed-char")) {
865 char_is_signed = truth_value;
866 } else if (streq(opt, "strength-reduce")) {
867 firm_option(truth_value ? "strength-red" : "no-strength-red");
868 } else if (streq(opt, "syntax-only")) {
869 mode = truth_value ? ParseOnly : CompileAssembleLink;
870 } else if (streq(opt, "unsigned-char")) {
871 char_is_signed = !truth_value;
872 } else if (truth_value == false &&
873 streq(opt, "asynchronous-unwind-tables")) {
874 /* nothing todo, a gcc feature which we don't support
875 * anyway was deactivated */
876 } else if (strstart(orig_opt, "align-loops=") ||
877 strstart(orig_opt, "align-jumps=") ||
878 strstart(orig_opt, "align-functions=")) {
879 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
880 } else if (strstart(orig_opt, "message-length=")) {
881 /* ignore: would only affect error message format */
882 } else if (streq(opt, "fast-math") ||
883 streq(opt, "jump-tables") ||
884 streq(opt, "expensive-optimizations") ||
885 streq(opt, "common") ||
886 streq(opt, "optimize-sibling-calls") ||
887 streq(opt, "align-loops") ||
888 streq(opt, "align-jumps") ||
889 streq(opt, "align-functions") ||
891 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
893 int res = firm_option(orig_opt);
895 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
897 argument_errors = true;
899 } else if (res == -1) {
900 help_displayed = true;
904 } else if (option[0] == 'b') {
906 GET_ARG_AFTER(opt, "-b");
907 int res = be_parse_arg(opt);
909 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
911 argument_errors = true;
912 } else if (res == -1) {
913 help_displayed = true;
914 } else if (strstart(opt, "isa=")) {
915 strncpy(cpu_arch, opt, sizeof(cpu_arch));
917 } else if (option[0] == 'W') {
918 if (option[1] == '\0') {
919 /* ignore -W, out defaults are already quiet verbose */
920 } else if (strstart(option + 1, "p,")) {
921 // pass options directly to the preprocessor
923 GET_ARG_AFTER(opt, "-Wp,");
924 add_flag(&cppflags_obst, "-Wp,%s", opt);
925 } else if (strstart(option + 1, "l,")) {
926 // pass options directly to the linker
928 GET_ARG_AFTER(opt, "-Wl,");
929 add_flag(&ldflags_obst, "-Wl,%s", opt);
930 } else if (streq(option + 1, "no-trigraphs")
931 || streq(option + 1, "undef")) {
932 add_flag(&cppflags_obst, "%s", arg);
934 set_warning_opt(&option[1]);
936 } else if (option[0] == 'm') {
941 GET_ARG_AFTER(opt, "-m");
942 if (strstart(opt, "arch=")) {
943 GET_ARG_AFTER(opt, "-march=");
944 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
945 int res = be_parse_arg(arch_opt);
947 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
948 argument_errors = true;
950 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
951 int res = be_parse_arg(arch_opt);
953 argument_errors = true;
955 } else if (strstart(opt, "tune=")) {
956 GET_ARG_AFTER(opt, "-mtune=");
957 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
958 int res = be_parse_arg(arch_opt);
960 argument_errors = true;
961 } else if (strstart(opt, "cpu=")) {
962 GET_ARG_AFTER(opt, "-mcpu=");
963 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
964 int res = be_parse_arg(arch_opt);
966 argument_errors = true;
967 } else if (strstart(opt, "fpmath=")) {
968 GET_ARG_AFTER(opt, "-mfpmath=");
969 if (streq(opt, "387"))
971 else if (streq(opt, "sse"))
974 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
975 argument_errors = true;
977 if (!argument_errors) {
978 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
979 int res = be_parse_arg(arch_opt);
981 argument_errors = true;
983 } else if (strstart(opt, "preferred-stack-boundary=")) {
984 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
985 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
986 int res = be_parse_arg(arch_opt);
988 argument_errors = true;
989 } else if (streq(opt, "omit-leaf-frame-pointer")) {
990 set_be_option("omitleaffp=1");
991 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
992 set_be_option("omitleaffp=0");
993 } else if (streq(opt, "rtd")) {
994 default_calling_convention = CC_STDCALL;
995 } else if (strstart(opt, "regparm=")) {
996 fprintf(stderr, "error: regparm convention not supported yet\n");
997 argument_errors = true;
998 } else if (streq(opt, "soft-float")) {
999 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1000 argument_errors = true;
1003 long int value = strtol(opt, &endptr, 10);
1004 if (*endptr != '\0') {
1005 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1006 argument_errors = true;
1008 if (value != 16 && value != 32 && value != 64) {
1009 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1010 argument_errors = true;
1012 machine_size = (unsigned int)value;
1015 } else if (streq(option, "pg")) {
1016 set_be_option("gprof");
1017 add_flag(&ldflags_obst, "-pg");
1018 } else if (streq(option, "pedantic") ||
1019 streq(option, "ansi")) {
1020 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1021 } else if (streq(option, "shared")) {
1022 add_flag(&ldflags_obst, "-shared");
1023 } else if (strstart(option, "std=")) {
1024 const char *const o = &option[4];
1026 streq(o, "c++") ? STANDARD_CXX98 :
1027 streq(o, "c++98") ? STANDARD_CXX98 :
1028 streq(o, "c89") ? STANDARD_C89 :
1029 streq(o, "c99") ? STANDARD_C99 :
1030 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1031 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1032 streq(o, "gnu89") ? STANDARD_GNU89 :
1033 streq(o, "gnu99") ? STANDARD_GNU99 :
1034 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1035 streq(o, "iso9899:1990") ? STANDARD_C89 :
1036 streq(o, "iso9899:199409") ? STANDARD_C90 :
1037 streq(o, "iso9899:1999") ? STANDARD_C99 :
1038 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1039 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1040 } else if (streq(option, "version")) {
1041 print_cparser_version();
1042 } else if (strstart(option, "print-file-name=")) {
1043 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1044 } else if (option[0] == '-') {
1045 /* double dash option */
1047 if (streq(option, "gcc")) {
1048 features_on |= _GNUC;
1049 features_off &= ~_GNUC;
1050 } else if (streq(option, "no-gcc")) {
1051 features_on &= ~_GNUC;
1052 features_off |= _GNUC;
1053 } else if (streq(option, "ms")) {
1055 features_off &= ~_MS;
1056 } else if (streq(option, "no-ms")) {
1057 features_on &= ~_MS;
1058 features_off |= _MS;
1059 } else if (streq(option, "strict")) {
1061 } else if (streq(option, "lextest")) {
1063 } else if (streq(option, "benchmark")) {
1064 mode = BenchmarkParser;
1065 } else if (streq(option, "print-ast")) {
1067 } else if (streq(option, "print-implicit-cast")) {
1068 print_implicit_casts = true;
1069 } else if (streq(option, "print-parenthesis")) {
1070 print_parenthesis = true;
1071 } else if (streq(option, "print-fluffy")) {
1073 } else if (streq(option, "print-caml")) {
1075 } else if (streq(option, "print-jna")) {
1077 } else if (streq(option, "time")) {
1079 } else if (streq(option, "version")) {
1080 print_cparser_version();
1082 } else if (streq(option, "help")) {
1083 print_help(argv[0]);
1084 help_displayed = true;
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_OBJECT; /* gcc behavior: unknown file extension means object file */
1133 file_list_entry_t *entry
1134 = obstack_alloc(&file_obst, sizeof(entry[0]));
1135 memset(entry, 0, sizeof(entry[0]));
1139 if (last_file != NULL) {
1140 last_file->next = entry;
1148 if (help_displayed) {
1149 return !argument_errors;
1152 if (print_file_name_file != NULL) {
1153 print_file_name(print_file_name_file);
1156 if (files == NULL) {
1157 fprintf(stderr, "error: no input files specified\n");
1158 argument_errors = true;
1161 if (argument_errors) {
1166 /* we do the lowering in ast2firm */
1167 firm_opt.lower_bitfields = FALSE;
1169 /* set the c_mode here, types depends on it */
1170 c_mode |= features_on;
1171 c_mode &= ~features_off;
1174 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1175 init_symbol_table();
1188 if (construct_dep_target) {
1189 if (outname != 0 && strlen(outname) >= 2) {
1190 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1192 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1195 dep_target[0] = '\0';
1198 char outnamebuf[4096];
1199 if (outname == NULL) {
1200 const char *filename = files->name;
1203 case BenchmarkParser:
1209 case PreprocessOnly:
1214 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1215 outname = outnamebuf;
1217 case CompileAssemble:
1218 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1219 outname = outnamebuf;
1222 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1224 outname = outnamebuf;
1226 case CompileExportIR:
1227 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1228 outname = outnamebuf;
1230 case CompileAssembleLink:
1240 assert(outname != NULL);
1243 if (streq(outname, "-")) {
1246 out = fopen(outname, "w");
1248 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1254 file_list_entry_t *file;
1255 bool already_constructed_firm = false;
1256 for (file = files; file != NULL; file = file->next) {
1257 char asm_tempfile[1024];
1258 const char *filename = file->name;
1259 filetype_t filetype = file->type;
1261 if (filetype == FILETYPE_OBJECT)
1265 if (mode == LexTest) {
1267 in = open_file(filename);
1268 lextest(in, filename);
1273 FILE *preprocessed_in = NULL;
1274 filetype_t next_filetype = filetype;
1277 next_filetype = FILETYPE_PREPROCESSED_C;
1280 next_filetype = FILETYPE_PREPROCESSED_CXX;
1282 case FILETYPE_ASSEMBLER:
1283 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1286 /* no support for input on FILE* yet */
1288 panic("internal compiler error: in for preprocessor != NULL");
1290 preprocessed_in = preprocess(filename, filetype);
1291 if (mode == PreprocessOnly) {
1292 copy_file(out, preprocessed_in);
1293 int result = pclose(preprocessed_in);
1295 /* remove output file in case of error */
1296 if (out != stdout && result != EXIT_SUCCESS) {
1302 in = preprocessed_in;
1303 filetype = next_filetype;
1311 if (mode == Compile) {
1314 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1318 in = open_file(filename);
1320 /* preprocess and compile */
1321 if (filetype == FILETYPE_PREPROCESSED_C) {
1322 char const* invalid_mode;
1325 case STANDARD_C89: c_mode = _C89; break;
1326 /* TODO determine difference between these two */
1327 case STANDARD_C90: c_mode = _C89; break;
1328 case STANDARD_C99: c_mode = _C89 | _C99; break;
1329 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1333 "warning: command line option \"-std=%s\" is not valid for C\n",
1336 case STANDARD_DEFAULT:
1337 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1339 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1340 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1343 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1344 char const* invalid_mode;
1346 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1347 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1348 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1349 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1350 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1353 case STANDARD_CXX98: c_mode = _CXX; break;
1357 "warning: command line option \"-std=%s\" is not valid for C++\n",
1359 case STANDARD_DEFAULT:
1360 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1364 c_mode |= features_on;
1365 c_mode &= ~features_off;
1367 /* do the actual parsing */
1368 ir_timer_t *t_parsing = ir_timer_new();
1369 timer_register(t_parsing, "Frontend: Parsing");
1370 timer_push(t_parsing);
1372 translation_unit_t *const unit = do_parsing(in, filename);
1373 timer_pop(t_parsing);
1375 /* prints the AST even if errors occurred */
1376 if (mode == PrintAst) {
1381 if (error_count > 0) {
1382 /* parsing failed because of errors */
1383 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1385 result = EXIT_FAILURE;
1387 } else if (warning_count > 0) {
1388 fprintf(stderr, "%u warning(s)\n", warning_count);
1391 if (in == preprocessed_in) {
1392 int pp_result = pclose(preprocessed_in);
1393 if (pp_result != EXIT_SUCCESS) {
1394 /* remove output file */
1401 if (mode == BenchmarkParser) {
1403 } else if (mode == PrintFluffy) {
1404 write_fluffy_decls(out, unit);
1406 } else if (mode == PrintCaml) {
1407 write_caml_decls(out, unit);
1409 } else if (mode == PrintJna) {
1410 write_jna_decls(out, unit);
1414 /* build the firm graph */
1415 ir_timer_t *t_construct = ir_timer_new();
1416 timer_register(t_construct, "Frontend: Graph construction");
1417 timer_push(t_construct);
1418 if (already_constructed_firm) {
1419 panic("compiling multiple files/translation units not possible");
1421 translation_unit_to_firm(unit);
1422 already_constructed_firm = true;
1423 timer_pop(t_construct);
1426 if (mode == ParseOnly) {
1430 if (mode == CompileDump) {
1432 ident *id = new_id_from_str(dumpfunction);
1433 ir_graph *irg = NULL;
1434 int n_irgs = get_irp_n_irgs();
1435 for (int i = 0; i < n_irgs; ++i) {
1436 ir_graph *tirg = get_irp_irg(i);
1437 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1445 fprintf(stderr, "No graph for function '%s' found\n",
1450 dump_ir_graph_file(out, irg);
1455 if (mode == CompileExportIR) {
1461 gen_firm_finish(asm_out, filename, have_const_functions);
1462 if (asm_out != out) {
1465 } else if (filetype == FILETYPE_IR) {
1467 ir_import(filename);
1469 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1470 copy_file(asm_out, in);
1471 if (in == preprocessed_in) {
1472 int pp_result = pclose(preprocessed_in);
1473 if (pp_result != EXIT_SUCCESS) {
1474 /* remove output in error case */
1480 if (asm_out != out) {
1485 if (mode == Compile)
1488 /* if we're here then we have preprocessed assembly */
1489 filename = asm_tempfile;
1490 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1493 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1495 const char *filename_o;
1496 if (mode == CompileAssemble) {
1498 filename_o = outname;
1500 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1505 assemble(filename_o, filename);
1507 size_t len = strlen(filename_o) + 1;
1508 filename = obstack_copy(&file_obst, filename_o, len);
1509 filetype = FILETYPE_OBJECT;
1512 /* ok we're done here, process next file */
1513 file->name = filename;
1514 file->type = filetype;
1517 if (result != EXIT_SUCCESS) {
1523 /* link program file */
1524 if (mode == CompileAssembleLink) {
1525 obstack_1grow(&ldflags_obst, '\0');
1526 const char *flags = obstack_finish(&ldflags_obst);
1528 /* construct commandline */
1529 const char *linker = getenv("CPARSER_LINK");
1532 obstack_printf(&file_obst, "%s", linker);
1533 for (file_list_entry_t *entry = files; entry != NULL;
1534 entry = entry->next) {
1535 if (entry->type != FILETYPE_OBJECT)
1538 add_flag(&file_obst, "%s", entry->name);
1541 add_flag(&file_obst, "-o");
1542 add_flag(&file_obst, outname);
1543 obstack_printf(&file_obst, "%s", flags);
1544 obstack_1grow(&file_obst, '\0');
1546 char *commandline = obstack_finish(&file_obst);
1551 int err = system(commandline);
1552 if (err != EXIT_SUCCESS) {
1553 fprintf(stderr, "linker reported an error\n");
1561 obstack_free(&cppflags_obst, NULL);
1562 obstack_free(&ldflags_obst, NULL);
1563 obstack_free(&file_obst, NULL);
1573 exit_symbol_table();