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