#endif
#include <stdarg.h>
-#include <mcheck.h>
#include "obst.h"
#include "bitset.h"
#include "becopyoptmain.h"
#include "becopystat.h"
#include "bessadestr.h"
-#include "bearch_firm.h"
#include "benode_t.h"
#include "beirgmod.h"
#include "bespillilp.h"
#include "bespillbelady.h"
-#include "beasm_dump_globals.h"
-#include "beasm_asm_gnu.h"
+// #include "beasm_dump_globals.h"
+// #include "beasm_asm_gnu.h"
+
+// #include "bearch_firm.h"
#undef DUMP_BEGIN
-#undef DUMP_PREPARED
+#define DUMP_PREPARED
#define DUMP_SCHED
#define DUMP_SPILL
#undef DUMP_ALLOCATED
#define N_PHASES 256
+extern const arch_isa_if_t firm_isa;
+extern const arch_isa_if_t ia32_isa;
+
+const struct {
+ const char *name;
+ const arch_isa_if_t *isa;
+} isa_name_arch_map[] = {
+ { "firm", &firm_isa },
+ { "ia32", &ia32_isa }
+};
+
+#define N_ISAS (sizeof(isa_name_arch_map) / sizeof(isa_name_arch_map[0]))
+
+const arch_isa_if_t *get_backend_isa_if(const char *name) {
+ int i;
+
+ for (i = 0; i < N_ISAS; i++) {
+ if (!strcmp(isa_name_arch_map[i].name, name))
+ return isa_name_arch_map[i].isa;
+ }
+
+ fprintf(stderr, "Unsupported ISA '%s'\n", name);
+ assert(0);
+ return NULL;
+}
+
void be_init(void)
{
- be_sched_init();
- be_liveness_init();
- be_numbering_init();
- be_ra_chordal_init();
- be_copy_opt_init();
- copystat_init();
- phi_class_init();
+ be_sched_init();
+ be_liveness_init();
+ be_numbering_init();
+ be_ra_chordal_init();
+ be_copy_opt_init();
+ copystat_init();
+ phi_class_init();
}
-static be_main_env_t *be_init_env(be_main_env_t *env)
+static be_main_env_t *be_init_env(be_main_env_t *env, const arch_isa_if_t *isa)
{
- const arch_isa_if_t *isa = &firm_isa;
-
obstack_init(&env->obst);
env->dbg = firm_dbg_register("be.main");
firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
be_node_factory_init(env->node_factory, isa);
- arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
- arch_env_add_irn_handler(env->arch_env,
- be_node_get_irn_handler(env->node_factory));
+ /* Register the irn handler of the architecture */
+ if (isa->irn_handler)
+ arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
+
+ /*
+ * Register the node handler of the back end infrastructure.
+ * This irn handler takes care of the platform independent
+ * spill, reload and perm nodes.
+ */
+ arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
return env;
}
static void prepare_graph(be_main_session_env_t *s)
{
- /* set the current graph (this is important for several firm functions) */
- current_ir_graph = s->irg;
+ /* set the current graph (this is important for several firm functions) */
+ current_ir_graph = s->irg;
- /* Let the isa prepare the graph. */
- s->main_env->arch_env->isa->prepare_graph(s->irg);
+ /* Let the isa prepare the graph. */
+ s->main_env->arch_env->isa->prepare_graph(s->irg);
- /* Normalize proj nodes. */
- normalize_proj_nodes(s->irg);
+ /* Normalize proj nodes. */
+ normalize_proj_nodes(s->irg);
- /* Remove critical edges */
- remove_critical_cf_edges(s->irg);
+ /* Remove critical edges */
+ remove_critical_cf_edges(s->irg);
- /* Compute the dominance information. */
- free_dom_and_peace(s->irg);
- compute_doms(s->irg);
+ /* Compute the dominance information. */
+ free_dom_and_peace(s->irg);
+ compute_doms(s->irg);
- /* Compute the dominance frontiers */
- s->dom_front = be_compute_dominance_frontiers(s->irg);
+ /* Compute the dominance frontiers */
+ s->dom_front = be_compute_dominance_frontiers(s->irg);
- /* Ensure, that the ir_edges are computed. */
- edges_activate(s->irg);
+ /* Ensure, that the ir_edges are computed. */
+ edges_activate(s->irg);
- /* Compute loop nesting information (for weighting copies) */
- if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
- construct_cf_backedges(s->irg);
+ /* Compute loop nesting information (for weighting copies) */
+ if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
+ construct_cf_backedges(s->irg);
- be_check_dominance(s->irg);
+ be_check_dominance(s->irg);
}
-static void be_main_loop(void)
+static void be_main_loop(const arch_isa_if_t *isa)
{
- int i, n;
+ int i, j, n, m;
be_main_env_t env;
- const arch_isa_if_t *isa;
- be_init_env(&env);
- isa = arch_env_get_isa(env.arch_env);
+ be_init_env(&env, isa);
/* For all graphs */
for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
- int j, m;
ir_graph *irg = get_irp_irg(i);
be_main_session_env_t session;
- DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
+ DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
#ifdef DUMP_BEGIN
dump_ir_block_graph(irg, "-begin");
#endif
/* Compute some analyses and prepare the graph for backend use. */
prepare_graph(&session);
+
#ifdef DUMP_PREPARED
- dump_dominator_information(true);
+ dump_dominator_information(1);
dump_ir_block_graph(irg, "-prepared");
- dump_dominator_information(false);
+ dump_dominator_information(0);
#endif
/* Schedule the graphs. */
list_sched(irg, trivial_selector);
+
+// TODO: add architecture dependent schedule magic
+// be_sched_imm(irg);
+
#ifdef DUMP_SCHED
dump_ir_block_graph_sched(irg, "-sched");
#endif
DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
/* spilling */
- be_spill_ilp(&session, cls);
- //be_spill_belady(&session, cls);
+ //be_spill_ilp(&session, cls);
+ be_spill_belady(&session, cls);
#ifdef DUMP_SPILL
dump_ir_block_graph_sched(session.irg, "-spill");
#endif
}
}
+/**
+ * Backend main driver. Expects the following arguments:
+ * argc = 2
+ * argv[0] = backend isa name
+ * argv[1] = output file name
+ */
void be_main(int argc, const char *argv[])
{
- assembler_t *gnu_assembler;
- FILE *asm_output_file;
+ FILE *asm_output_file;
+ const arch_isa_if_t *isa;
- be_main_loop();
+ /* get requested isa */
+ isa = get_backend_isa_if(argv[0]);
-#if 0
- gnu_assembler = gnuasm_create_assembler();
- asm_output_file = fopen("asm_output.asm", "w");
+ be_main_loop(isa);
- asm_dump_globals(gnu_assembler);
- gnuasm_dump(gnu_assembler, asm_output_file);
- gnuasm_delete_assembler(gnu_assembler);
- fclose(asm_output_file);
-#endif
+ /* generate code if supported by isa */
+ if (isa->codegen) {
+ asm_output_file = fopen(argv[1], "w");
+ isa->codegen(asm_output_file);
+ fclose(asm_output_file);
+ }
}