unsigned omit_framepointer: 1; /**< If one, the frame(base-)pointer can be used
as an ordinary register. */
+
+ firm_dbg_module_t *dbg; /**< The debugging module. */
};
static int cmp_call_arg(const void *a, const void *b, size_t n)
int max_arg = 0;
int reg_params_nr = 0;
- int n_reg_param = 0;
+ ir_node *proj_sp = NULL;
int i, j, n;
- ir_node *proj_sp;
ir_node *frame_pointer;
ir_node *reg_params, *reg_params_bl;
ir_node **args, **args_repl, **return_params;
const ir_edge_t *edge;
+ const arch_register_t *reg;
+
+ pset *callee_save = pset_new_ptr_default();
+ pset *regs = pset_new_ptr_default();
+
+ firm_dbg_module_t *dbg = env->dbg;
+ DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
/* Find the maximum proj number of the argument tuple proj */
foreach_out_edge(arg_tuple, edge) {
ir_node *irn = get_edge_src_irn(edge);
int nr = get_Proj_proj(irn);
args[nr] = irn;
+ DBG((dbg, LEVEL_2, "\treading arg: %d -> %+F\n", nr, irn));
}
/* Get the ABI constraints from the ISA */
arch_isa_get_call_abi(isa, method_type, call);
- {
- const arch_register_t **callee_save = env->birg->main_env->callee_save;
- int n_reg_param = 0;
+ /* Count the register params and add them to the number of projs for the RegParams node */
+ for(i = 0; i < n_params; ++i) {
+ be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
+ if(arg->in_reg) {
+ assert(arg->reg != sp && "cannot use stack pointer as parameter register");
+ pset_insert_ptr(regs, arg->reg);
+ DBG((dbg, LEVEL_2, "\targ #%d -> reg %s\n", i, arg->reg->name));
+ }
+ }
- /* Count the register params and add them to the number of projs for the RegParams node */
- for(i = 0; i < n_params; ++i) {
- be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
- n_reg_param += arg->in_reg;
+ /* Collect all callee-save registers which are not used as parameter registers. */
+ for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
+ const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
+ for(j = 0; j < cls->n_regs; ++j) {
+ const arch_register_t *reg = &cls->regs[j];
+ if(arch_register_type_is(reg, callee_save) && !pset_find_ptr(regs, reg))
+ pset_insert_ptr(callee_save, reg);
}
+ }
+
+ pset_insert_ptr(callee_save, sp);
- reg_params_bl = get_irg_start_block(irg);
- reg_params = be_new_RegParams(irg, reg_params_bl, n_reg_param);
- reg_params_nr = 0;
+ reg_params_bl = get_irg_start_block(irg);
+ reg_params = be_new_RegParams(irg, reg_params_bl, pset_count(regs) + pset_count(callee_save));
+ reg_params_nr = 0;
- proj_sp = new_r_Proj(irg, reg_params_bl, reg_params, sp->reg_class->mode, reg_params_nr);
- be_set_constr_single_reg(reg_params, -(reg_params_nr + 1), sp);
+ /*
+ * make proj nodes for the callee save registers.
+ * memorize them, since Return nodes get those as inputs.
+ */
+ for(reg = pset_first(callee_save); reg; reg = pset_next(callee_save)) {
+ ir_node *irn = new_r_Proj(irg, reg_params_bl, reg_params, reg->reg_class->mode, reg_params_nr);
+ be_set_constr_single_reg(reg_params, -(reg_params_nr + 1), reg);
+ obstack_ptr_grow(&env->obst, irn);
reg_params_nr++;
- /* Make Projs for the callee save registers. */
- for(i = 0; callee_save[i] != NULL; ++i, ++reg_params_nr) {
- const arch_register_t *reg = callee_save[i];
- ir_node *irn = new_r_Proj(irg, reg_params_bl, reg_params, reg->reg_class->mode, reg_params_nr);
- be_set_constr_single_reg(reg_params, -(reg_params_nr + 1), reg);
+ DBG((dbg, LEVEL_2, "\tcallee save proj #%d -> reg %s\n", reg_params_nr - 1, reg->name));
- /* memorize the callee save proj since they will be arguments to the Return nodes. */
- obstack_ptr_grow(&env->obst, irn);
- }
-
- obstack_ptr_grow(&env->obst, NULL);
- return_params = obstack_finish(&env->obst);
+ /* detect the proj of the stack register and memorize it. */
+ if(reg == sp)
+ proj_sp = irn;
}
+ obstack_ptr_grow(&env->obst, NULL);
+ return_params = obstack_finish(&env->obst);
+
+ assert(proj_sp != NULL && "There must be a Proj for the stack pointer");
- /* If we can omit the frame pointer, the stack pointer will become the frame pointer */
+ /* This is the stack pointer add/sub which allocates the frame. remind it for later fixup. */
env->init_sp = be_new_IncSP(sp, irg, reg_params_bl, proj_sp, 0, be_stack_dir_along);
- /* memorize this node for later fix up */
+
+ /*
+ * if we can omit the frame pointer (use it as an ordinary register), the stack pointer becomes
+ * the frame pointer, else we have to copy the current stack pointer to the frame pointer
+ */
frame_pointer = env->omit_framepointer ? env->init_sp : be_new_Copy(sp->reg_class, irg, reg_params_bl, proj_sp);
set_irg_frame(irg, frame_pointer);
- /* reroute the edges from the original argument projs to the RegParam ones. */
- for(i = 0; i < max_arg; ++i) {
- if(args[i]) {
- assert(args_repl != NULL);
- edges_reroute(args[i], args_repl[i], irg);
- }
- }
-
/* Now, introduce stack param nodes for all parameters passed on the stack */
for(i = 0; i < max_arg; ++i) {
ir_node *arg_proj = args[i];
/* when the (stack) parameter is primitive, we insert a StackParam
node representing the load of that parameter */
- else if(is_Primitive_type(param_type)) {
+ else if(is_atomic_type(param_type)) {
ir_mode *mode = get_type_mode(param_type);
const arch_register_class_t *cls = arch_isa_get_reg_class_for_mode(isa, mode);
// TODO: Correct offset computation!
}
}
+ /* reroute the edges from the original argument projs to the RegParam ones. */
+ for(i = 0; i < max_arg; ++i) {
+ if(args[i] != NULL) {
+ assert(args_repl[i] != NULL);
+ edges_reroute(args[i], args_repl[i], irg);
+ }
+ }
+
obstack_free(&env->obst, args);
be_abi_call_free(call);
+
+ del_pset(callee_save);
+ del_pset(regs);
}
static void collect_alloca_walker(ir_node *irn, void *data)
int i;
ir_node **stack_allocs;
- env->birg = birg;
- env->stack_ops = pset_new_ptr(32);
env->omit_framepointer = 1;
+ env->birg = birg;
+ env->stack_ops = pset_new_ptr(32);
+ env->dbg = firm_dbg_register("firm.be.abi");
obstack_init(&env->obst);
/* search for stack allocation nodes and record them */
void be_abi_fix_stack(be_abi_irg_t *env)
{
pset *origs = pset_new_ptr_default();
+ dom_front_info_t *df = be_compute_dominance_frontiers(env->birg->irg);
pset_insert_ptr(origs, env->init_sp);
- be_ssa_constr_sets(env->birg->dom_front, origs, env->stack_ops);
+ be_ssa_constr_sets(df, origs, env->stack_ops);
del_pset(origs);
+ be_free_dominance_frontiers(df);
}
#define DUMP_INITIAL (1 << 0)
-#define DUMP_SCHED (1 << 1)
-#define DUMP_PREPARED (1 << 2)
-#define DUMP_RA (1 << 3)
-#define DUMP_FINAL (1 << 4)
+#define DUMP_ABI (1 << 1)
+#define DUMP_SCHED (1 << 2)
+#define DUMP_PREPARED (1 << 3)
+#define DUMP_RA (1 << 4)
+#define DUMP_FINAL (1 << 5)
/* options visible for anyone */
be_options_t be_options = {
};
/* dump flags */
-static unsigned dump_flags = DUMP_INITIAL | DUMP_SCHED | DUMP_PREPARED | DUMP_RA | DUMP_FINAL;
+static unsigned dump_flags = 2 * DUMP_FINAL - 1;
/* register allocator to use. */
static const be_ra_t *ra = &be_ra_chordal_allocator;
/* back end instruction set architecture to use */
static const arch_isa_if_t *isa_if = &ia32_isa_if;
-
#ifdef WITH_LIBCORE
static lc_opt_entry_t *be_grp_root = NULL;
static const lc_opt_enum_mask_items_t dump_items[] = {
{ "none", 0 },
{ "initial", DUMP_INITIAL },
+ { "abi", DUMP_ABI },
{ "sched", DUMP_SCHED },
{ "prepared", DUMP_PREPARED },
{ "regalloc", DUMP_RA },
/* check, if the dominance property is fulfilled. */
be_check_dominance(irg);
- /* compute the dominance frontiers. */
- birg->dom_front = be_compute_dominance_frontiers(irg);
}
static void be_main_loop(FILE *file_handle)
prepare_graph(&birg);
/* implement the ABI conventions. */
- // birg.abi = be_abi_introduce(&birg);
+ birg.abi = be_abi_introduce(&birg);
+ dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
+ /* generate code */
arch_code_generator_prepare_graph(birg.cg);
/*
arch_code_generator_done(birg.cg);
dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
-
- be_free_dominance_frontiers(birg.dom_front);
}
be_done_env(&env);