70fa32d78882b0f07fc61b728d1dd75f85f9249a
[libfirm] / ir / be / mips / bearch_mips.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
27
28 #include "pseudo_irg.h"
29 #include "irgwalk.h"
30 #include "irprog.h"
31 #include "irprintf.h"
32 #include "ircons.h"
33 #include "irgmod.h"
34 #include "irgopt.h"
35 #include "irgwalk.h"
36 #include "iredges.h"
37 #include "irdump.h"
38 #include "irextbb.h"
39 #include "irtools.h"
40 #include "error.h"
41
42 #include "bitset.h"
43 #include "debug.h"
44
45 #include "../bearch_t.h"
46 #include "../benode_t.h"
47 #include "../belower.h"
48 #include "../besched_t.h"
49 #include "../beblocksched.h"
50 #include "../beirg_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 ir_node *node, int pos)
92 {
93         long               node_pos = pos == -1 ? 0 : pos;
94         ir_mode           *mode     = get_irn_mode(node);
95
96         if (is_Block(node) || mode == mode_X || mode == mode_M) {
97                 return arch_no_register_req;
98         }
99
100         if (mode == mode_T && pos < 0) {
101                 return arch_no_register_req;
102         }
103
104         if (is_Proj(node)) {
105                 /* in case of a proj, we need to get the correct OUT slot */
106                 /* of the node corresponding to the proj number */
107                 if (pos == -1) {
108                         node_pos = mips_translate_proj_pos(node);
109                 }
110                 else {
111                         node_pos = pos;
112                 }
113
114                 node = skip_Proj_const(node);
115         }
116
117         /* get requirements for our own nodes */
118         if (is_mips_irn(node)) {
119                 const arch_register_req_t *req;
120                 if (pos >= 0) {
121                         req = get_mips_in_req(node, pos);
122                 } else {
123                         req = get_mips_out_req(node, node_pos);
124                 }
125
126                 return req;
127         }
128
129         /* unknown should be translated by now */
130         assert(!is_Unknown(node));
131
132         return arch_no_register_req;
133 }
134
135 static void mips_set_irn_reg(ir_node *irn, const arch_register_t *reg)
136 {
137         int pos = 0;
138
139         if (is_Proj(irn)) {
140
141                 if (get_irn_mode(irn) == mode_X) {
142                         return;
143                 }
144
145                 pos = mips_translate_proj_pos(irn);
146                 irn = skip_Proj(irn);
147         }
148
149         if (is_mips_irn(irn)) {
150                 const arch_register_t **slots;
151
152                 slots      = get_mips_slots(irn);
153                 slots[pos] = reg;
154         } else {
155                 /* here we set the registers for the Phi nodes */
156                 mips_set_firm_reg(irn, reg, cur_reg_set);
157         }
158 }
159
160 static const arch_register_t *mips_get_irn_reg(const ir_node *irn)
161 {
162         int pos = 0;
163         const arch_register_t *reg = NULL;
164
165         if (is_Proj(irn)) {
166
167                 if (get_irn_mode(irn) == mode_X) {
168                         return NULL;
169                 }
170
171                 pos = mips_translate_proj_pos(irn);
172                 irn = skip_Proj_const(irn);
173         }
174
175         if (is_mips_irn(irn)) {
176                 const arch_register_t **slots;
177                 slots = get_mips_slots(irn);
178                 reg   = slots[pos];
179         }
180         else {
181                 reg = mips_get_firm_reg(irn, cur_reg_set);
182         }
183
184         return reg;
185 }
186
187 static arch_irn_class_t mips_classify(const ir_node *irn)
188 {
189         irn = skip_Proj_const(irn);
190
191         if (is_cfop(irn)) {
192                 return arch_irn_class_branch;
193         }
194
195         return 0;
196 }
197
198 static arch_irn_flags_t mips_get_flags(const ir_node *irn)
199 {
200         irn = skip_Proj_const(irn);
201
202         if (!is_mips_irn(irn))
203                 return 0;
204
205         return get_mips_flags(irn);
206 }
207
208 int mips_is_Load(const ir_node *node)
209 {
210         return is_mips_lw(node) || is_mips_lh(node) || is_mips_lhu(node) ||
211                 is_mips_lb(node) || is_mips_lbu(node);
212 }
213
214 int mips_is_Store(const ir_node *node)
215 {
216         return is_mips_sw(node) || is_mips_sh(node) || is_mips_sb(node);
217 }
218
219 static ir_entity *mips_get_frame_entity(const ir_node *node)
220 {
221         const mips_load_store_attr_t *attr;
222
223         if(!is_mips_irn(node))
224                 return NULL;
225         if(!mips_is_Load(node) && !mips_is_Store(node))
226                 return NULL;
227
228         attr = get_mips_load_store_attr_const(node);
229         return attr->stack_entity;
230 }
231
232 static void mips_set_frame_entity(ir_node *node, ir_entity *entity)
233 {
234         mips_load_store_attr_t *attr;
235
236         if(!is_mips_irn(node)) {
237                 panic("trying to set frame entity on non load/store node %+F", node);
238         }
239         if(!mips_is_Load(node) && !mips_is_Store(node)) {
240                 panic("trying to set frame entity on non load/store node %+F", node);
241         }
242
243         attr = get_irn_generic_attr(node);
244         attr->stack_entity = entity;
245 }
246
247 /**
248  * This function is called by the generic backend to correct offsets for
249  * nodes accessing the stack.
250  */
251 static void mips_set_frame_offset(ir_node *node, int offset)
252 {
253         mips_load_store_attr_t *attr;
254
255         if(!is_mips_irn(node)) {
256                 panic("trying to set frame offset on non load/store node %+F", node);
257         }
258         if(!mips_is_Load(node) && !mips_is_Store(node)) {
259                 panic("trying to set frame offset on non load/store node %+F", node);
260         }
261
262         attr = get_irn_generic_attr(node);
263         attr->offset += offset;
264
265         if(attr->offset < -32768 || attr->offset > 32767) {
266                 panic("Out of stack space! (mips supports only 16bit offsets)");
267         }
268 }
269
270 static int mips_get_sp_bias(const ir_node *irn)
271 {
272         (void) irn;
273         return 0;
274 }
275
276 /* fill register allocator interface */
277
278 static const arch_irn_ops_t mips_irn_ops = {
279         mips_get_irn_reg_req,
280         mips_set_irn_reg,
281         mips_get_irn_reg,
282         mips_classify,
283         mips_get_flags,
284         mips_get_frame_entity,
285         mips_set_frame_entity,
286         mips_set_frame_offset,
287         mips_get_sp_bias,
288         NULL,    /* get_inverse             */
289         NULL,    /* get_op_estimated_cost   */
290         NULL,    /* possible_memory_operand */
291         NULL,    /* perform_memory_operand  */
292 };
293
294 /**************************************************
295  *                _                         _  __
296  *               | |                       (_)/ _|
297  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
298  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
299  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
300  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
301  *                        __/ |
302  *                       |___/
303  **************************************************/
304
305 /**
306  * Transforms the standard firm graph into
307  * a mips firm graph
308  */
309 static void mips_prepare_graph(void *self) {
310         mips_code_gen_t *cg = self;
311
312         /* do local optimizations */
313         optimize_graph_df(cg->irg);
314
315         /* TODO: we often have dead code reachable through out-edges here. So for
316          * now we rebuild edges (as we need correct user count for code selection)
317          */
318 #if 1
319         edges_deactivate(cg->irg);
320         edges_activate(cg->irg);
321 #endif
322
323         // walk the graph and transform firm nodes into mips nodes where possible
324         mips_transform_graph(cg);
325         dump_ir_block_graph_sched(cg->irg, "-transformed");
326
327         /* do local optimizations (mainly CSE) */
328         optimize_graph_df(cg->irg);
329
330         /* do code placement, to optimize the position of constants */
331         place_code(cg->irg);
332
333         be_dump(cg->irg, "-place", dump_ir_block_graph_sched);
334 }
335
336 /**
337  * Called immediately before emit phase.
338  */
339 static void mips_finish_irg(void *self) {
340         mips_code_gen_t *cg = self;
341         ir_graph        *irg = cg->irg;
342
343         /* create block schedule, this also removes empty blocks which might
344          * produce critical edges */
345         cg->block_schedule = be_create_block_schedule(irg, cg->birg->exec_freq);
346
347         dump_ir_block_graph_sched(irg, "-mips-finished");
348 }
349
350
351 /**
352  * These are some hooks which must be filled but are probably not needed.
353  */
354 static void mips_before_sched(void *self)
355 {
356         (void) self;
357 }
358
359 static void mips_before_ra(void *self)
360 {
361         (void) self;
362 }
363
364 static void mips_after_ra(void* self)
365 {
366         mips_code_gen_t *cg = self;
367         be_coalesce_spillslots(cg->birg);
368         irg_walk_blkwise_graph(cg->irg, NULL, mips_after_ra_walker, self);
369 }
370
371 /**
372  * Emits the code, closes the output file and frees
373  * the code generator interface.
374  */
375 static void mips_emit_and_done(void *self)
376 {
377         mips_code_gen_t *cg  = self;
378         ir_graph        *irg = cg->irg;
379         (void) self;
380
381         mips_gen_routine(cg, irg);
382
383         cur_reg_set = NULL;
384
385         /* de-allocate code generator */
386         del_set(cg->reg_set);
387         free(cg);
388 }
389
390 static void *mips_cg_init(be_irg_t *birg);
391
392 static const arch_code_generator_if_t mips_code_gen_if = {
393         mips_cg_init,
394         NULL,                /* get_pic_base */
395         NULL,                /* before abi introduce */
396         mips_prepare_graph,
397         NULL,                /* spill */
398         mips_before_sched,   /* before scheduling hook */
399         mips_before_ra,      /* before register allocation hook */
400         mips_after_ra,
401         mips_finish_irg,
402         mips_emit_and_done
403 };
404
405 /**
406  * Initializes the code generator.
407  */
408 static void *mips_cg_init(be_irg_t *birg)
409 {
410         const arch_env_t *arch_env = be_get_birg_arch_env(birg);
411         mips_isa_t       *isa      = (mips_isa_t *) arch_env;
412         mips_code_gen_t  *cg       = XMALLOCZ(mips_code_gen_t);
413
414         cg->impl     = &mips_code_gen_if;
415         cg->irg      = be_get_birg_irg(birg);
416         cg->reg_set  = new_set(mips_cmp_irn_reg_assoc, 1024);
417         cg->isa      = isa;
418         cg->birg     = birg;
419
420         cur_reg_set = cg->reg_set;
421
422         isa->cg = cg;
423
424         return (arch_code_generator_t *)cg;
425 }
426
427
428 /*****************************************************************
429  *  ____             _                  _   _____  _____
430  * |  _ \           | |                | | |_   _|/ ____|  /\
431  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
432  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
433  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
434  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
435  *
436  *****************************************************************/
437
438 static mips_isa_t mips_isa_template = {
439         {
440                 &mips_isa_if,
441                 &mips_gp_regs[REG_SP],
442                 &mips_gp_regs[REG_FP],
443                 -1,             /* stack direction */
444                 2,      /* power of two stack alignment for calls, 2^2 == 4 */
445                 NULL,   /* main environment */
446                 7,      /* spill costs */
447                 5,      /* reload costs */
448         },
449         NULL,          /* cg */
450 };
451
452 /**
453  * Initializes the backend ISA and opens the output file.
454  */
455 static arch_env_t *mips_init(FILE *file_handle) {
456         static int inited = 0;
457         mips_isa_t *isa;
458
459         if(inited)
460                 return NULL;
461         inited = 1;
462
463         isa = XMALLOC(mips_isa_t);
464         memcpy(isa, &mips_isa_template, sizeof(isa[0]));
465
466         be_emit_init(file_handle);
467
468         mips_register_init();
469         mips_create_opcodes(&mips_irn_ops);
470         // mips_init_opcode_transforms();
471
472         /* we mark referenced global entities, so we can only emit those which
473          * are actually referenced. (Note: you mustn't use the type visited flag
474          * elsewhere in the backend)
475          */
476         inc_master_type_visited();
477
478         return &isa->arch_env;
479 }
480
481 /**
482  * Closes the output file and frees the ISA structure.
483  */
484 static void mips_done(void *self)
485 {
486         mips_isa_t *isa = self;
487
488         be_gas_emit_decls(isa->arch_env.main_env, 1);
489
490         be_emit_exit();
491         free(isa);
492 }
493
494 static unsigned mips_get_n_reg_class(const void *self)
495 {
496         (void) self;
497         return N_CLASSES;
498 }
499
500 static const arch_register_class_t *mips_get_reg_class(const void *self,
501                                                        unsigned i)
502 {
503         (void) self;
504         assert(i < N_CLASSES);
505         return &mips_reg_classes[i];
506 }
507
508
509
510 /**
511  * Get the register class which shall be used to store a value of a given mode.
512  * @param self The this pointer.
513  * @param mode The mode in question.
514  * @return A register class which can hold values of the given mode.
515  */
516 const arch_register_class_t *mips_get_reg_class_for_mode(const void *self,
517                                                          const ir_mode *mode)
518 {
519         (void) self;
520         (void) mode;
521         ASSERT_NO_FLOAT(mode);
522         return &mips_reg_classes[CLASS_mips_gp];
523 }
524
525 typedef struct {
526         be_abi_call_flags_bits_t flags;
527         const arch_env_t *arch_env;
528         ir_graph *irg;
529         // do special handling to support debuggers
530         int debug;
531 } mips_abi_env_t;
532
533 static void *mips_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
534 {
535         mips_abi_env_t *env    = XMALLOC(mips_abi_env_t);
536         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
537         env->flags             = fl.bits;
538         env->irg               = irg;
539         env->arch_env          = arch_env;
540         env->debug             = 1;
541         return env;
542 }
543
544 static void mips_abi_dont_save_regs(void *self, pset *s)
545 {
546         mips_abi_env_t *env = self;
547
548         if(env->flags.try_omit_fp)
549                 pset_insert_ptr(s, env->arch_env->bp);
550 }
551
552 static const arch_register_t *mips_abi_prologue(void *self, ir_node** mem, pmap *reg_map, int *stack_bias)
553 {
554         mips_abi_env_t *env = self;
555         ir_graph *irg = env->irg;
556         ir_node *block = get_irg_start_block(env->irg);
557         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
558         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
559         int initialstackframesize;
560
561         (void) stack_bias;
562
563         if (env->debug) {
564                 /*
565                  * The calling conventions wants a stack frame of at least 24bytes size with
566                  *   a0-a3 saved in offset 0-12
567                  *   fp saved in offset 16
568                  *   ra saved in offset 20
569                  */
570                 ir_node *mm[6];
571                 ir_node *sync, *reg, *store;
572                 initialstackframesize = 24;
573
574                 // - setup first part of stackframe
575                 sp = new_rd_mips_addu(NULL, irg, block, sp,
576                                       mips_create_Immediate(initialstackframesize));
577                 mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
578                 set_mips_flags(sp, arch_irn_flags_ignore);
579
580                 /* TODO: where to get an edge with a0-a3
581                 int i;
582                 for(i = 0; i < 4; ++i) {
583                         ir_node *reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_A0 + i]);
584                         ir_node *store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
585                         attr = get_mips_attr(store);
586                         attr->load_store_mode = mode_Iu;
587                         attr->tv = new_tarval_from_long(i * 4, mode_Is);
588
589                         mm[i] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
590                 }
591                 */
592
593                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
594                 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 16);
595
596                 mm[4] = store;
597
598                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_RA]);
599                 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 20);
600
601                 mm[5] = store;
602
603                 /* Note: ideally we would route these mem edges directly towards the
604                  * epilogue, but this is currently not supported so we sync all mems
605                  * together */
606                 sync = new_r_Sync(irg, block, 2, mm+4);
607                 *mem = sync;
608         } else {
609                 ir_node *reg, *store;
610                 initialstackframesize = 4;
611
612                 // save old framepointer
613                 sp = new_rd_mips_addu(NULL, irg, block, sp,
614                                       mips_create_Immediate(-initialstackframesize));
615                 mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
616                 set_mips_flags(sp, arch_irn_flags_ignore);
617
618                 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
619                 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 0);
620
621                 *mem = store;
622         }
623
624         // setup framepointer
625         fp = new_rd_mips_addu(NULL, irg, block, sp,
626                               mips_create_Immediate(-initialstackframesize));
627         mips_set_irn_reg(fp, &mips_gp_regs[REG_FP]);
628         set_mips_flags(fp, arch_irn_flags_ignore);
629
630         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
631         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
632
633         return &mips_gp_regs[REG_SP];
634 }
635
636 static void mips_abi_epilogue(void *self, ir_node *block, ir_node **mem, pmap *reg_map)
637 {
638         mips_abi_env_t   *env = self;
639
640         ir_graph *irg = env->irg;
641         ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
642         ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
643         ir_node *load;
644         int initial_frame_size = env->debug ? 24 : 4;
645         int fp_save_offset = env->debug ? 16 : 0;
646
647         // copy fp to sp
648         sp = new_rd_mips_or(NULL, irg, block, fp, mips_create_zero());
649         mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
650         set_mips_flags(sp, arch_irn_flags_ignore);
651
652         // 1. restore fp
653         load = new_rd_mips_lw(NULL, irg, block, sp, *mem, NULL,
654                               fp_save_offset - initial_frame_size);
655         set_mips_flags(load, arch_irn_flags_ignore);
656
657         fp = new_r_Proj(irg, block, load, mode_Iu, pn_mips_lw_res);
658         *mem = new_r_Proj(irg, block, load, mode_Iu, pn_mips_lw_M);
659         arch_set_irn_register(fp, &mips_gp_regs[REG_FP]);
660
661         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
662         be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
663 }
664
665 /**
666  * Produces the type which sits between the stack args and the locals on the stack.
667  * it will contain the return address and space to store the old frame pointer.
668  * @return The Firm type modelling the ABI between type.
669  */
670 static ir_type *mips_abi_get_between_type(void *self) {
671         mips_abi_env_t *env = self;
672
673         static ir_type *debug_between_type = NULL;
674         static ir_type *opt_between_type = NULL;
675         static ir_entity *old_fp_ent    = NULL;
676
677         if(env->debug && debug_between_type == NULL) {
678                 ir_entity *a0_ent, *a1_ent, *a2_ent, *a3_ent;
679                 ir_entity *ret_addr_ent;
680                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
681                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
682                 ir_type *old_param_type = new_type_primitive(new_id_from_str("param"), mode_Iu);
683
684                 debug_between_type     = new_type_class(new_id_from_str("mips_between_type"));
685                 a0_ent                             = new_entity(debug_between_type, new_id_from_str("a0_ent"), old_param_type);
686                 a1_ent                             = new_entity(debug_between_type, new_id_from_str("a1_ent"), old_param_type);
687                 a2_ent                             = new_entity(debug_between_type, new_id_from_str("a2_ent"), old_param_type);
688                 a3_ent                             = new_entity(debug_between_type, new_id_from_str("a3_ent"), old_param_type);
689                 old_fp_ent             = new_entity(debug_between_type, new_id_from_str("old_fp"), old_fp_type);
690                 ret_addr_ent           = new_entity(debug_between_type, new_id_from_str("ret_addr"), ret_addr_type);
691
692                 set_entity_offset(a0_ent, 0);
693                 set_entity_offset(a1_ent, 4);
694                 set_entity_offset(a2_ent, 8);
695                 set_entity_offset(a3_ent, 12);
696                 set_entity_offset(old_fp_ent, 16);
697                 set_entity_offset(ret_addr_ent, 20);
698
699                 set_type_size_bytes(debug_between_type, 24);
700         } else if(!env->debug && opt_between_type == NULL) {
701                 ir_type *old_fp_type   = new_type_primitive(new_id_from_str("fp"), mode_P);
702                 ir_entity *old_fp_ent;
703
704                 opt_between_type       = new_type_class(new_id_from_str("mips_between_type"));
705                 old_fp_ent             = new_entity(opt_between_type, new_id_from_str("old_fp"), old_fp_type);
706                 set_entity_offset(old_fp_ent, 0);
707                 set_type_size_bytes(opt_between_type, 4);
708         }
709
710         return env->debug ? debug_between_type : opt_between_type;
711 }
712
713 static const be_abi_callbacks_t mips_abi_callbacks = {
714         mips_abi_init,
715         free,
716         mips_abi_get_between_type,
717         mips_abi_dont_save_regs,
718         mips_abi_prologue,
719         mips_abi_epilogue,
720 };
721
722 /**
723  * Get the ABI restrictions for procedure calls.
724  * @param self        The this pointer.
725  * @param method_type The type of the method (procedure) in question.
726  * @param abi         The abi object to be modified
727  */
728 static void mips_get_call_abi(const void *self, ir_type *method_type,
729                               be_abi_call_t *abi)
730 {
731         ir_type  *tp;
732         ir_mode  *mode;
733         int       n = get_method_n_params(method_type);
734         int result_count;
735         int       i;
736         ir_mode **modes;
737         const arch_register_t *reg;
738         be_abi_call_flags_t call_flags;
739         (void) self;
740
741         memset(&call_flags, 0, sizeof(call_flags));
742         call_flags.bits.left_to_right         = 0;
743         call_flags.bits.store_args_sequential = 0;
744         call_flags.bits.try_omit_fp           = 1;
745         call_flags.bits.fp_free               = 0;
746         call_flags.bits.call_has_imm          = 1;
747
748         /* set stack parameter passing style */
749         be_abi_call_set_flags(abi, call_flags, &mips_abi_callbacks);
750
751         /* collect the mode for each type */
752         modes = alloca(n * sizeof(modes[0]));
753         for (i = 0; i < n; i++) {
754                 tp       = get_method_param_type(method_type, i);
755                 modes[i] = get_type_mode(tp);
756         }
757
758         // assigns parameters to registers or stack
759         for (i = 0; i < n; i++) {
760                 // first 4 params in $a0-$a3, the others on the stack
761                 if(i < 4) {
762                         reg = &mips_gp_regs[REG_A0 + i];
763                         be_abi_call_param_reg(abi, i, reg);
764                 } else {
765                         /* default: all parameters on stack */
766                         be_abi_call_param_stack(abi, i, modes[i], 4, 0, 0);
767                 }
768         }
769
770         /* set return register */
771         /* default: return value is in R0 (and maybe R1) */
772         result_count = get_method_n_ress(method_type);
773         assert(result_count <= 2 && "More than 2 result values not supported");
774         for(i = 0; i < result_count; ++i) {
775                 const arch_register_t* reg;
776                 tp   = get_method_res_type(method_type, i);
777                 mode = get_type_mode(tp);
778                 ASSERT_NO_FLOAT(mode);
779
780                 reg = &mips_gp_regs[REG_V0 + i];
781                 be_abi_call_res_reg(abi, i, reg);
782         }
783 }
784
785 /**
786  * Initializes the code generator interface.
787  */
788 static const arch_code_generator_if_t *mips_get_code_generator_if(void *self)
789 {
790         (void) self;
791         return &mips_code_gen_if;
792 }
793
794 /**
795  * Returns the necessary byte alignment for storing a register of given class.
796  */
797 static int mips_get_reg_class_alignment(const void *self,
798                                         const arch_register_class_t *cls)
799 {
800         ir_mode *mode = arch_register_class_mode(cls);
801         (void) self;
802         return get_mode_size_bytes(mode);
803 }
804
805 static const be_execution_unit_t ***mips_get_allowed_execution_units(
806                 const void *self, const ir_node *irn)
807 {
808         (void) self;
809         (void) irn;
810         /* TODO */
811         panic("Unimplemented mips_get_allowed_execution_units()");
812 }
813
814 static const be_machine_t *mips_get_machine(const void *self)
815 {
816         (void) self;
817         /* TODO */
818         panic("Unimplemented mips_get_machine()");
819 }
820
821 /**
822  * Return irp irgs in the desired order.
823  */
824 static ir_graph **mips_get_irg_list(const void *self, ir_graph ***irg_list)
825 {
826         (void) self;
827         (void) irg_list;
828         return NULL;
829 }
830
831 /**
832  * Returns the libFirm configuration parameter for this backend.
833  */
834 static const backend_params *mips_get_libfirm_params(void) {
835         static backend_params p = {
836                 1,     /* need dword lowering */
837                 0,     /* don't support inline assembler yet */
838                 0,     /* no immediate floating point mode. */
839                 NULL,  /* no additional opcodes */
840                 NULL,  /* will be set later */
841                 NULL,  /* but yet no creator function */
842                 NULL,  /* context for create_intrinsic_fkt */
843                 NULL,  /* no if conversion settings */
844                 NULL   /* no immediate fp mode */
845         };
846
847         return &p;
848 }
849
850 static asm_constraint_flags_t mips_parse_asm_constraint(const void *self,
851                                                         const char **c)
852 {
853         (void) self;
854         (void) c;
855         return ASM_CONSTRAINT_FLAG_INVALID;
856 }
857
858 static int mips_is_valid_clobber(const void *self, const char *clobber)
859 {
860         (void) self;
861         (void) clobber;
862         return 0;
863 }
864
865 const arch_isa_if_t mips_isa_if = {
866         mips_init,
867         mips_done,
868         mips_get_n_reg_class,
869         mips_get_reg_class,
870         mips_get_reg_class_for_mode,
871         mips_get_call_abi,
872         mips_get_code_generator_if,
873         mips_get_list_sched_selector,
874         mips_get_ilp_sched_selector,
875         mips_get_reg_class_alignment,
876         mips_get_libfirm_params,
877         mips_get_allowed_execution_units,
878         mips_get_machine,
879         mips_get_irg_list,
880         NULL,                /* mark remat */
881         mips_parse_asm_constraint,
882         mips_is_valid_clobber
883 };
884
885 void be_init_arch_mips(void)
886 {
887         be_register_isa_if("mips", &mips_isa_if);
888 }
889
890 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_mips);