further improve initializers
[cparser] / main.c
diff --git a/main.c b/main.c
index 802eb35..dbe4cfb 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,10 @@
 #include <config.h>
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <errno.h>
 #include <string.h>
 #include <assert.h>
 #include "parser.h"
 #include "ast2firm.h"
 #include "adt/error.h"
+#include "write_fluffy.h"
+
+#ifndef PREPROCESSOR
+#define PREPROCESSOR "cpp -std=c99"
+#endif
 
-#define PREPROCESSOR "gcc -E"
+#ifndef LINKER
 #define LINKER       "gcc"
+#endif
+
+#ifdef _WIN32
+/* remap some names */
+#define popen(cmd, mode)  _popen(cmd, mode)
+#define pclose(file)      _pclose(file)
+#endif /* _WIN32 */
 
-static int verbose;
+static int  verbose;
+static bool do_dump;
 
 static const ir_settings_if_conv_t *if_conv_info = NULL;
+static const backend_params        *be_params    = NULL;
 
 static void initialize_firm(void)
 {
        be_opt_register();
        firm_init_options(NULL, 0, NULL);
 
-       const backend_params *be_params;
        firm_parameter_t params;
        memset(&params, 0, sizeof(params));
 
@@ -44,10 +60,12 @@ static void initialize_firm(void)
 
        /* initialize backend */
        be_params = be_init();
-       be_set_debug_retrieve(retrieve_dbg);
+       ir_set_debug_retrieve(retrieve_dbg);
        params.arch_op_settings = be_params->arch_op_settings;
        if_conv_info            = be_params->if_conv_info;
 
+       (void) if_conv_info; /* avoid unused warning */
+
        /* intialize firm itself */
        init_firm(&params);
        dbg_init(NULL, NULL, dbg_snprint);
@@ -57,19 +75,11 @@ static void initialize_firm(void)
        set_opt_control_flow_straightening(1);
        set_opt_control_flow_weak_simplification(1);
        set_opt_control_flow_strong_simplification(1);
-       set_opt_dead_node_elimination(1);
-       set_opt_reassociation(1);
-       set_opt_inline(1);
        set_opt_dyn_meth_dispatch(1);
        set_opt_normalize(1);
-       set_opt_tail_recursion(1);
-       set_opt_dead_method_elimination(1);
        set_opt_precise_exc_context(0);
-       set_opt_loop_unrolling(0);
        set_opt_strength_red(0);
-       set_opt_redundant_loadstore(1);
        set_opt_fragile_ops(0);
-       set_opt_function_call(1);
        set_opt_optimize_class_casts(0);
        set_opt_suppress_downcast_optimization(0);
        set_opt_remove_confirm(1);
@@ -83,7 +93,9 @@ static void initialize_firm(void)
 
 static void dump(ir_graph *irg, const char *suffix)
 {
-       dump_ir_block_graph(irg, suffix);
+       if(do_dump) {
+               dump_ir_block_graph(irg, suffix);
+       }
 }
 
 static void get_output_name(char *buf, size_t buflen, const char *inputname,
@@ -109,20 +121,10 @@ static void get_output_name(char *buf, size_t buflen, const char *inputname,
        memcpy(buf+last_dot, newext, extlen);
 }
 
-static translation_unit_t *do_parsing(const char *fname, const char *input)
+static translation_unit_t *do_parsing(FILE *const in, const char *const input)
 {
-       FILE *in = fopen(fname, "r");
-       if(in == NULL) {
-               fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
-               exit(1);
-       }
-
        lexer_open_stream(in, input);
-
        translation_unit_t *unit = parse();
-
-       fclose(in);
-
        return unit;
 }
 
@@ -164,19 +166,21 @@ static void emit(const char *input_name, const char *out_name)
        backend(input_name, out_name);
 }
 
-static void preprocess(const char *in, const char *out)
+static FILE* preprocess(const char *in)
 {
        char buf[4096];
 
-       snprintf(buf, sizeof(buf), "%s %s -o %s", PREPROCESSOR, in, out);
+       snprintf(buf, sizeof(buf), PREPROCESSOR " %s", in);
+
        if(verbose) {
                puts(buf);
        }
-       int err = system(buf);
-       if(err != 0) {
-               fprintf(stderr, "preprocessor reported an error\n");
+       FILE* f = popen(buf, "r");
+       if (f == NULL) {
+               fprintf(stderr, "invoking preprocessor failed\n");
                exit(1);
        }
+       return f;
 }
 
 static void link(const char *in, const char *out)
@@ -213,11 +217,29 @@ static void create_firm_prog(translation_unit_t *unit)
 {
        translation_unit_to_firm(unit);
 
+       //dump_globals_as_text(dump_verbosity_max, "-globals");
+
        int n_irgs = get_irp_n_irgs();
        for(int i = 0; i < n_irgs; ++i) {
                ir_graph *const irg = get_irp_irg(i);
                dump(irg, "-start");
        }
+
+       lower_params_t params;
+
+       params.def_ptr_alignment    = 4;
+       params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
+       params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
+       params.find_pointer_type    = NULL;
+       params.ret_compound_in_regs = NULL;
+       lower_calls_with_compounds(&params);
+
+       lower_highlevel();
+
+       for(int i = 0; i < n_irgs; ++i) {
+               ir_graph *const irg = get_irp_irg(i);
+               dump(irg, "-lower");
+       }
 }
 
 static void optimize(void)
@@ -231,19 +253,37 @@ static void optimize(void)
 
        optimize_funccalls(0);
 
+       lwrdw_param_t lwrdw_param = {
+               1,
+               1,
+               mode_Ls, mode_Lu,
+               mode_Is, mode_Iu,
+               def_create_intrinsic_fkt,
+               NULL
+       };
+       if (be_params->arch_create_intrinsic_fkt) {
+               lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
+               lwrdw_param.ctx              = be_params->create_intrinsic_ctx;
+       }
+
        for(int i = 0; i < get_irp_n_irgs(); ++i) {
                ir_graph *irg = get_irp_irg(i);
+
+               optimize_graph_df(irg);
+               dump(irg, "-01-localopt");
                place_code(irg);
-               dump(irg, "-place");
+               dump(irg, "-02-place");
+               optimize_cf(irg);
+               dump(irg, "-03-cf");
+               lower_dw_ops(&lwrdw_param);
+               dump(irg, "-04-dw");
                optimize_graph_df(irg);
-               dump(irg, "-localopt");
+               dump(irg, "-05-localopt");
                optimize_cf(irg);
-               dump(irg, "-cf");
+               dump(irg, "-06-cf");
        }
 }
 
-void write_fluffy_decls(translation_unit_t *unit);
-
 typedef enum compile_mode_t {
        Compile,
        CompileAssemble,
@@ -264,9 +304,9 @@ int main(int argc, char **argv)
 
        init_symbol_table();
        init_tokens();
-       init_lexer();
        init_types();
        init_typehash();
+       init_lexer();
        init_ast();
        init_parser();
        init_ast2firm();
@@ -294,11 +334,52 @@ int main(int argc, char **argv)
                        mode = PrintAst;
                } else if(strcmp(arg, "--print-fluffy") == 0) {
                        mode = PrintFluffy;
+               } else if(strcmp(arg, "--dump") == 0) {
+                       do_dump = true;
                } else if(strcmp(arg, "-v") == 0) {
                        verbose = 1;
+               } else if(arg[0] == '-' && arg[1] == 'f') {
+                       const char *opt = &arg[2];
+                       if(opt[0] == 0) {
+                               ++i;
+                               if(i >= argc) {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                               opt = argv[i];
+                               if(opt[0] == '-') {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                       }
+                       //firm_option(opt);
+               } else if(arg[0] == '-' && arg[1] == 'b') {
+                       const char *opt = &arg[2];
+                       if(opt[0] == 0) {
+                               ++i;
+                               if(i >= argc) {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                               opt = argv[i];
+                               if(opt[0] == '-') {
+                                       usage(argv[0]);
+                                       return 1;
+                               }
+                       }
+                       //firm_be_option(opt);
                } else if(arg[0] == '-') {
-                       usage(argv[0]);
-                       return 1;
+                       if (arg[1] == 'D' ||
+                                       arg[1] == 'O' ||
+                                       arg[1] == 'f' ||
+                                       arg[1] == 'W' ||
+                                       arg[1] == 'g' ||
+                                       strncmp(arg + 1, "std=", 4) == 0) {
+                               fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
+                       } else {
+                               usage(argv[0]);
+                               return 1;
+                       }
                } else {
                        if(input != NULL) {
                                fprintf(stderr, "Error: multiple input files specified\n");
@@ -320,10 +401,9 @@ int main(int argc, char **argv)
                return 0;
        }
 
-       const char *tmpfile = tmpnam(NULL);
-       preprocess(input, tmpfile);
-
-       translation_unit_t *unit = do_parsing(tmpfile, input);
+       FILE *const in = preprocess(input);
+       translation_unit_t *const unit = do_parsing(in, input);
+       pclose(in);
        if(unit == NULL)
                return 1;
 
@@ -370,9 +450,9 @@ int main(int argc, char **argv)
        exit_ast2firm();
        exit_parser();
        exit_ast();
+       exit_lexer();
        exit_typehash();
        exit_types();
-       exit_lexer();
        exit_tokens();
        exit_symbol_table();
        return 0;