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) {
521 firm_get_version(&ver);
523 printf("cparser (%s) using libFirm (%u.%u",
524 cparser_REVISION, ver.major, ver.minor);
525 if (ver.revision[0] != 0) {
527 fputs(ver.revision, stdout);
529 if (ver.build[0] != 0) {
531 fputs(ver.build, stdout);
536 static void set_be_option(const char *arg)
538 int res = firm_be_option(arg);
543 static void set_option(const char *arg)
545 int res = firm_option(arg);
550 static void copy_file(FILE *dest, FILE *input)
554 while (!feof(input) && !ferror(dest)) {
555 size_t read = fread(buf, 1, sizeof(buf), input);
556 if (fwrite(buf, 1, read, dest) != read) {
557 perror("couldn't write output");
562 static inline bool streq(char const* a, char const* b)
564 return strcmp(a, b) == 0;
567 static inline bool strstart(char const* str, char const* start)
572 } while (*str++ == *start++);
576 static FILE *open_file(const char *filename)
578 if (streq(filename, "-")) {
582 FILE *in = fopen(filename, "r");
584 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
592 static filetype_t get_filetype_from_string(const char *string)
594 if (streq(string, "c") || streq(string, "c-header"))
596 if (streq(string, "c++") || streq(string, "c++-header"))
598 if (streq(string, "assembler"))
599 return FILETYPE_PREPROCESSED_ASSEMBLER;
600 if (streq(string, "assembler-with-cpp"))
601 return FILETYPE_ASSEMBLER;
602 if (streq(string, "none"))
603 return FILETYPE_AUTODETECT;
605 return FILETYPE_UNKNOWN;
608 static void init_os_support(void)
610 /* OS option must be set to the backend */
611 switch (firm_opt.os_support) {
612 case OS_SUPPORT_MINGW:
613 set_be_option("ia32-gasmode=mingw");
614 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
616 case OS_SUPPORT_LINUX:
617 set_be_option("ia32-gasmode=elf");
619 case OS_SUPPORT_MACHO:
620 set_be_option("ia32-gasmode=macho");
621 set_be_option("ia32-stackalign=4");
622 set_be_option("pic");
627 typedef enum lang_standard_t {
628 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
629 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
630 STANDARD_C89, /* ISO C90 (sic) */
631 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
632 STANDARD_C99, /* ISO C99 */
633 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
634 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
635 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
636 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
639 int main(int argc, char **argv)
643 const char *dumpfunction = NULL;
644 const char *print_file_name_file = NULL;
645 compile_mode_t mode = CompileAssembleLink;
647 int result = EXIT_SUCCESS;
648 char cpu_arch[16] = "ia32";
649 file_list_entry_t *files = NULL;
650 file_list_entry_t *last_file = NULL;
651 bool construct_dep_target = false;
652 struct obstack file_obst;
654 atexit(free_temp_files);
656 /* hack for now... */
657 if (strstr(argv[0], "pptest") != NULL) {
658 extern int pptest_main(int argc, char **argv);
659 return pptest_main(argc, argv);
662 obstack_init(&cppflags_obst);
663 obstack_init(&ldflags_obst);
664 obstack_init(&file_obst);
666 #define GET_ARG_AFTER(def, args) \
667 def = &arg[sizeof(args)-1]; \
668 if (def[0] == '\0') { \
671 fprintf(stderr, "error: expected argument after '" args "'\n"); \
672 argument_errors = true; \
676 if (def[0] == '-' && def[1] != '\0') { \
677 fprintf(stderr, "error: expected argument after '" args "'\n"); \
678 argument_errors = true; \
683 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
685 /* early options parsing (find out optimisation level and OS) */
686 for (int i = 1; i < argc; ++i) {
687 const char *arg = argv[i];
691 const char *option = &arg[1];
692 if (option[0] == 'O') {
693 sscanf(&option[1], "%d", &opt_level);
695 if (strcmp(arg, "-fwin32") == 0) {
696 firm_opt.os_support = OS_SUPPORT_MINGW;
697 } else if (strcmp(arg, "-fmac") == 0) {
698 firm_opt.os_support = OS_SUPPORT_MACHO;
699 } else if (strcmp(arg, "-flinux") == 0) {
700 firm_opt.os_support = OS_SUPPORT_LINUX;
704 /* set target/os specific stuff */
707 /* apply optimisation level */
710 set_option("no-opt");
713 set_option("no-inline");
717 set_option("strict-aliasing");
718 /* use_builtins = true; */
721 set_option("cond-eval");
722 set_option("if-conv");
725 set_option("inline");
726 set_option("deconv");
727 set_be_option("omitfp");
731 /* parse rest of options */
732 lang_standard_t standard = STANDARD_DEFAULT;
733 unsigned features_on = 0;
734 unsigned features_off = 0;
735 filetype_t forced_filetype = FILETYPE_AUTODETECT;
736 bool help_displayed = false;
737 bool argument_errors = false;
738 for (int i = 1; i < argc; ++i) {
739 const char *arg = argv[i];
740 if (arg[0] == '-' && arg[1] != '\0') {
742 const char *option = &arg[1];
743 if (option[0] == 'o') {
744 GET_ARG_AFTER(outname, "-o");
745 } else if (option[0] == 'g') {
746 set_be_option("debuginfo=stabs");
747 set_be_option("omitfp=no");
748 set_be_option("ia32-nooptcc=yes");
749 } else if (SINGLE_OPTION('c')) {
750 mode = CompileAssemble;
751 } else if (SINGLE_OPTION('E')) {
752 mode = PreprocessOnly;
753 } else if (SINGLE_OPTION('S')) {
755 } else if (option[0] == 'O') {
757 } else if (option[0] == 'I') {
759 GET_ARG_AFTER(opt, "-I");
760 add_flag(&cppflags_obst, "-I%s", opt);
761 } else if (option[0] == 'D') {
763 GET_ARG_AFTER(opt, "-D");
764 add_flag(&cppflags_obst, "-D%s", opt);
765 } else if (option[0] == 'U') {
767 GET_ARG_AFTER(opt, "-U");
768 add_flag(&cppflags_obst, "-U%s", opt);
769 } else if (option[0] == 'l') {
771 GET_ARG_AFTER(opt, "-l");
772 add_flag(&ldflags_obst, "-l%s", opt);
773 } else if (option[0] == 'L') {
775 GET_ARG_AFTER(opt, "-L");
776 add_flag(&ldflags_obst, "-L%s", opt);
777 } else if (SINGLE_OPTION('v')) {
779 } else if (SINGLE_OPTION('w')) {
780 memset(&warning, 0, sizeof(warning));
781 } else if (option[0] == 'x') {
783 GET_ARG_AFTER(opt, "-x");
784 forced_filetype = get_filetype_from_string(opt);
785 if (forced_filetype == FILETYPE_UNKNOWN) {
786 fprintf(stderr, "Unknown language '%s'\n", opt);
787 argument_errors = true;
789 } else if (streq(option, "M")) {
790 mode = PreprocessOnly;
791 add_flag(&cppflags_obst, "-M");
792 } else if (streq(option, "MMD") ||
793 streq(option, "MD")) {
794 construct_dep_target = true;
795 add_flag(&cppflags_obst, "-%s", option);
796 } else if (streq(option, "MM") ||
797 streq(option, "MP")) {
798 add_flag(&cppflags_obst, "-%s", option);
799 } else if (streq(option, "MT") ||
800 streq(option, "MQ") ||
801 streq(option, "MF")) {
803 GET_ARG_AFTER(opt, "-MT");
804 add_flag(&cppflags_obst, "-%s", option);
805 add_flag(&cppflags_obst, "%s", opt);
806 } else if (streq(option, "include")) {
808 GET_ARG_AFTER(opt, "-include");
809 add_flag(&cppflags_obst, "-include");
810 add_flag(&cppflags_obst, "%s", opt);
811 } else if (streq(option, "isystem")) {
813 GET_ARG_AFTER(opt, "-isystem");
814 add_flag(&cppflags_obst, "-isystem");
815 add_flag(&cppflags_obst, "%s", opt);
816 } else if (streq(option, "nostdinc")
817 || streq(option, "trigraphs")) {
818 /* pass these through to the preprocessor */
819 add_flag(&cppflags_obst, "%s", arg);
820 } else if (streq(option, "pipe")) {
821 /* here for gcc compatibility */
822 } else if (option[0] == 'f') {
823 char const *orig_opt;
824 GET_ARG_AFTER(orig_opt, "-f");
826 if (strstart(orig_opt, "align-loops=") ||
827 strstart(orig_opt, "align-jumps=") ||
828 strstart(orig_opt, "align-functions=")) {
829 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
830 } else if (streq(orig_opt, "verbose-asm")) {
831 /* ignore: we always print verbose assembler */
833 char const *opt = orig_opt;
834 bool truth_value = true;
835 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
840 if (streq(opt, "dollars-in-identifiers")) {
841 allow_dollar_in_symbol = truth_value;
842 } if (streq(opt, "builtins")) {
843 use_builtins = truth_value;
844 } else if (streq(opt, "short-wchar")) {
845 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
847 } else if (streq(opt, "syntax-only")) {
848 mode = truth_value ? ParseOnly : CompileAssembleLink;
849 } else if (streq(opt, "omit-frame-pointer")) {
850 set_be_option(truth_value ? "omitfp" : "omitfp=no");
851 } else if (streq(opt, "strength-reduce")) {
852 firm_option(truth_value ? "strength-red" : "no-strength-red");
853 } else if (streq(opt, "fast-math") ||
854 streq(opt, "jump-tables") ||
855 streq(opt, "unroll-loops") ||
856 streq(opt, "expensive-optimizations") ||
857 streq(opt, "common") ||
859 streq(opt, "align-loops") ||
860 streq(opt, "align-jumps") ||
861 streq(opt, "align-functions")) {
862 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
864 int res = firm_option(orig_opt);
866 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
868 argument_errors = true;
870 } else if (res == -1) {
871 help_displayed = true;
875 } else if (option[0] == 'b') {
877 GET_ARG_AFTER(opt, "-b");
878 int res = firm_be_option(opt);
880 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
882 argument_errors = true;
883 } else if (res == -1) {
884 help_displayed = true;
885 } else if (strstart(opt, "isa=")) {
886 strncpy(cpu_arch, opt, sizeof(cpu_arch));
888 } else if (option[0] == 'W') {
889 if (strstart(option + 1, "p,")) {
890 // pass options directly to the preprocessor
892 GET_ARG_AFTER(opt, "-Wp,");
893 add_flag(&cppflags_obst, "-Wp,%s", opt);
894 } else if (strstart(option + 1, "l,")) {
895 // pass options directly to the linker
897 GET_ARG_AFTER(opt, "-Wl,");
898 add_flag(&ldflags_obst, "-Wl,%s", opt);
899 } else if (streq(option + 1, "no-trigraphs")
900 || streq(option + 1, "undef")) {
901 add_flag(&cppflags_obst, "%s", arg);
903 set_warning_opt(&option[1]);
905 } else if (option[0] == 'm') {
910 GET_ARG_AFTER(opt, "-m");
911 if (strstart(opt, "arch=")) {
912 GET_ARG_AFTER(opt, "-march=");
913 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
914 int res = firm_be_option(arch_opt);
916 argument_errors = true;
918 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
919 int res = firm_be_option(arch_opt);
921 argument_errors = true;
923 } else if (strstart(opt, "tune=")) {
924 GET_ARG_AFTER(opt, "-mtune=");
925 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
926 int res = firm_be_option(arch_opt);
928 argument_errors = true;
929 } else if (strstart(opt, "cpu=")) {
930 GET_ARG_AFTER(opt, "-mcpu=");
931 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
932 int res = firm_be_option(arch_opt);
934 argument_errors = true;
935 } else if (strstart(opt, "fpmath=")) {
936 GET_ARG_AFTER(opt, "-mfpmath=");
937 if (streq(opt, "387"))
939 else if (streq(opt, "sse"))
942 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
943 argument_errors = true;
945 if (!argument_errors) {
946 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
947 int res = firm_be_option(arch_opt);
949 argument_errors = true;
951 } else if (strstart(opt, "preferred-stack-boundary=")) {
952 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
953 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
954 int res = firm_be_option(arch_opt);
956 argument_errors = true;
957 } else if (streq(opt, "omit-leaf-frame-pointer")) {
958 set_be_option("omitleaffp=1");
959 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
960 set_be_option("omitleaffp=0");
963 long int value = strtol(opt, &endptr, 10);
964 if (*endptr != '\0') {
965 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
966 argument_errors = true;
968 if (value != 16 && value != 32 && value != 64) {
969 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
970 argument_errors = true;
972 machine_size = (unsigned int)value;
975 } else if (streq(option, "pg")) {
976 set_be_option("gprof");
977 add_flag(&ldflags_obst, "-pg");
978 } else if (streq(option, "pedantic") ||
979 streq(option, "ansi")) {
980 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
981 } else if (streq(option, "shared")) {
982 add_flag(&ldflags_obst, "-shared");
983 } else if (strstart(option, "std=")) {
984 const char *const o = &option[4];
986 streq(o, "c++") ? STANDARD_CXX98 :
987 streq(o, "c++98") ? STANDARD_CXX98 :
988 streq(o, "c89") ? STANDARD_C89 :
989 streq(o, "c99") ? STANDARD_C99 :
990 streq(o, "c9x") ? STANDARD_C99 : // deprecated
991 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
992 streq(o, "gnu89") ? STANDARD_GNU89 :
993 streq(o, "gnu99") ? STANDARD_GNU99 :
994 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
995 streq(o, "iso9899:1990") ? STANDARD_C89 :
996 streq(o, "iso9899:199409") ? STANDARD_C90 :
997 streq(o, "iso9899:1999") ? STANDARD_C99 :
998 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
999 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1000 } else if (streq(option, "version")) {
1001 print_cparser_version();
1002 } else if (strstart(option, "print-file-name=")) {
1003 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1004 } else if (option[0] == '-') {
1005 /* double dash option */
1007 if (streq(option, "gcc")) {
1008 features_on |= _GNUC;
1009 features_off &= ~_GNUC;
1010 } else if (streq(option, "no-gcc")) {
1011 features_on &= ~_GNUC;
1012 features_off |= _GNUC;
1013 } else if (streq(option, "ms")) {
1015 features_off &= ~_MS;
1016 } else if (streq(option, "no-ms")) {
1017 features_on &= ~_MS;
1018 features_off |= _MS;
1019 } else if (streq(option, "signed-chars")) {
1020 char_is_signed = true;
1021 } else if (streq(option, "unsigned-chars")) {
1022 char_is_signed = false;
1023 } else if (streq(option, "strict")) {
1025 } else if (streq(option, "lextest")) {
1027 } else if (streq(option, "benchmark")) {
1028 mode = BenchmarkParser;
1029 } else if (streq(option, "print-ast")) {
1031 } else if (streq(option, "print-implicit-cast")) {
1032 print_implicit_casts = true;
1033 } else if (streq(option, "print-parenthesis")) {
1034 print_parenthesis = true;
1035 } else if (streq(option, "print-fluffy")) {
1037 } else if (streq(option, "print-caml")) {
1039 } else if (streq(option, "version")) {
1040 print_cparser_version();
1042 } else if (streq(option, "dump-function")) {
1045 fprintf(stderr, "error: "
1046 "expected argument after '--dump-function'\n");
1047 argument_errors = true;
1050 dumpfunction = argv[i];
1053 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1054 argument_errors = true;
1057 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1058 argument_errors = true;
1061 filetype_t type = forced_filetype;
1062 if (type == FILETYPE_AUTODETECT) {
1063 if (streq(arg, "-")) {
1064 /* - implicitly means C source file */
1067 const char *suffix = strrchr(arg, '.');
1068 /* Ensure there is at least one char before the suffix */
1069 if (suffix != NULL && suffix != arg) {
1072 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1073 streq(suffix, "a") ? FILETYPE_OBJECT :
1074 streq(suffix, "c") ? FILETYPE_C :
1075 streq(suffix, "cc") ? FILETYPE_CXX :
1076 streq(suffix, "cpp") ? FILETYPE_CXX :
1077 streq(suffix, "cxx") ? FILETYPE_CXX :
1078 streq(suffix, "h") ? FILETYPE_C :
1079 streq(suffix, "o") ? FILETYPE_OBJECT :
1080 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1081 streq(suffix, "so") ? FILETYPE_OBJECT :
1082 FILETYPE_AUTODETECT;
1086 if (type == FILETYPE_AUTODETECT) {
1087 fprintf(stderr, "'%s': file format not recognized\n", arg);
1092 file_list_entry_t *entry
1093 = obstack_alloc(&file_obst, sizeof(entry[0]));
1094 memset(entry, 0, sizeof(entry[0]));
1098 if (last_file != NULL) {
1099 last_file->next = entry;
1107 if (print_file_name_file != NULL) {
1108 print_file_name(print_file_name_file);
1112 if (files == NULL) {
1113 fprintf(stderr, "error: no input files specified\n");
1114 argument_errors = true;
1117 if (help_displayed) {
1118 return !argument_errors;
1120 if (argument_errors) {
1125 /* we do the lowering in ast2firm */
1126 firm_opt.lower_bitfields = FALSE;
1129 init_symbol_table();
1139 if (construct_dep_target) {
1140 if (outname != 0 && strlen(outname) >= 2) {
1141 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1143 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1146 dep_target[0] = '\0';
1149 char outnamebuf[4096];
1150 if (outname == NULL) {
1151 const char *filename = files->name;
1154 case BenchmarkParser:
1159 case PreprocessOnly:
1164 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1165 outname = outnamebuf;
1167 case CompileAssemble:
1168 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1169 outname = outnamebuf;
1172 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1174 outname = outnamebuf;
1176 case CompileAssembleLink:
1186 assert(outname != NULL);
1189 if (streq(outname, "-")) {
1192 out = fopen(outname, "w");
1194 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1200 file_list_entry_t *file;
1201 for (file = files; file != NULL; file = file->next) {
1202 char asm_tempfile[1024];
1203 const char *filename = file->name;
1204 filetype_t filetype = file->type;
1206 if (filetype == FILETYPE_OBJECT)
1210 if (mode == LexTest) {
1212 in = open_file(filename);
1213 lextest(in, filename);
1218 FILE *preprocessed_in = NULL;
1221 filetype = FILETYPE_PREPROCESSED_C;
1224 filetype = FILETYPE_PREPROCESSED_CXX;
1226 case FILETYPE_ASSEMBLER:
1227 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1228 add_flag(&cppflags_obst, "-x");
1229 add_flag(&cppflags_obst, "assembler-with-cpp");
1232 /* no support for input on FILE* yet */
1234 panic("internal compiler error: in for preprocessor != NULL");
1236 preprocessed_in = preprocess(filename);
1237 if (mode == PreprocessOnly) {
1238 copy_file(out, preprocessed_in);
1239 int result = pclose(preprocessed_in);
1241 /* remove output file in case of error */
1242 if (out != stdout && result != EXIT_SUCCESS) {
1248 in = preprocessed_in;
1256 if (mode == Compile) {
1259 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1263 in = open_file(filename);
1265 /* preprocess and compile */
1266 if (filetype == FILETYPE_PREPROCESSED_C) {
1267 char const* invalid_mode;
1270 case STANDARD_C89: c_mode = _C89; break;
1271 /* TODO ^v determine difference between these two */
1272 case STANDARD_C90: c_mode = _C89; break;
1273 case STANDARD_C99: c_mode = _C89 | _C99; break;
1274 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1278 "warning: command line option \"-std=%s\" is not valid for C\n",
1281 case STANDARD_DEFAULT:
1282 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1284 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1285 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1288 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1289 char const* invalid_mode;
1291 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1292 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1293 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1294 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1295 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1298 case STANDARD_CXX98: c_mode = _CXX; break;
1302 "warning: command line option \"-std=%s\" is not valid for C++\n",
1304 case STANDARD_DEFAULT:
1305 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1309 c_mode |= features_on;
1310 c_mode &= ~features_off;
1312 translation_unit_t *const unit = do_parsing(in, filename);
1314 /* prints the AST even if errors occurred */
1315 if (mode == PrintAst) {
1316 type_set_output(out);
1317 ast_set_output(out);
1321 if (error_count > 0) {
1322 /* parsing failed because of errors */
1323 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1325 result = EXIT_FAILURE;
1327 } else if (warning_count > 0) {
1328 fprintf(stderr, "%u warning(s)\n", warning_count);
1331 if (in == preprocessed_in) {
1332 int pp_result = pclose(preprocessed_in);
1333 if (pp_result != EXIT_SUCCESS) {
1334 /* remove output file */
1341 if (mode == BenchmarkParser) {
1343 } else if (mode == PrintFluffy) {
1344 write_fluffy_decls(out, unit);
1346 } else if (mode == PrintCaml) {
1347 write_caml_decls(out, unit);
1351 translation_unit_to_firm(unit);
1353 if (mode == ParseOnly) {
1357 if (mode == CompileDump) {
1359 ident *id = new_id_from_str(dumpfunction);
1360 ir_graph *irg = NULL;
1361 int n_irgs = get_irp_n_irgs();
1362 for (int i = 0; i < n_irgs; ++i) {
1363 ir_graph *tirg = get_irp_irg(i);
1364 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1372 fprintf(stderr, "No graph for function '%s' found\n",
1377 dump_ir_block_graph_file(irg, out);
1382 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1383 have_const_functions);
1384 if (asm_out != out) {
1387 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1388 copy_file(asm_out, in);
1389 if (in == preprocessed_in) {
1390 int pp_result = pclose(preprocessed_in);
1391 if (pp_result != EXIT_SUCCESS) {
1392 /* remove output in error case */
1398 if (asm_out != out) {
1403 if (mode == Compile)
1406 /* if we're here then we have preprocessed assembly */
1407 filename = asm_tempfile;
1408 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1411 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1413 const char *filename_o;
1414 if (mode == CompileAssemble) {
1416 filename_o = outname;
1418 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1423 assemble(filename_o, filename);
1425 size_t len = strlen(filename_o) + 1;
1426 filename = obstack_copy(&file_obst, filename_o, len);
1427 filetype = FILETYPE_OBJECT;
1430 /* ok we're done here, process next file */
1431 file->name = filename;
1432 file->type = filetype;
1435 if (result != EXIT_SUCCESS) {
1441 /* link program file */
1442 if (mode == CompileAssembleLink) {
1443 obstack_1grow(&ldflags_obst, '\0');
1444 const char *flags = obstack_finish(&ldflags_obst);
1446 /* construct commandline */
1447 obstack_printf(&file_obst, "%s", LINKER);
1448 for (file_list_entry_t *entry = files; entry != NULL;
1449 entry = entry->next) {
1450 if (entry->type != FILETYPE_OBJECT)
1453 add_flag(&file_obst, "%s", entry->name);
1456 add_flag(&file_obst, "-o");
1457 add_flag(&file_obst, outname);
1458 obstack_printf(&file_obst, "%s", flags);
1459 obstack_1grow(&file_obst, '\0');
1461 char *commandline = obstack_finish(&file_obst);
1466 int err = system(commandline);
1467 if (err != EXIT_SUCCESS) {
1468 fprintf(stderr, "linker reported an error\n");
1473 obstack_free(&cppflags_obst, NULL);
1474 obstack_free(&ldflags_obst, NULL);
1475 obstack_free(&file_obst, NULL);
1485 exit_symbol_table();