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 "adt/strutil.h"
77 #include "wrappergen/write_fluffy.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 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 FILE *open_file(const char *filename)
598 if (streq(filename, "-")) {
602 FILE *in = fopen(filename, "r");
604 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
612 static filetype_t get_filetype_from_string(const char *string)
614 if (streq(string, "c") || streq(string, "c-header"))
616 if (streq(string, "c++") || streq(string, "c++-header"))
618 if (streq(string, "assembler"))
619 return FILETYPE_PREPROCESSED_ASSEMBLER;
620 if (streq(string, "assembler-with-cpp"))
621 return FILETYPE_ASSEMBLER;
622 if (streq(string, "none"))
623 return FILETYPE_AUTODETECT;
625 return FILETYPE_UNKNOWN;
629 * Initialize firm codegeneration for a specific operating system.
630 * The argument is the operating system part of a target-triple */
631 static bool set_os_support(const char *os)
633 wchar_atomic_kind = ATOMIC_TYPE_INT;
634 force_long_double_size = 0;
635 enable_main_collect2_hack = false;
637 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
638 || streq(os, "solaris")) {
639 set_be_option("ia32-gasmode=elf");
640 set_create_ld_ident(create_name_linux_elf);
641 } else if (streq(os, "darwin")) {
642 force_long_double_size = 16;
643 set_be_option("ia32-gasmode=macho");
644 set_be_option("ia32-stackalign=4");
645 set_be_option("pic=true");
646 set_create_ld_ident(create_name_macho);
647 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
648 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
649 enable_main_collect2_hack = true;
650 set_be_option("ia32-gasmode=mingw");
651 set_create_ld_ident(create_name_win32);
659 static bool parse_target_triple(const char *arg)
661 const char *manufacturer = strchr(arg, '-');
662 if (manufacturer == NULL) {
663 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
668 const char *os = strchr(manufacturer, '-');
670 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
675 /* Note: Triples are more or less defined by what the config.guess and
676 * config.sub scripts from GNU autoconf emit. We have to lookup there what
677 * triples are possible */
679 /* process cpu type */
680 if (strstart(arg, "i386-")) {
681 be_parse_arg("isa=ia32");
682 be_parse_arg("ia32-arch=i386");
683 } else if (strstart(arg, "i486-")) {
684 be_parse_arg("isa=ia32");
685 be_parse_arg("ia32-arch=i486");
686 } else if (strstart(arg, "i586-")) {
687 be_parse_arg("isa=ia32");
688 be_parse_arg("ia32-arch=i586");
689 } else if (strstart(arg, "i686-")) {
690 be_parse_arg("isa=ia32");
691 be_parse_arg("ia32-arch=i686");
692 } else if (strstart(arg, "i786-")) {
693 be_parse_arg("isa=ia32");
694 be_parse_arg("ia32-arch=pentium4");
695 } else if (strstart(arg, "x86_64")) {
696 be_parse_arg("isa=amd64");
697 } else if (strstart(arg, "sparc-")) {
698 be_parse_arg("isa=sparc");
699 } else if (strstart(arg, "arm-")) {
700 be_parse_arg("isa=arm");
702 fprintf(stderr, "Unknown cpu in triple '%s'\n", arg);
706 /* process manufacturer, alot of people incorrectly leave out the
707 * manufacturer instead of using unknown- */
708 if (strstart(manufacturer, "linux")) {
710 manufacturer = "unknown-";
713 /* process operating system */
714 if (!set_os_support(os)) {
715 fprintf(stderr, "Unknown operating system '%s' in triple '%s'\n", os, arg);
723 int main(int argc, char **argv)
727 const char *dumpfunction = NULL;
728 const char *print_file_name_file = NULL;
729 compile_mode_t mode = CompileAssembleLink;
731 int result = EXIT_SUCCESS;
732 char cpu_arch[16] = "ia32";
733 file_list_entry_t *files = NULL;
734 file_list_entry_t *last_file = NULL;
735 bool construct_dep_target = false;
736 bool do_timing = false;
737 struct obstack file_obst;
739 atexit(free_temp_files);
741 /* hack for now... */
742 if (strstr(argv[0], "pptest") != NULL) {
743 extern int pptest_main(int argc, char **argv);
744 return pptest_main(argc, argv);
747 obstack_init(&cppflags_obst);
748 obstack_init(&ldflags_obst);
749 obstack_init(&asflags_obst);
750 obstack_init(&file_obst);
752 #define GET_ARG_AFTER(def, args) \
753 def = &arg[sizeof(args)-1]; \
754 if (def[0] == '\0') { \
757 fprintf(stderr, "error: expected argument after '" args "'\n"); \
758 argument_errors = true; \
762 if (def[0] == '-' && def[1] != '\0') { \
763 fprintf(stderr, "error: expected argument after '" args "'\n"); \
764 argument_errors = true; \
769 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
771 /* early options parsing (find out optimisation level and OS) */
772 for (int i = 1; i < argc; ++i) {
773 const char *arg = argv[i];
777 const char *option = &arg[1];
778 if (option[0] == 'O') {
779 sscanf(&option[1], "%d", &opt_level);
784 #if defined(_WIN32) || defined(__CYGWIN__)
785 set_os_support("win32");
786 #elif defined(__APPLE__)
787 set_os_support("darwin");
789 set_os_support("linux");
792 /* apply optimisation level */
795 set_option("no-opt");
798 set_option("no-inline");
802 /* use_builtins = true; */
805 set_option("thread-jumps");
806 set_option("if-conversion");
809 set_option("strict-aliasing");
810 set_option("inline");
811 set_option("fp-vrp");
812 set_option("deconv");
813 set_be_option("omitfp");
817 const char *target = getenv("TARGET");
819 parse_target_triple(target);
821 /* parse rest of options */
822 standard = STANDARD_DEFAULT;
823 unsigned features_on = 0;
824 unsigned features_off = 0;
825 filetype_t forced_filetype = FILETYPE_AUTODETECT;
826 bool help_displayed = false;
827 bool argument_errors = false;
828 for (int i = 1; i < argc; ++i) {
829 const char *arg = argv[i];
830 if (arg[0] == '-' && arg[1] != '\0') {
832 const char *option = &arg[1];
833 if (option[0] == 'o') {
834 GET_ARG_AFTER(outname, "-o");
835 } else if (option[0] == 'g') {
836 set_be_option("debuginfo=stabs");
837 set_be_option("omitfp=no");
838 set_be_option("ia32-nooptcc=yes");
839 } else if (SINGLE_OPTION('c')) {
840 mode = CompileAssemble;
841 } else if (SINGLE_OPTION('E')) {
842 mode = PreprocessOnly;
843 } else if (SINGLE_OPTION('S')) {
845 } else if (option[0] == 'O') {
847 } else if (option[0] == 'I') {
849 GET_ARG_AFTER(opt, "-I");
850 add_flag(&cppflags_obst, "-I%s", opt);
851 } else if (option[0] == 'D') {
853 GET_ARG_AFTER(opt, "-D");
854 add_flag(&cppflags_obst, "-D%s", opt);
855 } else if (option[0] == 'U') {
857 GET_ARG_AFTER(opt, "-U");
858 add_flag(&cppflags_obst, "-U%s", opt);
859 } else if (option[0] == 'l') {
861 GET_ARG_AFTER(opt, "-l");
862 add_flag(&ldflags_obst, "-l%s", opt);
863 } else if (option[0] == 'L') {
865 GET_ARG_AFTER(opt, "-L");
866 add_flag(&ldflags_obst, "-L%s", opt);
867 } else if (SINGLE_OPTION('v')) {
869 } else if (SINGLE_OPTION('w')) {
870 memset(&warning, 0, sizeof(warning));
871 } else if (option[0] == 'x') {
873 GET_ARG_AFTER(opt, "-x");
874 forced_filetype = get_filetype_from_string(opt);
875 if (forced_filetype == FILETYPE_UNKNOWN) {
876 fprintf(stderr, "Unknown language '%s'\n", opt);
877 argument_errors = true;
879 } else if (streq(option, "M")) {
880 mode = PreprocessOnly;
881 add_flag(&cppflags_obst, "-M");
882 } else if (streq(option, "MMD") ||
883 streq(option, "MD")) {
884 construct_dep_target = true;
885 add_flag(&cppflags_obst, "-%s", option);
886 } else if (streq(option, "MM") ||
887 streq(option, "MP")) {
888 add_flag(&cppflags_obst, "-%s", option);
889 } else if (streq(option, "MT") ||
890 streq(option, "MQ") ||
891 streq(option, "MF")) {
893 GET_ARG_AFTER(opt, "-MT");
894 add_flag(&cppflags_obst, "-%s", option);
895 add_flag(&cppflags_obst, "%s", opt);
896 } else if (streq(option, "include")) {
898 GET_ARG_AFTER(opt, "-include");
899 add_flag(&cppflags_obst, "-include");
900 add_flag(&cppflags_obst, "%s", opt);
901 } else if (streq(option, "isystem")) {
903 GET_ARG_AFTER(opt, "-isystem");
904 add_flag(&cppflags_obst, "-isystem");
905 add_flag(&cppflags_obst, "%s", opt);
906 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
907 } else if (streq(option, "pthread")) {
908 /* set flags for the preprocessor */
909 add_flag(&cppflags_obst, "-D_REENTRANT");
910 /* set flags for the linker */
911 add_flag(&ldflags_obst, "-lpthread");
913 } else if (streq(option, "nostdinc")
914 || streq(option, "trigraphs")) {
915 /* pass these through to the preprocessor */
916 add_flag(&cppflags_obst, "%s", arg);
917 } else if (streq(option, "pipe")) {
918 /* here for gcc compatibility */
919 } else if (streq(option, "static")) {
920 add_flag(&ldflags_obst, "-static");
921 } else if (streq(option, "shared")) {
922 add_flag(&ldflags_obst, "-shared");
923 } else if (option[0] == 'f') {
924 char const *orig_opt;
925 GET_ARG_AFTER(orig_opt, "-f");
927 if (strstart(orig_opt, "input-charset=")) {
928 char const* const encoding = strchr(orig_opt, '=') + 1;
929 select_input_encoding(encoding);
930 } else if (streq(orig_opt, "verbose-asm")) {
931 /* ignore: we always print verbose assembler */
933 char const *opt = orig_opt;
934 bool truth_value = true;
935 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
940 if (streq(opt, "builtins")) {
941 use_builtins = truth_value;
942 } else if (streq(opt, "dollars-in-identifiers")) {
943 allow_dollar_in_symbol = truth_value;
944 } else if (streq(opt, "omit-frame-pointer")) {
945 set_be_option(truth_value ? "omitfp" : "omitfp=no");
946 } else if (streq(opt, "short-wchar")) {
947 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
949 } else if (streq(opt, "signed-char")) {
950 char_is_signed = truth_value;
951 } else if (streq(opt, "strength-reduce")) {
952 firm_option(truth_value ? "strength-red" : "no-strength-red");
953 } else if (streq(opt, "syntax-only")) {
954 mode = truth_value ? ParseOnly : CompileAssembleLink;
955 } else if (streq(opt, "unsigned-char")) {
956 char_is_signed = !truth_value;
957 } else if (streq(opt, "freestanding")) {
959 } else if (streq(opt, "hosted")) {
960 freestanding = false;
961 } else if (truth_value == false &&
962 streq(opt, "asynchronous-unwind-tables")) {
963 /* nothing todo, a gcc feature which we don't support
964 * anyway was deactivated */
965 } else if (strstart(orig_opt, "align-loops=") ||
966 strstart(orig_opt, "align-jumps=") ||
967 strstart(orig_opt, "align-functions=")) {
968 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
969 } else if (strstart(orig_opt, "message-length=")) {
970 /* ignore: would only affect error message format */
971 } else if (streq(opt, "fast-math") ||
972 streq(opt, "jump-tables") ||
973 streq(opt, "expensive-optimizations") ||
974 streq(opt, "common") ||
975 streq(opt, "optimize-sibling-calls") ||
976 streq(opt, "align-loops") ||
977 streq(opt, "align-jumps") ||
978 streq(opt, "align-functions") ||
980 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
982 int res = firm_option(orig_opt);
984 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
986 argument_errors = true;
988 } else if (res == -1) {
989 help_displayed = true;
993 } else if (option[0] == 'b') {
995 GET_ARG_AFTER(opt, "-b");
996 int res = be_parse_arg(opt);
998 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1000 argument_errors = true;
1001 } else if (res == -1) {
1002 help_displayed = true;
1003 } else if (strstart(opt, "isa=")) {
1004 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1006 } else if (option[0] == 'W') {
1007 if (option[1] == '\0') {
1008 /* ignore -W, our defaults are already quite verbose */
1009 } else if (strstart(option + 1, "p,")) {
1010 // pass options directly to the preprocessor
1012 GET_ARG_AFTER(opt, "-Wp,");
1013 add_flag(&cppflags_obst, "-Wp,%s", opt);
1014 } else if (strstart(option + 1, "l,")) {
1015 // pass options directly to the linker
1017 GET_ARG_AFTER(opt, "-Wl,");
1018 add_flag(&ldflags_obst, "-Wl,%s", opt);
1019 } else if (streq(option + 1, "no-trigraphs")
1020 || streq(option + 1, "undef")) {
1021 add_flag(&cppflags_obst, "%s", arg);
1023 set_warning_opt(&option[1]);
1025 } else if (option[0] == 'm') {
1030 GET_ARG_AFTER(opt, "-m");
1031 if (strstart(opt, "target=")) {
1032 GET_ARG_AFTER(opt, "-mtarget=");
1033 if (!parse_target_triple(opt))
1034 argument_errors = true;
1035 } else if (strstart(opt, "triple=")) {
1036 GET_ARG_AFTER(opt, "-mtriple=");
1037 if (!parse_target_triple(opt))
1038 argument_errors = true;
1039 } else if (strstart(opt, "arch=")) {
1040 GET_ARG_AFTER(opt, "-march=");
1041 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1042 int res = be_parse_arg(arch_opt);
1044 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1045 argument_errors = true;
1047 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1048 int res = be_parse_arg(arch_opt);
1050 argument_errors = true;
1052 } else if (strstart(opt, "tune=")) {
1053 GET_ARG_AFTER(opt, "-mtune=");
1054 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1055 int res = be_parse_arg(arch_opt);
1057 argument_errors = true;
1058 } else if (strstart(opt, "cpu=")) {
1059 GET_ARG_AFTER(opt, "-mcpu=");
1060 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1061 int res = be_parse_arg(arch_opt);
1063 argument_errors = true;
1064 } else if (strstart(opt, "fpmath=")) {
1065 GET_ARG_AFTER(opt, "-mfpmath=");
1066 if (streq(opt, "387"))
1068 else if (streq(opt, "sse"))
1071 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1072 argument_errors = true;
1074 if (!argument_errors) {
1075 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1076 int res = be_parse_arg(arch_opt);
1078 argument_errors = true;
1080 } else if (strstart(opt, "preferred-stack-boundary=")) {
1081 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1082 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1083 int res = be_parse_arg(arch_opt);
1085 argument_errors = true;
1086 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1087 set_be_option("omitleaffp=1");
1088 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1089 set_be_option("omitleaffp=0");
1090 } else if (streq(opt, "rtd")) {
1091 default_calling_convention = CC_STDCALL;
1092 } else if (strstart(opt, "regparm=")) {
1093 fprintf(stderr, "error: regparm convention not supported yet\n");
1094 argument_errors = true;
1095 } else if (streq(opt, "soft-float")) {
1096 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1097 argument_errors = true;
1099 long int value = strtol(opt, NULL, 10);
1101 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1102 argument_errors = true;
1103 } else if (value != 16 && value != 32 && value != 64) {
1104 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1105 argument_errors = true;
1107 machine_size = (unsigned int)value;
1108 add_flag(&asflags_obst, "--%u", machine_size);
1109 add_flag(&ldflags_obst, "-m%u", machine_size);
1112 } else if (streq(option, "pg")) {
1113 set_be_option("gprof");
1114 add_flag(&ldflags_obst, "-pg");
1115 } else if (streq(option, "pedantic") ||
1116 streq(option, "ansi")) {
1117 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1118 } else if (streq(option, "shared")) {
1119 add_flag(&ldflags_obst, "-shared");
1120 } else if (strstart(option, "std=")) {
1121 const char *const o = &option[4];
1123 streq(o, "c++") ? STANDARD_CXX98 :
1124 streq(o, "c++98") ? STANDARD_CXX98 :
1125 streq(o, "c89") ? STANDARD_C89 :
1126 streq(o, "c99") ? STANDARD_C99 :
1127 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1128 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1129 streq(o, "gnu89") ? STANDARD_GNU89 :
1130 streq(o, "gnu99") ? STANDARD_GNU99 :
1131 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1132 streq(o, "iso9899:1990") ? STANDARD_C89 :
1133 streq(o, "iso9899:199409") ? STANDARD_C90 :
1134 streq(o, "iso9899:1999") ? STANDARD_C99 :
1135 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1136 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1137 } else if (streq(option, "version")) {
1138 print_cparser_version();
1139 } else if (strstart(option, "print-file-name=")) {
1140 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1141 } else if (option[0] == '-') {
1142 /* double dash option */
1144 if (streq(option, "gcc")) {
1145 features_on |= _GNUC;
1146 features_off &= ~_GNUC;
1147 } else if (streq(option, "no-gcc")) {
1148 features_on &= ~_GNUC;
1149 features_off |= _GNUC;
1150 } else if (streq(option, "ms")) {
1152 features_off &= ~_MS;
1153 } else if (streq(option, "no-ms")) {
1154 features_on &= ~_MS;
1155 features_off |= _MS;
1156 } else if (streq(option, "strict")) {
1158 } else if (streq(option, "lextest")) {
1160 } else if (streq(option, "benchmark")) {
1161 mode = BenchmarkParser;
1162 } else if (streq(option, "print-ast")) {
1164 } else if (streq(option, "print-implicit-cast")) {
1165 print_implicit_casts = true;
1166 } else if (streq(option, "print-parenthesis")) {
1167 print_parenthesis = true;
1168 } else if (streq(option, "print-fluffy")) {
1170 } else if (streq(option, "print-jna")) {
1172 } else if (streq(option, "jna-limit")) {
1175 fprintf(stderr, "error: "
1176 "expected argument after '--jna-limit'\n");
1177 argument_errors = true;
1180 jna_limit_output(argv[i]);
1181 } else if (streq(option, "jna-libname")) {
1184 fprintf(stderr, "error: "
1185 "expected argument after '--jna-libname'\n");
1186 argument_errors = true;
1189 jna_set_libname(argv[i]);
1190 } else if (streq(option, "time")) {
1192 } else if (streq(option, "version")) {
1193 print_cparser_version();
1194 return EXIT_SUCCESS;
1195 } else if (streq(option, "help")) {
1196 print_help(argv[0]);
1197 help_displayed = true;
1198 } else if (streq(option, "dump-function")) {
1201 fprintf(stderr, "error: "
1202 "expected argument after '--dump-function'\n");
1203 argument_errors = true;
1206 dumpfunction = argv[i];
1208 } else if (streq(option, "export-ir")) {
1209 mode = CompileExportIR;
1211 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1212 argument_errors = true;
1215 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1216 argument_errors = true;
1219 filetype_t type = forced_filetype;
1220 if (type == FILETYPE_AUTODETECT) {
1221 if (streq(arg, "-")) {
1222 /* - implicitly means C source file */
1225 const char *suffix = strrchr(arg, '.');
1226 /* Ensure there is at least one char before the suffix */
1227 if (suffix != NULL && suffix != arg) {
1230 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1231 streq(suffix, "a") ? FILETYPE_OBJECT :
1232 streq(suffix, "c") ? FILETYPE_C :
1233 streq(suffix, "C") ? FILETYPE_CXX :
1234 streq(suffix, "cc") ? FILETYPE_CXX :
1235 streq(suffix, "cp") ? FILETYPE_CXX :
1236 streq(suffix, "cpp") ? FILETYPE_CXX :
1237 streq(suffix, "CPP") ? FILETYPE_CXX :
1238 streq(suffix, "cxx") ? FILETYPE_CXX :
1239 streq(suffix, "c++") ? FILETYPE_CXX :
1240 streq(suffix, "ii") ? FILETYPE_CXX :
1241 streq(suffix, "h") ? FILETYPE_C :
1242 streq(suffix, "ir") ? FILETYPE_IR :
1243 streq(suffix, "o") ? FILETYPE_OBJECT :
1244 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1245 streq(suffix, "so") ? FILETYPE_OBJECT :
1246 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1251 file_list_entry_t *entry
1252 = obstack_alloc(&file_obst, sizeof(entry[0]));
1253 memset(entry, 0, sizeof(entry[0]));
1257 if (last_file != NULL) {
1258 last_file->next = entry;
1266 if (help_displayed) {
1267 return !argument_errors;
1270 if (print_file_name_file != NULL) {
1271 print_file_name(print_file_name_file);
1272 return EXIT_SUCCESS;
1274 if (files == NULL) {
1275 fprintf(stderr, "error: no input files specified\n");
1276 argument_errors = true;
1279 if (argument_errors) {
1281 return EXIT_FAILURE;
1284 /* set the c_mode here, types depends on it */
1285 c_mode |= features_on;
1286 c_mode &= ~features_off;
1289 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1290 init_symbol_table();
1303 if (construct_dep_target) {
1304 if (outname != 0 && strlen(outname) >= 2) {
1305 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1307 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1310 dep_target[0] = '\0';
1313 char outnamebuf[4096];
1314 if (outname == NULL) {
1315 const char *filename = files->name;
1318 case BenchmarkParser:
1323 case PreprocessOnly:
1328 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1329 outname = outnamebuf;
1331 case CompileAssemble:
1332 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1333 outname = outnamebuf;
1336 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1338 outname = outnamebuf;
1340 case CompileExportIR:
1341 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1342 outname = outnamebuf;
1344 case CompileAssembleLink:
1354 assert(outname != NULL);
1357 if (streq(outname, "-")) {
1360 out = fopen(outname, "w");
1362 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1364 return EXIT_FAILURE;
1368 file_list_entry_t *file;
1369 bool already_constructed_firm = false;
1370 for (file = files; file != NULL; file = file->next) {
1371 char asm_tempfile[1024];
1372 const char *filename = file->name;
1373 filetype_t filetype = file->type;
1375 if (filetype == FILETYPE_OBJECT)
1379 if (mode == LexTest) {
1381 in = open_file(filename);
1382 lextest(in, filename);
1384 return EXIT_SUCCESS;
1387 FILE *preprocessed_in = NULL;
1388 filetype_t next_filetype = filetype;
1391 next_filetype = FILETYPE_PREPROCESSED_C;
1394 next_filetype = FILETYPE_PREPROCESSED_CXX;
1396 case FILETYPE_ASSEMBLER:
1397 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1400 /* no support for input on FILE* yet */
1402 panic("internal compiler error: in for preprocessor != NULL");
1404 preprocessed_in = preprocess(filename, filetype);
1405 if (mode == PreprocessOnly) {
1406 copy_file(out, preprocessed_in);
1407 int result = pclose(preprocessed_in);
1409 /* remove output file in case of error */
1410 if (out != stdout && result != EXIT_SUCCESS) {
1416 in = preprocessed_in;
1417 filetype = next_filetype;
1425 if (mode == Compile) {
1428 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1432 in = open_file(filename);
1434 /* preprocess and compile */
1435 if (filetype == FILETYPE_PREPROCESSED_C) {
1436 char const* invalid_mode;
1439 case STANDARD_C89: c_mode = _C89; break;
1440 /* TODO determine difference between these two */
1441 case STANDARD_C90: c_mode = _C89; break;
1442 case STANDARD_C99: c_mode = _C89 | _C99; break;
1443 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1447 "warning: command line option \"-std=%s\" is not valid for C\n",
1450 case STANDARD_DEFAULT:
1451 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1453 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1454 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1457 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1458 char const* invalid_mode;
1460 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1461 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1462 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1463 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1464 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1467 case STANDARD_CXX98: c_mode = _CXX; break;
1471 "warning: command line option \"-std=%s\" is not valid for C++\n",
1473 case STANDARD_DEFAULT:
1474 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1478 c_mode |= features_on;
1479 c_mode &= ~features_off;
1481 /* do the actual parsing */
1482 ir_timer_t *t_parsing = ir_timer_new();
1483 timer_register(t_parsing, "Frontend: Parsing");
1484 timer_push(t_parsing);
1486 translation_unit_t *const unit = do_parsing(in, filename);
1487 timer_pop(t_parsing);
1489 /* prints the AST even if errors occurred */
1490 if (mode == PrintAst) {
1495 if (error_count > 0) {
1496 /* parsing failed because of errors */
1497 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1499 result = EXIT_FAILURE;
1501 } else if (warning_count > 0) {
1502 fprintf(stderr, "%u warning(s)\n", warning_count);
1505 if (in == preprocessed_in) {
1506 int pp_result = pclose(preprocessed_in);
1507 if (pp_result != EXIT_SUCCESS) {
1508 /* remove output file */
1511 return EXIT_FAILURE;
1515 if (mode == BenchmarkParser) {
1517 } else if (mode == PrintFluffy) {
1518 write_fluffy_decls(out, unit);
1520 } else if (mode == PrintJna) {
1521 write_jna_decls(out, unit);
1525 /* build the firm graph */
1526 ir_timer_t *t_construct = ir_timer_new();
1527 timer_register(t_construct, "Frontend: Graph construction");
1528 timer_push(t_construct);
1529 if (already_constructed_firm) {
1530 panic("compiling multiple files/translation units not possible");
1532 translation_unit_to_firm(unit);
1533 already_constructed_firm = true;
1534 timer_pop(t_construct);
1537 if (mode == ParseOnly) {
1541 if (mode == CompileDump) {
1543 ident *id = new_id_from_str(dumpfunction);
1544 ir_graph *irg = NULL;
1545 int n_irgs = get_irp_n_irgs();
1546 for (int i = 0; i < n_irgs; ++i) {
1547 ir_graph *tirg = get_irp_irg(i);
1548 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1556 fprintf(stderr, "No graph for function '%s' found\n",
1558 return EXIT_FAILURE;
1561 dump_ir_graph_file(out, irg);
1563 return EXIT_SUCCESS;
1566 if (mode == CompileExportIR) {
1569 return EXIT_SUCCESS;
1572 gen_firm_finish(asm_out, filename);
1573 if (asm_out != out) {
1576 } else if (filetype == FILETYPE_IR) {
1578 ir_import(filename);
1580 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1581 copy_file(asm_out, in);
1582 if (in == preprocessed_in) {
1583 int pp_result = pclose(preprocessed_in);
1584 if (pp_result != EXIT_SUCCESS) {
1585 /* remove output in error case */
1591 if (asm_out != out) {
1596 if (mode == Compile)
1599 /* if we're here then we have preprocessed assembly */
1600 filename = asm_tempfile;
1601 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1604 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1606 const char *filename_o;
1607 if (mode == CompileAssemble) {
1609 filename_o = outname;
1611 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1616 assemble(filename_o, filename);
1618 size_t len = strlen(filename_o) + 1;
1619 filename = obstack_copy(&file_obst, filename_o, len);
1620 filetype = FILETYPE_OBJECT;
1623 /* ok we're done here, process next file */
1624 file->name = filename;
1625 file->type = filetype;
1628 if (result != EXIT_SUCCESS) {
1634 /* link program file */
1635 if (mode == CompileAssembleLink) {
1636 obstack_1grow(&ldflags_obst, '\0');
1637 const char *flags = obstack_finish(&ldflags_obst);
1639 /* construct commandline */
1640 const char *linker = getenv("CPARSER_LINK");
1641 if (linker != NULL) {
1642 obstack_printf(&file_obst, "%s ", linker);
1644 if (target_triple != NULL)
1645 obstack_printf(&file_obst, "%s-", target_triple);
1646 obstack_printf(&file_obst, "%s ", LINKER);
1649 for (file_list_entry_t *entry = files; entry != NULL;
1650 entry = entry->next) {
1651 if (entry->type != FILETYPE_OBJECT)
1654 add_flag(&file_obst, "%s", entry->name);
1657 add_flag(&file_obst, "-o");
1658 add_flag(&file_obst, outname);
1659 obstack_printf(&file_obst, "%s", flags);
1660 obstack_1grow(&file_obst, '\0');
1662 char *commandline = obstack_finish(&file_obst);
1667 int err = system(commandline);
1668 if (err != EXIT_SUCCESS) {
1669 fprintf(stderr, "linker reported an error\n");
1670 return EXIT_FAILURE;
1677 obstack_free(&cppflags_obst, NULL);
1678 obstack_free(&ldflags_obst, NULL);
1679 obstack_free(&asflags_obst, NULL);
1680 obstack_free(&file_obst, NULL);
1690 exit_symbol_table();
1691 return EXIT_SUCCESS;