2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "driver/firm_timing.h"
75 #include "adt/error.h"
76 #include "wrappergen/write_fluffy.h"
77 #include "wrappergen/write_caml.h"
78 #include "wrappergen/write_jna.h"
86 #define PREPROCESSOR "gcc -E -m32 -U__STRICT_ANSI__"
88 #define PREPROCESSOR "cpp -m32 -U__STRICT_ANSI__"
93 #define LINKER "gcc -m32"
98 #define ASSEMBLER "gcc -m32 -c -xassembler"
100 #define ASSEMBLER "as --32"
104 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
105 unsigned int machine_size = 32;
106 bool byte_order_big_endian = false;
107 bool char_is_signed = true;
108 bool strict_mode = false;
109 bool use_builtins = false;
110 bool have_const_functions = false;
111 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
112 unsigned force_long_double_size = 0;
113 bool enable_main_collect2_hack = false;
114 bool freestanding = false;
116 /* to switch on printing of implicit casts */
117 extern bool print_implicit_casts;
119 /* to switch on printing of parenthesis to indicate operator precedence */
120 extern bool print_parenthesis;
122 static const char *target_triple;
124 static struct obstack cppflags_obst, ldflags_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);
339 obstack_1grow(&cppflags_obst, '\0');
340 char *buf = obstack_finish(&cppflags_obst);
345 FILE *f = popen(buf, "r");
347 fprintf(stderr, "invoking preprocessor failed\n");
351 /* we don't really need that anymore */
352 obstack_free(&cppflags_obst, buf);
357 static void assemble(const char *out, const char *in)
359 struct obstack asflags_obst;
362 obstack_init(&asflags_obst);
364 const char *assembler = getenv("CPARSER_AS");
365 if (assembler != NULL) {
366 obstack_printf(&asflags_obst, "%s ", assembler);
368 if (target_triple != NULL)
369 obstack_printf(&asflags_obst, "%s-", target_triple);
370 obstack_printf(&asflags_obst, "%s ", ASSEMBLER);
373 obstack_printf(&asflags_obst, "%s -o %s", in, out);
374 obstack_1grow(&asflags_obst, '\0');
375 buf = obstack_finish(&asflags_obst);
381 int err = system(buf);
383 fprintf(stderr, "assembler reported an error\n");
387 obstack_free(&asflags_obst, NULL);
390 static void print_file_name(const char *file)
392 add_flag(&ldflags_obst, "-print-file-name=%s", file);
394 obstack_1grow(&ldflags_obst, '\0');
395 const char *flags = obstack_finish(&ldflags_obst);
397 /* construct commandline */
398 const char *linker = getenv("CPARSER_LINK");
399 if (linker != NULL) {
400 obstack_printf(&ldflags_obst, "%s ", linker);
402 if (target_triple != NULL)
403 obstack_printf(&ldflags_obst, "%s-", target_triple);
404 obstack_printf(&ldflags_obst, "%s ", LINKER);
406 obstack_printf(&ldflags_obst, "%s ", linker);
407 obstack_printf(&ldflags_obst, "%s", flags);
408 obstack_1grow(&ldflags_obst, '\0');
410 char *commandline = obstack_finish(&ldflags_obst);
415 int err = system(commandline);
416 if (err != EXIT_SUCCESS) {
417 fprintf(stderr, "linker reported an error\n");
422 static const char *try_dir(const char *dir)
426 if (access(dir, R_OK | W_OK | X_OK) == 0)
431 static const char *get_tempdir(void)
433 static const char *tmpdir = NULL;
439 tmpdir = try_dir(getenv("TMPDIR"));
441 tmpdir = try_dir(getenv("TMP"));
443 tmpdir = try_dir(getenv("TEMP"));
447 tmpdir = try_dir(P_tmpdir);
451 tmpdir = try_dir("/var/tmp");
453 tmpdir = try_dir("/usr/tmp");
455 tmpdir = try_dir("/tmp");
464 /* cheap and nasty mkstemp replacement */
465 static int mkstemp(char *templ)
468 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
473 * an own version of tmpnam, which: writes in a buffer, emits no warnings
474 * during linking (like glibc/gnu ld do for tmpnam)...
476 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
478 const char *tempdir = get_tempdir();
480 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
482 int fd = mkstemp(buffer);
484 fprintf(stderr, "couldn't create temporary file: %s\n",
488 FILE *out = fdopen(fd, "w");
490 fprintf(stderr, "couldn't create temporary file FILE*\n");
494 file_list_entry_t *entry = xmalloc(sizeof(*entry));
495 memset(entry, 0, sizeof(*entry));
497 size_t name_len = strlen(buffer) + 1;
498 char *name = malloc(name_len);
499 memcpy(name, buffer, name_len);
502 entry->next = temp_files;
508 static void free_temp_files(void)
510 file_list_entry_t *entry = temp_files;
511 file_list_entry_t *next;
512 for ( ; entry != NULL; entry = next) {
516 free((char*) entry->name);
523 * Do the necessary lowering for compound parameters.
525 void lower_compound_params(void)
527 lower_params_t params;
529 params.def_ptr_alignment = 4;
530 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
531 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
532 params.find_pointer_type = NULL;
533 params.ret_compound_in_regs = NULL;
534 lower_calls_with_compounds(¶ms);
537 typedef enum compile_mode_t {
553 static void usage(const char *argv0)
555 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
558 static void print_cparser_version(void)
560 printf("cparser (%s) using libFirm (%u.%u",
561 cparser_REVISION, ir_get_version_major(),
562 ir_get_version_minor());
564 const char *revision = ir_get_version_revision();
565 if (revision[0] != 0) {
567 fputs(revision, stdout);
570 const char *build = ir_get_version_build();
573 fputs(build, stdout);
576 puts("This is free software; see the source for copying conditions. There is NO\n"
577 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
580 static void print_help(const char *argv0)
584 puts("\t-fhelp Display help about firm optimisation options");
585 puts("\t-bhelp Display help about firm backend options");
586 puts("A big number of gcc flags is also supported");
589 static void set_be_option(const char *arg)
591 int res = be_parse_arg(arg);
596 static void set_option(const char *arg)
598 int res = firm_option(arg);
603 static void copy_file(FILE *dest, FILE *input)
607 while (!feof(input) && !ferror(dest)) {
608 size_t read = fread(buf, 1, sizeof(buf), input);
609 if (fwrite(buf, 1, read, dest) != read) {
610 perror("couldn't write output");
615 static inline bool streq(char const* a, char const* b)
617 return strcmp(a, b) == 0;
620 static inline bool strstart(char const* str, char const* start)
625 } while (*str++ == *start++);
629 static FILE *open_file(const char *filename)
631 if (streq(filename, "-")) {
635 FILE *in = fopen(filename, "r");
637 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
645 static filetype_t get_filetype_from_string(const char *string)
647 if (streq(string, "c") || streq(string, "c-header"))
649 if (streq(string, "c++") || streq(string, "c++-header"))
651 if (streq(string, "assembler"))
652 return FILETYPE_PREPROCESSED_ASSEMBLER;
653 if (streq(string, "assembler-with-cpp"))
654 return FILETYPE_ASSEMBLER;
655 if (streq(string, "none"))
656 return FILETYPE_AUTODETECT;
658 return FILETYPE_UNKNOWN;
662 * Initialize firm codegeneration for a specific operating system.
663 * The argument is the operating system part of a target-triple */
664 static bool set_os_support(const char *os)
666 wchar_atomic_kind = ATOMIC_TYPE_INT;
667 force_long_double_size = 0;
668 enable_main_collect2_hack = false;
670 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
671 || streq(os, "solaris")) {
672 set_be_option("ia32-gasmode=elf");
673 set_create_ld_ident(create_name_linux_elf);
674 } else if (streq(os, "darwin")) {
675 force_long_double_size = 16;
676 set_be_option("ia32-gasmode=macho");
677 set_be_option("ia32-stackalign=4");
678 set_be_option("pic=true");
679 set_create_ld_ident(create_name_macho);
680 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
681 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
682 enable_main_collect2_hack = true;
683 set_be_option("ia32-gasmode=mingw");
684 set_create_ld_ident(create_name_win32);
692 static bool parse_target_triple(const char *arg)
694 const char *manufacturer = strchr(arg, '-');
695 if (manufacturer == NULL) {
696 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
701 const char *os = strchr(manufacturer, '-');
703 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
708 /* Note: Triples are more or less defined by what the config.guess and
709 * config.sub scripts from GNU autoconf emit. We have to lookup there what
710 * triples are possible */
712 /* process cpu type */
713 if (strstart(arg, "i386-")) {
714 be_parse_arg("isa=ia32");
715 be_parse_arg("ia32-arch=i386");
716 } else if (strstart(arg, "i486-")) {
717 be_parse_arg("isa=ia32");
718 be_parse_arg("ia32-arch=i486");
719 } else if (strstart(arg, "i586-")) {
720 be_parse_arg("isa=ia32");
721 be_parse_arg("ia32-arch=i586");
722 } else if (strstart(arg, "i686-")) {
723 be_parse_arg("isa=ia32");
724 be_parse_arg("ia32-arch=i686");
725 } else if (strstart(arg, "i786-")) {
726 be_parse_arg("isa=ia32");
727 be_parse_arg("ia32-arch=pentium4");
728 } else if (strstart(arg, "x86_64")) {
729 be_parse_arg("isa=amd64");
730 } else if (strstart(arg, "sparc-")) {
731 be_parse_arg("isa=sparc");
732 } else if (strstart(arg, "arm-")) {
733 be_parse_arg("isa=arm");
735 fprintf(stderr, "Unknown cpu in triple '%s'\n", arg);
739 /* process manufacturer, alot of people incorrectly leave out the
740 * manufacturer instead of using unknown- */
741 if (strstart(manufacturer, "linux")) {
743 manufacturer = "unknown-";
746 /* process operating system */
747 if (!set_os_support(os)) {
748 fprintf(stderr, "Unknown operating system '%s' in triple '%s'\n", os, arg);
756 int main(int argc, char **argv)
760 const char *dumpfunction = NULL;
761 const char *print_file_name_file = NULL;
762 compile_mode_t mode = CompileAssembleLink;
764 int result = EXIT_SUCCESS;
765 char cpu_arch[16] = "ia32";
766 file_list_entry_t *files = NULL;
767 file_list_entry_t *last_file = NULL;
768 bool construct_dep_target = false;
769 bool do_timing = false;
770 struct obstack file_obst;
772 atexit(free_temp_files);
774 /* hack for now... */
775 if (strstr(argv[0], "pptest") != NULL) {
776 extern int pptest_main(int argc, char **argv);
777 return pptest_main(argc, argv);
780 obstack_init(&cppflags_obst);
781 obstack_init(&ldflags_obst);
782 obstack_init(&file_obst);
784 #define GET_ARG_AFTER(def, args) \
785 def = &arg[sizeof(args)-1]; \
786 if (def[0] == '\0') { \
789 fprintf(stderr, "error: expected argument after '" args "'\n"); \
790 argument_errors = true; \
794 if (def[0] == '-' && def[1] != '\0') { \
795 fprintf(stderr, "error: expected argument after '" args "'\n"); \
796 argument_errors = true; \
801 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
803 /* early options parsing (find out optimisation level and OS) */
804 for (int i = 1; i < argc; ++i) {
805 const char *arg = argv[i];
809 const char *option = &arg[1];
810 if (option[0] == 'O') {
811 sscanf(&option[1], "%d", &opt_level);
816 #if defined(_WIN32) || defined(__CYGWIN__)
817 set_os_support("win32");
818 #elif defined(__APPLE__)
819 set_os_support("darwin");
821 set_os_support("linux");
824 /* apply optimisation level */
827 set_option("no-opt");
830 set_option("no-inline");
834 /* use_builtins = true; */
837 set_option("thread-jumps");
838 set_option("if-conversion");
841 set_option("strict-aliasing");
842 set_option("inline");
843 set_option("deconv");
844 set_be_option("omitfp");
848 const char *target = getenv("TARGET");
850 parse_target_triple(target);
852 /* parse rest of options */
853 standard = STANDARD_DEFAULT;
854 unsigned features_on = 0;
855 unsigned features_off = 0;
856 filetype_t forced_filetype = FILETYPE_AUTODETECT;
857 bool help_displayed = false;
858 bool argument_errors = false;
859 for (int i = 1; i < argc; ++i) {
860 const char *arg = argv[i];
861 if (arg[0] == '-' && arg[1] != '\0') {
863 const char *option = &arg[1];
864 if (option[0] == 'o') {
865 GET_ARG_AFTER(outname, "-o");
866 } else if (option[0] == 'g') {
867 set_be_option("debuginfo=stabs");
868 set_be_option("omitfp=no");
869 set_be_option("ia32-nooptcc=yes");
870 } else if (SINGLE_OPTION('c')) {
871 mode = CompileAssemble;
872 } else if (SINGLE_OPTION('E')) {
873 mode = PreprocessOnly;
874 } else if (SINGLE_OPTION('S')) {
876 } else if (option[0] == 'O') {
878 } else if (option[0] == 'I') {
880 GET_ARG_AFTER(opt, "-I");
881 add_flag(&cppflags_obst, "-I%s", opt);
882 } else if (option[0] == 'D') {
884 GET_ARG_AFTER(opt, "-D");
885 add_flag(&cppflags_obst, "-D%s", opt);
886 } else if (option[0] == 'U') {
888 GET_ARG_AFTER(opt, "-U");
889 add_flag(&cppflags_obst, "-U%s", opt);
890 } else if (option[0] == 'l') {
892 GET_ARG_AFTER(opt, "-l");
893 add_flag(&ldflags_obst, "-l%s", opt);
894 } else if (option[0] == 'L') {
896 GET_ARG_AFTER(opt, "-L");
897 add_flag(&ldflags_obst, "-L%s", opt);
898 } else if (SINGLE_OPTION('v')) {
900 } else if (SINGLE_OPTION('w')) {
901 memset(&warning, 0, sizeof(warning));
902 } else if (option[0] == 'x') {
904 GET_ARG_AFTER(opt, "-x");
905 forced_filetype = get_filetype_from_string(opt);
906 if (forced_filetype == FILETYPE_UNKNOWN) {
907 fprintf(stderr, "Unknown language '%s'\n", opt);
908 argument_errors = true;
910 } else if (streq(option, "M")) {
911 mode = PreprocessOnly;
912 add_flag(&cppflags_obst, "-M");
913 } else if (streq(option, "MMD") ||
914 streq(option, "MD")) {
915 construct_dep_target = true;
916 add_flag(&cppflags_obst, "-%s", option);
917 } else if (streq(option, "MM") ||
918 streq(option, "MP")) {
919 add_flag(&cppflags_obst, "-%s", option);
920 } else if (streq(option, "MT") ||
921 streq(option, "MQ") ||
922 streq(option, "MF")) {
924 GET_ARG_AFTER(opt, "-MT");
925 add_flag(&cppflags_obst, "-%s", option);
926 add_flag(&cppflags_obst, "%s", opt);
927 } else if (streq(option, "include")) {
929 GET_ARG_AFTER(opt, "-include");
930 add_flag(&cppflags_obst, "-include");
931 add_flag(&cppflags_obst, "%s", opt);
932 } else if (streq(option, "isystem")) {
934 GET_ARG_AFTER(opt, "-isystem");
935 add_flag(&cppflags_obst, "-isystem");
936 add_flag(&cppflags_obst, "%s", opt);
937 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
938 } else if (streq(option, "pthread")) {
939 /* set flags for the preprocessor */
940 add_flag(&cppflags_obst, "-D_REENTRANT");
941 /* set flags for the linker */
942 add_flag(&ldflags_obst, "-lpthread");
944 } else if (streq(option, "nostdinc")
945 || streq(option, "trigraphs")) {
946 /* pass these through to the preprocessor */
947 add_flag(&cppflags_obst, "%s", arg);
948 } else if (streq(option, "pipe")) {
949 /* here for gcc compatibility */
950 } else if (option[0] == 'f') {
951 char const *orig_opt;
952 GET_ARG_AFTER(orig_opt, "-f");
954 if (strstart(orig_opt, "input-charset=")) {
955 char const* const encoding = strchr(orig_opt, '=') + 1;
956 select_input_encoding(encoding);
957 } else if (streq(orig_opt, "verbose-asm")) {
958 /* ignore: we always print verbose assembler */
960 char const *opt = orig_opt;
961 bool truth_value = true;
962 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
967 if (streq(opt, "builtins")) {
968 use_builtins = truth_value;
969 } else if (streq(opt, "dollars-in-identifiers")) {
970 allow_dollar_in_symbol = truth_value;
971 } else if (streq(opt, "omit-frame-pointer")) {
972 set_be_option(truth_value ? "omitfp" : "omitfp=no");
973 } else if (streq(opt, "short-wchar")) {
974 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
976 } else if (streq(opt, "signed-char")) {
977 char_is_signed = truth_value;
978 } else if (streq(opt, "strength-reduce")) {
979 firm_option(truth_value ? "strength-red" : "no-strength-red");
980 } else if (streq(opt, "syntax-only")) {
981 mode = truth_value ? ParseOnly : CompileAssembleLink;
982 } else if (streq(opt, "unsigned-char")) {
983 char_is_signed = !truth_value;
984 } else if (streq(opt, "freestanding")) {
986 } else if (streq(opt, "hosted")) {
987 freestanding = false;
988 } else if (truth_value == false &&
989 streq(opt, "asynchronous-unwind-tables")) {
990 /* nothing todo, a gcc feature which we don't support
991 * anyway was deactivated */
992 } else if (strstart(orig_opt, "align-loops=") ||
993 strstart(orig_opt, "align-jumps=") ||
994 strstart(orig_opt, "align-functions=")) {
995 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
996 } else if (strstart(orig_opt, "message-length=")) {
997 /* ignore: would only affect error message format */
998 } else if (streq(opt, "fast-math") ||
999 streq(opt, "jump-tables") ||
1000 streq(opt, "expensive-optimizations") ||
1001 streq(opt, "common") ||
1002 streq(opt, "optimize-sibling-calls") ||
1003 streq(opt, "align-loops") ||
1004 streq(opt, "align-jumps") ||
1005 streq(opt, "align-functions") ||
1006 streq(opt, "PIC")) {
1007 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1009 int res = firm_option(orig_opt);
1011 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1013 argument_errors = true;
1015 } else if (res == -1) {
1016 help_displayed = true;
1020 } else if (option[0] == 'b') {
1022 GET_ARG_AFTER(opt, "-b");
1023 int res = be_parse_arg(opt);
1025 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1027 argument_errors = true;
1028 } else if (res == -1) {
1029 help_displayed = true;
1030 } else if (strstart(opt, "isa=")) {
1031 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1033 } else if (option[0] == 'W') {
1034 if (option[1] == '\0') {
1035 /* ignore -W, our defaults are already quite verbose */
1036 } else if (strstart(option + 1, "p,")) {
1037 // pass options directly to the preprocessor
1039 GET_ARG_AFTER(opt, "-Wp,");
1040 add_flag(&cppflags_obst, "-Wp,%s", opt);
1041 } else if (strstart(option + 1, "l,")) {
1042 // pass options directly to the linker
1044 GET_ARG_AFTER(opt, "-Wl,");
1045 add_flag(&ldflags_obst, "-Wl,%s", opt);
1046 } else if (streq(option + 1, "no-trigraphs")
1047 || streq(option + 1, "undef")) {
1048 add_flag(&cppflags_obst, "%s", arg);
1050 set_warning_opt(&option[1]);
1052 } else if (option[0] == 'm') {
1057 GET_ARG_AFTER(opt, "-m");
1058 if (strstart(opt, "target=")) {
1059 GET_ARG_AFTER(opt, "-mtarget=");
1060 if (!parse_target_triple(opt))
1061 argument_errors = true;
1062 } else if (strstart(opt, "triple=")) {
1063 GET_ARG_AFTER(opt, "-mtriple=");
1064 if (!parse_target_triple(opt))
1065 argument_errors = true;
1066 } else if (strstart(opt, "arch=")) {
1067 GET_ARG_AFTER(opt, "-march=");
1068 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1069 int res = be_parse_arg(arch_opt);
1071 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1072 argument_errors = true;
1074 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1075 int res = be_parse_arg(arch_opt);
1077 argument_errors = true;
1079 } else if (strstart(opt, "tune=")) {
1080 GET_ARG_AFTER(opt, "-mtune=");
1081 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1082 int res = be_parse_arg(arch_opt);
1084 argument_errors = true;
1085 } else if (strstart(opt, "cpu=")) {
1086 GET_ARG_AFTER(opt, "-mcpu=");
1087 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1088 int res = be_parse_arg(arch_opt);
1090 argument_errors = true;
1091 } else if (strstart(opt, "fpmath=")) {
1092 GET_ARG_AFTER(opt, "-mfpmath=");
1093 if (streq(opt, "387"))
1095 else if (streq(opt, "sse"))
1098 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1099 argument_errors = true;
1101 if (!argument_errors) {
1102 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1103 int res = be_parse_arg(arch_opt);
1105 argument_errors = true;
1107 } else if (strstart(opt, "preferred-stack-boundary=")) {
1108 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1109 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1110 int res = be_parse_arg(arch_opt);
1112 argument_errors = true;
1113 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1114 set_be_option("omitleaffp=1");
1115 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1116 set_be_option("omitleaffp=0");
1117 } else if (streq(opt, "rtd")) {
1118 default_calling_convention = CC_STDCALL;
1119 } else if (strstart(opt, "regparm=")) {
1120 fprintf(stderr, "error: regparm convention not supported yet\n");
1121 argument_errors = true;
1122 } else if (streq(opt, "soft-float")) {
1123 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1124 argument_errors = true;
1127 long int value = strtol(opt, &endptr, 10);
1128 if (*endptr != '\0') {
1129 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1130 argument_errors = true;
1132 if (value != 16 && value != 32 && value != 64) {
1133 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1134 argument_errors = true;
1136 machine_size = (unsigned int)value;
1139 } else if (streq(option, "pg")) {
1140 set_be_option("gprof");
1141 add_flag(&ldflags_obst, "-pg");
1142 } else if (streq(option, "pedantic") ||
1143 streq(option, "ansi")) {
1144 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1145 } else if (streq(option, "shared")) {
1146 add_flag(&ldflags_obst, "-shared");
1147 } else if (strstart(option, "std=")) {
1148 const char *const o = &option[4];
1150 streq(o, "c++") ? STANDARD_CXX98 :
1151 streq(o, "c++98") ? STANDARD_CXX98 :
1152 streq(o, "c89") ? STANDARD_C89 :
1153 streq(o, "c99") ? STANDARD_C99 :
1154 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1155 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1156 streq(o, "gnu89") ? STANDARD_GNU89 :
1157 streq(o, "gnu99") ? STANDARD_GNU99 :
1158 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1159 streq(o, "iso9899:1990") ? STANDARD_C89 :
1160 streq(o, "iso9899:199409") ? STANDARD_C90 :
1161 streq(o, "iso9899:1999") ? STANDARD_C99 :
1162 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1163 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1164 } else if (streq(option, "version")) {
1165 print_cparser_version();
1166 } else if (strstart(option, "print-file-name=")) {
1167 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1168 } else if (option[0] == '-') {
1169 /* double dash option */
1171 if (streq(option, "gcc")) {
1172 features_on |= _GNUC;
1173 features_off &= ~_GNUC;
1174 } else if (streq(option, "no-gcc")) {
1175 features_on &= ~_GNUC;
1176 features_off |= _GNUC;
1177 } else if (streq(option, "ms")) {
1179 features_off &= ~_MS;
1180 } else if (streq(option, "no-ms")) {
1181 features_on &= ~_MS;
1182 features_off |= _MS;
1183 } else if (streq(option, "strict")) {
1185 } else if (streq(option, "lextest")) {
1187 } else if (streq(option, "benchmark")) {
1188 mode = BenchmarkParser;
1189 } else if (streq(option, "print-ast")) {
1191 } else if (streq(option, "print-implicit-cast")) {
1192 print_implicit_casts = true;
1193 } else if (streq(option, "print-parenthesis")) {
1194 print_parenthesis = true;
1195 } else if (streq(option, "print-fluffy")) {
1197 } else if (streq(option, "print-caml")) {
1199 } else if (streq(option, "print-jna")) {
1201 } else if (streq(option, "jna-limit")) {
1204 fprintf(stderr, "error: "
1205 "expected argument after '--jna-limit'\n");
1206 argument_errors = true;
1209 jna_limit_output(argv[i]);
1210 } else if (streq(option, "jna-libname")) {
1213 fprintf(stderr, "error: "
1214 "expected argument after '--jna-libname'\n");
1215 argument_errors = true;
1218 jna_set_libname(argv[i]);
1219 } else if (streq(option, "time")) {
1221 } else if (streq(option, "version")) {
1222 print_cparser_version();
1223 return EXIT_SUCCESS;
1224 } else if (streq(option, "help")) {
1225 print_help(argv[0]);
1226 help_displayed = true;
1227 } else if (streq(option, "dump-function")) {
1230 fprintf(stderr, "error: "
1231 "expected argument after '--dump-function'\n");
1232 argument_errors = true;
1235 dumpfunction = argv[i];
1237 } else if (streq(option, "export-ir")) {
1238 mode = CompileExportIR;
1240 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1241 argument_errors = true;
1244 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1245 argument_errors = true;
1248 filetype_t type = forced_filetype;
1249 if (type == FILETYPE_AUTODETECT) {
1250 if (streq(arg, "-")) {
1251 /* - implicitly means C source file */
1254 const char *suffix = strrchr(arg, '.');
1255 /* Ensure there is at least one char before the suffix */
1256 if (suffix != NULL && suffix != arg) {
1259 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1260 streq(suffix, "a") ? FILETYPE_OBJECT :
1261 streq(suffix, "c") ? FILETYPE_C :
1262 streq(suffix, "C") ? FILETYPE_CXX :
1263 streq(suffix, "cc") ? FILETYPE_CXX :
1264 streq(suffix, "cp") ? FILETYPE_CXX :
1265 streq(suffix, "cpp") ? FILETYPE_CXX :
1266 streq(suffix, "CPP") ? FILETYPE_CXX :
1267 streq(suffix, "cxx") ? FILETYPE_CXX :
1268 streq(suffix, "c++") ? FILETYPE_CXX :
1269 streq(suffix, "ii") ? FILETYPE_CXX :
1270 streq(suffix, "h") ? FILETYPE_C :
1271 streq(suffix, "ir") ? FILETYPE_IR :
1272 streq(suffix, "o") ? FILETYPE_OBJECT :
1273 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1274 streq(suffix, "so") ? FILETYPE_OBJECT :
1275 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1280 file_list_entry_t *entry
1281 = obstack_alloc(&file_obst, sizeof(entry[0]));
1282 memset(entry, 0, sizeof(entry[0]));
1286 if (last_file != NULL) {
1287 last_file->next = entry;
1295 if (help_displayed) {
1296 return !argument_errors;
1299 if (print_file_name_file != NULL) {
1300 print_file_name(print_file_name_file);
1301 return EXIT_SUCCESS;
1303 if (files == NULL) {
1304 fprintf(stderr, "error: no input files specified\n");
1305 argument_errors = true;
1308 if (argument_errors) {
1310 return EXIT_FAILURE;
1313 /* we do the lowering in ast2firm */
1314 firm_opt.lower_bitfields = FALSE;
1316 /* set the c_mode here, types depends on it */
1317 c_mode |= features_on;
1318 c_mode &= ~features_off;
1321 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1322 init_symbol_table();
1335 if (construct_dep_target) {
1336 if (outname != 0 && strlen(outname) >= 2) {
1337 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1339 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1342 dep_target[0] = '\0';
1345 char outnamebuf[4096];
1346 if (outname == NULL) {
1347 const char *filename = files->name;
1350 case BenchmarkParser:
1356 case PreprocessOnly:
1361 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1362 outname = outnamebuf;
1364 case CompileAssemble:
1365 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1366 outname = outnamebuf;
1369 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1371 outname = outnamebuf;
1373 case CompileExportIR:
1374 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1375 outname = outnamebuf;
1377 case CompileAssembleLink:
1387 assert(outname != NULL);
1390 if (streq(outname, "-")) {
1393 out = fopen(outname, "w");
1395 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1397 return EXIT_FAILURE;
1401 file_list_entry_t *file;
1402 bool already_constructed_firm = false;
1403 for (file = files; file != NULL; file = file->next) {
1404 char asm_tempfile[1024];
1405 const char *filename = file->name;
1406 filetype_t filetype = file->type;
1408 if (filetype == FILETYPE_OBJECT)
1412 if (mode == LexTest) {
1414 in = open_file(filename);
1415 lextest(in, filename);
1417 return EXIT_SUCCESS;
1420 FILE *preprocessed_in = NULL;
1421 filetype_t next_filetype = filetype;
1424 next_filetype = FILETYPE_PREPROCESSED_C;
1427 next_filetype = FILETYPE_PREPROCESSED_CXX;
1429 case FILETYPE_ASSEMBLER:
1430 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1433 /* no support for input on FILE* yet */
1435 panic("internal compiler error: in for preprocessor != NULL");
1437 preprocessed_in = preprocess(filename, filetype);
1438 if (mode == PreprocessOnly) {
1439 copy_file(out, preprocessed_in);
1440 int result = pclose(preprocessed_in);
1442 /* remove output file in case of error */
1443 if (out != stdout && result != EXIT_SUCCESS) {
1449 in = preprocessed_in;
1450 filetype = next_filetype;
1458 if (mode == Compile) {
1461 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1465 in = open_file(filename);
1467 /* preprocess and compile */
1468 if (filetype == FILETYPE_PREPROCESSED_C) {
1469 char const* invalid_mode;
1472 case STANDARD_C89: c_mode = _C89; break;
1473 /* TODO determine difference between these two */
1474 case STANDARD_C90: c_mode = _C89; break;
1475 case STANDARD_C99: c_mode = _C89 | _C99; break;
1476 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1480 "warning: command line option \"-std=%s\" is not valid for C\n",
1483 case STANDARD_DEFAULT:
1484 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1486 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1487 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1490 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1491 char const* invalid_mode;
1493 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1494 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1495 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1496 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1497 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1500 case STANDARD_CXX98: c_mode = _CXX; break;
1504 "warning: command line option \"-std=%s\" is not valid for C++\n",
1506 case STANDARD_DEFAULT:
1507 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1511 c_mode |= features_on;
1512 c_mode &= ~features_off;
1514 /* do the actual parsing */
1515 ir_timer_t *t_parsing = ir_timer_new();
1516 timer_register(t_parsing, "Frontend: Parsing");
1517 timer_push(t_parsing);
1519 translation_unit_t *const unit = do_parsing(in, filename);
1520 timer_pop(t_parsing);
1522 /* prints the AST even if errors occurred */
1523 if (mode == PrintAst) {
1528 if (error_count > 0) {
1529 /* parsing failed because of errors */
1530 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1532 result = EXIT_FAILURE;
1534 } else if (warning_count > 0) {
1535 fprintf(stderr, "%u warning(s)\n", warning_count);
1538 if (in == preprocessed_in) {
1539 int pp_result = pclose(preprocessed_in);
1540 if (pp_result != EXIT_SUCCESS) {
1541 /* remove output file */
1544 return EXIT_FAILURE;
1548 if (mode == BenchmarkParser) {
1550 } else if (mode == PrintFluffy) {
1551 write_fluffy_decls(out, unit);
1553 } else if (mode == PrintCaml) {
1554 write_caml_decls(out, unit);
1556 } else if (mode == PrintJna) {
1557 write_jna_decls(out, unit);
1561 /* build the firm graph */
1562 ir_timer_t *t_construct = ir_timer_new();
1563 timer_register(t_construct, "Frontend: Graph construction");
1564 timer_push(t_construct);
1565 if (already_constructed_firm) {
1566 panic("compiling multiple files/translation units not possible");
1568 translation_unit_to_firm(unit);
1569 already_constructed_firm = true;
1570 timer_pop(t_construct);
1573 if (mode == ParseOnly) {
1577 if (mode == CompileDump) {
1579 ident *id = new_id_from_str(dumpfunction);
1580 ir_graph *irg = NULL;
1581 int n_irgs = get_irp_n_irgs();
1582 for (int i = 0; i < n_irgs; ++i) {
1583 ir_graph *tirg = get_irp_irg(i);
1584 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1592 fprintf(stderr, "No graph for function '%s' found\n",
1594 return EXIT_FAILURE;
1597 dump_ir_graph_file(out, irg);
1599 return EXIT_SUCCESS;
1602 if (mode == CompileExportIR) {
1605 return EXIT_SUCCESS;
1608 gen_firm_finish(asm_out, filename, have_const_functions);
1609 if (asm_out != out) {
1612 } else if (filetype == FILETYPE_IR) {
1614 ir_import(filename);
1616 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1617 copy_file(asm_out, in);
1618 if (in == preprocessed_in) {
1619 int pp_result = pclose(preprocessed_in);
1620 if (pp_result != EXIT_SUCCESS) {
1621 /* remove output in error case */
1627 if (asm_out != out) {
1632 if (mode == Compile)
1635 /* if we're here then we have preprocessed assembly */
1636 filename = asm_tempfile;
1637 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1640 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1642 const char *filename_o;
1643 if (mode == CompileAssemble) {
1645 filename_o = outname;
1647 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1652 assemble(filename_o, filename);
1654 size_t len = strlen(filename_o) + 1;
1655 filename = obstack_copy(&file_obst, filename_o, len);
1656 filetype = FILETYPE_OBJECT;
1659 /* ok we're done here, process next file */
1660 file->name = filename;
1661 file->type = filetype;
1664 if (result != EXIT_SUCCESS) {
1670 /* link program file */
1671 if (mode == CompileAssembleLink) {
1672 obstack_1grow(&ldflags_obst, '\0');
1673 const char *flags = obstack_finish(&ldflags_obst);
1675 /* construct commandline */
1676 const char *linker = getenv("CPARSER_LINK");
1677 if (linker != NULL) {
1678 obstack_printf(&file_obst, "%s ", linker);
1680 if (target_triple != NULL)
1681 obstack_printf(&file_obst, "%s-", target_triple);
1682 obstack_printf(&file_obst, "%s ", LINKER);
1685 for (file_list_entry_t *entry = files; entry != NULL;
1686 entry = entry->next) {
1687 if (entry->type != FILETYPE_OBJECT)
1690 add_flag(&file_obst, "%s", entry->name);
1693 add_flag(&file_obst, "-o");
1694 add_flag(&file_obst, outname);
1695 obstack_printf(&file_obst, "%s", flags);
1696 obstack_1grow(&file_obst, '\0');
1698 char *commandline = obstack_finish(&file_obst);
1703 int err = system(commandline);
1704 if (err != EXIT_SUCCESS) {
1705 fprintf(stderr, "linker reported an error\n");
1706 return EXIT_FAILURE;
1713 obstack_free(&cppflags_obst, NULL);
1714 obstack_free(&ldflags_obst, NULL);
1715 obstack_free(&file_obst, NULL);
1725 exit_symbol_table();
1726 return EXIT_SUCCESS;