+static void create_register_perms(const arch_isa_t *isa, ir_graph *irg, ir_node *bl, pmap *regs)
+{
+ int i, j, n;
+ struct obstack obst;
+
+ obstack_init(&obst);
+
+ /* Create a Perm after the RegParams node to delimit it. */
+ 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);
+ ir_node *perm;
+ ir_node **in;
+ int n_regs;
+
+ for(n_regs = 0, j = 0; j < cls->n_regs; ++j) {
+ const arch_register_t *reg = &cls->regs[j];
+ ir_node *irn = pmap_get(regs, (void *) reg);
+
+ if(irn && !arch_register_type_is(reg, ignore)) {
+ n_regs++;
+ obstack_ptr_grow(&obst, irn);
+ set_irn_link(irn, (void *) reg);
+ }
+ }
+
+ obstack_ptr_grow(&obst, NULL);
+ in = obstack_finish(&obst);
+ if(n_regs > 0) {
+ perm = be_new_Perm(cls, irg, bl, n_regs, in);
+ for(j = 0; j < n_regs; ++j) {
+ ir_node *arg = in[j];
+ arch_register_t *reg = get_irn_link(arg);
+ pmap_insert(regs, reg, arg);
+ be_set_constr_single_reg(perm, BE_OUT_POS(j), reg);
+ }
+ }
+ obstack_free(&obst, in);
+ }
+
+ obstack_free(&obst, NULL);
+}
+
+typedef struct {
+ const arch_register_t *reg;
+ ir_node *irn;
+} reg_node_map_t;
+
+static int cmp_regs(const void *a, const void *b)
+{
+ const reg_node_map_t *p = a;
+ const reg_node_map_t *q = b;
+
+ if(p->reg->reg_class == q->reg->reg_class)
+ return p->reg->index - q->reg->index;
+ else
+ return p->reg->reg_class - q->reg->reg_class;
+}
+
+static reg_node_map_t *reg_map_to_arr(struct obstack *obst, pmap *reg_map)
+{
+ pmap_entry *ent;
+ int n = pmap_count(reg_map);
+ int i = 0;
+ reg_node_map_t *res = obstack_alloc(obst, n * sizeof(res[0]));
+
+ pmap_foreach(reg_map, ent) {
+ res[i].reg = ent->key;
+ res[i].irn = ent->value;
+ i++;
+ }
+
+ qsort(res, n, sizeof(res[0]), cmp_regs);
+ return res;
+}
+
+/**
+ * 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) {
+ ir_node *res = get_Return_res(irn, 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;
+}
+