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 "driver/firm_machine.h"
76 #include "adt/error.h"
77 #include "adt/strutil.h"
78 #include "wrappergen/write_fluffy.h"
79 #include "wrappergen/write_jna.h"
87 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
89 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
98 #define ASSEMBLER "gcc -c -xassembler"
101 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
102 unsigned int machine_size = 32;
103 bool byte_order_big_endian = false;
104 bool char_is_signed = true;
105 bool strict_mode = false;
106 bool use_builtins = false;
107 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
108 unsigned force_long_double_size = 0;
109 bool enable_main_collect2_hack = false;
110 bool freestanding = false;
112 /* to switch on printing of implicit casts */
113 extern bool print_implicit_casts;
115 /* to switch on printing of parenthesis to indicate operator precedence */
116 extern bool print_parenthesis;
118 static machine_triple_t *target_machine;
119 static const char *target_triple;
121 static struct obstack cppflags_obst;
122 static struct obstack ldflags_obst;
123 static struct obstack asflags_obst;
124 static char dep_target[1024];
125 static const char *outname;
126 static bool define_intmax_types;
128 typedef enum lang_standard_t {
129 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
130 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
131 STANDARD_C89, /* ISO C90 (sic) */
132 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
133 STANDARD_C99, /* ISO C99 */
134 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
135 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
136 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
137 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
140 static lang_standard_t standard;
142 typedef struct file_list_entry_t file_list_entry_t;
144 typedef enum filetype_t {
147 FILETYPE_PREPROCESSED_C,
149 FILETYPE_PREPROCESSED_CXX,
151 FILETYPE_PREPROCESSED_ASSEMBLER,
157 struct file_list_entry_t {
158 const char *name; /**< filename or NULL for stdin */
160 file_list_entry_t *next;
163 static file_list_entry_t *temp_files;
165 static void get_output_name(char *buf, size_t buflen, const char *inputname,
168 if (inputname == NULL)
171 char const *const last_slash = strrchr(inputname, '/');
172 char const *const filename =
173 last_slash != NULL ? last_slash + 1 : inputname;
174 char const *const last_dot = strrchr(filename, '.');
175 char const *const name_end =
176 last_dot != NULL ? last_dot : strchr(filename, '\0');
178 int const len = snprintf(buf, buflen, "%.*s%s",
179 (int)(name_end - filename), filename, newext);
181 if (len < 0 || buflen <= (size_t)len)
183 if (buflen <= (size_t)len)
185 panic("filename too long");
188 #include "gen_builtins.h"
190 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
195 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
199 lexer_open_stream(in, input_name);
202 translation_unit_t *unit = finish_parsing();
206 static void lextest(FILE *in, const char *fname)
208 lexer_open_stream(in, fname);
211 lexer_next_preprocessing_token();
212 print_token(stdout, &lexer_token);
214 } while (lexer_token.type != T_EOF);
217 static void add_flag(struct obstack *obst, const char *format, ...)
222 va_start(ap, format);
226 vsnprintf(buf, sizeof(buf), format, ap);
229 obstack_1grow(obst, ' ');
231 obstack_1grow(obst, '"');
232 obstack_grow(obst, buf, len);
233 obstack_1grow(obst, '"');
235 /* escape stuff... */
236 for (char *c = buf; *c != '\0'; ++c) {
254 obstack_1grow(obst, '\\');
257 obstack_1grow(obst, *c);
264 static const char *type_to_string(type_t *type)
266 assert(type->kind == TYPE_ATOMIC);
267 return get_atomic_kind_name(type->atomic.akind);
270 static FILE *preprocess(const char *fname, filetype_t filetype)
272 static const char *common_flags = NULL;
274 if (common_flags == NULL) {
275 obstack_1grow(&cppflags_obst, '\0');
276 const char *flags = obstack_finish(&cppflags_obst);
278 /* setup default defines */
279 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
280 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
281 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
282 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
284 add_flag(&cppflags_obst, "-U__VERSION__");
285 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
287 if (define_intmax_types) {
288 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
289 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
290 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
291 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
294 if (flags[0] != '\0') {
295 size_t len = strlen(flags);
296 obstack_1grow(&cppflags_obst, ' ');
297 obstack_grow(&cppflags_obst, flags, len);
299 obstack_1grow(&cppflags_obst, '\0');
300 common_flags = obstack_finish(&cppflags_obst);
303 assert(obstack_object_size(&cppflags_obst) == 0);
305 const char *preprocessor = getenv("CPARSER_PP");
306 if (preprocessor != NULL) {
307 obstack_printf(&cppflags_obst, "%s ", preprocessor);
309 if (target_triple != NULL)
310 obstack_printf(&cppflags_obst, "%s-", target_triple);
311 obstack_printf(&cppflags_obst, PREPROCESSOR);
316 add_flag(&cppflags_obst, "-std=c99");
319 add_flag(&cppflags_obst, "-std=c++98");
321 case FILETYPE_ASSEMBLER:
322 add_flag(&cppflags_obst, "-x");
323 add_flag(&cppflags_obst, "assembler-with-cpp");
328 obstack_printf(&cppflags_obst, "%s", common_flags);
330 /* handle dependency generation */
331 if (dep_target[0] != '\0') {
332 add_flag(&cppflags_obst, "-MF");
333 add_flag(&cppflags_obst, dep_target);
334 if (outname != NULL) {
335 add_flag(&cppflags_obst, "-MQ");
336 add_flag(&cppflags_obst, outname);
339 add_flag(&cppflags_obst, fname);
340 obstack_1grow(&cppflags_obst, '\0');
342 char *commandline = obstack_finish(&cppflags_obst);
346 FILE *f = popen(commandline, "r");
348 fprintf(stderr, "invoking preprocessor failed\n");
351 /* we don't really need that anymore */
352 obstack_free(&cppflags_obst, commandline);
357 static void assemble(const char *out, const char *in)
359 obstack_1grow(&asflags_obst, '\0');
360 const char *flags = obstack_finish(&asflags_obst);
362 const char *assembler = getenv("CPARSER_AS");
363 if (assembler != NULL) {
364 obstack_printf(&asflags_obst, "%s", assembler);
366 if (target_triple != NULL)
367 obstack_printf(&asflags_obst, "%s-", target_triple);
368 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
370 if (flags[0] != '\0')
371 obstack_printf(&asflags_obst, " %s", flags);
373 obstack_printf(&asflags_obst, " %s -o %s", in, out);
374 obstack_1grow(&asflags_obst, '\0');
376 char *commandline = obstack_finish(&asflags_obst);
380 int err = system(commandline);
381 if (err != EXIT_SUCCESS) {
382 fprintf(stderr, "assembler reported an error\n");
385 obstack_free(&asflags_obst, commandline);
388 static void print_file_name(const char *file)
390 add_flag(&ldflags_obst, "-print-file-name=%s", file);
392 obstack_1grow(&ldflags_obst, '\0');
393 const char *flags = obstack_finish(&ldflags_obst);
395 /* construct commandline */
396 const char *linker = getenv("CPARSER_LINK");
397 if (linker != NULL) {
398 obstack_printf(&ldflags_obst, "%s ", linker);
400 if (target_triple != NULL)
401 obstack_printf(&ldflags_obst, "%s-", target_triple);
402 obstack_printf(&ldflags_obst, "%s ", LINKER);
404 obstack_printf(&ldflags_obst, "%s ", linker);
405 obstack_printf(&ldflags_obst, "%s", flags);
406 obstack_1grow(&ldflags_obst, '\0');
408 char *commandline = obstack_finish(&ldflags_obst);
412 int err = system(commandline);
413 if (err != EXIT_SUCCESS) {
414 fprintf(stderr, "linker reported an error\n");
417 obstack_free(&ldflags_obst, commandline);
420 static const char *try_dir(const char *dir)
424 if (access(dir, R_OK | W_OK | X_OK) == 0)
429 static const char *get_tempdir(void)
431 static const char *tmpdir = NULL;
437 tmpdir = try_dir(getenv("TMPDIR"));
439 tmpdir = try_dir(getenv("TMP"));
441 tmpdir = try_dir(getenv("TEMP"));
445 tmpdir = try_dir(P_tmpdir);
449 tmpdir = try_dir("/var/tmp");
451 tmpdir = try_dir("/usr/tmp");
453 tmpdir = try_dir("/tmp");
462 /* cheap and nasty mkstemp replacement */
463 static int mkstemp(char *templ)
466 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
471 * an own version of tmpnam, which: writes in a buffer, emits no warnings
472 * during linking (like glibc/gnu ld do for tmpnam)...
474 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
476 const char *tempdir = get_tempdir();
478 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
480 int fd = mkstemp(buffer);
482 fprintf(stderr, "couldn't create temporary file: %s\n",
486 FILE *out = fdopen(fd, "w");
488 fprintf(stderr, "couldn't create temporary file FILE*\n");
492 file_list_entry_t *entry = xmalloc(sizeof(*entry));
493 memset(entry, 0, sizeof(*entry));
495 size_t name_len = strlen(buffer) + 1;
496 char *name = malloc(name_len);
497 memcpy(name, buffer, name_len);
500 entry->next = temp_files;
506 static void free_temp_files(void)
508 file_list_entry_t *entry = temp_files;
509 file_list_entry_t *next;
510 for ( ; entry != NULL; entry = next) {
514 free((char*) entry->name);
520 typedef enum compile_mode_t {
535 static void usage(const char *argv0)
537 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
540 static void print_cparser_version(void)
542 printf("cparser (%s) using libFirm (%u.%u",
543 cparser_REVISION, ir_get_version_major(),
544 ir_get_version_minor());
546 const char *revision = ir_get_version_revision();
547 if (revision[0] != 0) {
549 fputs(revision, stdout);
552 const char *build = ir_get_version_build();
555 fputs(build, stdout);
558 puts("This is free software; see the source for copying conditions. There is NO\n"
559 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
562 static void print_help(const char *argv0)
566 puts("\t-fhelp Display help about firm optimisation options");
567 puts("\t-bhelp Display help about firm backend options");
568 puts("A big number of gcc flags is also supported");
571 static void set_be_option(const char *arg)
573 int res = be_parse_arg(arg);
578 static void copy_file(FILE *dest, FILE *input)
582 while (!feof(input) && !ferror(dest)) {
583 size_t read = fread(buf, 1, sizeof(buf), input);
584 if (fwrite(buf, 1, read, dest) != read) {
585 perror("couldn't write output");
590 static FILE *open_file(const char *filename)
592 if (streq(filename, "-")) {
596 FILE *in = fopen(filename, "r");
598 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
606 static filetype_t get_filetype_from_string(const char *string)
608 if (streq(string, "c") || streq(string, "c-header"))
610 if (streq(string, "c++") || streq(string, "c++-header"))
612 if (streq(string, "assembler"))
613 return FILETYPE_PREPROCESSED_ASSEMBLER;
614 if (streq(string, "assembler-with-cpp"))
615 return FILETYPE_ASSEMBLER;
616 if (streq(string, "none"))
617 return FILETYPE_AUTODETECT;
619 return FILETYPE_UNKNOWN;
622 static bool init_os_support(void)
624 const char *os = target_machine->operating_system;
625 wchar_atomic_kind = ATOMIC_TYPE_INT;
626 force_long_double_size = 0;
627 enable_main_collect2_hack = false;
628 define_intmax_types = false;
630 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
631 || streq(os, "solaris")) {
632 set_create_ld_ident(create_name_linux_elf);
633 } else if (streq(os, "darwin")) {
634 force_long_double_size = 16;
635 set_create_ld_ident(create_name_macho);
636 define_intmax_types = true;
637 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
638 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
639 enable_main_collect2_hack = true;
640 set_create_ld_ident(create_name_win32);
648 static bool parse_target_triple(const char *arg)
650 machine_triple_t *triple = firm_parse_machine_triple(arg);
651 if (triple == NULL) {
652 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
655 target_machine = triple;
659 static void setup_target_machine(void)
661 if (!setup_firm_for_machine(target_machine))
666 int main(int argc, char **argv)
670 const char *dumpfunction = NULL;
671 const char *print_file_name_file = NULL;
672 compile_mode_t mode = CompileAssembleLink;
674 int result = EXIT_SUCCESS;
675 char cpu_arch[16] = "ia32";
676 file_list_entry_t *files = NULL;
677 file_list_entry_t *last_file = NULL;
678 bool construct_dep_target = false;
679 bool do_timing = false;
680 bool profile_generate = false;
681 bool profile_use = false;
682 struct obstack file_obst;
684 atexit(free_temp_files);
686 /* hack for now... */
687 if (strstr(argv[0], "pptest") != NULL) {
688 extern int pptest_main(int argc, char **argv);
689 return pptest_main(argc, argv);
692 obstack_init(&cppflags_obst);
693 obstack_init(&ldflags_obst);
694 obstack_init(&asflags_obst);
695 obstack_init(&file_obst);
697 #define GET_ARG_AFTER(def, args) \
698 def = &arg[sizeof(args)-1]; \
699 if (def[0] == '\0') { \
702 fprintf(stderr, "error: expected argument after '" args "'\n"); \
703 argument_errors = true; \
707 if (def[0] == '-' && def[1] != '\0') { \
708 fprintf(stderr, "error: expected argument after '" args "'\n"); \
709 argument_errors = true; \
714 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
716 /* early options parsing (find out optimisation level and OS) */
717 for (int i = 1; i < argc; ++i) {
718 const char *arg = argv[i];
722 const char *option = &arg[1];
723 if (option[0] == 'O') {
724 sscanf(&option[1], "%d", &opt_level);
728 const char *target = getenv("TARGET");
730 parse_target_triple(target);
731 if (target_machine == NULL) {
732 target_machine = firm_get_host_machine();
734 choose_optimization_pack(opt_level);
735 setup_target_machine();
737 /* parse rest of options */
738 standard = STANDARD_DEFAULT;
739 unsigned features_on = 0;
740 unsigned features_off = 0;
741 filetype_t forced_filetype = FILETYPE_AUTODETECT;
742 bool help_displayed = false;
743 bool argument_errors = false;
744 for (int i = 1; i < argc; ++i) {
745 const char *arg = argv[i];
746 if (arg[0] == '-' && arg[1] != '\0') {
748 const char *option = &arg[1];
749 if (option[0] == 'o') {
750 GET_ARG_AFTER(outname, "-o");
751 } else if (option[0] == 'g') {
752 set_be_option("debuginfo=stabs");
753 set_be_option("omitfp=no");
754 set_be_option("ia32-nooptcc=yes");
755 } else if (SINGLE_OPTION('c')) {
756 mode = CompileAssemble;
757 } else if (SINGLE_OPTION('E')) {
758 mode = PreprocessOnly;
759 } else if (SINGLE_OPTION('S')) {
761 } else if (option[0] == 'O') {
763 } else if (option[0] == 'I') {
765 GET_ARG_AFTER(opt, "-I");
766 add_flag(&cppflags_obst, "-I%s", opt);
767 } else if (option[0] == 'D') {
769 GET_ARG_AFTER(opt, "-D");
770 add_flag(&cppflags_obst, "-D%s", opt);
771 } else if (option[0] == 'U') {
773 GET_ARG_AFTER(opt, "-U");
774 add_flag(&cppflags_obst, "-U%s", opt);
775 } else if (option[0] == 'l') {
777 GET_ARG_AFTER(opt, "-l");
778 add_flag(&ldflags_obst, "-l%s", opt);
779 } else if (option[0] == 'L') {
781 GET_ARG_AFTER(opt, "-L");
782 add_flag(&ldflags_obst, "-L%s", opt);
783 } else if (SINGLE_OPTION('v')) {
785 } else if (SINGLE_OPTION('w')) {
786 memset(&warning, 0, sizeof(warning));
787 } else if (option[0] == 'x') {
789 GET_ARG_AFTER(opt, "-x");
790 forced_filetype = get_filetype_from_string(opt);
791 if (forced_filetype == FILETYPE_UNKNOWN) {
792 fprintf(stderr, "Unknown language '%s'\n", opt);
793 argument_errors = true;
795 } else if (streq(option, "M")) {
796 mode = PreprocessOnly;
797 add_flag(&cppflags_obst, "-M");
798 } else if (streq(option, "MMD") ||
799 streq(option, "MD")) {
800 construct_dep_target = true;
801 add_flag(&cppflags_obst, "-%s", option);
802 } else if (streq(option, "MM") ||
803 streq(option, "MP")) {
804 add_flag(&cppflags_obst, "-%s", option);
805 } else if (streq(option, "MT") ||
806 streq(option, "MQ") ||
807 streq(option, "MF")) {
809 GET_ARG_AFTER(opt, "-MT");
810 add_flag(&cppflags_obst, "-%s", option);
811 add_flag(&cppflags_obst, "%s", opt);
812 } else if (streq(option, "include")) {
814 GET_ARG_AFTER(opt, "-include");
815 add_flag(&cppflags_obst, "-include");
816 add_flag(&cppflags_obst, "%s", opt);
817 } else if (streq(option, "isystem")) {
819 GET_ARG_AFTER(opt, "-isystem");
820 add_flag(&cppflags_obst, "-isystem");
821 add_flag(&cppflags_obst, "%s", opt);
822 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
823 } else if (streq(option, "pthread")) {
824 /* set flags for the preprocessor */
825 add_flag(&cppflags_obst, "-D_REENTRANT");
826 /* set flags for the linker */
827 add_flag(&ldflags_obst, "-lpthread");
829 } else if (streq(option, "nostdinc")
830 || streq(option, "trigraphs")) {
831 /* pass these through to the preprocessor */
832 add_flag(&cppflags_obst, "%s", arg);
833 } else if (streq(option, "pipe")) {
834 /* here for gcc compatibility */
835 } else if (streq(option, "static")) {
836 add_flag(&ldflags_obst, "-static");
837 } else if (streq(option, "shared")) {
838 add_flag(&ldflags_obst, "-shared");
839 } else if (option[0] == 'f') {
840 char const *orig_opt;
841 GET_ARG_AFTER(orig_opt, "-f");
843 if (strstart(orig_opt, "input-charset=")) {
844 char const* const encoding = strchr(orig_opt, '=') + 1;
845 select_input_encoding(encoding);
846 } else if (strstart(orig_opt, "align-loops=") ||
847 strstart(orig_opt, "align-jumps=") ||
848 strstart(orig_opt, "align-functions=")) {
849 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
850 } else if (strstart(orig_opt, "message-length=")) {
851 /* ignore: would only affect error message format */
853 /* -f options which have an -fno- variant */
854 char const *opt = orig_opt;
855 bool truth_value = true;
856 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
861 if (streq(opt, "builtins")) {
862 use_builtins = truth_value;
863 } else if (streq(opt, "dollars-in-identifiers")) {
864 allow_dollar_in_symbol = truth_value;
865 } else if (streq(opt, "omit-frame-pointer")) {
866 set_be_option(truth_value ? "omitfp" : "omitfp=no");
867 } else if (streq(opt, "short-wchar")) {
868 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
870 } else if (streq(opt, "signed-char")) {
871 char_is_signed = truth_value;
872 } else if (streq(opt, "strength-reduce")) {
873 firm_option(truth_value ? "strength-red" : "no-strength-red");
874 } else if (streq(opt, "syntax-only")) {
875 mode = truth_value ? ParseOnly : CompileAssembleLink;
876 } else if (streq(opt, "unsigned-char")) {
877 char_is_signed = !truth_value;
878 } else if (streq(opt, "freestanding")) {
879 freestanding = truth_value;
880 } else if (streq(opt, "hosted")) {
881 freestanding = !truth_value;
882 } else if (streq(opt, "profile-generate")) {
883 profile_generate = truth_value;
884 } else if (streq(opt, "profile-use")) {
885 profile_use = truth_value;
886 } else if (truth_value == false &&
887 streq(opt, "asynchronous-unwind-tables")) {
888 /* nothing todo, a gcc feature which we don't support
889 * anyway was deactivated */
890 } else if (streq(orig_opt, "verbose-asm")) {
891 /* ignore: we always print verbose assembler */
892 } else if (streq(opt, "fast-math") ||
893 streq(opt, "jump-tables") ||
894 streq(opt, "expensive-optimizations") ||
895 streq(opt, "common") ||
896 streq(opt, "optimize-sibling-calls") ||
897 streq(opt, "align-loops") ||
898 streq(opt, "align-jumps") ||
899 streq(opt, "align-functions") ||
901 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
903 int res = firm_option(orig_opt);
905 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
907 argument_errors = true;
909 } else if (res == -1) {
910 help_displayed = true;
914 } else if (option[0] == 'b') {
916 GET_ARG_AFTER(opt, "-b");
917 int res = be_parse_arg(opt);
919 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
921 argument_errors = true;
922 } else if (res == -1) {
923 help_displayed = true;
924 } else if (strstart(opt, "isa=")) {
925 strncpy(cpu_arch, opt, sizeof(cpu_arch));
927 } else if (option[0] == 'W') {
928 if (option[1] == '\0') {
929 /* ignore -W, our defaults are already quite verbose */
930 } else if (strstart(option + 1, "p,")) {
931 // pass options directly to the preprocessor
933 GET_ARG_AFTER(opt, "-Wp,");
934 add_flag(&cppflags_obst, "-Wp,%s", opt);
935 } else if (strstart(option + 1, "l,")) {
936 // pass options directly to the linker
938 GET_ARG_AFTER(opt, "-Wl,");
939 add_flag(&ldflags_obst, "-Wl,%s", opt);
940 } else if (streq(option + 1, "no-trigraphs")
941 || streq(option + 1, "undef")) {
942 add_flag(&cppflags_obst, "%s", arg);
944 set_warning_opt(&option[1]);
946 } else if (option[0] == 'm') {
951 GET_ARG_AFTER(opt, "-m");
952 if (strstart(opt, "target=")) {
953 GET_ARG_AFTER(opt, "-mtarget=");
954 if (!parse_target_triple(opt)) {
955 argument_errors = true;
957 setup_target_machine();
960 } else if (strstart(opt, "triple=")) {
961 GET_ARG_AFTER(opt, "-mtriple=");
962 if (!parse_target_triple(opt)) {
963 argument_errors = true;
965 setup_target_machine();
968 } else if (strstart(opt, "arch=")) {
969 GET_ARG_AFTER(opt, "-march=");
970 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
971 int res = be_parse_arg(arch_opt);
973 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
974 argument_errors = true;
976 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
977 int res = be_parse_arg(arch_opt);
979 argument_errors = true;
981 } else if (strstart(opt, "tune=")) {
982 GET_ARG_AFTER(opt, "-mtune=");
983 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
984 int res = be_parse_arg(arch_opt);
986 argument_errors = true;
987 } else if (strstart(opt, "cpu=")) {
988 GET_ARG_AFTER(opt, "-mcpu=");
989 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
990 int res = be_parse_arg(arch_opt);
992 argument_errors = true;
993 } else if (strstart(opt, "fpmath=")) {
994 GET_ARG_AFTER(opt, "-mfpmath=");
995 if (streq(opt, "387"))
997 else if (streq(opt, "sse"))
1000 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1001 argument_errors = true;
1003 if (!argument_errors) {
1004 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1005 int res = be_parse_arg(arch_opt);
1007 argument_errors = true;
1009 } else if (strstart(opt, "preferred-stack-boundary=")) {
1010 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1011 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1012 int res = be_parse_arg(arch_opt);
1014 argument_errors = true;
1015 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1016 set_be_option("omitleaffp=1");
1017 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1018 set_be_option("omitleaffp=0");
1019 } else if (streq(opt, "rtd")) {
1020 default_calling_convention = CC_STDCALL;
1021 } else if (strstart(opt, "regparm=")) {
1022 fprintf(stderr, "error: regparm convention not supported yet\n");
1023 argument_errors = true;
1024 } else if (streq(opt, "soft-float")) {
1025 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1026 argument_errors = true;
1028 long int value = strtol(opt, NULL, 10);
1030 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1031 argument_errors = true;
1032 } else if (value != 16 && value != 32 && value != 64) {
1033 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1034 argument_errors = true;
1036 machine_size = (unsigned int)value;
1037 add_flag(&asflags_obst, "-m%u", machine_size);
1038 add_flag(&ldflags_obst, "-m%u", machine_size);
1041 } else if (streq(option, "pg")) {
1042 set_be_option("gprof");
1043 add_flag(&ldflags_obst, "-pg");
1044 } else if (streq(option, "pedantic") ||
1045 streq(option, "ansi")) {
1046 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1047 } else if (streq(option, "shared")) {
1048 add_flag(&ldflags_obst, "-shared");
1049 } else if (strstart(option, "std=")) {
1050 const char *const o = &option[4];
1052 streq(o, "c++") ? STANDARD_CXX98 :
1053 streq(o, "c++98") ? STANDARD_CXX98 :
1054 streq(o, "c89") ? STANDARD_C89 :
1055 streq(o, "c99") ? STANDARD_C99 :
1056 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1057 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1058 streq(o, "gnu89") ? STANDARD_GNU89 :
1059 streq(o, "gnu99") ? STANDARD_GNU99 :
1060 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1061 streq(o, "iso9899:1990") ? STANDARD_C89 :
1062 streq(o, "iso9899:199409") ? STANDARD_C90 :
1063 streq(o, "iso9899:1999") ? STANDARD_C99 :
1064 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1065 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1066 } else if (streq(option, "version")) {
1067 print_cparser_version();
1068 } else if (strstart(option, "print-file-name=")) {
1069 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1070 } else if (option[0] == '-') {
1071 /* double dash option */
1073 if (streq(option, "gcc")) {
1074 features_on |= _GNUC;
1075 features_off &= ~_GNUC;
1076 } else if (streq(option, "no-gcc")) {
1077 features_on &= ~_GNUC;
1078 features_off |= _GNUC;
1079 } else if (streq(option, "ms")) {
1081 features_off &= ~_MS;
1082 } else if (streq(option, "no-ms")) {
1083 features_on &= ~_MS;
1084 features_off |= _MS;
1085 } else if (streq(option, "strict")) {
1087 } else if (streq(option, "lextest")) {
1089 } else if (streq(option, "benchmark")) {
1090 mode = BenchmarkParser;
1091 } else if (streq(option, "print-ast")) {
1093 } else if (streq(option, "print-implicit-cast")) {
1094 print_implicit_casts = true;
1095 } else if (streq(option, "print-parenthesis")) {
1096 print_parenthesis = true;
1097 } else if (streq(option, "print-fluffy")) {
1099 } else if (streq(option, "print-jna")) {
1101 } else if (streq(option, "jna-limit")) {
1104 fprintf(stderr, "error: "
1105 "expected argument after '--jna-limit'\n");
1106 argument_errors = true;
1109 jna_limit_output(argv[i]);
1110 } else if (streq(option, "jna-libname")) {
1113 fprintf(stderr, "error: "
1114 "expected argument after '--jna-libname'\n");
1115 argument_errors = true;
1118 jna_set_libname(argv[i]);
1119 } else if (streq(option, "time")) {
1121 } else if (streq(option, "version")) {
1122 print_cparser_version();
1123 return EXIT_SUCCESS;
1124 } else if (streq(option, "help")) {
1125 print_help(argv[0]);
1126 help_displayed = true;
1127 } else if (streq(option, "dump-function")) {
1130 fprintf(stderr, "error: "
1131 "expected argument after '--dump-function'\n");
1132 argument_errors = true;
1135 dumpfunction = argv[i];
1137 } else if (streq(option, "export-ir")) {
1138 mode = CompileExportIR;
1140 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1141 argument_errors = true;
1144 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1145 argument_errors = true;
1148 filetype_t type = forced_filetype;
1149 if (type == FILETYPE_AUTODETECT) {
1150 if (streq(arg, "-")) {
1151 /* - implicitly means C source file */
1154 const char *suffix = strrchr(arg, '.');
1155 /* Ensure there is at least one char before the suffix */
1156 if (suffix != NULL && suffix != arg) {
1159 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1160 streq(suffix, "a") ? FILETYPE_OBJECT :
1161 streq(suffix, "c") ? FILETYPE_C :
1162 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1163 streq(suffix, "C") ? FILETYPE_CXX :
1164 streq(suffix, "cc") ? FILETYPE_CXX :
1165 streq(suffix, "cp") ? FILETYPE_CXX :
1166 streq(suffix, "cpp") ? FILETYPE_CXX :
1167 streq(suffix, "CPP") ? FILETYPE_CXX :
1168 streq(suffix, "cxx") ? FILETYPE_CXX :
1169 streq(suffix, "c++") ? FILETYPE_CXX :
1170 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1171 streq(suffix, "h") ? FILETYPE_C :
1172 streq(suffix, "ir") ? FILETYPE_IR :
1173 streq(suffix, "o") ? FILETYPE_OBJECT :
1174 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1175 streq(suffix, "so") ? FILETYPE_OBJECT :
1176 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1181 file_list_entry_t *entry
1182 = obstack_alloc(&file_obst, sizeof(entry[0]));
1183 memset(entry, 0, sizeof(entry[0]));
1187 if (last_file != NULL) {
1188 last_file->next = entry;
1196 if (help_displayed) {
1197 return !argument_errors;
1200 if (print_file_name_file != NULL) {
1201 print_file_name(print_file_name_file);
1202 return EXIT_SUCCESS;
1204 if (files == NULL) {
1205 fprintf(stderr, "error: no input files specified\n");
1206 argument_errors = true;
1209 if (argument_errors) {
1211 return EXIT_FAILURE;
1214 /* apply some effects from switches */
1215 c_mode |= features_on;
1216 c_mode &= ~features_off;
1217 if (profile_generate) {
1218 add_flag(&ldflags_obst, "-lfirmprof");
1219 set_be_option("profilegenerate");
1222 set_be_option("profileuse");
1226 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1227 init_symbol_table();
1240 if (construct_dep_target) {
1241 if (outname != 0 && strlen(outname) >= 2) {
1242 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1244 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1247 dep_target[0] = '\0';
1250 char outnamebuf[4096];
1251 if (outname == NULL) {
1252 const char *filename = files->name;
1255 case BenchmarkParser:
1260 case PreprocessOnly:
1265 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1266 outname = outnamebuf;
1268 case CompileAssemble:
1269 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1270 outname = outnamebuf;
1273 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1275 outname = outnamebuf;
1277 case CompileExportIR:
1278 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1279 outname = outnamebuf;
1281 case CompileAssembleLink:
1291 assert(outname != NULL);
1294 if (streq(outname, "-")) {
1297 out = fopen(outname, "w");
1299 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1301 return EXIT_FAILURE;
1305 file_list_entry_t *file;
1306 bool already_constructed_firm = false;
1307 for (file = files; file != NULL; file = file->next) {
1308 char asm_tempfile[1024];
1309 const char *filename = file->name;
1310 filetype_t filetype = file->type;
1312 if (filetype == FILETYPE_OBJECT)
1316 if (mode == LexTest) {
1318 in = open_file(filename);
1319 lextest(in, filename);
1321 return EXIT_SUCCESS;
1324 FILE *preprocessed_in = NULL;
1325 filetype_t next_filetype = filetype;
1328 next_filetype = FILETYPE_PREPROCESSED_C;
1331 next_filetype = FILETYPE_PREPROCESSED_CXX;
1333 case FILETYPE_ASSEMBLER:
1334 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1337 /* no support for input on FILE* yet */
1339 panic("internal compiler error: in for preprocessor != NULL");
1341 preprocessed_in = preprocess(filename, filetype);
1342 if (mode == PreprocessOnly) {
1343 copy_file(out, preprocessed_in);
1344 int result = pclose(preprocessed_in);
1346 /* remove output file in case of error */
1347 if (out != stdout && result != EXIT_SUCCESS) {
1353 in = preprocessed_in;
1354 filetype = next_filetype;
1362 if (mode == Compile) {
1365 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1369 in = open_file(filename);
1371 /* preprocess and compile */
1372 if (filetype == FILETYPE_PREPROCESSED_C) {
1373 char const* invalid_mode;
1376 case STANDARD_C89: c_mode = _C89; break;
1377 /* TODO determine difference between these two */
1378 case STANDARD_C90: c_mode = _C89; break;
1379 case STANDARD_C99: c_mode = _C89 | _C99; break;
1380 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1384 "warning: command line option \"-std=%s\" is not valid for C\n",
1387 case STANDARD_DEFAULT:
1388 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1390 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1391 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1394 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1395 char const* invalid_mode;
1397 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1398 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1399 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1400 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1401 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1404 case STANDARD_CXX98: c_mode = _CXX; break;
1408 "warning: command line option \"-std=%s\" is not valid for C++\n",
1410 case STANDARD_DEFAULT:
1411 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1415 c_mode |= features_on;
1416 c_mode &= ~features_off;
1418 /* do the actual parsing */
1419 ir_timer_t *t_parsing = ir_timer_new();
1420 timer_register(t_parsing, "Frontend: Parsing");
1421 timer_push(t_parsing);
1423 translation_unit_t *const unit = do_parsing(in, filename);
1424 timer_pop(t_parsing);
1426 /* prints the AST even if errors occurred */
1427 if (mode == PrintAst) {
1432 if (error_count > 0) {
1433 /* parsing failed because of errors */
1434 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1436 result = EXIT_FAILURE;
1438 } else if (warning_count > 0) {
1439 fprintf(stderr, "%u warning(s)\n", warning_count);
1442 if (in == preprocessed_in) {
1443 int pp_result = pclose(preprocessed_in);
1444 if (pp_result != EXIT_SUCCESS) {
1445 /* remove output file */
1448 return EXIT_FAILURE;
1452 if (mode == BenchmarkParser) {
1454 } else if (mode == PrintFluffy) {
1455 write_fluffy_decls(out, unit);
1457 } else if (mode == PrintJna) {
1458 write_jna_decls(out, unit);
1462 /* build the firm graph */
1463 ir_timer_t *t_construct = ir_timer_new();
1464 timer_register(t_construct, "Frontend: Graph construction");
1465 timer_push(t_construct);
1466 if (already_constructed_firm) {
1467 panic("compiling multiple files/translation units not possible");
1469 translation_unit_to_firm(unit);
1470 already_constructed_firm = true;
1471 timer_pop(t_construct);
1474 if (mode == ParseOnly) {
1478 if (mode == CompileDump) {
1480 ident *id = new_id_from_str(dumpfunction);
1481 ir_graph *irg = NULL;
1482 int n_irgs = get_irp_n_irgs();
1483 for (int i = 0; i < n_irgs; ++i) {
1484 ir_graph *tirg = get_irp_irg(i);
1485 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1493 fprintf(stderr, "No graph for function '%s' found\n",
1495 return EXIT_FAILURE;
1498 dump_ir_graph_file(out, irg);
1500 return EXIT_SUCCESS;
1503 if (mode == CompileExportIR) {
1506 return EXIT_SUCCESS;
1509 gen_firm_finish(asm_out, filename);
1510 if (asm_out != out) {
1513 } else if (filetype == FILETYPE_IR) {
1515 ir_import(filename);
1517 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1518 copy_file(asm_out, in);
1519 if (in == preprocessed_in) {
1520 int pp_result = pclose(preprocessed_in);
1521 if (pp_result != EXIT_SUCCESS) {
1522 /* remove output in error case */
1528 if (asm_out != out) {
1533 if (mode == Compile)
1536 /* if we're here then we have preprocessed assembly */
1537 filename = asm_tempfile;
1538 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1541 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1543 const char *filename_o;
1544 if (mode == CompileAssemble) {
1546 filename_o = outname;
1548 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1553 assemble(filename_o, filename);
1555 size_t len = strlen(filename_o) + 1;
1556 filename = obstack_copy(&file_obst, filename_o, len);
1557 filetype = FILETYPE_OBJECT;
1560 /* ok we're done here, process next file */
1561 file->name = filename;
1562 file->type = filetype;
1565 if (result != EXIT_SUCCESS) {
1571 /* link program file */
1572 if (mode == CompileAssembleLink) {
1573 obstack_1grow(&ldflags_obst, '\0');
1574 const char *flags = obstack_finish(&ldflags_obst);
1576 /* construct commandline */
1577 const char *linker = getenv("CPARSER_LINK");
1578 if (linker != NULL) {
1579 obstack_printf(&file_obst, "%s ", linker);
1581 if (target_triple != NULL)
1582 obstack_printf(&file_obst, "%s-", target_triple);
1583 obstack_printf(&file_obst, "%s ", LINKER);
1586 for (file_list_entry_t *entry = files; entry != NULL;
1587 entry = entry->next) {
1588 if (entry->type != FILETYPE_OBJECT)
1591 add_flag(&file_obst, "%s", entry->name);
1594 add_flag(&file_obst, "-o");
1595 add_flag(&file_obst, outname);
1596 obstack_printf(&file_obst, "%s", flags);
1597 obstack_1grow(&file_obst, '\0');
1599 char *commandline = obstack_finish(&file_obst);
1604 int err = system(commandline);
1605 if (err != EXIT_SUCCESS) {
1606 fprintf(stderr, "linker reported an error\n");
1607 return EXIT_FAILURE;
1614 obstack_free(&cppflags_obst, NULL);
1615 obstack_free(&ldflags_obst, NULL);
1616 obstack_free(&asflags_obst, NULL);
1617 obstack_free(&file_obst, NULL);
1627 exit_symbol_table();
1628 return EXIT_SUCCESS;