Fix C/gnu99/typeof.c: revert_automatic_type_conversion() was missing in the default...
[cparser] / main.c
diff --git a/main.c b/main.c
index dc9cb03..36e2962 100644 (file)
--- a/main.c
+++ b/main.c
@@ -79,6 +79,7 @@
 #include "revision.h"
 #include "warning.h"
 #include "mangle.h"
+#include "printer.h"
 
 #ifndef PREPROCESSOR
 #ifndef __WIN32__
@@ -94,7 +95,7 @@
 
 #ifndef ASSEMBLER
 #ifdef __APPLE__
-#define ASSEMBLER "gcc -c -xassembler"
+#define ASSEMBLER "gcc -m32 -c -xassembler"
 #else
 #define ASSEMBLER "as --32"
 #endif
@@ -217,7 +218,7 @@ static void get_output_name(char *buf, size_t buflen, const char *inputname,
                panic("filename too long");
 }
 
-#include "builtins.h"
+#include "gen_builtins.h"
 
 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
 {
@@ -316,18 +317,6 @@ static FILE *preprocess(const char *fname, filetype_t filetype)
                add_flag(&cppflags_obst, "-U__VERSION__");
                add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
 
-               /* TODO hack... */
-               add_flag(&cppflags_obst, "-D__builtin_abort=abort");
-               add_flag(&cppflags_obst, "-D__builtin_abs=abs");
-               add_flag(&cppflags_obst, "-D__builtin_exit=exit");
-               add_flag(&cppflags_obst, "-D__builtin_malloc=malloc");
-               add_flag(&cppflags_obst, "-D__builtin_memcmp=memcmp");
-               add_flag(&cppflags_obst, "-D__builtin_memcpy=memcpy");
-               add_flag(&cppflags_obst, "-D__builtin_memset=memset");
-               add_flag(&cppflags_obst, "-D__builtin_strlen=strlen");
-               add_flag(&cppflags_obst, "-D__builtin_strcmp=strcmp");
-               add_flag(&cppflags_obst, "-D__builtin_strcpy=strcpy");
-
                if (flags[0] != '\0') {
                        size_t len = strlen(flags);
                        obstack_1grow(&cppflags_obst, ' ');
@@ -338,7 +327,12 @@ static FILE *preprocess(const char *fname, filetype_t filetype)
        }
 
        assert(obstack_object_size(&cppflags_obst) == 0);
-       obstack_printf(&cppflags_obst, "%s ", PREPROCESSOR);
+
+       const char *preprocessor = getenv("CPARSER_PP");
+       if (preprocessor == NULL)
+               preprocessor = PREPROCESSOR;
+
+       obstack_printf(&cppflags_obst, "%s ", preprocessor);
        switch (filetype) {
        case FILETYPE_C:
                add_flag(&cppflags_obst, "-std=c99");
@@ -388,7 +382,11 @@ static void assemble(const char *out, const char *in)
 {
        char buf[65536];
 
-       snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
+       const char *assembler = getenv("CPARSER_AS");
+       if (assembler == NULL)
+               assembler = ASSEMBLER;
+
+       snprintf(buf, sizeof(buf), "%s %s -o %s", assembler, in, out);
        if (verbose) {
                puts(buf);
        }
@@ -408,7 +406,10 @@ static void print_file_name(const char *file)
        const char *flags = obstack_finish(&ldflags_obst);
 
        /* construct commandline */
-       obstack_printf(&ldflags_obst, "%s ", LINKER);
+       const char *linker = getenv("CPARSER_LINK");
+       if (linker == NULL)
+               linker = LINKER;
+       obstack_printf(&ldflags_obst, "%s ", linker);
        obstack_printf(&ldflags_obst, "%s", flags);
        obstack_1grow(&ldflags_obst, '\0');
 
@@ -756,7 +757,7 @@ int main(int argc, char **argv)
                /* fallthrough */
        case 3:
                set_option("thread-jumps");
-               set_option("if-conv");
+               set_option("if-conversion");
                /* fallthrough */
        case 2:
                set_option("strict-aliasing");
@@ -861,11 +862,7 @@ int main(int argc, char **argv)
                                char const *orig_opt;
                                GET_ARG_AFTER(orig_opt, "-f");
 
-                               if (strstart(orig_opt, "align-loops=") ||
-                                   strstart(orig_opt, "align-jumps=") ||
-                                   strstart(orig_opt, "align-functions=")) {
-                                       fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
-                               } else if (strstart(orig_opt, "input-charset=")) {
+                               if (strstart(orig_opt, "input-charset=")) {
                                        char const* const encoding = strchr(orig_opt, '=') + 1;
                                        select_input_encoding(encoding);
                                } else if (streq(orig_opt, "verbose-asm")) {
@@ -895,15 +892,23 @@ int main(int argc, char **argv)
                                                mode = truth_value ? ParseOnly : CompileAssembleLink;
                                        } else if (streq(opt, "unsigned-char")) {
                                                char_is_signed = !truth_value;
+                                       } else if (truth_value == false &&
+                                                  streq(opt, "asynchronous-unwind-tables")) {
+                                           /* nothing todo, a gcc feature which we don't support
+                                            * anyway was deactivated */
+                                       } else if (strstart(orig_opt, "align-loops=") ||
+                                                       strstart(orig_opt, "align-jumps=") ||
+                                                       strstart(orig_opt, "align-functions=")) {
+                                               fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
                                        } else if (streq(opt, "fast-math")               ||
                                                   streq(opt, "jump-tables")             ||
-                                                  streq(opt, "unroll-loops")            ||
                                                   streq(opt, "expensive-optimizations") ||
                                                   streq(opt, "common")                  ||
-                                                  streq(opt, "PIC")                     ||
+                                                  streq(opt, "optimize-sibling-calls")  ||
                                                   streq(opt, "align-loops")             ||
                                                   streq(opt, "align-jumps")             ||
-                                                  streq(opt, "align-functions")) {
+                                                  streq(opt, "align-functions")         ||
+                                                  streq(opt, "PIC")) {
                                                fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
                                        } else {
                                                int res = firm_option(orig_opt);
@@ -931,7 +936,9 @@ int main(int argc, char **argv)
                                        strncpy(cpu_arch, opt, sizeof(cpu_arch));
                                }
                        } else if (option[0] == 'W') {
-                               if (strstart(option + 1, "p,")) {
+                               if (option[1] == '\0') {
+                                       /* ignore -W, out defaults are already quiet verbose */
+                               } else if (strstart(option + 1, "p,")) {
                                        // pass options directly to the preprocessor
                                        const char *opt;
                                        GET_ARG_AFTER(opt, "-Wp,");
@@ -957,9 +964,10 @@ int main(int argc, char **argv)
                                        GET_ARG_AFTER(opt, "-march=");
                                        snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
                                        int res = firm_be_option(arch_opt);
-                                       if (res == 0)
+                                       if (res == 0) {
+                                               fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
                                                argument_errors = true;
-                                       else {
+                                       else {
                                                snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
                                                int res = firm_be_option(arch_opt);
                                                if (res == 0)
@@ -1005,6 +1013,12 @@ int main(int argc, char **argv)
                                        set_be_option("omitleaffp=0");
                                } else if (streq(opt, "rtd")) {
                                        default_calling_convention = CC_STDCALL;
+                               } else if (strstart(opt, "regparm=")) {
+                                       fprintf(stderr, "error: regparm convention not supported yet\n");
+                                       argument_errors = true;
+                               } else if (streq(opt, "soft-float")) {
+                                       fprintf(stderr, "error: software floatingpoint not supported yet\n");
+                                       argument_errors = true;
                                } else {
                                        char *endptr;
                                        long int value = strtol(opt, &endptr, 10);
@@ -1260,6 +1274,7 @@ int main(int argc, char **argv)
        }
 
        file_list_entry_t *file;
+       bool               already_constructed_firm = false;
        for (file = files; file != NULL; file = file->next) {
                char        asm_tempfile[1024];
                const char *filename = file->name;
@@ -1371,15 +1386,17 @@ do_parsing:
                        c_mode |= features_on;
                        c_mode &= ~features_off;
 
-                       timer_push(TV_PARSING);
+                       /* do the actual parsing */
+                       ir_timer_t *t_parsing = ir_timer_new();
+                       timer_register(t_parsing, "Frontend: Parsing");
+                       timer_push(t_parsing);
                        init_tokens();
                        translation_unit_t *const unit = do_parsing(in, filename);
-                       timer_pop();
+                       timer_pop(t_parsing);
 
                        /* prints the AST even if errors occurred */
                        if (mode == PrintAst) {
-                               type_set_output(out);
-                               ast_set_output(out);
+                               print_to_file(out);
                                print_ast(unit);
                        }
 
@@ -1416,9 +1433,16 @@ do_parsing:
                                continue;
                        }
 
-                       timer_push(TV_CONSTRUCT);
+                       /* build the firm graph */
+                       ir_timer_t *t_construct = ir_timer_new();
+                       timer_register(t_construct, "Frontend: Graph construction");
+                       timer_push(t_construct);
+                       if (already_constructed_firm) {
+                               panic("compiling multiple files/translation units not possible");
+                       }
                        translation_unit_to_firm(unit);
-                       timer_pop();
+                       already_constructed_firm = true;
+                       timer_pop(t_construct);
 
 graph_built:
                        if (mode == ParseOnly) {
@@ -1525,7 +1549,10 @@ graph_built:
                const char *flags = obstack_finish(&ldflags_obst);
 
                /* construct commandline */
-               obstack_printf(&file_obst, "%s", LINKER);
+               const char *linker = getenv("CPARSER_LINK");
+               if (linker == NULL)
+                       linker = LINKER;
+               obstack_printf(&file_obst, "%s", linker);
                for (file_list_entry_t *entry = files; entry != NULL;
                                entry = entry->next) {
                        if (entry->type != FILETYPE_OBJECT)