ae5be16ea93a9c63d98d6287392b83dd65fd393a
[libfirm] / ir / be / mips / bearch_mips.c
1 /* The main mips backend driver file. */
2 /* $Id$ */
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include "pseudo_irg.h"
9 #include "irgwalk.h"
10 #include "irprog.h"
11 #include "irprintf.h"
12 #include "ircons.h"
13 #include "irgmod.h"
14 #include "irgopt.h"
15 #include "irgwalk.h"
16 #include "iredges.h"
17 #include "irdump.h"
18 #include "irextbb.h"
19
20 #include "bitset.h"
21 #include "debug.h"
22
23 #include "../bearch.h"                /* the general register allocator interface */
24 #include "../benode_t.h"
25 #include "../belower.h"
26 #include "../besched_t.h"
27 #include "../be.h"
28 #include "../beabi.h"
29 #include "../bemachine.h"
30 #include "../bemodule.h"
31
32 #include "bearch_mips_t.h"
33
34 #include "mips_new_nodes.h"           /* mips nodes interface */
35 #include "gen_mips_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
36 #include "mips_gen_decls.h"           /* interface declaration emitter */
37 #include "mips_transform.h"
38 #include "mips_emitter.h"
39 #include "mips_map_regs.h"
40 #include "mips_util.h"
41 #include "mips_scheduler.h"
42
43 #define DEBUG_MODULE "firm.be.mips.isa"
44
45 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
46 static set *cur_reg_set = NULL;
47
48 /**************************************************
49  *                         _ _              _  __
50  *                        | | |            (_)/ _|
51  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
52  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
53  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
54  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
55  *            __/ |
56  *           |___/
57  **************************************************/
58
59 static ir_node *my_skip_proj(const ir_node *n) {
60         while (is_Proj(n))
61                 n = get_Proj_pred(n);
62         return (ir_node *)n;
63 }
64
65 /**
66  * Return register requirements for a mips node.
67  * If the node returns a tuple (mode_T) then the proj's
68  * will be asked for this information.
69  */
70 static const arch_register_req_t *mips_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
71         const mips_register_req_t *irn_req;
72         long               node_pos = pos == -1 ? 0 : pos;
73         ir_mode           *mode     = get_irn_mode(irn);
74         FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
75
76         if (is_Block(irn) || mode == mode_X || mode == mode_M) {
77                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
78                 return NULL;
79         }
80
81         if (mode == mode_T && pos < 0) {
82                 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F\n", irn));
83                 return NULL;
84         }
85
86         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
87
88         if (is_Proj(irn)) {
89                 /* in case of a proj, we need to get the correct OUT slot */
90                 /* of the node corresponding to the proj number */
91                 if (pos == -1) {
92                         node_pos = mips_translate_proj_pos(irn);
93                 }
94                 else {
95                         node_pos = pos;
96                 }
97
98                 irn = my_skip_proj(irn);
99
100                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
101         }
102
103         /* get requirements for our own nodes */
104         if (is_mips_irn(irn)) {
105                 if (pos >= 0) {
106                         irn_req = get_mips_in_req(irn, pos);
107                 }
108                 else {
109                         irn_req = get_mips_out_req(irn, node_pos);
110                 }
111
112                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
113
114                 memcpy(req, &(irn_req->req), sizeof(*req));
115
116                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
117                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
118                         req->other_same = get_irn_n(irn, irn_req->same_pos);
119                 }
120
121                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
122                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
123                         req->other_different = get_irn_n(irn, irn_req->different_pos);
124                 }
125         }
126         /* get requirements for FIRM nodes */
127         else {
128                 /* treat Phi like Const with default requirements */
129                 if (is_Phi(irn)) {
130                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
131
132                         if (mode_is_float(mode)) {
133                                 //memcpy(req, &(mips_default_req_mips_floating_point.req), sizeof(*req));
134                                 assert(0 && "floating point not supported (yet)");
135                         }
136                         else if (mode_is_int(mode) || mode_is_reference(mode)) {
137                                 memcpy(req, &(mips_default_req_mips_gp.req), sizeof(*req));
138                         }
139                         else if (mode == mode_T || mode == mode_M) {
140                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
141                                 return NULL;
142                         }
143                         else {
144                                 assert(0 && "unsupported Phi-Mode");
145                         }
146                 }
147                 else {
148                         DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
149                         req = NULL;
150                 }
151         }
152
153         return req;
154 }
155
156 static void mips_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
157         int pos = 0;
158
159         if (is_Proj(irn)) {
160
161                 if (get_irn_mode(irn) == mode_X) {
162                         return;
163                 }
164
165                 pos = mips_translate_proj_pos(irn);
166                 irn = my_skip_proj(irn);
167         }
168
169         if (is_mips_irn(irn)) {
170                 const arch_register_t **slots;
171
172                 slots      = get_mips_slots(irn);
173                 slots[pos] = reg;
174         }
175         else {
176                 /* here we set the registers for the Phi nodes */
177                 mips_set_firm_reg(irn, reg, cur_reg_set);
178         }
179 }
180
181 static const arch_register_t *mips_get_irn_reg(const void *self, const ir_node *irn) {
182         int pos = 0;
183         const arch_register_t *reg = NULL;
184
185         if (is_Proj(irn)) {
186
187                 if (get_irn_mode(irn) == mode_X) {
188                         return NULL;
189                 }
190
191                 pos = mips_translate_proj_pos(irn);
192                 irn = my_skip_proj(irn);
193         }
194
195         if (is_mips_irn(irn)) {
196                 const arch_register_t **slots;
197                 slots = get_mips_slots(irn);
198                 reg   = slots[pos];
199         }
200         else {
201                 reg = mips_get_firm_reg(irn, cur_reg_set);
202         }
203
204         return reg;
205 }
206
207 static arch_irn_class_t mips_classify(const void *self, const ir_node *irn) {
208         irn = my_skip_proj(irn);
209
210         if (is_cfop(irn)) {
211                 return arch_irn_class_branch;
212         } else if (is_mips_irn(irn)) {
213                 return arch_irn_class_normal;
214         }
215
216         return 0;
217 }
218
219 static arch_irn_flags_t mips_get_flags(const void *self, const ir_node *irn) {
220         irn = my_skip_proj(irn);
221
222         if (is_mips_irn(irn)) {
223                 return get_mips_flags(irn);
224         }
225         else if (is_Unknown(irn)) {
226                 return arch_irn_flags_ignore;
227         }
228
229         return 0;
230 }
231
232 static ir_entity *mips_get_frame_entity(const void *self, const ir_node *irn) {
233         if(is_mips_load_r(irn) || is_mips_store_r(irn)) {
234                 mips_attr_t *attr = get_mips_attr(irn);
235
236                 return attr->stack_entity;
237         }
238
239         return NULL;
240 }
241
242 static void mips_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent) {
243         mips_attr_t *attr  = get_mips_attr(irn);
244         assert(is_mips_load_r(irn) || is_mips_store_r(irn));
245         attr->stack_entity = ent;
246 }
247
248 /**
249  * This function is called by the generic backend to correct offsets for
250  * nodes accessing the stack.
251  */
252 static void mips_set_frame_offset(const void *self, ir_node *irn, int offset) {
253         mips_attr_t *attr = get_mips_attr(irn);
254         assert(is_mips_load_r(irn) || is_mips_store_r(irn));
255
256         attr->stack_entity_offset = offset;
257 }
258
259 static int mips_get_sp_bias(const void *self, const ir_node *irn) {
260         return 0;
261 }
262
263 /* fill register allocator interface */
264
265 static const arch_irn_ops_if_t mips_irn_ops_if = {
266         mips_get_irn_reg_req,
267         mips_set_irn_reg,
268         mips_get_irn_reg,
269         mips_classify,
270         mips_get_flags,
271         mips_get_frame_entity,
272         mips_set_frame_entity,
273         mips_set_frame_offset,
274         mips_get_sp_bias,
275         NULL,    /* get_inverse             */
276         NULL,    /* get_op_estimated_cost   */
277         NULL,    /* possible_memory_operand */
278         NULL,    /* perform_memory_operand  */
279 };
280
281 mips_irn_ops_t mips_irn_ops = {
282         &mips_irn_ops_if,
283         NULL
284 };
285
286
287
288 /**************************************************
289  *                _                         _  __
290  *               | |                       (_)/ _|
291  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
292  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
293  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
294  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
295  *                        __/ |
296  *                       |___/
297  **************************************************/
298
299
300 typedef struct {
301         ir_node *start;
302         ir_node *end;
303         unsigned cnt;
304 } anchor;
305
306 /**
307  * Ext-Block walker: create a block schedule
308  */
309 static void create_block_list(ir_extblk *blk, void *env) {
310         anchor *list = env;
311         int i, n;
312
313         for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
314                 ir_node *block = get_extbb_block(blk, i);
315
316                 set_irn_link(block, NULL);
317                 if (list->start)
318                         set_irn_link(list->end, block);
319                 else
320                         list->start = block;
321
322                 list->end = block;
323                 list->cnt += 1;
324         }
325 }
326
327 /* return the scheduled block at position pos */
328 ir_node *mips_get_sched_block(const mips_code_gen_t *cg, int pos) {
329         if (0 <= pos && pos < ARR_LEN(cg->bl_list))
330                 return cg->bl_list[pos];
331         return NULL;
332 }
333
334 /* return the number of scheduled blocks */
335 int mips_get_sched_n_blocks(const mips_code_gen_t *cg) {
336         return ARR_LEN(cg->bl_list);
337 }
338
339 /* set a block schedule number */
340 void mips_set_block_sched_nr(ir_node *block, int nr) {
341         set_irn_link(block, INT_TO_PTR(nr));
342 }
343
344 /* get a block schedule number */
345 int mips_get_block_sched_nr(ir_node *block) {
346         return PTR_TO_INT(get_irn_link(block));
347 }
348
349 /**
350  * Creates a block schedule for the given graph.
351  */
352 static void mips_create_block_sched(mips_code_gen_t *cg) {
353         anchor list;
354         ir_node **bl_list, *block;
355         unsigned i;
356
357         if (cg->bl_list) {
358                 DEL_ARR_F(cg->bl_list);
359                 free_survive_dce(cg->bl_list_sdce);
360         }
361
362         /* calculate the block schedule here */
363         compute_extbb(cg->irg);
364
365         list.start = NULL;
366         list.end   = NULL;
367         list.cnt   = 0;
368         irg_extblock_walk_graph(cg->irg, NULL, create_block_list, &list);
369
370
371         bl_list = NEW_ARR_F(ir_node *, list.cnt);
372         cg->bl_list_sdce = new_survive_dce();
373         for (i = 0, block = list.start; block; block = get_irn_link(block)) {
374                 bl_list[i] = block;
375                 survive_dce_register_irn(cg->bl_list_sdce, &bl_list[i]);
376                 i++;
377         }
378
379         cg->bl_list = bl_list;
380 }
381
382 typedef struct _wenv_t {
383         ir_node *list;
384 } wenv_t;
385
386 /**
387  * Walker: link all CopyB nodes
388  */
389 static void collect_copyb_nodes(ir_node *node, void *env) {
390         wenv_t *wenv = env;
391
392         if (get_irn_op(node) == op_CopyB) {
393                 set_irn_link(node, wenv->list);
394                 wenv->list = node;
395         }
396 }
397
398 static void replace_copyb_nodes(mips_code_gen_t *cg) {
399         wenv_t env;
400         ir_node *copy, *next;
401         ir_node *old_bl, *new_bl, *jmp, *new_jmp, *mem;
402         const ir_edge_t *edge;
403
404         /* build code for all copyB */
405         env.list = NULL;
406         irg_walk_graph(cg->irg, NULL, collect_copyb_nodes, &env);
407
408         for (copy = env.list; copy; copy = next) {
409                 next = get_irn_link(copy);
410
411                 old_bl = get_nodes_block(copy);
412                 part_block(copy);
413                 jmp     = get_Block_cfgpred(old_bl, 0);
414                 new_jmp = new_r_Jmp(cg->irg, get_nodes_block(copy));
415
416                 new_bl = new_r_Block(cg->irg, 1, &new_jmp);
417                 set_nodes_block(jmp, new_bl);
418
419                 mem = gen_code_for_CopyB(new_bl, copy);
420
421                 /* fix copyB's out edges */
422                 foreach_out_edge(copy, edge) {
423                         ir_node *succ = get_edge_src_irn(edge);
424
425                         assert(is_Proj(succ));
426                         switch (get_Proj_proj(succ)) {
427                         case pn_CopyB_M_regular:
428                         case pn_CopyB_M_except:
429                                 exchange(succ, mem);
430                                 break;
431                         default:
432                                 exchange(succ, get_irg_bad(cg->irg));
433                         }
434                 }
435         }
436 }
437
438 /**
439  * Transforms the standard firm graph into
440  * a mips firm graph
441  */
442 static void mips_prepare_graph(void *self) {
443         mips_code_gen_t *cg = self;
444         int bl_nr, n;
445
446         // replace all copyb nodes in the block with a loop
447         // and mips store/load nodes
448         replace_copyb_nodes(cg);
449
450         // Calculate block schedule
451         mips_create_block_sched(cg);
452
453         /* enter the block number into every blocks link field */
454         for (bl_nr = 0, n = mips_get_sched_n_blocks(cg); bl_nr < n; ++bl_nr) {
455                 ir_node *bl = mips_get_sched_block(cg, bl_nr);
456                 mips_set_block_sched_nr(bl, bl_nr);
457         }
458
459         // walk the graph and transform firm nodes into mips nodes where possible
460         irg_walk_blkwise_graph(cg->irg, mips_pre_transform_node, mips_transform_node, cg);
461
462         dump_ir_block_graph_sched(cg->irg, "-transformed");
463 }
464
465 /**
466  * Called immediately before emit phase.
467  */
468 static void mips_finish_irg(void *self) {
469         mips_code_gen_t *cg = self;
470         ir_graph        *irg = cg->irg;
471
472         dump_ir_block_graph_sched(irg, "-mips-finished");
473 }
474
475
476 /**
477  * These are some hooks which must be filled but are probably not needed.
478  */
479 static void mips_before_sched(void *self) {
480         /* Some stuff you need to do after scheduling but before register allocation */
481 }
482
483 static void mips_before_ra(void *self) {
484         /* Some stuff you need to do immediately after register allocation */
485 }
486
487 static void mips_after_ra(void* self) {
488         mips_code_gen_t *cg = self;
489         irg_walk_blkwise_graph(cg->irg, NULL, mips_after_ra_walker, self);
490 }
491
492 /**
493  * Emits the code, closes the output file and frees
494  * the code generator interface.
495  */
496 static void mips_emit_and_done(void *self) {
497         mips_code_gen_t *cg = self;
498         ir_graph           *irg = cg->irg;
499         FILE               *out = cg->isa->out;
500
501         mips_register_emitters();
502
503         if (cg->emit_decls) {
504                 mips_gen_decls(out);
505                 cg->emit_decls = 0;
506         }
507
508         mips_gen_routine(out, irg, cg);
509
510         cur_reg_set = NULL;
511
512         /* de-allocate code generator */
513         del_set(cg->reg_set);
514         if (cg->bl_list) {
515                 DEL_ARR_F(cg->bl_list);
516                 free_survive_dce(cg->bl_list_sdce);
517         }
518         free(cg);
519 }
520
521 static void *mips_cg_init(be_irg_t *birg);
522
523 static const arch_code_generator_if_t mips_code_gen_if = {
524         mips_cg_init,
525         NULL,                /* before abi introduce */
526         mips_prepare_graph,
527         NULL,                /* spill */
528         mips_before_sched,   /* before scheduling hook */
529         mips_before_ra,      /* before register allocation hook */
530         mips_after_ra,
531         mips_finish_irg,
532         mips_emit_and_done
533 };
534
535 /**
536  * Initializes the code generator.
537  */
538 static void *mips_cg_init(be_irg_t *birg) {
539         mips_isa_t      *isa = (mips_isa_t *)birg->main_env->arch_env->isa;
540         mips_code_gen_t *cg  = xmalloc(sizeof(*cg));
541
542         cg->impl     = &mips_code_gen_if;
543         cg->irg      = birg->irg;
544         cg->reg_set  = new_set(mips_cmp_irn_reg_assoc, 1024);
545         cg->arch_env = birg->main_env->arch_env;
546         cg->isa      = isa;
547         cg->birg     = birg;
548         cg->bl_list  = NULL;
549         FIRM_DBG_REGISTER(cg->mod, "firm.be.mips.cg");
550
551         isa->num_codegens++;
552
553         if (isa->num_codegens > 1)
554                 cg->emit_decls = 0;
555         else
556                 cg->emit_decls = 1;
557
558         cur_reg_set = cg->reg_set;
559
560         mips_irn_ops.cg = cg;
561
562         return (arch_code_generator_t *)cg;
563 }
564
565
566 /*****************************************************************
567  *  ____             _                  _   _____  _____
568  * |  _ \           | |                | | |_   _|/ ____|  /\
569  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
570  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
571  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
572  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
573  *
574  *****************************************************************/
575
576 static mips_isa_t mips_isa_template = {
577         &mips_isa_if,
578         &mips_gp_regs[REG_SP],
579         &mips_gp_regs[REG_FP],
580         -1,             // stack direction
581         0,              // num codegens?!? TODO what is this?
582         NULL
583 };
584
585 /**
586  * Initializes the backend ISA and opens the output file.
587  */
588 static void *mips_init(FILE *file_handle) {
589         static int inited = 0;
590         mips_isa_t *isa;
591
592         if(inited)
593                 return NULL;
594
595         isa = xcalloc(1, sizeof(*isa));
596         memcpy(isa, &mips_isa_template, sizeof(*isa));
597
598         isa->out = file_handle;
599
600         mips_register_init(isa);
601         mips_create_opcodes();
602         mips_init_opcode_transforms();
603
604         inited = 1;
605
606         return isa;
607 }
608
609 /**
610  * Closes the output file and frees the ISA structure.
611  */
612 static void mips_done(void *self) {
613         free(self);
614 }
615
616 static int mips_get_n_reg_class(const void *self) {
617         return N_CLASSES;
618 }
619
620 static const arch_register_class_t *mips_get_reg_class(const void *self, int i) {
621         assert(i >= 0 && i < N_CLASSES && "Invalid mips register class requested.");
622         return &mips_reg_classes[i];
623 }
624
625
626
627 /**
628  * Get the register class which shall be used to store a value of a given mode.
629  * @param self The this pointer.
630  * @param mode The mode in question.
631  * @return A register class which can hold values of the given mode.
632  */
633 const arch_register_class_t *mips_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
634         ASSERT_NO_FLOAT(mode);
635         return &mips_reg_classes[CLASS_mips_gp];
636 }
637
638 typedef struct {
639         be_abi_call_flags_bits_t flags;
640         const mips_isa_t *isa;
641         const arch_env_t *arch_env;
642         ir_graph *irg;
643         // do special handling to support debuggers
644         int debug;
645 } mips_abi_env_t;
646
647 static void *mips_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
648 {
649         mips_abi_env_t *env    = xmalloc(sizeof(env[0]));
650         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
651         env->flags = fl.bits;
652         env->irg   = irg;
653         env->arch_env = arch_env;
654         env->isa   = (const mips_isa_t*) arch_env->isa;
655         env->debug = 1;
656         return env;
657 }
658
659 static void mips_abi_dont_save_regs(void *self, pset *s)
660 {
661         mips_abi_env_t *env = self;
662         if(env->flags.try_omit_fp)
663                 pset_insert_ptr(s, env->isa->fp);
664 }
665
666 static const arch_register_t *mips_abi_prologue(void *self, ir_node** mem, pmap *reg_map)
667 {
668         mips_abi_env_t *env = self;
669         ir_graph *irg = env->irg;
670         dbg_info *dbg = NULL; // TODO where can I get this from?
671         ir_node *block = get_irg_start_block(env->irg);
672         mips_attr_t *attr;
673         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
674         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
675         int initialstackframesize;
676
677         if(env->debug) {
678                 /*
679                  * The calling conventions wants a stack frame of at least 24bytes size with
680                  *   a0-a3 saved in offset 0-12
681                  *   fp saved in offset 16
682                  *   ra saved in offset 20
683                  */
684                 ir_node *mm[6];
685                 ir_node *sync, *reg, *store;
686                 initialstackframesize = 24;
687
688                 // - setup first part of stackframe
689                 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
690                 attr = get_mips_attr(sp);
691                 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
692                 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
693                 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
694
695                 /* TODO: where to get an edge with a0-a3
696                 int i;
697                 for(i = 0; i < 4; ++i) {
698                         ir_node *reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_A0 + i]);
699                         ir_node *store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
700                         attr = get_mips_attr(store);
701                         attr->load_store_mode = mode_Iu;
702                         attr->tv = new_tarval_from_long(i * 4, mode_Is);
703
704                         mm[i] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
705                 }
706                 */
707
708                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
709                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
710                 attr = get_mips_attr(store);
711                 attr->modes.load_store_mode = mode_Iu;
712                 attr->tv = new_tarval_from_long(16, mode_Is);
713
714                 mm[4] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
715
716                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_RA]);
717                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
718                 attr = get_mips_attr(store);
719                 attr->modes.load_store_mode = mode_Iu;
720                 attr->tv = new_tarval_from_long(20, mode_Is);
721
722                 mm[5] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
723
724                 // TODO ideally we would route these mem edges directly towards the epilogue
725                 sync = new_r_Sync(irg, block, 2, mm+4);
726                 *mem = sync;
727         } else {
728                 ir_node *reg, *store;
729                 initialstackframesize = 4;
730
731                 // save old framepointer
732                 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
733                 attr = get_mips_attr(sp);
734                 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
735                 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
736                 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
737
738                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
739                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
740                 attr = get_mips_attr(store);
741                 attr->modes.load_store_mode = mode_Iu;
742                 attr->tv = new_tarval_from_long(0, mode_Is);
743
744                 *mem = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
745         }
746
747         // setup framepointer
748         fp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
749         attr = get_mips_attr(fp);
750         attr->tv = new_tarval_from_long(initialstackframesize, mode_Is);
751         mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
752         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
753
754         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
755         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
756
757         return &mips_gp_regs[REG_SP];
758 }
759
760 static void mips_abi_epilogue(void *self, ir_node *block, ir_node **mem, pmap *reg_map)
761 {
762         mips_abi_env_t *env = self;
763         ir_graph *irg = env->irg;
764         dbg_info *dbg = NULL; // TODO where can I get this from?
765         mips_attr_t *attr;
766         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
767         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
768         ir_node *load;
769         int initial_frame_size = env->debug ? 24 : 4;
770         int fp_save_offset = env->debug ? 16 : 0;
771
772         // copy fp to sp
773         sp = new_rd_mips_move(dbg, irg, block, fp, mode_Iu);
774         mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
775         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_SP]);
776
777         // 1. restore fp
778         load = new_rd_mips_load_r(dbg, irg, block, *mem, sp, mode_T);
779         attr = get_mips_attr(load);
780         attr->modes.load_store_mode = mode_Iu;
781         // sp is at the fp address already, so we have to do fp_save_offset - initial_frame_size
782         attr->tv = new_tarval_from_long(fp_save_offset - initial_frame_size, mode_Is);
783
784         fp = new_r_Proj(irg, block, load, mode_Iu, pn_Load_res);
785         mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
786         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
787
788         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
789         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
790 }
791
792 /**
793  * Produces the type which sits between the stack args and the locals on the stack.
794  * it will contain the return address and space to store the old frame pointer.
795  * @return The Firm type modelling the ABI between type.
796  */
797 static ir_type *mips_abi_get_between_type(void *self) {
798         mips_abi_env_t *env = self;
799
800         static ir_type *debug_between_type = NULL;
801         static ir_type *opt_between_type = NULL;
802         static ir_entity *old_fp_ent    = NULL;
803
804         if(env->debug && debug_between_type == NULL) {
805                 ir_entity *a0_ent, *a1_ent, *a2_ent, *a3_ent;
806                 ir_entity *ret_addr_ent;
807                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
808                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
809                 ir_type *old_param_type = new_type_primitive(new_id_from_str("param"), mode_Iu);
810
811                 debug_between_type     = new_type_class(new_id_from_str("mips_between_type"));
812                 a0_ent                             = new_entity(debug_between_type, new_id_from_str("a0_ent"), old_param_type);
813                 a1_ent                             = new_entity(debug_between_type, new_id_from_str("a1_ent"), old_param_type);
814                 a2_ent                             = new_entity(debug_between_type, new_id_from_str("a2_ent"), old_param_type);
815                 a3_ent                             = new_entity(debug_between_type, new_id_from_str("a3_ent"), old_param_type);
816                 old_fp_ent             = new_entity(debug_between_type, new_id_from_str("old_fp"), old_fp_type);
817                 ret_addr_ent           = new_entity(debug_between_type, new_id_from_str("ret_addr"), ret_addr_type);
818
819                 set_entity_offset(a0_ent, 0);
820                 set_entity_offset(a1_ent, 4);
821                 set_entity_offset(a2_ent, 8);
822                 set_entity_offset(a3_ent, 12);
823                 set_entity_offset(old_fp_ent, 16);
824                 set_entity_offset(ret_addr_ent, 20);
825
826                 set_type_size_bytes(debug_between_type, 24);
827         } else if(!env->debug && opt_between_type == NULL) {
828                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
829                 ir_entity *old_fp_ent;
830
831                 opt_between_type       = new_type_class(new_id_from_str("mips_between_type"));
832                 old_fp_ent             = new_entity(opt_between_type, new_id_from_str("old_fp"), old_fp_type);
833                 set_entity_offset(old_fp_ent, 0);
834                 set_type_size_bytes(opt_between_type, 4);
835         }
836
837         return env->debug ? debug_between_type : opt_between_type;
838 }
839
840 static const be_abi_callbacks_t mips_abi_callbacks = {
841         mips_abi_init,
842         free,
843         mips_abi_get_between_type,
844         mips_abi_dont_save_regs,
845         mips_abi_prologue,
846         mips_abi_epilogue,
847 };
848
849 /**
850  * Get the ABI restrictions for procedure calls.
851  * @param self        The this pointer.
852  * @param method_type The type of the method (procedure) in question.
853  * @param abi         The abi object to be modified
854  */
855 static void mips_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
856         ir_type  *tp;
857         ir_mode  *mode;
858         int       n = get_method_n_params(method_type);
859         int result_count;
860         int       i;
861         ir_mode **modes;
862         const arch_register_t *reg;
863         be_abi_call_flags_t call_flags;
864
865         memset(&call_flags, 0, sizeof(call_flags));
866         call_flags.bits.left_to_right         = 0;
867         call_flags.bits.store_args_sequential = 0;
868         call_flags.bits.try_omit_fp           = 1;
869         call_flags.bits.fp_free               = 0;
870         call_flags.bits.call_has_imm          = 1;
871
872         /* set stack parameter passing style */
873         be_abi_call_set_flags(abi, call_flags, &mips_abi_callbacks);
874
875         /* collect the mode for each type */
876         modes = alloca(n * sizeof(modes[0]));
877         for (i = 0; i < n; i++) {
878                 tp       = get_method_param_type(method_type, i);
879                 modes[i] = get_type_mode(tp);
880         }
881
882         // assigns parameters to registers or stack
883         for (i = 0; i < n; i++) {
884                 // first 4 params in $a0-$a3, the others on the stack
885                 if(i < 4) {
886                         reg = &mips_gp_regs[REG_A0 + i];
887                         be_abi_call_param_reg(abi, i, reg);
888                 } else {
889                         /* default: all parameters on stack */
890                         be_abi_call_param_stack(abi, i, 4, 0, 0);
891                 }
892         }
893
894         /* set return register */
895         /* default: return value is in R0 (and maybe R1) */
896         result_count = get_method_n_ress(method_type);
897         assert(result_count <= 2 && "More than 2 result values not supported");
898         for(i = 0; i < result_count; ++i) {
899                 const arch_register_t* reg;
900                 tp   = get_method_res_type(method_type, i);
901                 mode = get_type_mode(tp);
902                 ASSERT_NO_FLOAT(mode);
903
904                 reg = &mips_gp_regs[REG_V0 + i];
905                 be_abi_call_res_reg(abi, i, reg);
906         }
907 }
908
909 static const void *mips_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
910         return &mips_irn_ops;
911 }
912
913 const arch_irn_handler_t mips_irn_handler = {
914         mips_get_irn_ops
915 };
916
917 const arch_irn_handler_t *mips_get_irn_handler(const void *self) {
918         return &mips_irn_handler;
919 }
920
921 /**
922  * Initializes the code generator interface.
923  */
924 static const arch_code_generator_if_t *mips_get_code_generator_if(void *self) {
925         return &mips_code_gen_if;
926 }
927
928 /**
929  * Returns the necessary byte alignment for storing a register of given class.
930  */
931 static int mips_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
932         ir_mode *mode = arch_register_class_mode(cls);
933         return get_mode_size_bytes(mode);
934 }
935
936 static const be_execution_unit_t ***mips_get_allowed_execution_units(const void *self, const ir_node *irn) {
937         /* TODO */
938         assert(0);
939         return NULL;
940 }
941
942 static const be_machine_t *mips_get_machine(const void *self) {
943         /* TODO */
944         assert(0);
945         return NULL;
946 }
947
948 /**
949  * Return irp irgs in the desired order.
950  */
951 static ir_graph **mips_get_irg_list(const void *self, ir_graph ***irg_list) {
952         return NULL;
953 }
954
955 /**
956  * Returns the libFirm configuration parameter for this backend.
957  */
958 static const backend_params *mips_get_libfirm_params(void) {
959         static arch_dep_params_t ad = {
960                 1,  /* allow subs */
961                 0,      /* Muls are fast enough on Mips */
962                 31, /* shift would be ok */
963                 0,  /* no Mulhs */
964                 0,  /* no Mulhu */
965                 32, /* Mulhs & Mulhu available for 32 bit */
966         };
967         static backend_params p = {
968                 NULL,  /* no additional opcodes */
969                 NULL,  /* will be set later */
970                 1,     /* need dword lowering */
971                 NULL,  /* but yet no creator function */
972                 NULL,  /* context for create_intrinsic_fkt */
973         };
974
975         p.dep_param = &ad;
976         return &p;
977 }
978
979 const arch_isa_if_t mips_isa_if = {
980         mips_init,
981         mips_done,
982         mips_get_n_reg_class,
983         mips_get_reg_class,
984         mips_get_reg_class_for_mode,
985         mips_get_call_abi,
986         mips_get_irn_handler,
987         mips_get_code_generator_if,
988         mips_get_list_sched_selector,
989         mips_get_ilp_sched_selector,
990         mips_get_reg_class_alignment,
991         mips_get_libfirm_params,
992         mips_get_allowed_execution_units,
993         mips_get_machine,
994         mips_get_irg_list,
995 };
996
997 void be_init_arch_mips(void)
998 {
999         be_register_isa_if("mips", &mips_isa_if);
1000 }
1001
1002 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_mips);