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 "adt/error.h"
75 #include "wrappergen/write_fluffy.h"
76 #include "wrappergen/write_caml.h"
77 #include "wrappergen/write_jna.h"
84 #define PREPROCESSOR "gcc -E -m32 -U__STRICT_ANSI__"
86 #define PREPROCESSOR "cpp -m32 -U__STRICT_ANSI__"
91 #define LINKER "gcc -m32"
96 #define ASSEMBLER "gcc -c -xassembler"
98 #define ASSEMBLER "as --32"
102 /** The current c mode/dialect. */
103 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
105 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
106 unsigned int machine_size = 32;
108 /** true if the char type is signed. */
109 bool char_is_signed = true;
111 /** true for strict language checking. */
112 bool strict_mode = false;
114 /** use builtins for some libc functions */
115 bool use_builtins = false;
117 /** we have extern function with const attribute. */
118 bool have_const_functions = false;
120 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
122 /* to switch on printing of implicit casts */
123 extern bool print_implicit_casts;
125 /* to switch on printing of parenthesis to indicate operator precedence */
126 extern bool print_parenthesis;
129 static struct obstack cppflags_obst, ldflags_obst;
130 static char dep_target[1024];
131 static const char *outname;
133 typedef enum lang_standard_t {
134 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
135 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
136 STANDARD_C89, /* ISO C90 (sic) */
137 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
138 STANDARD_C99, /* ISO C99 */
139 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
140 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
141 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
142 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
145 static lang_standard_t standard;
147 typedef struct file_list_entry_t file_list_entry_t;
149 typedef enum filetype_t {
152 FILETYPE_PREPROCESSED_C,
154 FILETYPE_PREPROCESSED_CXX,
156 FILETYPE_PREPROCESSED_ASSEMBLER,
162 struct file_list_entry_t {
163 const char *name; /**< filename or NULL for stdin */
165 file_list_entry_t *next;
168 static file_list_entry_t *temp_files;
170 #if defined(_DEBUG) || defined(FIRM_DEBUG)
172 * Debug printf implementation.
174 * @param fmt printf style format parameter
176 void dbg_printf(const char *fmt, ...)
180 if (firm_dump.debug_print) {
186 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
188 static void initialize_firm(void)
192 dump_consts_local(1);
193 dump_keepalive_edges(1);
196 static void get_output_name(char *buf, size_t buflen, const char *inputname,
199 if (inputname == NULL)
202 char const *const last_slash = strrchr(inputname, '/');
203 char const *const filename =
204 last_slash != NULL ? last_slash + 1 : inputname;
205 char const *const last_dot = strrchr(filename, '.');
206 char const *const name_end =
207 last_dot != NULL ? last_dot : strchr(filename, '\0');
209 int const len = snprintf(buf, buflen, "%.*s%s",
210 (int)(name_end - filename), filename, newext);
212 if (len < 0 || buflen <= (size_t)len)
214 if (buflen <= (size_t)len)
216 panic("filename too long");
219 #include "builtins.h"
221 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
226 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
230 lexer_open_stream(in, input_name);
233 translation_unit_t *unit = finish_parsing();
237 static void lextest(FILE *in, const char *fname)
239 lexer_open_stream(in, fname);
242 lexer_next_preprocessing_token();
243 print_token(stdout, &lexer_token);
245 } while (lexer_token.type != T_EOF);
248 static void add_flag(struct obstack *obst, const char *format, ...)
253 va_start(ap, format);
257 vsnprintf(buf, sizeof(buf), format, ap);
260 obstack_1grow(obst, ' ');
262 obstack_1grow(obst, '"');
263 obstack_grow(obst, buf, len);
264 obstack_1grow(obst, '"');
266 /* escape stuff... */
267 for (char *c = buf; *c != '\0'; ++c) {
285 obstack_1grow(obst, '\\');
288 obstack_1grow(obst, *c);
295 static const char *type_to_string(type_t *type)
297 assert(type->kind == TYPE_ATOMIC);
298 return get_atomic_kind_name(type->atomic.akind);
301 static FILE *preprocess(const char *fname, filetype_t filetype)
303 static const char *common_flags = NULL;
305 if (common_flags == NULL) {
306 obstack_1grow(&cppflags_obst, '\0');
307 const char *flags = obstack_finish(&cppflags_obst);
309 /* setup default defines */
310 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
311 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
312 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
313 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
315 add_flag(&cppflags_obst, "-U__VERSION__");
316 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
319 add_flag(&cppflags_obst, "-D__builtin_abort=abort");
320 add_flag(&cppflags_obst, "-D__builtin_abs=abs");
321 add_flag(&cppflags_obst, "-D__builtin_exit=exit");
322 add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
323 add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
324 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
325 add_flag(&cppflags_obst, "-D__builtin_memset=memset");
326 add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
327 add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
328 add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
330 if (flags[0] != '\0') {
331 size_t len = strlen(flags);
332 obstack_1grow(&cppflags_obst, ' ');
333 obstack_grow(&cppflags_obst, flags, len);
335 common_flags = obstack_finish(&cppflags_obst);
338 assert(obstack_object_size(&cppflags_obst) == 0);
339 obstack_printf(&cppflags_obst, "%s ", PREPROCESSOR);
342 add_flag(&cppflags_obst, "-std=c99");
345 add_flag(&cppflags_obst, "-std=c++98");
347 case FILETYPE_ASSEMBLER:
348 add_flag(&cppflags_obst, "-x");
349 add_flag(&cppflags_obst, "assembler-with-cpp");
354 obstack_printf(&cppflags_obst, "%s", common_flags);
356 /* handle dependency generation */
357 if (dep_target[0] != '\0') {
358 add_flag(&cppflags_obst, "-MF");
359 add_flag(&cppflags_obst, dep_target);
360 if (outname != NULL) {
361 add_flag(&cppflags_obst, "-MQ");
362 add_flag(&cppflags_obst, outname);
365 add_flag(&cppflags_obst, fname);
367 obstack_1grow(&cppflags_obst, '\0');
368 char *buf = obstack_finish(&cppflags_obst);
373 FILE *f = popen(buf, "r");
375 fprintf(stderr, "invoking preprocessor failed\n");
379 /* we don't really need that anymore */
380 obstack_free(&cppflags_obst, buf);
385 static void assemble(const char *out, const char *in)
389 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
394 int err = system(buf);
396 fprintf(stderr, "assembler reported an error\n");
401 static void print_file_name(const char *file)
403 add_flag(&ldflags_obst, "-print-file-name=%s", file);
405 obstack_1grow(&ldflags_obst, '\0');
406 const char *flags = obstack_finish(&ldflags_obst);
408 /* construct commandline */
409 obstack_printf(&ldflags_obst, "%s ", LINKER);
410 obstack_printf(&ldflags_obst, "%s", flags);
411 obstack_1grow(&ldflags_obst, '\0');
413 char *commandline = obstack_finish(&ldflags_obst);
418 int err = system(commandline);
419 if (err != EXIT_SUCCESS) {
420 fprintf(stderr, "linker reported an error\n");
425 static const char *try_dir(const char *dir)
429 if (access(dir, R_OK | W_OK | X_OK) == 0)
434 static const char *get_tempdir(void)
436 static const char *tmpdir = NULL;
442 tmpdir = try_dir(getenv("TMPDIR"));
444 tmpdir = try_dir(getenv("TMP"));
446 tmpdir = try_dir(getenv("TEMP"));
450 tmpdir = try_dir(P_tmpdir);
454 tmpdir = try_dir("/var/tmp");
456 tmpdir = try_dir("/usr/tmp");
458 tmpdir = try_dir("/tmp");
467 /* cheap and nasty mkstemp replacement */
468 static int mkstemp(char *templ)
471 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
476 * an own version of tmpnam, which: writes in a buffer, emits no warnings
477 * during linking (like glibc/gnu ld do for tmpnam)...
479 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
481 const char *tempdir = get_tempdir();
483 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
485 int fd = mkstemp(buffer);
487 fprintf(stderr, "couldn't create temporary file: %s\n",
491 FILE *out = fdopen(fd, "w");
493 fprintf(stderr, "couldn't create temporary file FILE*\n");
497 file_list_entry_t *entry = xmalloc(sizeof(*entry));
498 memset(entry, 0, sizeof(*entry));
500 size_t name_len = strlen(buffer) + 1;
501 char *name = malloc(name_len);
502 memcpy(name, buffer, name_len);
505 entry->next = temp_files;
511 static void free_temp_files(void)
513 file_list_entry_t *entry = temp_files;
514 file_list_entry_t *next;
515 for ( ; entry != NULL; entry = next) {
519 free((char*) entry->name);
526 * Do the necessary lowering for compound parameters.
528 void lower_compound_params(void)
530 lower_params_t params;
532 params.def_ptr_alignment = 4;
533 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
534 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
535 params.find_pointer_type = NULL;
536 params.ret_compound_in_regs = NULL;
537 lower_calls_with_compounds(¶ms);
540 typedef enum compile_mode_t {
556 static void usage(const char *argv0)
558 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
561 static void print_cparser_version(void)
563 printf("cparser (%s) using libFirm (%u.%u",
564 cparser_REVISION, ir_get_version_major(),
565 ir_get_version_minor());
567 const char *revision = ir_get_version_revision();
568 if (revision[0] != 0) {
570 fputs(revision, stdout);
573 const char *build = ir_get_version_build();
576 fputs(build, stdout);
579 puts("This is free software; see the source for copying conditions. There is NO\n"
580 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
583 static void set_be_option(const char *arg)
585 int res = firm_be_option(arg);
590 static void set_option(const char *arg)
592 int res = firm_option(arg);
597 static void copy_file(FILE *dest, FILE *input)
601 while (!feof(input) && !ferror(dest)) {
602 size_t read = fread(buf, 1, sizeof(buf), input);
603 if (fwrite(buf, 1, read, dest) != read) {
604 perror("couldn't write output");
609 static inline bool streq(char const* a, char const* b)
611 return strcmp(a, b) == 0;
614 static inline bool strstart(char const* str, char const* start)
619 } while (*str++ == *start++);
623 static FILE *open_file(const char *filename)
625 if (streq(filename, "-")) {
629 FILE *in = fopen(filename, "r");
631 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
639 static filetype_t get_filetype_from_string(const char *string)
641 if (streq(string, "c") || streq(string, "c-header"))
643 if (streq(string, "c++") || streq(string, "c++-header"))
645 if (streq(string, "assembler"))
646 return FILETYPE_PREPROCESSED_ASSEMBLER;
647 if (streq(string, "assembler-with-cpp"))
648 return FILETYPE_ASSEMBLER;
649 if (streq(string, "none"))
650 return FILETYPE_AUTODETECT;
652 return FILETYPE_UNKNOWN;
655 static void init_os_support(void)
657 /* OS option must be set to the backend */
658 switch (firm_opt.os_support) {
659 case OS_SUPPORT_MINGW:
660 set_be_option("ia32-gasmode=mingw");
661 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
663 case OS_SUPPORT_LINUX:
664 set_be_option("ia32-gasmode=elf");
666 case OS_SUPPORT_MACHO:
667 set_be_option("ia32-gasmode=macho");
668 set_be_option("ia32-stackalign=4");
669 set_be_option("pic");
674 int main(int argc, char **argv)
678 const char *dumpfunction = NULL;
679 const char *print_file_name_file = NULL;
680 compile_mode_t mode = CompileAssembleLink;
682 int result = EXIT_SUCCESS;
683 char cpu_arch[16] = "ia32";
684 file_list_entry_t *files = NULL;
685 file_list_entry_t *last_file = NULL;
686 bool construct_dep_target = false;
687 struct obstack file_obst;
689 atexit(free_temp_files);
691 /* hack for now... */
692 if (strstr(argv[0], "pptest") != NULL) {
693 extern int pptest_main(int argc, char **argv);
694 return pptest_main(argc, argv);
697 obstack_init(&cppflags_obst);
698 obstack_init(&ldflags_obst);
699 obstack_init(&file_obst);
701 #define GET_ARG_AFTER(def, args) \
702 def = &arg[sizeof(args)-1]; \
703 if (def[0] == '\0') { \
706 fprintf(stderr, "error: expected argument after '" args "'\n"); \
707 argument_errors = true; \
711 if (def[0] == '-' && def[1] != '\0') { \
712 fprintf(stderr, "error: expected argument after '" args "'\n"); \
713 argument_errors = true; \
718 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
720 /* early options parsing (find out optimisation level and OS) */
721 for (int i = 1; i < argc; ++i) {
722 const char *arg = argv[i];
726 const char *option = &arg[1];
727 if (option[0] == 'O') {
728 sscanf(&option[1], "%d", &opt_level);
730 if (strcmp(arg, "-fwin32") == 0) {
731 firm_opt.os_support = OS_SUPPORT_MINGW;
732 } else if (strcmp(arg, "-fmac") == 0) {
733 firm_opt.os_support = OS_SUPPORT_MACHO;
734 } else if (strcmp(arg, "-flinux") == 0) {
735 firm_opt.os_support = OS_SUPPORT_LINUX;
739 /* set target/os specific stuff */
742 /* apply optimisation level */
745 set_option("no-opt");
748 set_option("no-inline");
752 /* use_builtins = true; */
755 set_option("cond-eval");
756 set_option("if-conv");
759 set_option("strict-aliasing");
760 set_option("inline");
761 set_option("deconv");
762 set_be_option("omitfp");
766 /* parse rest of options */
767 standard = STANDARD_DEFAULT;
768 unsigned features_on = 0;
769 unsigned features_off = 0;
770 filetype_t forced_filetype = FILETYPE_AUTODETECT;
771 bool help_displayed = false;
772 bool argument_errors = false;
773 for (int i = 1; i < argc; ++i) {
774 const char *arg = argv[i];
775 if (arg[0] == '-' && arg[1] != '\0') {
777 const char *option = &arg[1];
778 if (option[0] == 'o') {
779 GET_ARG_AFTER(outname, "-o");
780 } else if (option[0] == 'g') {
781 set_be_option("debuginfo=stabs");
782 set_be_option("omitfp=no");
783 set_be_option("ia32-nooptcc=yes");
784 } else if (SINGLE_OPTION('c')) {
785 mode = CompileAssemble;
786 } else if (SINGLE_OPTION('E')) {
787 mode = PreprocessOnly;
788 } else if (SINGLE_OPTION('S')) {
790 } else if (option[0] == 'O') {
792 } else if (option[0] == 'I') {
794 GET_ARG_AFTER(opt, "-I");
795 add_flag(&cppflags_obst, "-I%s", opt);
796 } else if (option[0] == 'D') {
798 GET_ARG_AFTER(opt, "-D");
799 add_flag(&cppflags_obst, "-D%s", opt);
800 } else if (option[0] == 'U') {
802 GET_ARG_AFTER(opt, "-U");
803 add_flag(&cppflags_obst, "-U%s", opt);
804 } else if (option[0] == 'l') {
806 GET_ARG_AFTER(opt, "-l");
807 add_flag(&ldflags_obst, "-l%s", opt);
808 } else if (option[0] == 'L') {
810 GET_ARG_AFTER(opt, "-L");
811 add_flag(&ldflags_obst, "-L%s", opt);
812 } else if (SINGLE_OPTION('v')) {
814 } else if (SINGLE_OPTION('w')) {
815 memset(&warning, 0, sizeof(warning));
816 } else if (option[0] == 'x') {
818 GET_ARG_AFTER(opt, "-x");
819 forced_filetype = get_filetype_from_string(opt);
820 if (forced_filetype == FILETYPE_UNKNOWN) {
821 fprintf(stderr, "Unknown language '%s'\n", opt);
822 argument_errors = true;
824 } else if (streq(option, "M")) {
825 mode = PreprocessOnly;
826 add_flag(&cppflags_obst, "-M");
827 } else if (streq(option, "MMD") ||
828 streq(option, "MD")) {
829 construct_dep_target = true;
830 add_flag(&cppflags_obst, "-%s", option);
831 } else if (streq(option, "MM") ||
832 streq(option, "MP")) {
833 add_flag(&cppflags_obst, "-%s", option);
834 } else if (streq(option, "MT") ||
835 streq(option, "MQ") ||
836 streq(option, "MF")) {
838 GET_ARG_AFTER(opt, "-MT");
839 add_flag(&cppflags_obst, "-%s", option);
840 add_flag(&cppflags_obst, "%s", opt);
841 } else if (streq(option, "include")) {
843 GET_ARG_AFTER(opt, "-include");
844 add_flag(&cppflags_obst, "-include");
845 add_flag(&cppflags_obst, "%s", opt);
846 } else if (streq(option, "isystem")) {
848 GET_ARG_AFTER(opt, "-isystem");
849 add_flag(&cppflags_obst, "-isystem");
850 add_flag(&cppflags_obst, "%s", opt);
851 } else if (streq(option, "nostdinc")
852 || streq(option, "trigraphs")) {
853 /* pass these through to the preprocessor */
854 add_flag(&cppflags_obst, "%s", arg);
855 } else if (streq(option, "pipe")) {
856 /* here for gcc compatibility */
857 } else if (option[0] == 'f') {
858 char const *orig_opt;
859 GET_ARG_AFTER(orig_opt, "-f");
861 if (strstart(orig_opt, "align-loops=") ||
862 strstart(orig_opt, "align-jumps=") ||
863 strstart(orig_opt, "align-functions=")) {
864 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
865 } else if (strstart(orig_opt, "input-charset=")) {
866 char const* const encoding = strchr(orig_opt, '=') + 1;
867 select_input_encoding(encoding);
868 } else if (streq(orig_opt, "verbose-asm")) {
869 /* ignore: we always print verbose assembler */
871 char const *opt = orig_opt;
872 bool truth_value = true;
873 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
878 if (streq(opt, "builtins")) {
879 use_builtins = truth_value;
880 } else if (streq(opt, "dollars-in-identifiers")) {
881 allow_dollar_in_symbol = truth_value;
882 } else if (streq(opt, "omit-frame-pointer")) {
883 set_be_option(truth_value ? "omitfp" : "omitfp=no");
884 } else if (streq(opt, "short-wchar")) {
885 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
887 } else if (streq(opt, "signed-char")) {
888 char_is_signed = truth_value;
889 } else if (streq(opt, "strength-reduce")) {
890 firm_option(truth_value ? "strength-red" : "no-strength-red");
891 } else if (streq(opt, "syntax-only")) {
892 mode = truth_value ? ParseOnly : CompileAssembleLink;
893 } else if (streq(opt, "unsigned-char")) {
894 char_is_signed = !truth_value;
895 } else if (streq(opt, "fast-math") ||
896 streq(opt, "jump-tables") ||
897 streq(opt, "unroll-loops") ||
898 streq(opt, "expensive-optimizations") ||
899 streq(opt, "common") ||
901 streq(opt, "align-loops") ||
902 streq(opt, "align-jumps") ||
903 streq(opt, "align-functions")) {
904 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
906 int res = firm_option(orig_opt);
908 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
910 argument_errors = true;
912 } else if (res == -1) {
913 help_displayed = true;
917 } else if (option[0] == 'b') {
919 GET_ARG_AFTER(opt, "-b");
920 int res = firm_be_option(opt);
922 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
924 argument_errors = true;
925 } else if (res == -1) {
926 help_displayed = true;
927 } else if (strstart(opt, "isa=")) {
928 strncpy(cpu_arch, opt, sizeof(cpu_arch));
930 } else if (option[0] == 'W') {
931 if (strstart(option + 1, "p,")) {
932 // pass options directly to the preprocessor
934 GET_ARG_AFTER(opt, "-Wp,");
935 add_flag(&cppflags_obst, "-Wp,%s", opt);
936 } else if (strstart(option + 1, "l,")) {
937 // pass options directly to the linker
939 GET_ARG_AFTER(opt, "-Wl,");
940 add_flag(&ldflags_obst, "-Wl,%s", opt);
941 } else if (streq(option + 1, "no-trigraphs")
942 || streq(option + 1, "undef")) {
943 add_flag(&cppflags_obst, "%s", arg);
945 set_warning_opt(&option[1]);
947 } else if (option[0] == 'm') {
952 GET_ARG_AFTER(opt, "-m");
953 if (strstart(opt, "arch=")) {
954 GET_ARG_AFTER(opt, "-march=");
955 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
956 int res = firm_be_option(arch_opt);
958 argument_errors = true;
960 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
961 int res = firm_be_option(arch_opt);
963 argument_errors = true;
965 } else if (strstart(opt, "tune=")) {
966 GET_ARG_AFTER(opt, "-mtune=");
967 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
968 int res = firm_be_option(arch_opt);
970 argument_errors = true;
971 } else if (strstart(opt, "cpu=")) {
972 GET_ARG_AFTER(opt, "-mcpu=");
973 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
974 int res = firm_be_option(arch_opt);
976 argument_errors = true;
977 } else if (strstart(opt, "fpmath=")) {
978 GET_ARG_AFTER(opt, "-mfpmath=");
979 if (streq(opt, "387"))
981 else if (streq(opt, "sse"))
984 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
985 argument_errors = true;
987 if (!argument_errors) {
988 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
989 int res = firm_be_option(arch_opt);
991 argument_errors = true;
993 } else if (strstart(opt, "preferred-stack-boundary=")) {
994 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
995 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
996 int res = firm_be_option(arch_opt);
998 argument_errors = true;
999 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1000 set_be_option("omitleaffp=1");
1001 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1002 set_be_option("omitleaffp=0");
1003 } else if (streq(opt, "rtd")) {
1004 default_calling_convention = CC_STDCALL;
1007 long int value = strtol(opt, &endptr, 10);
1008 if (*endptr != '\0') {
1009 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1010 argument_errors = true;
1012 if (value != 16 && value != 32 && value != 64) {
1013 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1014 argument_errors = true;
1016 machine_size = (unsigned int)value;
1019 } else if (streq(option, "pg")) {
1020 set_be_option("gprof");
1021 add_flag(&ldflags_obst, "-pg");
1022 } else if (streq(option, "pedantic") ||
1023 streq(option, "ansi")) {
1024 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1025 } else if (streq(option, "shared")) {
1026 add_flag(&ldflags_obst, "-shared");
1027 } else if (strstart(option, "std=")) {
1028 const char *const o = &option[4];
1030 streq(o, "c++") ? STANDARD_CXX98 :
1031 streq(o, "c++98") ? STANDARD_CXX98 :
1032 streq(o, "c89") ? STANDARD_C89 :
1033 streq(o, "c99") ? STANDARD_C99 :
1034 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1035 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1036 streq(o, "gnu89") ? STANDARD_GNU89 :
1037 streq(o, "gnu99") ? STANDARD_GNU99 :
1038 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1039 streq(o, "iso9899:1990") ? STANDARD_C89 :
1040 streq(o, "iso9899:199409") ? STANDARD_C90 :
1041 streq(o, "iso9899:1999") ? STANDARD_C99 :
1042 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1043 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1044 } else if (streq(option, "version")) {
1045 print_cparser_version();
1046 } else if (strstart(option, "print-file-name=")) {
1047 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1048 } else if (option[0] == '-') {
1049 /* double dash option */
1051 if (streq(option, "gcc")) {
1052 features_on |= _GNUC;
1053 features_off &= ~_GNUC;
1054 } else if (streq(option, "no-gcc")) {
1055 features_on &= ~_GNUC;
1056 features_off |= _GNUC;
1057 } else if (streq(option, "ms")) {
1059 features_off &= ~_MS;
1060 } else if (streq(option, "no-ms")) {
1061 features_on &= ~_MS;
1062 features_off |= _MS;
1063 } else if (streq(option, "strict")) {
1065 } else if (streq(option, "lextest")) {
1067 } else if (streq(option, "benchmark")) {
1068 mode = BenchmarkParser;
1069 } else if (streq(option, "print-ast")) {
1071 } else if (streq(option, "print-implicit-cast")) {
1072 print_implicit_casts = true;
1073 } else if (streq(option, "print-parenthesis")) {
1074 print_parenthesis = true;
1075 } else if (streq(option, "print-fluffy")) {
1077 } else if (streq(option, "print-caml")) {
1079 } else if (streq(option, "print-jna")) {
1081 } else if (streq(option, "version")) {
1082 print_cparser_version();
1084 } else if (streq(option, "dump-function")) {
1087 fprintf(stderr, "error: "
1088 "expected argument after '--dump-function'\n");
1089 argument_errors = true;
1092 dumpfunction = argv[i];
1094 } else if (streq(option, "export-ir")) {
1095 mode = CompileExportIR;
1097 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1098 argument_errors = true;
1101 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1102 argument_errors = true;
1105 filetype_t type = forced_filetype;
1106 if (type == FILETYPE_AUTODETECT) {
1107 if (streq(arg, "-")) {
1108 /* - implicitly means C source file */
1111 const char *suffix = strrchr(arg, '.');
1112 /* Ensure there is at least one char before the suffix */
1113 if (suffix != NULL && suffix != arg) {
1116 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1117 streq(suffix, "a") ? FILETYPE_OBJECT :
1118 streq(suffix, "c") ? FILETYPE_C :
1119 streq(suffix, "cc") ? FILETYPE_CXX :
1120 streq(suffix, "cpp") ? FILETYPE_CXX :
1121 streq(suffix, "cxx") ? FILETYPE_CXX :
1122 streq(suffix, "h") ? FILETYPE_C :
1123 streq(suffix, "ir") ? FILETYPE_IR :
1124 streq(suffix, "o") ? FILETYPE_OBJECT :
1125 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1126 streq(suffix, "so") ? FILETYPE_OBJECT :
1127 FILETYPE_AUTODETECT;
1131 if (type == FILETYPE_AUTODETECT) {
1132 fprintf(stderr, "'%s': file format not recognized\n", arg);
1137 file_list_entry_t *entry
1138 = obstack_alloc(&file_obst, sizeof(entry[0]));
1139 memset(entry, 0, sizeof(entry[0]));
1143 if (last_file != NULL) {
1144 last_file->next = entry;
1152 if (print_file_name_file != NULL) {
1153 print_file_name(print_file_name_file);
1157 if (files == NULL) {
1158 fprintf(stderr, "error: no input files specified\n");
1159 argument_errors = true;
1162 if (help_displayed) {
1163 return !argument_errors;
1165 if (argument_errors) {
1170 /* we do the lowering in ast2firm */
1171 firm_opt.lower_bitfields = FALSE;
1173 /* set the c_mode here, types depends on it */
1174 c_mode |= features_on;
1175 c_mode &= ~features_off;
1178 init_symbol_table();
1188 if (construct_dep_target) {
1189 if (outname != 0 && strlen(outname) >= 2) {
1190 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1192 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1195 dep_target[0] = '\0';
1198 char outnamebuf[4096];
1199 if (outname == NULL) {
1200 const char *filename = files->name;
1203 case BenchmarkParser:
1209 case PreprocessOnly:
1214 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1215 outname = outnamebuf;
1217 case CompileAssemble:
1218 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1219 outname = outnamebuf;
1222 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1224 outname = outnamebuf;
1226 case CompileExportIR:
1227 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1228 outname = outnamebuf;
1230 case CompileAssembleLink:
1240 assert(outname != NULL);
1243 if (streq(outname, "-")) {
1246 out = fopen(outname, "w");
1248 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1254 file_list_entry_t *file;
1255 for (file = files; file != NULL; file = file->next) {
1256 char asm_tempfile[1024];
1257 const char *filename = file->name;
1258 filetype_t filetype = file->type;
1260 if (filetype == FILETYPE_OBJECT)
1264 if (mode == LexTest) {
1266 in = open_file(filename);
1267 lextest(in, filename);
1272 FILE *preprocessed_in = NULL;
1273 filetype_t next_filetype = filetype;
1276 next_filetype = FILETYPE_PREPROCESSED_C;
1279 next_filetype = FILETYPE_PREPROCESSED_CXX;
1281 case FILETYPE_ASSEMBLER:
1282 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1285 /* no support for input on FILE* yet */
1287 panic("internal compiler error: in for preprocessor != NULL");
1289 preprocessed_in = preprocess(filename, filetype);
1290 if (mode == PreprocessOnly) {
1291 copy_file(out, preprocessed_in);
1292 int result = pclose(preprocessed_in);
1294 /* remove output file in case of error */
1295 if (out != stdout && result != EXIT_SUCCESS) {
1301 in = preprocessed_in;
1302 filetype = next_filetype;
1310 if (mode == Compile) {
1313 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1317 in = open_file(filename);
1319 /* preprocess and compile */
1320 if (filetype == FILETYPE_PREPROCESSED_C) {
1321 char const* invalid_mode;
1324 case STANDARD_C89: c_mode = _C89; break;
1325 /* TODO determine difference between these two */
1326 case STANDARD_C90: c_mode = _C89; break;
1327 case STANDARD_C99: c_mode = _C89 | _C99; break;
1328 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1332 "warning: command line option \"-std=%s\" is not valid for C\n",
1335 case STANDARD_DEFAULT:
1336 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1338 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1339 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1342 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1343 char const* invalid_mode;
1345 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1346 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1347 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1348 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1349 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1352 case STANDARD_CXX98: c_mode = _CXX; break;
1356 "warning: command line option \"-std=%s\" is not valid for C++\n",
1358 case STANDARD_DEFAULT:
1359 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1363 c_mode |= features_on;
1364 c_mode &= ~features_off;
1367 translation_unit_t *const unit = do_parsing(in, filename);
1369 /* prints the AST even if errors occurred */
1370 if (mode == PrintAst) {
1371 type_set_output(out);
1372 ast_set_output(out);
1376 if (error_count > 0) {
1377 /* parsing failed because of errors */
1378 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1380 result = EXIT_FAILURE;
1382 } else if (warning_count > 0) {
1383 fprintf(stderr, "%u warning(s)\n", warning_count);
1386 if (in == preprocessed_in) {
1387 int pp_result = pclose(preprocessed_in);
1388 if (pp_result != EXIT_SUCCESS) {
1389 /* remove output file */
1396 if (mode == BenchmarkParser) {
1398 } else if (mode == PrintFluffy) {
1399 write_fluffy_decls(out, unit);
1401 } else if (mode == PrintCaml) {
1402 write_caml_decls(out, unit);
1404 } else if (mode == PrintJna) {
1405 write_jna_decls(out, unit);
1409 translation_unit_to_firm(unit);
1412 if (mode == ParseOnly) {
1416 if (mode == CompileDump) {
1418 ident *id = new_id_from_str(dumpfunction);
1419 ir_graph *irg = NULL;
1420 int n_irgs = get_irp_n_irgs();
1421 for (int i = 0; i < n_irgs; ++i) {
1422 ir_graph *tirg = get_irp_irg(i);
1423 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1431 fprintf(stderr, "No graph for function '%s' found\n",
1436 dump_ir_block_graph_file(irg, out);
1441 if (mode == CompileExportIR) {
1447 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1448 have_const_functions);
1449 if (asm_out != out) {
1452 } else if (filetype == FILETYPE_IR) {
1454 ir_import(filename);
1456 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1457 copy_file(asm_out, in);
1458 if (in == preprocessed_in) {
1459 int pp_result = pclose(preprocessed_in);
1460 if (pp_result != EXIT_SUCCESS) {
1461 /* remove output in error case */
1467 if (asm_out != out) {
1472 if (mode == Compile)
1475 /* if we're here then we have preprocessed assembly */
1476 filename = asm_tempfile;
1477 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1480 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1482 const char *filename_o;
1483 if (mode == CompileAssemble) {
1485 filename_o = outname;
1487 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1492 assemble(filename_o, filename);
1494 size_t len = strlen(filename_o) + 1;
1495 filename = obstack_copy(&file_obst, filename_o, len);
1496 filetype = FILETYPE_OBJECT;
1499 /* ok we're done here, process next file */
1500 file->name = filename;
1501 file->type = filetype;
1504 if (result != EXIT_SUCCESS) {
1510 /* link program file */
1511 if (mode == CompileAssembleLink) {
1512 obstack_1grow(&ldflags_obst, '\0');
1513 const char *flags = obstack_finish(&ldflags_obst);
1515 /* construct commandline */
1516 obstack_printf(&file_obst, "%s", LINKER);
1517 for (file_list_entry_t *entry = files; entry != NULL;
1518 entry = entry->next) {
1519 if (entry->type != FILETYPE_OBJECT)
1522 add_flag(&file_obst, "%s", entry->name);
1525 add_flag(&file_obst, "-o");
1526 add_flag(&file_obst, outname);
1527 obstack_printf(&file_obst, "%s", flags);
1528 obstack_1grow(&file_obst, '\0');
1530 char *commandline = obstack_finish(&file_obst);
1535 int err = system(commandline);
1536 if (err != EXIT_SUCCESS) {
1537 fprintf(stderr, "linker reported an error\n");
1542 obstack_free(&cppflags_obst, NULL);
1543 obstack_free(&ldflags_obst, NULL);
1544 obstack_free(&file_obst, NULL);
1554 exit_symbol_table();