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