+/**
+ * Creates a barrier.
+ */
+static ir_node *create_barrier(be_abi_irg_t *env, ir_node *bl, ir_node **mem, pmap *regs, int in_req)
+{
+ ir_graph *irg = env->birg->irg;
+ int n_regs = pmap_count(regs);
+ int n;
+ ir_node *irn;
+ ir_node **in;
+ reg_node_map_t *rm;
+
+ rm = reg_map_to_arr(&env->obst, regs);
+
+ for(n = 0; n < n_regs; ++n)
+ obstack_ptr_grow(&env->obst, rm[n].irn);
+
+ if(mem) {
+ obstack_ptr_grow(&env->obst, *mem);
+ n++;
+ }
+
+ in = (ir_node **) obstack_finish(&env->obst);
+ irn = be_new_Barrier(irg, bl, n, in);
+ obstack_free(&env->obst, in);
+
+ for(n = 0; n < n_regs; ++n) {
+ const arch_register_t *reg = rm[n].reg;
+ int flags = 0;
+ int pos = BE_OUT_POS(n);
+ ir_node *proj;
+
+ proj = new_r_Proj(irg, bl, irn, get_irn_mode(rm[n].irn), n);
+ be_node_set_reg_class(irn, n, reg->reg_class);
+ if(in_req)
+ be_set_constr_single_reg(irn, n, reg);
+ be_set_constr_single_reg(irn, pos, reg);
+ be_node_set_reg_class(irn, pos, reg->reg_class);
+ arch_set_irn_register(env->birg->main_env->arch_env, proj, reg);
+
+ /* if the proj projects a ignore register or a node which is set to ignore, propagate this property. */
+ if(arch_register_type_is(reg, ignore) || arch_irn_is(env->birg->main_env->arch_env, in[n], ignore))
+ flags |= arch_irn_flags_ignore;
+
+ if(arch_irn_is(env->birg->main_env->arch_env, in[n], modify_sp))
+ flags |= arch_irn_flags_modify_sp;
+
+ be_node_set_flags(irn, pos, flags);
+
+ pmap_insert(regs, (void *) reg, proj);
+ }
+
+ if(mem) {
+ *mem = new_r_Proj(irg, bl, irn, mode_M, n);
+ }
+
+ obstack_free(&env->obst, rm);
+ return irn;
+}
+
+/**
+ * Creates a be_Return for a Return node.
+ *
+ * @param @env the abi environment
+ * @param irn the Return node or NULL if there was none
+ * @param bl the block where the be_Retun should be placed
+ * @param mem the current memory
+ * @param n_res number of return results
+ */
+static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl, ir_node *mem, int n_res) {
+ be_abi_call_t *call = env->call;
+ const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
+
+ pmap *reg_map = pmap_create();
+ ir_node *keep = pmap_get(env->keep_map, bl);
+ int in_max;
+ ir_node *ret;
+ int i, n;
+ ir_node **in;
+ ir_node *stack;
+ const arch_register_t **regs;
+ pmap_entry *ent ;
+
+ /*
+ get the valid stack node in this block.
+ If we had a call in that block there is a Keep constructed by process_calls()
+ which points to the last stack modification in that block. we'll use
+ it then. Else we use the stack from the start block and let
+ the ssa construction fix the usage.
+ */
+ stack = be_abi_reg_map_get(env->regs, isa->sp);
+ if (keep) {
+ ir_node *bad = new_r_Bad(env->birg->irg);
+ stack = get_irn_n(keep, 0);
+ set_nodes_block(keep, bad);
+ set_irn_n(keep, 0, bad);
+ // exchange(keep, new_r_Bad(env->birg->irg));
+ }
+
+ /* Insert results for Return into the register map. */
+ for(i = 0; i < n_res; ++i) {
+ ir_node *res = get_Return_res(irn, i);
+ be_abi_call_arg_t *arg = get_call_arg(call, 1, i);
+ assert(arg->in_reg && "return value must be passed in register");
+ pmap_insert(reg_map, (void *) arg->reg, res);
+ }
+
+ /* Add uses of the callee save registers. */
+ pmap_foreach(env->regs, ent) {
+ const arch_register_t *reg = ent->key;
+ if(arch_register_type_is(reg, callee_save) || arch_register_type_is(reg, ignore))
+ pmap_insert(reg_map, ent->key, ent->value);
+ }
+
+ be_abi_reg_map_set(reg_map, isa->sp, stack);
+
+ /* Make the Epilogue node and call the arch's epilogue maker. */
+ create_barrier(env, bl, &mem, reg_map, 1);
+ call->cb->epilogue(env->cb, bl, &mem, reg_map);
+
+ /*
+ Maximum size of the in array for Return nodes is
+ return args + callee save/ignore registers + memory + stack pointer
+ */
+ in_max = pmap_count(reg_map) + n_res + 2;
+
+ in = obstack_alloc(&env->obst, in_max * sizeof(in[0]));
+ regs = obstack_alloc(&env->obst, in_max * sizeof(regs[0]));
+
+ in[0] = mem;
+ in[1] = be_abi_reg_map_get(reg_map, isa->sp);
+ regs[0] = NULL;
+ regs[1] = isa->sp;
+ n = 2;
+
+ /* clear SP entry, since it has already been grown. */
+ pmap_insert(reg_map, (void *) isa->sp, NULL);
+ for(i = 0; i < n_res; ++i) {
+ be_abi_call_arg_t *arg = get_call_arg(call, 1, i);
+
+ in[n] = be_abi_reg_map_get(reg_map, arg->reg);
+ regs[n++] = arg->reg;
+
+ /* Clear the map entry to mark the register as processed. */
+ be_abi_reg_map_set(reg_map, arg->reg, NULL);
+ }
+
+ /* grow the rest of the stuff. */
+ pmap_foreach(reg_map, ent) {
+ if(ent->value) {
+ in[n] = ent->value;
+ regs[n++] = ent->key;
+ }
+ }
+
+ /* The in array for the new back end return is now ready. */
+ ret = be_new_Return(irn ? get_irn_dbg_info(irn) : NULL, env->birg->irg, bl, n_res, n, in);
+
+ /* Set the register classes of the return's parameter accordingly. */
+ for(i = 0; i < n; ++i)
+ if(regs[i])
+ be_node_set_reg_class(ret, i, regs[i]->reg_class);
+
+ /* Free the space of the Epilog's in array and the register <-> proj map. */
+ obstack_free(&env->obst, in);
+ pmap_destroy(reg_map);
+
+ return ret;
+}
+
+typedef struct lower_frame_sels_env_t {
+ be_abi_irg_t *env;
+ ir_entity *value_param_list; /**< the list of all value param entities */
+} lower_frame_sels_env_t;
+
+/**
+ * Walker: Replaces Sels of frame type and
+ * value param type entities by FrameAddress.
+ */
+static void lower_frame_sels_walker(ir_node *irn, void *data)
+{
+ lower_frame_sels_env_t *ctx = data;
+
+ if (is_Sel(irn)) {
+ ir_graph *irg = current_ir_graph;
+ ir_node *frame = get_irg_frame(irg);
+ ir_node *param_base = get_irg_value_param_base(irg);
+ ir_node *ptr = get_Sel_ptr(irn);
+
+ if (ptr == frame || ptr == param_base) {
+ be_abi_irg_t *env = ctx->env;
+ ir_entity *ent = get_Sel_entity(irn);
+ ir_node *bl = get_nodes_block(irn);
+ ir_node *nw;
+
+ nw = be_new_FrameAddr(env->isa->sp->reg_class, irg, bl, frame, ent);
+ exchange(irn, nw);
+
+ /* check, if it's a param sel and if have not seen this entity immediatly before */
+ if (ptr == param_base && ctx->value_param_list != ent) {
+ set_entity_link(ent, ctx->value_param_list);
+ ctx->value_param_list = ent;
+ }
+ }
+ }
+}
+
+/**
+ * Check if a value parameter is transmitted as a register.
+ * This might happen if the address of an parameter is taken which is
+ * transmitted in registers.
+ *
+ * Note that on some architectures this case must be handled specially
+ * because the place of the backing store is determined by their ABI.
+ *
+ * In the default case we move the entity to the frame type and create
+ * a backing store into the first block.
+ */
+static void fix_address_of_parameter_access(be_abi_irg_t *env, ir_entity *value_param_list) {
+ be_abi_call_t *call = env->call;
+ ir_graph *irg = env->birg->irg;
+ ir_entity *ent, *next_ent, *new_list;
+ ir_type *frame_tp;
+ DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
+
+ new_list = NULL;
+ for (ent = value_param_list; ent; ent = next_ent) {
+ int i = get_struct_member_index(get_entity_owner(ent), ent);
+ be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
+
+ next_ent = get_entity_link(ent);
+ if (arg->in_reg) {
+ DBG((dbg, LEVEL_2, "\targ #%d need backing store\n", i));
+ set_entity_link(ent, new_list);
+ new_list = ent;
+ }
+ }
+ if (new_list) {
+ /* ok, change the graph */
+ ir_node *start_bl = get_irg_start_block(irg);
+ ir_node *first_bl = NULL;
+ ir_node *frame, *imem, *nmem, *store, *mem, *args, *args_bl;
+ const ir_edge_t *edge;
+ optimization_state_t state;
+ int offset;
+
+ foreach_block_succ(start_bl, edge) {
+ ir_node *succ = get_edge_src_irn(edge);
+ if (start_bl != succ) {
+ first_bl = succ;
+ break;
+ }
+ }
+ assert(first_bl);
+ /* we had already removed critical edges, so the following
+ assertion should be always true. */
+ assert(get_Block_n_cfgpreds(first_bl) == 1);
+
+ /* now create backing stores */
+ frame = get_irg_frame(irg);
+ imem = get_irg_initial_mem(irg);
+
+ save_optimization_state(&state);
+ set_optimize(0);
+ nmem = new_r_Proj(irg, first_bl, get_irg_start(irg), mode_M, pn_Start_M);
+ restore_optimization_state(&state);
+
+ /* reroute all edges to the new memory source */
+ edges_reroute(imem, nmem, irg);
+
+ store = NULL;
+ mem = imem;
+ args = get_irg_args(irg);
+ args_bl = get_nodes_block(args);
+ for (ent = new_list; ent; ent = get_entity_link(ent)) {
+ int i = get_struct_member_index(get_entity_owner(ent), ent);
+ ir_type *tp = get_entity_type(ent);
+ ir_mode *mode = get_type_mode(tp);
+ ir_node *addr;
+
+ /* address for the backing store */
+ addr = be_new_FrameAddr(env->isa->sp->reg_class, irg, first_bl, frame, ent);
+
+ if (store)
+ mem = new_r_Proj(irg, first_bl, store, mode_M, pn_Store_M);
+
+ /* the backing store itself */
+ store = new_r_Store(irg, first_bl, mem, addr,
+ new_r_Proj(irg, args_bl, args, mode, i));
+ }
+ /* the new memory Proj gets the last Proj from store */
+ set_Proj_pred(nmem, store);
+ set_Proj_proj(nmem, pn_Store_M);
+
+ /* move all entities to the frame type */
+ frame_tp = get_irg_frame_type(irg);
+ offset = get_type_size_bytes(frame_tp);
+ for (ent = new_list; ent; ent = get_entity_link(ent)) {
+ ir_type *tp = get_entity_type(ent);
+ int align = get_type_alignment_bytes(tp);
+
+ offset += align - 1;
+ offset &= -align;
+ set_entity_owner(ent, frame_tp);
+ add_class_member(frame_tp, ent);
+ /* must be automatic to set a fixed layout */
+ set_entity_allocation(ent, allocation_automatic);
+ set_entity_offset(ent, offset);
+ offset += get_type_size_bytes(tp);
+ }
+ set_type_size_bytes(frame_tp, offset);
+ }
+}
+
+/**
+ * Modify the irg itself and the frame type.
+ */
+static void modify_irg(be_abi_irg_t *env)
+{
+ be_abi_call_t *call = env->call;
+ const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
+ const arch_register_t *sp = arch_isa_sp(isa);
+ ir_graph *irg = env->birg->irg;
+ ir_node *bl = get_irg_start_block(irg);
+ ir_node *end = get_irg_end_block(irg);
+ ir_node *mem = get_irg_initial_mem(irg);
+ ir_type *method_type = get_entity_type(get_irg_entity(irg));
+ pset *dont_save = pset_new_ptr(8);
+
+ int n_params;
+ int i, j, n;
+
+ reg_node_map_t *rm;
+ const arch_register_t *fp_reg;
+ ir_node *frame_pointer;
+ ir_node *barrier;
+ ir_node *reg_params_bl;
+ ir_node **args;
+ ir_node *arg_tuple;
+ const ir_edge_t *edge;
+ ir_type *arg_type, *bet_type;
+ lower_frame_sels_env_t ctx;
+ ir_entity **param_map;
+
+ bitset_t *used_proj_nr;
+ DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
+
+ DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
+
+ /* Convert the Sel nodes in the irg to frame load/store/addr nodes. */
+ ctx.env = env;
+ ctx.value_param_list = NULL;
+ irg_walk_graph(irg, lower_frame_sels_walker, NULL, &ctx);
+
+ env->frame = obstack_alloc(&env->obst, sizeof(env->frame[0]));
+ env->regs = pmap_create();
+
+ used_proj_nr = bitset_alloca(1024);
+ n_params = get_method_n_params(method_type);
+ args = obstack_alloc(&env->obst, n_params * sizeof(args[0]));
+ memset(args, 0, n_params * sizeof(args[0]));
+
+ /* Check if a value parameter is transmitted as a register.
+ * This might happen if the address of an parameter is taken which is
+ * transmitted in registers.
+ *
+ * Note that on some architectures this case must be handled specially
+ * because the place of the backing store is determined by their ABI.
+ *
+ * In the default case we move the entity to the frame type and create
+ * a backing store into the first block.
+ */
+ fix_address_of_parameter_access(env, ctx.value_param_list);
+
+ /* Fill the argument vector */
+ arg_tuple = get_irg_args(irg);
+ 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));
+ }
+
+ arg_type = compute_arg_type(env, call, method_type, ¶m_map);
+ bet_type = call->cb->get_between_type(env->cb);
+ stack_frame_init(env->frame, arg_type, bet_type, get_irg_frame_type(irg), isa->stack_dir, param_map);
+
+ /* 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 && args[i]) {
+ assert(arg->reg != sp && "cannot use stack pointer as parameter register");
+ assert(i == get_Proj_proj(args[i]));
+
+ /* For now, associate the register with the old Proj from Start representing that argument. */
+ pmap_insert(env->regs, (void *) arg->reg, args[i]);
+ bitset_set(used_proj_nr, i);
+ DBG((dbg, LEVEL_2, "\targ #%d -> reg %s\n", i, arg->reg->name));
+ }
+ }
+
+ /* Collect all callee-save 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) || arch_register_type_is(reg, ignore))
+ pmap_insert(env->regs, (void *) reg, NULL);
+ }
+ }
+
+ pmap_insert(env->regs, (void *) sp, NULL);
+ pmap_insert(env->regs, (void *) isa->bp, NULL);
+ reg_params_bl = get_irg_start_block(irg);
+ env->reg_params = be_new_RegParams(irg, reg_params_bl, pmap_count(env->regs));
+ add_irn_dep(env->reg_params, get_irg_start(irg));
+
+ /*
+ * make proj nodes for the callee save registers.
+ * memorize them, since Return nodes get those as inputs.
+ *
+ * Note, that if a register corresponds to an argument, the regs map contains
+ * the old Proj from start for that argument.
+ */
+
+ rm = reg_map_to_arr(&env->obst, env->regs);
+ for(i = 0, n = pmap_count(env->regs); i < n; ++i) {
+ arch_register_t *reg = (void *) rm[i].reg;
+ ir_node *arg_proj = rm[i].irn;
+ ir_mode *mode = arg_proj ? get_irn_mode(arg_proj) : reg->reg_class->mode;
+ long nr = i;
+ int pos = BE_OUT_POS((int) nr);
+ int flags = 0;
+
+ ir_node *proj;
+
+ assert(nr >= 0);
+ bitset_set(used_proj_nr, nr);
+ proj = new_r_Proj(irg, reg_params_bl, env->reg_params, mode, nr);
+ pmap_insert(env->regs, (void *) reg, proj);
+ be_set_constr_single_reg(env->reg_params, pos, reg);
+ arch_set_irn_register(env->birg->main_env->arch_env, proj, reg);
+
+ /*
+ * If the register is an ignore register,
+ * The Proj for that register shall also be ignored during register allocation.
+ */
+ if(arch_register_type_is(reg, ignore))
+ flags |= arch_irn_flags_ignore;
+
+ if(reg == sp)
+ flags |= arch_irn_flags_modify_sp;
+
+ be_node_set_flags(env->reg_params, pos, flags);
+
+ DBG((dbg, LEVEL_2, "\tregister save proj #%d -> reg %s\n", nr, reg->name));
+ }
+ obstack_free(&env->obst, rm);
+
+ /* Generate the Prologue */
+ fp_reg = call->cb->prologue(env->cb, &mem, env->regs);
+
+ /* do the stack allocation BEFORE the barrier, or spill code
+ might be added before it */
+ env->init_sp = be_abi_reg_map_get(env->regs, sp);
+ env->init_sp = be_new_IncSP(sp, irg, bl, env->init_sp, BE_STACK_FRAME_SIZE_EXPAND);
+ be_abi_reg_map_set(env->regs, sp, env->init_sp);
+
+ env->start_barrier = barrier = create_barrier(env, bl, &mem, env->regs, 0);
+
+ env->init_sp = be_abi_reg_map_get(env->regs, sp);
+ arch_set_irn_register(env->birg->main_env->arch_env, env->init_sp, sp);
+
+ frame_pointer = be_abi_reg_map_get(env->regs, fp_reg);
+ set_irg_frame(irg, frame_pointer);
+ pset_insert_ptr(env->ignore_regs, fp_reg);
+
+ /* Now, introduce stack param nodes for all parameters passed on the stack */
+ for(i = 0; i < n_params; ++i) {
+ ir_node *arg_proj = args[i];
+ ir_node *repl = NULL;
+
+ if(arg_proj != NULL) {
+ be_abi_call_arg_t *arg;
+ ir_type *param_type;
+ int nr = get_Proj_proj(arg_proj);
+
+ nr = MIN(nr, n_params);
+ arg = get_call_arg(call, 0, nr);
+ param_type = get_method_param_type(method_type, nr);
+
+ if(arg->in_reg) {
+ repl = pmap_get(env->regs, (void *) arg->reg);
+ }
+
+ else if(arg->on_stack) {
+ /* For atomic parameters which are actually used, we create a StackParam node. */
+ if(is_atomic_type(param_type) && get_irn_n_edges(args[i]) > 0) {
+ ir_mode *mode = get_type_mode(param_type);
+ const arch_register_class_t *cls = arch_isa_get_reg_class_for_mode(isa, mode);
+ repl = be_new_StackParam(cls, isa->bp->reg_class, irg, reg_params_bl, mode, frame_pointer, arg->stack_ent);
+ }
+
+ /* The stack parameter is not primitive (it is a struct or array),
+ we thus will create a node representing the parameter's address
+ on the stack. */
+ else {
+ repl = be_new_FrameAddr(sp->reg_class, irg, reg_params_bl, frame_pointer, arg->stack_ent);
+ }
+ }
+
+ assert(repl != NULL);
+ edges_reroute(args[i], repl, irg);
+ }
+ }
+
+ /* All Return nodes hang on the End node, so look for them there. */
+ for (i = 0, n = get_Block_n_cfgpreds(end); i < n; ++i) {
+ ir_node *irn = get_Block_cfgpred(end, i);
+
+ if (is_Return(irn)) {
+ ir_node *ret = create_be_return(env, irn, get_nodes_block(irn), get_Return_mem(irn), get_Return_n_ress(irn));
+ exchange(irn, ret);
+ }
+ }
+ /* if we have endless loops here, n might be <= 0. Do NOT create a be_Return than,
+ the code is dead and will never be executed. */
+
+ del_pset(dont_save);
+ obstack_free(&env->obst, args);
+}
+
+be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
+{
+ be_abi_irg_t *env = xmalloc(sizeof(env[0]));
+ ir_node *old_frame = get_irg_frame(birg->irg);
+ ir_graph *irg = birg->irg;
+
+ pmap_entry *ent;
+ ir_node *dummy;
+ optimization_state_t state;
+
+ be_omit_fp = birg->main_env->options->omit_fp;
+
+ obstack_init(&env->obst);
+
+ env->isa = birg->main_env->arch_env->isa;
+ env->method_type = get_entity_type(get_irg_entity(irg));
+ env->call = be_abi_call_new();
+ arch_isa_get_call_abi(env->isa, env->method_type, env->call);
+
+ env->ignore_regs = pset_new_ptr_default();
+ env->keep_map = pmap_create();
+ env->dce_survivor = new_survive_dce();
+ env->birg = birg;
+ env->stack_phis = pset_new_ptr(16);
+ /* Beware: later we replace this node by the real one, ensure it is not CSE'd
+ to another Unknown or the stack pointer gets used */
+ save_optimization_state(&state);
+ set_optimize(0);
+ env->init_sp = dummy = new_r_Unknown(irg, env->isa->sp->reg_class->mode);
+ restore_optimization_state(&state);
+ FIRM_DBG_REGISTER(env->dbg, "firm.be.abi");
+
+ memcpy(&env->irn_handler, &abi_irn_handler, sizeof(abi_irn_handler));
+ env->irn_ops.impl = &abi_irn_ops;
+
+ /* Lower all call nodes in the IRG. */
+ process_calls(env);
+
+ /*
+ Beware: init backend abi call object after processing calls,
+ otherwise some information might be not yet available.
+ */
+ env->cb = env->call->cb->init(env->call, birg->main_env->arch_env, irg);
+
+ /* Process the IRG */
+ modify_irg(env);
+
+ /* We don't need the keep map anymore. */
+ pmap_destroy(env->keep_map);
+
+ /* reroute the stack origin of the calls to the true stack origin. */
+ edges_reroute(dummy, env->init_sp, irg);
+ edges_reroute(old_frame, get_irg_frame(irg), irg);
+
+ /* Make some important node pointers survive the dead node elimination. */
+ survive_dce_register_irn(env->dce_survivor, &env->init_sp);
+ pmap_foreach(env->regs, ent)
+ survive_dce_register_irn(env->dce_survivor, (ir_node **) &ent->value);
+
+ arch_env_push_irn_handler(env->birg->main_env->arch_env, &env->irn_handler);
+
+ env->call->cb->done(env->cb);
+ env->cb = NULL;
+ return env;
+}