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)
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
69 #include "type_hash.h"
72 #include "diagnostic.h"
73 #include "lang_features.h"
74 #include "driver/firm_opt.h"
75 #include "driver/firm_cmdline.h"
76 #include "adt/error.h"
77 #include "write_fluffy.h"
78 #include "write_caml.h"
84 #define PREPROCESSOR "gcc -E -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__"
86 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__"
91 #define LINKER "gcc -m32"
96 #define ASSEMBLER "gcc -c -xassembler"
98 #define ASSEMBLER "as --32"
102 /** The current c mode/dialect. */
103 lang_features_t c_mode;
105 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
106 unsigned int machine_size = 32;
108 /** true if the char type is signed. */
109 bool char_is_signed = true;
111 /** true for strict language checking. */
112 bool strict_mode = false;
114 /** use builtins for some libc functions */
115 bool use_builtins = false;
117 /** we have extern function with const attribute. */
118 bool have_const_functions = false;
120 /* to switch on printing of implicit casts */
121 extern bool print_implicit_casts;
123 /* to switch on printing of parenthesis to indicate operator precedence */
124 extern bool print_parenthesis;
127 static struct obstack cppflags_obst, ldflags_obst;
128 static char dep_target[1024];
129 static const char *outname;
131 typedef struct file_list_entry_t file_list_entry_t;
133 typedef enum filetype_t {
136 FILETYPE_PREPROCESSED_C,
138 FILETYPE_PREPROCESSED_CXX,
140 FILETYPE_PREPROCESSED_ASSEMBLER,
145 struct file_list_entry_t {
146 const char *name; /**< filename or NULL for stdin */
148 file_list_entry_t *next;
151 #if defined(_DEBUG) || defined(FIRM_DEBUG)
153 * Debug printf implementation.
155 * @param fmt printf style format parameter
157 void dbg_printf(const char *fmt, ...)
161 if (firm_dump.debug_print) {
167 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
169 static void initialize_firm(void)
173 dump_consts_local(1);
174 dump_keepalive_edges(1);
177 static void get_output_name(char *buf, size_t buflen, const char *inputname,
180 size_t last_dot = 0xffffffff;
183 if(inputname == NULL) {
184 snprintf(buf, buflen, "a%s", newext);
188 for(const char *c = inputname; *c != 0; ++c) {
193 if(last_dot == 0xffffffff)
196 if(last_dot >= buflen)
197 panic("filename too long");
198 memcpy(buf, inputname, last_dot);
200 size_t extlen = strlen(newext) + 1;
201 if(extlen + last_dot >= buflen)
202 panic("filename too long");
203 memcpy(buf+last_dot, newext, extlen);
206 #include "builtins.h"
208 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
213 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
217 lexer_open_stream(in, input_name);
220 translation_unit_t *unit = finish_parsing();
224 static void lextest(FILE *in, const char *fname)
226 lexer_open_stream(in, fname);
229 lexer_next_preprocessing_token();
230 print_token(stdout, &lexer_token);
232 } while(lexer_token.type != T_EOF);
235 static void add_flag(struct obstack *obst, const char *format, ...)
238 va_start(ap, format);
241 vsnprintf(buf, sizeof(buf), format, ap);
243 /* escape stuff... */
244 obstack_1grow(obst, ' ');
245 for (char *c = buf; *c != '\0'; ++c) {
258 obstack_1grow(obst, '\\');
261 obstack_1grow(obst, *c);
269 static FILE *preprocess(const char *fname)
271 obstack_1grow(&cppflags_obst, '\0');
272 const char *flags = obstack_finish(&cppflags_obst);
274 obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
275 if (dep_target[0] != '\0') {
276 add_flag(&cppflags_obst, "-MF");
277 add_flag(&cppflags_obst, "%s", dep_target);
278 if (outname != NULL) {
279 add_flag(&cppflags_obst, "-MQ");
280 add_flag(&cppflags_obst, "%s", outname);
283 if (flags[0] != '\0') {
284 obstack_printf(&cppflags_obst, " %s", flags);
286 add_flag(&cppflags_obst, "%s", fname);
288 obstack_1grow(&cppflags_obst, '\0');
289 const char *buf = obstack_finish(&cppflags_obst);
294 FILE *f = popen(buf, "r");
296 fprintf(stderr, "invoking preprocessor failed\n");
303 static void assemble(const char *out, const char *in)
307 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
312 int err = system(buf);
314 fprintf(stderr, "assembler reported an error\n");
319 static const char *try_dir(const char *dir)
323 if(access(dir, R_OK | W_OK | X_OK) == 0)
328 static const char *get_tempdir(void)
330 static const char *tmpdir = NULL;
336 tmpdir = try_dir(getenv("TMPDIR"));
338 tmpdir = try_dir(getenv("TMP"));
340 tmpdir = try_dir(getenv("TEMP"));
344 tmpdir = try_dir(P_tmpdir);
348 tmpdir = try_dir("/var/tmp");
350 tmpdir = try_dir("/usr/tmp");
352 tmpdir = try_dir("/tmp");
361 /* cheap and nasty mkstemp replacement */
362 static int mkstemp(char *templ)
365 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
370 * an own version of tmpnam, which: writes in a buffer, emits no warnings
371 * during linking (like glibc/gnu ld do for tmpnam)...
373 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
375 const char *tempdir = get_tempdir();
377 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
379 int fd = mkstemp(buffer);
381 fprintf(stderr, "couldn't create temporary file: %s\n",
385 FILE *out = fdopen(fd, "w");
387 fprintf(stderr, "couldn't create temporary file FILE*\n");
395 * Do the necessary lowering for compound parameters.
397 void lower_compound_params(void)
399 lower_params_t params;
401 params.def_ptr_alignment = 4;
402 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
403 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
404 params.find_pointer_type = NULL;
405 params.ret_compound_in_regs = NULL;
406 lower_calls_with_compounds(¶ms);
409 typedef enum compile_mode_t {
423 static void usage(const char *argv0)
425 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
428 static void print_cparser_version(void) {
430 firm_get_version(&ver);
432 printf("cparser (%s) using libFirm (%u.%u",
433 cparser_REVISION, ver.major, ver.minor);
434 if(ver.revision[0] != 0) {
436 fputs(ver.revision, stdout);
438 if(ver.build[0] != 0) {
440 fputs(ver.build, stdout);
445 static void set_be_option(const char *arg)
447 int res = firm_be_option(arg);
452 static void set_option(const char *arg)
454 int res = firm_option(arg);
459 static void copy_file(FILE *dest, FILE *input)
463 while (!feof(input) && !ferror(dest)) {
464 size_t read = fread(buf, 1, sizeof(buf), input);
465 if(fwrite(buf, 1, read, dest) != read) {
466 perror("couldn't write output");
471 static inline bool streq(char const* a, char const* b)
473 return strcmp(a, b) == 0;
476 static inline bool strstart(char const* str, char const* start)
481 } while (*str++ == *start++);
485 static FILE *open_file(const char *filename)
487 if (streq(filename, "-")) {
491 FILE *in = fopen(filename, "r");
493 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
501 static filetype_t get_filetype_from_string(const char *string)
503 if (streq(string, "c") || streq(string, "c-header"))
505 if (streq(string, "c++") || streq(string, "c++-header"))
507 if (streq(string, "assembler"))
508 return FILETYPE_PREPROCESSED_ASSEMBLER;
509 if (streq(string, "assembler-with-cpp"))
510 return FILETYPE_ASSEMBLER;
511 if (streq(string, "none"))
512 return FILETYPE_AUTODETECT;
514 return FILETYPE_UNKNOWN;
517 typedef enum lang_standard_t {
518 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
519 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
520 STANDARD_C89, /* ISO C90 (sic) */
521 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
522 STANDARD_C99, /* ISO C99 */
523 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
524 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
525 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
526 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
529 int main(int argc, char **argv)
533 const char *dumpfunction = NULL;
534 compile_mode_t mode = CompileAssembleLink;
536 int result = EXIT_SUCCESS;
537 char cpu_arch[16] = "ia32";
538 file_list_entry_t *files = NULL;
539 file_list_entry_t *last_file = NULL;
540 bool construct_dep_target = false;
541 struct obstack file_obst;
543 obstack_init(&cppflags_obst);
544 obstack_init(&ldflags_obst);
545 obstack_init(&file_obst);
547 #define GET_ARG_AFTER(def, args) \
548 def = &arg[sizeof(args)-1]; \
549 if(def[0] == '\0') { \
552 fprintf(stderr, "error: expected argument after '" args "'\n"); \
553 argument_errors = true; \
557 if(def[0] == '-' && def[1] != '\0') { \
558 fprintf(stderr, "error: expected argument after '" args "'\n"); \
559 argument_errors = true; \
564 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
566 /* early options parsing (find out optimisation level) */
567 for(int i = 1; i < argc; ++i) {
568 const char *arg = argv[i];
572 const char *option = &arg[1];
573 if(option[0] == 'O') {
574 sscanf(&option[1], "%d", &opt_level);
578 /* apply optimisation level */
581 set_option("no-opt");
584 set_option("no-inline");
588 set_option("strict-aliasing");
589 /* use_builtins = true; */
592 set_option("cond-eval");
593 set_option("if-conv");
596 set_option("inline");
597 set_option("deconv");
598 set_be_option("omitfp");
603 /* Darwin expects the stack to be aligned to 16byte boundary */
604 firm_be_option("ia32-stackalign=4");
607 /* parse rest of options */
608 lang_standard_t standard = STANDARD_DEFAULT;
609 lang_features_t features_on = 0;
610 lang_features_t features_off = 0;
611 filetype_t forced_filetype = FILETYPE_AUTODETECT;
612 bool help_displayed = false;
613 bool argument_errors = false;
614 for(int i = 1; i < argc; ++i) {
615 const char *arg = argv[i];
616 if (arg[0] == '-' && arg[1] != '\0') {
618 const char *option = &arg[1];
619 if(option[0] == 'o') {
620 GET_ARG_AFTER(outname, "-o");
621 } else if(option[0] == 'g') {
622 set_be_option("debuginfo=stabs");
623 set_be_option("omitfp=no");
624 set_be_option("ia32-nooptcc=yes");
625 } else if(SINGLE_OPTION('c')) {
626 mode = CompileAssemble;
627 } else if(SINGLE_OPTION('E')) {
628 mode = PreprocessOnly;
629 } else if(SINGLE_OPTION('S')) {
631 } else if(option[0] == 'O') {
633 } else if(option[0] == 'I') {
635 GET_ARG_AFTER(opt, "-I");
636 add_flag(&cppflags_obst, "-I%s", opt);
637 } else if(option[0] == 'D') {
639 GET_ARG_AFTER(opt, "-D");
640 add_flag(&cppflags_obst, "-D%s", opt);
641 } else if(option[0] == 'U') {
643 GET_ARG_AFTER(opt, "-U");
644 add_flag(&cppflags_obst, "-U%s", opt);
645 } else if(option[0] == 'l') {
647 GET_ARG_AFTER(opt, "-l");
648 add_flag(&ldflags_obst, "-l%s", opt);
649 } else if(option[0] == 'L') {
651 GET_ARG_AFTER(opt, "-L");
652 add_flag(&ldflags_obst, "-L%s", opt);
653 } else if(SINGLE_OPTION('v')) {
655 } else if(SINGLE_OPTION('w')) {
656 memset(&warning, 0, sizeof(warning));
657 } else if(option[0] == 'x') {
659 GET_ARG_AFTER(opt, "-x");
660 forced_filetype = get_filetype_from_string(opt);
661 if (forced_filetype == FILETYPE_UNKNOWN) {
662 fprintf(stderr, "Unknown language '%s'\n", opt);
663 argument_errors = true;
665 } else if (streq(option, "M")) {
666 mode = PreprocessOnly;
667 add_flag(&cppflags_obst, "-M");
668 } else if (streq(option, "MMD") ||
669 streq(option, "MD")) {
670 construct_dep_target = true;
671 add_flag(&cppflags_obst, "-%s", option);
672 } else if(streq(option, "MM") ||
673 streq(option, "MP")) {
674 add_flag(&cppflags_obst, "-%s", option);
675 } else if (streq(option, "MT") ||
676 streq(option, "MQ") ||
677 streq(option, "MF")) {
679 GET_ARG_AFTER(opt, "-MT");
680 add_flag(&cppflags_obst, "-%s", option);
681 add_flag(&cppflags_obst, "%s", opt);
682 } else if (streq(option, "pipe")) {
683 /* here for gcc compatibility */
684 } else if (option[0] == 'f') {
685 char const *orig_opt;
686 GET_ARG_AFTER(orig_opt, "-f");
688 if (strstart(orig_opt, "align-loops=") ||
689 strstart(orig_opt, "align-jumps=") ||
690 strstart(orig_opt, "align-functions=")) {
691 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
693 char const *opt = orig_opt;
694 bool truth_value = true;
695 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
700 if (streq(opt, "dollars-in-identifiers")) {
701 allow_dollar_in_symbol = truth_value;
702 } if (streq(opt, "builtins")) {
703 use_builtins = truth_value;
704 } else if (streq(opt, "short-wchar")) {
705 opt_short_wchar_t = truth_value;
706 } else if (streq(opt, "syntax-only")) {
707 mode = truth_value ? ParseOnly : CompileAssembleLink;
708 } else if (streq(opt, "omit-frame-pointer")) {
709 set_be_option(truth_value ? "omitfp" : "omitfp=no");
710 } else if (streq(opt, "strength-reduce")) {
711 firm_option(truth_value ? "strength-red" : "no-strength-red");
712 } else if (streq(opt, "fast-math") ||
713 streq(opt, "jump-tables") ||
714 streq(opt, "unroll-loops") ||
715 streq(opt, "expensive-optimizations") ||
716 streq(opt, "common") ||
718 streq(opt, "align-loops") ||
719 streq(opt, "align-jumps") ||
720 streq(opt, "align-functions")) {
721 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
723 int res = firm_option(orig_opt);
725 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
727 argument_errors = true;
729 } else if (res == -1) {
730 help_displayed = true;
734 } else if (option[0] == 'b') {
736 GET_ARG_AFTER(opt, "-b");
737 int res = firm_be_option(opt);
739 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
741 argument_errors = true;
742 } else if (res == -1) {
743 help_displayed = true;
744 } else if (strstart(opt, "isa=")) {
745 strncpy(cpu_arch, opt, sizeof(cpu_arch));
747 } else if (option[0] == 'W') {
748 if (strstart(option + 1, "l,")) // a gcc-style linker option
751 GET_ARG_AFTER(opt, "-Wl,");
752 add_flag(&ldflags_obst, "-Wl,%s", opt);
754 else set_warning_opt(&option[1]);
755 } else if(option[0] == 'm') {
760 GET_ARG_AFTER(opt, "-m");
761 if (strstart(opt, "arch=")) {
762 GET_ARG_AFTER(opt, "-march=");
763 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
764 int res = firm_be_option(arch_opt);
766 argument_errors = true;
768 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
769 int res = firm_be_option(arch_opt);
771 argument_errors = true;
773 } else if (strstart(opt, "tune=")) {
774 GET_ARG_AFTER(opt, "-mtune=");
775 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
776 int res = firm_be_option(arch_opt);
778 argument_errors = true;
779 } else if (strstart(opt, "cpu=")) {
780 GET_ARG_AFTER(opt, "-mcpu=");
781 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
782 int res = firm_be_option(arch_opt);
784 argument_errors = true;
785 } else if (strstart(opt, "fpmath=")) {
786 GET_ARG_AFTER(opt, "-mfpmath=");
787 if (streq(opt, "387"))
789 else if (streq(opt, "sse"))
792 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
793 argument_errors = true;
795 if(!argument_errors) {
796 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
797 int res = firm_be_option(arch_opt);
799 argument_errors = true;
801 } else if (strstart(opt, "preferred-stack-boundary=")) {
802 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
803 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
804 int res = firm_be_option(arch_opt);
806 argument_errors = true;
807 } else if (streq(opt, "omit-leaf-frame-pointer")) {
808 set_be_option("omitleaffp=1");
809 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
810 set_be_option("omitleaffp=0");
813 long int value = strtol(opt, &endptr, 10);
814 if (*endptr != '\0') {
815 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
816 argument_errors = true;
818 if (value != 16 && value != 32 && value != 64) {
819 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
820 argument_errors = true;
822 machine_size = (unsigned int)value;
825 } else if (streq(option, "pg")) {
826 set_be_option("gprof");
827 add_flag(&ldflags_obst, "-pg");
828 } else if (streq(option, "pedantic") ||
829 streq(option, "ansi")) {
830 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
831 } else if (streq(option, "shared")) {
832 add_flag(&ldflags_obst, "-shared");
833 } else if (strstart(option, "std=")) {
834 const char *const o = &option[4];
836 streq(o, "c++98") ? STANDARD_CXX98 :
837 streq(o, "c89") ? STANDARD_C89 :
838 streq(o, "c99") ? STANDARD_C99 :
839 streq(o, "c9x") ? STANDARD_C99 : // deprecated
840 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
841 streq(o, "gnu89") ? STANDARD_GNU89 :
842 streq(o, "gnu99") ? STANDARD_GNU99 :
843 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
844 streq(o, "iso9899:1990") ? STANDARD_C89 :
845 streq(o, "iso9899:199409") ? STANDARD_C90 :
846 streq(o, "iso9899:1999") ? STANDARD_C99 :
847 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
848 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
849 } else if (streq(option, "version")) {
850 print_cparser_version();
851 } else if (option[0] == '-') {
852 /* double dash option */
854 if (streq(option, "gcc")) {
855 features_on |= _GNUC;
856 features_off &= ~_GNUC;
857 } else if (streq(option, "no-gcc")) {
858 features_on &= ~_GNUC;
859 features_off |= _GNUC;
860 } else if (streq(option, "ms")) {
862 features_off &= ~_MS;
863 } else if (streq(option, "no-ms")) {
866 } else if (streq(option, "signed-chars")) {
867 char_is_signed = true;
868 } else if (streq(option, "unsigned-chars")) {
869 char_is_signed = false;
870 } else if (streq(option, "strict")) {
872 } else if (streq(option, "lextest")) {
874 } else if (streq(option, "benchmark")) {
875 mode = BenchmarkParser;
876 } else if (streq(option, "print-ast")) {
878 } else if (streq(option, "print-implicit-cast")) {
879 print_implicit_casts = true;
880 } else if (streq(option, "print-parenthesis")) {
881 print_parenthesis = true;
882 } else if (streq(option, "print-fluffy")) {
884 } else if (streq(option, "print-caml")) {
886 } else if (streq(option, "version")) {
887 print_cparser_version();
889 } else if (streq(option, "dump-function")) {
892 fprintf(stderr, "error: "
893 "expected argument after '--dump-function'\n");
894 argument_errors = true;
897 dumpfunction = argv[i];
900 fprintf(stderr, "error: unknown argument '%s'\n", arg);
901 argument_errors = true;
904 fprintf(stderr, "error: unknown argument '%s'\n", arg);
905 argument_errors = true;
908 filetype_t type = forced_filetype;
909 if (type == FILETYPE_AUTODETECT) {
910 if (streq(arg, "-")) {
911 /* - implicitly means C source file */
914 const char *suffix = strrchr(arg, '.');
915 /* Ensure there is at least one char before the suffix */
916 if (suffix != NULL && suffix != arg) {
919 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
920 streq(suffix, "a") ? FILETYPE_OBJECT :
921 streq(suffix, "c") ? FILETYPE_C :
922 streq(suffix, "cc") ? FILETYPE_CXX :
923 streq(suffix, "cpp") ? FILETYPE_CXX :
924 streq(suffix, "cxx") ? FILETYPE_CXX :
925 streq(suffix, "h") ? FILETYPE_C :
926 streq(suffix, "o") ? FILETYPE_OBJECT :
927 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
928 streq(suffix, "so") ? FILETYPE_OBJECT :
933 if (type == FILETYPE_AUTODETECT) {
934 fprintf(stderr, "'%s': file format not recognized\n", arg);
939 file_list_entry_t *entry
940 = obstack_alloc(&file_obst, sizeof(entry[0]));
941 memset(entry, 0, sizeof(entry[0]));
945 if (last_file != NULL) {
946 last_file->next = entry;
955 fprintf(stderr, "error: no input files specified\n");
956 argument_errors = true;
959 if (help_displayed) {
960 return !argument_errors;
962 if (argument_errors) {
967 /* we do the lowering in ast2firm */
968 firm_opt.lower_bitfields = FALSE;
980 if (construct_dep_target) {
981 if (outname != 0 && strlen(outname) >= 2) {
982 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
984 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
987 dep_target[0] = '\0';
990 char outnamebuf[4096];
991 if (outname == NULL) {
992 const char *filename = files->name;
995 case BenchmarkParser:
1000 case PreprocessOnly:
1005 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1006 outname = outnamebuf;
1008 case CompileAssemble:
1009 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1010 outname = outnamebuf;
1013 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1015 outname = outnamebuf;
1017 case CompileAssembleLink:
1027 assert(outname != NULL);
1030 if (streq(outname, "-")) {
1033 out = fopen(outname, "w");
1035 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1041 file_list_entry_t *file;
1042 for (file = files; file != NULL; file = file->next) {
1043 char asm_tempfile[1024];
1044 const char *filename = file->name;
1045 filetype_t filetype = file->type;
1047 if (filetype == FILETYPE_OBJECT)
1051 if (mode == LexTest) {
1053 in = open_file(filename);
1054 lextest(in, filename);
1059 FILE *preprocessed_in = NULL;
1061 case FILETYPE_C: filetype = FILETYPE_PREPROCESSED_C; goto preprocess;
1062 case FILETYPE_CXX: filetype = FILETYPE_PREPROCESSED_CXX; goto preprocess;
1063 case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1065 /* no support for input on FILE* yet */
1067 panic("internal compiler error: in for preprocessor != NULL");
1069 preprocessed_in = preprocess(filename);
1070 if (mode == PreprocessOnly) {
1071 copy_file(out, preprocessed_in);
1072 int result = pclose(preprocessed_in);
1077 in = preprocessed_in;
1085 if(mode == Compile) {
1088 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1092 in = open_file(filename);
1094 /* preprocess and compile */
1095 if (filetype == FILETYPE_PREPROCESSED_C) {
1096 char const* invalid_mode;
1099 case STANDARD_C89: c_mode = _C89; break;
1100 /* TODO ^v determine difference between these two */
1101 case STANDARD_C90: c_mode = _C89; break;
1102 case STANDARD_C99: c_mode = _C89 | _C99; break;
1103 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1107 "warning: command line option \"-std=%s\" is not valid for C\n",
1110 case STANDARD_DEFAULT:
1111 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1113 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1114 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1117 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1118 char const* invalid_mode;
1120 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1121 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1122 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1123 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1124 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1127 case STANDARD_CXX98: c_mode = _CXX; break;
1131 "warning: command line option \"-std=%s\" is not valid for C++\n",
1133 case STANDARD_DEFAULT:
1134 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1138 c_mode |= features_on;
1139 c_mode &= ~features_off;
1141 translation_unit_t *const unit = do_parsing(in, filename);
1143 /* prints the AST even if errors occurred */
1144 if (mode == PrintAst) {
1145 type_set_output(out);
1146 ast_set_output(out);
1150 if(error_count > 0) {
1151 /* parsing failed because of errors */
1152 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1154 result = EXIT_FAILURE;
1156 } else if(warning_count > 0) {
1157 fprintf(stderr, "%u warning(s)\n", warning_count);
1160 if (in == preprocessed_in) {
1161 int pp_result = pclose(preprocessed_in);
1162 if (pp_result != EXIT_SUCCESS) {
1167 if(mode == BenchmarkParser) {
1169 } else if(mode == PrintFluffy) {
1170 write_fluffy_decls(out, unit);
1172 } else if (mode == PrintCaml) {
1173 write_caml_decls(out, unit);
1177 translation_unit_to_firm(unit);
1179 if (mode == ParseOnly) {
1183 if (mode == CompileDump) {
1185 ident *id = new_id_from_str(dumpfunction);
1186 ir_graph *irg = NULL;
1187 int n_irgs = get_irp_n_irgs();
1188 for(int i = 0; i < n_irgs; ++i) {
1189 ir_graph *tirg = get_irp_irg(i);
1190 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1198 fprintf(stderr, "No graph for function '%s' found\n",
1203 dump_ir_block_graph_file(irg, out);
1208 gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1209 have_const_functions);
1210 if (asm_out != out) {
1213 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1214 copy_file(asm_out, in);
1215 if (in == preprocessed_in) {
1216 int pp_result = pclose(preprocessed_in);
1217 if (pp_result != EXIT_SUCCESS) {
1221 if(asm_out != out) {
1226 if (mode == Compile)
1229 /* if we're here then we have preprocessed assembly */
1230 filename = asm_tempfile;
1231 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1234 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1236 const char *filename_o;
1237 if(mode == CompileAssemble) {
1239 filename_o = outname;
1241 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1246 assemble(filename_o, filename);
1248 size_t len = strlen(filename_o) + 1;
1249 filename = obstack_copy(&file_obst, filename_o, len);
1250 filetype = FILETYPE_OBJECT;
1253 /* ok we're done here, process next file */
1254 file->name = filename;
1255 file->type = filetype;
1258 if (result != EXIT_SUCCESS)
1261 /* link program file */
1262 if(mode == CompileAssembleLink) {
1263 obstack_1grow(&ldflags_obst, '\0');
1264 const char *flags = obstack_finish(&ldflags_obst);
1266 /* construct commandline */
1267 obstack_printf(&file_obst, "%s", LINKER);
1268 for (file_list_entry_t *entry = files; entry != NULL;
1269 entry = entry->next) {
1270 if (entry->type != FILETYPE_OBJECT)
1273 add_flag(&file_obst, "%s", entry->name);
1276 add_flag(&file_obst, "-o");
1277 add_flag(&file_obst, outname);
1278 obstack_printf(&file_obst, "%s", flags);
1279 obstack_1grow(&file_obst, '\0');
1281 char *commandline = obstack_finish(&file_obst);
1286 int err = system(commandline);
1287 if(err != EXIT_SUCCESS) {
1288 fprintf(stderr, "linker reported an error\n");
1293 obstack_free(&cppflags_obst, NULL);
1294 obstack_free(&ldflags_obst, NULL);
1295 obstack_free(&file_obst, NULL);
1304 exit_symbol_table();