added context parameter for create_intrinsic_fkt
[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_REGISTER(firm_dbg_module_t *mod, 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_gp.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         NULL
262 };
263
264 mips_irn_ops_t mips_irn_ops = {
265         &mips_irn_ops_if,
266         NULL
267 };
268
269
270
271 /**************************************************
272  *                _                         _  __
273  *               | |                       (_)/ _|
274  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
275  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
276  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
277  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
278  *                        __/ |
279  *                       |___/
280  **************************************************/
281
282
283 typedef struct {
284         ir_node *start;
285         ir_node *end;
286         unsigned cnt;
287 } anchor;
288
289 /**
290  * Ext-Block walker: create a block schedule
291  */
292 static void create_block_list(ir_extblk *blk, void *env) {
293         anchor *list = env;
294         int i, n;
295
296         for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
297                 ir_node *block = get_extbb_block(blk, i);
298
299                 set_irn_link(block, NULL);
300                 if (list->start)
301                         set_irn_link(list->end, block);
302                 else
303                         list->start = block;
304
305                 list->end = block;
306                 list->cnt += 1;
307         }
308 }
309
310 /* return the scheduled block at position pos */
311 ir_node *mips_get_sched_block(const mips_code_gen_t *cg, int pos) {
312         if (0 <= pos && pos < ARR_LEN(cg->bl_list))
313                 return cg->bl_list[pos];
314         return NULL;
315 }
316
317 /* return the number of scheduled blocks */
318 int mips_get_sched_n_blocks(const mips_code_gen_t *cg) {
319         return ARR_LEN(cg->bl_list);
320 }
321
322 /* set a block schedule number */
323 void mips_set_block_sched_nr(ir_node *block, int nr) {
324         set_irn_link(block, INT_TO_PTR(nr));
325 }
326
327 /* get a block schedule number */
328 int mips_get_block_sched_nr(ir_node *block) {
329         return PTR_TO_INT(get_irn_link(block));
330 }
331
332 /**
333  * Creates a block schedule for the given graph.
334  */
335 static void mips_create_block_sched(mips_code_gen_t *cg) {
336         anchor list;
337         ir_node **bl_list, *block;
338         unsigned i;
339
340         if (cg->bl_list) {
341                 DEL_ARR_F(cg->bl_list);
342                 free_survive_dce(cg->bl_list_sdce);
343         }
344
345         /* calculate the block schedule here */
346         compute_extbb(cg->irg);
347
348         list.start = NULL;
349         list.end   = NULL;
350         list.cnt   = 0;
351         irg_extblock_walk_graph(cg->irg, NULL, create_block_list, &list);
352
353
354         bl_list = NEW_ARR_F(ir_node *, list.cnt);
355         cg->bl_list_sdce = new_survive_dce();
356         for (i = 0, block = list.start; block; block = get_irn_link(block)) {
357                 bl_list[i] = block;
358                 survive_dce_register_irn(cg->bl_list_sdce, &bl_list[i]);
359                 i++;
360         }
361
362         cg->bl_list = bl_list;
363 }
364
365 typedef struct _wenv_t {
366         ir_node *list;
367 } wenv_t;
368
369 /**
370  * Walker: link all CopyB nodes
371  */
372 static void collect_copyb_nodes(ir_node *node, void *env) {
373         wenv_t *wenv = env;
374
375         if (get_irn_op(node) == op_CopyB) {
376                 set_irn_link(node, wenv->list);
377                 wenv->list = node;
378         }
379 }
380
381 static void replace_copyb_nodes(mips_code_gen_t *cg) {
382         wenv_t env;
383         ir_node *copy, *next;
384         ir_node *old_bl, *new_bl, *jmp, *new_jmp, *mem;
385         const ir_edge_t *edge;
386
387         /* build code for all copyB */
388         env.list = NULL;
389         irg_walk_graph(cg->irg, NULL, collect_copyb_nodes, &env);
390
391         for (copy = env.list; copy; copy = next) {
392                 next = get_irn_link(copy);
393
394                 old_bl = get_nodes_block(copy);
395                 part_block(copy);
396                 jmp     = get_Block_cfgpred(old_bl, 0);
397                 new_jmp = new_r_Jmp(cg->irg, get_nodes_block(copy));
398
399                 new_bl = new_r_Block(cg->irg, 1, &new_jmp);
400                 set_nodes_block(jmp, new_bl);
401
402                 mem = gen_code_for_CopyB(new_bl, copy);
403
404                 /* fix copyB's out edges */
405                 foreach_out_edge(copy, edge) {
406                         ir_node *succ = get_edge_src_irn(edge);
407
408                         assert(is_Proj(succ));
409                         switch (get_Proj_proj(succ)) {
410                         case pn_CopyB_M_regular:
411                         case pn_CopyB_M_except:
412                                 exchange(succ, mem);
413                                 break;
414                         default:
415                                 exchange(succ, get_irg_bad(cg->irg));
416                         }
417                 }
418         }
419 }
420
421 /**
422  * Transforms the standard firm graph into
423  * a mips firm graph
424  */
425 static void mips_prepare_graph(void *self) {
426         mips_code_gen_t *cg = self;
427         int bl_nr, n;
428
429         // replace all copyb nodes in the block with a loop
430         // and mips store/load nodes
431         replace_copyb_nodes(cg);
432
433         // Calculate block schedule
434         mips_create_block_sched(cg);
435
436         /* enter the block number into every blocks link field */
437         for (bl_nr = 0, n = mips_get_sched_n_blocks(cg); bl_nr < n; ++bl_nr) {
438                 ir_node *bl = mips_get_sched_block(cg, bl_nr);
439                 mips_set_block_sched_nr(bl, bl_nr);
440         }
441
442         // walk the graph and transform firm nodes into mips nodes where possible
443         irg_walk_blkwise_graph(cg->irg, mips_pre_transform_node, mips_transform_node, cg);
444
445         dump_ir_block_graph_sched(cg->irg, "-transformed");
446 }
447
448 /**
449  * Called immediately before emit phase.
450  */
451 static void mips_finish_irg(ir_graph *irg, mips_code_gen_t *cg) {
452         /* TODO: - fix offsets for nodes accessing stack
453                          - ...
454         */
455 }
456
457
458 /**
459  * These are some hooks which must be filled but are probably not needed.
460  */
461 static void mips_before_sched(void *self) {
462         /* Some stuff you need to do after scheduling but before register allocation */
463 }
464
465 static void mips_before_ra(void *self) {
466         /* Some stuff you need to do immediately after register allocation */
467 }
468
469 static void mips_after_ra(void* self) {
470         mips_code_gen_t *cg = self;
471         irg_walk_blkwise_graph(cg->irg, NULL, mips_after_ra_walker, self);
472 }
473
474 /**
475  * Emits the code, closes the output file and frees
476  * the code generator interface.
477  */
478 static void mips_emit_and_done(void *self) {
479         mips_code_gen_t *cg = self;
480         ir_graph           *irg = cg->irg;
481         FILE               *out = cg->isa->out;
482
483         mips_register_emitters();
484
485         if (cg->emit_decls) {
486                 mips_gen_decls(out);
487                 cg->emit_decls = 0;
488         }
489
490         mips_finish_irg(irg, cg);
491         dump_ir_block_graph_sched(irg, "-mips-finished");
492         mips_gen_routine(out, irg, cg);
493
494         cur_reg_set = NULL;
495
496         /* de-allocate code generator */
497         del_set(cg->reg_set);
498         if (cg->bl_list) {
499                 DEL_ARR_F(cg->bl_list);
500                 free_survive_dce(cg->bl_list_sdce);
501         }
502         free(cg);
503 }
504
505 static void *mips_cg_init(const be_irg_t *birg);
506
507 static const arch_code_generator_if_t mips_code_gen_if = {
508         mips_cg_init,
509         NULL,                /* before abi introduce */
510         mips_prepare_graph,
511         mips_before_sched,   /* before scheduling hook */
512         mips_before_ra,      /* before register allocation hook */
513         mips_after_ra,
514         mips_emit_and_done
515 };
516
517 /**
518  * Initializes the code generator.
519  */
520 static void *mips_cg_init(const be_irg_t *birg) {
521         mips_isa_t      *isa = (mips_isa_t *)birg->main_env->arch_env->isa;
522         mips_code_gen_t *cg  = xmalloc(sizeof(*cg));
523
524         cg->impl     = &mips_code_gen_if;
525         cg->irg      = birg->irg;
526         cg->reg_set  = new_set(mips_cmp_irn_reg_assoc, 1024);
527         cg->arch_env = birg->main_env->arch_env;
528         cg->isa      = isa;
529         cg->birg     = birg;
530         cg->bl_list  = NULL;
531         FIRM_DBG_REGISTER(cg->mod, "firm.be.mips.cg");
532
533         isa->num_codegens++;
534
535         if (isa->num_codegens > 1)
536                 cg->emit_decls = 0;
537         else
538                 cg->emit_decls = 1;
539
540         cur_reg_set = cg->reg_set;
541
542         mips_irn_ops.cg = cg;
543
544         return (arch_code_generator_t *)cg;
545 }
546
547
548 /*****************************************************************
549  *  ____             _                  _   _____  _____
550  * |  _ \           | |                | | |_   _|/ ____|  /\
551  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
552  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
553  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
554  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
555  *
556  *****************************************************************/
557
558 static mips_isa_t mips_isa_template = {
559         &mips_isa_if,
560         &mips_gp_regs[REG_SP],
561         &mips_gp_regs[REG_FP],
562         -1,             // stack direction
563         0,              // num codegens?!? TODO what is this?
564         NULL
565 };
566
567 /**
568  * Initializes the backend ISA and opens the output file.
569  */
570 static void *mips_init(FILE *file_handle) {
571         static int inited = 0;
572         mips_isa_t *isa;
573
574         if(inited)
575                 return NULL;
576
577         isa = xcalloc(1, sizeof(*isa));
578         memcpy(isa, &mips_isa_template, sizeof(*isa));
579
580         isa->out = file_handle;
581
582         mips_register_init(isa);
583         mips_create_opcodes();
584         mips_init_opcode_transforms();
585
586         inited = 1;
587
588         return isa;
589 }
590
591 /**
592  * Closes the output file and frees the ISA structure.
593  */
594 static void mips_done(void *self) {
595         free(self);
596 }
597
598 static int mips_get_n_reg_class(const void *self) {
599         return N_CLASSES;
600 }
601
602 static const arch_register_class_t *mips_get_reg_class(const void *self, int i) {
603         assert(i >= 0 && i < N_CLASSES && "Invalid mips register class requested.");
604         return &mips_reg_classes[i];
605 }
606
607
608
609 /**
610  * Get the register class which shall be used to store a value of a given mode.
611  * @param self The this pointer.
612  * @param mode The mode in question.
613  * @return A register class which can hold values of the given mode.
614  */
615 const arch_register_class_t *mips_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
616         ASSERT_NO_FLOAT(mode);
617         return &mips_reg_classes[CLASS_mips_gp];
618 }
619
620 typedef struct {
621         be_abi_call_flags_bits_t flags;
622         const mips_isa_t *isa;
623         const arch_env_t *arch_env;
624         ir_graph *irg;
625         // do special handling to support debuggers
626         int debug;
627 } mips_abi_env_t;
628
629 static void *mips_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
630 {
631         mips_abi_env_t *env    = xmalloc(sizeof(env[0]));
632         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
633         env->flags = fl.bits;
634         env->irg   = irg;
635         env->arch_env = arch_env;
636         env->isa   = (const mips_isa_t*) arch_env->isa;
637         env->debug = 1;
638         return env;
639 }
640
641 static void mips_abi_dont_save_regs(void *self, pset *s)
642 {
643         mips_abi_env_t *env = self;
644         if(env->flags.try_omit_fp)
645                 pset_insert_ptr(s, env->isa->fp);
646 }
647
648 static const arch_register_t *mips_abi_prologue(void *self, ir_node** mem, pmap *reg_map)
649 {
650         mips_abi_env_t *env = self;
651         ir_graph *irg = env->irg;
652         dbg_info *dbg = NULL; // TODO where can I get this from?
653         ir_node *block = get_irg_start_block(env->irg);
654         mips_attr_t *attr;
655         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
656         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
657         int initialstackframesize;
658
659         if(env->debug) {
660                 /*
661                  * The calling conventions wants a stack frame of at least 24bytes size with
662                  *   a0-a3 saved in offset 0-12
663                  *   fp saved in offset 16
664                  *   ra saved in offset 20
665                  */
666                 ir_node *mm[6];
667                 ir_node *sync, *reg, *store;
668                 initialstackframesize = 24;
669
670                 // - setup first part of stackframe
671                 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
672                 attr = get_mips_attr(sp);
673                 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
674                 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
675                 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
676
677                 /* TODO: where to get an edge with a0-a3
678                 int i;
679                 for(i = 0; i < 4; ++i) {
680                         ir_node *reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_A0 + i]);
681                         ir_node *store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
682                         attr = get_mips_attr(store);
683                         attr->load_store_mode = mode_Iu;
684                         attr->tv = new_tarval_from_long(i * 4, mode_Is);
685
686                         mm[i] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
687                 }
688                 */
689
690                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
691                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
692                 attr = get_mips_attr(store);
693                 attr->modes.load_store_mode = mode_Iu;
694                 attr->tv = new_tarval_from_long(16, mode_Is);
695
696                 mm[4] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
697
698                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_RA]);
699                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
700                 attr = get_mips_attr(store);
701                 attr->modes.load_store_mode = mode_Iu;
702                 attr->tv = new_tarval_from_long(20, mode_Is);
703
704                 mm[5] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
705
706                 // TODO ideally we would route these mem edges directly towards the epilogue
707                 sync = new_r_Sync(irg, block, 2, mm+4);
708                 *mem = sync;
709         } else {
710                 ir_node *reg, *store;
711                 initialstackframesize = 4;
712
713                 // save old framepointer
714                 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
715                 attr = get_mips_attr(sp);
716                 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
717                 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
718                 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
719
720                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
721                 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
722                 attr = get_mips_attr(store);
723                 attr->modes.load_store_mode = mode_Iu;
724                 attr->tv = new_tarval_from_long(0, mode_Is);
725
726                 *mem = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
727         }
728
729         // setup framepointer
730         fp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
731         attr = get_mips_attr(fp);
732         attr->tv = new_tarval_from_long(initialstackframesize, mode_Is);
733         mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
734         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
735
736         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
737         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
738
739         return &mips_gp_regs[REG_SP];
740 }
741
742 static void mips_abi_epilogue(void *self, ir_node *block, ir_node **mem, pmap *reg_map)
743 {
744         mips_abi_env_t *env = self;
745         ir_graph *irg = env->irg;
746         dbg_info *dbg = NULL; // TODO where can I get this from?
747         mips_attr_t *attr;
748         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
749         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
750         ir_node *load;
751         int initial_frame_size = env->debug ? 24 : 4;
752         int fp_save_offset = env->debug ? 16 : 0;
753
754         // restore sp
755         //sp = be_new_IncSP(&mips_gp_regs[REG_SP], irg, block, sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_against);
756
757         // copy fp to sp
758         sp = new_rd_mips_move(dbg, irg, block, fp, mode_Iu);
759         mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
760         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_SP]);
761
762         // 1. restore fp
763         load = new_rd_mips_load_r(dbg, irg, block, *mem, sp, mode_T);
764         attr = get_mips_attr(load);
765         attr->modes.load_store_mode = mode_Iu;
766         // sp is at the fp address already, so we have to do fp_save_offset - initial_frame_size
767         attr->tv = new_tarval_from_long(fp_save_offset - initial_frame_size, mode_Is);
768
769         fp = new_r_Proj(irg, block, load, mode_Iu, pn_Load_res);
770         mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
771         //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
772
773         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
774         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
775 }
776
777 /**
778  * Produces the type which sits between the stack args and the locals on the stack.
779  * it will contain the return address and space to store the old frame pointer.
780  * @return The Firm type modelling the ABI between type.
781  */
782 static ir_type *mips_abi_get_between_type(void *self) {
783         mips_abi_env_t *env = self;
784
785         static ir_type *debug_between_type = NULL;
786         static ir_type *opt_between_type = NULL;
787         static entity *old_fp_ent    = NULL;
788
789         if(env->debug && debug_between_type == NULL) {
790                 entity *a0_ent, *a1_ent, *a2_ent, *a3_ent;
791                 entity *ret_addr_ent;
792                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
793                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
794                 ir_type *old_param_type = new_type_primitive(new_id_from_str("param"), mode_Iu);
795
796                 debug_between_type     = new_type_class(new_id_from_str("mips_between_type"));
797                 a0_ent                             = new_entity(debug_between_type, new_id_from_str("a0_ent"), old_param_type);
798                 a1_ent                             = new_entity(debug_between_type, new_id_from_str("a1_ent"), old_param_type);
799                 a2_ent                             = new_entity(debug_between_type, new_id_from_str("a2_ent"), old_param_type);
800                 a3_ent                             = new_entity(debug_between_type, new_id_from_str("a3_ent"), old_param_type);
801                 old_fp_ent             = new_entity(debug_between_type, new_id_from_str("old_fp"), old_fp_type);
802                 ret_addr_ent           = new_entity(debug_between_type, new_id_from_str("ret_addr"), ret_addr_type);
803
804                 set_entity_offset_bytes(a0_ent, 0);
805                 set_entity_offset_bytes(a1_ent, 4);
806                 set_entity_offset_bytes(a2_ent, 8);
807                 set_entity_offset_bytes(a3_ent, 12);
808                 set_entity_offset_bytes(old_fp_ent, 16);
809                 set_entity_offset_bytes(ret_addr_ent, 20);
810
811                 set_type_size_bytes(debug_between_type, 24);
812         } else if(!env->debug && opt_between_type == NULL) {
813                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
814                 entity *old_fp_ent;
815
816                 opt_between_type       = new_type_class(new_id_from_str("mips_between_type"));
817                 old_fp_ent             = new_entity(opt_between_type, new_id_from_str("old_fp"), old_fp_type);
818                 set_entity_offset_bytes(old_fp_ent, 0);
819                 set_type_size_bytes(opt_between_type, 4);
820         }
821
822         return env->debug ? debug_between_type : opt_between_type;
823 }
824
825 static const be_abi_callbacks_t mips_abi_callbacks = {
826         mips_abi_init,
827         free,
828         mips_abi_get_between_type,
829         mips_abi_dont_save_regs,
830         mips_abi_prologue,
831         mips_abi_epilogue,
832 };
833
834 /**
835  * Get the ABI restrictions for procedure calls.
836  * @param self        The this pointer.
837  * @param method_type The type of the method (procedure) in question.
838  * @param abi         The abi object to be modified
839  */
840 static void mips_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
841         ir_type  *tp;
842         ir_mode  *mode;
843         int       n = get_method_n_params(method_type);
844         int result_count;
845         int       i;
846         ir_mode **modes;
847         const arch_register_t *reg;
848         be_abi_call_flags_t call_flags;
849
850         memset(&call_flags, 0, sizeof(call_flags));
851         call_flags.bits.left_to_right         = 0;
852         call_flags.bits.store_args_sequential = 0;
853         call_flags.bits.try_omit_fp           = 1;
854         call_flags.bits.fp_free               = 0;
855         call_flags.bits.call_has_imm          = 1;
856
857         /* set stack parameter passing style */
858         be_abi_call_set_flags(abi, call_flags, &mips_abi_callbacks);
859
860         /* collect the mode for each type */
861         modes = alloca(n * sizeof(modes[0]));
862         for (i = 0; i < n; i++) {
863                 tp       = get_method_param_type(method_type, i);
864                 modes[i] = get_type_mode(tp);
865         }
866
867         // assigns parameters to registers or stack
868         for (i = 0; i < n; i++) {
869                 // first 4 params in $a0-$a3, the others on the stack
870                 if(i < 4) {
871                         reg = &mips_gp_regs[REG_A0 + i];
872                         be_abi_call_param_reg(abi, i, reg);
873                 } else {
874                         /* default: all parameters on stack */
875                         be_abi_call_param_stack(abi, i, 4, 0, 0);
876                 }
877         }
878
879         /* set return register */
880         /* default: return value is in R0 (and maybe R1) */
881         result_count = get_method_n_ress(method_type);
882         assert(result_count <= 2 && "More than 2 result values not supported");
883         for(i = 0; i < result_count; ++i) {
884                 const arch_register_t* reg;
885                 tp   = get_method_res_type(method_type, i);
886                 mode = get_type_mode(tp);
887                 ASSERT_NO_FLOAT(mode);
888
889                 reg = &mips_gp_regs[REG_V0 + i];
890                 be_abi_call_res_reg(abi, i, reg);
891         }
892 }
893
894 static const void *mips_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
895         return &mips_irn_ops;
896 }
897
898 const arch_irn_handler_t mips_irn_handler = {
899         mips_get_irn_ops
900 };
901
902 const arch_irn_handler_t *mips_get_irn_handler(const void *self) {
903         return &mips_irn_handler;
904 }
905
906 /**
907  * Initializes the code generator interface.
908  */
909 static const arch_code_generator_if_t *mips_get_code_generator_if(void *self) {
910         return &mips_code_gen_if;
911 }
912
913 /**
914  * Returns the necessary byte alignment for storing a register of given class.
915  */
916 static int mips_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
917         ir_mode *mode = arch_register_class_mode(cls);
918         return get_mode_size_bytes(mode);
919 }
920
921 /**
922  * Returns the libFirm configuration parameter for this backend.
923  */
924 static const backend_params *mips_get_libfirm_params(void) {
925         static arch_dep_params_t ad = {
926                 1,  /* allow subs */
927                 0,      /* Muls are fast enough on Mips */
928                 31, /* shift would be ok */
929                 0,  /* no Mulhs */
930                 0,  /* no Mulhu */
931                 32, /* Mulhs & Mulhu available for 32 bit */
932         };
933         static backend_params p = {
934                 NULL,  /* no additional opcodes */
935                 NULL,  /* will be set later */
936                 1,     /* need dword lowering */
937                 NULL,  /* but yet no creator function */
938                 NULL,  /* context for create_intrinsic_fkt */
939         };
940
941         p.dep_param = &ad;
942         return &p;
943 }
944
945 #ifdef WITH_LIBCORE
946 static void mips_register_options(lc_opt_entry_t *ent)
947 {
948 }
949 #endif /* WITH_LIBCORE */
950
951 const arch_isa_if_t mips_isa_if = {
952         mips_init,
953         mips_done,
954         mips_get_n_reg_class,
955         mips_get_reg_class,
956         mips_get_reg_class_for_mode,
957         mips_get_call_abi,
958         mips_get_irn_handler,
959         mips_get_code_generator_if,
960         mips_get_list_sched_selector,
961         mips_get_reg_class_alignment,
962         mips_get_libfirm_params,
963 #ifdef WITH_LIBCORE
964         mips_register_options
965 #endif
966 };