2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "driver/firm_timing.h"
75 #include "adt/error.h"
76 #include "wrappergen/write_fluffy.h"
77 #include "wrappergen/write_caml.h"
78 #include "wrappergen/write_jna.h"
86 #define PREPROCESSOR "gcc -E -m32 -U__STRICT_ANSI__"
88 #define PREPROCESSOR "cpp -m32 -U__STRICT_ANSI__"
93 #define LINKER "gcc -m32"
98 #define ASSEMBLER "gcc -m32 -c -xassembler"
100 #define ASSEMBLER "as --32"
104 /** The current c mode/dialect. */
105 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
107 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
108 unsigned int machine_size = 32;
110 /** true if the char type is signed. */
111 bool char_is_signed = true;
113 /** true for strict language checking. */
114 bool strict_mode = false;
116 /** use builtins for some libc functions */
117 bool use_builtins = false;
119 /** we have extern function with const attribute. */
120 bool have_const_functions = false;
122 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
124 /* to switch on printing of implicit casts */
125 extern bool print_implicit_casts;
127 /* to switch on printing of parenthesis to indicate operator precedence */
128 extern bool print_parenthesis;
131 static struct obstack cppflags_obst, ldflags_obst;
132 static char dep_target[1024];
133 static const char *outname;
135 typedef enum lang_standard_t {
136 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
137 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
138 STANDARD_C89, /* ISO C90 (sic) */
139 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
140 STANDARD_C99, /* ISO C99 */
141 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
142 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
143 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
144 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
147 static lang_standard_t standard;
149 typedef struct file_list_entry_t file_list_entry_t;
151 typedef enum filetype_t {
154 FILETYPE_PREPROCESSED_C,
156 FILETYPE_PREPROCESSED_CXX,
158 FILETYPE_PREPROCESSED_ASSEMBLER,
164 struct file_list_entry_t {
165 const char *name; /**< filename or NULL for stdin */
167 file_list_entry_t *next;
170 static file_list_entry_t *temp_files;
172 #if defined(_DEBUG) || defined(FIRM_DEBUG)
174 * Debug printf implementation.
176 * @param fmt printf style format parameter
178 void dbg_printf(const char *fmt, ...)
182 if (firm_dump.debug_print) {
188 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
190 static void initialize_firm(void)
194 dump_consts_local(1);
195 dump_keepalive_edges(1);
198 static void get_output_name(char *buf, size_t buflen, const char *inputname,
201 if (inputname == NULL)
204 char const *const last_slash = strrchr(inputname, '/');
205 char const *const filename =
206 last_slash != NULL ? last_slash + 1 : inputname;
207 char const *const last_dot = strrchr(filename, '.');
208 char const *const name_end =
209 last_dot != NULL ? last_dot : strchr(filename, '\0');
211 int const len = snprintf(buf, buflen, "%.*s%s",
212 (int)(name_end - filename), filename, newext);
214 if (len < 0 || buflen <= (size_t)len)
216 if (buflen <= (size_t)len)
218 panic("filename too long");
221 #include "gen_builtins.h"
223 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
228 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
232 lexer_open_stream(in, input_name);
235 translation_unit_t *unit = finish_parsing();
239 static void lextest(FILE *in, const char *fname)
241 lexer_open_stream(in, fname);
244 lexer_next_preprocessing_token();
245 print_token(stdout, &lexer_token);
247 } while (lexer_token.type != T_EOF);
250 static void add_flag(struct obstack *obst, const char *format, ...)
255 va_start(ap, format);
259 vsnprintf(buf, sizeof(buf), format, ap);
262 obstack_1grow(obst, ' ');
264 obstack_1grow(obst, '"');
265 obstack_grow(obst, buf, len);
266 obstack_1grow(obst, '"');
268 /* escape stuff... */
269 for (char *c = buf; *c != '\0'; ++c) {
287 obstack_1grow(obst, '\\');
290 obstack_1grow(obst, *c);
297 static const char *type_to_string(type_t *type)
299 assert(type->kind == TYPE_ATOMIC);
300 return get_atomic_kind_name(type->atomic.akind);
303 static FILE *preprocess(const char *fname, filetype_t filetype)
305 static const char *common_flags = NULL;
307 if (common_flags == NULL) {
308 obstack_1grow(&cppflags_obst, '\0');
309 const char *flags = obstack_finish(&cppflags_obst);
311 /* setup default defines */
312 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
313 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
314 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
315 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
317 add_flag(&cppflags_obst, "-U__VERSION__");
318 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
320 if (flags[0] != '\0') {
321 size_t len = strlen(flags);
322 obstack_1grow(&cppflags_obst, ' ');
323 obstack_grow(&cppflags_obst, flags, len);
325 obstack_1grow(&cppflags_obst, '\0');
326 common_flags = obstack_finish(&cppflags_obst);
329 assert(obstack_object_size(&cppflags_obst) == 0);
331 const char *preprocessor = getenv("CPARSER_PP");
332 if (preprocessor == NULL)
333 preprocessor = PREPROCESSOR;
335 obstack_printf(&cppflags_obst, "%s ", preprocessor);
338 add_flag(&cppflags_obst, "-std=c99");
341 add_flag(&cppflags_obst, "-std=c++98");
343 case FILETYPE_ASSEMBLER:
344 add_flag(&cppflags_obst, "-x");
345 add_flag(&cppflags_obst, "assembler-with-cpp");
350 obstack_printf(&cppflags_obst, "%s", common_flags);
352 /* handle dependency generation */
353 if (dep_target[0] != '\0') {
354 add_flag(&cppflags_obst, "-MF");
355 add_flag(&cppflags_obst, dep_target);
356 if (outname != NULL) {
357 add_flag(&cppflags_obst, "-MQ");
358 add_flag(&cppflags_obst, outname);
361 add_flag(&cppflags_obst, fname);
363 obstack_1grow(&cppflags_obst, '\0');
364 char *buf = obstack_finish(&cppflags_obst);
369 FILE *f = popen(buf, "r");
371 fprintf(stderr, "invoking preprocessor failed\n");
375 /* we don't really need that anymore */
376 obstack_free(&cppflags_obst, buf);
381 static void assemble(const char *out, const char *in)
385 const char *assembler = getenv("CPARSER_AS");
386 if (assembler == NULL)
387 assembler = ASSEMBLER;
389 snprintf(buf, sizeof(buf), "%s %s -o %s", assembler, in, out);
394 int err = system(buf);
396 fprintf(stderr, "assembler reported an error\n");
401 static void print_file_name(const char *file)
403 add_flag(&ldflags_obst, "-print-file-name=%s", file);
405 obstack_1grow(&ldflags_obst, '\0');
406 const char *flags = obstack_finish(&ldflags_obst);
408 /* construct commandline */
409 const char *linker = getenv("CPARSER_LINK");
412 obstack_printf(&ldflags_obst, "%s ", linker);
413 obstack_printf(&ldflags_obst, "%s", flags);
414 obstack_1grow(&ldflags_obst, '\0');
416 char *commandline = obstack_finish(&ldflags_obst);
421 int err = system(commandline);
422 if (err != EXIT_SUCCESS) {
423 fprintf(stderr, "linker reported an error\n");
428 static const char *try_dir(const char *dir)
432 if (access(dir, R_OK | W_OK | X_OK) == 0)
437 static const char *get_tempdir(void)
439 static const char *tmpdir = NULL;
445 tmpdir = try_dir(getenv("TMPDIR"));
447 tmpdir = try_dir(getenv("TMP"));
449 tmpdir = try_dir(getenv("TEMP"));
453 tmpdir = try_dir(P_tmpdir);
457 tmpdir = try_dir("/var/tmp");
459 tmpdir = try_dir("/usr/tmp");
461 tmpdir = try_dir("/tmp");
470 /* cheap and nasty mkstemp replacement */
471 static int mkstemp(char *templ)
474 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
479 * an own version of tmpnam, which: writes in a buffer, emits no warnings
480 * during linking (like glibc/gnu ld do for tmpnam)...
482 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
484 const char *tempdir = get_tempdir();
486 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
488 int fd = mkstemp(buffer);
490 fprintf(stderr, "couldn't create temporary file: %s\n",
494 FILE *out = fdopen(fd, "w");
496 fprintf(stderr, "couldn't create temporary file FILE*\n");
500 file_list_entry_t *entry = xmalloc(sizeof(*entry));
501 memset(entry, 0, sizeof(*entry));
503 size_t name_len = strlen(buffer) + 1;
504 char *name = malloc(name_len);
505 memcpy(name, buffer, name_len);
508 entry->next = temp_files;
514 static void free_temp_files(void)
516 file_list_entry_t *entry = temp_files;
517 file_list_entry_t *next;
518 for ( ; entry != NULL; entry = next) {
522 free((char*) entry->name);
529 * Do the necessary lowering for compound parameters.
531 void lower_compound_params(void)
533 lower_params_t params;
535 params.def_ptr_alignment = 4;
536 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
537 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
538 params.find_pointer_type = NULL;
539 params.ret_compound_in_regs = NULL;
540 lower_calls_with_compounds(¶ms);
543 typedef enum compile_mode_t {
559 static void usage(const char *argv0)
561 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
564 static void print_cparser_version(void)
566 printf("cparser (%s) using libFirm (%u.%u",
567 cparser_REVISION, ir_get_version_major(),
568 ir_get_version_minor());
570 const char *revision = ir_get_version_revision();
571 if (revision[0] != 0) {
573 fputs(revision, stdout);
576 const char *build = ir_get_version_build();
579 fputs(build, stdout);
582 puts("This is free software; see the source for copying conditions. There is NO\n"
583 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
586 static void set_be_option(const char *arg)
588 int res = firm_be_option(arg);
593 static void set_option(const char *arg)
595 int res = firm_option(arg);
600 static void copy_file(FILE *dest, FILE *input)
604 while (!feof(input) && !ferror(dest)) {
605 size_t read = fread(buf, 1, sizeof(buf), input);
606 if (fwrite(buf, 1, read, dest) != read) {
607 perror("couldn't write output");
612 static inline bool streq(char const* a, char const* b)
614 return strcmp(a, b) == 0;
617 static inline bool strstart(char const* str, char const* start)
622 } while (*str++ == *start++);
626 static FILE *open_file(const char *filename)
628 if (streq(filename, "-")) {
632 FILE *in = fopen(filename, "r");
634 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
642 static filetype_t get_filetype_from_string(const char *string)
644 if (streq(string, "c") || streq(string, "c-header"))
646 if (streq(string, "c++") || streq(string, "c++-header"))
648 if (streq(string, "assembler"))
649 return FILETYPE_PREPROCESSED_ASSEMBLER;
650 if (streq(string, "assembler-with-cpp"))
651 return FILETYPE_ASSEMBLER;
652 if (streq(string, "none"))
653 return FILETYPE_AUTODETECT;
655 return FILETYPE_UNKNOWN;
658 static void init_os_support(void)
660 /* OS option must be set to the backend */
661 switch (firm_opt.os_support) {
662 case OS_SUPPORT_MINGW:
663 set_be_option("ia32-gasmode=mingw");
664 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
666 case OS_SUPPORT_LINUX:
667 set_be_option("ia32-gasmode=elf");
669 case OS_SUPPORT_MACHO:
670 set_be_option("ia32-gasmode=macho");
671 set_be_option("ia32-stackalign=4");
672 set_be_option("pic");
677 int main(int argc, char **argv)
681 const char *dumpfunction = NULL;
682 const char *print_file_name_file = NULL;
683 compile_mode_t mode = CompileAssembleLink;
685 int result = EXIT_SUCCESS;
686 char cpu_arch[16] = "ia32";
687 file_list_entry_t *files = NULL;
688 file_list_entry_t *last_file = NULL;
689 bool construct_dep_target = false;
690 bool do_timing = false;
691 struct obstack file_obst;
693 atexit(free_temp_files);
695 /* hack for now... */
696 if (strstr(argv[0], "pptest") != NULL) {
697 extern int pptest_main(int argc, char **argv);
698 return pptest_main(argc, argv);
701 obstack_init(&cppflags_obst);
702 obstack_init(&ldflags_obst);
703 obstack_init(&file_obst);
705 #define GET_ARG_AFTER(def, args) \
706 def = &arg[sizeof(args)-1]; \
707 if (def[0] == '\0') { \
710 fprintf(stderr, "error: expected argument after '" args "'\n"); \
711 argument_errors = true; \
715 if (def[0] == '-' && def[1] != '\0') { \
716 fprintf(stderr, "error: expected argument after '" args "'\n"); \
717 argument_errors = true; \
722 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
724 /* early options parsing (find out optimisation level and OS) */
725 for (int i = 1; i < argc; ++i) {
726 const char *arg = argv[i];
730 const char *option = &arg[1];
731 if (option[0] == 'O') {
732 sscanf(&option[1], "%d", &opt_level);
734 if (strcmp(arg, "-fwin32") == 0) {
735 firm_opt.os_support = OS_SUPPORT_MINGW;
736 } else if (strcmp(arg, "-fmac") == 0) {
737 firm_opt.os_support = OS_SUPPORT_MACHO;
738 } else if (strcmp(arg, "-flinux") == 0) {
739 firm_opt.os_support = OS_SUPPORT_LINUX;
743 /* set target/os specific stuff */
746 /* apply optimisation level */
749 set_option("no-opt");
752 set_option("no-inline");
756 /* use_builtins = true; */
759 set_option("thread-jumps");
760 set_option("if-conversion");
763 set_option("strict-aliasing");
764 set_option("inline");
765 set_option("deconv");
766 set_be_option("omitfp");
770 /* parse rest of options */
771 standard = STANDARD_DEFAULT;
772 unsigned features_on = 0;
773 unsigned features_off = 0;
774 filetype_t forced_filetype = FILETYPE_AUTODETECT;
775 bool help_displayed = false;
776 bool argument_errors = false;
777 for (int i = 1; i < argc; ++i) {
778 const char *arg = argv[i];
779 if (arg[0] == '-' && arg[1] != '\0') {
781 const char *option = &arg[1];
782 if (option[0] == 'o') {
783 GET_ARG_AFTER(outname, "-o");
784 } else if (option[0] == 'g') {
785 set_be_option("debuginfo=stabs");
786 set_be_option("omitfp=no");
787 set_be_option("ia32-nooptcc=yes");
788 } else if (SINGLE_OPTION('c')) {
789 mode = CompileAssemble;
790 } else if (SINGLE_OPTION('E')) {
791 mode = PreprocessOnly;
792 } else if (SINGLE_OPTION('S')) {
794 } else if (option[0] == 'O') {
796 } else if (option[0] == 'I') {
798 GET_ARG_AFTER(opt, "-I");
799 add_flag(&cppflags_obst, "-I%s", opt);
800 } else if (option[0] == 'D') {
802 GET_ARG_AFTER(opt, "-D");
803 add_flag(&cppflags_obst, "-D%s", opt);
804 } else if (option[0] == 'U') {
806 GET_ARG_AFTER(opt, "-U");
807 add_flag(&cppflags_obst, "-U%s", opt);
808 } else if (option[0] == 'l') {
810 GET_ARG_AFTER(opt, "-l");
811 add_flag(&ldflags_obst, "-l%s", opt);
812 } else if (option[0] == 'L') {
814 GET_ARG_AFTER(opt, "-L");
815 add_flag(&ldflags_obst, "-L%s", opt);
816 } else if (SINGLE_OPTION('v')) {
818 } else if (SINGLE_OPTION('w')) {
819 memset(&warning, 0, sizeof(warning));
820 } else if (option[0] == 'x') {
822 GET_ARG_AFTER(opt, "-x");
823 forced_filetype = get_filetype_from_string(opt);
824 if (forced_filetype == FILETYPE_UNKNOWN) {
825 fprintf(stderr, "Unknown language '%s'\n", opt);
826 argument_errors = true;
828 } else if (streq(option, "M")) {
829 mode = PreprocessOnly;
830 add_flag(&cppflags_obst, "-M");
831 } else if (streq(option, "MMD") ||
832 streq(option, "MD")) {
833 construct_dep_target = true;
834 add_flag(&cppflags_obst, "-%s", option);
835 } else if (streq(option, "MM") ||
836 streq(option, "MP")) {
837 add_flag(&cppflags_obst, "-%s", option);
838 } else if (streq(option, "MT") ||
839 streq(option, "MQ") ||
840 streq(option, "MF")) {
842 GET_ARG_AFTER(opt, "-MT");
843 add_flag(&cppflags_obst, "-%s", option);
844 add_flag(&cppflags_obst, "%s", opt);
845 } else if (streq(option, "include")) {
847 GET_ARG_AFTER(opt, "-include");
848 add_flag(&cppflags_obst, "-include");
849 add_flag(&cppflags_obst, "%s", opt);
850 } else if (streq(option, "isystem")) {
852 GET_ARG_AFTER(opt, "-isystem");
853 add_flag(&cppflags_obst, "-isystem");
854 add_flag(&cppflags_obst, "%s", opt);
855 } else if (streq(option, "nostdinc")
856 || streq(option, "trigraphs")) {
857 /* pass these through to the preprocessor */
858 add_flag(&cppflags_obst, "%s", arg);
859 } else if (streq(option, "pipe")) {
860 /* here for gcc compatibility */
861 } else if (option[0] == 'f') {
862 char const *orig_opt;
863 GET_ARG_AFTER(orig_opt, "-f");
865 if (strstart(orig_opt, "input-charset=")) {
866 char const* const encoding = strchr(orig_opt, '=') + 1;
867 select_input_encoding(encoding);
868 } else if (streq(orig_opt, "verbose-asm")) {
869 /* ignore: we always print verbose assembler */
871 char const *opt = orig_opt;
872 bool truth_value = true;
873 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
878 if (streq(opt, "builtins")) {
879 use_builtins = truth_value;
880 } else if (streq(opt, "dollars-in-identifiers")) {
881 allow_dollar_in_symbol = truth_value;
882 } else if (streq(opt, "omit-frame-pointer")) {
883 set_be_option(truth_value ? "omitfp" : "omitfp=no");
884 } else if (streq(opt, "short-wchar")) {
885 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
887 } else if (streq(opt, "signed-char")) {
888 char_is_signed = truth_value;
889 } else if (streq(opt, "strength-reduce")) {
890 firm_option(truth_value ? "strength-red" : "no-strength-red");
891 } else if (streq(opt, "syntax-only")) {
892 mode = truth_value ? ParseOnly : CompileAssembleLink;
893 } else if (streq(opt, "unsigned-char")) {
894 char_is_signed = !truth_value;
895 } else if (truth_value == false &&
896 streq(opt, "asynchronous-unwind-tables")) {
897 /* nothing todo, a gcc feature which we don't support
898 * anyway was deactivated */
899 } else if (strstart(orig_opt, "align-loops=") ||
900 strstart(orig_opt, "align-jumps=") ||
901 strstart(orig_opt, "align-functions=")) {
902 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
903 } else if (streq(opt, "fast-math") ||
904 streq(opt, "jump-tables") ||
905 streq(opt, "expensive-optimizations") ||
906 streq(opt, "common") ||
907 streq(opt, "optimize-sibling-calls") ||
908 streq(opt, "align-loops") ||
909 streq(opt, "align-jumps") ||
910 streq(opt, "align-functions") ||
912 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
914 int res = firm_option(orig_opt);
916 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
918 argument_errors = true;
920 } else if (res == -1) {
921 help_displayed = true;
925 } else if (option[0] == 'b') {
927 GET_ARG_AFTER(opt, "-b");
928 int res = firm_be_option(opt);
930 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
932 argument_errors = true;
933 } else if (res == -1) {
934 help_displayed = true;
935 } else if (strstart(opt, "isa=")) {
936 strncpy(cpu_arch, opt, sizeof(cpu_arch));
938 } else if (option[0] == 'W') {
939 if (option[1] == '\0') {
940 /* ignore -W, out defaults are already quiet verbose */
941 } else if (strstart(option + 1, "p,")) {
942 // pass options directly to the preprocessor
944 GET_ARG_AFTER(opt, "-Wp,");
945 add_flag(&cppflags_obst, "-Wp,%s", opt);
946 } else if (strstart(option + 1, "l,")) {
947 // pass options directly to the linker
949 GET_ARG_AFTER(opt, "-Wl,");
950 add_flag(&ldflags_obst, "-Wl,%s", opt);
951 } else if (streq(option + 1, "no-trigraphs")
952 || streq(option + 1, "undef")) {
953 add_flag(&cppflags_obst, "%s", arg);
955 set_warning_opt(&option[1]);
957 } else if (option[0] == 'm') {
962 GET_ARG_AFTER(opt, "-m");
963 if (strstart(opt, "arch=")) {
964 GET_ARG_AFTER(opt, "-march=");
965 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
966 int res = firm_be_option(arch_opt);
968 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
969 argument_errors = true;
971 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
972 int res = firm_be_option(arch_opt);
974 argument_errors = true;
976 } else if (strstart(opt, "tune=")) {
977 GET_ARG_AFTER(opt, "-mtune=");
978 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
979 int res = firm_be_option(arch_opt);
981 argument_errors = true;
982 } else if (strstart(opt, "cpu=")) {
983 GET_ARG_AFTER(opt, "-mcpu=");
984 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
985 int res = firm_be_option(arch_opt);
987 argument_errors = true;
988 } else if (strstart(opt, "fpmath=")) {
989 GET_ARG_AFTER(opt, "-mfpmath=");
990 if (streq(opt, "387"))
992 else if (streq(opt, "sse"))
995 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
996 argument_errors = true;
998 if (!argument_errors) {
999 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1000 int res = firm_be_option(arch_opt);
1002 argument_errors = true;
1004 } else if (strstart(opt, "preferred-stack-boundary=")) {
1005 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1006 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1007 int res = firm_be_option(arch_opt);
1009 argument_errors = true;
1010 } else if (streq(opt, "omit-leaf-frame-pointer")) {
1011 set_be_option("omitleaffp=1");
1012 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
1013 set_be_option("omitleaffp=0");
1014 } else if (streq(opt, "rtd")) {
1015 default_calling_convention = CC_STDCALL;
1016 } else if (strstart(opt, "regparm=")) {
1017 fprintf(stderr, "error: regparm convention not supported yet\n");
1018 argument_errors = true;
1019 } else if (streq(opt, "soft-float")) {
1020 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1021 argument_errors = true;
1024 long int value = strtol(opt, &endptr, 10);
1025 if (*endptr != '\0') {
1026 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1027 argument_errors = true;
1029 if (value != 16 && value != 32 && value != 64) {
1030 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1031 argument_errors = true;
1033 machine_size = (unsigned int)value;
1036 } else if (streq(option, "pg")) {
1037 set_be_option("gprof");
1038 add_flag(&ldflags_obst, "-pg");
1039 } else if (streq(option, "pedantic") ||
1040 streq(option, "ansi")) {
1041 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1042 } else if (streq(option, "shared")) {
1043 add_flag(&ldflags_obst, "-shared");
1044 } else if (strstart(option, "std=")) {
1045 const char *const o = &option[4];
1047 streq(o, "c++") ? STANDARD_CXX98 :
1048 streq(o, "c++98") ? STANDARD_CXX98 :
1049 streq(o, "c89") ? STANDARD_C89 :
1050 streq(o, "c99") ? STANDARD_C99 :
1051 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1052 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1053 streq(o, "gnu89") ? STANDARD_GNU89 :
1054 streq(o, "gnu99") ? STANDARD_GNU99 :
1055 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1056 streq(o, "iso9899:1990") ? STANDARD_C89 :
1057 streq(o, "iso9899:199409") ? STANDARD_C90 :
1058 streq(o, "iso9899:1999") ? STANDARD_C99 :
1059 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1060 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1061 } else if (streq(option, "version")) {
1062 print_cparser_version();
1063 } else if (strstart(option, "print-file-name=")) {
1064 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1065 } else if (option[0] == '-') {
1066 /* double dash option */
1068 if (streq(option, "gcc")) {
1069 features_on |= _GNUC;
1070 features_off &= ~_GNUC;
1071 } else if (streq(option, "no-gcc")) {
1072 features_on &= ~_GNUC;
1073 features_off |= _GNUC;
1074 } else if (streq(option, "ms")) {
1076 features_off &= ~_MS;
1077 } else if (streq(option, "no-ms")) {
1078 features_on &= ~_MS;
1079 features_off |= _MS;
1080 } else if (streq(option, "strict")) {
1082 } else if (streq(option, "lextest")) {
1084 } else if (streq(option, "benchmark")) {
1085 mode = BenchmarkParser;
1086 } else if (streq(option, "print-ast")) {
1088 } else if (streq(option, "print-implicit-cast")) {
1089 print_implicit_casts = true;
1090 } else if (streq(option, "print-parenthesis")) {
1091 print_parenthesis = true;
1092 } else if (streq(option, "print-fluffy")) {
1094 } else if (streq(option, "print-caml")) {
1096 } else if (streq(option, "print-jna")) {
1098 } else if (streq(option, "time")) {
1100 } else if (streq(option, "version")) {
1101 print_cparser_version();
1103 } else if (streq(option, "dump-function")) {
1106 fprintf(stderr, "error: "
1107 "expected argument after '--dump-function'\n");
1108 argument_errors = true;
1111 dumpfunction = argv[i];
1113 } else if (streq(option, "export-ir")) {
1114 mode = CompileExportIR;
1116 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1117 argument_errors = true;
1120 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1121 argument_errors = true;
1124 filetype_t type = forced_filetype;
1125 if (type == FILETYPE_AUTODETECT) {
1126 if (streq(arg, "-")) {
1127 /* - implicitly means C source file */
1130 const char *suffix = strrchr(arg, '.');
1131 /* Ensure there is at least one char before the suffix */
1132 if (suffix != NULL && suffix != arg) {
1135 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1136 streq(suffix, "a") ? FILETYPE_OBJECT :
1137 streq(suffix, "c") ? FILETYPE_C :
1138 streq(suffix, "cc") ? FILETYPE_CXX :
1139 streq(suffix, "cpp") ? FILETYPE_CXX :
1140 streq(suffix, "cxx") ? FILETYPE_CXX :
1141 streq(suffix, "h") ? FILETYPE_C :
1142 streq(suffix, "ir") ? FILETYPE_IR :
1143 streq(suffix, "o") ? FILETYPE_OBJECT :
1144 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1145 streq(suffix, "so") ? FILETYPE_OBJECT :
1146 FILETYPE_AUTODETECT;
1150 if (type == FILETYPE_AUTODETECT) {
1151 fprintf(stderr, "'%s': file format not recognized\n", arg);
1156 file_list_entry_t *entry
1157 = obstack_alloc(&file_obst, sizeof(entry[0]));
1158 memset(entry, 0, sizeof(entry[0]));
1162 if (last_file != NULL) {
1163 last_file->next = entry;
1171 if (print_file_name_file != NULL) {
1172 print_file_name(print_file_name_file);
1176 if (files == NULL) {
1177 fprintf(stderr, "error: no input files specified\n");
1178 argument_errors = true;
1181 if (help_displayed) {
1182 return !argument_errors;
1184 if (argument_errors) {
1189 /* we do the lowering in ast2firm */
1190 firm_opt.lower_bitfields = FALSE;
1192 /* set the c_mode here, types depends on it */
1193 c_mode |= features_on;
1194 c_mode &= ~features_off;
1197 init_symbol_table();
1210 if (construct_dep_target) {
1211 if (outname != 0 && strlen(outname) >= 2) {
1212 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1214 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1217 dep_target[0] = '\0';
1220 char outnamebuf[4096];
1221 if (outname == NULL) {
1222 const char *filename = files->name;
1225 case BenchmarkParser:
1231 case PreprocessOnly:
1236 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1237 outname = outnamebuf;
1239 case CompileAssemble:
1240 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1241 outname = outnamebuf;
1244 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1246 outname = outnamebuf;
1248 case CompileExportIR:
1249 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1250 outname = outnamebuf;
1252 case CompileAssembleLink:
1262 assert(outname != NULL);
1265 if (streq(outname, "-")) {
1268 out = fopen(outname, "w");
1270 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1276 file_list_entry_t *file;
1277 bool already_constructed_firm = false;
1278 for (file = files; file != NULL; file = file->next) {
1279 char asm_tempfile[1024];
1280 const char *filename = file->name;
1281 filetype_t filetype = file->type;
1283 if (filetype == FILETYPE_OBJECT)
1287 if (mode == LexTest) {
1289 in = open_file(filename);
1290 lextest(in, filename);
1295 FILE *preprocessed_in = NULL;
1296 filetype_t next_filetype = filetype;
1299 next_filetype = FILETYPE_PREPROCESSED_C;
1302 next_filetype = FILETYPE_PREPROCESSED_CXX;
1304 case FILETYPE_ASSEMBLER:
1305 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1308 /* no support for input on FILE* yet */
1310 panic("internal compiler error: in for preprocessor != NULL");
1312 preprocessed_in = preprocess(filename, filetype);
1313 if (mode == PreprocessOnly) {
1314 copy_file(out, preprocessed_in);
1315 int result = pclose(preprocessed_in);
1317 /* remove output file in case of error */
1318 if (out != stdout && result != EXIT_SUCCESS) {
1324 in = preprocessed_in;
1325 filetype = next_filetype;
1333 if (mode == Compile) {
1336 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1340 in = open_file(filename);
1342 /* preprocess and compile */
1343 if (filetype == FILETYPE_PREPROCESSED_C) {
1344 char const* invalid_mode;
1347 case STANDARD_C89: c_mode = _C89; break;
1348 /* TODO determine difference between these two */
1349 case STANDARD_C90: c_mode = _C89; break;
1350 case STANDARD_C99: c_mode = _C89 | _C99; break;
1351 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1355 "warning: command line option \"-std=%s\" is not valid for C\n",
1358 case STANDARD_DEFAULT:
1359 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1361 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1362 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1365 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1366 char const* invalid_mode;
1368 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1369 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1370 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1371 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1372 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1375 case STANDARD_CXX98: c_mode = _CXX; break;
1379 "warning: command line option \"-std=%s\" is not valid for C++\n",
1381 case STANDARD_DEFAULT:
1382 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1386 c_mode |= features_on;
1387 c_mode &= ~features_off;
1389 /* do the actual parsing */
1390 ir_timer_t *t_parsing = ir_timer_new();
1391 timer_register(t_parsing, "Frontend: Parsing");
1392 timer_push(t_parsing);
1394 translation_unit_t *const unit = do_parsing(in, filename);
1395 timer_pop(t_parsing);
1397 /* prints the AST even if errors occurred */
1398 if (mode == PrintAst) {
1403 if (error_count > 0) {
1404 /* parsing failed because of errors */
1405 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1407 result = EXIT_FAILURE;
1409 } else if (warning_count > 0) {
1410 fprintf(stderr, "%u warning(s)\n", warning_count);
1413 if (in == preprocessed_in) {
1414 int pp_result = pclose(preprocessed_in);
1415 if (pp_result != EXIT_SUCCESS) {
1416 /* remove output file */
1423 if (mode == BenchmarkParser) {
1425 } else if (mode == PrintFluffy) {
1426 write_fluffy_decls(out, unit);
1428 } else if (mode == PrintCaml) {
1429 write_caml_decls(out, unit);
1431 } else if (mode == PrintJna) {
1432 write_jna_decls(out, unit);
1436 /* build the firm graph */
1437 ir_timer_t *t_construct = ir_timer_new();
1438 timer_register(t_construct, "Frontend: Graph construction");
1439 timer_push(t_construct);
1440 if (already_constructed_firm) {
1441 panic("compiling multiple files/translation units not possible");
1443 translation_unit_to_firm(unit);
1444 already_constructed_firm = true;
1445 timer_pop(t_construct);
1448 if (mode == ParseOnly) {
1452 if (mode == CompileDump) {
1454 ident *id = new_id_from_str(dumpfunction);
1455 ir_graph *irg = NULL;
1456 int n_irgs = get_irp_n_irgs();
1457 for (int i = 0; i < n_irgs; ++i) {
1458 ir_graph *tirg = get_irp_irg(i);
1459 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1467 fprintf(stderr, "No graph for function '%s' found\n",
1472 dump_ir_block_graph_file(irg, out);
1477 if (mode == CompileExportIR) {
1483 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1484 have_const_functions);
1485 if (asm_out != out) {
1488 } else if (filetype == FILETYPE_IR) {
1490 ir_import(filename);
1492 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1493 copy_file(asm_out, in);
1494 if (in == preprocessed_in) {
1495 int pp_result = pclose(preprocessed_in);
1496 if (pp_result != EXIT_SUCCESS) {
1497 /* remove output in error case */
1503 if (asm_out != out) {
1508 if (mode == Compile)
1511 /* if we're here then we have preprocessed assembly */
1512 filename = asm_tempfile;
1513 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1516 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1518 const char *filename_o;
1519 if (mode == CompileAssemble) {
1521 filename_o = outname;
1523 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1528 assemble(filename_o, filename);
1530 size_t len = strlen(filename_o) + 1;
1531 filename = obstack_copy(&file_obst, filename_o, len);
1532 filetype = FILETYPE_OBJECT;
1535 /* ok we're done here, process next file */
1536 file->name = filename;
1537 file->type = filetype;
1540 if (result != EXIT_SUCCESS) {
1546 /* link program file */
1547 if (mode == CompileAssembleLink) {
1548 obstack_1grow(&ldflags_obst, '\0');
1549 const char *flags = obstack_finish(&ldflags_obst);
1551 /* construct commandline */
1552 const char *linker = getenv("CPARSER_LINK");
1555 obstack_printf(&file_obst, "%s", linker);
1556 for (file_list_entry_t *entry = files; entry != NULL;
1557 entry = entry->next) {
1558 if (entry->type != FILETYPE_OBJECT)
1561 add_flag(&file_obst, "%s", entry->name);
1564 add_flag(&file_obst, "-o");
1565 add_flag(&file_obst, outname);
1566 obstack_printf(&file_obst, "%s", flags);
1567 obstack_1grow(&file_obst, '\0');
1569 char *commandline = obstack_finish(&file_obst);
1574 int err = system(commandline);
1575 if (err != EXIT_SUCCESS) {
1576 fprintf(stderr, "linker reported an error\n");
1584 obstack_free(&cppflags_obst, NULL);
1585 obstack_free(&ldflags_obst, NULL);
1586 obstack_free(&file_obst, NULL);
1596 exit_symbol_table();