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 -U__STRICT_ANSI__"
88 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
98 #define ASSEMBLER "gcc -c -xassembler"
100 #define ASSEMBLER "as"
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;
125 static struct obstack ldflags_obst;
126 static struct obstack asflags_obst;
127 static char dep_target[1024];
128 static const char *outname;
130 typedef enum lang_standard_t {
131 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
132 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
133 STANDARD_C89, /* ISO C90 (sic) */
134 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
135 STANDARD_C99, /* ISO C99 */
136 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
137 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
138 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
139 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
142 static lang_standard_t standard;
144 typedef struct file_list_entry_t file_list_entry_t;
146 typedef enum filetype_t {
149 FILETYPE_PREPROCESSED_C,
151 FILETYPE_PREPROCESSED_CXX,
153 FILETYPE_PREPROCESSED_ASSEMBLER,
159 struct file_list_entry_t {
160 const char *name; /**< filename or NULL for stdin */
162 file_list_entry_t *next;
165 static file_list_entry_t *temp_files;
167 static void initialize_firm(void)
172 static void get_output_name(char *buf, size_t buflen, const char *inputname,
175 if (inputname == NULL)
178 char const *const last_slash = strrchr(inputname, '/');
179 char const *const filename =
180 last_slash != NULL ? last_slash + 1 : inputname;
181 char const *const last_dot = strrchr(filename, '.');
182 char const *const name_end =
183 last_dot != NULL ? last_dot : strchr(filename, '\0');
185 int const len = snprintf(buf, buflen, "%.*s%s",
186 (int)(name_end - filename), filename, newext);
188 if (len < 0 || buflen <= (size_t)len)
190 if (buflen <= (size_t)len)
192 panic("filename too long");
195 #include "gen_builtins.h"
197 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
202 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
206 lexer_open_stream(in, input_name);
209 translation_unit_t *unit = finish_parsing();
213 static void lextest(FILE *in, const char *fname)
215 lexer_open_stream(in, fname);
218 lexer_next_preprocessing_token();
219 print_token(stdout, &lexer_token);
221 } while (lexer_token.type != T_EOF);
224 static void add_flag(struct obstack *obst, const char *format, ...)
229 va_start(ap, format);
233 vsnprintf(buf, sizeof(buf), format, ap);
236 obstack_1grow(obst, ' ');
238 obstack_1grow(obst, '"');
239 obstack_grow(obst, buf, len);
240 obstack_1grow(obst, '"');
242 /* escape stuff... */
243 for (char *c = buf; *c != '\0'; ++c) {
261 obstack_1grow(obst, '\\');
264 obstack_1grow(obst, *c);
271 static const char *type_to_string(type_t *type)
273 assert(type->kind == TYPE_ATOMIC);
274 return get_atomic_kind_name(type->atomic.akind);
277 static FILE *preprocess(const char *fname, filetype_t filetype)
279 static const char *common_flags = NULL;
281 if (common_flags == NULL) {
282 obstack_1grow(&cppflags_obst, '\0');
283 const char *flags = obstack_finish(&cppflags_obst);
285 /* setup default defines */
286 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
287 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
288 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
289 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
291 add_flag(&cppflags_obst, "-U__VERSION__");
292 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
294 if (flags[0] != '\0') {
295 size_t len = strlen(flags);
296 obstack_1grow(&cppflags_obst, ' ');
297 obstack_grow(&cppflags_obst, flags, len);
299 obstack_1grow(&cppflags_obst, '\0');
300 common_flags = obstack_finish(&cppflags_obst);
303 assert(obstack_object_size(&cppflags_obst) == 0);
305 const char *preprocessor = getenv("CPARSER_PP");
306 if (preprocessor != NULL) {
307 obstack_printf(&cppflags_obst, "%s ", preprocessor);
309 if (target_triple != NULL)
310 obstack_printf(&cppflags_obst, "%s-", target_triple);
311 obstack_printf(&cppflags_obst, PREPROCESSOR);
316 add_flag(&cppflags_obst, "-std=c99");
319 add_flag(&cppflags_obst, "-std=c++98");
321 case FILETYPE_ASSEMBLER:
322 add_flag(&cppflags_obst, "-x");
323 add_flag(&cppflags_obst, "assembler-with-cpp");
328 obstack_printf(&cppflags_obst, "%s", common_flags);
330 /* handle dependency generation */
331 if (dep_target[0] != '\0') {
332 add_flag(&cppflags_obst, "-MF");
333 add_flag(&cppflags_obst, dep_target);
334 if (outname != NULL) {
335 add_flag(&cppflags_obst, "-MQ");
336 add_flag(&cppflags_obst, outname);
339 add_flag(&cppflags_obst, fname);
340 obstack_1grow(&cppflags_obst, '\0');
342 char *commandline = obstack_finish(&cppflags_obst);
346 FILE *f = popen(commandline, "r");
348 fprintf(stderr, "invoking preprocessor failed\n");
351 /* we don't really need that anymore */
352 obstack_free(&cppflags_obst, commandline);
357 static void assemble(const char *out, const char *in)
359 obstack_1grow(&asflags_obst, '\0');
360 const char *flags = obstack_finish(&asflags_obst);
362 const char *assembler = getenv("CPARSER_AS");
363 if (assembler != NULL) {
364 obstack_printf(&asflags_obst, "%s", assembler);
366 if (target_triple != NULL)
367 obstack_printf(&asflags_obst, "%s-", target_triple);
368 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
370 if (flags[0] != '\0')
371 obstack_printf(&asflags_obst, " %s", flags);
373 obstack_printf(&asflags_obst, " %s -o %s", in, out);
374 obstack_1grow(&asflags_obst, '\0');
376 char *commandline = obstack_finish(&asflags_obst);
380 int err = system(commandline);
381 if (err != EXIT_SUCCESS) {
382 fprintf(stderr, "assembler reported an error\n");
385 obstack_free(&asflags_obst, commandline);
388 static void print_file_name(const char *file)
390 add_flag(&ldflags_obst, "-print-file-name=%s", file);
392 obstack_1grow(&ldflags_obst, '\0');
393 const char *flags = obstack_finish(&ldflags_obst);
395 /* construct commandline */
396 const char *linker = getenv("CPARSER_LINK");
397 if (linker != NULL) {
398 obstack_printf(&ldflags_obst, "%s ", linker);
400 if (target_triple != NULL)
401 obstack_printf(&ldflags_obst, "%s-", target_triple);
402 obstack_printf(&ldflags_obst, "%s ", LINKER);
404 obstack_printf(&ldflags_obst, "%s ", linker);
405 obstack_printf(&ldflags_obst, "%s", flags);
406 obstack_1grow(&ldflags_obst, '\0');
408 char *commandline = obstack_finish(&ldflags_obst);
412 int err = system(commandline);
413 if (err != EXIT_SUCCESS) {
414 fprintf(stderr, "linker reported an error\n");
417 obstack_free(&ldflags_obst, commandline);
420 static const char *try_dir(const char *dir)
424 if (access(dir, R_OK | W_OK | X_OK) == 0)
429 static const char *get_tempdir(void)
431 static const char *tmpdir = NULL;
437 tmpdir = try_dir(getenv("TMPDIR"));
439 tmpdir = try_dir(getenv("TMP"));
441 tmpdir = try_dir(getenv("TEMP"));
445 tmpdir = try_dir(P_tmpdir);
449 tmpdir = try_dir("/var/tmp");
451 tmpdir = try_dir("/usr/tmp");
453 tmpdir = try_dir("/tmp");
462 /* cheap and nasty mkstemp replacement */
463 static int mkstemp(char *templ)
466 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
471 * an own version of tmpnam, which: writes in a buffer, emits no warnings
472 * during linking (like glibc/gnu ld do for tmpnam)...
474 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
476 const char *tempdir = get_tempdir();
478 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
480 int fd = mkstemp(buffer);
482 fprintf(stderr, "couldn't create temporary file: %s\n",
486 FILE *out = fdopen(fd, "w");
488 fprintf(stderr, "couldn't create temporary file FILE*\n");
492 file_list_entry_t *entry = xmalloc(sizeof(*entry));
493 memset(entry, 0, sizeof(*entry));
495 size_t name_len = strlen(buffer) + 1;
496 char *name = malloc(name_len);
497 memcpy(name, buffer, name_len);
500 entry->next = temp_files;
506 static void free_temp_files(void)
508 file_list_entry_t *entry = temp_files;
509 file_list_entry_t *next;
510 for ( ; entry != NULL; entry = next) {
514 free((char*) entry->name);
520 typedef enum compile_mode_t {
536 static void usage(const char *argv0)
538 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
541 static void print_cparser_version(void)
543 printf("cparser (%s) using libFirm (%u.%u",
544 cparser_REVISION, ir_get_version_major(),
545 ir_get_version_minor());
547 const char *revision = ir_get_version_revision();
548 if (revision[0] != 0) {
550 fputs(revision, stdout);
553 const char *build = ir_get_version_build();
556 fputs(build, stdout);
559 puts("This is free software; see the source for copying conditions. There is NO\n"
560 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
563 static void print_help(const char *argv0)
567 puts("\t-fhelp Display help about firm optimisation options");
568 puts("\t-bhelp Display help about firm backend options");
569 puts("A big number of gcc flags is also supported");
572 static void set_be_option(const char *arg)
574 int res = be_parse_arg(arg);
579 static void set_option(const char *arg)
581 int res = firm_option(arg);
586 static void copy_file(FILE *dest, FILE *input)
590 while (!feof(input) && !ferror(dest)) {
591 size_t read = fread(buf, 1, sizeof(buf), input);
592 if (fwrite(buf, 1, read, dest) != read) {
593 perror("couldn't write output");
598 static inline bool streq(char const* a, char const* b)
600 return strcmp(a, b) == 0;
603 static inline bool strstart(char const* str, char const* start)
608 } while (*str++ == *start++);
612 static FILE *open_file(const char *filename)
614 if (streq(filename, "-")) {
618 FILE *in = fopen(filename, "r");
620 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
628 static filetype_t get_filetype_from_string(const char *string)
630 if (streq(string, "c") || streq(string, "c-header"))
632 if (streq(string, "c++") || streq(string, "c++-header"))
634 if (streq(string, "assembler"))
635 return FILETYPE_PREPROCESSED_ASSEMBLER;
636 if (streq(string, "assembler-with-cpp"))
637 return FILETYPE_ASSEMBLER;
638 if (streq(string, "none"))
639 return FILETYPE_AUTODETECT;
641 return FILETYPE_UNKNOWN;
645 * Initialize firm codegeneration for a specific operating system.
646 * The argument is the operating system part of a target-triple */
647 static bool set_os_support(const char *os)
649 wchar_atomic_kind = ATOMIC_TYPE_INT;
650 force_long_double_size = 0;
651 enable_main_collect2_hack = false;
653 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
654 || streq(os, "solaris")) {
655 set_be_option("ia32-gasmode=elf");
656 set_create_ld_ident(create_name_linux_elf);
657 } else if (streq(os, "darwin")) {
658 force_long_double_size = 16;
659 set_be_option("ia32-gasmode=macho");
660 set_be_option("ia32-stackalign=4");
661 set_be_option("pic=true");
662 set_create_ld_ident(create_name_macho);
663 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
664 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
665 enable_main_collect2_hack = true;
666 set_be_option("ia32-gasmode=mingw");
667 set_create_ld_ident(create_name_win32);
675 static bool parse_target_triple(const char *arg)
677 const char *manufacturer = strchr(arg, '-');
678 if (manufacturer == NULL) {
679 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
684 const char *os = strchr(manufacturer, '-');
686 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
691 /* Note: Triples are more or less defined by what the config.guess and
692 * config.sub scripts from GNU autoconf emit. We have to lookup there what
693 * triples are possible */
695 /* process cpu type */
696 if (strstart(arg, "i386-")) {
697 be_parse_arg("isa=ia32");
698 be_parse_arg("ia32-arch=i386");
699 } else if (strstart(arg, "i486-")) {
700 be_parse_arg("isa=ia32");
701 be_parse_arg("ia32-arch=i486");
702 } else if (strstart(arg, "i586-")) {
703 be_parse_arg("isa=ia32");
704 be_parse_arg("ia32-arch=i586");
705 } else if (strstart(arg, "i686-")) {
706 be_parse_arg("isa=ia32");
707 be_parse_arg("ia32-arch=i686");
708 } else if (strstart(arg, "i786-")) {
709 be_parse_arg("isa=ia32");
710 be_parse_arg("ia32-arch=pentium4");
711 } else if (strstart(arg, "x86_64")) {
712 be_parse_arg("isa=amd64");
713 } else if (strstart(arg, "sparc-")) {
714 be_parse_arg("isa=sparc");
715 } else if (strstart(arg, "arm-")) {
716 be_parse_arg("isa=arm");
718 fprintf(stderr, "Unknown cpu in triple '%s'\n", arg);
722 /* process manufacturer, alot of people incorrectly leave out the
723 * manufacturer instead of using unknown- */
724 if (strstart(manufacturer, "linux")) {
726 manufacturer = "unknown-";
729 /* process operating system */
730 if (!set_os_support(os)) {
731 fprintf(stderr, "Unknown operating system '%s' in triple '%s'\n", os, arg);
739 int main(int argc, char **argv)
743 const char *dumpfunction = NULL;
744 const char *print_file_name_file = NULL;
745 compile_mode_t mode = CompileAssembleLink;
747 int result = EXIT_SUCCESS;
748 char cpu_arch[16] = "ia32";
749 file_list_entry_t *files = NULL;
750 file_list_entry_t *last_file = NULL;
751 bool construct_dep_target = false;
752 bool do_timing = false;
753 struct obstack file_obst;
755 atexit(free_temp_files);
757 /* hack for now... */
758 if (strstr(argv[0], "pptest") != NULL) {
759 extern int pptest_main(int argc, char **argv);
760 return pptest_main(argc, argv);
763 obstack_init(&cppflags_obst);
764 obstack_init(&ldflags_obst);
765 obstack_init(&asflags_obst);
766 obstack_init(&file_obst);
768 #define GET_ARG_AFTER(def, args) \
769 def = &arg[sizeof(args)-1]; \
770 if (def[0] == '\0') { \
773 fprintf(stderr, "error: expected argument after '" args "'\n"); \
774 argument_errors = true; \
778 if (def[0] == '-' && def[1] != '\0') { \
779 fprintf(stderr, "error: expected argument after '" args "'\n"); \
780 argument_errors = true; \
785 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
787 /* early options parsing (find out optimisation level and OS) */
788 for (int i = 1; i < argc; ++i) {
789 const char *arg = argv[i];
793 const char *option = &arg[1];
794 if (option[0] == 'O') {
795 sscanf(&option[1], "%d", &opt_level);
800 #if defined(_WIN32) || defined(__CYGWIN__)
801 set_os_support("win32");
802 #elif defined(__APPLE__)
803 set_os_support("darwin");
805 set_os_support("linux");
808 /* apply optimisation level */
811 set_option("no-opt");
814 set_option("no-inline");
818 /* use_builtins = true; */
821 set_option("thread-jumps");
822 set_option("if-conversion");
825 set_option("strict-aliasing");
826 set_option("inline");
827 set_option("deconv");
828 set_be_option("omitfp");
832 const char *target = getenv("TARGET");
834 parse_target_triple(target);
836 /* parse rest of options */
837 standard = STANDARD_DEFAULT;
838 unsigned features_on = 0;
839 unsigned features_off = 0;
840 filetype_t forced_filetype = FILETYPE_AUTODETECT;
841 bool help_displayed = false;
842 bool argument_errors = false;
843 for (int i = 1; i < argc; ++i) {
844 const char *arg = argv[i];
845 if (arg[0] == '-' && arg[1] != '\0') {
847 const char *option = &arg[1];
848 if (option[0] == 'o') {
849 GET_ARG_AFTER(outname, "-o");
850 } else if (option[0] == 'g') {
851 set_be_option("debuginfo=stabs");
852 set_be_option("omitfp=no");
853 set_be_option("ia32-nooptcc=yes");
854 } else if (SINGLE_OPTION('c')) {
855 mode = CompileAssemble;
856 } else if (SINGLE_OPTION('E')) {
857 mode = PreprocessOnly;
858 } else if (SINGLE_OPTION('S')) {
860 } else if (option[0] == 'O') {
862 } else if (option[0] == 'I') {
864 GET_ARG_AFTER(opt, "-I");
865 add_flag(&cppflags_obst, "-I%s", opt);
866 } else if (option[0] == 'D') {
868 GET_ARG_AFTER(opt, "-D");
869 add_flag(&cppflags_obst, "-D%s", opt);
870 } else if (option[0] == 'U') {
872 GET_ARG_AFTER(opt, "-U");
873 add_flag(&cppflags_obst, "-U%s", opt);
874 } else if (option[0] == 'l') {
876 GET_ARG_AFTER(opt, "-l");
877 add_flag(&ldflags_obst, "-l%s", opt);
878 } else if (option[0] == 'L') {
880 GET_ARG_AFTER(opt, "-L");
881 add_flag(&ldflags_obst, "-L%s", opt);
882 } else if (SINGLE_OPTION('v')) {
884 } else if (SINGLE_OPTION('w')) {
885 memset(&warning, 0, sizeof(warning));
886 } else if (option[0] == 'x') {
888 GET_ARG_AFTER(opt, "-x");
889 forced_filetype = get_filetype_from_string(opt);
890 if (forced_filetype == FILETYPE_UNKNOWN) {
891 fprintf(stderr, "Unknown language '%s'\n", opt);
892 argument_errors = true;
894 } else if (streq(option, "M")) {
895 mode = PreprocessOnly;
896 add_flag(&cppflags_obst, "-M");
897 } else if (streq(option, "MMD") ||
898 streq(option, "MD")) {
899 construct_dep_target = true;
900 add_flag(&cppflags_obst, "-%s", option);
901 } else if (streq(option, "MM") ||
902 streq(option, "MP")) {
903 add_flag(&cppflags_obst, "-%s", option);
904 } else if (streq(option, "MT") ||
905 streq(option, "MQ") ||
906 streq(option, "MF")) {
908 GET_ARG_AFTER(opt, "-MT");
909 add_flag(&cppflags_obst, "-%s", option);
910 add_flag(&cppflags_obst, "%s", opt);
911 } else if (streq(option, "include")) {
913 GET_ARG_AFTER(opt, "-include");
914 add_flag(&cppflags_obst, "-include");
915 add_flag(&cppflags_obst, "%s", opt);
916 } else if (streq(option, "isystem")) {
918 GET_ARG_AFTER(opt, "-isystem");
919 add_flag(&cppflags_obst, "-isystem");
920 add_flag(&cppflags_obst, "%s", opt);
921 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
922 } else if (streq(option, "pthread")) {
923 /* set flags for the preprocessor */
924 add_flag(&cppflags_obst, "-D_REENTRANT");
925 /* set flags for the linker */
926 add_flag(&ldflags_obst, "-lpthread");
928 } else if (streq(option, "nostdinc")
929 || streq(option, "trigraphs")) {
930 /* pass these through to the preprocessor */
931 add_flag(&cppflags_obst, "%s", arg);
932 } else if (streq(option, "pipe")) {
933 /* here for gcc compatibility */
934 } else if (streq(option, "static")) {
935 add_flag(&ldflags_obst, "-static");
936 } else if (streq(option, "shared")) {
937 add_flag(&ldflags_obst, "-shared");
938 } else if (option[0] == 'f') {
939 char const *orig_opt;
940 GET_ARG_AFTER(orig_opt, "-f");
942 if (strstart(orig_opt, "input-charset=")) {
943 char const* const encoding = strchr(orig_opt, '=') + 1;
944 select_input_encoding(encoding);
945 } else if (streq(orig_opt, "verbose-asm")) {
946 /* ignore: we always print verbose assembler */
948 char const *opt = orig_opt;
949 bool truth_value = true;
950 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
955 if (streq(opt, "builtins")) {
956 use_builtins = truth_value;
957 } else if (streq(opt, "dollars-in-identifiers")) {
958 allow_dollar_in_symbol = truth_value;
959 } else if (streq(opt, "omit-frame-pointer")) {
960 set_be_option(truth_value ? "omitfp" : "omitfp=no");
961 } else if (streq(opt, "short-wchar")) {
962 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
964 } else if (streq(opt, "signed-char")) {
965 char_is_signed = truth_value;
966 } else if (streq(opt, "strength-reduce")) {
967 firm_option(truth_value ? "strength-red" : "no-strength-red");
968 } else if (streq(opt, "syntax-only")) {
969 mode = truth_value ? ParseOnly : CompileAssembleLink;
970 } else if (streq(opt, "unsigned-char")) {
971 char_is_signed = !truth_value;
972 } else if (streq(opt, "freestanding")) {
974 } else if (streq(opt, "hosted")) {
975 freestanding = false;
976 } else if (truth_value == false &&
977 streq(opt, "asynchronous-unwind-tables")) {
978 /* nothing todo, a gcc feature which we don't support
979 * anyway was deactivated */
980 } else if (strstart(orig_opt, "align-loops=") ||
981 strstart(orig_opt, "align-jumps=") ||
982 strstart(orig_opt, "align-functions=")) {
983 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
984 } else if (strstart(orig_opt, "message-length=")) {
985 /* ignore: would only affect error message format */
986 } else if (streq(opt, "fast-math") ||
987 streq(opt, "jump-tables") ||
988 streq(opt, "expensive-optimizations") ||
989 streq(opt, "common") ||
990 streq(opt, "optimize-sibling-calls") ||
991 streq(opt, "align-loops") ||
992 streq(opt, "align-jumps") ||
993 streq(opt, "align-functions") ||
995 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
997 int res = firm_option(orig_opt);
999 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1001 argument_errors = true;
1003 } else if (res == -1) {
1004 help_displayed = true;
1008 } else if (option[0] == 'b') {
1010 GET_ARG_AFTER(opt, "-b");
1011 int res = be_parse_arg(opt);
1013 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1015 argument_errors = true;
1016 } else if (res == -1) {
1017 help_displayed = true;
1018 } else if (strstart(opt, "isa=")) {
1019 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1021 } else if (option[0] == 'W') {
1022 if (option[1] == '\0') {
1023 /* ignore -W, our defaults are already quite verbose */
1024 } else if (strstart(option + 1, "p,")) {
1025 // pass options directly to the preprocessor
1027 GET_ARG_AFTER(opt, "-Wp,");
1028 add_flag(&cppflags_obst, "-Wp,%s", opt);
1029 } else if (strstart(option + 1, "l,")) {
1030 // pass options directly to the linker
1032 GET_ARG_AFTER(opt, "-Wl,");
1033 add_flag(&ldflags_obst, "-Wl,%s", opt);
1034 } else if (streq(option + 1, "no-trigraphs")
1035 || streq(option + 1, "undef")) {
1036 add_flag(&cppflags_obst, "%s", arg);
1038 set_warning_opt(&option[1]);
1040 } else if (option[0] == 'm') {
1045 GET_ARG_AFTER(opt, "-m");
1046 if (strstart(opt, "target=")) {
1047 GET_ARG_AFTER(opt, "-mtarget=");
1048 if (!parse_target_triple(opt))
1049 argument_errors = true;
1050 } else if (strstart(opt, "triple=")) {
1051 GET_ARG_AFTER(opt, "-mtriple=");
1052 if (!parse_target_triple(opt))
1053 argument_errors = true;
1054 } else if (strstart(opt, "arch=")) {
1055 GET_ARG_AFTER(opt, "-march=");
1056 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1057 int res = be_parse_arg(arch_opt);
1059 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1060 argument_errors = true;
1062 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1063 int res = be_parse_arg(arch_opt);
1065 argument_errors = true;
1067 } else if (strstart(opt, "tune=")) {
1068 GET_ARG_AFTER(opt, "-mtune=");
1069 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1070 int res = be_parse_arg(arch_opt);
1072 argument_errors = true;
1073 } else if (strstart(opt, "cpu=")) {
1074 GET_ARG_AFTER(opt, "-mcpu=");
1075 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1076 int res = be_parse_arg(arch_opt);
1078 argument_errors = true;
1079 } else if (strstart(opt, "fpmath=")) {
1080 GET_ARG_AFTER(opt, "-mfpmath=");
1081 if (streq(opt, "387"))
1083 else if (streq(opt, "sse"))
1086 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1087 argument_errors = true;
1089 if (!argument_errors) {
1090 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1091 int res = be_parse_arg(arch_opt);
1093 argument_errors = true;
1095 } else if (strstart(opt, "preferred-stack-boundary=")) {
1096 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1097 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1098 int res = be_parse_arg(arch_opt);
1100 argument_errors = true;
1101 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1102 set_be_option("omitleaffp=1");
1103 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1104 set_be_option("omitleaffp=0");
1105 } else if (streq(opt, "rtd")) {
1106 default_calling_convention = CC_STDCALL;
1107 } else if (strstart(opt, "regparm=")) {
1108 fprintf(stderr, "error: regparm convention not supported yet\n");
1109 argument_errors = true;
1110 } else if (streq(opt, "soft-float")) {
1111 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1112 argument_errors = true;
1114 long int value = strtol(opt, NULL, 10);
1116 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1117 argument_errors = true;
1118 } else if (value != 16 && value != 32 && value != 64) {
1119 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1120 argument_errors = true;
1122 machine_size = (unsigned int)value;
1123 add_flag(&asflags_obst, "--%u", machine_size);
1124 add_flag(&ldflags_obst, "-m%u", machine_size);
1127 } else if (streq(option, "pg")) {
1128 set_be_option("gprof");
1129 add_flag(&ldflags_obst, "-pg");
1130 } else if (streq(option, "pedantic") ||
1131 streq(option, "ansi")) {
1132 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1133 } else if (streq(option, "shared")) {
1134 add_flag(&ldflags_obst, "-shared");
1135 } else if (strstart(option, "std=")) {
1136 const char *const o = &option[4];
1138 streq(o, "c++") ? STANDARD_CXX98 :
1139 streq(o, "c++98") ? STANDARD_CXX98 :
1140 streq(o, "c89") ? STANDARD_C89 :
1141 streq(o, "c99") ? STANDARD_C99 :
1142 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1143 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1144 streq(o, "gnu89") ? STANDARD_GNU89 :
1145 streq(o, "gnu99") ? STANDARD_GNU99 :
1146 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1147 streq(o, "iso9899:1990") ? STANDARD_C89 :
1148 streq(o, "iso9899:199409") ? STANDARD_C90 :
1149 streq(o, "iso9899:1999") ? STANDARD_C99 :
1150 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1151 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1152 } else if (streq(option, "version")) {
1153 print_cparser_version();
1154 } else if (strstart(option, "print-file-name=")) {
1155 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1156 } else if (option[0] == '-') {
1157 /* double dash option */
1159 if (streq(option, "gcc")) {
1160 features_on |= _GNUC;
1161 features_off &= ~_GNUC;
1162 } else if (streq(option, "no-gcc")) {
1163 features_on &= ~_GNUC;
1164 features_off |= _GNUC;
1165 } else if (streq(option, "ms")) {
1167 features_off &= ~_MS;
1168 } else if (streq(option, "no-ms")) {
1169 features_on &= ~_MS;
1170 features_off |= _MS;
1171 } else if (streq(option, "strict")) {
1173 } else if (streq(option, "lextest")) {
1175 } else if (streq(option, "benchmark")) {
1176 mode = BenchmarkParser;
1177 } else if (streq(option, "print-ast")) {
1179 } else if (streq(option, "print-implicit-cast")) {
1180 print_implicit_casts = true;
1181 } else if (streq(option, "print-parenthesis")) {
1182 print_parenthesis = true;
1183 } else if (streq(option, "print-fluffy")) {
1185 } else if (streq(option, "print-caml")) {
1187 } else if (streq(option, "print-jna")) {
1189 } else if (streq(option, "jna-limit")) {
1192 fprintf(stderr, "error: "
1193 "expected argument after '--jna-limit'\n");
1194 argument_errors = true;
1197 jna_limit_output(argv[i]);
1198 } else if (streq(option, "jna-libname")) {
1201 fprintf(stderr, "error: "
1202 "expected argument after '--jna-libname'\n");
1203 argument_errors = true;
1206 jna_set_libname(argv[i]);
1207 } else if (streq(option, "time")) {
1209 } else if (streq(option, "version")) {
1210 print_cparser_version();
1211 return EXIT_SUCCESS;
1212 } else if (streq(option, "help")) {
1213 print_help(argv[0]);
1214 help_displayed = true;
1215 } else if (streq(option, "dump-function")) {
1218 fprintf(stderr, "error: "
1219 "expected argument after '--dump-function'\n");
1220 argument_errors = true;
1223 dumpfunction = argv[i];
1225 } else if (streq(option, "export-ir")) {
1226 mode = CompileExportIR;
1228 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1229 argument_errors = true;
1232 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1233 argument_errors = true;
1236 filetype_t type = forced_filetype;
1237 if (type == FILETYPE_AUTODETECT) {
1238 if (streq(arg, "-")) {
1239 /* - implicitly means C source file */
1242 const char *suffix = strrchr(arg, '.');
1243 /* Ensure there is at least one char before the suffix */
1244 if (suffix != NULL && suffix != arg) {
1247 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1248 streq(suffix, "a") ? FILETYPE_OBJECT :
1249 streq(suffix, "c") ? FILETYPE_C :
1250 streq(suffix, "C") ? FILETYPE_CXX :
1251 streq(suffix, "cc") ? FILETYPE_CXX :
1252 streq(suffix, "cp") ? FILETYPE_CXX :
1253 streq(suffix, "cpp") ? FILETYPE_CXX :
1254 streq(suffix, "CPP") ? FILETYPE_CXX :
1255 streq(suffix, "cxx") ? FILETYPE_CXX :
1256 streq(suffix, "c++") ? FILETYPE_CXX :
1257 streq(suffix, "ii") ? FILETYPE_CXX :
1258 streq(suffix, "h") ? FILETYPE_C :
1259 streq(suffix, "ir") ? FILETYPE_IR :
1260 streq(suffix, "o") ? FILETYPE_OBJECT :
1261 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1262 streq(suffix, "so") ? FILETYPE_OBJECT :
1263 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1268 file_list_entry_t *entry
1269 = obstack_alloc(&file_obst, sizeof(entry[0]));
1270 memset(entry, 0, sizeof(entry[0]));
1274 if (last_file != NULL) {
1275 last_file->next = entry;
1283 if (help_displayed) {
1284 return !argument_errors;
1287 if (print_file_name_file != NULL) {
1288 print_file_name(print_file_name_file);
1289 return EXIT_SUCCESS;
1291 if (files == NULL) {
1292 fprintf(stderr, "error: no input files specified\n");
1293 argument_errors = true;
1296 if (argument_errors) {
1298 return EXIT_FAILURE;
1301 /* we do the lowering in ast2firm */
1302 firm_opt.lower_bitfields = FALSE;
1304 /* set the c_mode here, types depends on it */
1305 c_mode |= features_on;
1306 c_mode &= ~features_off;
1309 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1310 init_symbol_table();
1323 if (construct_dep_target) {
1324 if (outname != 0 && strlen(outname) >= 2) {
1325 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1327 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1330 dep_target[0] = '\0';
1333 char outnamebuf[4096];
1334 if (outname == NULL) {
1335 const char *filename = files->name;
1338 case BenchmarkParser:
1344 case PreprocessOnly:
1349 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1350 outname = outnamebuf;
1352 case CompileAssemble:
1353 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1354 outname = outnamebuf;
1357 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1359 outname = outnamebuf;
1361 case CompileExportIR:
1362 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1363 outname = outnamebuf;
1365 case CompileAssembleLink:
1375 assert(outname != NULL);
1378 if (streq(outname, "-")) {
1381 out = fopen(outname, "w");
1383 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1385 return EXIT_FAILURE;
1389 file_list_entry_t *file;
1390 bool already_constructed_firm = false;
1391 for (file = files; file != NULL; file = file->next) {
1392 char asm_tempfile[1024];
1393 const char *filename = file->name;
1394 filetype_t filetype = file->type;
1396 if (filetype == FILETYPE_OBJECT)
1400 if (mode == LexTest) {
1402 in = open_file(filename);
1403 lextest(in, filename);
1405 return EXIT_SUCCESS;
1408 FILE *preprocessed_in = NULL;
1409 filetype_t next_filetype = filetype;
1412 next_filetype = FILETYPE_PREPROCESSED_C;
1415 next_filetype = FILETYPE_PREPROCESSED_CXX;
1417 case FILETYPE_ASSEMBLER:
1418 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1421 /* no support for input on FILE* yet */
1423 panic("internal compiler error: in for preprocessor != NULL");
1425 preprocessed_in = preprocess(filename, filetype);
1426 if (mode == PreprocessOnly) {
1427 copy_file(out, preprocessed_in);
1428 int result = pclose(preprocessed_in);
1430 /* remove output file in case of error */
1431 if (out != stdout && result != EXIT_SUCCESS) {
1437 in = preprocessed_in;
1438 filetype = next_filetype;
1446 if (mode == Compile) {
1449 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1453 in = open_file(filename);
1455 /* preprocess and compile */
1456 if (filetype == FILETYPE_PREPROCESSED_C) {
1457 char const* invalid_mode;
1460 case STANDARD_C89: c_mode = _C89; break;
1461 /* TODO determine difference between these two */
1462 case STANDARD_C90: c_mode = _C89; break;
1463 case STANDARD_C99: c_mode = _C89 | _C99; break;
1464 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1468 "warning: command line option \"-std=%s\" is not valid for C\n",
1471 case STANDARD_DEFAULT:
1472 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1474 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1475 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1478 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1479 char const* invalid_mode;
1481 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1482 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1483 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1484 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1485 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1488 case STANDARD_CXX98: c_mode = _CXX; break;
1492 "warning: command line option \"-std=%s\" is not valid for C++\n",
1494 case STANDARD_DEFAULT:
1495 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1499 c_mode |= features_on;
1500 c_mode &= ~features_off;
1502 /* do the actual parsing */
1503 ir_timer_t *t_parsing = ir_timer_new();
1504 timer_register(t_parsing, "Frontend: Parsing");
1505 timer_push(t_parsing);
1507 translation_unit_t *const unit = do_parsing(in, filename);
1508 timer_pop(t_parsing);
1510 /* prints the AST even if errors occurred */
1511 if (mode == PrintAst) {
1516 if (error_count > 0) {
1517 /* parsing failed because of errors */
1518 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1520 result = EXIT_FAILURE;
1522 } else if (warning_count > 0) {
1523 fprintf(stderr, "%u warning(s)\n", warning_count);
1526 if (in == preprocessed_in) {
1527 int pp_result = pclose(preprocessed_in);
1528 if (pp_result != EXIT_SUCCESS) {
1529 /* remove output file */
1532 return EXIT_FAILURE;
1536 if (mode == BenchmarkParser) {
1538 } else if (mode == PrintFluffy) {
1539 write_fluffy_decls(out, unit);
1541 } else if (mode == PrintCaml) {
1542 write_caml_decls(out, unit);
1544 } else if (mode == PrintJna) {
1545 write_jna_decls(out, unit);
1549 /* build the firm graph */
1550 ir_timer_t *t_construct = ir_timer_new();
1551 timer_register(t_construct, "Frontend: Graph construction");
1552 timer_push(t_construct);
1553 if (already_constructed_firm) {
1554 panic("compiling multiple files/translation units not possible");
1556 translation_unit_to_firm(unit);
1557 already_constructed_firm = true;
1558 timer_pop(t_construct);
1561 if (mode == ParseOnly) {
1565 if (mode == CompileDump) {
1567 ident *id = new_id_from_str(dumpfunction);
1568 ir_graph *irg = NULL;
1569 int n_irgs = get_irp_n_irgs();
1570 for (int i = 0; i < n_irgs; ++i) {
1571 ir_graph *tirg = get_irp_irg(i);
1572 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1580 fprintf(stderr, "No graph for function '%s' found\n",
1582 return EXIT_FAILURE;
1585 dump_ir_graph_file(out, irg);
1587 return EXIT_SUCCESS;
1590 if (mode == CompileExportIR) {
1593 return EXIT_SUCCESS;
1596 gen_firm_finish(asm_out, filename, have_const_functions);
1597 if (asm_out != out) {
1600 } else if (filetype == FILETYPE_IR) {
1602 ir_import(filename);
1604 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1605 copy_file(asm_out, in);
1606 if (in == preprocessed_in) {
1607 int pp_result = pclose(preprocessed_in);
1608 if (pp_result != EXIT_SUCCESS) {
1609 /* remove output in error case */
1615 if (asm_out != out) {
1620 if (mode == Compile)
1623 /* if we're here then we have preprocessed assembly */
1624 filename = asm_tempfile;
1625 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1628 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1630 const char *filename_o;
1631 if (mode == CompileAssemble) {
1633 filename_o = outname;
1635 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1640 assemble(filename_o, filename);
1642 size_t len = strlen(filename_o) + 1;
1643 filename = obstack_copy(&file_obst, filename_o, len);
1644 filetype = FILETYPE_OBJECT;
1647 /* ok we're done here, process next file */
1648 file->name = filename;
1649 file->type = filetype;
1652 if (result != EXIT_SUCCESS) {
1658 /* link program file */
1659 if (mode == CompileAssembleLink) {
1660 obstack_1grow(&ldflags_obst, '\0');
1661 const char *flags = obstack_finish(&ldflags_obst);
1663 /* construct commandline */
1664 const char *linker = getenv("CPARSER_LINK");
1665 if (linker != NULL) {
1666 obstack_printf(&file_obst, "%s ", linker);
1668 if (target_triple != NULL)
1669 obstack_printf(&file_obst, "%s-", target_triple);
1670 obstack_printf(&file_obst, "%s ", LINKER);
1673 for (file_list_entry_t *entry = files; entry != NULL;
1674 entry = entry->next) {
1675 if (entry->type != FILETYPE_OBJECT)
1678 add_flag(&file_obst, "%s", entry->name);
1681 add_flag(&file_obst, "-o");
1682 add_flag(&file_obst, outname);
1683 obstack_printf(&file_obst, "%s", flags);
1684 obstack_1grow(&file_obst, '\0');
1686 char *commandline = obstack_finish(&file_obst);
1691 int err = system(commandline);
1692 if (err != EXIT_SUCCESS) {
1693 fprintf(stderr, "linker reported an error\n");
1694 return EXIT_FAILURE;
1701 obstack_free(&cppflags_obst, NULL);
1702 obstack_free(&ldflags_obst, NULL);
1703 obstack_free(&asflags_obst, NULL);
1704 obstack_free(&file_obst, NULL);
1714 exit_symbol_table();
1715 return EXIT_SUCCESS;