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 "write_fluffy.h"
76 #include "write_caml.h"
83 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
85 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
90 #define LINKER "gcc -m32"
95 #define ASSEMBLER "gcc -c -xassembler"
97 #define ASSEMBLER "as --32"
101 /** The current c mode/dialect. */
102 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
104 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
105 unsigned int machine_size = 32;
107 /** true if the char type is signed. */
108 bool char_is_signed = true;
110 /** true for strict language checking. */
111 bool strict_mode = false;
113 /** use builtins for some libc functions */
114 bool use_builtins = false;
116 /** we have extern function with const attribute. */
117 bool have_const_functions = false;
119 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
121 /* to switch on printing of implicit casts */
122 extern bool print_implicit_casts;
124 /* to switch on printing of parenthesis to indicate operator precedence */
125 extern bool print_parenthesis;
128 static struct obstack cppflags_obst, ldflags_obst;
129 static char dep_target[1024];
130 static const char *outname;
132 typedef struct file_list_entry_t file_list_entry_t;
134 typedef enum filetype_t {
137 FILETYPE_PREPROCESSED_C,
139 FILETYPE_PREPROCESSED_CXX,
141 FILETYPE_PREPROCESSED_ASSEMBLER,
146 struct file_list_entry_t {
147 const char *name; /**< filename or NULL for stdin */
149 file_list_entry_t *next;
152 static file_list_entry_t *temp_files;
154 #if defined(_DEBUG) || defined(FIRM_DEBUG)
156 * Debug printf implementation.
158 * @param fmt printf style format parameter
160 void dbg_printf(const char *fmt, ...)
164 if (firm_dump.debug_print) {
170 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
172 static void initialize_firm(void)
176 dump_consts_local(1);
177 dump_keepalive_edges(1);
180 static void get_output_name(char *buf, size_t buflen, const char *inputname,
183 size_t last_dot = 0xffffffff;
186 if (inputname == NULL) {
187 snprintf(buf, buflen, "a%s", newext);
191 for (const char *c = inputname; *c != 0; ++c) {
196 if (last_dot == 0xffffffff)
199 if (last_dot >= buflen)
200 panic("filename too long");
201 memcpy(buf, inputname, last_dot);
203 size_t extlen = strlen(newext) + 1;
204 if (extlen + last_dot >= buflen)
205 panic("filename too long");
206 memcpy(buf+last_dot, newext, extlen);
209 #include "builtins.h"
211 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
216 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
220 lexer_open_stream(in, input_name);
223 translation_unit_t *unit = finish_parsing();
227 static void lextest(FILE *in, const char *fname)
229 lexer_open_stream(in, fname);
232 lexer_next_preprocessing_token();
233 print_token(stdout, &lexer_token);
235 } while (lexer_token.type != T_EOF);
238 static void add_flag(struct obstack *obst, const char *format, ...)
243 va_start(ap, format);
247 vsnprintf(buf, sizeof(buf), format, ap);
250 obstack_1grow(obst, ' ');
252 obstack_1grow(obst, '"');
253 obstack_grow(obst, buf, len);
254 obstack_1grow(obst, '"');
256 /* escape stuff... */
257 for (char *c = buf; *c != '\0'; ++c) {
275 obstack_1grow(obst, '\\');
278 obstack_1grow(obst, *c);
285 static const char *type_to_string(type_t *type)
287 assert(type->kind == TYPE_ATOMIC);
288 return get_atomic_kind_name(type->atomic.akind);
291 static FILE *preprocess(const char *fname)
293 obstack_1grow(&cppflags_obst, '\0');
294 const char *flags = obstack_finish(&cppflags_obst);
296 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
298 /* setup default defines */
299 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
300 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
301 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
302 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
305 add_flag(&cppflags_obst, "-D__builtin_abort=abort");
306 add_flag(&cppflags_obst, "-D__builtin_abs=abs");
307 add_flag(&cppflags_obst, "-D__builtin_exit=exit");
308 add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
309 add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
310 add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
311 add_flag(&cppflags_obst, "-D__builtin_memset=memset");
312 add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
313 add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
314 add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
316 /* handle dependency generation */
317 if (dep_target[0] != '\0') {
318 add_flag(&cppflags_obst, "-MF");
319 add_flag(&cppflags_obst, dep_target);
320 if (outname != NULL) {
321 add_flag(&cppflags_obst, "-MQ");
322 add_flag(&cppflags_obst, outname);
325 if (flags[0] != '\0') {
326 obstack_printf(&cppflags_obst, " %s", flags);
328 add_flag(&cppflags_obst, fname);
330 obstack_1grow(&cppflags_obst, '\0');
331 const char *buf = obstack_finish(&cppflags_obst);
336 FILE *f = popen(buf, "r");
338 fprintf(stderr, "invoking preprocessor failed\n");
345 static void assemble(const char *out, const char *in)
349 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
354 int err = system(buf);
356 fprintf(stderr, "assembler reported an error\n");
361 static void print_file_name(const char *file)
363 add_flag(&ldflags_obst, "-print-file-name=%s", file);
365 obstack_1grow(&ldflags_obst, '\0');
366 const char *flags = obstack_finish(&ldflags_obst);
368 /* construct commandline */
369 obstack_printf(&ldflags_obst, "%s ", LINKER);
370 obstack_printf(&ldflags_obst, "%s", flags);
371 obstack_1grow(&ldflags_obst, '\0');
373 char *commandline = obstack_finish(&ldflags_obst);
378 int err = system(commandline);
379 if (err != EXIT_SUCCESS) {
380 fprintf(stderr, "linker reported an error\n");
385 static const char *try_dir(const char *dir)
389 if (access(dir, R_OK | W_OK | X_OK) == 0)
394 static const char *get_tempdir(void)
396 static const char *tmpdir = NULL;
402 tmpdir = try_dir(getenv("TMPDIR"));
404 tmpdir = try_dir(getenv("TMP"));
406 tmpdir = try_dir(getenv("TEMP"));
410 tmpdir = try_dir(P_tmpdir);
414 tmpdir = try_dir("/var/tmp");
416 tmpdir = try_dir("/usr/tmp");
418 tmpdir = try_dir("/tmp");
427 /* cheap and nasty mkstemp replacement */
428 static int mkstemp(char *templ)
431 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
436 * an own version of tmpnam, which: writes in a buffer, emits no warnings
437 * during linking (like glibc/gnu ld do for tmpnam)...
439 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
441 const char *tempdir = get_tempdir();
443 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
445 int fd = mkstemp(buffer);
447 fprintf(stderr, "couldn't create temporary file: %s\n",
451 FILE *out = fdopen(fd, "w");
453 fprintf(stderr, "couldn't create temporary file FILE*\n");
457 file_list_entry_t *entry = xmalloc(sizeof(*entry));
458 memset(entry, 0, sizeof(*entry));
460 size_t name_len = strlen(buffer) + 1;
461 char *name = malloc(name_len);
462 memcpy(name, buffer, name_len);
465 entry->next = temp_files;
471 static void free_temp_files(void)
473 file_list_entry_t *entry = temp_files;
474 file_list_entry_t *next;
475 for ( ; entry != NULL; entry = next) {
479 free((char*) entry->name);
486 * Do the necessary lowering for compound parameters.
488 void lower_compound_params(void)
490 lower_params_t params;
492 params.def_ptr_alignment = 4;
493 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
494 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
495 params.find_pointer_type = NULL;
496 params.ret_compound_in_regs = NULL;
497 lower_calls_with_compounds(¶ms);
500 typedef enum compile_mode_t {
514 static void usage(const char *argv0)
516 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
519 static void print_cparser_version(void) {
520 printf("cparser (%s) using libFirm (%u.%u",
521 cparser_REVISION, ir_get_version_major(),
522 ir_get_version_minor());
524 const char *revision = ir_get_version_revision();
525 if (revision[0] != 0) {
527 fputs(revision, stdout);
530 const char *build = ir_get_version_build();
533 fputs(build, stdout);
538 static void set_be_option(const char *arg)
540 int res = firm_be_option(arg);
545 static void set_option(const char *arg)
547 int res = firm_option(arg);
552 static void copy_file(FILE *dest, FILE *input)
556 while (!feof(input) && !ferror(dest)) {
557 size_t read = fread(buf, 1, sizeof(buf), input);
558 if (fwrite(buf, 1, read, dest) != read) {
559 perror("couldn't write output");
564 static inline bool streq(char const* a, char const* b)
566 return strcmp(a, b) == 0;
569 static inline bool strstart(char const* str, char const* start)
574 } while (*str++ == *start++);
578 static FILE *open_file(const char *filename)
580 if (streq(filename, "-")) {
584 FILE *in = fopen(filename, "r");
586 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
594 static filetype_t get_filetype_from_string(const char *string)
596 if (streq(string, "c") || streq(string, "c-header"))
598 if (streq(string, "c++") || streq(string, "c++-header"))
600 if (streq(string, "assembler"))
601 return FILETYPE_PREPROCESSED_ASSEMBLER;
602 if (streq(string, "assembler-with-cpp"))
603 return FILETYPE_ASSEMBLER;
604 if (streq(string, "none"))
605 return FILETYPE_AUTODETECT;
607 return FILETYPE_UNKNOWN;
610 static void init_os_support(void)
612 /* OS option must be set to the backend */
613 switch (firm_opt.os_support) {
614 case OS_SUPPORT_MINGW:
615 set_be_option("ia32-gasmode=mingw");
616 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
618 case OS_SUPPORT_LINUX:
619 set_be_option("ia32-gasmode=elf");
621 case OS_SUPPORT_MACHO:
622 set_be_option("ia32-gasmode=macho");
623 set_be_option("ia32-stackalign=4");
624 set_be_option("pic");
629 typedef enum lang_standard_t {
630 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
631 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
632 STANDARD_C89, /* ISO C90 (sic) */
633 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
634 STANDARD_C99, /* ISO C99 */
635 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
636 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
637 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
638 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
641 int main(int argc, char **argv)
645 const char *dumpfunction = NULL;
646 const char *print_file_name_file = NULL;
647 compile_mode_t mode = CompileAssembleLink;
649 int result = EXIT_SUCCESS;
650 char cpu_arch[16] = "ia32";
651 file_list_entry_t *files = NULL;
652 file_list_entry_t *last_file = NULL;
653 bool construct_dep_target = false;
654 struct obstack file_obst;
656 atexit(free_temp_files);
658 /* hack for now... */
659 if (strstr(argv[0], "pptest") != NULL) {
660 extern int pptest_main(int argc, char **argv);
661 return pptest_main(argc, argv);
664 obstack_init(&cppflags_obst);
665 obstack_init(&ldflags_obst);
666 obstack_init(&file_obst);
668 #define GET_ARG_AFTER(def, args) \
669 def = &arg[sizeof(args)-1]; \
670 if (def[0] == '\0') { \
673 fprintf(stderr, "error: expected argument after '" args "'\n"); \
674 argument_errors = true; \
678 if (def[0] == '-' && def[1] != '\0') { \
679 fprintf(stderr, "error: expected argument after '" args "'\n"); \
680 argument_errors = true; \
685 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
687 /* early options parsing (find out optimisation level and OS) */
688 for (int i = 1; i < argc; ++i) {
689 const char *arg = argv[i];
693 const char *option = &arg[1];
694 if (option[0] == 'O') {
695 sscanf(&option[1], "%d", &opt_level);
697 if (strcmp(arg, "-fwin32") == 0) {
698 firm_opt.os_support = OS_SUPPORT_MINGW;
699 } else if (strcmp(arg, "-fmac") == 0) {
700 firm_opt.os_support = OS_SUPPORT_MACHO;
701 } else if (strcmp(arg, "-flinux") == 0) {
702 firm_opt.os_support = OS_SUPPORT_LINUX;
706 /* set target/os specific stuff */
709 /* apply optimisation level */
712 set_option("no-opt");
715 set_option("no-inline");
719 set_option("strict-aliasing");
720 /* use_builtins = true; */
723 set_option("cond-eval");
724 set_option("if-conv");
727 set_option("inline");
728 set_option("deconv");
729 set_be_option("omitfp");
733 /* parse rest of options */
734 lang_standard_t standard = STANDARD_DEFAULT;
735 unsigned features_on = 0;
736 unsigned features_off = 0;
737 filetype_t forced_filetype = FILETYPE_AUTODETECT;
738 bool help_displayed = false;
739 bool argument_errors = false;
740 for (int i = 1; i < argc; ++i) {
741 const char *arg = argv[i];
742 if (arg[0] == '-' && arg[1] != '\0') {
744 const char *option = &arg[1];
745 if (option[0] == 'o') {
746 GET_ARG_AFTER(outname, "-o");
747 } else if (option[0] == 'g') {
748 set_be_option("debuginfo=stabs");
749 set_be_option("omitfp=no");
750 set_be_option("ia32-nooptcc=yes");
751 } else if (SINGLE_OPTION('c')) {
752 mode = CompileAssemble;
753 } else if (SINGLE_OPTION('E')) {
754 mode = PreprocessOnly;
755 } else if (SINGLE_OPTION('S')) {
757 } else if (option[0] == 'O') {
759 } else if (option[0] == 'I') {
761 GET_ARG_AFTER(opt, "-I");
762 add_flag(&cppflags_obst, "-I%s", opt);
763 } else if (option[0] == 'D') {
765 GET_ARG_AFTER(opt, "-D");
766 add_flag(&cppflags_obst, "-D%s", opt);
767 } else if (option[0] == 'U') {
769 GET_ARG_AFTER(opt, "-U");
770 add_flag(&cppflags_obst, "-U%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 (option[0] == 'L') {
777 GET_ARG_AFTER(opt, "-L");
778 add_flag(&ldflags_obst, "-L%s", opt);
779 } else if (SINGLE_OPTION('v')) {
781 } else if (SINGLE_OPTION('w')) {
782 memset(&warning, 0, sizeof(warning));
783 } else if (option[0] == 'x') {
785 GET_ARG_AFTER(opt, "-x");
786 forced_filetype = get_filetype_from_string(opt);
787 if (forced_filetype == FILETYPE_UNKNOWN) {
788 fprintf(stderr, "Unknown language '%s'\n", opt);
789 argument_errors = true;
791 } else if (streq(option, "M")) {
792 mode = PreprocessOnly;
793 add_flag(&cppflags_obst, "-M");
794 } else if (streq(option, "MMD") ||
795 streq(option, "MD")) {
796 construct_dep_target = true;
797 add_flag(&cppflags_obst, "-%s", option);
798 } else if (streq(option, "MM") ||
799 streq(option, "MP")) {
800 add_flag(&cppflags_obst, "-%s", option);
801 } else if (streq(option, "MT") ||
802 streq(option, "MQ") ||
803 streq(option, "MF")) {
805 GET_ARG_AFTER(opt, "-MT");
806 add_flag(&cppflags_obst, "-%s", option);
807 add_flag(&cppflags_obst, "%s", opt);
808 } else if (streq(option, "include")) {
810 GET_ARG_AFTER(opt, "-include");
811 add_flag(&cppflags_obst, "-include");
812 add_flag(&cppflags_obst, "%s", opt);
813 } else if (streq(option, "isystem")) {
815 GET_ARG_AFTER(opt, "-isystem");
816 add_flag(&cppflags_obst, "-isystem");
817 add_flag(&cppflags_obst, "%s", opt);
818 } else if (streq(option, "nostdinc")
819 || streq(option, "trigraphs")) {
820 /* pass these through to the preprocessor */
821 add_flag(&cppflags_obst, "%s", arg);
822 } else if (streq(option, "pipe")) {
823 /* here for gcc compatibility */
824 } else if (option[0] == 'f') {
825 char const *orig_opt;
826 GET_ARG_AFTER(orig_opt, "-f");
828 if (strstart(orig_opt, "align-loops=") ||
829 strstart(orig_opt, "align-jumps=") ||
830 strstart(orig_opt, "align-functions=")) {
831 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
832 } else if (streq(orig_opt, "verbose-asm")) {
833 /* ignore: we always print verbose assembler */
835 char const *opt = orig_opt;
836 bool truth_value = true;
837 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
842 if (streq(opt, "dollars-in-identifiers")) {
843 allow_dollar_in_symbol = truth_value;
844 } if (streq(opt, "builtins")) {
845 use_builtins = truth_value;
846 } else if (streq(opt, "short-wchar")) {
847 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
849 } else if (streq(opt, "syntax-only")) {
850 mode = truth_value ? ParseOnly : CompileAssembleLink;
851 } else if (streq(opt, "omit-frame-pointer")) {
852 set_be_option(truth_value ? "omitfp" : "omitfp=no");
853 } else if (streq(opt, "strength-reduce")) {
854 firm_option(truth_value ? "strength-red" : "no-strength-red");
855 } else if (streq(opt, "fast-math") ||
856 streq(opt, "jump-tables") ||
857 streq(opt, "unroll-loops") ||
858 streq(opt, "expensive-optimizations") ||
859 streq(opt, "common") ||
861 streq(opt, "align-loops") ||
862 streq(opt, "align-jumps") ||
863 streq(opt, "align-functions")) {
864 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
866 int res = firm_option(orig_opt);
868 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
870 argument_errors = true;
872 } else if (res == -1) {
873 help_displayed = true;
877 } else if (option[0] == 'b') {
879 GET_ARG_AFTER(opt, "-b");
880 int res = firm_be_option(opt);
882 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
884 argument_errors = true;
885 } else if (res == -1) {
886 help_displayed = true;
887 } else if (strstart(opt, "isa=")) {
888 strncpy(cpu_arch, opt, sizeof(cpu_arch));
890 } else if (option[0] == 'W') {
891 if (strstart(option + 1, "p,")) {
892 // pass options directly to the preprocessor
894 GET_ARG_AFTER(opt, "-Wp,");
895 add_flag(&cppflags_obst, "-Wp,%s", opt);
896 } else if (strstart(option + 1, "l,")) {
897 // pass options directly to the linker
899 GET_ARG_AFTER(opt, "-Wl,");
900 add_flag(&ldflags_obst, "-Wl,%s", opt);
901 } else if (streq(option + 1, "no-trigraphs")
902 || streq(option + 1, "undef")) {
903 add_flag(&cppflags_obst, "%s", arg);
905 set_warning_opt(&option[1]);
907 } else if (option[0] == 'm') {
912 GET_ARG_AFTER(opt, "-m");
913 if (strstart(opt, "arch=")) {
914 GET_ARG_AFTER(opt, "-march=");
915 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
916 int res = firm_be_option(arch_opt);
918 argument_errors = true;
920 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
921 int res = firm_be_option(arch_opt);
923 argument_errors = true;
925 } else if (strstart(opt, "tune=")) {
926 GET_ARG_AFTER(opt, "-mtune=");
927 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
928 int res = firm_be_option(arch_opt);
930 argument_errors = true;
931 } else if (strstart(opt, "cpu=")) {
932 GET_ARG_AFTER(opt, "-mcpu=");
933 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
934 int res = firm_be_option(arch_opt);
936 argument_errors = true;
937 } else if (strstart(opt, "fpmath=")) {
938 GET_ARG_AFTER(opt, "-mfpmath=");
939 if (streq(opt, "387"))
941 else if (streq(opt, "sse"))
944 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
945 argument_errors = true;
947 if (!argument_errors) {
948 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
949 int res = firm_be_option(arch_opt);
951 argument_errors = true;
953 } else if (strstart(opt, "preferred-stack-boundary=")) {
954 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
955 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
956 int res = firm_be_option(arch_opt);
958 argument_errors = true;
959 } else if (streq(opt, "omit-leaf-frame-pointer")) {
960 set_be_option("omitleaffp=1");
961 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
962 set_be_option("omitleaffp=0");
965 long int value = strtol(opt, &endptr, 10);
966 if (*endptr != '\0') {
967 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
968 argument_errors = true;
970 if (value != 16 && value != 32 && value != 64) {
971 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
972 argument_errors = true;
974 machine_size = (unsigned int)value;
977 } else if (streq(option, "pg")) {
978 set_be_option("gprof");
979 add_flag(&ldflags_obst, "-pg");
980 } else if (streq(option, "pedantic") ||
981 streq(option, "ansi")) {
982 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
983 } else if (streq(option, "shared")) {
984 add_flag(&ldflags_obst, "-shared");
985 } else if (strstart(option, "std=")) {
986 const char *const o = &option[4];
988 streq(o, "c++") ? STANDARD_CXX98 :
989 streq(o, "c++98") ? STANDARD_CXX98 :
990 streq(o, "c89") ? STANDARD_C89 :
991 streq(o, "c99") ? STANDARD_C99 :
992 streq(o, "c9x") ? STANDARD_C99 : // deprecated
993 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
994 streq(o, "gnu89") ? STANDARD_GNU89 :
995 streq(o, "gnu99") ? STANDARD_GNU99 :
996 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
997 streq(o, "iso9899:1990") ? STANDARD_C89 :
998 streq(o, "iso9899:199409") ? STANDARD_C90 :
999 streq(o, "iso9899:1999") ? STANDARD_C99 :
1000 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1001 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1002 } else if (streq(option, "version")) {
1003 print_cparser_version();
1004 } else if (strstart(option, "print-file-name=")) {
1005 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1006 } else if (option[0] == '-') {
1007 /* double dash option */
1009 if (streq(option, "gcc")) {
1010 features_on |= _GNUC;
1011 features_off &= ~_GNUC;
1012 } else if (streq(option, "no-gcc")) {
1013 features_on &= ~_GNUC;
1014 features_off |= _GNUC;
1015 } else if (streq(option, "ms")) {
1017 features_off &= ~_MS;
1018 } else if (streq(option, "no-ms")) {
1019 features_on &= ~_MS;
1020 features_off |= _MS;
1021 } else if (streq(option, "signed-chars")) {
1022 char_is_signed = true;
1023 } else if (streq(option, "unsigned-chars")) {
1024 char_is_signed = false;
1025 } else if (streq(option, "strict")) {
1027 } else if (streq(option, "lextest")) {
1029 } else if (streq(option, "benchmark")) {
1030 mode = BenchmarkParser;
1031 } else if (streq(option, "print-ast")) {
1033 } else if (streq(option, "print-implicit-cast")) {
1034 print_implicit_casts = true;
1035 } else if (streq(option, "print-parenthesis")) {
1036 print_parenthesis = true;
1037 } else if (streq(option, "print-fluffy")) {
1039 } else if (streq(option, "print-caml")) {
1041 } else if (streq(option, "version")) {
1042 print_cparser_version();
1044 } else if (streq(option, "dump-function")) {
1047 fprintf(stderr, "error: "
1048 "expected argument after '--dump-function'\n");
1049 argument_errors = true;
1052 dumpfunction = argv[i];
1055 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1056 argument_errors = true;
1059 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1060 argument_errors = true;
1063 filetype_t type = forced_filetype;
1064 if (type == FILETYPE_AUTODETECT) {
1065 if (streq(arg, "-")) {
1066 /* - implicitly means C source file */
1069 const char *suffix = strrchr(arg, '.');
1070 /* Ensure there is at least one char before the suffix */
1071 if (suffix != NULL && suffix != arg) {
1074 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1075 streq(suffix, "a") ? FILETYPE_OBJECT :
1076 streq(suffix, "c") ? FILETYPE_C :
1077 streq(suffix, "cc") ? FILETYPE_CXX :
1078 streq(suffix, "cpp") ? FILETYPE_CXX :
1079 streq(suffix, "cxx") ? FILETYPE_CXX :
1080 streq(suffix, "h") ? FILETYPE_C :
1081 streq(suffix, "o") ? FILETYPE_OBJECT :
1082 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1083 streq(suffix, "so") ? FILETYPE_OBJECT :
1084 FILETYPE_AUTODETECT;
1088 if (type == FILETYPE_AUTODETECT) {
1089 fprintf(stderr, "'%s': file format not recognized\n", arg);
1094 file_list_entry_t *entry
1095 = obstack_alloc(&file_obst, sizeof(entry[0]));
1096 memset(entry, 0, sizeof(entry[0]));
1100 if (last_file != NULL) {
1101 last_file->next = entry;
1109 if (print_file_name_file != NULL) {
1110 print_file_name(print_file_name_file);
1114 if (files == NULL) {
1115 fprintf(stderr, "error: no input files specified\n");
1116 argument_errors = true;
1119 if (help_displayed) {
1120 return !argument_errors;
1122 if (argument_errors) {
1127 /* we do the lowering in ast2firm */
1128 firm_opt.lower_bitfields = FALSE;
1131 init_symbol_table();
1141 if (construct_dep_target) {
1142 if (outname != 0 && strlen(outname) >= 2) {
1143 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1145 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1148 dep_target[0] = '\0';
1151 char outnamebuf[4096];
1152 if (outname == NULL) {
1153 const char *filename = files->name;
1156 case BenchmarkParser:
1161 case PreprocessOnly:
1166 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1167 outname = outnamebuf;
1169 case CompileAssemble:
1170 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1171 outname = outnamebuf;
1174 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1176 outname = outnamebuf;
1178 case CompileAssembleLink:
1188 assert(outname != NULL);
1191 if (streq(outname, "-")) {
1194 out = fopen(outname, "w");
1196 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1202 file_list_entry_t *file;
1203 for (file = files; file != NULL; file = file->next) {
1204 char asm_tempfile[1024];
1205 const char *filename = file->name;
1206 filetype_t filetype = file->type;
1208 if (filetype == FILETYPE_OBJECT)
1212 if (mode == LexTest) {
1214 in = open_file(filename);
1215 lextest(in, filename);
1220 FILE *preprocessed_in = NULL;
1223 filetype = FILETYPE_PREPROCESSED_C;
1226 filetype = FILETYPE_PREPROCESSED_CXX;
1228 case FILETYPE_ASSEMBLER:
1229 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1230 add_flag(&cppflags_obst, "-x");
1231 add_flag(&cppflags_obst, "assembler-with-cpp");
1234 /* no support for input on FILE* yet */
1236 panic("internal compiler error: in for preprocessor != NULL");
1238 preprocessed_in = preprocess(filename);
1239 if (mode == PreprocessOnly) {
1240 copy_file(out, preprocessed_in);
1241 int result = pclose(preprocessed_in);
1243 /* remove output file in case of error */
1244 if (out != stdout && result != EXIT_SUCCESS) {
1250 in = preprocessed_in;
1258 if (mode == Compile) {
1261 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1265 in = open_file(filename);
1267 /* preprocess and compile */
1268 if (filetype == FILETYPE_PREPROCESSED_C) {
1269 char const* invalid_mode;
1272 case STANDARD_C89: c_mode = _C89; break;
1273 /* TODO ^v determine difference between these two */
1274 case STANDARD_C90: c_mode = _C89; break;
1275 case STANDARD_C99: c_mode = _C89 | _C99; break;
1276 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1280 "warning: command line option \"-std=%s\" is not valid for C\n",
1283 case STANDARD_DEFAULT:
1284 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1286 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1287 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1290 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1291 char const* invalid_mode;
1293 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1294 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1295 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1296 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1297 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1300 case STANDARD_CXX98: c_mode = _CXX; break;
1304 "warning: command line option \"-std=%s\" is not valid for C++\n",
1306 case STANDARD_DEFAULT:
1307 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1311 c_mode |= features_on;
1312 c_mode &= ~features_off;
1314 translation_unit_t *const unit = do_parsing(in, filename);
1316 /* prints the AST even if errors occurred */
1317 if (mode == PrintAst) {
1318 type_set_output(out);
1319 ast_set_output(out);
1323 if (error_count > 0) {
1324 /* parsing failed because of errors */
1325 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1327 result = EXIT_FAILURE;
1329 } else if (warning_count > 0) {
1330 fprintf(stderr, "%u warning(s)\n", warning_count);
1333 if (in == preprocessed_in) {
1334 int pp_result = pclose(preprocessed_in);
1335 if (pp_result != EXIT_SUCCESS) {
1336 /* remove output file */
1343 if (mode == BenchmarkParser) {
1345 } else if (mode == PrintFluffy) {
1346 write_fluffy_decls(out, unit);
1348 } else if (mode == PrintCaml) {
1349 write_caml_decls(out, unit);
1353 translation_unit_to_firm(unit);
1355 if (mode == ParseOnly) {
1359 if (mode == CompileDump) {
1361 ident *id = new_id_from_str(dumpfunction);
1362 ir_graph *irg = NULL;
1363 int n_irgs = get_irp_n_irgs();
1364 for (int i = 0; i < n_irgs; ++i) {
1365 ir_graph *tirg = get_irp_irg(i);
1366 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1374 fprintf(stderr, "No graph for function '%s' found\n",
1379 dump_ir_block_graph_file(irg, out);
1384 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1385 have_const_functions);
1386 if (asm_out != out) {
1389 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1390 copy_file(asm_out, in);
1391 if (in == preprocessed_in) {
1392 int pp_result = pclose(preprocessed_in);
1393 if (pp_result != EXIT_SUCCESS) {
1394 /* remove output in error case */
1400 if (asm_out != out) {
1405 if (mode == Compile)
1408 /* if we're here then we have preprocessed assembly */
1409 filename = asm_tempfile;
1410 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1413 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1415 const char *filename_o;
1416 if (mode == CompileAssemble) {
1418 filename_o = outname;
1420 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1425 assemble(filename_o, filename);
1427 size_t len = strlen(filename_o) + 1;
1428 filename = obstack_copy(&file_obst, filename_o, len);
1429 filetype = FILETYPE_OBJECT;
1432 /* ok we're done here, process next file */
1433 file->name = filename;
1434 file->type = filetype;
1437 if (result != EXIT_SUCCESS) {
1443 /* link program file */
1444 if (mode == CompileAssembleLink) {
1445 obstack_1grow(&ldflags_obst, '\0');
1446 const char *flags = obstack_finish(&ldflags_obst);
1448 /* construct commandline */
1449 obstack_printf(&file_obst, "%s", LINKER);
1450 for (file_list_entry_t *entry = files; entry != NULL;
1451 entry = entry->next) {
1452 if (entry->type != FILETYPE_OBJECT)
1455 add_flag(&file_obst, "%s", entry->name);
1458 add_flag(&file_obst, "-o");
1459 add_flag(&file_obst, outname);
1460 obstack_printf(&file_obst, "%s", flags);
1461 obstack_1grow(&file_obst, '\0');
1463 char *commandline = obstack_finish(&file_obst);
1468 int err = system(commandline);
1469 if (err != EXIT_SUCCESS) {
1470 fprintf(stderr, "linker reported an error\n");
1475 obstack_free(&cppflags_obst, NULL);
1476 obstack_free(&ldflags_obst, NULL);
1477 obstack_free(&file_obst, NULL);
1487 exit_symbol_table();