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)
177 static void get_output_name(char *buf, size_t buflen, const char *inputname,
180 if (inputname == NULL)
183 char const *const last_slash = strrchr(inputname, '/');
184 char const *const filename =
185 last_slash != NULL ? last_slash + 1 : inputname;
186 char const *const last_dot = strrchr(filename, '.');
187 char const *const name_end =
188 last_dot != NULL ? last_dot : strchr(filename, '\0');
190 int const len = snprintf(buf, buflen, "%.*s%s",
191 (int)(name_end - filename), filename, newext);
193 if (len < 0 || buflen <= (size_t)len)
195 if (buflen <= (size_t)len)
197 panic("filename too long");
200 #include "gen_builtins.h"
202 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
207 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
211 lexer_open_stream(in, input_name);
214 translation_unit_t *unit = finish_parsing();
218 static void lextest(FILE *in, const char *fname)
220 lexer_open_stream(in, fname);
223 lexer_next_preprocessing_token();
224 print_token(stdout, &lexer_token);
226 } while (lexer_token.type != T_EOF);
229 static void add_flag(struct obstack *obst, const char *format, ...)
234 va_start(ap, format);
238 vsnprintf(buf, sizeof(buf), format, ap);
241 obstack_1grow(obst, ' ');
243 obstack_1grow(obst, '"');
244 obstack_grow(obst, buf, len);
245 obstack_1grow(obst, '"');
247 /* escape stuff... */
248 for (char *c = buf; *c != '\0'; ++c) {
266 obstack_1grow(obst, '\\');
269 obstack_1grow(obst, *c);
276 static const char *type_to_string(type_t *type)
278 assert(type->kind == TYPE_ATOMIC);
279 return get_atomic_kind_name(type->atomic.akind);
282 static FILE *preprocess(const char *fname, filetype_t filetype)
284 static const char *common_flags = NULL;
286 if (common_flags == NULL) {
287 obstack_1grow(&cppflags_obst, '\0');
288 const char *flags = obstack_finish(&cppflags_obst);
290 /* setup default defines */
291 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
292 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
293 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
294 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
296 add_flag(&cppflags_obst, "-U__VERSION__");
297 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
299 if (flags[0] != '\0') {
300 size_t len = strlen(flags);
301 obstack_1grow(&cppflags_obst, ' ');
302 obstack_grow(&cppflags_obst, flags, len);
304 obstack_1grow(&cppflags_obst, '\0');
305 common_flags = obstack_finish(&cppflags_obst);
308 assert(obstack_object_size(&cppflags_obst) == 0);
310 const char *preprocessor = getenv("CPARSER_PP");
311 if (preprocessor == NULL)
312 preprocessor = PREPROCESSOR;
314 obstack_printf(&cppflags_obst, "%s ", preprocessor);
317 add_flag(&cppflags_obst, "-std=c99");
320 add_flag(&cppflags_obst, "-std=c++98");
322 case FILETYPE_ASSEMBLER:
323 add_flag(&cppflags_obst, "-x");
324 add_flag(&cppflags_obst, "assembler-with-cpp");
329 obstack_printf(&cppflags_obst, "%s", common_flags);
331 /* handle dependency generation */
332 if (dep_target[0] != '\0') {
333 add_flag(&cppflags_obst, "-MF");
334 add_flag(&cppflags_obst, dep_target);
335 if (outname != NULL) {
336 add_flag(&cppflags_obst, "-MQ");
337 add_flag(&cppflags_obst, outname);
340 add_flag(&cppflags_obst, fname);
342 obstack_1grow(&cppflags_obst, '\0');
343 char *buf = obstack_finish(&cppflags_obst);
348 FILE *f = popen(buf, "r");
350 fprintf(stderr, "invoking preprocessor failed\n");
354 /* we don't really need that anymore */
355 obstack_free(&cppflags_obst, buf);
360 static void assemble(const char *out, const char *in)
364 const char *assembler = getenv("CPARSER_AS");
365 if (assembler == NULL)
366 assembler = ASSEMBLER;
368 snprintf(buf, sizeof(buf), "%s %s -o %s", assembler, in, out);
373 int err = system(buf);
375 fprintf(stderr, "assembler reported an error\n");
380 static void print_file_name(const char *file)
382 add_flag(&ldflags_obst, "-print-file-name=%s", file);
384 obstack_1grow(&ldflags_obst, '\0');
385 const char *flags = obstack_finish(&ldflags_obst);
387 /* construct commandline */
388 const char *linker = getenv("CPARSER_LINK");
391 obstack_printf(&ldflags_obst, "%s ", linker);
392 obstack_printf(&ldflags_obst, "%s", flags);
393 obstack_1grow(&ldflags_obst, '\0');
395 char *commandline = obstack_finish(&ldflags_obst);
400 int err = system(commandline);
401 if (err != EXIT_SUCCESS) {
402 fprintf(stderr, "linker reported an error\n");
407 static const char *try_dir(const char *dir)
411 if (access(dir, R_OK | W_OK | X_OK) == 0)
416 static const char *get_tempdir(void)
418 static const char *tmpdir = NULL;
424 tmpdir = try_dir(getenv("TMPDIR"));
426 tmpdir = try_dir(getenv("TMP"));
428 tmpdir = try_dir(getenv("TEMP"));
432 tmpdir = try_dir(P_tmpdir);
436 tmpdir = try_dir("/var/tmp");
438 tmpdir = try_dir("/usr/tmp");
440 tmpdir = try_dir("/tmp");
449 /* cheap and nasty mkstemp replacement */
450 static int mkstemp(char *templ)
453 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
458 * an own version of tmpnam, which: writes in a buffer, emits no warnings
459 * during linking (like glibc/gnu ld do for tmpnam)...
461 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
463 const char *tempdir = get_tempdir();
465 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
467 int fd = mkstemp(buffer);
469 fprintf(stderr, "couldn't create temporary file: %s\n",
473 FILE *out = fdopen(fd, "w");
475 fprintf(stderr, "couldn't create temporary file FILE*\n");
479 file_list_entry_t *entry = xmalloc(sizeof(*entry));
480 memset(entry, 0, sizeof(*entry));
482 size_t name_len = strlen(buffer) + 1;
483 char *name = malloc(name_len);
484 memcpy(name, buffer, name_len);
487 entry->next = temp_files;
493 static void free_temp_files(void)
495 file_list_entry_t *entry = temp_files;
496 file_list_entry_t *next;
497 for ( ; entry != NULL; entry = next) {
501 free((char*) entry->name);
508 * Do the necessary lowering for compound parameters.
510 void lower_compound_params(void)
512 lower_params_t params;
514 params.def_ptr_alignment = 4;
515 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
516 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
517 params.find_pointer_type = NULL;
518 params.ret_compound_in_regs = NULL;
519 lower_calls_with_compounds(¶ms);
522 typedef enum compile_mode_t {
538 static void usage(const char *argv0)
540 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
543 static void print_cparser_version(void)
545 printf("cparser (%s) using libFirm (%u.%u",
546 cparser_REVISION, ir_get_version_major(),
547 ir_get_version_minor());
549 const char *revision = ir_get_version_revision();
550 if (revision[0] != 0) {
552 fputs(revision, stdout);
555 const char *build = ir_get_version_build();
558 fputs(build, stdout);
561 puts("This is free software; see the source for copying conditions. There is NO\n"
562 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
565 static void print_help(const char *argv0)
569 puts("\t-fhelp Display help about firm optimisation options");
570 puts("\t-bhelp Display help about firm backend options");
571 puts("A big number of gcc flags is also supported");
574 static void set_be_option(const char *arg)
576 int res = be_parse_arg(arg);
581 static void set_option(const char *arg)
583 int res = firm_option(arg);
588 static void copy_file(FILE *dest, FILE *input)
592 while (!feof(input) && !ferror(dest)) {
593 size_t read = fread(buf, 1, sizeof(buf), input);
594 if (fwrite(buf, 1, read, dest) != read) {
595 perror("couldn't write output");
600 static inline bool streq(char const* a, char const* b)
602 return strcmp(a, b) == 0;
605 static inline bool strstart(char const* str, char const* start)
610 } while (*str++ == *start++);
614 static FILE *open_file(const char *filename)
616 if (streq(filename, "-")) {
620 FILE *in = fopen(filename, "r");
622 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
630 static filetype_t get_filetype_from_string(const char *string)
632 if (streq(string, "c") || streq(string, "c-header"))
634 if (streq(string, "c++") || streq(string, "c++-header"))
636 if (streq(string, "assembler"))
637 return FILETYPE_PREPROCESSED_ASSEMBLER;
638 if (streq(string, "assembler-with-cpp"))
639 return FILETYPE_ASSEMBLER;
640 if (streq(string, "none"))
641 return FILETYPE_AUTODETECT;
643 return FILETYPE_UNKNOWN;
646 static void init_os_support(void)
648 /* OS option must be set to the backend */
649 switch (firm_opt.os_support) {
650 case OS_SUPPORT_MINGW:
651 set_be_option("ia32-gasmode=mingw");
652 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
654 case OS_SUPPORT_LINUX:
655 set_be_option("ia32-gasmode=elf");
657 case OS_SUPPORT_MACHO:
658 set_be_option("ia32-gasmode=macho");
659 set_be_option("ia32-stackalign=4");
660 set_be_option("pic");
665 int main(int argc, char **argv)
669 const char *dumpfunction = NULL;
670 const char *print_file_name_file = NULL;
671 compile_mode_t mode = CompileAssembleLink;
673 int result = EXIT_SUCCESS;
674 char cpu_arch[16] = "ia32";
675 file_list_entry_t *files = NULL;
676 file_list_entry_t *last_file = NULL;
677 bool construct_dep_target = false;
678 bool do_timing = false;
679 struct obstack file_obst;
681 atexit(free_temp_files);
683 /* hack for now... */
684 if (strstr(argv[0], "pptest") != NULL) {
685 extern int pptest_main(int argc, char **argv);
686 return pptest_main(argc, argv);
689 obstack_init(&cppflags_obst);
690 obstack_init(&ldflags_obst);
691 obstack_init(&file_obst);
693 #define GET_ARG_AFTER(def, args) \
694 def = &arg[sizeof(args)-1]; \
695 if (def[0] == '\0') { \
698 fprintf(stderr, "error: expected argument after '" args "'\n"); \
699 argument_errors = true; \
703 if (def[0] == '-' && def[1] != '\0') { \
704 fprintf(stderr, "error: expected argument after '" args "'\n"); \
705 argument_errors = true; \
710 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
712 /* early options parsing (find out optimisation level and OS) */
713 for (int i = 1; i < argc; ++i) {
714 const char *arg = argv[i];
718 const char *option = &arg[1];
719 if (option[0] == 'O') {
720 sscanf(&option[1], "%d", &opt_level);
722 if (strcmp(arg, "-fwin32") == 0) {
723 firm_opt.os_support = OS_SUPPORT_MINGW;
724 } else if (strcmp(arg, "-fmac") == 0) {
725 firm_opt.os_support = OS_SUPPORT_MACHO;
726 } else if (strcmp(arg, "-flinux") == 0) {
727 firm_opt.os_support = OS_SUPPORT_LINUX;
731 /* set target/os specific stuff */
734 /* apply optimisation level */
737 set_option("no-opt");
740 set_option("no-inline");
744 /* use_builtins = true; */
747 set_option("thread-jumps");
748 set_option("if-conversion");
751 set_option("strict-aliasing");
752 set_option("inline");
753 set_option("deconv");
754 set_be_option("omitfp");
758 /* parse rest of options */
759 standard = STANDARD_DEFAULT;
760 unsigned features_on = 0;
761 unsigned features_off = 0;
762 filetype_t forced_filetype = FILETYPE_AUTODETECT;
763 bool help_displayed = false;
764 bool argument_errors = false;
765 for (int i = 1; i < argc; ++i) {
766 const char *arg = argv[i];
767 if (arg[0] == '-' && arg[1] != '\0') {
769 const char *option = &arg[1];
770 if (option[0] == 'o') {
771 GET_ARG_AFTER(outname, "-o");
772 } else if (option[0] == 'g') {
773 set_be_option("debuginfo=stabs");
774 set_be_option("omitfp=no");
775 set_be_option("ia32-nooptcc=yes");
776 } else if (SINGLE_OPTION('c')) {
777 mode = CompileAssemble;
778 } else if (SINGLE_OPTION('E')) {
779 mode = PreprocessOnly;
780 } else if (SINGLE_OPTION('S')) {
782 } else if (option[0] == 'O') {
784 } else if (option[0] == 'I') {
786 GET_ARG_AFTER(opt, "-I");
787 add_flag(&cppflags_obst, "-I%s", opt);
788 } else if (option[0] == 'D') {
790 GET_ARG_AFTER(opt, "-D");
791 add_flag(&cppflags_obst, "-D%s", opt);
792 } else if (option[0] == 'U') {
794 GET_ARG_AFTER(opt, "-U");
795 add_flag(&cppflags_obst, "-U%s", opt);
796 } else if (option[0] == 'l') {
798 GET_ARG_AFTER(opt, "-l");
799 add_flag(&ldflags_obst, "-l%s", opt);
800 } else if (option[0] == 'L') {
802 GET_ARG_AFTER(opt, "-L");
803 add_flag(&ldflags_obst, "-L%s", opt);
804 } else if (SINGLE_OPTION('v')) {
806 } else if (SINGLE_OPTION('w')) {
807 memset(&warning, 0, sizeof(warning));
808 } else if (option[0] == 'x') {
810 GET_ARG_AFTER(opt, "-x");
811 forced_filetype = get_filetype_from_string(opt);
812 if (forced_filetype == FILETYPE_UNKNOWN) {
813 fprintf(stderr, "Unknown language '%s'\n", opt);
814 argument_errors = true;
816 } else if (streq(option, "M")) {
817 mode = PreprocessOnly;
818 add_flag(&cppflags_obst, "-M");
819 } else if (streq(option, "MMD") ||
820 streq(option, "MD")) {
821 construct_dep_target = true;
822 add_flag(&cppflags_obst, "-%s", option);
823 } else if (streq(option, "MM") ||
824 streq(option, "MP")) {
825 add_flag(&cppflags_obst, "-%s", option);
826 } else if (streq(option, "MT") ||
827 streq(option, "MQ") ||
828 streq(option, "MF")) {
830 GET_ARG_AFTER(opt, "-MT");
831 add_flag(&cppflags_obst, "-%s", option);
832 add_flag(&cppflags_obst, "%s", opt);
833 } else if (streq(option, "include")) {
835 GET_ARG_AFTER(opt, "-include");
836 add_flag(&cppflags_obst, "-include");
837 add_flag(&cppflags_obst, "%s", opt);
838 } else if (streq(option, "isystem")) {
840 GET_ARG_AFTER(opt, "-isystem");
841 add_flag(&cppflags_obst, "-isystem");
842 add_flag(&cppflags_obst, "%s", opt);
843 } else if (streq(option, "nostdinc")
844 || streq(option, "trigraphs")) {
845 /* pass these through to the preprocessor */
846 add_flag(&cppflags_obst, "%s", arg);
847 } else if (streq(option, "pipe")) {
848 /* here for gcc compatibility */
849 } else if (option[0] == 'f') {
850 char const *orig_opt;
851 GET_ARG_AFTER(orig_opt, "-f");
853 if (strstart(orig_opt, "input-charset=")) {
854 char const* const encoding = strchr(orig_opt, '=') + 1;
855 select_input_encoding(encoding);
856 } else if (streq(orig_opt, "verbose-asm")) {
857 /* ignore: we always print verbose assembler */
859 char const *opt = orig_opt;
860 bool truth_value = true;
861 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
866 if (streq(opt, "builtins")) {
867 use_builtins = truth_value;
868 } else if (streq(opt, "dollars-in-identifiers")) {
869 allow_dollar_in_symbol = truth_value;
870 } else if (streq(opt, "omit-frame-pointer")) {
871 set_be_option(truth_value ? "omitfp" : "omitfp=no");
872 } else if (streq(opt, "short-wchar")) {
873 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
875 } else if (streq(opt, "signed-char")) {
876 char_is_signed = truth_value;
877 } else if (streq(opt, "strength-reduce")) {
878 firm_option(truth_value ? "strength-red" : "no-strength-red");
879 } else if (streq(opt, "syntax-only")) {
880 mode = truth_value ? ParseOnly : CompileAssembleLink;
881 } else if (streq(opt, "unsigned-char")) {
882 char_is_signed = !truth_value;
883 } else if (truth_value == false &&
884 streq(opt, "asynchronous-unwind-tables")) {
885 /* nothing todo, a gcc feature which we don't support
886 * anyway was deactivated */
887 } else if (strstart(orig_opt, "align-loops=") ||
888 strstart(orig_opt, "align-jumps=") ||
889 strstart(orig_opt, "align-functions=")) {
890 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
891 } else if (streq(opt, "fast-math") ||
892 streq(opt, "jump-tables") ||
893 streq(opt, "expensive-optimizations") ||
894 streq(opt, "common") ||
895 streq(opt, "optimize-sibling-calls") ||
896 streq(opt, "align-loops") ||
897 streq(opt, "align-jumps") ||
898 streq(opt, "align-functions") ||
900 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
902 int res = firm_option(orig_opt);
904 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
906 argument_errors = true;
908 } else if (res == -1) {
909 help_displayed = true;
913 } else if (option[0] == 'b') {
915 GET_ARG_AFTER(opt, "-b");
916 int res = be_parse_arg(opt);
918 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
920 argument_errors = true;
921 } else if (res == -1) {
922 help_displayed = true;
923 } else if (strstart(opt, "isa=")) {
924 strncpy(cpu_arch, opt, sizeof(cpu_arch));
926 } else if (option[0] == 'W') {
927 if (option[1] == '\0') {
928 /* ignore -W, out defaults are already quiet verbose */
929 } else if (strstart(option + 1, "p,")) {
930 // pass options directly to the preprocessor
932 GET_ARG_AFTER(opt, "-Wp,");
933 add_flag(&cppflags_obst, "-Wp,%s", opt);
934 } else if (strstart(option + 1, "l,")) {
935 // pass options directly to the linker
937 GET_ARG_AFTER(opt, "-Wl,");
938 add_flag(&ldflags_obst, "-Wl,%s", opt);
939 } else if (streq(option + 1, "no-trigraphs")
940 || streq(option + 1, "undef")) {
941 add_flag(&cppflags_obst, "%s", arg);
943 set_warning_opt(&option[1]);
945 } else if (option[0] == 'm') {
950 GET_ARG_AFTER(opt, "-m");
951 if (strstart(opt, "arch=")) {
952 GET_ARG_AFTER(opt, "-march=");
953 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
954 int res = be_parse_arg(arch_opt);
956 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
957 argument_errors = true;
959 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
960 int res = be_parse_arg(arch_opt);
962 argument_errors = true;
964 } else if (strstart(opt, "tune=")) {
965 GET_ARG_AFTER(opt, "-mtune=");
966 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
967 int res = be_parse_arg(arch_opt);
969 argument_errors = true;
970 } else if (strstart(opt, "cpu=")) {
971 GET_ARG_AFTER(opt, "-mcpu=");
972 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
973 int res = be_parse_arg(arch_opt);
975 argument_errors = true;
976 } else if (strstart(opt, "fpmath=")) {
977 GET_ARG_AFTER(opt, "-mfpmath=");
978 if (streq(opt, "387"))
980 else if (streq(opt, "sse"))
983 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
984 argument_errors = true;
986 if (!argument_errors) {
987 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
988 int res = be_parse_arg(arch_opt);
990 argument_errors = true;
992 } else if (strstart(opt, "preferred-stack-boundary=")) {
993 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
994 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
995 int res = be_parse_arg(arch_opt);
997 argument_errors = true;
998 } else if (streq(opt, "omit-leaf-frame-pointer")) {
999 set_be_option("omitleaffp=1");
1000 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1001 set_be_option("omitleaffp=0");
1002 } else if (streq(opt, "rtd")) {
1003 default_calling_convention = CC_STDCALL;
1004 } else if (strstart(opt, "regparm=")) {
1005 fprintf(stderr, "error: regparm convention not supported yet\n");
1006 argument_errors = true;
1007 } else if (streq(opt, "soft-float")) {
1008 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1009 argument_errors = true;
1012 long int value = strtol(opt, &endptr, 10);
1013 if (*endptr != '\0') {
1014 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1015 argument_errors = true;
1017 if (value != 16 && value != 32 && value != 64) {
1018 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1019 argument_errors = true;
1021 machine_size = (unsigned int)value;
1024 } else if (streq(option, "pg")) {
1025 set_be_option("gprof");
1026 add_flag(&ldflags_obst, "-pg");
1027 } else if (streq(option, "pedantic") ||
1028 streq(option, "ansi")) {
1029 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1030 } else if (streq(option, "shared")) {
1031 add_flag(&ldflags_obst, "-shared");
1032 } else if (strstart(option, "std=")) {
1033 const char *const o = &option[4];
1035 streq(o, "c++") ? STANDARD_CXX98 :
1036 streq(o, "c++98") ? STANDARD_CXX98 :
1037 streq(o, "c89") ? STANDARD_C89 :
1038 streq(o, "c99") ? STANDARD_C99 :
1039 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1040 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1041 streq(o, "gnu89") ? STANDARD_GNU89 :
1042 streq(o, "gnu99") ? STANDARD_GNU99 :
1043 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1044 streq(o, "iso9899:1990") ? STANDARD_C89 :
1045 streq(o, "iso9899:199409") ? STANDARD_C90 :
1046 streq(o, "iso9899:1999") ? STANDARD_C99 :
1047 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1048 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1049 } else if (streq(option, "version")) {
1050 print_cparser_version();
1051 } else if (strstart(option, "print-file-name=")) {
1052 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1053 } else if (option[0] == '-') {
1054 /* double dash option */
1056 if (streq(option, "gcc")) {
1057 features_on |= _GNUC;
1058 features_off &= ~_GNUC;
1059 } else if (streq(option, "no-gcc")) {
1060 features_on &= ~_GNUC;
1061 features_off |= _GNUC;
1062 } else if (streq(option, "ms")) {
1064 features_off &= ~_MS;
1065 } else if (streq(option, "no-ms")) {
1066 features_on &= ~_MS;
1067 features_off |= _MS;
1068 } else if (streq(option, "strict")) {
1070 } else if (streq(option, "lextest")) {
1072 } else if (streq(option, "benchmark")) {
1073 mode = BenchmarkParser;
1074 } else if (streq(option, "print-ast")) {
1076 } else if (streq(option, "print-implicit-cast")) {
1077 print_implicit_casts = true;
1078 } else if (streq(option, "print-parenthesis")) {
1079 print_parenthesis = true;
1080 } else if (streq(option, "print-fluffy")) {
1082 } else if (streq(option, "print-caml")) {
1084 } else if (streq(option, "print-jna")) {
1086 } else if (streq(option, "time")) {
1088 } else if (streq(option, "version")) {
1089 print_cparser_version();
1091 } else if (streq(option, "help")) {
1092 print_help(argv[0]);
1093 help_displayed = true;
1094 } else if (streq(option, "dump-function")) {
1097 fprintf(stderr, "error: "
1098 "expected argument after '--dump-function'\n");
1099 argument_errors = true;
1102 dumpfunction = argv[i];
1104 } else if (streq(option, "export-ir")) {
1105 mode = CompileExportIR;
1107 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1108 argument_errors = true;
1111 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1112 argument_errors = true;
1115 filetype_t type = forced_filetype;
1116 if (type == FILETYPE_AUTODETECT) {
1117 if (streq(arg, "-")) {
1118 /* - implicitly means C source file */
1121 const char *suffix = strrchr(arg, '.');
1122 /* Ensure there is at least one char before the suffix */
1123 if (suffix != NULL && suffix != arg) {
1126 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1127 streq(suffix, "a") ? FILETYPE_OBJECT :
1128 streq(suffix, "c") ? FILETYPE_C :
1129 streq(suffix, "cc") ? FILETYPE_CXX :
1130 streq(suffix, "cpp") ? FILETYPE_CXX :
1131 streq(suffix, "cxx") ? FILETYPE_CXX :
1132 streq(suffix, "h") ? FILETYPE_C :
1133 streq(suffix, "ir") ? FILETYPE_IR :
1134 streq(suffix, "o") ? FILETYPE_OBJECT :
1135 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1136 streq(suffix, "so") ? FILETYPE_OBJECT :
1137 FILETYPE_AUTODETECT;
1141 if (type == FILETYPE_AUTODETECT) {
1142 fprintf(stderr, "'%s': file format not recognized\n", arg);
1147 file_list_entry_t *entry
1148 = obstack_alloc(&file_obst, sizeof(entry[0]));
1149 memset(entry, 0, sizeof(entry[0]));
1153 if (last_file != NULL) {
1154 last_file->next = entry;
1162 if (help_displayed) {
1163 return !argument_errors;
1166 if (print_file_name_file != NULL) {
1167 print_file_name(print_file_name_file);
1170 if (files == NULL) {
1171 fprintf(stderr, "error: no input files specified\n");
1172 argument_errors = true;
1175 if (argument_errors) {
1180 /* we do the lowering in ast2firm */
1181 firm_opt.lower_bitfields = FALSE;
1183 /* set the c_mode here, types depends on it */
1184 c_mode |= features_on;
1185 c_mode &= ~features_off;
1188 init_symbol_table();
1201 if (construct_dep_target) {
1202 if (outname != 0 && strlen(outname) >= 2) {
1203 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1205 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1208 dep_target[0] = '\0';
1211 char outnamebuf[4096];
1212 if (outname == NULL) {
1213 const char *filename = files->name;
1216 case BenchmarkParser:
1222 case PreprocessOnly:
1227 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1228 outname = outnamebuf;
1230 case CompileAssemble:
1231 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1232 outname = outnamebuf;
1235 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1237 outname = outnamebuf;
1239 case CompileExportIR:
1240 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1241 outname = outnamebuf;
1243 case CompileAssembleLink:
1253 assert(outname != NULL);
1256 if (streq(outname, "-")) {
1259 out = fopen(outname, "w");
1261 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1267 file_list_entry_t *file;
1268 bool already_constructed_firm = false;
1269 for (file = files; file != NULL; file = file->next) {
1270 char asm_tempfile[1024];
1271 const char *filename = file->name;
1272 filetype_t filetype = file->type;
1274 if (filetype == FILETYPE_OBJECT)
1278 if (mode == LexTest) {
1280 in = open_file(filename);
1281 lextest(in, filename);
1286 FILE *preprocessed_in = NULL;
1287 filetype_t next_filetype = filetype;
1290 next_filetype = FILETYPE_PREPROCESSED_C;
1293 next_filetype = FILETYPE_PREPROCESSED_CXX;
1295 case FILETYPE_ASSEMBLER:
1296 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1299 /* no support for input on FILE* yet */
1301 panic("internal compiler error: in for preprocessor != NULL");
1303 preprocessed_in = preprocess(filename, filetype);
1304 if (mode == PreprocessOnly) {
1305 copy_file(out, preprocessed_in);
1306 int result = pclose(preprocessed_in);
1308 /* remove output file in case of error */
1309 if (out != stdout && result != EXIT_SUCCESS) {
1315 in = preprocessed_in;
1316 filetype = next_filetype;
1324 if (mode == Compile) {
1327 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1331 in = open_file(filename);
1333 /* preprocess and compile */
1334 if (filetype == FILETYPE_PREPROCESSED_C) {
1335 char const* invalid_mode;
1338 case STANDARD_C89: c_mode = _C89; break;
1339 /* TODO determine difference between these two */
1340 case STANDARD_C90: c_mode = _C89; break;
1341 case STANDARD_C99: c_mode = _C89 | _C99; break;
1342 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1346 "warning: command line option \"-std=%s\" is not valid for C\n",
1349 case STANDARD_DEFAULT:
1350 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1352 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1353 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1356 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1357 char const* invalid_mode;
1359 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1360 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1361 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1362 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1363 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1366 case STANDARD_CXX98: c_mode = _CXX; break;
1370 "warning: command line option \"-std=%s\" is not valid for C++\n",
1372 case STANDARD_DEFAULT:
1373 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1377 c_mode |= features_on;
1378 c_mode &= ~features_off;
1380 /* do the actual parsing */
1381 ir_timer_t *t_parsing = ir_timer_new();
1382 timer_register(t_parsing, "Frontend: Parsing");
1383 timer_push(t_parsing);
1385 translation_unit_t *const unit = do_parsing(in, filename);
1386 timer_pop(t_parsing);
1388 /* prints the AST even if errors occurred */
1389 if (mode == PrintAst) {
1394 if (error_count > 0) {
1395 /* parsing failed because of errors */
1396 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1398 result = EXIT_FAILURE;
1400 } else if (warning_count > 0) {
1401 fprintf(stderr, "%u warning(s)\n", warning_count);
1404 if (in == preprocessed_in) {
1405 int pp_result = pclose(preprocessed_in);
1406 if (pp_result != EXIT_SUCCESS) {
1407 /* remove output file */
1414 if (mode == BenchmarkParser) {
1416 } else if (mode == PrintFluffy) {
1417 write_fluffy_decls(out, unit);
1419 } else if (mode == PrintCaml) {
1420 write_caml_decls(out, unit);
1422 } else if (mode == PrintJna) {
1423 write_jna_decls(out, unit);
1427 /* build the firm graph */
1428 ir_timer_t *t_construct = ir_timer_new();
1429 timer_register(t_construct, "Frontend: Graph construction");
1430 timer_push(t_construct);
1431 if (already_constructed_firm) {
1432 panic("compiling multiple files/translation units not possible");
1434 translation_unit_to_firm(unit);
1435 already_constructed_firm = true;
1436 timer_pop(t_construct);
1439 if (mode == ParseOnly) {
1443 if (mode == CompileDump) {
1445 ident *id = new_id_from_str(dumpfunction);
1446 ir_graph *irg = NULL;
1447 int n_irgs = get_irp_n_irgs();
1448 for (int i = 0; i < n_irgs; ++i) {
1449 ir_graph *tirg = get_irp_irg(i);
1450 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1458 fprintf(stderr, "No graph for function '%s' found\n",
1463 dump_ir_graph_file(out, irg);
1468 if (mode == CompileExportIR) {
1474 gen_firm_finish(asm_out, filename, have_const_functions);
1475 if (asm_out != out) {
1478 } else if (filetype == FILETYPE_IR) {
1480 ir_import(filename);
1482 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1483 copy_file(asm_out, in);
1484 if (in == preprocessed_in) {
1485 int pp_result = pclose(preprocessed_in);
1486 if (pp_result != EXIT_SUCCESS) {
1487 /* remove output in error case */
1493 if (asm_out != out) {
1498 if (mode == Compile)
1501 /* if we're here then we have preprocessed assembly */
1502 filename = asm_tempfile;
1503 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1506 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1508 const char *filename_o;
1509 if (mode == CompileAssemble) {
1511 filename_o = outname;
1513 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1518 assemble(filename_o, filename);
1520 size_t len = strlen(filename_o) + 1;
1521 filename = obstack_copy(&file_obst, filename_o, len);
1522 filetype = FILETYPE_OBJECT;
1525 /* ok we're done here, process next file */
1526 file->name = filename;
1527 file->type = filetype;
1530 if (result != EXIT_SUCCESS) {
1536 /* link program file */
1537 if (mode == CompileAssembleLink) {
1538 obstack_1grow(&ldflags_obst, '\0');
1539 const char *flags = obstack_finish(&ldflags_obst);
1541 /* construct commandline */
1542 const char *linker = getenv("CPARSER_LINK");
1545 obstack_printf(&file_obst, "%s", linker);
1546 for (file_list_entry_t *entry = files; entry != NULL;
1547 entry = entry->next) {
1548 if (entry->type != FILETYPE_OBJECT)
1551 add_flag(&file_obst, "%s", entry->name);
1554 add_flag(&file_obst, "-o");
1555 add_flag(&file_obst, outname);
1556 obstack_printf(&file_obst, "%s", flags);
1557 obstack_1grow(&file_obst, '\0');
1559 char *commandline = obstack_finish(&file_obst);
1564 int err = system(commandline);
1565 if (err != EXIT_SUCCESS) {
1566 fprintf(stderr, "linker reported an error\n");
1574 obstack_free(&cppflags_obst, NULL);
1575 obstack_free(&ldflags_obst, NULL);
1576 obstack_free(&file_obst, NULL);
1586 exit_symbol_table();