2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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 -std=c99 -m32 -U__STRICT_ANSI__"
86 #define PREPROCESSOR "cpp -std=c99 -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 struct file_list_entry_t file_list_entry_t;
135 typedef enum filetype_t {
138 FILETYPE_PREPROCESSED_C,
140 FILETYPE_PREPROCESSED_CXX,
142 FILETYPE_PREPROCESSED_ASSEMBLER,
147 struct file_list_entry_t {
148 const char *name; /**< filename or NULL for stdin */
150 file_list_entry_t *next;
153 static file_list_entry_t *temp_files;
155 #if defined(_DEBUG) || defined(FIRM_DEBUG)
157 * Debug printf implementation.
159 * @param fmt printf style format parameter
161 void dbg_printf(const char *fmt, ...)
165 if (firm_dump.debug_print) {
171 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
173 static void initialize_firm(void)
177 dump_consts_local(1);
178 dump_keepalive_edges(1);
181 static void get_output_name(char *buf, size_t buflen, const char *inputname,
184 if (inputname == NULL)
187 char const *const last_slash = strrchr(inputname, '/');
188 char const *const filename =
189 last_slash != NULL ? last_slash + 1 : inputname;
190 char const *const last_dot = strrchr(filename, '.');
191 char const *const name_end =
192 last_dot != NULL ? last_dot : strchr(filename, '\0');
194 int const len = snprintf(buf, buflen, "%.*s%s",
195 (int)(name_end - filename), filename, newext);
197 if (len < 0 || buflen <= (size_t)len)
199 if (buflen <= (size_t)len)
201 panic("filename too long");
204 #include "builtins.h"
206 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
211 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
215 lexer_open_stream(in, input_name);
218 translation_unit_t *unit = finish_parsing();
222 static void lextest(FILE *in, const char *fname)
224 lexer_open_stream(in, fname);
227 lexer_next_preprocessing_token();
228 print_token(stdout, &lexer_token);
230 } while (lexer_token.type != T_EOF);
233 static void add_flag(struct obstack *obst, const char *format, ...)
238 va_start(ap, format);
242 vsnprintf(buf, sizeof(buf), format, ap);
245 obstack_1grow(obst, ' ');
247 obstack_1grow(obst, '"');
248 obstack_grow(obst, buf, len);
249 obstack_1grow(obst, '"');
251 /* escape stuff... */
252 for (char *c = buf; *c != '\0'; ++c) {
270 obstack_1grow(obst, '\\');
273 obstack_1grow(obst, *c);
280 static const char *type_to_string(type_t *type)
282 assert(type->kind == TYPE_ATOMIC);
283 return get_atomic_kind_name(type->atomic.akind);
286 static FILE *preprocess(const char *fname)
288 obstack_1grow(&cppflags_obst, '\0');
289 const char *flags = obstack_finish(&cppflags_obst);
291 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
293 /* setup default defines */
294 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
295 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
296 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
297 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
300 add_flag(&cppflags_obst, "-D__builtin_abort=abort");
301 add_flag(&cppflags_obst, "-D__builtin_abs=abs");
302 add_flag(&cppflags_obst, "-D__builtin_exit=exit");
303 add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
304 add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
305 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
306 add_flag(&cppflags_obst, "-D__builtin_memset=memset");
307 add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
308 add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
309 add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
311 /* handle dependency generation */
312 if (dep_target[0] != '\0') {
313 add_flag(&cppflags_obst, "-MF");
314 add_flag(&cppflags_obst, dep_target);
315 if (outname != NULL) {
316 add_flag(&cppflags_obst, "-MQ");
317 add_flag(&cppflags_obst, outname);
320 if (flags[0] != '\0') {
321 obstack_printf(&cppflags_obst, " %s", flags);
323 add_flag(&cppflags_obst, fname);
325 obstack_1grow(&cppflags_obst, '\0');
326 const char *buf = obstack_finish(&cppflags_obst);
331 FILE *f = popen(buf, "r");
333 fprintf(stderr, "invoking preprocessor failed\n");
340 static void assemble(const char *out, const char *in)
344 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
349 int err = system(buf);
351 fprintf(stderr, "assembler reported an error\n");
356 static void print_file_name(const char *file)
358 add_flag(&ldflags_obst, "-print-file-name=%s", file);
360 obstack_1grow(&ldflags_obst, '\0');
361 const char *flags = obstack_finish(&ldflags_obst);
363 /* construct commandline */
364 obstack_printf(&ldflags_obst, "%s ", LINKER);
365 obstack_printf(&ldflags_obst, "%s", flags);
366 obstack_1grow(&ldflags_obst, '\0');
368 char *commandline = obstack_finish(&ldflags_obst);
373 int err = system(commandline);
374 if (err != EXIT_SUCCESS) {
375 fprintf(stderr, "linker reported an error\n");
380 static const char *try_dir(const char *dir)
384 if (access(dir, R_OK | W_OK | X_OK) == 0)
389 static const char *get_tempdir(void)
391 static const char *tmpdir = NULL;
397 tmpdir = try_dir(getenv("TMPDIR"));
399 tmpdir = try_dir(getenv("TMP"));
401 tmpdir = try_dir(getenv("TEMP"));
405 tmpdir = try_dir(P_tmpdir);
409 tmpdir = try_dir("/var/tmp");
411 tmpdir = try_dir("/usr/tmp");
413 tmpdir = try_dir("/tmp");
422 /* cheap and nasty mkstemp replacement */
423 static int mkstemp(char *templ)
426 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
431 * an own version of tmpnam, which: writes in a buffer, emits no warnings
432 * during linking (like glibc/gnu ld do for tmpnam)...
434 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
436 const char *tempdir = get_tempdir();
438 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
440 int fd = mkstemp(buffer);
442 fprintf(stderr, "couldn't create temporary file: %s\n",
446 FILE *out = fdopen(fd, "w");
448 fprintf(stderr, "couldn't create temporary file FILE*\n");
452 file_list_entry_t *entry = xmalloc(sizeof(*entry));
453 memset(entry, 0, sizeof(*entry));
455 size_t name_len = strlen(buffer) + 1;
456 char *name = malloc(name_len);
457 memcpy(name, buffer, name_len);
460 entry->next = temp_files;
466 static void free_temp_files(void)
468 file_list_entry_t *entry = temp_files;
469 file_list_entry_t *next;
470 for ( ; entry != NULL; entry = next) {
474 free((char*) entry->name);
481 * Do the necessary lowering for compound parameters.
483 void lower_compound_params(void)
485 lower_params_t params;
487 params.def_ptr_alignment = 4;
488 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
489 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
490 params.find_pointer_type = NULL;
491 params.ret_compound_in_regs = NULL;
492 lower_calls_with_compounds(¶ms);
495 typedef enum compile_mode_t {
510 static void usage(const char *argv0)
512 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
515 static void print_cparser_version(void) {
516 printf("cparser (%s) using libFirm (%u.%u",
517 cparser_REVISION, ir_get_version_major(),
518 ir_get_version_minor());
520 const char *revision = ir_get_version_revision();
521 if (revision[0] != 0) {
523 fputs(revision, stdout);
526 const char *build = ir_get_version_build();
529 fputs(build, stdout);
534 static void set_be_option(const char *arg)
536 int res = firm_be_option(arg);
541 static void set_option(const char *arg)
543 int res = firm_option(arg);
548 static void copy_file(FILE *dest, FILE *input)
552 while (!feof(input) && !ferror(dest)) {
553 size_t read = fread(buf, 1, sizeof(buf), input);
554 if (fwrite(buf, 1, read, dest) != read) {
555 perror("couldn't write output");
560 static inline bool streq(char const* a, char const* b)
562 return strcmp(a, b) == 0;
565 static inline bool strstart(char const* str, char const* start)
570 } while (*str++ == *start++);
574 static FILE *open_file(const char *filename)
576 if (streq(filename, "-")) {
580 FILE *in = fopen(filename, "r");
582 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
590 static filetype_t get_filetype_from_string(const char *string)
592 if (streq(string, "c") || streq(string, "c-header"))
594 if (streq(string, "c++") || streq(string, "c++-header"))
596 if (streq(string, "assembler"))
597 return FILETYPE_PREPROCESSED_ASSEMBLER;
598 if (streq(string, "assembler-with-cpp"))
599 return FILETYPE_ASSEMBLER;
600 if (streq(string, "none"))
601 return FILETYPE_AUTODETECT;
603 return FILETYPE_UNKNOWN;
606 static void init_os_support(void)
608 /* OS option must be set to the backend */
609 switch (firm_opt.os_support) {
610 case OS_SUPPORT_MINGW:
611 set_be_option("ia32-gasmode=mingw");
612 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
614 case OS_SUPPORT_LINUX:
615 set_be_option("ia32-gasmode=elf");
617 case OS_SUPPORT_MACHO:
618 set_be_option("ia32-gasmode=macho");
619 set_be_option("ia32-stackalign=4");
620 set_be_option("pic");
625 typedef enum lang_standard_t {
626 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
627 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
628 STANDARD_C89, /* ISO C90 (sic) */
629 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
630 STANDARD_C99, /* ISO C99 */
631 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
632 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
633 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
634 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
637 int main(int argc, char **argv)
641 const char *dumpfunction = NULL;
642 const char *print_file_name_file = NULL;
643 compile_mode_t mode = CompileAssembleLink;
645 int result = EXIT_SUCCESS;
646 char cpu_arch[16] = "ia32";
647 file_list_entry_t *files = NULL;
648 file_list_entry_t *last_file = NULL;
649 bool construct_dep_target = false;
650 struct obstack file_obst;
652 atexit(free_temp_files);
654 /* hack for now... */
655 if (strstr(argv[0], "pptest") != NULL) {
656 extern int pptest_main(int argc, char **argv);
657 return pptest_main(argc, argv);
660 obstack_init(&cppflags_obst);
661 obstack_init(&ldflags_obst);
662 obstack_init(&file_obst);
664 #define GET_ARG_AFTER(def, args) \
665 def = &arg[sizeof(args)-1]; \
666 if (def[0] == '\0') { \
669 fprintf(stderr, "error: expected argument after '" args "'\n"); \
670 argument_errors = true; \
674 if (def[0] == '-' && def[1] != '\0') { \
675 fprintf(stderr, "error: expected argument after '" args "'\n"); \
676 argument_errors = true; \
681 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
683 /* early options parsing (find out optimisation level and OS) */
684 for (int i = 1; i < argc; ++i) {
685 const char *arg = argv[i];
689 const char *option = &arg[1];
690 if (option[0] == 'O') {
691 sscanf(&option[1], "%d", &opt_level);
693 if (strcmp(arg, "-fwin32") == 0) {
694 firm_opt.os_support = OS_SUPPORT_MINGW;
695 } else if (strcmp(arg, "-fmac") == 0) {
696 firm_opt.os_support = OS_SUPPORT_MACHO;
697 } else if (strcmp(arg, "-flinux") == 0) {
698 firm_opt.os_support = OS_SUPPORT_LINUX;
702 /* set target/os specific stuff */
705 /* apply optimisation level */
708 set_option("no-opt");
711 set_option("no-inline");
715 set_option("strict-aliasing");
716 /* use_builtins = true; */
719 set_option("cond-eval");
720 set_option("if-conv");
723 set_option("inline");
724 set_option("deconv");
725 set_be_option("omitfp");
729 /* parse rest of options */
730 lang_standard_t standard = STANDARD_DEFAULT;
731 unsigned features_on = 0;
732 unsigned features_off = 0;
733 filetype_t forced_filetype = FILETYPE_AUTODETECT;
734 bool help_displayed = false;
735 bool argument_errors = false;
736 for (int i = 1; i < argc; ++i) {
737 const char *arg = argv[i];
738 if (arg[0] == '-' && arg[1] != '\0') {
740 const char *option = &arg[1];
741 if (option[0] == 'o') {
742 GET_ARG_AFTER(outname, "-o");
743 } else if (option[0] == 'g') {
744 set_be_option("debuginfo=stabs");
745 set_be_option("omitfp=no");
746 set_be_option("ia32-nooptcc=yes");
747 } else if (SINGLE_OPTION('c')) {
748 mode = CompileAssemble;
749 } else if (SINGLE_OPTION('E')) {
750 mode = PreprocessOnly;
751 } else if (SINGLE_OPTION('S')) {
753 } else if (option[0] == 'O') {
755 } else if (option[0] == 'I') {
757 GET_ARG_AFTER(opt, "-I");
758 add_flag(&cppflags_obst, "-I%s", opt);
759 } else if (option[0] == 'D') {
761 GET_ARG_AFTER(opt, "-D");
762 add_flag(&cppflags_obst, "-D%s", opt);
763 } else if (option[0] == 'U') {
765 GET_ARG_AFTER(opt, "-U");
766 add_flag(&cppflags_obst, "-U%s", opt);
767 } else if (option[0] == 'l') {
769 GET_ARG_AFTER(opt, "-l");
770 add_flag(&ldflags_obst, "-l%s", opt);
771 } else if (option[0] == 'L') {
773 GET_ARG_AFTER(opt, "-L");
774 add_flag(&ldflags_obst, "-L%s", opt);
775 } else if (SINGLE_OPTION('v')) {
777 } else if (SINGLE_OPTION('w')) {
778 memset(&warning, 0, sizeof(warning));
779 } else if (option[0] == 'x') {
781 GET_ARG_AFTER(opt, "-x");
782 forced_filetype = get_filetype_from_string(opt);
783 if (forced_filetype == FILETYPE_UNKNOWN) {
784 fprintf(stderr, "Unknown language '%s'\n", opt);
785 argument_errors = true;
787 } else if (streq(option, "M")) {
788 mode = PreprocessOnly;
789 add_flag(&cppflags_obst, "-M");
790 } else if (streq(option, "MMD") ||
791 streq(option, "MD")) {
792 construct_dep_target = true;
793 add_flag(&cppflags_obst, "-%s", option);
794 } else if (streq(option, "MM") ||
795 streq(option, "MP")) {
796 add_flag(&cppflags_obst, "-%s", option);
797 } else if (streq(option, "MT") ||
798 streq(option, "MQ") ||
799 streq(option, "MF")) {
801 GET_ARG_AFTER(opt, "-MT");
802 add_flag(&cppflags_obst, "-%s", option);
803 add_flag(&cppflags_obst, "%s", opt);
804 } else if (streq(option, "include")) {
806 GET_ARG_AFTER(opt, "-include");
807 add_flag(&cppflags_obst, "-include");
808 add_flag(&cppflags_obst, "%s", opt);
809 } else if (streq(option, "isystem")) {
811 GET_ARG_AFTER(opt, "-isystem");
812 add_flag(&cppflags_obst, "-isystem");
813 add_flag(&cppflags_obst, "%s", opt);
814 } else if (streq(option, "nostdinc")
815 || streq(option, "trigraphs")) {
816 /* pass these through to the preprocessor */
817 add_flag(&cppflags_obst, "%s", arg);
818 } else if (streq(option, "pipe")) {
819 /* here for gcc compatibility */
820 } else if (option[0] == 'f') {
821 char const *orig_opt;
822 GET_ARG_AFTER(orig_opt, "-f");
824 if (strstart(orig_opt, "align-loops=") ||
825 strstart(orig_opt, "align-jumps=") ||
826 strstart(orig_opt, "align-functions=")) {
827 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
828 } else if (strstart(orig_opt, "input-charset=")) {
829 char const* const encoding = strchr(orig_opt, '=') + 1;
830 select_input_encoding(encoding);
831 } else if (streq(orig_opt, "verbose-asm")) {
832 /* ignore: we always print verbose assembler */
834 char const *opt = orig_opt;
835 bool truth_value = true;
836 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
841 if (streq(opt, "builtins")) {
842 use_builtins = truth_value;
843 } else if (streq(opt, "dollars-in-identifiers")) {
844 allow_dollar_in_symbol = truth_value;
845 } else if (streq(opt, "omit-frame-pointer")) {
846 set_be_option(truth_value ? "omitfp" : "omitfp=no");
847 } else if (streq(opt, "short-wchar")) {
848 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
850 } else if (streq(opt, "signed-char")) {
851 char_is_signed = truth_value;
852 } else if (streq(opt, "strength-reduce")) {
853 firm_option(truth_value ? "strength-red" : "no-strength-red");
854 } else if (streq(opt, "syntax-only")) {
855 mode = truth_value ? ParseOnly : CompileAssembleLink;
856 } else if (streq(opt, "unsigned-char")) {
857 char_is_signed = !truth_value;
858 } else if (streq(opt, "fast-math") ||
859 streq(opt, "jump-tables") ||
860 streq(opt, "unroll-loops") ||
861 streq(opt, "expensive-optimizations") ||
862 streq(opt, "common") ||
864 streq(opt, "align-loops") ||
865 streq(opt, "align-jumps") ||
866 streq(opt, "align-functions")) {
867 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
869 int res = firm_option(orig_opt);
871 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
873 argument_errors = true;
875 } else if (res == -1) {
876 help_displayed = true;
880 } else if (option[0] == 'b') {
882 GET_ARG_AFTER(opt, "-b");
883 int res = firm_be_option(opt);
885 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
887 argument_errors = true;
888 } else if (res == -1) {
889 help_displayed = true;
890 } else if (strstart(opt, "isa=")) {
891 strncpy(cpu_arch, opt, sizeof(cpu_arch));
893 } else if (option[0] == 'W') {
894 if (strstart(option + 1, "p,")) {
895 // pass options directly to the preprocessor
897 GET_ARG_AFTER(opt, "-Wp,");
898 add_flag(&cppflags_obst, "-Wp,%s", opt);
899 } else if (strstart(option + 1, "l,")) {
900 // pass options directly to the linker
902 GET_ARG_AFTER(opt, "-Wl,");
903 add_flag(&ldflags_obst, "-Wl,%s", opt);
904 } else if (streq(option + 1, "no-trigraphs")
905 || streq(option + 1, "undef")) {
906 add_flag(&cppflags_obst, "%s", arg);
908 set_warning_opt(&option[1]);
910 } else if (option[0] == 'm') {
915 GET_ARG_AFTER(opt, "-m");
916 if (strstart(opt, "arch=")) {
917 GET_ARG_AFTER(opt, "-march=");
918 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
919 int res = firm_be_option(arch_opt);
921 argument_errors = true;
923 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
924 int res = firm_be_option(arch_opt);
926 argument_errors = true;
928 } else if (strstart(opt, "tune=")) {
929 GET_ARG_AFTER(opt, "-mtune=");
930 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
931 int res = firm_be_option(arch_opt);
933 argument_errors = true;
934 } else if (strstart(opt, "cpu=")) {
935 GET_ARG_AFTER(opt, "-mcpu=");
936 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
937 int res = firm_be_option(arch_opt);
939 argument_errors = true;
940 } else if (strstart(opt, "fpmath=")) {
941 GET_ARG_AFTER(opt, "-mfpmath=");
942 if (streq(opt, "387"))
944 else if (streq(opt, "sse"))
947 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
948 argument_errors = true;
950 if (!argument_errors) {
951 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
952 int res = firm_be_option(arch_opt);
954 argument_errors = true;
956 } else if (strstart(opt, "preferred-stack-boundary=")) {
957 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
958 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
959 int res = firm_be_option(arch_opt);
961 argument_errors = true;
962 } else if (streq(opt, "omit-leaf-frame-pointer")) {
963 set_be_option("omitleaffp=1");
964 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
965 set_be_option("omitleaffp=0");
968 long int value = strtol(opt, &endptr, 10);
969 if (*endptr != '\0') {
970 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
971 argument_errors = true;
973 if (value != 16 && value != 32 && value != 64) {
974 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
975 argument_errors = true;
977 machine_size = (unsigned int)value;
980 } else if (streq(option, "pg")) {
981 set_be_option("gprof");
982 add_flag(&ldflags_obst, "-pg");
983 } else if (streq(option, "pedantic") ||
984 streq(option, "ansi")) {
985 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
986 } else if (streq(option, "shared")) {
987 add_flag(&ldflags_obst, "-shared");
988 } else if (strstart(option, "std=")) {
989 const char *const o = &option[4];
991 streq(o, "c++") ? STANDARD_CXX98 :
992 streq(o, "c++98") ? STANDARD_CXX98 :
993 streq(o, "c89") ? STANDARD_C89 :
994 streq(o, "c99") ? STANDARD_C99 :
995 streq(o, "c9x") ? STANDARD_C99 : // deprecated
996 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
997 streq(o, "gnu89") ? STANDARD_GNU89 :
998 streq(o, "gnu99") ? STANDARD_GNU99 :
999 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1000 streq(o, "iso9899:1990") ? STANDARD_C89 :
1001 streq(o, "iso9899:199409") ? STANDARD_C90 :
1002 streq(o, "iso9899:1999") ? STANDARD_C99 :
1003 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1004 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1005 } else if (streq(option, "version")) {
1006 print_cparser_version();
1007 } else if (strstart(option, "print-file-name=")) {
1008 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1009 } else if (option[0] == '-') {
1010 /* double dash option */
1012 if (streq(option, "gcc")) {
1013 features_on |= _GNUC;
1014 features_off &= ~_GNUC;
1015 } else if (streq(option, "no-gcc")) {
1016 features_on &= ~_GNUC;
1017 features_off |= _GNUC;
1018 } else if (streq(option, "ms")) {
1020 features_off &= ~_MS;
1021 } else if (streq(option, "no-ms")) {
1022 features_on &= ~_MS;
1023 features_off |= _MS;
1024 } else if (streq(option, "strict")) {
1026 } else if (streq(option, "lextest")) {
1028 } else if (streq(option, "benchmark")) {
1029 mode = BenchmarkParser;
1030 } else if (streq(option, "print-ast")) {
1032 } else if (streq(option, "print-implicit-cast")) {
1033 print_implicit_casts = true;
1034 } else if (streq(option, "print-parenthesis")) {
1035 print_parenthesis = true;
1036 } else if (streq(option, "print-fluffy")) {
1038 } else if (streq(option, "print-caml")) {
1040 } else if (streq(option, "print-jna")) {
1042 } else if (streq(option, "version")) {
1043 print_cparser_version();
1045 } else if (streq(option, "dump-function")) {
1048 fprintf(stderr, "error: "
1049 "expected argument after '--dump-function'\n");
1050 argument_errors = true;
1053 dumpfunction = argv[i];
1056 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1057 argument_errors = true;
1060 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1061 argument_errors = true;
1064 filetype_t type = forced_filetype;
1065 if (type == FILETYPE_AUTODETECT) {
1066 if (streq(arg, "-")) {
1067 /* - implicitly means C source file */
1070 const char *suffix = strrchr(arg, '.');
1071 /* Ensure there is at least one char before the suffix */
1072 if (suffix != NULL && suffix != arg) {
1075 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1076 streq(suffix, "a") ? FILETYPE_OBJECT :
1077 streq(suffix, "c") ? FILETYPE_C :
1078 streq(suffix, "cc") ? FILETYPE_CXX :
1079 streq(suffix, "cpp") ? FILETYPE_CXX :
1080 streq(suffix, "cxx") ? FILETYPE_CXX :
1081 streq(suffix, "h") ? FILETYPE_C :
1082 streq(suffix, "o") ? FILETYPE_OBJECT :
1083 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1084 streq(suffix, "so") ? FILETYPE_OBJECT :
1085 FILETYPE_AUTODETECT;
1089 if (type == FILETYPE_AUTODETECT) {
1090 fprintf(stderr, "'%s': file format not recognized\n", arg);
1095 file_list_entry_t *entry
1096 = obstack_alloc(&file_obst, sizeof(entry[0]));
1097 memset(entry, 0, sizeof(entry[0]));
1101 if (last_file != NULL) {
1102 last_file->next = entry;
1110 if (print_file_name_file != NULL) {
1111 print_file_name(print_file_name_file);
1115 if (files == NULL) {
1116 fprintf(stderr, "error: no input files specified\n");
1117 argument_errors = true;
1120 if (help_displayed) {
1121 return !argument_errors;
1123 if (argument_errors) {
1128 /* we do the lowering in ast2firm */
1129 firm_opt.lower_bitfields = FALSE;
1132 init_symbol_table();
1142 if (construct_dep_target) {
1143 if (outname != 0 && strlen(outname) >= 2) {
1144 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1146 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1149 dep_target[0] = '\0';
1152 char outnamebuf[4096];
1153 if (outname == NULL) {
1154 const char *filename = files->name;
1157 case BenchmarkParser:
1163 case PreprocessOnly:
1168 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1169 outname = outnamebuf;
1171 case CompileAssemble:
1172 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1173 outname = outnamebuf;
1176 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1178 outname = outnamebuf;
1180 case CompileAssembleLink:
1190 assert(outname != NULL);
1193 if (streq(outname, "-")) {
1196 out = fopen(outname, "w");
1198 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1204 file_list_entry_t *file;
1205 for (file = files; file != NULL; file = file->next) {
1206 char asm_tempfile[1024];
1207 const char *filename = file->name;
1208 filetype_t filetype = file->type;
1210 if (filetype == FILETYPE_OBJECT)
1214 if (mode == LexTest) {
1216 in = open_file(filename);
1217 lextest(in, filename);
1222 FILE *preprocessed_in = NULL;
1225 filetype = FILETYPE_PREPROCESSED_C;
1228 filetype = FILETYPE_PREPROCESSED_CXX;
1230 case FILETYPE_ASSEMBLER:
1231 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1232 add_flag(&cppflags_obst, "-x");
1233 add_flag(&cppflags_obst, "assembler-with-cpp");
1236 /* no support for input on FILE* yet */
1238 panic("internal compiler error: in for preprocessor != NULL");
1240 preprocessed_in = preprocess(filename);
1241 if (mode == PreprocessOnly) {
1242 copy_file(out, preprocessed_in);
1243 int result = pclose(preprocessed_in);
1245 /* remove output file in case of error */
1246 if (out != stdout && result != EXIT_SUCCESS) {
1252 in = preprocessed_in;
1260 if (mode == Compile) {
1263 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1267 in = open_file(filename);
1269 /* preprocess and compile */
1270 if (filetype == FILETYPE_PREPROCESSED_C) {
1271 char const* invalid_mode;
1274 case STANDARD_C89: c_mode = _C89; break;
1275 /* TODO ^v determine difference between these two */
1276 case STANDARD_C90: c_mode = _C89; break;
1277 case STANDARD_C99: c_mode = _C89 | _C99; break;
1278 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1282 "warning: command line option \"-std=%s\" is not valid for C\n",
1285 case STANDARD_DEFAULT:
1286 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1288 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1289 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1292 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1293 char const* invalid_mode;
1295 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1296 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1297 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1298 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1299 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1302 case STANDARD_CXX98: c_mode = _CXX; break;
1306 "warning: command line option \"-std=%s\" is not valid for C++\n",
1308 case STANDARD_DEFAULT:
1309 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1313 c_mode |= features_on;
1314 c_mode &= ~features_off;
1316 translation_unit_t *const unit = do_parsing(in, filename);
1318 /* prints the AST even if errors occurred */
1319 if (mode == PrintAst) {
1320 type_set_output(out);
1321 ast_set_output(out);
1325 if (error_count > 0) {
1326 /* parsing failed because of errors */
1327 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1329 result = EXIT_FAILURE;
1331 } else if (warning_count > 0) {
1332 fprintf(stderr, "%u warning(s)\n", warning_count);
1335 if (in == preprocessed_in) {
1336 int pp_result = pclose(preprocessed_in);
1337 if (pp_result != EXIT_SUCCESS) {
1338 /* remove output file */
1345 if (mode == BenchmarkParser) {
1347 } else if (mode == PrintFluffy) {
1348 write_fluffy_decls(out, unit);
1350 } else if (mode == PrintCaml) {
1351 write_caml_decls(out, unit);
1353 } else if (mode == PrintJna) {
1354 write_jna_decls(out, unit);
1358 translation_unit_to_firm(unit);
1360 if (mode == ParseOnly) {
1364 if (mode == CompileDump) {
1366 ident *id = new_id_from_str(dumpfunction);
1367 ir_graph *irg = NULL;
1368 int n_irgs = get_irp_n_irgs();
1369 for (int i = 0; i < n_irgs; ++i) {
1370 ir_graph *tirg = get_irp_irg(i);
1371 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1379 fprintf(stderr, "No graph for function '%s' found\n",
1384 dump_ir_block_graph_file(irg, out);
1389 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1390 have_const_functions);
1391 if (asm_out != out) {
1394 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1395 copy_file(asm_out, in);
1396 if (in == preprocessed_in) {
1397 int pp_result = pclose(preprocessed_in);
1398 if (pp_result != EXIT_SUCCESS) {
1399 /* remove output in error case */
1405 if (asm_out != out) {
1410 if (mode == Compile)
1413 /* if we're here then we have preprocessed assembly */
1414 filename = asm_tempfile;
1415 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1418 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1420 const char *filename_o;
1421 if (mode == CompileAssemble) {
1423 filename_o = outname;
1425 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1430 assemble(filename_o, filename);
1432 size_t len = strlen(filename_o) + 1;
1433 filename = obstack_copy(&file_obst, filename_o, len);
1434 filetype = FILETYPE_OBJECT;
1437 /* ok we're done here, process next file */
1438 file->name = filename;
1439 file->type = filetype;
1442 if (result != EXIT_SUCCESS) {
1448 /* link program file */
1449 if (mode == CompileAssembleLink) {
1450 obstack_1grow(&ldflags_obst, '\0');
1451 const char *flags = obstack_finish(&ldflags_obst);
1453 /* construct commandline */
1454 obstack_printf(&file_obst, "%s", LINKER);
1455 for (file_list_entry_t *entry = files; entry != NULL;
1456 entry = entry->next) {
1457 if (entry->type != FILETYPE_OBJECT)
1460 add_flag(&file_obst, "%s", entry->name);
1463 add_flag(&file_obst, "-o");
1464 add_flag(&file_obst, outname);
1465 obstack_printf(&file_obst, "%s", flags);
1466 obstack_1grow(&file_obst, '\0');
1468 char *commandline = obstack_finish(&file_obst);
1473 int err = system(commandline);
1474 if (err != EXIT_SUCCESS) {
1475 fprintf(stderr, "linker reported an error\n");
1480 obstack_free(&cppflags_obst, NULL);
1481 obstack_free(&ldflags_obst, NULL);
1482 obstack_free(&file_obst, NULL);
1492 exit_symbol_table();