Bugfixes
[libfirm] / ir / be / beabi.c
1 /**
2  * ABI lowering.
3  *
4  *
5  *
6  */
7
8 #include "firm_config.h"
9 #include "obst.h"
10
11 #include "type.h"
12
13 #include "irgraph_t.h"
14 #include "irnode_t.h"
15 #include "ircons_t.h"
16 #include "iredges_t.h"
17 #include "irgmod.h"
18 #include "irgwalk.h"
19
20 #include "be.h"
21 #include "beabi.h"
22 #include "bearch.h"
23 #include "benode_t.h"
24 #include "besched_t.h"
25
26 #define MAX(x, y) ((x) > (y) ? (x) : (y))
27 #define MIN(x, y) ((x) < (y) ? (x) : (y))
28
29 typedef struct _be_abi_call_arg_t {
30         unsigned is_res : 1;
31         unsigned in_reg : 1;
32
33         int pos;
34         const arch_register_t *reg;
35 } be_abi_call_arg_t;
36
37 struct _be_abi_call_t {
38         be_abi_call_flags_t flags;
39         unsigned arg_gap;
40         set *params;
41 };
42
43 struct _be_abi_irg_t {
44         struct obstack      obst;
45         be_irg_t            *birg;
46         be_abi_call_t       *call;
47         type                *method_type;
48
49         ir_node             *init_sp;      /**< The node representing the stack pointer
50                                                                              at the start of the function. */
51
52         ir_node             *reg_params;
53
54         pset                *stack_ops;    /**< Contains all nodes modifying the stack pointer. */
55         pmap                *regs;
56
57         int start_block_bias;
58
59         unsigned omit_fp : 1;
60         unsigned dedicated_fp : 1;
61         unsigned left_to_right : 1;
62
63         firm_dbg_module_t *dbg;            /**< The debugging module. */
64 };
65
66 static int cmp_call_arg(const void *a, const void *b, size_t n)
67 {
68         const be_abi_call_arg_t *p = a, *q = b;
69         return !(p->is_res == q->is_res && p->pos == q->pos);
70 }
71
72 static be_abi_call_arg_t *get_or_set_call_arg(be_abi_call_t *call, int is_res, int pos, int do_insert)
73 {
74         be_abi_call_arg_t arg;
75         unsigned hash;
76
77         arg.is_res = is_res;
78         arg.pos    = pos;
79
80         hash = is_res * 100 + pos;
81
82         return do_insert
83                 ? set_insert(call->params, &arg, sizeof(arg), hash)
84                 : set_find(call->params, &arg, sizeof(arg), hash);
85 }
86
87 static INLINE be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos)
88 {
89         return get_or_set_call_arg(call, is_res, pos, 0);
90 }
91
92 void be_abi_call_set_flags(be_abi_call_t *call, be_abi_call_flags_t flags, unsigned arg_gap)
93 {
94         call->flags   = flags;
95         call->arg_gap = arg_gap;
96 }
97
98 void be_abi_call_param_stack(be_abi_call_t *call, int arg_pos)
99 {
100         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
101 }
102
103 void be_abi_call_param_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
104 {
105         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
106         arg->reg = reg;
107 }
108
109 void be_abi_call_res_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
110 {
111         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 1, arg_pos, 1);
112         arg->reg = reg;
113 }
114
115 be_abi_call_t *be_abi_call_new(void)
116 {
117         be_abi_call_t *call = malloc(sizeof(call[0]));
118         call->flags  = BE_ABI_NONE;
119         call->params = new_set(cmp_call_arg, 16);
120         return call;
121 }
122
123 void be_abi_call_free(be_abi_call_t *call)
124 {
125         del_set(call->params);
126         free(call);
127 }
128
129 static INLINE int is_on_stack(be_abi_call_t *call, int pos)
130 {
131         be_abi_call_arg_t *arg = get_call_arg(call, 0, pos);
132         return arg && !arg->in_reg;
133 }
134
135 static void adjust_call(be_abi_irg_t *env, ir_node *irn)
136 {
137         ir_graph *irg             = env->birg->irg;
138         const arch_isa_t *isa     = env->birg->main_env->arch_env->isa;
139         be_abi_call_t *call       = be_abi_call_new();
140         ir_type *mt               = get_Call_type(irn);
141         int n_params              = get_method_n_params(mt);
142         ir_node *curr_sp          = get_irg_frame(irg);
143         ir_node *curr_mem         = get_Call_mem(irn);
144         ir_node *bl               = get_nodes_block(irn);
145         pset *results             = pset_new_ptr(8);
146         pset *caller_save         = pset_new_ptr(8);
147         int stack_size            = 0;
148         int stack_dir             = arch_isa_stack_dir(isa);
149         const arch_register_t *sp = arch_isa_sp(isa);
150         ir_mode *mach_mode        = sp->reg_class->mode;
151         struct obstack *obst      = &env->obst;
152         ir_node *no_mem           = get_irg_no_mem(irg);
153
154         ir_node *res_proj = NULL;
155         int curr_res_proj = -1;
156         int n_low_args    = 0;
157         int n_pos         = 0;
158
159         ir_node *low_call;
160         ir_node **in;
161         ir_node *sp_proj;
162         const ir_edge_t *edge;
163         int *low_args;
164         int *pos;
165         int i, n;
166
167         /* Let the isa fill out the abi description for that call node. */
168         arch_isa_get_call_abi(isa, mt, call);
169
170         assert(get_method_variadicity(mt) == variadicity_non_variadic);
171
172         /* Insert code to put the stack arguments on the stack. */
173         /* TODO: Vargargs */
174         for(i = 0, n = get_Call_n_params(irn); i < n; ++i) {
175                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
176                 if(arg && !arg->in_reg) {
177                         stack_size += get_type_size_bytes(get_method_param_type(mt, i));
178                         obstack_int_grow(obst, i);
179                         n_pos++;
180                 }
181         }
182         pos = obstack_finish(obst);
183
184         /* Collect all arguments which are passed in registers. */
185         for(i = 0, n = get_Call_n_params(irn); i < n; ++i) {
186                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
187                 if(arg && arg->in_reg) {
188                         obstack_int_grow(obst, i);
189                         n_low_args++;
190                 }
191         }
192         low_args = obstack_finish(obst);
193
194         /* If there are some parameters shich shall be passed on the stack. */
195         if(n_pos > 0) {
196                 int curr_ofs      = 0;
197                 int do_seq        = (call->flags & BE_ABI_USE_PUSH);
198
199                 /* Reverse list of stack parameters if call arguments are from left to right */
200                 if(call->flags & BE_ABI_LEFT_TO_RIGHT) {
201                         for(i = 0; i < n_pos / 2; ++i) {
202                                 int other  = n_pos - i - 1;
203                                 int tmp    = pos[i];
204                                 pos[i]     = pos[other];
205                                 pos[other] = tmp;
206                         }
207                 }
208
209                 /*
210                  * If the stack is decreasing and we do not want to store sequentially,
211                  * we allocate as much space on the stack all parameters need, by
212                  * moving the stack pointer along the stack's direction.
213                  */
214                 if(stack_dir < 0 && !do_seq) {
215                         curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, no_mem, stack_size, be_stack_dir_along);
216                         pset_insert_ptr(env->stack_ops, curr_sp);
217                 }
218
219                 assert(mode_is_reference(mach_mode) && "machine mode must be pointer");
220                 for(i = 0; i < n_pos; ++i) {
221                         int p            = pos[i];
222                         ir_node *param   = get_Call_param(irn, p);
223                         ir_node *addr    = curr_sp;
224                         ir_node *mem     = NULL;
225                         type *param_type = get_method_param_type(mt, p);
226                         int param_size   = get_type_size_bytes(param_type);
227
228                         /* Make the expression to compute the argument's offset. */
229                         if(curr_ofs > 0) {
230                                 addr = new_r_Const_long(irg, bl, mode_Is, curr_ofs);
231                                 addr = new_r_Add(irg, bl, curr_sp, addr, mach_mode);
232                         }
233
234                         /* Insert a store for primitive arguments. */
235                         if(is_atomic_type(param_type)) {
236                                 mem = new_r_Store(irg, bl, curr_mem, addr, param);
237                                 mem = new_r_Proj(irg, bl, mem, mode_M, pn_Store_M);
238                         }
239
240                         /* Make a memcopy for compound arguments. */
241                         else {
242                                 assert(mode_is_reference(get_irn_mode(param)));
243                                 mem = new_r_CopyB(irg, bl, curr_mem, addr, param, param_type);
244                                 mem = new_r_Proj(irg, bl, mem, mode_M, pn_CopyB_M_regular);
245                         }
246
247                         obstack_ptr_grow(obst, mem);
248
249                         curr_ofs += param_size;
250
251                         /*
252                         * If we wanted to build the arguments sequentially,
253                         * the stack pointer for the next must be incremented,
254                         * and the memory value propagated.
255                         */
256                         if(do_seq) {
257                                 curr_ofs = 0;
258                                 curr_sp  = be_new_IncSP(sp, irg, bl, curr_sp, no_mem, param_size, be_stack_dir_along);
259                                 curr_mem = mem;
260
261                                 /*
262                                  * only put the first IncSP to the stack fixup set since the other
263                                  * ones are correctly connected to other nodes and do not need
264                                  * to be fixed.
265                                  */
266                                 if(i == 0)
267                                         pset_insert_ptr(env->stack_ops, curr_sp);
268                         }
269                 }
270
271                 in = (ir_node **) obstack_finish(obst);
272
273                 /* We need the sync only, if we didn't build the stores sequentially. */
274                 if(!do_seq)
275                         curr_mem = new_r_Sync(irg, bl, n_pos, in);
276                 obstack_free(obst, in);
277         }
278
279         /* Collect caller save registers */
280         for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
281                 int j;
282                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
283                 for(j = 0; j < cls->n_regs; ++j) {
284                         const arch_register_t *reg = arch_register_for_index(cls, j);
285                         if(arch_register_type_is(reg, caller_save))
286                                 pset_insert_ptr(caller_save, (void *) reg);
287                 }
288         }
289
290         /* search the greatest result proj number */
291         foreach_out_edge(irn, edge) {
292                 const ir_edge_t *res_edge;
293                 ir_node *irn = get_edge_src_irn(edge);
294
295                 if(is_Proj(irn) && get_irn_mode(irn) == mode_T) {
296                         res_proj = irn;
297                         foreach_out_edge(irn, res_edge) {
298                                 int proj;
299                                 be_abi_call_arg_t *arg;
300                                 ir_node *res = get_edge_src_irn(res_edge);
301
302                                 assert(is_Proj(res));
303                                 proj = get_Proj_proj(res);
304                                 arg = get_call_arg(call, 1, proj);
305                                 if(proj > curr_res_proj)
306                                         curr_res_proj = proj;
307                                 if(arg->in_reg)
308                                         pset_remove_ptr(caller_save, arg->reg);
309                         }
310                 }
311         }
312         curr_res_proj++;
313
314         /* Make additional projs for the caller save registers
315            and the Keep node which keeps them alive. */
316         if(pset_count(caller_save) > 0) {
317                 const arch_register_t *reg;
318                 ir_node **in;
319
320                 if(!res_proj)
321                         res_proj = new_r_Proj(irg, bl, irn, mode_T, pn_Call_T_result);
322
323                 for(reg = pset_first(caller_save); reg; reg = pset_next(caller_save))
324                         obstack_ptr_grow(obst, new_r_Proj(irg, bl, res_proj, reg->reg_class->mode, curr_res_proj++));
325
326                 in = (ir_node **) obstack_finish(obst);
327                 be_new_Keep(NULL, irg, bl, pset_count(caller_save), in);
328                 obstack_free(obst, in);
329         }
330
331         /* Clean up the stack. */
332         if(stack_size > 0) {
333                 ir_node *last_inc_sp;
334
335                 /* Get the result ProjT */
336                 if(!res_proj)
337                         res_proj = new_r_Proj(irg, bl, irn, mode_T, pn_Call_T_result);
338
339                 /* Make a Proj for the stack pointer. */
340                 sp_proj     = new_r_Proj(irg, bl, res_proj, sp->reg_class->mode, curr_res_proj++);
341                 last_inc_sp = be_new_IncSP(sp, irg, bl, sp_proj, no_mem, stack_size, be_stack_dir_against);
342                 pset_insert_ptr(env->stack_ops, last_inc_sp);
343         }
344
345         /* at last make the backend call node and set its register requirements. */
346         for(i = 0; i < n_low_args; ++i)
347                 obstack_ptr_grow(obst, get_Call_param(irn, low_args[i]));
348
349         in = obstack_finish(obst);
350         low_call = be_new_Call(irg, bl, curr_mem, curr_sp, get_Call_ptr(irn), curr_res_proj, n_low_args, in);
351         obstack_free(obst, in);
352
353         exchange(irn, low_call);
354
355         be_abi_call_free(call);
356         obstack_free(obst, pos);
357         del_pset(results);
358         del_pset(caller_save);
359 }
360
361 static void adjust_call_walker(ir_node *irn, void *data)
362 {
363         if(get_irn_opcode(irn) == iro_Call)
364                 adjust_call(data, irn);
365 }
366
367 /**
368  * Walker to implement alloca-style allocations.
369  * They are implemented using an add to the stack pointer
370  * and a copy instruction.
371  */
372 static void implement_stack_alloc(be_abi_irg_t *env, ir_node *irn)
373 {
374         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
375         ir_node *bl           = get_nodes_block(irn);
376         ir_node *res          = env->init_sp;
377         ir_node *size;
378
379         assert(get_irn_opcode(irn) == iro_Alloc && get_Alloc_where(irn) == stack_alloc);
380
381         size = get_Alloc_size(irn);
382         if(isa->stack_dir > 0)
383                 res = be_new_Copy(isa->sp->reg_class, env->birg->irg, bl, res);
384
385         res = be_new_AddSP(isa->sp, env->birg->irg, bl, res, size);
386         pset_insert_ptr(env->stack_ops, res);
387
388         if(isa->stack_dir < 0)
389                 res = be_new_Copy(isa->sp->reg_class, env->birg->irg, bl, res);
390
391 }
392
393 static void collect_return_walker(ir_node *irn, void *data)
394 {
395         if(get_irn_opcode(irn) == iro_Return) {
396                 struct obstack *obst = data;
397                 obstack_ptr_grow(obst, irn);
398         }
399 }
400
401 static ir_node *setup_frame(be_abi_irg_t *env)
402 {
403         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
404         const arch_register_t *sp = isa->sp;
405         const arch_register_t *bp = isa->bp;
406         ir_graph *irg      = env->birg->irg;
407         ir_node *bl        = get_irg_start_block(irg);
408         ir_node *no_mem    = get_irg_no_mem(irg);
409         ir_node *old_frame = get_irg_frame(irg);
410         int store_old_fp   = 1;
411         int omit_fp        = env->omit_fp;
412         ir_node *stack     = pmap_get(env->regs, (void *) sp);
413         ir_node *frame     = pmap_get(env->regs, (void *) bp);
414
415         int stack_nr       = get_Proj_proj(stack);
416
417         if(omit_fp) {
418                 stack = be_new_IncSP(sp, irg, bl, stack, no_mem, BE_STACK_FRAME_SIZE, be_stack_dir_along);
419                 frame = stack;
420         }
421
422         else {
423                 if(store_old_fp) {
424                         ir_node *irn;
425
426                         irn   = new_r_Store(irg, bl, get_irg_initial_mem(irg), stack, frame);
427                         irn   = new_r_Proj(irg, bl, irn, mode_M, pn_Store_M);
428                         stack = be_new_IncSP(sp, irg, bl, stack, irn, get_mode_size_bytes(bp->reg_class->mode), be_stack_dir_along);
429                 }
430
431                 frame = be_new_Copy(bp->reg_class, irg, bl, stack);
432
433                 be_node_set_flags(frame, -1, arch_irn_flags_dont_spill);
434                 if(env->dedicated_fp) {
435                         be_set_constr_single_reg(frame, -1, bp);
436                         be_node_set_flags(frame, -1, arch_irn_flags_ignore);
437                 }
438
439                 stack = be_new_IncSP(sp, irg, bl, stack, no_mem, BE_STACK_FRAME_SIZE, be_stack_dir_along);
440         }
441
442         be_node_set_flags(env->reg_params, -(stack_nr + 1), arch_irn_flags_ignore);
443         env->init_sp = stack;
444         set_irg_frame(irg, frame);
445         edges_reroute(old_frame, frame, irg);
446
447         return frame;
448 }
449
450 static void clearup_frame(be_abi_irg_t *env, ir_node *bl, struct obstack *obst)
451 {
452         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
453         const arch_register_t *sp = isa->sp;
454         const arch_register_t *bp = isa->bp;
455         ir_graph *irg      = env->birg->irg;
456         ir_node *no_mem    = get_irg_no_mem(irg);
457         ir_node *frame     = get_irg_frame(irg);
458         ir_node *stack     = env->init_sp;
459         int store_old_fp   = 1;
460
461         pmap_entry *ent;
462
463
464         if(env->omit_fp) {
465                 stack = be_new_IncSP(sp, irg, bl, stack, no_mem, BE_STACK_FRAME_SIZE, be_stack_dir_against);
466         }
467
468         else {
469                 stack = be_new_Copy(sp->reg_class, irg, bl, frame);
470
471                 if(store_old_fp) {
472                         ir_mode *mode = sp->reg_class->mode;
473                         ir_node *irn;
474
475                         stack = be_new_IncSP(sp, irg, bl, stack, no_mem, get_mode_size_bytes(mode), be_stack_dir_against);
476                         irn   = new_r_Load(irg, bl, no_mem, stack, mode);
477                         irn   = new_r_Proj(irg, bl, irn, mode, pn_Load_res);
478                         frame = be_new_Copy(bp->reg_class, irg, bl, irn);
479                 }
480
481                 if(env->dedicated_fp) {
482                         be_set_constr_single_reg(frame, -1, bp);
483                 }
484
485         }
486
487         pmap_foreach(env->regs, ent) {
488                 const arch_register_t *reg = ent->key;
489                 ir_node *irn               = ent->value;
490
491                 if(reg == sp)
492                         irn = stack;
493                 else if(reg == bp)
494                         irn = frame;
495
496                 obstack_ptr_grow(obst, irn);
497         }
498 }
499
500 /**
501  * Modify the irg itself and the frame type.
502  */
503 static void modify_irg(be_abi_irg_t *env)
504 {
505         firm_dbg_module_t *dbg    = env->dbg;
506         be_abi_call_t *call       = be_abi_call_new();
507         const arch_isa_t *isa     = env->birg->main_env->arch_env->isa;
508         const arch_register_t *sp = arch_isa_sp(isa);
509         ir_graph *irg             = env->birg->irg;
510         ir_node *bl               = get_irg_start_block(irg);
511         ir_node *end              = get_irg_end_block(irg);
512         ir_node *arg_tuple        = get_irg_args(irg);
513         ir_node *no_mem           = get_irg_no_mem(irg);
514         type *method_type         = get_entity_type(get_irg_entity(irg));
515         int n_params              = get_method_n_params(method_type);
516
517         int max_arg               = 0;
518         int reg_params_nr         = 0;
519         int arg_offset            = 0;
520
521         int i, j, n;
522
523         ir_node *frame_pointer;
524         ir_node *reg_params, *reg_params_bl;
525         ir_node **args, **args_repl;
526         const ir_edge_t *edge;
527
528         pmap_entry *ent;
529
530         env->regs = pmap_create();
531
532         DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
533
534         /* Find the maximum proj number of the argument tuple proj */
535         foreach_out_edge(arg_tuple, edge)  {
536                 ir_node *irn = get_edge_src_irn(edge);
537                 int nr       = get_Proj_proj(irn);
538                 max_arg      = MAX(max_arg, nr);
539         }
540         max_arg += 1;
541         args      = obstack_alloc(&env->obst, max_arg * sizeof(args[0]));
542         args_repl = obstack_alloc(&env->obst, max_arg * sizeof(args[0]));
543         memset(args, 0, max_arg * sizeof(args[0]));
544         memset(args_repl, 0, max_arg * sizeof(args[0]));
545
546         /* Fill the argument vector */
547         foreach_out_edge(arg_tuple, edge) {
548                 ir_node *irn = get_edge_src_irn(edge);
549                 int nr       = get_Proj_proj(irn);
550                 args[nr]     = irn;
551                 DBG((dbg, LEVEL_2, "\treading arg: %d -> %+F\n", nr, irn));
552         }
553
554         /* Get the ABI constraints from the ISA */
555         arch_isa_get_call_abi(isa, method_type, call);
556
557         /* Count the register params and add them to the number of Projs for the RegParams node */
558         for(i = 0; i < n_params; ++i) {
559                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
560                 if(arg->in_reg) {
561                         assert(arg->reg != sp && "cannot use stack pointer as parameter register");
562                         pmap_insert(env->regs, (void *) arg->reg, NULL);
563                         DBG((dbg, LEVEL_2, "\targ #%d -> reg %s\n", i, arg->reg->name));
564                 }
565         }
566
567         /* Collect all callee-save registers */
568         for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
569                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
570                 for(j = 0; j < cls->n_regs; ++j) {
571                         const arch_register_t *reg = &cls->regs[j];
572                         if(arch_register_type_is(reg, callee_save))
573                                 pmap_insert(env->regs, (void *) reg, NULL);
574                 }
575         }
576
577         pmap_insert(env->regs, (void *) sp, NULL);
578         pmap_insert(env->regs, (void *) isa->bp, NULL);
579         reg_params_bl = get_irg_start_block(irg);
580         env->reg_params = reg_params = be_new_RegParams(irg, reg_params_bl, pmap_count(env->regs));
581         reg_params_nr = 0;
582
583         /*
584          * make proj nodes for the callee save registers.
585          * memorize them, since Return nodes get those as inputs.
586          */
587         for(ent = pmap_first(env->regs); ent; ent = pmap_next(env->regs)) {
588                 arch_register_t *reg = ent->key;
589                 int pos = -(reg_params_nr + 1);
590                 ent->value = new_r_Proj(irg, reg_params_bl, reg_params, reg->reg_class->mode, reg_params_nr);
591                 be_set_constr_single_reg(reg_params, pos, reg);
592
593                 /*
594                  * If the register is an ignore register,
595                  * The Proj for that register shall also be ignored during register allocation.
596                  */
597                 if(arch_register_type_is(reg, ignore))
598                         be_node_set_flags(reg_params, pos, arch_irn_flags_ignore);
599
600                 reg_params_nr++;
601
602                 DBG((dbg, LEVEL_2, "\tregister save proj #%d -> reg %s\n", reg_params_nr - 1, reg->name));
603         }
604
605         /* Insert the code to set up the stack frame */
606         frame_pointer = setup_frame(env);
607
608 #if 0
609         proj_sp = pmap_get(regs, (void *) sp);
610         proj_bp = pmap_get(regs, (void *) bp);
611         assert(proj_sp != NULL && "There must be a Proj for the stack pointer");
612         assert(proj_sp != NULL && "There must be a Proj for the base pointer");
613
614         /* Set the Proj for the stack pointer to ignore. */
615         be_node_set_flags(reg_params, -(get_Proj_proj(proj_sp) + 1), arch_irn_flags_ignore);
616
617         /*
618          * If a frame pointer is needed and the frame pointer is in a dedicated register,
619          * also exclude that from register allocation by setting the corresponding
620          * Proj to ignore.
621          */
622         if(!env->omit_fp && env->dedicated_fp)
623                 be_node_set_flags(reg_params, -(get_Proj_proj(proj_bp) + 1), arch_irn_flags_ignore);
624
625
626         if(env->omit_fp) {
627                 /* This is the stack pointer add/sub which allocates the frame. remind it for later fix up. */
628                 env->init_sp  = be_new_IncSP(sp, irg, reg_params_bl, proj_sp, no_mem, 0, be_stack_dir_along);
629                 frame_pointer = env->init_sp;
630         }
631
632         else {
633                 env->init_sp  = proj_sp;
634                 frame_pointer = be_new_Copy(sp->reg_class, irg, reg_params_bl, proj_sp);
635         }
636
637         /* Set the new frame pointer. */
638         exchange(get_irg_frame(irg), frame_pointer);
639         set_irg_frame(irg, frame_pointer);
640 #endif
641
642         /* compute the start offset for the stack parameters. */
643         {
644                 int arg_offset = 0;
645                 int arg_size   = 0;
646                 int inc_dir    = isa->stack_dir * (env->left_to_right ? 1 : -1);
647
648                 for(i = 0; i < n_params; ++i) {
649                         be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
650                         if(!arg->in_reg)
651                                 arg_size += get_type_size_bytes(get_method_param_type(method_type, i));
652                 }
653
654                 arg_offset = -isa->stack_dir * call->arg_gap + env->left_to_right * arg_size;
655
656                 /* Now, introduce stack param nodes for all parameters passed on the stack */
657                 for(i = 0; i < max_arg; ++i) {
658                         ir_node *arg_proj = args[i];
659                         if(arg_proj != NULL) {
660                                 be_abi_call_arg_t *arg;
661                                 ir_type *param_type;
662                                 int nr = get_Proj_proj(arg_proj);
663
664                                 nr         = MIN(nr, n_params);
665                                 arg        = get_call_arg(call, 0, nr);
666                                 param_type = get_method_param_type(method_type, nr);
667
668                                 if(arg->in_reg) {
669                                         args_repl[i] = new_r_Proj(irg, reg_params_bl, reg_params, get_irn_mode(arg_proj), reg_params_nr);
670                                         be_set_constr_single_reg(reg_params, -(reg_params_nr + 1), arg->reg);
671                                         reg_params_nr++;
672                                 }
673
674                                 /* when the (stack) parameter is primitive, we insert a StackParam
675                                 node representing the load of that parameter */
676                                 else {
677                                         int size = get_type_size_bytes(param_type) * isa->stack_dir;
678
679                                         if(inc_dir < 0)
680                                                 arg_offset -= size;
681
682                                         /* For atomic parameters which are actually used, we create a StackParam node. */
683                                         if(is_atomic_type(param_type) && get_irn_n_edges(args[i]) > 0) {
684                                                 ir_mode *mode                    = get_type_mode(param_type);
685                                                 const arch_register_class_t *cls = arch_isa_get_reg_class_for_mode(isa, mode);
686                                                 args_repl[i] = be_new_StackParam(cls, irg, reg_params_bl, mode, frame_pointer, arg_offset);
687                                         }
688
689                                         /* The stack parameter is not primitive (it is a struct or array),
690                                         we thus will create a node representing the parameter's address
691                                         on the stack. */
692                                         else {
693                                                 assert(0 && "struct parameters are not supported");
694                                         }
695
696                                         if(inc_dir > 0)
697                                                 arg_offset += size;
698                                 }
699                         }
700                 }
701         }
702
703         /* reroute the edges from the original argument projs to the RegParam ones. */
704         for(i = 0; i < max_arg; ++i) {
705                 if(args[i] != NULL) {
706                         assert(args_repl[i] != NULL);
707                         edges_reroute(args[i], args_repl[i], irg);
708                 }
709         }
710
711         /* All Return nodes hang on the End node, so look for them there. */
712         for(i = 0, n = get_irn_arity(end); i < n; ++i) {
713                 ir_node *irn = get_irn_n(end, i);
714
715                 if(get_irn_opcode(irn) == iro_Return) {
716                         ir_node *bl   = get_nodes_block(irn);
717                         int n_res     = get_Return_n_ress(irn);
718                         pmap *reg_map = pmap_create_ex(n_res);
719                         ir_node *ret;
720                         int i, n;
721                         ir_node **in;
722
723                         /* collect all arguments of the return */
724                         for(i = 0; i < n_res; ++i) {
725                                 ir_node *res           = get_Return_res(irn, i);
726                                 be_abi_call_arg_t *arg = get_call_arg(call, 1, i);
727
728                                 assert(arg->in_reg && "return value must be passed in register");
729                                 pmap_insert(reg_map, res, (void *) arg->reg);
730                                 obstack_ptr_grow(&env->obst, res);
731                         }
732
733                         /* generate the clean up code and add additional parameters to the return. */
734                         clearup_frame(env, bl, &env->obst);
735
736                         /* The in array for the new back end return is now ready. */
737                         n   = obstack_object_size(&env->obst) / sizeof(in[0]);
738                         in  = obstack_finish(&env->obst);
739                         ret = be_new_Return(irg, bl, n, in);
740
741                         /* Set the constraints for some arguments of the return. */
742                         for(i = 0; i < n; i++) {
743                                 const arch_register_t *reg = pmap_get(reg_map, in[i]);
744                                 if(reg != NULL)
745                                         be_set_constr_single_reg(ret, i, reg);
746                         }
747                         exchange(irn, ret);
748                         obstack_free(&env->obst, in);
749                         pmap_destroy(reg_map);
750                 }
751         }
752
753         obstack_free(&env->obst, args);
754         be_abi_call_free(call);
755 }
756
757 static void collect_alloca_walker(ir_node *irn, void *data)
758 {
759         be_abi_irg_t *env = data;
760         if(get_irn_opcode(irn) == iro_Alloc && get_Alloc_where(irn) == stack_alloc)
761                 obstack_ptr_grow(&env->obst, irn);
762 }
763
764 be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
765 {
766         be_abi_irg_t *env = malloc(sizeof(env[0]));
767
768         int i;
769         ir_node **stack_allocs;
770
771         env->method_type   = get_entity_type(get_irg_entity(birg->irg));
772         env->call          = be_abi_call_new();
773         arch_isa_get_call_abi(birg->main_env->arch_env->isa, env->method_type, env->call);
774
775         env->omit_fp       = (env->call->flags & BE_ABI_TRY_OMIT_FRAME_POINTER) != 0;
776         env->dedicated_fp  = (env->call->flags & BE_ABI_FRAME_POINTER_DEDICATED) != 0;
777         env->left_to_right = (env->call->flags & BE_ABI_LEFT_TO_RIGHT) != 0;
778         env->birg          = birg;
779         env->stack_ops     = pset_new_ptr(32);
780         env->dbg           = firm_dbg_register("firm.be.abi");
781         obstack_init(&env->obst);
782
783         /* search for stack allocation nodes and record them */
784         irg_walk_graph(env->birg->irg, collect_alloca_walker, NULL, env);
785         obstack_ptr_grow(&env->obst, NULL);
786         stack_allocs = obstack_finish(&env->obst);
787
788         /* If there are stack allocations in the irg, we need a frame pointer */
789         if(stack_allocs[0] != NULL)
790                 env->omit_fp = 0;
791
792         modify_irg(env);
793
794         for(i = 0; stack_allocs[i] != NULL; ++i)
795                 implement_stack_alloc(env, stack_allocs[i]);
796
797         irg_walk_graph(env->birg->irg, NULL, adjust_call_walker, env);
798         return env;
799 }
800
801 static void collect_stack_nodes(ir_node *irn, void *data)
802 {
803         pset *s = data;
804
805         switch(be_get_irn_opcode(irn)) {
806         case beo_IncSP:
807         case beo_AddSP:
808                 pset_insert_ptr(s, irn);
809         }
810 }
811
812 void be_abi_fix_stack_nodes(be_abi_irg_t *env)
813 {
814         dom_front_info_t *df;
815         pset *stack_ops;
816
817         /* We need dominance frontiers for fix up */
818         df = be_compute_dominance_frontiers(env->birg->irg);
819
820         stack_ops = pset_new_ptr_default();
821         pset_insert_ptr(env->stack_ops, env->init_sp);
822         irg_walk_graph(env->birg->irg, collect_stack_nodes, NULL, stack_ops);
823         be_ssa_constr_set(df, stack_ops);
824         del_pset(stack_ops);
825
826         /* free these dominance frontiers */
827         be_free_dominance_frontiers(df);
828 }
829
830 static int get_dir(ir_node *irn)
831 {
832         return 1 - 2 * (be_get_IncSP_direction(irn) == be_stack_dir_against);
833 }
834
835 static int process_stack_bias(be_abi_irg_t *env, ir_node *bl, int bias)
836 {
837         const arch_env_t *aenv = env->birg->main_env->arch_env;
838         ir_node *irn;
839         int start_bias = bias;
840
841         sched_foreach(bl, irn) {
842                 if(be_is_IncSP(irn)) {
843                         int ofs = be_get_IncSP_offset(irn);
844                         int dir = get_dir(irn);
845
846                         if(ofs == BE_STACK_FRAME_SIZE) {
847                                 ofs = get_type_size_bytes(get_irg_frame_type(env->birg->irg));
848                                 be_set_IncSP_offset(irn, ofs);
849                         }
850
851                         bias += dir * ofs;
852                 }
853
854                 else
855                         arch_set_stack_bias(aenv, irn, bias);
856         }
857
858         return bias;
859 }
860
861 static void stack_bias_walker(ir_node *bl, void *data)
862 {
863         if(bl != get_irg_start_block(get_irn_irg(bl))) {
864                 be_abi_irg_t *env = data;
865                 process_stack_bias(env, bl, env->start_block_bias);
866         }
867 }
868
869 void be_abi_fix_stack_bias(be_abi_irg_t *env)
870 {
871         ir_graph *irg  = env->birg->irg;
872
873         /* Determine the stack bias at the and of the start block. */
874         env->start_block_bias = process_stack_bias(env, get_irg_start_block(irg), 0);
875
876         /* fix the bias is all other blocks */
877         irg_block_walk_graph(irg, stack_bias_walker, NULL, env);
878 }
879
880 void be_abi_free(be_abi_irg_t *env)
881 {
882         del_pset(env->stack_ops);
883         obstack_free(&env->obst, NULL);
884         free(env);
885 }
886
887 ir_node *be_abi_get_callee_save_irn(be_abi_irg_t *abi, const arch_register_t *reg)
888 {
889         assert(arch_register_type_is(reg, callee_save));
890         assert(pmap_contains(abi->regs, (void *) reg));
891         return pmap_get(abi->regs, (void *) reg);
892 }