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_memcpy=memcpy");
307 /* handle dependency generation */
308 if (dep_target[0] != '\0') {
309 add_flag(&cppflags_obst, "-MF");
310 add_flag(&cppflags_obst, dep_target);
311 if (outname != NULL) {
312 add_flag(&cppflags_obst, "-MQ");
313 add_flag(&cppflags_obst, outname);
316 if (flags[0] != '\0') {
317 obstack_printf(&cppflags_obst, " %s", flags);
319 add_flag(&cppflags_obst, fname);
321 obstack_1grow(&cppflags_obst, '\0');
322 const char *buf = obstack_finish(&cppflags_obst);
327 FILE *f = popen(buf, "r");
329 fprintf(stderr, "invoking preprocessor failed\n");
336 static void assemble(const char *out, const char *in)
340 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
345 int err = system(buf);
347 fprintf(stderr, "assembler reported an error\n");
352 static void print_file_name(const char *file)
354 add_flag(&ldflags_obst, "-print-file-name=%s", file);
356 obstack_1grow(&ldflags_obst, '\0');
357 const char *flags = obstack_finish(&ldflags_obst);
359 /* construct commandline */
360 obstack_printf(&ldflags_obst, "%s ", LINKER);
361 obstack_printf(&ldflags_obst, "%s", flags);
362 obstack_1grow(&ldflags_obst, '\0');
364 char *commandline = obstack_finish(&ldflags_obst);
369 int err = system(commandline);
370 if (err != EXIT_SUCCESS) {
371 fprintf(stderr, "linker reported an error\n");
376 static const char *try_dir(const char *dir)
380 if (access(dir, R_OK | W_OK | X_OK) == 0)
385 static const char *get_tempdir(void)
387 static const char *tmpdir = NULL;
393 tmpdir = try_dir(getenv("TMPDIR"));
395 tmpdir = try_dir(getenv("TMP"));
397 tmpdir = try_dir(getenv("TEMP"));
401 tmpdir = try_dir(P_tmpdir);
405 tmpdir = try_dir("/var/tmp");
407 tmpdir = try_dir("/usr/tmp");
409 tmpdir = try_dir("/tmp");
418 /* cheap and nasty mkstemp replacement */
419 static int mkstemp(char *templ)
422 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
427 * an own version of tmpnam, which: writes in a buffer, emits no warnings
428 * during linking (like glibc/gnu ld do for tmpnam)...
430 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
432 const char *tempdir = get_tempdir();
434 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
436 int fd = mkstemp(buffer);
438 fprintf(stderr, "couldn't create temporary file: %s\n",
442 FILE *out = fdopen(fd, "w");
444 fprintf(stderr, "couldn't create temporary file FILE*\n");
448 file_list_entry_t *entry = xmalloc(sizeof(*entry));
449 memset(entry, 0, sizeof(*entry));
451 size_t name_len = strlen(buffer) + 1;
452 char *name = malloc(name_len);
453 memcpy(name, buffer, name_len);
456 entry->next = temp_files;
462 static void free_temp_files(void)
464 file_list_entry_t *entry = temp_files;
465 file_list_entry_t *next;
466 for ( ; entry != NULL; entry = next) {
470 free((char*) entry->name);
477 * Do the necessary lowering for compound parameters.
479 void lower_compound_params(void)
481 lower_params_t params;
483 params.def_ptr_alignment = 4;
484 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
485 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
486 params.find_pointer_type = NULL;
487 params.ret_compound_in_regs = NULL;
488 lower_calls_with_compounds(¶ms);
491 typedef enum compile_mode_t {
505 static void usage(const char *argv0)
507 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
510 static void print_cparser_version(void) {
512 firm_get_version(&ver);
514 printf("cparser (%s) using libFirm (%u.%u",
515 cparser_REVISION, ver.major, ver.minor);
516 if (ver.revision[0] != 0) {
518 fputs(ver.revision, stdout);
520 if (ver.build[0] != 0) {
522 fputs(ver.build, stdout);
527 static void set_be_option(const char *arg)
529 int res = firm_be_option(arg);
534 static void set_option(const char *arg)
536 int res = firm_option(arg);
541 static void copy_file(FILE *dest, FILE *input)
545 while (!feof(input) && !ferror(dest)) {
546 size_t read = fread(buf, 1, sizeof(buf), input);
547 if (fwrite(buf, 1, read, dest) != read) {
548 perror("couldn't write output");
553 static inline bool streq(char const* a, char const* b)
555 return strcmp(a, b) == 0;
558 static inline bool strstart(char const* str, char const* start)
563 } while (*str++ == *start++);
567 static FILE *open_file(const char *filename)
569 if (streq(filename, "-")) {
573 FILE *in = fopen(filename, "r");
575 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
583 static filetype_t get_filetype_from_string(const char *string)
585 if (streq(string, "c") || streq(string, "c-header"))
587 if (streq(string, "c++") || streq(string, "c++-header"))
589 if (streq(string, "assembler"))
590 return FILETYPE_PREPROCESSED_ASSEMBLER;
591 if (streq(string, "assembler-with-cpp"))
592 return FILETYPE_ASSEMBLER;
593 if (streq(string, "none"))
594 return FILETYPE_AUTODETECT;
596 return FILETYPE_UNKNOWN;
599 static void init_os_support(void)
601 /* OS option must be set to the backend */
602 switch (firm_opt.os_support) {
603 case OS_SUPPORT_MINGW:
604 set_be_option("ia32-gasmode=mingw");
605 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
607 case OS_SUPPORT_LINUX:
608 set_be_option("ia32-gasmode=elf");
610 case OS_SUPPORT_MACHO:
611 set_be_option("ia32-gasmode=macho");
612 set_be_option("ia32-stackalign=4");
613 set_be_option("pic");
618 typedef enum lang_standard_t {
619 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
620 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
621 STANDARD_C89, /* ISO C90 (sic) */
622 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
623 STANDARD_C99, /* ISO C99 */
624 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
625 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
626 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
627 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
630 int main(int argc, char **argv)
634 const char *dumpfunction = NULL;
635 const char *print_file_name_file = NULL;
636 compile_mode_t mode = CompileAssembleLink;
638 int result = EXIT_SUCCESS;
639 char cpu_arch[16] = "ia32";
640 file_list_entry_t *files = NULL;
641 file_list_entry_t *last_file = NULL;
642 bool construct_dep_target = false;
643 struct obstack file_obst;
645 atexit(free_temp_files);
647 /* hack for now... */
648 if (strstr(argv[0], "pptest") != NULL) {
649 extern int pptest_main(int argc, char **argv);
650 return pptest_main(argc, argv);
653 obstack_init(&cppflags_obst);
654 obstack_init(&ldflags_obst);
655 obstack_init(&file_obst);
657 #define GET_ARG_AFTER(def, args) \
658 def = &arg[sizeof(args)-1]; \
659 if (def[0] == '\0') { \
662 fprintf(stderr, "error: expected argument after '" args "'\n"); \
663 argument_errors = true; \
667 if (def[0] == '-' && def[1] != '\0') { \
668 fprintf(stderr, "error: expected argument after '" args "'\n"); \
669 argument_errors = true; \
674 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
676 /* early options parsing (find out optimisation level and OS) */
677 for (int i = 1; i < argc; ++i) {
678 const char *arg = argv[i];
682 const char *option = &arg[1];
683 if (option[0] == 'O') {
684 sscanf(&option[1], "%d", &opt_level);
686 if (strcmp(arg, "-fwin32") == 0) {
687 firm_opt.os_support = OS_SUPPORT_MINGW;
688 } else if (strcmp(arg, "-fmac") == 0) {
689 firm_opt.os_support = OS_SUPPORT_MACHO;
690 } else if (strcmp(arg, "-flinux") == 0) {
691 firm_opt.os_support = OS_SUPPORT_LINUX;
695 /* set target/os specific stuff */
698 /* apply optimisation level */
701 set_option("no-opt");
704 set_option("no-inline");
708 set_option("strict-aliasing");
709 /* use_builtins = true; */
712 set_option("cond-eval");
713 set_option("if-conv");
716 set_option("inline");
717 set_option("deconv");
718 set_be_option("omitfp");
722 /* parse rest of options */
723 lang_standard_t standard = STANDARD_DEFAULT;
724 unsigned features_on = 0;
725 unsigned features_off = 0;
726 filetype_t forced_filetype = FILETYPE_AUTODETECT;
727 bool help_displayed = false;
728 bool argument_errors = false;
729 for (int i = 1; i < argc; ++i) {
730 const char *arg = argv[i];
731 if (arg[0] == '-' && arg[1] != '\0') {
733 const char *option = &arg[1];
734 if (option[0] == 'o') {
735 GET_ARG_AFTER(outname, "-o");
736 } else if (option[0] == 'g') {
737 set_be_option("debuginfo=stabs");
738 set_be_option("omitfp=no");
739 set_be_option("ia32-nooptcc=yes");
740 } else if (SINGLE_OPTION('c')) {
741 mode = CompileAssemble;
742 } else if (SINGLE_OPTION('E')) {
743 mode = PreprocessOnly;
744 } else if (SINGLE_OPTION('S')) {
746 } else if (option[0] == 'O') {
748 } else if (option[0] == 'I') {
750 GET_ARG_AFTER(opt, "-I");
751 add_flag(&cppflags_obst, "-I%s", opt);
752 } else if (option[0] == 'D') {
754 GET_ARG_AFTER(opt, "-D");
755 add_flag(&cppflags_obst, "-D%s", opt);
756 } else if (option[0] == 'U') {
758 GET_ARG_AFTER(opt, "-U");
759 add_flag(&cppflags_obst, "-U%s", opt);
760 } else if (option[0] == 'l') {
762 GET_ARG_AFTER(opt, "-l");
763 add_flag(&ldflags_obst, "-l%s", opt);
764 } else if (option[0] == 'L') {
766 GET_ARG_AFTER(opt, "-L");
767 add_flag(&ldflags_obst, "-L%s", opt);
768 } else if (SINGLE_OPTION('v')) {
770 } else if (SINGLE_OPTION('w')) {
771 memset(&warning, 0, sizeof(warning));
772 } else if (option[0] == 'x') {
774 GET_ARG_AFTER(opt, "-x");
775 forced_filetype = get_filetype_from_string(opt);
776 if (forced_filetype == FILETYPE_UNKNOWN) {
777 fprintf(stderr, "Unknown language '%s'\n", opt);
778 argument_errors = true;
780 } else if (streq(option, "M")) {
781 mode = PreprocessOnly;
782 add_flag(&cppflags_obst, "-M");
783 } else if (streq(option, "MMD") ||
784 streq(option, "MD")) {
785 construct_dep_target = true;
786 add_flag(&cppflags_obst, "-%s", option);
787 } else if (streq(option, "MM") ||
788 streq(option, "MP")) {
789 add_flag(&cppflags_obst, "-%s", option);
790 } else if (streq(option, "MT") ||
791 streq(option, "MQ") ||
792 streq(option, "MF")) {
794 GET_ARG_AFTER(opt, "-MT");
795 add_flag(&cppflags_obst, "-%s", option);
796 add_flag(&cppflags_obst, "%s", opt);
797 } else if (streq(option, "include")) {
799 GET_ARG_AFTER(opt, "-include");
800 add_flag(&cppflags_obst, "-include");
801 add_flag(&cppflags_obst, "%s", opt);
802 } else if (streq(option, "isystem")) {
804 GET_ARG_AFTER(opt, "-isystem");
805 add_flag(&cppflags_obst, "-isystem");
806 add_flag(&cppflags_obst, "%s", opt);
807 } else if (streq(option, "nostdinc")
808 || streq(option, "trigraphs")) {
809 /* pass these through to the preprocessor */
810 add_flag(&cppflags_obst, "%s", arg);
811 } else if (streq(option, "pipe")) {
812 /* here for gcc compatibility */
813 } else if (option[0] == 'f') {
814 char const *orig_opt;
815 GET_ARG_AFTER(orig_opt, "-f");
817 if (strstart(orig_opt, "align-loops=") ||
818 strstart(orig_opt, "align-jumps=") ||
819 strstart(orig_opt, "align-functions=")) {
820 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
821 } else if (streq(orig_opt, "verbose-asm")) {
822 /* ignore: we always print verbose assembler */
824 char const *opt = orig_opt;
825 bool truth_value = true;
826 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
831 if (streq(opt, "dollars-in-identifiers")) {
832 allow_dollar_in_symbol = truth_value;
833 } if (streq(opt, "builtins")) {
834 use_builtins = truth_value;
835 } else if (streq(opt, "short-wchar")) {
836 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
838 } else if (streq(opt, "syntax-only")) {
839 mode = truth_value ? ParseOnly : CompileAssembleLink;
840 } else if (streq(opt, "omit-frame-pointer")) {
841 set_be_option(truth_value ? "omitfp" : "omitfp=no");
842 } else if (streq(opt, "strength-reduce")) {
843 firm_option(truth_value ? "strength-red" : "no-strength-red");
844 } else if (streq(opt, "fast-math") ||
845 streq(opt, "jump-tables") ||
846 streq(opt, "unroll-loops") ||
847 streq(opt, "expensive-optimizations") ||
848 streq(opt, "common") ||
850 streq(opt, "align-loops") ||
851 streq(opt, "align-jumps") ||
852 streq(opt, "align-functions")) {
853 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
855 int res = firm_option(orig_opt);
857 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
859 argument_errors = true;
861 } else if (res == -1) {
862 help_displayed = true;
866 } else if (option[0] == 'b') {
868 GET_ARG_AFTER(opt, "-b");
869 int res = firm_be_option(opt);
871 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
873 argument_errors = true;
874 } else if (res == -1) {
875 help_displayed = true;
876 } else if (strstart(opt, "isa=")) {
877 strncpy(cpu_arch, opt, sizeof(cpu_arch));
879 } else if (option[0] == 'W') {
880 if (strstart(option + 1, "p,")) {
881 // pass options directly to the preprocessor
883 GET_ARG_AFTER(opt, "-Wp,");
884 add_flag(&cppflags_obst, "-Wp,%s", opt);
885 } else if (strstart(option + 1, "l,")) {
886 // pass options directly to the linker
888 GET_ARG_AFTER(opt, "-Wl,");
889 add_flag(&ldflags_obst, "-Wl,%s", opt);
890 } else if (streq(option + 1, "no-trigraphs")
891 || streq(option + 1, "undef")) {
892 add_flag(&cppflags_obst, "%s", arg);
894 set_warning_opt(&option[1]);
896 } else if (option[0] == 'm') {
901 GET_ARG_AFTER(opt, "-m");
902 if (strstart(opt, "arch=")) {
903 GET_ARG_AFTER(opt, "-march=");
904 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
905 int res = firm_be_option(arch_opt);
907 argument_errors = true;
909 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
910 int res = firm_be_option(arch_opt);
912 argument_errors = true;
914 } else if (strstart(opt, "tune=")) {
915 GET_ARG_AFTER(opt, "-mtune=");
916 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
917 int res = firm_be_option(arch_opt);
919 argument_errors = true;
920 } else if (strstart(opt, "cpu=")) {
921 GET_ARG_AFTER(opt, "-mcpu=");
922 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
923 int res = firm_be_option(arch_opt);
925 argument_errors = true;
926 } else if (strstart(opt, "fpmath=")) {
927 GET_ARG_AFTER(opt, "-mfpmath=");
928 if (streq(opt, "387"))
930 else if (streq(opt, "sse"))
933 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
934 argument_errors = true;
936 if (!argument_errors) {
937 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
938 int res = firm_be_option(arch_opt);
940 argument_errors = true;
942 } else if (strstart(opt, "preferred-stack-boundary=")) {
943 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
944 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
945 int res = firm_be_option(arch_opt);
947 argument_errors = true;
948 } else if (streq(opt, "omit-leaf-frame-pointer")) {
949 set_be_option("omitleaffp=1");
950 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
951 set_be_option("omitleaffp=0");
954 long int value = strtol(opt, &endptr, 10);
955 if (*endptr != '\0') {
956 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
957 argument_errors = true;
959 if (value != 16 && value != 32 && value != 64) {
960 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
961 argument_errors = true;
963 machine_size = (unsigned int)value;
966 } else if (streq(option, "pg")) {
967 set_be_option("gprof");
968 add_flag(&ldflags_obst, "-pg");
969 } else if (streq(option, "pedantic") ||
970 streq(option, "ansi")) {
971 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
972 } else if (streq(option, "shared")) {
973 add_flag(&ldflags_obst, "-shared");
974 } else if (strstart(option, "std=")) {
975 const char *const o = &option[4];
977 streq(o, "c++") ? STANDARD_CXX98 :
978 streq(o, "c++98") ? STANDARD_CXX98 :
979 streq(o, "c89") ? STANDARD_C89 :
980 streq(o, "c99") ? STANDARD_C99 :
981 streq(o, "c9x") ? STANDARD_C99 : // deprecated
982 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
983 streq(o, "gnu89") ? STANDARD_GNU89 :
984 streq(o, "gnu99") ? STANDARD_GNU99 :
985 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
986 streq(o, "iso9899:1990") ? STANDARD_C89 :
987 streq(o, "iso9899:199409") ? STANDARD_C90 :
988 streq(o, "iso9899:1999") ? STANDARD_C99 :
989 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
990 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
991 } else if (streq(option, "version")) {
992 print_cparser_version();
993 } else if (strstart(option, "print-file-name=")) {
994 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
995 } else if (option[0] == '-') {
996 /* double dash option */
998 if (streq(option, "gcc")) {
999 features_on |= _GNUC;
1000 features_off &= ~_GNUC;
1001 } else if (streq(option, "no-gcc")) {
1002 features_on &= ~_GNUC;
1003 features_off |= _GNUC;
1004 } else if (streq(option, "ms")) {
1006 features_off &= ~_MS;
1007 } else if (streq(option, "no-ms")) {
1008 features_on &= ~_MS;
1009 features_off |= _MS;
1010 } else if (streq(option, "signed-chars")) {
1011 char_is_signed = true;
1012 } else if (streq(option, "unsigned-chars")) {
1013 char_is_signed = false;
1014 } else if (streq(option, "strict")) {
1016 } else if (streq(option, "lextest")) {
1018 } else if (streq(option, "benchmark")) {
1019 mode = BenchmarkParser;
1020 } else if (streq(option, "print-ast")) {
1022 } else if (streq(option, "print-implicit-cast")) {
1023 print_implicit_casts = true;
1024 } else if (streq(option, "print-parenthesis")) {
1025 print_parenthesis = true;
1026 } else if (streq(option, "print-fluffy")) {
1028 } else if (streq(option, "print-caml")) {
1030 } else if (streq(option, "version")) {
1031 print_cparser_version();
1033 } else if (streq(option, "dump-function")) {
1036 fprintf(stderr, "error: "
1037 "expected argument after '--dump-function'\n");
1038 argument_errors = true;
1041 dumpfunction = argv[i];
1044 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1045 argument_errors = true;
1048 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1049 argument_errors = true;
1052 filetype_t type = forced_filetype;
1053 if (type == FILETYPE_AUTODETECT) {
1054 if (streq(arg, "-")) {
1055 /* - implicitly means C source file */
1058 const char *suffix = strrchr(arg, '.');
1059 /* Ensure there is at least one char before the suffix */
1060 if (suffix != NULL && suffix != arg) {
1063 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1064 streq(suffix, "a") ? FILETYPE_OBJECT :
1065 streq(suffix, "c") ? FILETYPE_C :
1066 streq(suffix, "cc") ? FILETYPE_CXX :
1067 streq(suffix, "cpp") ? FILETYPE_CXX :
1068 streq(suffix, "cxx") ? FILETYPE_CXX :
1069 streq(suffix, "h") ? FILETYPE_C :
1070 streq(suffix, "o") ? FILETYPE_OBJECT :
1071 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1072 streq(suffix, "so") ? FILETYPE_OBJECT :
1073 FILETYPE_AUTODETECT;
1077 if (type == FILETYPE_AUTODETECT) {
1078 fprintf(stderr, "'%s': file format not recognized\n", arg);
1083 file_list_entry_t *entry
1084 = obstack_alloc(&file_obst, sizeof(entry[0]));
1085 memset(entry, 0, sizeof(entry[0]));
1089 if (last_file != NULL) {
1090 last_file->next = entry;
1098 if (print_file_name_file != NULL) {
1099 print_file_name(print_file_name_file);
1103 if (files == NULL) {
1104 fprintf(stderr, "error: no input files specified\n");
1105 argument_errors = true;
1108 if (help_displayed) {
1109 return !argument_errors;
1111 if (argument_errors) {
1116 /* we do the lowering in ast2firm */
1117 firm_opt.lower_bitfields = FALSE;
1120 init_symbol_table();
1130 if (construct_dep_target) {
1131 if (outname != 0 && strlen(outname) >= 2) {
1132 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1134 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1137 dep_target[0] = '\0';
1140 char outnamebuf[4096];
1141 if (outname == NULL) {
1142 const char *filename = files->name;
1145 case BenchmarkParser:
1150 case PreprocessOnly:
1155 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1156 outname = outnamebuf;
1158 case CompileAssemble:
1159 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1160 outname = outnamebuf;
1163 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1165 outname = outnamebuf;
1167 case CompileAssembleLink:
1177 assert(outname != NULL);
1180 if (streq(outname, "-")) {
1183 out = fopen(outname, "w");
1185 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1191 file_list_entry_t *file;
1192 for (file = files; file != NULL; file = file->next) {
1193 char asm_tempfile[1024];
1194 const char *filename = file->name;
1195 filetype_t filetype = file->type;
1197 if (filetype == FILETYPE_OBJECT)
1201 if (mode == LexTest) {
1203 in = open_file(filename);
1204 lextest(in, filename);
1209 FILE *preprocessed_in = NULL;
1212 filetype = FILETYPE_PREPROCESSED_C;
1215 filetype = FILETYPE_PREPROCESSED_CXX;
1217 case FILETYPE_ASSEMBLER:
1218 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1219 add_flag(&cppflags_obst, "-x");
1220 add_flag(&cppflags_obst, "assembler-with-cpp");
1223 /* no support for input on FILE* yet */
1225 panic("internal compiler error: in for preprocessor != NULL");
1227 preprocessed_in = preprocess(filename);
1228 if (mode == PreprocessOnly) {
1229 copy_file(out, preprocessed_in);
1230 int result = pclose(preprocessed_in);
1232 /* remove output file in case of error */
1233 if (out != stdout && result != EXIT_SUCCESS) {
1239 in = preprocessed_in;
1247 if (mode == Compile) {
1250 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1254 in = open_file(filename);
1256 /* preprocess and compile */
1257 if (filetype == FILETYPE_PREPROCESSED_C) {
1258 char const* invalid_mode;
1261 case STANDARD_C89: c_mode = _C89; break;
1262 /* TODO ^v determine difference between these two */
1263 case STANDARD_C90: c_mode = _C89; break;
1264 case STANDARD_C99: c_mode = _C89 | _C99; break;
1265 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1269 "warning: command line option \"-std=%s\" is not valid for C\n",
1272 case STANDARD_DEFAULT:
1273 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1275 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1276 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1279 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1280 char const* invalid_mode;
1282 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1283 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1284 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1285 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1286 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1289 case STANDARD_CXX98: c_mode = _CXX; break;
1293 "warning: command line option \"-std=%s\" is not valid for C++\n",
1295 case STANDARD_DEFAULT:
1296 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1300 c_mode |= features_on;
1301 c_mode &= ~features_off;
1303 translation_unit_t *const unit = do_parsing(in, filename);
1305 /* prints the AST even if errors occurred */
1306 if (mode == PrintAst) {
1307 type_set_output(out);
1308 ast_set_output(out);
1312 if (error_count > 0) {
1313 /* parsing failed because of errors */
1314 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1316 result = EXIT_FAILURE;
1318 } else if (warning_count > 0) {
1319 fprintf(stderr, "%u warning(s)\n", warning_count);
1322 if (in == preprocessed_in) {
1323 int pp_result = pclose(preprocessed_in);
1324 if (pp_result != EXIT_SUCCESS) {
1325 /* remove output file */
1332 if (mode == BenchmarkParser) {
1334 } else if (mode == PrintFluffy) {
1335 write_fluffy_decls(out, unit);
1337 } else if (mode == PrintCaml) {
1338 write_caml_decls(out, unit);
1342 translation_unit_to_firm(unit);
1344 if (mode == ParseOnly) {
1348 if (mode == CompileDump) {
1350 ident *id = new_id_from_str(dumpfunction);
1351 ir_graph *irg = NULL;
1352 int n_irgs = get_irp_n_irgs();
1353 for (int i = 0; i < n_irgs; ++i) {
1354 ir_graph *tirg = get_irp_irg(i);
1355 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1363 fprintf(stderr, "No graph for function '%s' found\n",
1368 dump_ir_block_graph_file(irg, out);
1373 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1374 have_const_functions);
1375 if (asm_out != out) {
1378 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1379 copy_file(asm_out, in);
1380 if (in == preprocessed_in) {
1381 int pp_result = pclose(preprocessed_in);
1382 if (pp_result != EXIT_SUCCESS) {
1383 /* remove output in error case */
1389 if (asm_out != out) {
1394 if (mode == Compile)
1397 /* if we're here then we have preprocessed assembly */
1398 filename = asm_tempfile;
1399 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1402 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1404 const char *filename_o;
1405 if (mode == CompileAssemble) {
1407 filename_o = outname;
1409 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1414 assemble(filename_o, filename);
1416 size_t len = strlen(filename_o) + 1;
1417 filename = obstack_copy(&file_obst, filename_o, len);
1418 filetype = FILETYPE_OBJECT;
1421 /* ok we're done here, process next file */
1422 file->name = filename;
1423 file->type = filetype;
1426 if (result != EXIT_SUCCESS) {
1432 /* link program file */
1433 if (mode == CompileAssembleLink) {
1434 obstack_1grow(&ldflags_obst, '\0');
1435 const char *flags = obstack_finish(&ldflags_obst);
1437 /* construct commandline */
1438 obstack_printf(&file_obst, "%s", LINKER);
1439 for (file_list_entry_t *entry = files; entry != NULL;
1440 entry = entry->next) {
1441 if (entry->type != FILETYPE_OBJECT)
1444 add_flag(&file_obst, "%s", entry->name);
1447 add_flag(&file_obst, "-o");
1448 add_flag(&file_obst, outname);
1449 obstack_printf(&file_obst, "%s", flags);
1450 obstack_1grow(&file_obst, '\0');
1452 char *commandline = obstack_finish(&file_obst);
1457 int err = system(commandline);
1458 if (err != EXIT_SUCCESS) {
1459 fprintf(stderr, "linker reported an error\n");
1464 obstack_free(&cppflags_obst, NULL);
1465 obstack_free(&ldflags_obst, NULL);
1466 obstack_free(&file_obst, NULL);
1476 exit_symbol_table();