+ if(reg == sp)
+ obstack_ptr_grow(&env->obst, stack);
+ else if(reg == bp)
+ obstack_ptr_grow(&env->obst, frame);
+ else if(arch_register_type_is(reg, callee_save) || arch_register_type_is(reg, ignore))
+ obstack_ptr_grow(obst, irn);
+ }
+}
+*/
+#endif
+
+/**
+ * Computes the stack argument layout type.
+ * Changes a possibly allocated value param type by moving
+ * entities to the stack layout type.
+ *
+ * @param env the ABI environment
+ * @param call the current call ABI
+ * @param method_type the method type
+ *
+ * @return the stack argument layout type
+ */
+static ir_type *compute_arg_type(be_abi_irg_t *env, be_abi_call_t *call, ir_type *method_type)
+{
+ int dir = env->call->flags.bits.left_to_right ? 1 : -1;
+ int inc = env->birg->main_env->arch_env->isa->stack_dir * dir;
+ int n = get_method_n_params(method_type);
+ int curr = inc > 0 ? 0 : n - 1;
+ int ofs = 0;
+
+ char buf[128];
+ ir_type *res;
+ int i;
+ ir_type *val_param_tp = get_method_value_param_type(method_type);
+ ident *id = get_entity_ident(get_irg_entity(env->birg->irg));
+
+ res = new_type_struct(mangle_u(id, new_id_from_chars("arg_type", 8)));
+ for (i = 0; i < n; ++i, curr += inc) {
+ ir_type *param_type = get_method_param_type(method_type, curr);
+ be_abi_call_arg_t *arg = get_call_arg(call, 0, curr);
+
+ if (arg->on_stack) {
+ if (val_param_tp) {
+ /* the entity was already created, move it to the param type */
+ arg->stack_ent = get_method_value_param_ent(method_type, i);
+ remove_struct_member(val_param_tp, arg->stack_ent);
+ set_entity_owner(arg->stack_ent, res);
+ add_struct_member(res, arg->stack_ent);
+ /* must be automatic to set a fixed layout */
+ set_entity_allocation(arg->stack_ent, allocation_automatic);
+ }
+ else {
+ snprintf(buf, sizeof(buf), "param_%d", i);
+ arg->stack_ent = new_entity(res, new_id_from_str(buf), param_type);
+ }
+ ofs += arg->space_before;
+ ofs = round_up2(ofs, arg->alignment);
+ set_entity_offset_bytes(arg->stack_ent, ofs);
+ ofs += arg->space_after;
+ ofs += get_type_size_bytes(param_type);
+ }
+ }
+ set_type_size_bytes(res, ofs);
+ set_type_state(res, layout_fixed);
+ return res;
+}
+
+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) {
+ 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;
+ entity *value_param_list; /**< the list of all value param antities */
+} 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;
+ 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);
+
+ if (ptr == param_base) {
+ 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, entity *value_param_list) {
+ be_abi_call_t *call = env->call;
+ ir_graph *irg = env->birg->irg;
+ 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_bytes(ent, offset);
+ offset += get_type_size_bytes(tp);
+ }
+ set_type_size_bytes(frame_tp, offset);
+ }