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;
127 typedef enum lang_standard_t {
128 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
129 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
130 STANDARD_C89, /* ISO C90 (sic) */
131 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
132 STANDARD_C99, /* ISO C99 */
133 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
134 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
135 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
136 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
139 static lang_standard_t standard;
141 typedef struct file_list_entry_t file_list_entry_t;
143 typedef enum filetype_t {
146 FILETYPE_PREPROCESSED_C,
148 FILETYPE_PREPROCESSED_CXX,
150 FILETYPE_PREPROCESSED_ASSEMBLER,
156 struct file_list_entry_t {
157 const char *name; /**< filename or NULL for stdin */
159 file_list_entry_t *next;
162 static file_list_entry_t *temp_files;
164 static void get_output_name(char *buf, size_t buflen, const char *inputname,
167 if (inputname == NULL)
170 char const *const last_slash = strrchr(inputname, '/');
171 char const *const filename =
172 last_slash != NULL ? last_slash + 1 : inputname;
173 char const *const last_dot = strrchr(filename, '.');
174 char const *const name_end =
175 last_dot != NULL ? last_dot : strchr(filename, '\0');
177 int const len = snprintf(buf, buflen, "%.*s%s",
178 (int)(name_end - filename), filename, newext);
180 if (len < 0 || buflen <= (size_t)len)
182 if (buflen <= (size_t)len)
184 panic("filename too long");
187 #include "gen_builtins.h"
189 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
194 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
198 lexer_open_stream(in, input_name);
201 translation_unit_t *unit = finish_parsing();
205 static void lextest(FILE *in, const char *fname)
207 lexer_open_stream(in, fname);
210 lexer_next_preprocessing_token();
211 print_token(stdout, &lexer_token);
213 } while (lexer_token.type != T_EOF);
216 static void add_flag(struct obstack *obst, const char *format, ...)
221 va_start(ap, format);
225 vsnprintf(buf, sizeof(buf), format, ap);
228 obstack_1grow(obst, ' ');
230 obstack_1grow(obst, '"');
231 obstack_grow(obst, buf, len);
232 obstack_1grow(obst, '"');
234 /* escape stuff... */
235 for (char *c = buf; *c != '\0'; ++c) {
253 obstack_1grow(obst, '\\');
256 obstack_1grow(obst, *c);
263 static const char *type_to_string(type_t *type)
265 assert(type->kind == TYPE_ATOMIC);
266 return get_atomic_kind_name(type->atomic.akind);
269 static FILE *preprocess(const char *fname, filetype_t filetype)
271 static const char *common_flags = NULL;
273 if (common_flags == NULL) {
274 obstack_1grow(&cppflags_obst, '\0');
275 const char *flags = obstack_finish(&cppflags_obst);
277 /* setup default defines */
278 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
279 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
280 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
281 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
283 add_flag(&cppflags_obst, "-U__VERSION__");
284 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
286 if (flags[0] != '\0') {
287 size_t len = strlen(flags);
288 obstack_1grow(&cppflags_obst, ' ');
289 obstack_grow(&cppflags_obst, flags, len);
291 obstack_1grow(&cppflags_obst, '\0');
292 common_flags = obstack_finish(&cppflags_obst);
295 assert(obstack_object_size(&cppflags_obst) == 0);
297 const char *preprocessor = getenv("CPARSER_PP");
298 if (preprocessor != NULL) {
299 obstack_printf(&cppflags_obst, "%s ", preprocessor);
301 if (target_triple != NULL)
302 obstack_printf(&cppflags_obst, "%s-", target_triple);
303 obstack_printf(&cppflags_obst, PREPROCESSOR);
308 add_flag(&cppflags_obst, "-std=c99");
311 add_flag(&cppflags_obst, "-std=c++98");
313 case FILETYPE_ASSEMBLER:
314 add_flag(&cppflags_obst, "-x");
315 add_flag(&cppflags_obst, "assembler-with-cpp");
320 obstack_printf(&cppflags_obst, "%s", common_flags);
322 /* handle dependency generation */
323 if (dep_target[0] != '\0') {
324 add_flag(&cppflags_obst, "-MF");
325 add_flag(&cppflags_obst, dep_target);
326 if (outname != NULL) {
327 add_flag(&cppflags_obst, "-MQ");
328 add_flag(&cppflags_obst, outname);
331 add_flag(&cppflags_obst, fname);
332 obstack_1grow(&cppflags_obst, '\0');
334 char *commandline = obstack_finish(&cppflags_obst);
338 FILE *f = popen(commandline, "r");
340 fprintf(stderr, "invoking preprocessor failed\n");
343 /* we don't really need that anymore */
344 obstack_free(&cppflags_obst, commandline);
349 static void assemble(const char *out, const char *in)
351 obstack_1grow(&asflags_obst, '\0');
352 const char *flags = obstack_finish(&asflags_obst);
354 const char *assembler = getenv("CPARSER_AS");
355 if (assembler != NULL) {
356 obstack_printf(&asflags_obst, "%s", assembler);
358 if (target_triple != NULL)
359 obstack_printf(&asflags_obst, "%s-", target_triple);
360 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
362 if (flags[0] != '\0')
363 obstack_printf(&asflags_obst, " %s", flags);
365 obstack_printf(&asflags_obst, " %s -o %s", in, out);
366 obstack_1grow(&asflags_obst, '\0');
368 char *commandline = obstack_finish(&asflags_obst);
372 int err = system(commandline);
373 if (err != EXIT_SUCCESS) {
374 fprintf(stderr, "assembler reported an error\n");
377 obstack_free(&asflags_obst, commandline);
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");
389 if (linker != NULL) {
390 obstack_printf(&ldflags_obst, "%s ", linker);
392 if (target_triple != NULL)
393 obstack_printf(&ldflags_obst, "%s-", target_triple);
394 obstack_printf(&ldflags_obst, "%s ", LINKER);
396 obstack_printf(&ldflags_obst, "%s ", linker);
397 obstack_printf(&ldflags_obst, "%s", flags);
398 obstack_1grow(&ldflags_obst, '\0');
400 char *commandline = obstack_finish(&ldflags_obst);
404 int err = system(commandline);
405 if (err != EXIT_SUCCESS) {
406 fprintf(stderr, "linker reported an error\n");
409 obstack_free(&ldflags_obst, commandline);
412 static const char *try_dir(const char *dir)
416 if (access(dir, R_OK | W_OK | X_OK) == 0)
421 static const char *get_tempdir(void)
423 static const char *tmpdir = NULL;
429 tmpdir = try_dir(getenv("TMPDIR"));
431 tmpdir = try_dir(getenv("TMP"));
433 tmpdir = try_dir(getenv("TEMP"));
437 tmpdir = try_dir(P_tmpdir);
441 tmpdir = try_dir("/var/tmp");
443 tmpdir = try_dir("/usr/tmp");
445 tmpdir = try_dir("/tmp");
454 /* cheap and nasty mkstemp replacement */
455 static int mkstemp(char *templ)
458 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
463 * an own version of tmpnam, which: writes in a buffer, emits no warnings
464 * during linking (like glibc/gnu ld do for tmpnam)...
466 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
468 const char *tempdir = get_tempdir();
470 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
472 int fd = mkstemp(buffer);
474 fprintf(stderr, "couldn't create temporary file: %s\n",
478 FILE *out = fdopen(fd, "w");
480 fprintf(stderr, "couldn't create temporary file FILE*\n");
484 file_list_entry_t *entry = xmalloc(sizeof(*entry));
485 memset(entry, 0, sizeof(*entry));
487 size_t name_len = strlen(buffer) + 1;
488 char *name = malloc(name_len);
489 memcpy(name, buffer, name_len);
492 entry->next = temp_files;
498 static void free_temp_files(void)
500 file_list_entry_t *entry = temp_files;
501 file_list_entry_t *next;
502 for ( ; entry != NULL; entry = next) {
506 free((char*) entry->name);
512 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 copy_file(FILE *dest, FILE *input)
574 while (!feof(input) && !ferror(dest)) {
575 size_t read = fread(buf, 1, sizeof(buf), input);
576 if (fwrite(buf, 1, read, dest) != read) {
577 perror("couldn't write output");
582 static FILE *open_file(const char *filename)
584 if (streq(filename, "-")) {
588 FILE *in = fopen(filename, "r");
590 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
598 static filetype_t get_filetype_from_string(const char *string)
600 if (streq(string, "c") || streq(string, "c-header"))
602 if (streq(string, "c++") || streq(string, "c++-header"))
604 if (streq(string, "assembler"))
605 return FILETYPE_PREPROCESSED_ASSEMBLER;
606 if (streq(string, "assembler-with-cpp"))
607 return FILETYPE_ASSEMBLER;
608 if (streq(string, "none"))
609 return FILETYPE_AUTODETECT;
611 return FILETYPE_UNKNOWN;
614 static bool init_os_support(void)
616 const char *os = target_machine->operating_system;
617 wchar_atomic_kind = ATOMIC_TYPE_INT;
618 force_long_double_size = 0;
619 enable_main_collect2_hack = false;
621 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
622 || streq(os, "solaris")) {
623 set_create_ld_ident(create_name_linux_elf);
624 } else if (streq(os, "darwin")) {
625 force_long_double_size = 16;
626 set_create_ld_ident(create_name_macho);
627 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
628 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
629 enable_main_collect2_hack = true;
630 set_create_ld_ident(create_name_win32);
638 static bool parse_target_triple(const char *arg)
640 machine_triple_t *triple = firm_parse_machine_triple(arg);
641 if (triple == NULL) {
642 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
645 target_machine = triple;
649 static void setup_target_machine(void)
651 if (!setup_firm_for_machine(target_machine))
656 int main(int argc, char **argv)
660 const char *dumpfunction = NULL;
661 const char *print_file_name_file = NULL;
662 compile_mode_t mode = CompileAssembleLink;
664 int result = EXIT_SUCCESS;
665 char cpu_arch[16] = "ia32";
666 file_list_entry_t *files = NULL;
667 file_list_entry_t *last_file = NULL;
668 bool construct_dep_target = false;
669 bool do_timing = false;
670 struct obstack file_obst;
672 atexit(free_temp_files);
674 /* hack for now... */
675 if (strstr(argv[0], "pptest") != NULL) {
676 extern int pptest_main(int argc, char **argv);
677 return pptest_main(argc, argv);
680 obstack_init(&cppflags_obst);
681 obstack_init(&ldflags_obst);
682 obstack_init(&asflags_obst);
683 obstack_init(&file_obst);
685 #define GET_ARG_AFTER(def, args) \
686 def = &arg[sizeof(args)-1]; \
687 if (def[0] == '\0') { \
690 fprintf(stderr, "error: expected argument after '" args "'\n"); \
691 argument_errors = true; \
695 if (def[0] == '-' && def[1] != '\0') { \
696 fprintf(stderr, "error: expected argument after '" args "'\n"); \
697 argument_errors = true; \
702 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
704 /* early options parsing (find out optimisation level and OS) */
705 for (int i = 1; i < argc; ++i) {
706 const char *arg = argv[i];
710 const char *option = &arg[1];
711 if (option[0] == 'O') {
712 sscanf(&option[1], "%d", &opt_level);
716 const char *target = getenv("TARGET");
718 parse_target_triple(target);
719 if (target_machine == NULL) {
720 target_machine = firm_get_host_machine();
722 choose_optimization_pack(opt_level);
723 setup_target_machine();
725 /* parse rest of options */
726 standard = STANDARD_DEFAULT;
727 unsigned features_on = 0;
728 unsigned features_off = 0;
729 filetype_t forced_filetype = FILETYPE_AUTODETECT;
730 bool help_displayed = false;
731 bool argument_errors = false;
732 for (int i = 1; i < argc; ++i) {
733 const char *arg = argv[i];
734 if (arg[0] == '-' && arg[1] != '\0') {
736 const char *option = &arg[1];
737 if (option[0] == 'o') {
738 GET_ARG_AFTER(outname, "-o");
739 } else if (option[0] == 'g') {
740 set_be_option("debuginfo=stabs");
741 set_be_option("omitfp=no");
742 set_be_option("ia32-nooptcc=yes");
743 } else if (SINGLE_OPTION('c')) {
744 mode = CompileAssemble;
745 } else if (SINGLE_OPTION('E')) {
746 mode = PreprocessOnly;
747 } else if (SINGLE_OPTION('S')) {
749 } else if (option[0] == 'O') {
751 } else if (option[0] == 'I') {
753 GET_ARG_AFTER(opt, "-I");
754 add_flag(&cppflags_obst, "-I%s", opt);
755 } else if (option[0] == 'D') {
757 GET_ARG_AFTER(opt, "-D");
758 add_flag(&cppflags_obst, "-D%s", opt);
759 } else if (option[0] == 'U') {
761 GET_ARG_AFTER(opt, "-U");
762 add_flag(&cppflags_obst, "-U%s", opt);
763 } else if (option[0] == 'l') {
765 GET_ARG_AFTER(opt, "-l");
766 add_flag(&ldflags_obst, "-l%s", opt);
767 } else if (option[0] == 'L') {
769 GET_ARG_AFTER(opt, "-L");
770 add_flag(&ldflags_obst, "-L%s", opt);
771 } else if (SINGLE_OPTION('v')) {
773 } else if (SINGLE_OPTION('w')) {
774 memset(&warning, 0, sizeof(warning));
775 } else if (option[0] == 'x') {
777 GET_ARG_AFTER(opt, "-x");
778 forced_filetype = get_filetype_from_string(opt);
779 if (forced_filetype == FILETYPE_UNKNOWN) {
780 fprintf(stderr, "Unknown language '%s'\n", opt);
781 argument_errors = true;
783 } else if (streq(option, "M")) {
784 mode = PreprocessOnly;
785 add_flag(&cppflags_obst, "-M");
786 } else if (streq(option, "MMD") ||
787 streq(option, "MD")) {
788 construct_dep_target = true;
789 add_flag(&cppflags_obst, "-%s", option);
790 } else if (streq(option, "MM") ||
791 streq(option, "MP")) {
792 add_flag(&cppflags_obst, "-%s", option);
793 } else if (streq(option, "MT") ||
794 streq(option, "MQ") ||
795 streq(option, "MF")) {
797 GET_ARG_AFTER(opt, "-MT");
798 add_flag(&cppflags_obst, "-%s", option);
799 add_flag(&cppflags_obst, "%s", opt);
800 } else if (streq(option, "include")) {
802 GET_ARG_AFTER(opt, "-include");
803 add_flag(&cppflags_obst, "-include");
804 add_flag(&cppflags_obst, "%s", opt);
805 } else if (streq(option, "isystem")) {
807 GET_ARG_AFTER(opt, "-isystem");
808 add_flag(&cppflags_obst, "-isystem");
809 add_flag(&cppflags_obst, "%s", opt);
810 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
811 } else if (streq(option, "pthread")) {
812 /* set flags for the preprocessor */
813 add_flag(&cppflags_obst, "-D_REENTRANT");
814 /* set flags for the linker */
815 add_flag(&ldflags_obst, "-lpthread");
817 } else if (streq(option, "nostdinc")
818 || streq(option, "trigraphs")) {
819 /* pass these through to the preprocessor */
820 add_flag(&cppflags_obst, "%s", arg);
821 } else if (streq(option, "pipe")) {
822 /* here for gcc compatibility */
823 } else if (streq(option, "static")) {
824 add_flag(&ldflags_obst, "-static");
825 } else if (streq(option, "shared")) {
826 add_flag(&ldflags_obst, "-shared");
827 } else if (option[0] == 'f') {
828 char const *orig_opt;
829 GET_ARG_AFTER(orig_opt, "-f");
831 if (strstart(orig_opt, "input-charset=")) {
832 char const* const encoding = strchr(orig_opt, '=') + 1;
833 select_input_encoding(encoding);
834 } else if (strstart(orig_opt, "align-loops=") ||
835 strstart(orig_opt, "align-jumps=") ||
836 strstart(orig_opt, "align-functions=")) {
837 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
838 } else if (strstart(orig_opt, "message-length=")) {
839 /* ignore: would only affect error message format */
841 /* -f options which have an -fno- variant */
842 char const *opt = orig_opt;
843 bool truth_value = true;
844 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
849 if (streq(opt, "builtins")) {
850 use_builtins = truth_value;
851 } else if (streq(opt, "dollars-in-identifiers")) {
852 allow_dollar_in_symbol = truth_value;
853 } else if (streq(opt, "omit-frame-pointer")) {
854 set_be_option(truth_value ? "omitfp" : "omitfp=no");
855 } else if (streq(opt, "short-wchar")) {
856 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
858 } else if (streq(opt, "signed-char")) {
859 char_is_signed = truth_value;
860 } else if (streq(opt, "strength-reduce")) {
861 firm_option(truth_value ? "strength-red" : "no-strength-red");
862 } else if (streq(opt, "syntax-only")) {
863 mode = truth_value ? ParseOnly : CompileAssembleLink;
864 } else if (streq(opt, "unsigned-char")) {
865 char_is_signed = !truth_value;
866 } else if (streq(opt, "freestanding")) {
867 freestanding = truth_value;
868 } else if (streq(opt, "hosted")) {
869 freestanding = !truth_value;
870 } else if (truth_value == false &&
871 streq(opt, "asynchronous-unwind-tables")) {
872 /* nothing todo, a gcc feature which we don't support
873 * anyway was deactivated */
874 } else if (streq(orig_opt, "verbose-asm")) {
875 /* ignore: we always print verbose assembler */
876 } else if (streq(opt, "fast-math") ||
877 streq(opt, "jump-tables") ||
878 streq(opt, "expensive-optimizations") ||
879 streq(opt, "common") ||
880 streq(opt, "optimize-sibling-calls") ||
881 streq(opt, "align-loops") ||
882 streq(opt, "align-jumps") ||
883 streq(opt, "align-functions") ||
885 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
887 int res = firm_option(orig_opt);
889 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
891 argument_errors = true;
893 } else if (res == -1) {
894 help_displayed = true;
898 } else if (option[0] == 'b') {
900 GET_ARG_AFTER(opt, "-b");
901 int res = be_parse_arg(opt);
903 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
905 argument_errors = true;
906 } else if (res == -1) {
907 help_displayed = true;
908 } else if (strstart(opt, "isa=")) {
909 strncpy(cpu_arch, opt, sizeof(cpu_arch));
911 } else if (option[0] == 'W') {
912 if (option[1] == '\0') {
913 /* ignore -W, our defaults are already quite verbose */
914 } else if (strstart(option + 1, "p,")) {
915 // pass options directly to the preprocessor
917 GET_ARG_AFTER(opt, "-Wp,");
918 add_flag(&cppflags_obst, "-Wp,%s", opt);
919 } else if (strstart(option + 1, "l,")) {
920 // pass options directly to the linker
922 GET_ARG_AFTER(opt, "-Wl,");
923 add_flag(&ldflags_obst, "-Wl,%s", opt);
924 } else if (streq(option + 1, "no-trigraphs")
925 || streq(option + 1, "undef")) {
926 add_flag(&cppflags_obst, "%s", arg);
928 set_warning_opt(&option[1]);
930 } else if (option[0] == 'm') {
935 GET_ARG_AFTER(opt, "-m");
936 if (strstart(opt, "target=")) {
937 GET_ARG_AFTER(opt, "-mtarget=");
938 if (!parse_target_triple(opt)) {
939 argument_errors = true;
941 setup_target_machine();
944 } else if (strstart(opt, "triple=")) {
945 GET_ARG_AFTER(opt, "-mtriple=");
946 if (!parse_target_triple(opt)) {
947 argument_errors = true;
949 setup_target_machine();
952 } else if (strstart(opt, "arch=")) {
953 GET_ARG_AFTER(opt, "-march=");
954 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
955 int res = be_parse_arg(arch_opt);
957 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
958 argument_errors = true;
960 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
961 int res = be_parse_arg(arch_opt);
963 argument_errors = true;
965 } else if (strstart(opt, "tune=")) {
966 GET_ARG_AFTER(opt, "-mtune=");
967 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
968 int res = be_parse_arg(arch_opt);
970 argument_errors = true;
971 } else if (strstart(opt, "cpu=")) {
972 GET_ARG_AFTER(opt, "-mcpu=");
973 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
974 int res = be_parse_arg(arch_opt);
976 argument_errors = true;
977 } else if (strstart(opt, "fpmath=")) {
978 GET_ARG_AFTER(opt, "-mfpmath=");
979 if (streq(opt, "387"))
981 else if (streq(opt, "sse"))
984 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
985 argument_errors = true;
987 if (!argument_errors) {
988 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
989 int res = be_parse_arg(arch_opt);
991 argument_errors = true;
993 } else if (strstart(opt, "preferred-stack-boundary=")) {
994 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
995 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
996 int res = be_parse_arg(arch_opt);
998 argument_errors = true;
999 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1000 set_be_option("omitleaffp=1");
1001 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1002 set_be_option("omitleaffp=0");
1003 } else if (streq(opt, "rtd")) {
1004 default_calling_convention = CC_STDCALL;
1005 } else if (strstart(opt, "regparm=")) {
1006 fprintf(stderr, "error: regparm convention not supported yet\n");
1007 argument_errors = true;
1008 } else if (streq(opt, "soft-float")) {
1009 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1010 argument_errors = true;
1012 long int value = strtol(opt, NULL, 10);
1014 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1015 argument_errors = true;
1016 } else if (value != 16 && value != 32 && value != 64) {
1017 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1018 argument_errors = true;
1020 machine_size = (unsigned int)value;
1021 add_flag(&asflags_obst, "-m%u", machine_size);
1022 add_flag(&ldflags_obst, "-m%u", machine_size);
1025 } else if (streq(option, "pg")) {
1026 set_be_option("gprof");
1027 add_flag(&ldflags_obst, "-pg");
1028 } else if (streq(option, "pedantic") ||
1029 streq(option, "ansi")) {
1030 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1031 } else if (streq(option, "shared")) {
1032 add_flag(&ldflags_obst, "-shared");
1033 } else if (strstart(option, "std=")) {
1034 const char *const o = &option[4];
1036 streq(o, "c++") ? STANDARD_CXX98 :
1037 streq(o, "c++98") ? STANDARD_CXX98 :
1038 streq(o, "c89") ? STANDARD_C89 :
1039 streq(o, "c99") ? STANDARD_C99 :
1040 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1041 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1042 streq(o, "gnu89") ? STANDARD_GNU89 :
1043 streq(o, "gnu99") ? STANDARD_GNU99 :
1044 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1045 streq(o, "iso9899:1990") ? STANDARD_C89 :
1046 streq(o, "iso9899:199409") ? STANDARD_C90 :
1047 streq(o, "iso9899:1999") ? STANDARD_C99 :
1048 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1049 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1050 } else if (streq(option, "version")) {
1051 print_cparser_version();
1052 } else if (strstart(option, "print-file-name=")) {
1053 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1054 } else if (option[0] == '-') {
1055 /* double dash option */
1057 if (streq(option, "gcc")) {
1058 features_on |= _GNUC;
1059 features_off &= ~_GNUC;
1060 } else if (streq(option, "no-gcc")) {
1061 features_on &= ~_GNUC;
1062 features_off |= _GNUC;
1063 } else if (streq(option, "ms")) {
1065 features_off &= ~_MS;
1066 } else if (streq(option, "no-ms")) {
1067 features_on &= ~_MS;
1068 features_off |= _MS;
1069 } else if (streq(option, "strict")) {
1071 } else if (streq(option, "lextest")) {
1073 } else if (streq(option, "benchmark")) {
1074 mode = BenchmarkParser;
1075 } else if (streq(option, "print-ast")) {
1077 } else if (streq(option, "print-implicit-cast")) {
1078 print_implicit_casts = true;
1079 } else if (streq(option, "print-parenthesis")) {
1080 print_parenthesis = true;
1081 } else if (streq(option, "print-fluffy")) {
1083 } else if (streq(option, "print-jna")) {
1085 } else if (streq(option, "jna-limit")) {
1088 fprintf(stderr, "error: "
1089 "expected argument after '--jna-limit'\n");
1090 argument_errors = true;
1093 jna_limit_output(argv[i]);
1094 } else if (streq(option, "jna-libname")) {
1097 fprintf(stderr, "error: "
1098 "expected argument after '--jna-libname'\n");
1099 argument_errors = true;
1102 jna_set_libname(argv[i]);
1103 } else if (streq(option, "time")) {
1105 } else if (streq(option, "version")) {
1106 print_cparser_version();
1107 return EXIT_SUCCESS;
1108 } else if (streq(option, "help")) {
1109 print_help(argv[0]);
1110 help_displayed = true;
1111 } else if (streq(option, "dump-function")) {
1114 fprintf(stderr, "error: "
1115 "expected argument after '--dump-function'\n");
1116 argument_errors = true;
1119 dumpfunction = argv[i];
1121 } else if (streq(option, "export-ir")) {
1122 mode = CompileExportIR;
1124 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1125 argument_errors = true;
1128 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1129 argument_errors = true;
1132 filetype_t type = forced_filetype;
1133 if (type == FILETYPE_AUTODETECT) {
1134 if (streq(arg, "-")) {
1135 /* - implicitly means C source file */
1138 const char *suffix = strrchr(arg, '.');
1139 /* Ensure there is at least one char before the suffix */
1140 if (suffix != NULL && suffix != arg) {
1143 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1144 streq(suffix, "a") ? FILETYPE_OBJECT :
1145 streq(suffix, "c") ? FILETYPE_C :
1146 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1147 streq(suffix, "C") ? FILETYPE_CXX :
1148 streq(suffix, "cc") ? FILETYPE_CXX :
1149 streq(suffix, "cp") ? FILETYPE_CXX :
1150 streq(suffix, "cpp") ? FILETYPE_CXX :
1151 streq(suffix, "CPP") ? FILETYPE_CXX :
1152 streq(suffix, "cxx") ? FILETYPE_CXX :
1153 streq(suffix, "c++") ? FILETYPE_CXX :
1154 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1155 streq(suffix, "h") ? FILETYPE_C :
1156 streq(suffix, "ir") ? FILETYPE_IR :
1157 streq(suffix, "o") ? FILETYPE_OBJECT :
1158 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1159 streq(suffix, "so") ? FILETYPE_OBJECT :
1160 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1165 file_list_entry_t *entry
1166 = obstack_alloc(&file_obst, sizeof(entry[0]));
1167 memset(entry, 0, sizeof(entry[0]));
1171 if (last_file != NULL) {
1172 last_file->next = entry;
1180 if (help_displayed) {
1181 return !argument_errors;
1184 if (print_file_name_file != NULL) {
1185 print_file_name(print_file_name_file);
1186 return EXIT_SUCCESS;
1188 if (files == NULL) {
1189 fprintf(stderr, "error: no input files specified\n");
1190 argument_errors = true;
1193 if (argument_errors) {
1195 return EXIT_FAILURE;
1198 /* set the c_mode here, types depends on it */
1199 c_mode |= features_on;
1200 c_mode &= ~features_off;
1203 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1204 init_symbol_table();
1217 if (construct_dep_target) {
1218 if (outname != 0 && strlen(outname) >= 2) {
1219 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1221 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1224 dep_target[0] = '\0';
1227 char outnamebuf[4096];
1228 if (outname == NULL) {
1229 const char *filename = files->name;
1232 case BenchmarkParser:
1237 case PreprocessOnly:
1242 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1243 outname = outnamebuf;
1245 case CompileAssemble:
1246 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1247 outname = outnamebuf;
1250 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1252 outname = outnamebuf;
1254 case CompileExportIR:
1255 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1256 outname = outnamebuf;
1258 case CompileAssembleLink:
1268 assert(outname != NULL);
1271 if (streq(outname, "-")) {
1274 out = fopen(outname, "w");
1276 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1278 return EXIT_FAILURE;
1282 file_list_entry_t *file;
1283 bool already_constructed_firm = false;
1284 for (file = files; file != NULL; file = file->next) {
1285 char asm_tempfile[1024];
1286 const char *filename = file->name;
1287 filetype_t filetype = file->type;
1289 if (filetype == FILETYPE_OBJECT)
1293 if (mode == LexTest) {
1295 in = open_file(filename);
1296 lextest(in, filename);
1298 return EXIT_SUCCESS;
1301 FILE *preprocessed_in = NULL;
1302 filetype_t next_filetype = filetype;
1305 next_filetype = FILETYPE_PREPROCESSED_C;
1308 next_filetype = FILETYPE_PREPROCESSED_CXX;
1310 case FILETYPE_ASSEMBLER:
1311 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1314 /* no support for input on FILE* yet */
1316 panic("internal compiler error: in for preprocessor != NULL");
1318 preprocessed_in = preprocess(filename, filetype);
1319 if (mode == PreprocessOnly) {
1320 copy_file(out, preprocessed_in);
1321 int result = pclose(preprocessed_in);
1323 /* remove output file in case of error */
1324 if (out != stdout && result != EXIT_SUCCESS) {
1330 in = preprocessed_in;
1331 filetype = next_filetype;
1339 if (mode == Compile) {
1342 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1346 in = open_file(filename);
1348 /* preprocess and compile */
1349 if (filetype == FILETYPE_PREPROCESSED_C) {
1350 char const* invalid_mode;
1353 case STANDARD_C89: c_mode = _C89; break;
1354 /* TODO determine difference between these two */
1355 case STANDARD_C90: c_mode = _C89; break;
1356 case STANDARD_C99: c_mode = _C89 | _C99; break;
1357 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1361 "warning: command line option \"-std=%s\" is not valid for C\n",
1364 case STANDARD_DEFAULT:
1365 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1367 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1368 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1371 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1372 char const* invalid_mode;
1374 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1375 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1376 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1377 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1378 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1381 case STANDARD_CXX98: c_mode = _CXX; break;
1385 "warning: command line option \"-std=%s\" is not valid for C++\n",
1387 case STANDARD_DEFAULT:
1388 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1392 c_mode |= features_on;
1393 c_mode &= ~features_off;
1395 /* do the actual parsing */
1396 ir_timer_t *t_parsing = ir_timer_new();
1397 timer_register(t_parsing, "Frontend: Parsing");
1398 timer_push(t_parsing);
1400 translation_unit_t *const unit = do_parsing(in, filename);
1401 timer_pop(t_parsing);
1403 /* prints the AST even if errors occurred */
1404 if (mode == PrintAst) {
1409 if (error_count > 0) {
1410 /* parsing failed because of errors */
1411 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1413 result = EXIT_FAILURE;
1415 } else if (warning_count > 0) {
1416 fprintf(stderr, "%u warning(s)\n", warning_count);
1419 if (in == preprocessed_in) {
1420 int pp_result = pclose(preprocessed_in);
1421 if (pp_result != EXIT_SUCCESS) {
1422 /* remove output file */
1425 return EXIT_FAILURE;
1429 if (mode == BenchmarkParser) {
1431 } else if (mode == PrintFluffy) {
1432 write_fluffy_decls(out, unit);
1434 } else if (mode == PrintJna) {
1435 write_jna_decls(out, unit);
1439 /* build the firm graph */
1440 ir_timer_t *t_construct = ir_timer_new();
1441 timer_register(t_construct, "Frontend: Graph construction");
1442 timer_push(t_construct);
1443 if (already_constructed_firm) {
1444 panic("compiling multiple files/translation units not possible");
1446 translation_unit_to_firm(unit);
1447 already_constructed_firm = true;
1448 timer_pop(t_construct);
1451 if (mode == ParseOnly) {
1455 if (mode == CompileDump) {
1457 ident *id = new_id_from_str(dumpfunction);
1458 ir_graph *irg = NULL;
1459 int n_irgs = get_irp_n_irgs();
1460 for (int i = 0; i < n_irgs; ++i) {
1461 ir_graph *tirg = get_irp_irg(i);
1462 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1470 fprintf(stderr, "No graph for function '%s' found\n",
1472 return EXIT_FAILURE;
1475 dump_ir_graph_file(out, irg);
1477 return EXIT_SUCCESS;
1480 if (mode == CompileExportIR) {
1483 return EXIT_SUCCESS;
1486 gen_firm_finish(asm_out, filename);
1487 if (asm_out != out) {
1490 } else if (filetype == FILETYPE_IR) {
1492 ir_import(filename);
1494 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1495 copy_file(asm_out, in);
1496 if (in == preprocessed_in) {
1497 int pp_result = pclose(preprocessed_in);
1498 if (pp_result != EXIT_SUCCESS) {
1499 /* remove output in error case */
1505 if (asm_out != out) {
1510 if (mode == Compile)
1513 /* if we're here then we have preprocessed assembly */
1514 filename = asm_tempfile;
1515 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1518 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1520 const char *filename_o;
1521 if (mode == CompileAssemble) {
1523 filename_o = outname;
1525 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1530 assemble(filename_o, filename);
1532 size_t len = strlen(filename_o) + 1;
1533 filename = obstack_copy(&file_obst, filename_o, len);
1534 filetype = FILETYPE_OBJECT;
1537 /* ok we're done here, process next file */
1538 file->name = filename;
1539 file->type = filetype;
1542 if (result != EXIT_SUCCESS) {
1548 /* link program file */
1549 if (mode == CompileAssembleLink) {
1550 obstack_1grow(&ldflags_obst, '\0');
1551 const char *flags = obstack_finish(&ldflags_obst);
1553 /* construct commandline */
1554 const char *linker = getenv("CPARSER_LINK");
1555 if (linker != NULL) {
1556 obstack_printf(&file_obst, "%s ", linker);
1558 if (target_triple != NULL)
1559 obstack_printf(&file_obst, "%s-", target_triple);
1560 obstack_printf(&file_obst, "%s ", LINKER);
1563 for (file_list_entry_t *entry = files; entry != NULL;
1564 entry = entry->next) {
1565 if (entry->type != FILETYPE_OBJECT)
1568 add_flag(&file_obst, "%s", entry->name);
1571 add_flag(&file_obst, "-o");
1572 add_flag(&file_obst, outname);
1573 obstack_printf(&file_obst, "%s", flags);
1574 obstack_1grow(&file_obst, '\0');
1576 char *commandline = obstack_finish(&file_obst);
1581 int err = system(commandline);
1582 if (err != EXIT_SUCCESS) {
1583 fprintf(stderr, "linker reported an error\n");
1584 return EXIT_FAILURE;
1591 obstack_free(&cppflags_obst, NULL);
1592 obstack_free(&ldflags_obst, NULL);
1593 obstack_free(&asflags_obst, NULL);
1594 obstack_free(&file_obst, NULL);
1604 exit_symbol_table();
1605 return EXIT_SUCCESS;