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