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 bool have_const_functions = false;
110 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
111 unsigned force_long_double_size = 0;
112 bool enable_main_collect2_hack = false;
113 bool freestanding = false;
115 /* to switch on printing of implicit casts */
116 extern bool print_implicit_casts;
118 /* to switch on printing of parenthesis to indicate operator precedence */
119 extern bool print_parenthesis;
121 static const char *target_triple;
123 static struct obstack cppflags_obst;
124 static struct obstack ldflags_obst;
125 static struct obstack asflags_obst;
126 static char dep_target[1024];
127 static const char *outname;
129 typedef enum lang_standard_t {
130 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
131 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
132 STANDARD_C89, /* ISO C90 (sic) */
133 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
134 STANDARD_C99, /* ISO C99 */
135 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
136 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
137 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
138 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
141 static lang_standard_t standard;
143 typedef struct file_list_entry_t file_list_entry_t;
145 typedef enum filetype_t {
148 FILETYPE_PREPROCESSED_C,
150 FILETYPE_PREPROCESSED_CXX,
152 FILETYPE_PREPROCESSED_ASSEMBLER,
158 struct file_list_entry_t {
159 const char *name; /**< filename or NULL for stdin */
161 file_list_entry_t *next;
164 static file_list_entry_t *temp_files;
166 static void initialize_firm(void)
171 static void get_output_name(char *buf, size_t buflen, const char *inputname,
174 if (inputname == NULL)
177 char const *const last_slash = strrchr(inputname, '/');
178 char const *const filename =
179 last_slash != NULL ? last_slash + 1 : inputname;
180 char const *const last_dot = strrchr(filename, '.');
181 char const *const name_end =
182 last_dot != NULL ? last_dot : strchr(filename, '\0');
184 int const len = snprintf(buf, buflen, "%.*s%s",
185 (int)(name_end - filename), filename, newext);
187 if (len < 0 || buflen <= (size_t)len)
189 if (buflen <= (size_t)len)
191 panic("filename too long");
194 #include "gen_builtins.h"
196 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
201 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
205 lexer_open_stream(in, input_name);
208 translation_unit_t *unit = finish_parsing();
212 static void lextest(FILE *in, const char *fname)
214 lexer_open_stream(in, fname);
217 lexer_next_preprocessing_token();
218 print_token(stdout, &lexer_token);
220 } while (lexer_token.type != T_EOF);
223 static void add_flag(struct obstack *obst, const char *format, ...)
228 va_start(ap, format);
232 vsnprintf(buf, sizeof(buf), format, ap);
235 obstack_1grow(obst, ' ');
237 obstack_1grow(obst, '"');
238 obstack_grow(obst, buf, len);
239 obstack_1grow(obst, '"');
241 /* escape stuff... */
242 for (char *c = buf; *c != '\0'; ++c) {
260 obstack_1grow(obst, '\\');
263 obstack_1grow(obst, *c);
270 static const char *type_to_string(type_t *type)
272 assert(type->kind == TYPE_ATOMIC);
273 return get_atomic_kind_name(type->atomic.akind);
276 static FILE *preprocess(const char *fname, filetype_t filetype)
278 static const char *common_flags = NULL;
280 if (common_flags == NULL) {
281 obstack_1grow(&cppflags_obst, '\0');
282 const char *flags = obstack_finish(&cppflags_obst);
284 /* setup default defines */
285 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
286 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
287 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
288 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
290 add_flag(&cppflags_obst, "-U__VERSION__");
291 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
293 if (flags[0] != '\0') {
294 size_t len = strlen(flags);
295 obstack_1grow(&cppflags_obst, ' ');
296 obstack_grow(&cppflags_obst, flags, len);
298 obstack_1grow(&cppflags_obst, '\0');
299 common_flags = obstack_finish(&cppflags_obst);
302 assert(obstack_object_size(&cppflags_obst) == 0);
304 const char *preprocessor = getenv("CPARSER_PP");
305 if (preprocessor != NULL) {
306 obstack_printf(&cppflags_obst, "%s ", preprocessor);
308 if (target_triple != NULL)
309 obstack_printf(&cppflags_obst, "%s-", target_triple);
310 obstack_printf(&cppflags_obst, PREPROCESSOR);
315 add_flag(&cppflags_obst, "-std=c99");
318 add_flag(&cppflags_obst, "-std=c++98");
320 case FILETYPE_ASSEMBLER:
321 add_flag(&cppflags_obst, "-x");
322 add_flag(&cppflags_obst, "assembler-with-cpp");
327 obstack_printf(&cppflags_obst, "%s", common_flags);
329 /* handle dependency generation */
330 if (dep_target[0] != '\0') {
331 add_flag(&cppflags_obst, "-MF");
332 add_flag(&cppflags_obst, dep_target);
333 if (outname != NULL) {
334 add_flag(&cppflags_obst, "-MQ");
335 add_flag(&cppflags_obst, outname);
338 add_flag(&cppflags_obst, fname);
339 obstack_1grow(&cppflags_obst, '\0');
341 char *commandline = obstack_finish(&cppflags_obst);
345 FILE *f = popen(commandline, "r");
347 fprintf(stderr, "invoking preprocessor failed\n");
350 /* we don't really need that anymore */
351 obstack_free(&cppflags_obst, commandline);
356 static void assemble(const char *out, const char *in)
358 obstack_1grow(&asflags_obst, '\0');
359 const char *flags = obstack_finish(&asflags_obst);
361 const char *assembler = getenv("CPARSER_AS");
362 if (assembler != NULL) {
363 obstack_printf(&asflags_obst, "%s", assembler);
365 if (target_triple != NULL)
366 obstack_printf(&asflags_obst, "%s-", target_triple);
367 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
369 if (flags[0] != '\0')
370 obstack_printf(&asflags_obst, " %s", flags);
372 obstack_printf(&asflags_obst, " %s -o %s", in, out);
373 obstack_1grow(&asflags_obst, '\0');
375 char *commandline = obstack_finish(&asflags_obst);
379 int err = system(commandline);
380 if (err != EXIT_SUCCESS) {
381 fprintf(stderr, "assembler reported an error\n");
384 obstack_free(&asflags_obst, commandline);
387 static void print_file_name(const char *file)
389 add_flag(&ldflags_obst, "-print-file-name=%s", file);
391 obstack_1grow(&ldflags_obst, '\0');
392 const char *flags = obstack_finish(&ldflags_obst);
394 /* construct commandline */
395 const char *linker = getenv("CPARSER_LINK");
396 if (linker != NULL) {
397 obstack_printf(&ldflags_obst, "%s ", linker);
399 if (target_triple != NULL)
400 obstack_printf(&ldflags_obst, "%s-", target_triple);
401 obstack_printf(&ldflags_obst, "%s ", LINKER);
403 obstack_printf(&ldflags_obst, "%s ", linker);
404 obstack_printf(&ldflags_obst, "%s", flags);
405 obstack_1grow(&ldflags_obst, '\0');
407 char *commandline = obstack_finish(&ldflags_obst);
411 int err = system(commandline);
412 if (err != EXIT_SUCCESS) {
413 fprintf(stderr, "linker reported an error\n");
416 obstack_free(&ldflags_obst, commandline);
419 static const char *try_dir(const char *dir)
423 if (access(dir, R_OK | W_OK | X_OK) == 0)
428 static const char *get_tempdir(void)
430 static const char *tmpdir = NULL;
436 tmpdir = try_dir(getenv("TMPDIR"));
438 tmpdir = try_dir(getenv("TMP"));
440 tmpdir = try_dir(getenv("TEMP"));
444 tmpdir = try_dir(P_tmpdir);
448 tmpdir = try_dir("/var/tmp");
450 tmpdir = try_dir("/usr/tmp");
452 tmpdir = try_dir("/tmp");
461 /* cheap and nasty mkstemp replacement */
462 static int mkstemp(char *templ)
465 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
470 * an own version of tmpnam, which: writes in a buffer, emits no warnings
471 * during linking (like glibc/gnu ld do for tmpnam)...
473 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
475 const char *tempdir = get_tempdir();
477 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
479 int fd = mkstemp(buffer);
481 fprintf(stderr, "couldn't create temporary file: %s\n",
485 FILE *out = fdopen(fd, "w");
487 fprintf(stderr, "couldn't create temporary file FILE*\n");
491 file_list_entry_t *entry = xmalloc(sizeof(*entry));
492 memset(entry, 0, sizeof(*entry));
494 size_t name_len = strlen(buffer) + 1;
495 char *name = malloc(name_len);
496 memcpy(name, buffer, name_len);
499 entry->next = temp_files;
505 static void free_temp_files(void)
507 file_list_entry_t *entry = temp_files;
508 file_list_entry_t *next;
509 for ( ; entry != NULL; entry = next) {
513 free((char*) entry->name);
519 typedef enum compile_mode_t {
534 static void usage(const char *argv0)
536 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
539 static void print_cparser_version(void)
541 printf("cparser (%s) using libFirm (%u.%u",
542 cparser_REVISION, ir_get_version_major(),
543 ir_get_version_minor());
545 const char *revision = ir_get_version_revision();
546 if (revision[0] != 0) {
548 fputs(revision, stdout);
551 const char *build = ir_get_version_build();
554 fputs(build, stdout);
557 puts("This is free software; see the source for copying conditions. There is NO\n"
558 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
561 static void print_help(const char *argv0)
565 puts("\t-fhelp Display help about firm optimisation options");
566 puts("\t-bhelp Display help about firm backend options");
567 puts("A big number of gcc flags is also supported");
570 static void set_be_option(const char *arg)
572 int res = be_parse_arg(arg);
577 static void set_option(const char *arg)
579 int res = firm_option(arg);
584 static void copy_file(FILE *dest, FILE *input)
588 while (!feof(input) && !ferror(dest)) {
589 size_t read = fread(buf, 1, sizeof(buf), input);
590 if (fwrite(buf, 1, read, dest) != read) {
591 perror("couldn't write output");
596 static inline bool streq(char const* a, char const* b)
598 return strcmp(a, b) == 0;
601 static inline bool strstart(char const* str, char const* start)
606 } while (*str++ == *start++);
610 static FILE *open_file(const char *filename)
612 if (streq(filename, "-")) {
616 FILE *in = fopen(filename, "r");
618 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
626 static filetype_t get_filetype_from_string(const char *string)
628 if (streq(string, "c") || streq(string, "c-header"))
630 if (streq(string, "c++") || streq(string, "c++-header"))
632 if (streq(string, "assembler"))
633 return FILETYPE_PREPROCESSED_ASSEMBLER;
634 if (streq(string, "assembler-with-cpp"))
635 return FILETYPE_ASSEMBLER;
636 if (streq(string, "none"))
637 return FILETYPE_AUTODETECT;
639 return FILETYPE_UNKNOWN;
643 * Initialize firm codegeneration for a specific operating system.
644 * The argument is the operating system part of a target-triple */
645 static bool set_os_support(const char *os)
647 wchar_atomic_kind = ATOMIC_TYPE_INT;
648 force_long_double_size = 0;
649 enable_main_collect2_hack = false;
651 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
652 || streq(os, "solaris")) {
653 set_be_option("ia32-gasmode=elf");
654 set_create_ld_ident(create_name_linux_elf);
655 } else if (streq(os, "darwin")) {
656 force_long_double_size = 16;
657 set_be_option("ia32-gasmode=macho");
658 set_be_option("ia32-stackalign=4");
659 set_be_option("pic=true");
660 set_create_ld_ident(create_name_macho);
661 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
662 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
663 enable_main_collect2_hack = true;
664 set_be_option("ia32-gasmode=mingw");
665 set_create_ld_ident(create_name_win32);
673 static bool parse_target_triple(const char *arg)
675 const char *manufacturer = strchr(arg, '-');
676 if (manufacturer == NULL) {
677 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
682 const char *os = strchr(manufacturer, '-');
684 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
689 /* Note: Triples are more or less defined by what the config.guess and
690 * config.sub scripts from GNU autoconf emit. We have to lookup there what
691 * triples are possible */
693 /* process cpu type */
694 if (strstart(arg, "i386-")) {
695 be_parse_arg("isa=ia32");
696 be_parse_arg("ia32-arch=i386");
697 } else if (strstart(arg, "i486-")) {
698 be_parse_arg("isa=ia32");
699 be_parse_arg("ia32-arch=i486");
700 } else if (strstart(arg, "i586-")) {
701 be_parse_arg("isa=ia32");
702 be_parse_arg("ia32-arch=i586");
703 } else if (strstart(arg, "i686-")) {
704 be_parse_arg("isa=ia32");
705 be_parse_arg("ia32-arch=i686");
706 } else if (strstart(arg, "i786-")) {
707 be_parse_arg("isa=ia32");
708 be_parse_arg("ia32-arch=pentium4");
709 } else if (strstart(arg, "x86_64")) {
710 be_parse_arg("isa=amd64");
711 } else if (strstart(arg, "sparc-")) {
712 be_parse_arg("isa=sparc");
713 } else if (strstart(arg, "arm-")) {
714 be_parse_arg("isa=arm");
716 fprintf(stderr, "Unknown cpu in triple '%s'\n", arg);
720 /* process manufacturer, alot of people incorrectly leave out the
721 * manufacturer instead of using unknown- */
722 if (strstart(manufacturer, "linux")) {
724 manufacturer = "unknown-";
727 /* process operating system */
728 if (!set_os_support(os)) {
729 fprintf(stderr, "Unknown operating system '%s' in triple '%s'\n", os, arg);
737 int main(int argc, char **argv)
741 const char *dumpfunction = NULL;
742 const char *print_file_name_file = NULL;
743 compile_mode_t mode = CompileAssembleLink;
745 int result = EXIT_SUCCESS;
746 char cpu_arch[16] = "ia32";
747 file_list_entry_t *files = NULL;
748 file_list_entry_t *last_file = NULL;
749 bool construct_dep_target = false;
750 bool do_timing = false;
751 struct obstack file_obst;
753 atexit(free_temp_files);
755 /* hack for now... */
756 if (strstr(argv[0], "pptest") != NULL) {
757 extern int pptest_main(int argc, char **argv);
758 return pptest_main(argc, argv);
761 obstack_init(&cppflags_obst);
762 obstack_init(&ldflags_obst);
763 obstack_init(&asflags_obst);
764 obstack_init(&file_obst);
766 #define GET_ARG_AFTER(def, args) \
767 def = &arg[sizeof(args)-1]; \
768 if (def[0] == '\0') { \
771 fprintf(stderr, "error: expected argument after '" args "'\n"); \
772 argument_errors = true; \
776 if (def[0] == '-' && def[1] != '\0') { \
777 fprintf(stderr, "error: expected argument after '" args "'\n"); \
778 argument_errors = true; \
783 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
785 /* early options parsing (find out optimisation level and OS) */
786 for (int i = 1; i < argc; ++i) {
787 const char *arg = argv[i];
791 const char *option = &arg[1];
792 if (option[0] == 'O') {
793 sscanf(&option[1], "%d", &opt_level);
798 #if defined(_WIN32) || defined(__CYGWIN__)
799 set_os_support("win32");
800 #elif defined(__APPLE__)
801 set_os_support("darwin");
803 set_os_support("linux");
806 /* apply optimisation level */
809 set_option("no-opt");
812 set_option("no-inline");
816 /* use_builtins = true; */
819 set_option("thread-jumps");
820 set_option("if-conversion");
823 set_option("strict-aliasing");
824 set_option("inline");
825 set_option("deconv");
826 set_be_option("omitfp");
830 const char *target = getenv("TARGET");
832 parse_target_triple(target);
834 /* parse rest of options */
835 standard = STANDARD_DEFAULT;
836 unsigned features_on = 0;
837 unsigned features_off = 0;
838 filetype_t forced_filetype = FILETYPE_AUTODETECT;
839 bool help_displayed = false;
840 bool argument_errors = false;
841 for (int i = 1; i < argc; ++i) {
842 const char *arg = argv[i];
843 if (arg[0] == '-' && arg[1] != '\0') {
845 const char *option = &arg[1];
846 if (option[0] == 'o') {
847 GET_ARG_AFTER(outname, "-o");
848 } else if (option[0] == 'g') {
849 set_be_option("debuginfo=stabs");
850 set_be_option("omitfp=no");
851 set_be_option("ia32-nooptcc=yes");
852 } else if (SINGLE_OPTION('c')) {
853 mode = CompileAssemble;
854 } else if (SINGLE_OPTION('E')) {
855 mode = PreprocessOnly;
856 } else if (SINGLE_OPTION('S')) {
858 } else if (option[0] == 'O') {
860 } else if (option[0] == 'I') {
862 GET_ARG_AFTER(opt, "-I");
863 add_flag(&cppflags_obst, "-I%s", opt);
864 } else if (option[0] == 'D') {
866 GET_ARG_AFTER(opt, "-D");
867 add_flag(&cppflags_obst, "-D%s", opt);
868 } else if (option[0] == 'U') {
870 GET_ARG_AFTER(opt, "-U");
871 add_flag(&cppflags_obst, "-U%s", opt);
872 } else if (option[0] == 'l') {
874 GET_ARG_AFTER(opt, "-l");
875 add_flag(&ldflags_obst, "-l%s", opt);
876 } else if (option[0] == 'L') {
878 GET_ARG_AFTER(opt, "-L");
879 add_flag(&ldflags_obst, "-L%s", opt);
880 } else if (SINGLE_OPTION('v')) {
882 } else if (SINGLE_OPTION('w')) {
883 memset(&warning, 0, sizeof(warning));
884 } else if (option[0] == 'x') {
886 GET_ARG_AFTER(opt, "-x");
887 forced_filetype = get_filetype_from_string(opt);
888 if (forced_filetype == FILETYPE_UNKNOWN) {
889 fprintf(stderr, "Unknown language '%s'\n", opt);
890 argument_errors = true;
892 } else if (streq(option, "M")) {
893 mode = PreprocessOnly;
894 add_flag(&cppflags_obst, "-M");
895 } else if (streq(option, "MMD") ||
896 streq(option, "MD")) {
897 construct_dep_target = true;
898 add_flag(&cppflags_obst, "-%s", option);
899 } else if (streq(option, "MM") ||
900 streq(option, "MP")) {
901 add_flag(&cppflags_obst, "-%s", option);
902 } else if (streq(option, "MT") ||
903 streq(option, "MQ") ||
904 streq(option, "MF")) {
906 GET_ARG_AFTER(opt, "-MT");
907 add_flag(&cppflags_obst, "-%s", option);
908 add_flag(&cppflags_obst, "%s", opt);
909 } else if (streq(option, "include")) {
911 GET_ARG_AFTER(opt, "-include");
912 add_flag(&cppflags_obst, "-include");
913 add_flag(&cppflags_obst, "%s", opt);
914 } else if (streq(option, "isystem")) {
916 GET_ARG_AFTER(opt, "-isystem");
917 add_flag(&cppflags_obst, "-isystem");
918 add_flag(&cppflags_obst, "%s", opt);
919 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
920 } else if (streq(option, "pthread")) {
921 /* set flags for the preprocessor */
922 add_flag(&cppflags_obst, "-D_REENTRANT");
923 /* set flags for the linker */
924 add_flag(&ldflags_obst, "-lpthread");
926 } else if (streq(option, "nostdinc")
927 || streq(option, "trigraphs")) {
928 /* pass these through to the preprocessor */
929 add_flag(&cppflags_obst, "%s", arg);
930 } else if (streq(option, "pipe")) {
931 /* here for gcc compatibility */
932 } else if (streq(option, "static")) {
933 add_flag(&ldflags_obst, "-static");
934 } else if (streq(option, "shared")) {
935 add_flag(&ldflags_obst, "-shared");
936 } else if (option[0] == 'f') {
937 char const *orig_opt;
938 GET_ARG_AFTER(orig_opt, "-f");
940 if (strstart(orig_opt, "input-charset=")) {
941 char const* const encoding = strchr(orig_opt, '=') + 1;
942 select_input_encoding(encoding);
943 } else if (streq(orig_opt, "verbose-asm")) {
944 /* ignore: we always print verbose assembler */
946 char const *opt = orig_opt;
947 bool truth_value = true;
948 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
953 if (streq(opt, "builtins")) {
954 use_builtins = truth_value;
955 } else if (streq(opt, "dollars-in-identifiers")) {
956 allow_dollar_in_symbol = truth_value;
957 } else if (streq(opt, "omit-frame-pointer")) {
958 set_be_option(truth_value ? "omitfp" : "omitfp=no");
959 } else if (streq(opt, "short-wchar")) {
960 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
962 } else if (streq(opt, "signed-char")) {
963 char_is_signed = truth_value;
964 } else if (streq(opt, "strength-reduce")) {
965 firm_option(truth_value ? "strength-red" : "no-strength-red");
966 } else if (streq(opt, "syntax-only")) {
967 mode = truth_value ? ParseOnly : CompileAssembleLink;
968 } else if (streq(opt, "unsigned-char")) {
969 char_is_signed = !truth_value;
970 } else if (streq(opt, "freestanding")) {
972 } else if (streq(opt, "hosted")) {
973 freestanding = false;
974 } else if (truth_value == false &&
975 streq(opt, "asynchronous-unwind-tables")) {
976 /* nothing todo, a gcc feature which we don't support
977 * anyway was deactivated */
978 } else if (strstart(orig_opt, "align-loops=") ||
979 strstart(orig_opt, "align-jumps=") ||
980 strstart(orig_opt, "align-functions=")) {
981 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
982 } else if (strstart(orig_opt, "message-length=")) {
983 /* ignore: would only affect error message format */
984 } else if (streq(opt, "fast-math") ||
985 streq(opt, "jump-tables") ||
986 streq(opt, "expensive-optimizations") ||
987 streq(opt, "common") ||
988 streq(opt, "optimize-sibling-calls") ||
989 streq(opt, "align-loops") ||
990 streq(opt, "align-jumps") ||
991 streq(opt, "align-functions") ||
993 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
995 int res = firm_option(orig_opt);
997 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
999 argument_errors = true;
1001 } else if (res == -1) {
1002 help_displayed = true;
1006 } else if (option[0] == 'b') {
1008 GET_ARG_AFTER(opt, "-b");
1009 int res = be_parse_arg(opt);
1011 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1013 argument_errors = true;
1014 } else if (res == -1) {
1015 help_displayed = true;
1016 } else if (strstart(opt, "isa=")) {
1017 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1019 } else if (option[0] == 'W') {
1020 if (option[1] == '\0') {
1021 /* ignore -W, our defaults are already quite verbose */
1022 } else if (strstart(option + 1, "p,")) {
1023 // pass options directly to the preprocessor
1025 GET_ARG_AFTER(opt, "-Wp,");
1026 add_flag(&cppflags_obst, "-Wp,%s", opt);
1027 } else if (strstart(option + 1, "l,")) {
1028 // pass options directly to the linker
1030 GET_ARG_AFTER(opt, "-Wl,");
1031 add_flag(&ldflags_obst, "-Wl,%s", opt);
1032 } else if (streq(option + 1, "no-trigraphs")
1033 || streq(option + 1, "undef")) {
1034 add_flag(&cppflags_obst, "%s", arg);
1036 set_warning_opt(&option[1]);
1038 } else if (option[0] == 'm') {
1043 GET_ARG_AFTER(opt, "-m");
1044 if (strstart(opt, "target=")) {
1045 GET_ARG_AFTER(opt, "-mtarget=");
1046 if (!parse_target_triple(opt))
1047 argument_errors = true;
1048 } else if (strstart(opt, "triple=")) {
1049 GET_ARG_AFTER(opt, "-mtriple=");
1050 if (!parse_target_triple(opt))
1051 argument_errors = true;
1052 } else if (strstart(opt, "arch=")) {
1053 GET_ARG_AFTER(opt, "-march=");
1054 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1055 int res = be_parse_arg(arch_opt);
1057 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1058 argument_errors = true;
1060 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1061 int res = be_parse_arg(arch_opt);
1063 argument_errors = true;
1065 } else if (strstart(opt, "tune=")) {
1066 GET_ARG_AFTER(opt, "-mtune=");
1067 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1068 int res = be_parse_arg(arch_opt);
1070 argument_errors = true;
1071 } else if (strstart(opt, "cpu=")) {
1072 GET_ARG_AFTER(opt, "-mcpu=");
1073 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1074 int res = be_parse_arg(arch_opt);
1076 argument_errors = true;
1077 } else if (strstart(opt, "fpmath=")) {
1078 GET_ARG_AFTER(opt, "-mfpmath=");
1079 if (streq(opt, "387"))
1081 else if (streq(opt, "sse"))
1084 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1085 argument_errors = true;
1087 if (!argument_errors) {
1088 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1089 int res = be_parse_arg(arch_opt);
1091 argument_errors = true;
1093 } else if (strstart(opt, "preferred-stack-boundary=")) {
1094 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1095 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1096 int res = be_parse_arg(arch_opt);
1098 argument_errors = true;
1099 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1100 set_be_option("omitleaffp=1");
1101 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1102 set_be_option("omitleaffp=0");
1103 } else if (streq(opt, "rtd")) {
1104 default_calling_convention = CC_STDCALL;
1105 } else if (strstart(opt, "regparm=")) {
1106 fprintf(stderr, "error: regparm convention not supported yet\n");
1107 argument_errors = true;
1108 } else if (streq(opt, "soft-float")) {
1109 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1110 argument_errors = true;
1112 long int value = strtol(opt, NULL, 10);
1114 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1115 argument_errors = true;
1116 } else if (value != 16 && value != 32 && value != 64) {
1117 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1118 argument_errors = true;
1120 machine_size = (unsigned int)value;
1121 add_flag(&asflags_obst, "--%u", machine_size);
1122 add_flag(&ldflags_obst, "-m%u", machine_size);
1125 } else if (streq(option, "pg")) {
1126 set_be_option("gprof");
1127 add_flag(&ldflags_obst, "-pg");
1128 } else if (streq(option, "pedantic") ||
1129 streq(option, "ansi")) {
1130 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1131 } else if (streq(option, "shared")) {
1132 add_flag(&ldflags_obst, "-shared");
1133 } else if (strstart(option, "std=")) {
1134 const char *const o = &option[4];
1136 streq(o, "c++") ? STANDARD_CXX98 :
1137 streq(o, "c++98") ? STANDARD_CXX98 :
1138 streq(o, "c89") ? STANDARD_C89 :
1139 streq(o, "c99") ? STANDARD_C99 :
1140 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1141 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1142 streq(o, "gnu89") ? STANDARD_GNU89 :
1143 streq(o, "gnu99") ? STANDARD_GNU99 :
1144 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1145 streq(o, "iso9899:1990") ? STANDARD_C89 :
1146 streq(o, "iso9899:199409") ? STANDARD_C90 :
1147 streq(o, "iso9899:1999") ? STANDARD_C99 :
1148 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1149 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1150 } else if (streq(option, "version")) {
1151 print_cparser_version();
1152 } else if (strstart(option, "print-file-name=")) {
1153 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1154 } else if (option[0] == '-') {
1155 /* double dash option */
1157 if (streq(option, "gcc")) {
1158 features_on |= _GNUC;
1159 features_off &= ~_GNUC;
1160 } else if (streq(option, "no-gcc")) {
1161 features_on &= ~_GNUC;
1162 features_off |= _GNUC;
1163 } else if (streq(option, "ms")) {
1165 features_off &= ~_MS;
1166 } else if (streq(option, "no-ms")) {
1167 features_on &= ~_MS;
1168 features_off |= _MS;
1169 } else if (streq(option, "strict")) {
1171 } else if (streq(option, "lextest")) {
1173 } else if (streq(option, "benchmark")) {
1174 mode = BenchmarkParser;
1175 } else if (streq(option, "print-ast")) {
1177 } else if (streq(option, "print-implicit-cast")) {
1178 print_implicit_casts = true;
1179 } else if (streq(option, "print-parenthesis")) {
1180 print_parenthesis = true;
1181 } else if (streq(option, "print-fluffy")) {
1183 } else if (streq(option, "print-jna")) {
1185 } else if (streq(option, "jna-limit")) {
1188 fprintf(stderr, "error: "
1189 "expected argument after '--jna-limit'\n");
1190 argument_errors = true;
1193 jna_limit_output(argv[i]);
1194 } else if (streq(option, "jna-libname")) {
1197 fprintf(stderr, "error: "
1198 "expected argument after '--jna-libname'\n");
1199 argument_errors = true;
1202 jna_set_libname(argv[i]);
1203 } else if (streq(option, "time")) {
1205 } else if (streq(option, "version")) {
1206 print_cparser_version();
1207 return EXIT_SUCCESS;
1208 } else if (streq(option, "help")) {
1209 print_help(argv[0]);
1210 help_displayed = true;
1211 } else if (streq(option, "dump-function")) {
1214 fprintf(stderr, "error: "
1215 "expected argument after '--dump-function'\n");
1216 argument_errors = true;
1219 dumpfunction = argv[i];
1221 } else if (streq(option, "export-ir")) {
1222 mode = CompileExportIR;
1224 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1225 argument_errors = true;
1228 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1229 argument_errors = true;
1232 filetype_t type = forced_filetype;
1233 if (type == FILETYPE_AUTODETECT) {
1234 if (streq(arg, "-")) {
1235 /* - implicitly means C source file */
1238 const char *suffix = strrchr(arg, '.');
1239 /* Ensure there is at least one char before the suffix */
1240 if (suffix != NULL && suffix != arg) {
1243 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1244 streq(suffix, "a") ? FILETYPE_OBJECT :
1245 streq(suffix, "c") ? FILETYPE_C :
1246 streq(suffix, "C") ? FILETYPE_CXX :
1247 streq(suffix, "cc") ? FILETYPE_CXX :
1248 streq(suffix, "cp") ? FILETYPE_CXX :
1249 streq(suffix, "cpp") ? FILETYPE_CXX :
1250 streq(suffix, "CPP") ? FILETYPE_CXX :
1251 streq(suffix, "cxx") ? FILETYPE_CXX :
1252 streq(suffix, "c++") ? FILETYPE_CXX :
1253 streq(suffix, "ii") ? FILETYPE_CXX :
1254 streq(suffix, "h") ? FILETYPE_C :
1255 streq(suffix, "ir") ? FILETYPE_IR :
1256 streq(suffix, "o") ? FILETYPE_OBJECT :
1257 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1258 streq(suffix, "so") ? FILETYPE_OBJECT :
1259 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1264 file_list_entry_t *entry
1265 = obstack_alloc(&file_obst, sizeof(entry[0]));
1266 memset(entry, 0, sizeof(entry[0]));
1270 if (last_file != NULL) {
1271 last_file->next = entry;
1279 if (help_displayed) {
1280 return !argument_errors;
1283 if (print_file_name_file != NULL) {
1284 print_file_name(print_file_name_file);
1285 return EXIT_SUCCESS;
1287 if (files == NULL) {
1288 fprintf(stderr, "error: no input files specified\n");
1289 argument_errors = true;
1292 if (argument_errors) {
1294 return EXIT_FAILURE;
1297 /* we do the lowering in ast2firm */
1298 firm_opt.lower_bitfields = FALSE;
1300 /* set the c_mode here, types depends on it */
1301 c_mode |= features_on;
1302 c_mode &= ~features_off;
1305 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1306 init_symbol_table();
1319 if (construct_dep_target) {
1320 if (outname != 0 && strlen(outname) >= 2) {
1321 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1323 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1326 dep_target[0] = '\0';
1329 char outnamebuf[4096];
1330 if (outname == NULL) {
1331 const char *filename = files->name;
1334 case BenchmarkParser:
1339 case PreprocessOnly:
1344 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1345 outname = outnamebuf;
1347 case CompileAssemble:
1348 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1349 outname = outnamebuf;
1352 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1354 outname = outnamebuf;
1356 case CompileExportIR:
1357 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1358 outname = outnamebuf;
1360 case CompileAssembleLink:
1370 assert(outname != NULL);
1373 if (streq(outname, "-")) {
1376 out = fopen(outname, "w");
1378 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1380 return EXIT_FAILURE;
1384 file_list_entry_t *file;
1385 bool already_constructed_firm = false;
1386 for (file = files; file != NULL; file = file->next) {
1387 char asm_tempfile[1024];
1388 const char *filename = file->name;
1389 filetype_t filetype = file->type;
1391 if (filetype == FILETYPE_OBJECT)
1395 if (mode == LexTest) {
1397 in = open_file(filename);
1398 lextest(in, filename);
1400 return EXIT_SUCCESS;
1403 FILE *preprocessed_in = NULL;
1404 filetype_t next_filetype = filetype;
1407 next_filetype = FILETYPE_PREPROCESSED_C;
1410 next_filetype = FILETYPE_PREPROCESSED_CXX;
1412 case FILETYPE_ASSEMBLER:
1413 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1416 /* no support for input on FILE* yet */
1418 panic("internal compiler error: in for preprocessor != NULL");
1420 preprocessed_in = preprocess(filename, filetype);
1421 if (mode == PreprocessOnly) {
1422 copy_file(out, preprocessed_in);
1423 int result = pclose(preprocessed_in);
1425 /* remove output file in case of error */
1426 if (out != stdout && result != EXIT_SUCCESS) {
1432 in = preprocessed_in;
1433 filetype = next_filetype;
1441 if (mode == Compile) {
1444 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1448 in = open_file(filename);
1450 /* preprocess and compile */
1451 if (filetype == FILETYPE_PREPROCESSED_C) {
1452 char const* invalid_mode;
1455 case STANDARD_C89: c_mode = _C89; break;
1456 /* TODO determine difference between these two */
1457 case STANDARD_C90: c_mode = _C89; break;
1458 case STANDARD_C99: c_mode = _C89 | _C99; break;
1459 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1463 "warning: command line option \"-std=%s\" is not valid for C\n",
1466 case STANDARD_DEFAULT:
1467 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1469 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1470 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1473 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1474 char const* invalid_mode;
1476 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1477 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1478 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1479 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1480 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1483 case STANDARD_CXX98: c_mode = _CXX; break;
1487 "warning: command line option \"-std=%s\" is not valid for C++\n",
1489 case STANDARD_DEFAULT:
1490 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1494 c_mode |= features_on;
1495 c_mode &= ~features_off;
1497 /* do the actual parsing */
1498 ir_timer_t *t_parsing = ir_timer_new();
1499 timer_register(t_parsing, "Frontend: Parsing");
1500 timer_push(t_parsing);
1502 translation_unit_t *const unit = do_parsing(in, filename);
1503 timer_pop(t_parsing);
1505 /* prints the AST even if errors occurred */
1506 if (mode == PrintAst) {
1511 if (error_count > 0) {
1512 /* parsing failed because of errors */
1513 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1515 result = EXIT_FAILURE;
1517 } else if (warning_count > 0) {
1518 fprintf(stderr, "%u warning(s)\n", warning_count);
1521 if (in == preprocessed_in) {
1522 int pp_result = pclose(preprocessed_in);
1523 if (pp_result != EXIT_SUCCESS) {
1524 /* remove output file */
1527 return EXIT_FAILURE;
1531 if (mode == BenchmarkParser) {
1533 } else if (mode == PrintFluffy) {
1534 write_fluffy_decls(out, unit);
1536 } else if (mode == PrintJna) {
1537 write_jna_decls(out, unit);
1541 /* build the firm graph */
1542 ir_timer_t *t_construct = ir_timer_new();
1543 timer_register(t_construct, "Frontend: Graph construction");
1544 timer_push(t_construct);
1545 if (already_constructed_firm) {
1546 panic("compiling multiple files/translation units not possible");
1548 translation_unit_to_firm(unit);
1549 already_constructed_firm = true;
1550 timer_pop(t_construct);
1553 if (mode == ParseOnly) {
1557 if (mode == CompileDump) {
1559 ident *id = new_id_from_str(dumpfunction);
1560 ir_graph *irg = NULL;
1561 int n_irgs = get_irp_n_irgs();
1562 for (int i = 0; i < n_irgs; ++i) {
1563 ir_graph *tirg = get_irp_irg(i);
1564 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1572 fprintf(stderr, "No graph for function '%s' found\n",
1574 return EXIT_FAILURE;
1577 dump_ir_graph_file(out, irg);
1579 return EXIT_SUCCESS;
1582 if (mode == CompileExportIR) {
1585 return EXIT_SUCCESS;
1588 gen_firm_finish(asm_out, filename, have_const_functions);
1589 if (asm_out != out) {
1592 } else if (filetype == FILETYPE_IR) {
1594 ir_import(filename);
1596 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1597 copy_file(asm_out, in);
1598 if (in == preprocessed_in) {
1599 int pp_result = pclose(preprocessed_in);
1600 if (pp_result != EXIT_SUCCESS) {
1601 /* remove output in error case */
1607 if (asm_out != out) {
1612 if (mode == Compile)
1615 /* if we're here then we have preprocessed assembly */
1616 filename = asm_tempfile;
1617 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1620 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1622 const char *filename_o;
1623 if (mode == CompileAssemble) {
1625 filename_o = outname;
1627 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1632 assemble(filename_o, filename);
1634 size_t len = strlen(filename_o) + 1;
1635 filename = obstack_copy(&file_obst, filename_o, len);
1636 filetype = FILETYPE_OBJECT;
1639 /* ok we're done here, process next file */
1640 file->name = filename;
1641 file->type = filetype;
1644 if (result != EXIT_SUCCESS) {
1650 /* link program file */
1651 if (mode == CompileAssembleLink) {
1652 obstack_1grow(&ldflags_obst, '\0');
1653 const char *flags = obstack_finish(&ldflags_obst);
1655 /* construct commandline */
1656 const char *linker = getenv("CPARSER_LINK");
1657 if (linker != NULL) {
1658 obstack_printf(&file_obst, "%s ", linker);
1660 if (target_triple != NULL)
1661 obstack_printf(&file_obst, "%s-", target_triple);
1662 obstack_printf(&file_obst, "%s ", LINKER);
1665 for (file_list_entry_t *entry = files; entry != NULL;
1666 entry = entry->next) {
1667 if (entry->type != FILETYPE_OBJECT)
1670 add_flag(&file_obst, "%s", entry->name);
1673 add_flag(&file_obst, "-o");
1674 add_flag(&file_obst, outname);
1675 obstack_printf(&file_obst, "%s", flags);
1676 obstack_1grow(&file_obst, '\0');
1678 char *commandline = obstack_finish(&file_obst);
1683 int err = system(commandline);
1684 if (err != EXIT_SUCCESS) {
1685 fprintf(stderr, "linker reported an error\n");
1686 return EXIT_FAILURE;
1693 obstack_free(&cppflags_obst, NULL);
1694 obstack_free(&ldflags_obst, NULL);
1695 obstack_free(&asflags_obst, NULL);
1696 obstack_free(&file_obst, NULL);
1706 exit_symbol_table();
1707 return EXIT_SUCCESS;