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_jna.h"
85 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
87 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
97 #define ASSEMBLER "gcc -c -xassembler"
99 #define ASSEMBLER "as"
103 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
104 unsigned int machine_size = 32;
105 bool byte_order_big_endian = false;
106 bool char_is_signed = true;
107 bool strict_mode = false;
108 bool use_builtins = false;
109 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
110 unsigned force_long_double_size = 0;
111 bool enable_main_collect2_hack = false;
112 bool freestanding = false;
114 /* to switch on printing of implicit casts */
115 extern bool print_implicit_casts;
117 /* to switch on printing of parenthesis to indicate operator precedence */
118 extern bool print_parenthesis;
120 static const char *target_triple;
122 static struct obstack cppflags_obst;
123 static struct obstack ldflags_obst;
124 static struct obstack asflags_obst;
125 static char dep_target[1024];
126 static const char *outname;
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 initialize_firm(void)
170 static void get_output_name(char *buf, size_t buflen, const char *inputname,
173 if (inputname == NULL)
176 char const *const last_slash = strrchr(inputname, '/');
177 char const *const filename =
178 last_slash != NULL ? last_slash + 1 : inputname;
179 char const *const last_dot = strrchr(filename, '.');
180 char const *const name_end =
181 last_dot != NULL ? last_dot : strchr(filename, '\0');
183 int const len = snprintf(buf, buflen, "%.*s%s",
184 (int)(name_end - filename), filename, newext);
186 if (len < 0 || buflen <= (size_t)len)
188 if (buflen <= (size_t)len)
190 panic("filename too long");
193 #include "gen_builtins.h"
195 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
200 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
204 lexer_open_stream(in, input_name);
207 translation_unit_t *unit = finish_parsing();
211 static void lextest(FILE *in, const char *fname)
213 lexer_open_stream(in, fname);
216 lexer_next_preprocessing_token();
217 print_token(stdout, &lexer_token);
219 } while (lexer_token.type != T_EOF);
222 static void add_flag(struct obstack *obst, const char *format, ...)
227 va_start(ap, format);
231 vsnprintf(buf, sizeof(buf), format, ap);
234 obstack_1grow(obst, ' ');
236 obstack_1grow(obst, '"');
237 obstack_grow(obst, buf, len);
238 obstack_1grow(obst, '"');
240 /* escape stuff... */
241 for (char *c = buf; *c != '\0'; ++c) {
259 obstack_1grow(obst, '\\');
262 obstack_1grow(obst, *c);
269 static const char *type_to_string(type_t *type)
271 assert(type->kind == TYPE_ATOMIC);
272 return get_atomic_kind_name(type->atomic.akind);
275 static FILE *preprocess(const char *fname, filetype_t filetype)
277 static const char *common_flags = NULL;
279 if (common_flags == NULL) {
280 obstack_1grow(&cppflags_obst, '\0');
281 const char *flags = obstack_finish(&cppflags_obst);
283 /* setup default defines */
284 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
285 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
286 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
287 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
289 add_flag(&cppflags_obst, "-U__VERSION__");
290 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
292 if (flags[0] != '\0') {
293 size_t len = strlen(flags);
294 obstack_1grow(&cppflags_obst, ' ');
295 obstack_grow(&cppflags_obst, flags, len);
297 obstack_1grow(&cppflags_obst, '\0');
298 common_flags = obstack_finish(&cppflags_obst);
301 assert(obstack_object_size(&cppflags_obst) == 0);
303 const char *preprocessor = getenv("CPARSER_PP");
304 if (preprocessor != NULL) {
305 obstack_printf(&cppflags_obst, "%s ", preprocessor);
307 if (target_triple != NULL)
308 obstack_printf(&cppflags_obst, "%s-", target_triple);
309 obstack_printf(&cppflags_obst, PREPROCESSOR);
314 add_flag(&cppflags_obst, "-std=c99");
317 add_flag(&cppflags_obst, "-std=c++98");
319 case FILETYPE_ASSEMBLER:
320 add_flag(&cppflags_obst, "-x");
321 add_flag(&cppflags_obst, "assembler-with-cpp");
326 obstack_printf(&cppflags_obst, "%s", common_flags);
328 /* handle dependency generation */
329 if (dep_target[0] != '\0') {
330 add_flag(&cppflags_obst, "-MF");
331 add_flag(&cppflags_obst, dep_target);
332 if (outname != NULL) {
333 add_flag(&cppflags_obst, "-MQ");
334 add_flag(&cppflags_obst, outname);
337 add_flag(&cppflags_obst, fname);
338 obstack_1grow(&cppflags_obst, '\0');
340 char *commandline = obstack_finish(&cppflags_obst);
344 FILE *f = popen(commandline, "r");
346 fprintf(stderr, "invoking preprocessor failed\n");
349 /* we don't really need that anymore */
350 obstack_free(&cppflags_obst, commandline);
355 static void assemble(const char *out, const char *in)
357 obstack_1grow(&asflags_obst, '\0');
358 const char *flags = obstack_finish(&asflags_obst);
360 const char *assembler = getenv("CPARSER_AS");
361 if (assembler != NULL) {
362 obstack_printf(&asflags_obst, "%s", assembler);
364 if (target_triple != NULL)
365 obstack_printf(&asflags_obst, "%s-", target_triple);
366 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
368 if (flags[0] != '\0')
369 obstack_printf(&asflags_obst, " %s", flags);
371 obstack_printf(&asflags_obst, " %s -o %s", in, out);
372 obstack_1grow(&asflags_obst, '\0');
374 char *commandline = obstack_finish(&asflags_obst);
378 int err = system(commandline);
379 if (err != EXIT_SUCCESS) {
380 fprintf(stderr, "assembler reported an error\n");
383 obstack_free(&asflags_obst, commandline);
386 static void print_file_name(const char *file)
388 add_flag(&ldflags_obst, "-print-file-name=%s", file);
390 obstack_1grow(&ldflags_obst, '\0');
391 const char *flags = obstack_finish(&ldflags_obst);
393 /* construct commandline */
394 const char *linker = getenv("CPARSER_LINK");
395 if (linker != NULL) {
396 obstack_printf(&ldflags_obst, "%s ", linker);
398 if (target_triple != NULL)
399 obstack_printf(&ldflags_obst, "%s-", target_triple);
400 obstack_printf(&ldflags_obst, "%s ", LINKER);
402 obstack_printf(&ldflags_obst, "%s ", linker);
403 obstack_printf(&ldflags_obst, "%s", flags);
404 obstack_1grow(&ldflags_obst, '\0');
406 char *commandline = obstack_finish(&ldflags_obst);
410 int err = system(commandline);
411 if (err != EXIT_SUCCESS) {
412 fprintf(stderr, "linker reported an error\n");
415 obstack_free(&ldflags_obst, commandline);
418 static const char *try_dir(const char *dir)
422 if (access(dir, R_OK | W_OK | X_OK) == 0)
427 static const char *get_tempdir(void)
429 static const char *tmpdir = NULL;
435 tmpdir = try_dir(getenv("TMPDIR"));
437 tmpdir = try_dir(getenv("TMP"));
439 tmpdir = try_dir(getenv("TEMP"));
443 tmpdir = try_dir(P_tmpdir);
447 tmpdir = try_dir("/var/tmp");
449 tmpdir = try_dir("/usr/tmp");
451 tmpdir = try_dir("/tmp");
460 /* cheap and nasty mkstemp replacement */
461 static int mkstemp(char *templ)
464 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
469 * an own version of tmpnam, which: writes in a buffer, emits no warnings
470 * during linking (like glibc/gnu ld do for tmpnam)...
472 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
474 const char *tempdir = get_tempdir();
476 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
478 int fd = mkstemp(buffer);
480 fprintf(stderr, "couldn't create temporary file: %s\n",
484 FILE *out = fdopen(fd, "w");
486 fprintf(stderr, "couldn't create temporary file FILE*\n");
490 file_list_entry_t *entry = xmalloc(sizeof(*entry));
491 memset(entry, 0, sizeof(*entry));
493 size_t name_len = strlen(buffer) + 1;
494 char *name = malloc(name_len);
495 memcpy(name, buffer, name_len);
498 entry->next = temp_files;
504 static void free_temp_files(void)
506 file_list_entry_t *entry = temp_files;
507 file_list_entry_t *next;
508 for ( ; entry != NULL; entry = next) {
512 free((char*) entry->name);
518 typedef enum compile_mode_t {
533 static void usage(const char *argv0)
535 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
538 static void print_cparser_version(void)
540 printf("cparser (%s) using libFirm (%u.%u",
541 cparser_REVISION, ir_get_version_major(),
542 ir_get_version_minor());
544 const char *revision = ir_get_version_revision();
545 if (revision[0] != 0) {
547 fputs(revision, stdout);
550 const char *build = ir_get_version_build();
553 fputs(build, stdout);
556 puts("This is free software; see the source for copying conditions. There is NO\n"
557 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
560 static void print_help(const char *argv0)
564 puts("\t-fhelp Display help about firm optimisation options");
565 puts("\t-bhelp Display help about firm backend options");
566 puts("A big number of gcc flags is also supported");
569 static void set_be_option(const char *arg)
571 int res = be_parse_arg(arg);
576 static void set_option(const char *arg)
578 int res = firm_option(arg);
583 static void copy_file(FILE *dest, FILE *input)
587 while (!feof(input) && !ferror(dest)) {
588 size_t read = fread(buf, 1, sizeof(buf), input);
589 if (fwrite(buf, 1, read, dest) != read) {
590 perror("couldn't write output");
595 static inline bool streq(char const* a, char const* b)
597 return strcmp(a, b) == 0;
600 static inline bool strstart(char const* str, char const* start)
605 } while (*str++ == *start++);
609 static FILE *open_file(const char *filename)
611 if (streq(filename, "-")) {
615 FILE *in = fopen(filename, "r");
617 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
625 static filetype_t get_filetype_from_string(const char *string)
627 if (streq(string, "c") || streq(string, "c-header"))
629 if (streq(string, "c++") || streq(string, "c++-header"))
631 if (streq(string, "assembler"))
632 return FILETYPE_PREPROCESSED_ASSEMBLER;
633 if (streq(string, "assembler-with-cpp"))
634 return FILETYPE_ASSEMBLER;
635 if (streq(string, "none"))
636 return FILETYPE_AUTODETECT;
638 return FILETYPE_UNKNOWN;
642 * Initialize firm codegeneration for a specific operating system.
643 * The argument is the operating system part of a target-triple */
644 static bool set_os_support(const char *os)
646 wchar_atomic_kind = ATOMIC_TYPE_INT;
647 force_long_double_size = 0;
648 enable_main_collect2_hack = false;
650 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
651 || streq(os, "solaris")) {
652 set_be_option("ia32-gasmode=elf");
653 set_create_ld_ident(create_name_linux_elf);
654 } else if (streq(os, "darwin")) {
655 force_long_double_size = 16;
656 set_be_option("ia32-gasmode=macho");
657 set_be_option("ia32-stackalign=4");
658 set_be_option("pic=true");
659 set_create_ld_ident(create_name_macho);
660 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
661 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
662 enable_main_collect2_hack = true;
663 set_be_option("ia32-gasmode=mingw");
664 set_create_ld_ident(create_name_win32);
672 static bool parse_target_triple(const char *arg)
674 const char *manufacturer = strchr(arg, '-');
675 if (manufacturer == NULL) {
676 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
681 const char *os = strchr(manufacturer, '-');
683 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
688 /* Note: Triples are more or less defined by what the config.guess and
689 * config.sub scripts from GNU autoconf emit. We have to lookup there what
690 * triples are possible */
692 /* process cpu type */
693 if (strstart(arg, "i386-")) {
694 be_parse_arg("isa=ia32");
695 be_parse_arg("ia32-arch=i386");
696 } else if (strstart(arg, "i486-")) {
697 be_parse_arg("isa=ia32");
698 be_parse_arg("ia32-arch=i486");
699 } else if (strstart(arg, "i586-")) {
700 be_parse_arg("isa=ia32");
701 be_parse_arg("ia32-arch=i586");
702 } else if (strstart(arg, "i686-")) {
703 be_parse_arg("isa=ia32");
704 be_parse_arg("ia32-arch=i686");
705 } else if (strstart(arg, "i786-")) {
706 be_parse_arg("isa=ia32");
707 be_parse_arg("ia32-arch=pentium4");
708 } else if (strstart(arg, "x86_64")) {
709 be_parse_arg("isa=amd64");
710 } else if (strstart(arg, "sparc-")) {
711 be_parse_arg("isa=sparc");
712 } else if (strstart(arg, "arm-")) {
713 be_parse_arg("isa=arm");
715 fprintf(stderr, "Unknown cpu in triple '%s'\n", arg);
719 /* process manufacturer, alot of people incorrectly leave out the
720 * manufacturer instead of using unknown- */
721 if (strstart(manufacturer, "linux")) {
723 manufacturer = "unknown-";
726 /* process operating system */
727 if (!set_os_support(os)) {
728 fprintf(stderr, "Unknown operating system '%s' in triple '%s'\n", os, arg);
736 int main(int argc, char **argv)
740 const char *dumpfunction = NULL;
741 const char *print_file_name_file = NULL;
742 compile_mode_t mode = CompileAssembleLink;
744 int result = EXIT_SUCCESS;
745 char cpu_arch[16] = "ia32";
746 file_list_entry_t *files = NULL;
747 file_list_entry_t *last_file = NULL;
748 bool construct_dep_target = false;
749 bool do_timing = false;
750 struct obstack file_obst;
752 atexit(free_temp_files);
754 /* hack for now... */
755 if (strstr(argv[0], "pptest") != NULL) {
756 extern int pptest_main(int argc, char **argv);
757 return pptest_main(argc, argv);
760 obstack_init(&cppflags_obst);
761 obstack_init(&ldflags_obst);
762 obstack_init(&asflags_obst);
763 obstack_init(&file_obst);
765 #define GET_ARG_AFTER(def, args) \
766 def = &arg[sizeof(args)-1]; \
767 if (def[0] == '\0') { \
770 fprintf(stderr, "error: expected argument after '" args "'\n"); \
771 argument_errors = true; \
775 if (def[0] == '-' && def[1] != '\0') { \
776 fprintf(stderr, "error: expected argument after '" args "'\n"); \
777 argument_errors = true; \
782 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
784 /* early options parsing (find out optimisation level and OS) */
785 for (int i = 1; i < argc; ++i) {
786 const char *arg = argv[i];
790 const char *option = &arg[1];
791 if (option[0] == 'O') {
792 sscanf(&option[1], "%d", &opt_level);
797 #if defined(_WIN32) || defined(__CYGWIN__)
798 set_os_support("win32");
799 #elif defined(__APPLE__)
800 set_os_support("darwin");
802 set_os_support("linux");
805 /* apply optimisation level */
808 set_option("no-opt");
811 set_option("no-inline");
815 /* use_builtins = true; */
818 set_option("thread-jumps");
819 set_option("if-conversion");
822 set_option("strict-aliasing");
823 set_option("inline");
824 set_option("deconv");
825 set_be_option("omitfp");
829 const char *target = getenv("TARGET");
831 parse_target_triple(target);
833 /* parse rest of options */
834 standard = STANDARD_DEFAULT;
835 unsigned features_on = 0;
836 unsigned features_off = 0;
837 filetype_t forced_filetype = FILETYPE_AUTODETECT;
838 bool help_displayed = false;
839 bool argument_errors = false;
840 for (int i = 1; i < argc; ++i) {
841 const char *arg = argv[i];
842 if (arg[0] == '-' && arg[1] != '\0') {
844 const char *option = &arg[1];
845 if (option[0] == 'o') {
846 GET_ARG_AFTER(outname, "-o");
847 } else if (option[0] == 'g') {
848 set_be_option("debuginfo=stabs");
849 set_be_option("omitfp=no");
850 set_be_option("ia32-nooptcc=yes");
851 } else if (SINGLE_OPTION('c')) {
852 mode = CompileAssemble;
853 } else if (SINGLE_OPTION('E')) {
854 mode = PreprocessOnly;
855 } else if (SINGLE_OPTION('S')) {
857 } else if (option[0] == 'O') {
859 } else if (option[0] == 'I') {
861 GET_ARG_AFTER(opt, "-I");
862 add_flag(&cppflags_obst, "-I%s", opt);
863 } else if (option[0] == 'D') {
865 GET_ARG_AFTER(opt, "-D");
866 add_flag(&cppflags_obst, "-D%s", opt);
867 } else if (option[0] == 'U') {
869 GET_ARG_AFTER(opt, "-U");
870 add_flag(&cppflags_obst, "-U%s", opt);
871 } else if (option[0] == 'l') {
873 GET_ARG_AFTER(opt, "-l");
874 add_flag(&ldflags_obst, "-l%s", opt);
875 } else if (option[0] == 'L') {
877 GET_ARG_AFTER(opt, "-L");
878 add_flag(&ldflags_obst, "-L%s", opt);
879 } else if (SINGLE_OPTION('v')) {
881 } else if (SINGLE_OPTION('w')) {
882 memset(&warning, 0, sizeof(warning));
883 } else if (option[0] == 'x') {
885 GET_ARG_AFTER(opt, "-x");
886 forced_filetype = get_filetype_from_string(opt);
887 if (forced_filetype == FILETYPE_UNKNOWN) {
888 fprintf(stderr, "Unknown language '%s'\n", opt);
889 argument_errors = true;
891 } else if (streq(option, "M")) {
892 mode = PreprocessOnly;
893 add_flag(&cppflags_obst, "-M");
894 } else if (streq(option, "MMD") ||
895 streq(option, "MD")) {
896 construct_dep_target = true;
897 add_flag(&cppflags_obst, "-%s", option);
898 } else if (streq(option, "MM") ||
899 streq(option, "MP")) {
900 add_flag(&cppflags_obst, "-%s", option);
901 } else if (streq(option, "MT") ||
902 streq(option, "MQ") ||
903 streq(option, "MF")) {
905 GET_ARG_AFTER(opt, "-MT");
906 add_flag(&cppflags_obst, "-%s", option);
907 add_flag(&cppflags_obst, "%s", opt);
908 } else if (streq(option, "include")) {
910 GET_ARG_AFTER(opt, "-include");
911 add_flag(&cppflags_obst, "-include");
912 add_flag(&cppflags_obst, "%s", opt);
913 } else if (streq(option, "isystem")) {
915 GET_ARG_AFTER(opt, "-isystem");
916 add_flag(&cppflags_obst, "-isystem");
917 add_flag(&cppflags_obst, "%s", opt);
918 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
919 } else if (streq(option, "pthread")) {
920 /* set flags for the preprocessor */
921 add_flag(&cppflags_obst, "-D_REENTRANT");
922 /* set flags for the linker */
923 add_flag(&ldflags_obst, "-lpthread");
925 } else if (streq(option, "nostdinc")
926 || streq(option, "trigraphs")) {
927 /* pass these through to the preprocessor */
928 add_flag(&cppflags_obst, "%s", arg);
929 } else if (streq(option, "pipe")) {
930 /* here for gcc compatibility */
931 } else if (streq(option, "static")) {
932 add_flag(&ldflags_obst, "-static");
933 } else if (streq(option, "shared")) {
934 add_flag(&ldflags_obst, "-shared");
935 } else if (option[0] == 'f') {
936 char const *orig_opt;
937 GET_ARG_AFTER(orig_opt, "-f");
939 if (strstart(orig_opt, "input-charset=")) {
940 char const* const encoding = strchr(orig_opt, '=') + 1;
941 select_input_encoding(encoding);
942 } else if (streq(orig_opt, "verbose-asm")) {
943 /* ignore: we always print verbose assembler */
945 char const *opt = orig_opt;
946 bool truth_value = true;
947 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
952 if (streq(opt, "builtins")) {
953 use_builtins = truth_value;
954 } else if (streq(opt, "dollars-in-identifiers")) {
955 allow_dollar_in_symbol = truth_value;
956 } else if (streq(opt, "omit-frame-pointer")) {
957 set_be_option(truth_value ? "omitfp" : "omitfp=no");
958 } else if (streq(opt, "short-wchar")) {
959 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
961 } else if (streq(opt, "signed-char")) {
962 char_is_signed = truth_value;
963 } else if (streq(opt, "strength-reduce")) {
964 firm_option(truth_value ? "strength-red" : "no-strength-red");
965 } else if (streq(opt, "syntax-only")) {
966 mode = truth_value ? ParseOnly : CompileAssembleLink;
967 } else if (streq(opt, "unsigned-char")) {
968 char_is_signed = !truth_value;
969 } else if (streq(opt, "freestanding")) {
971 } else if (streq(opt, "hosted")) {
972 freestanding = false;
973 } else if (truth_value == false &&
974 streq(opt, "asynchronous-unwind-tables")) {
975 /* nothing todo, a gcc feature which we don't support
976 * anyway was deactivated */
977 } else if (strstart(orig_opt, "align-loops=") ||
978 strstart(orig_opt, "align-jumps=") ||
979 strstart(orig_opt, "align-functions=")) {
980 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
981 } else if (strstart(orig_opt, "message-length=")) {
982 /* ignore: would only affect error message format */
983 } else if (streq(opt, "fast-math") ||
984 streq(opt, "jump-tables") ||
985 streq(opt, "expensive-optimizations") ||
986 streq(opt, "common") ||
987 streq(opt, "optimize-sibling-calls") ||
988 streq(opt, "align-loops") ||
989 streq(opt, "align-jumps") ||
990 streq(opt, "align-functions") ||
992 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
994 int res = firm_option(orig_opt);
996 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
998 argument_errors = true;
1000 } else if (res == -1) {
1001 help_displayed = true;
1005 } else if (option[0] == 'b') {
1007 GET_ARG_AFTER(opt, "-b");
1008 int res = be_parse_arg(opt);
1010 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1012 argument_errors = true;
1013 } else if (res == -1) {
1014 help_displayed = true;
1015 } else if (strstart(opt, "isa=")) {
1016 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1018 } else if (option[0] == 'W') {
1019 if (option[1] == '\0') {
1020 /* ignore -W, our defaults are already quite verbose */
1021 } else if (strstart(option + 1, "p,")) {
1022 // pass options directly to the preprocessor
1024 GET_ARG_AFTER(opt, "-Wp,");
1025 add_flag(&cppflags_obst, "-Wp,%s", opt);
1026 } else if (strstart(option + 1, "l,")) {
1027 // pass options directly to the linker
1029 GET_ARG_AFTER(opt, "-Wl,");
1030 add_flag(&ldflags_obst, "-Wl,%s", opt);
1031 } else if (streq(option + 1, "no-trigraphs")
1032 || streq(option + 1, "undef")) {
1033 add_flag(&cppflags_obst, "%s", arg);
1035 set_warning_opt(&option[1]);
1037 } else if (option[0] == 'm') {
1042 GET_ARG_AFTER(opt, "-m");
1043 if (strstart(opt, "target=")) {
1044 GET_ARG_AFTER(opt, "-mtarget=");
1045 if (!parse_target_triple(opt))
1046 argument_errors = true;
1047 } else if (strstart(opt, "triple=")) {
1048 GET_ARG_AFTER(opt, "-mtriple=");
1049 if (!parse_target_triple(opt))
1050 argument_errors = true;
1051 } else if (strstart(opt, "arch=")) {
1052 GET_ARG_AFTER(opt, "-march=");
1053 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1054 int res = be_parse_arg(arch_opt);
1056 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1057 argument_errors = true;
1059 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1060 int res = be_parse_arg(arch_opt);
1062 argument_errors = true;
1064 } else if (strstart(opt, "tune=")) {
1065 GET_ARG_AFTER(opt, "-mtune=");
1066 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1067 int res = be_parse_arg(arch_opt);
1069 argument_errors = true;
1070 } else if (strstart(opt, "cpu=")) {
1071 GET_ARG_AFTER(opt, "-mcpu=");
1072 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1073 int res = be_parse_arg(arch_opt);
1075 argument_errors = true;
1076 } else if (strstart(opt, "fpmath=")) {
1077 GET_ARG_AFTER(opt, "-mfpmath=");
1078 if (streq(opt, "387"))
1080 else if (streq(opt, "sse"))
1083 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1084 argument_errors = true;
1086 if (!argument_errors) {
1087 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1088 int res = be_parse_arg(arch_opt);
1090 argument_errors = true;
1092 } else if (strstart(opt, "preferred-stack-boundary=")) {
1093 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1094 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1095 int res = be_parse_arg(arch_opt);
1097 argument_errors = true;
1098 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1099 set_be_option("omitleaffp=1");
1100 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1101 set_be_option("omitleaffp=0");
1102 } else if (streq(opt, "rtd")) {
1103 default_calling_convention = CC_STDCALL;
1104 } else if (strstart(opt, "regparm=")) {
1105 fprintf(stderr, "error: regparm convention not supported yet\n");
1106 argument_errors = true;
1107 } else if (streq(opt, "soft-float")) {
1108 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1109 argument_errors = true;
1111 long int value = strtol(opt, NULL, 10);
1113 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1114 argument_errors = true;
1115 } else if (value != 16 && value != 32 && value != 64) {
1116 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1117 argument_errors = true;
1119 machine_size = (unsigned int)value;
1120 add_flag(&asflags_obst, "--%u", machine_size);
1121 add_flag(&ldflags_obst, "-m%u", machine_size);
1124 } else if (streq(option, "pg")) {
1125 set_be_option("gprof");
1126 add_flag(&ldflags_obst, "-pg");
1127 } else if (streq(option, "pedantic") ||
1128 streq(option, "ansi")) {
1129 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1130 } else if (streq(option, "shared")) {
1131 add_flag(&ldflags_obst, "-shared");
1132 } else if (strstart(option, "std=")) {
1133 const char *const o = &option[4];
1135 streq(o, "c++") ? STANDARD_CXX98 :
1136 streq(o, "c++98") ? STANDARD_CXX98 :
1137 streq(o, "c89") ? STANDARD_C89 :
1138 streq(o, "c99") ? STANDARD_C99 :
1139 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1140 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1141 streq(o, "gnu89") ? STANDARD_GNU89 :
1142 streq(o, "gnu99") ? STANDARD_GNU99 :
1143 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1144 streq(o, "iso9899:1990") ? STANDARD_C89 :
1145 streq(o, "iso9899:199409") ? STANDARD_C90 :
1146 streq(o, "iso9899:1999") ? STANDARD_C99 :
1147 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1148 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1149 } else if (streq(option, "version")) {
1150 print_cparser_version();
1151 } else if (strstart(option, "print-file-name=")) {
1152 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1153 } else if (option[0] == '-') {
1154 /* double dash option */
1156 if (streq(option, "gcc")) {
1157 features_on |= _GNUC;
1158 features_off &= ~_GNUC;
1159 } else if (streq(option, "no-gcc")) {
1160 features_on &= ~_GNUC;
1161 features_off |= _GNUC;
1162 } else if (streq(option, "ms")) {
1164 features_off &= ~_MS;
1165 } else if (streq(option, "no-ms")) {
1166 features_on &= ~_MS;
1167 features_off |= _MS;
1168 } else if (streq(option, "strict")) {
1170 } else if (streq(option, "lextest")) {
1172 } else if (streq(option, "benchmark")) {
1173 mode = BenchmarkParser;
1174 } else if (streq(option, "print-ast")) {
1176 } else if (streq(option, "print-implicit-cast")) {
1177 print_implicit_casts = true;
1178 } else if (streq(option, "print-parenthesis")) {
1179 print_parenthesis = true;
1180 } else if (streq(option, "print-fluffy")) {
1182 } else if (streq(option, "print-jna")) {
1184 } else if (streq(option, "jna-limit")) {
1187 fprintf(stderr, "error: "
1188 "expected argument after '--jna-limit'\n");
1189 argument_errors = true;
1192 jna_limit_output(argv[i]);
1193 } else if (streq(option, "jna-libname")) {
1196 fprintf(stderr, "error: "
1197 "expected argument after '--jna-libname'\n");
1198 argument_errors = true;
1201 jna_set_libname(argv[i]);
1202 } else if (streq(option, "time")) {
1204 } else if (streq(option, "version")) {
1205 print_cparser_version();
1206 return EXIT_SUCCESS;
1207 } else if (streq(option, "help")) {
1208 print_help(argv[0]);
1209 help_displayed = true;
1210 } else if (streq(option, "dump-function")) {
1213 fprintf(stderr, "error: "
1214 "expected argument after '--dump-function'\n");
1215 argument_errors = true;
1218 dumpfunction = argv[i];
1220 } else if (streq(option, "export-ir")) {
1221 mode = CompileExportIR;
1223 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1224 argument_errors = true;
1227 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1228 argument_errors = true;
1231 filetype_t type = forced_filetype;
1232 if (type == FILETYPE_AUTODETECT) {
1233 if (streq(arg, "-")) {
1234 /* - implicitly means C source file */
1237 const char *suffix = strrchr(arg, '.');
1238 /* Ensure there is at least one char before the suffix */
1239 if (suffix != NULL && suffix != arg) {
1242 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1243 streq(suffix, "a") ? FILETYPE_OBJECT :
1244 streq(suffix, "c") ? FILETYPE_C :
1245 streq(suffix, "C") ? FILETYPE_CXX :
1246 streq(suffix, "cc") ? FILETYPE_CXX :
1247 streq(suffix, "cp") ? FILETYPE_CXX :
1248 streq(suffix, "cpp") ? FILETYPE_CXX :
1249 streq(suffix, "CPP") ? FILETYPE_CXX :
1250 streq(suffix, "cxx") ? FILETYPE_CXX :
1251 streq(suffix, "c++") ? FILETYPE_CXX :
1252 streq(suffix, "ii") ? FILETYPE_CXX :
1253 streq(suffix, "h") ? FILETYPE_C :
1254 streq(suffix, "ir") ? FILETYPE_IR :
1255 streq(suffix, "o") ? FILETYPE_OBJECT :
1256 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1257 streq(suffix, "so") ? FILETYPE_OBJECT :
1258 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1263 file_list_entry_t *entry
1264 = obstack_alloc(&file_obst, sizeof(entry[0]));
1265 memset(entry, 0, sizeof(entry[0]));
1269 if (last_file != NULL) {
1270 last_file->next = entry;
1278 if (help_displayed) {
1279 return !argument_errors;
1282 if (print_file_name_file != NULL) {
1283 print_file_name(print_file_name_file);
1284 return EXIT_SUCCESS;
1286 if (files == NULL) {
1287 fprintf(stderr, "error: no input files specified\n");
1288 argument_errors = true;
1291 if (argument_errors) {
1293 return EXIT_FAILURE;
1296 /* set the c_mode here, types depends on it */
1297 c_mode |= features_on;
1298 c_mode &= ~features_off;
1301 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1302 init_symbol_table();
1315 if (construct_dep_target) {
1316 if (outname != 0 && strlen(outname) >= 2) {
1317 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1319 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1322 dep_target[0] = '\0';
1325 char outnamebuf[4096];
1326 if (outname == NULL) {
1327 const char *filename = files->name;
1330 case BenchmarkParser:
1335 case PreprocessOnly:
1340 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1341 outname = outnamebuf;
1343 case CompileAssemble:
1344 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1345 outname = outnamebuf;
1348 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1350 outname = outnamebuf;
1352 case CompileExportIR:
1353 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1354 outname = outnamebuf;
1356 case CompileAssembleLink:
1366 assert(outname != NULL);
1369 if (streq(outname, "-")) {
1372 out = fopen(outname, "w");
1374 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1376 return EXIT_FAILURE;
1380 file_list_entry_t *file;
1381 bool already_constructed_firm = false;
1382 for (file = files; file != NULL; file = file->next) {
1383 char asm_tempfile[1024];
1384 const char *filename = file->name;
1385 filetype_t filetype = file->type;
1387 if (filetype == FILETYPE_OBJECT)
1391 if (mode == LexTest) {
1393 in = open_file(filename);
1394 lextest(in, filename);
1396 return EXIT_SUCCESS;
1399 FILE *preprocessed_in = NULL;
1400 filetype_t next_filetype = filetype;
1403 next_filetype = FILETYPE_PREPROCESSED_C;
1406 next_filetype = FILETYPE_PREPROCESSED_CXX;
1408 case FILETYPE_ASSEMBLER:
1409 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1412 /* no support for input on FILE* yet */
1414 panic("internal compiler error: in for preprocessor != NULL");
1416 preprocessed_in = preprocess(filename, filetype);
1417 if (mode == PreprocessOnly) {
1418 copy_file(out, preprocessed_in);
1419 int result = pclose(preprocessed_in);
1421 /* remove output file in case of error */
1422 if (out != stdout && result != EXIT_SUCCESS) {
1428 in = preprocessed_in;
1429 filetype = next_filetype;
1437 if (mode == Compile) {
1440 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1444 in = open_file(filename);
1446 /* preprocess and compile */
1447 if (filetype == FILETYPE_PREPROCESSED_C) {
1448 char const* invalid_mode;
1451 case STANDARD_C89: c_mode = _C89; break;
1452 /* TODO determine difference between these two */
1453 case STANDARD_C90: c_mode = _C89; break;
1454 case STANDARD_C99: c_mode = _C89 | _C99; break;
1455 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1459 "warning: command line option \"-std=%s\" is not valid for C\n",
1462 case STANDARD_DEFAULT:
1463 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1465 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1466 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1469 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1470 char const* invalid_mode;
1472 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1473 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1474 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1475 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1476 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1479 case STANDARD_CXX98: c_mode = _CXX; break;
1483 "warning: command line option \"-std=%s\" is not valid for C++\n",
1485 case STANDARD_DEFAULT:
1486 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1490 c_mode |= features_on;
1491 c_mode &= ~features_off;
1493 /* do the actual parsing */
1494 ir_timer_t *t_parsing = ir_timer_new();
1495 timer_register(t_parsing, "Frontend: Parsing");
1496 timer_push(t_parsing);
1498 translation_unit_t *const unit = do_parsing(in, filename);
1499 timer_pop(t_parsing);
1501 /* prints the AST even if errors occurred */
1502 if (mode == PrintAst) {
1507 if (error_count > 0) {
1508 /* parsing failed because of errors */
1509 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1511 result = EXIT_FAILURE;
1513 } else if (warning_count > 0) {
1514 fprintf(stderr, "%u warning(s)\n", warning_count);
1517 if (in == preprocessed_in) {
1518 int pp_result = pclose(preprocessed_in);
1519 if (pp_result != EXIT_SUCCESS) {
1520 /* remove output file */
1523 return EXIT_FAILURE;
1527 if (mode == BenchmarkParser) {
1529 } else if (mode == PrintFluffy) {
1530 write_fluffy_decls(out, unit);
1532 } else if (mode == PrintJna) {
1533 write_jna_decls(out, unit);
1537 /* build the firm graph */
1538 ir_timer_t *t_construct = ir_timer_new();
1539 timer_register(t_construct, "Frontend: Graph construction");
1540 timer_push(t_construct);
1541 if (already_constructed_firm) {
1542 panic("compiling multiple files/translation units not possible");
1544 translation_unit_to_firm(unit);
1545 already_constructed_firm = true;
1546 timer_pop(t_construct);
1549 if (mode == ParseOnly) {
1553 if (mode == CompileDump) {
1555 ident *id = new_id_from_str(dumpfunction);
1556 ir_graph *irg = NULL;
1557 int n_irgs = get_irp_n_irgs();
1558 for (int i = 0; i < n_irgs; ++i) {
1559 ir_graph *tirg = get_irp_irg(i);
1560 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1568 fprintf(stderr, "No graph for function '%s' found\n",
1570 return EXIT_FAILURE;
1573 dump_ir_graph_file(out, irg);
1575 return EXIT_SUCCESS;
1578 if (mode == CompileExportIR) {
1581 return EXIT_SUCCESS;
1584 gen_firm_finish(asm_out, filename);
1585 if (asm_out != out) {
1588 } else if (filetype == FILETYPE_IR) {
1590 ir_import(filename);
1592 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1593 copy_file(asm_out, in);
1594 if (in == preprocessed_in) {
1595 int pp_result = pclose(preprocessed_in);
1596 if (pp_result != EXIT_SUCCESS) {
1597 /* remove output in error case */
1603 if (asm_out != out) {
1608 if (mode == Compile)
1611 /* if we're here then we have preprocessed assembly */
1612 filename = asm_tempfile;
1613 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1616 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1618 const char *filename_o;
1619 if (mode == CompileAssemble) {
1621 filename_o = outname;
1623 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1628 assemble(filename_o, filename);
1630 size_t len = strlen(filename_o) + 1;
1631 filename = obstack_copy(&file_obst, filename_o, len);
1632 filetype = FILETYPE_OBJECT;
1635 /* ok we're done here, process next file */
1636 file->name = filename;
1637 file->type = filetype;
1640 if (result != EXIT_SUCCESS) {
1646 /* link program file */
1647 if (mode == CompileAssembleLink) {
1648 obstack_1grow(&ldflags_obst, '\0');
1649 const char *flags = obstack_finish(&ldflags_obst);
1651 /* construct commandline */
1652 const char *linker = getenv("CPARSER_LINK");
1653 if (linker != NULL) {
1654 obstack_printf(&file_obst, "%s ", linker);
1656 if (target_triple != NULL)
1657 obstack_printf(&file_obst, "%s-", target_triple);
1658 obstack_printf(&file_obst, "%s ", LINKER);
1661 for (file_list_entry_t *entry = files; entry != NULL;
1662 entry = entry->next) {
1663 if (entry->type != FILETYPE_OBJECT)
1666 add_flag(&file_obst, "%s", entry->name);
1669 add_flag(&file_obst, "-o");
1670 add_flag(&file_obst, outname);
1671 obstack_printf(&file_obst, "%s", flags);
1672 obstack_1grow(&file_obst, '\0');
1674 char *commandline = obstack_finish(&file_obst);
1679 int err = system(commandline);
1680 if (err != EXIT_SUCCESS) {
1681 fprintf(stderr, "linker reported an error\n");
1682 return EXIT_FAILURE;
1689 obstack_free(&cppflags_obst, NULL);
1690 obstack_free(&ldflags_obst, NULL);
1691 obstack_free(&asflags_obst, NULL);
1692 obstack_free(&file_obst, NULL);
1702 exit_symbol_table();
1703 return EXIT_SUCCESS;