+#include "bespillbelady.h"
+#include "bera.h"
+#include "beraextern.h"
+#include "bechordal_t.h"
+#include "beifg.h"
+#include "beifg_impl.h"
+#include "becopyopt.h"
+#include "becopystat.h"
+#include "bessadestr.h"
+#include "beabi.h"
+#include "belower.h"
+#include "beschedmris.h"
+#include "bestat.h"
+#include "beverify.h"
+#include "beprofile.h"
+#include "be_dbgout.h"
+
+#ifdef WITH_ILP
+#include "beilpsched.h"
+#endif /* WITH_ILP */
+
+/* options visible for anyone */
+static be_options_t be_options = {
+ DUMP_NONE, /* dump flags */
+ BE_TIME_OFF, /* no timing */
+ 0, /* no opt profile */
+ 1, /* try to omit frame pointer */
+ 0, /* always stabs debugging output */
+ BE_VRFY_WARN, /* verification level: warn */
+ BE_SCHED_LIST, /* scheduler: list scheduler */
+ "i44pc52.info.uni-karlsruhe.de", /* ilp server */
+ "cplex" /* ilp solver */
+};
+
+/* config file. */
+static char config_file[256] = { 0 };
+
+/* back end instruction set architecture to use */
+static const arch_isa_if_t *isa_if = NULL;
+
+#ifdef WITH_LIBCORE
+
+/* possible dumping options */
+static const lc_opt_enum_mask_items_t dump_items[] = {
+ { "none", DUMP_NONE },
+ { "initial", DUMP_INITIAL },
+ { "abi", DUMP_ABI },
+ { "sched", DUMP_SCHED },
+ { "prepared", DUMP_PREPARED },
+ { "regalloc", DUMP_RA },
+ { "final", DUMP_FINAL },
+ { "be", DUMP_BE },
+ { "all", 2 * DUMP_BE - 1 },
+ { NULL, 0 }
+};
+
+/* verify options. */
+static const lc_opt_enum_int_items_t vrfy_items[] = {
+ { "off", BE_VRFY_OFF },
+ { "warn", BE_VRFY_WARN },
+ { "assert", BE_VRFY_ASSERT },
+ { NULL, 0 }
+};
+
+/* scheduling options. */
+static const lc_opt_enum_int_items_t sched_items[] = {
+ { "list", BE_SCHED_LIST },
+#ifdef WITH_ILP
+ { "ilp", BE_SCHED_ILP },
+#endif /* WITH_ILP */
+ { NULL, 0 }
+};
+
+static lc_opt_enum_mask_var_t dump_var = {
+ &be_options.dump_flags, dump_items
+};
+
+static lc_opt_enum_int_var_t vrfy_var = {
+ &be_options.vrfy_option, vrfy_items
+};
+
+static lc_opt_enum_int_var_t sched_var = {
+ &be_options.scheduler, sched_items
+};
+
+static const lc_opt_table_entry_t be_main_options[] = {
+ LC_OPT_ENT_STR ("config", "read another config file containing backend options", config_file, sizeof(config_file)),
+ LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
+ LC_OPT_ENT_NEGBOOL ("noomitfp", "do not omit frame pointer", &be_options.omit_fp),
+ LC_OPT_ENT_BOOL ("stabs", "enable stabs debug support", &be_options.stabs_debug_support),
+ LC_OPT_ENT_ENUM_PTR ("vrfy", "verify the backend irg", &vrfy_var),
+ LC_OPT_ENT_BOOL ("time", "get backend timing statistics", &be_options.timing),
+ LC_OPT_ENT_BOOL ("profile", "instrument the code for execution count profiling", &be_options.opt_profile),
+ LC_OPT_ENT_ENUM_PTR ("sched", "select a scheduler", &sched_var),
+ LC_OPT_ENT_STR ("statfile", "append statistics to file statfile", &be_options.stat_file_name, sizeof(be_options.stat_file_name)),
+
+#ifdef WITH_ILP
+ LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
+ LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
+#endif /* WITH_ILP */
+ { NULL }
+};
+
+#endif /* WITH_LIBCORE */
+
+static be_module_list_entry_t *isa_ifs = NULL;
+
+void be_register_isa_if(const char *name, const arch_isa_if_t *isa)
+{
+ if(isa_if == NULL)
+ isa_if = isa;
+
+ be_add_module_to_list(&isa_ifs, name, (void*) isa);
+}
+
+void be_opt_register(void)
+{
+#ifdef WITH_LIBCORE
+ lc_opt_entry_t *be_grp;
+ static int run_once = 0;
+
+ if (run_once) {
+ return;
+ }
+ run_once = 1;
+
+ be_init_modules();
+
+ be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
+ lc_opt_add_table(be_grp, be_main_options);
+
+ be_add_module_list_opt(be_grp, "isa", "the instruction set architecture",
+ &isa_ifs, (void**) &isa_if);
+#endif /* WITH_LIBCORE */
+}
+
+/* Parse one argument. */
+int be_parse_arg(const char *arg) {
+#ifdef WITH_LIBCORE
+ lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
+ if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
+ lc_opt_print_help(be_grp, stdout);
+ return -1;
+ }
+ return lc_opt_from_single_arg(be_grp, NULL, arg, NULL);
+#else
+ return 0;
+#endif /* WITH_LIBCORE */
+}
+
+/** The be parameters returned by default, all off. */
+const static backend_params be_params = {
+ NULL,
+ NULL,
+ 0,
+ NULL,
+};
+
+/* Perform schedule verification if requested. */
+static void be_sched_vrfy(ir_graph *irg, int vrfy_opt) {
+ if (vrfy_opt == BE_VRFY_WARN) {
+ be_verify_schedule(irg);
+ }
+ else if (vrfy_opt == BE_VRFY_ASSERT) {
+ assert(be_verify_schedule(irg) && "Schedule verification failed.");
+ }
+}
+
+/* Initialize the Firm backend. Must be run BEFORE init_firm()! */
+const backend_params *be_init(void)
+{
+ be_opt_register();
+ be_init_modules();
+
+ if (isa_if->get_params)
+ return isa_if->get_params();
+ return &be_params;
+}
+
+/**
+ * Initializes the main environment for the backend.
+ *
+ * @param env an empty environment
+ * @param file_handle the file handle where the output will be written to
+ */
+static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
+{
+ memset(env, 0, sizeof(*env));
+ obstack_init(&env->obst);
+ env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
+ env->options = &be_options;
+ FIRM_DBG_REGISTER(env->dbg, "be.main");