1 /* The main mips backend driver file. */
8 #include "pseudo_irg.h"
23 #include "../bearch.h" /* the general register allocator interface */
24 #include "../benode_t.h"
25 #include "../belower.h"
26 #include "../besched_t.h"
30 #include "bearch_mips_t.h"
32 #include "mips_new_nodes.h" /* mips nodes interface */
33 #include "gen_mips_regalloc_if.h" /* the generated interface (register type and class defenitions) */
34 #include "mips_gen_decls.h" /* interface declaration emitter */
35 #include "mips_transform.h"
36 #include "mips_emitter.h"
37 #include "mips_map_regs.h"
38 #include "mips_util.h"
39 #include "mips_scheduler.h"
41 #define DEBUG_MODULE "firm.be.mips.isa"
43 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
44 static set *cur_reg_set = NULL;
46 /**************************************************
49 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
50 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
51 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
52 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
55 **************************************************/
57 static ir_node *my_skip_proj(const ir_node *n) {
64 * Return register requirements for a mips node.
65 * If the node returns a tuple (mode_T) then the proj's
66 * will be asked for this information.
68 static const arch_register_req_t *mips_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
69 const mips_register_req_t *irn_req;
70 long node_pos = pos == -1 ? 0 : pos;
71 ir_mode *mode = get_irn_mode(irn);
72 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
74 if (is_Block(irn) || mode == mode_X || mode == mode_M) {
75 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
79 if (mode == mode_T && pos < 0) {
80 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F\n", irn));
84 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
87 /* in case of a proj, we need to get the correct OUT slot */
88 /* of the node corresponding to the proj number */
90 node_pos = mips_translate_proj_pos(irn);
96 irn = my_skip_proj(irn);
98 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
101 /* get requirements for our own nodes */
102 if (is_mips_irn(irn)) {
104 irn_req = get_mips_in_req(irn, pos);
107 irn_req = get_mips_out_req(irn, node_pos);
110 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
112 memcpy(req, &(irn_req->req), sizeof(*req));
114 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
115 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
116 req->other_same = get_irn_n(irn, irn_req->same_pos);
119 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
120 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
121 req->other_different = get_irn_n(irn, irn_req->different_pos);
124 /* get requirements for FIRM nodes */
126 /* treat Phi like Const with default requirements */
128 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
130 if (mode_is_float(mode)) {
131 //memcpy(req, &(mips_default_req_mips_floating_point.req), sizeof(*req));
132 assert(0 && "floating point not supported (yet)");
134 else if (mode_is_int(mode) || mode_is_reference(mode)) {
135 memcpy(req, &(mips_default_req_mips_gp.req), sizeof(*req));
137 else if (mode == mode_T || mode == mode_M) {
138 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
142 assert(0 && "unsupported Phi-Mode");
146 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
154 static void mips_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
159 if (get_irn_mode(irn) == mode_X) {
163 pos = mips_translate_proj_pos(irn);
164 irn = my_skip_proj(irn);
167 if (is_mips_irn(irn)) {
168 const arch_register_t **slots;
170 slots = get_mips_slots(irn);
174 /* here we set the registers for the Phi nodes */
175 mips_set_firm_reg(irn, reg, cur_reg_set);
179 static const arch_register_t *mips_get_irn_reg(const void *self, const ir_node *irn) {
181 const arch_register_t *reg = NULL;
185 if (get_irn_mode(irn) == mode_X) {
189 pos = mips_translate_proj_pos(irn);
190 irn = my_skip_proj(irn);
193 if (is_mips_irn(irn)) {
194 const arch_register_t **slots;
195 slots = get_mips_slots(irn);
199 reg = mips_get_firm_reg(irn, cur_reg_set);
205 static arch_irn_class_t mips_classify(const void *self, const ir_node *irn) {
206 irn = my_skip_proj(irn);
209 return arch_irn_class_branch;
210 } else if (is_mips_irn(irn)) {
211 return arch_irn_class_normal;
217 static arch_irn_flags_t mips_get_flags(const void *self, const ir_node *irn) {
218 irn = my_skip_proj(irn);
220 if (is_mips_irn(irn)) {
221 return get_mips_flags(irn);
223 else if (is_Unknown(irn)) {
224 return arch_irn_flags_ignore;
230 static entity *mips_get_frame_entity(const void *self, const ir_node *irn) {
231 if(is_mips_load_r(irn) || is_mips_store_r(irn)) {
232 mips_attr_t *attr = get_mips_attr(irn);
234 return attr->stack_entity;
240 static void mips_set_frame_entity(const void *self, ir_node *irn, entity *ent) {
241 mips_attr_t *attr = get_mips_attr(irn);
242 assert(is_mips_load_r(irn) || is_mips_store_r(irn));
243 attr->stack_entity = ent;
247 * This function is called by the generic backend to correct offsets for
248 * nodes accessing the stack.
250 static void mips_set_frame_offset(const void *self, ir_node *irn, int offset) {
251 mips_attr_t *attr = get_mips_attr(irn);
252 assert(is_mips_load_r(irn) || is_mips_store_r(irn));
254 attr->stack_entity_offset = offset;
257 /* fill register allocator interface */
259 static const arch_irn_ops_if_t mips_irn_ops_if = {
260 mips_get_irn_reg_req,
265 mips_get_frame_entity,
266 mips_set_frame_entity,
267 mips_set_frame_offset,
268 NULL, /* get_inverse */
269 NULL, /* get_op_estimated_cost */
270 NULL, /* possible_memory_operand */
271 NULL, /* perform_memory_operand */
274 mips_irn_ops_t mips_irn_ops = {
281 /**************************************************
284 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
285 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
286 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
287 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
290 **************************************************/
300 * Ext-Block walker: create a block schedule
302 static void create_block_list(ir_extblk *blk, void *env) {
306 for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
307 ir_node *block = get_extbb_block(blk, i);
309 set_irn_link(block, NULL);
311 set_irn_link(list->end, block);
320 /* return the scheduled block at position pos */
321 ir_node *mips_get_sched_block(const mips_code_gen_t *cg, int pos) {
322 if (0 <= pos && pos < ARR_LEN(cg->bl_list))
323 return cg->bl_list[pos];
327 /* return the number of scheduled blocks */
328 int mips_get_sched_n_blocks(const mips_code_gen_t *cg) {
329 return ARR_LEN(cg->bl_list);
332 /* set a block schedule number */
333 void mips_set_block_sched_nr(ir_node *block, int nr) {
334 set_irn_link(block, INT_TO_PTR(nr));
337 /* get a block schedule number */
338 int mips_get_block_sched_nr(ir_node *block) {
339 return PTR_TO_INT(get_irn_link(block));
343 * Creates a block schedule for the given graph.
345 static void mips_create_block_sched(mips_code_gen_t *cg) {
347 ir_node **bl_list, *block;
351 DEL_ARR_F(cg->bl_list);
352 free_survive_dce(cg->bl_list_sdce);
355 /* calculate the block schedule here */
356 compute_extbb(cg->irg);
361 irg_extblock_walk_graph(cg->irg, NULL, create_block_list, &list);
364 bl_list = NEW_ARR_F(ir_node *, list.cnt);
365 cg->bl_list_sdce = new_survive_dce();
366 for (i = 0, block = list.start; block; block = get_irn_link(block)) {
368 survive_dce_register_irn(cg->bl_list_sdce, &bl_list[i]);
372 cg->bl_list = bl_list;
375 typedef struct _wenv_t {
380 * Walker: link all CopyB nodes
382 static void collect_copyb_nodes(ir_node *node, void *env) {
385 if (get_irn_op(node) == op_CopyB) {
386 set_irn_link(node, wenv->list);
391 static void replace_copyb_nodes(mips_code_gen_t *cg) {
393 ir_node *copy, *next;
394 ir_node *old_bl, *new_bl, *jmp, *new_jmp, *mem;
395 const ir_edge_t *edge;
397 /* build code for all copyB */
399 irg_walk_graph(cg->irg, NULL, collect_copyb_nodes, &env);
401 for (copy = env.list; copy; copy = next) {
402 next = get_irn_link(copy);
404 old_bl = get_nodes_block(copy);
406 jmp = get_Block_cfgpred(old_bl, 0);
407 new_jmp = new_r_Jmp(cg->irg, get_nodes_block(copy));
409 new_bl = new_r_Block(cg->irg, 1, &new_jmp);
410 set_nodes_block(jmp, new_bl);
412 mem = gen_code_for_CopyB(new_bl, copy);
414 /* fix copyB's out edges */
415 foreach_out_edge(copy, edge) {
416 ir_node *succ = get_edge_src_irn(edge);
418 assert(is_Proj(succ));
419 switch (get_Proj_proj(succ)) {
420 case pn_CopyB_M_regular:
421 case pn_CopyB_M_except:
425 exchange(succ, get_irg_bad(cg->irg));
432 * Transforms the standard firm graph into
435 static void mips_prepare_graph(void *self) {
436 mips_code_gen_t *cg = self;
439 // replace all copyb nodes in the block with a loop
440 // and mips store/load nodes
441 replace_copyb_nodes(cg);
443 // Calculate block schedule
444 mips_create_block_sched(cg);
446 /* enter the block number into every blocks link field */
447 for (bl_nr = 0, n = mips_get_sched_n_blocks(cg); bl_nr < n; ++bl_nr) {
448 ir_node *bl = mips_get_sched_block(cg, bl_nr);
449 mips_set_block_sched_nr(bl, bl_nr);
452 // walk the graph and transform firm nodes into mips nodes where possible
453 irg_walk_blkwise_graph(cg->irg, mips_pre_transform_node, mips_transform_node, cg);
455 dump_ir_block_graph_sched(cg->irg, "-transformed");
459 * Called immediately before emit phase.
461 static void mips_finish_irg(void *self) {
462 mips_code_gen_t *cg = self;
463 ir_graph *irg = cg->irg;
465 dump_ir_block_graph_sched(irg, "-mips-finished");
470 * These are some hooks which must be filled but are probably not needed.
472 static void mips_before_sched(void *self) {
473 /* Some stuff you need to do after scheduling but before register allocation */
476 static void mips_before_ra(void *self) {
477 /* Some stuff you need to do immediately after register allocation */
480 static void mips_after_ra(void* self) {
481 mips_code_gen_t *cg = self;
482 irg_walk_blkwise_graph(cg->irg, NULL, mips_after_ra_walker, self);
486 * Emits the code, closes the output file and frees
487 * the code generator interface.
489 static void mips_emit_and_done(void *self) {
490 mips_code_gen_t *cg = self;
491 ir_graph *irg = cg->irg;
492 FILE *out = cg->isa->out;
494 mips_register_emitters();
496 if (cg->emit_decls) {
501 mips_gen_routine(out, irg, cg);
505 /* de-allocate code generator */
506 del_set(cg->reg_set);
508 DEL_ARR_F(cg->bl_list);
509 free_survive_dce(cg->bl_list_sdce);
514 static void *mips_cg_init(const be_irg_t *birg);
516 static const arch_code_generator_if_t mips_code_gen_if = {
518 NULL, /* before abi introduce */
520 mips_before_sched, /* before scheduling hook */
521 mips_before_ra, /* before register allocation hook */
528 * Initializes the code generator.
530 static void *mips_cg_init(const be_irg_t *birg) {
531 mips_isa_t *isa = (mips_isa_t *)birg->main_env->arch_env->isa;
532 mips_code_gen_t *cg = xmalloc(sizeof(*cg));
534 cg->impl = &mips_code_gen_if;
536 cg->reg_set = new_set(mips_cmp_irn_reg_assoc, 1024);
537 cg->arch_env = birg->main_env->arch_env;
541 FIRM_DBG_REGISTER(cg->mod, "firm.be.mips.cg");
545 if (isa->num_codegens > 1)
550 cur_reg_set = cg->reg_set;
552 mips_irn_ops.cg = cg;
554 return (arch_code_generator_t *)cg;
558 /*****************************************************************
559 * ____ _ _ _____ _____
560 * | _ \ | | | | |_ _|/ ____| /\
561 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
562 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
563 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
564 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
566 *****************************************************************/
568 static mips_isa_t mips_isa_template = {
570 &mips_gp_regs[REG_SP],
571 &mips_gp_regs[REG_FP],
572 -1, // stack direction
573 0, // num codegens?!? TODO what is this?
578 * Initializes the backend ISA and opens the output file.
580 static void *mips_init(FILE *file_handle) {
581 static int inited = 0;
587 isa = xcalloc(1, sizeof(*isa));
588 memcpy(isa, &mips_isa_template, sizeof(*isa));
590 isa->out = file_handle;
592 mips_register_init(isa);
593 mips_create_opcodes();
594 mips_init_opcode_transforms();
602 * Closes the output file and frees the ISA structure.
604 static void mips_done(void *self) {
608 static int mips_get_n_reg_class(const void *self) {
612 static const arch_register_class_t *mips_get_reg_class(const void *self, int i) {
613 assert(i >= 0 && i < N_CLASSES && "Invalid mips register class requested.");
614 return &mips_reg_classes[i];
620 * Get the register class which shall be used to store a value of a given mode.
621 * @param self The this pointer.
622 * @param mode The mode in question.
623 * @return A register class which can hold values of the given mode.
625 const arch_register_class_t *mips_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
626 ASSERT_NO_FLOAT(mode);
627 return &mips_reg_classes[CLASS_mips_gp];
631 be_abi_call_flags_bits_t flags;
632 const mips_isa_t *isa;
633 const arch_env_t *arch_env;
635 // do special handling to support debuggers
639 static void *mips_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
641 mips_abi_env_t *env = xmalloc(sizeof(env[0]));
642 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
643 env->flags = fl.bits;
645 env->arch_env = arch_env;
646 env->isa = (const mips_isa_t*) arch_env->isa;
651 static void mips_abi_dont_save_regs(void *self, pset *s)
653 mips_abi_env_t *env = self;
654 if(env->flags.try_omit_fp)
655 pset_insert_ptr(s, env->isa->fp);
658 static const arch_register_t *mips_abi_prologue(void *self, ir_node** mem, pmap *reg_map)
660 mips_abi_env_t *env = self;
661 ir_graph *irg = env->irg;
662 dbg_info *dbg = NULL; // TODO where can I get this from?
663 ir_node *block = get_irg_start_block(env->irg);
665 ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
666 ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
667 int initialstackframesize;
671 * The calling conventions wants a stack frame of at least 24bytes size with
672 * a0-a3 saved in offset 0-12
673 * fp saved in offset 16
674 * ra saved in offset 20
677 ir_node *sync, *reg, *store;
678 initialstackframesize = 24;
680 // - setup first part of stackframe
681 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
682 attr = get_mips_attr(sp);
683 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
684 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
685 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
687 /* TODO: where to get an edge with a0-a3
689 for(i = 0; i < 4; ++i) {
690 ir_node *reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_A0 + i]);
691 ir_node *store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
692 attr = get_mips_attr(store);
693 attr->load_store_mode = mode_Iu;
694 attr->tv = new_tarval_from_long(i * 4, mode_Is);
696 mm[i] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
700 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
701 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
702 attr = get_mips_attr(store);
703 attr->modes.load_store_mode = mode_Iu;
704 attr->tv = new_tarval_from_long(16, mode_Is);
706 mm[4] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
708 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_RA]);
709 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
710 attr = get_mips_attr(store);
711 attr->modes.load_store_mode = mode_Iu;
712 attr->tv = new_tarval_from_long(20, mode_Is);
714 mm[5] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
716 // TODO ideally we would route these mem edges directly towards the epilogue
717 sync = new_r_Sync(irg, block, 2, mm+4);
720 ir_node *reg, *store;
721 initialstackframesize = 4;
723 // save old framepointer
724 sp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
725 attr = get_mips_attr(sp);
726 attr->tv = new_tarval_from_long(-initialstackframesize, mode_Is);
727 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
728 //arch_set_irn_register(mips_get_arg_env(), sp, &mips_gp_regs[REG_SP]);
730 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
731 store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
732 attr = get_mips_attr(store);
733 attr->modes.load_store_mode = mode_Iu;
734 attr->tv = new_tarval_from_long(0, mode_Is);
736 *mem = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
739 // setup framepointer
740 fp = new_rd_mips_addi(dbg, irg, block, sp, mode_Is);
741 attr = get_mips_attr(fp);
742 attr->tv = new_tarval_from_long(initialstackframesize, mode_Is);
743 mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
744 //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
746 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
747 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
749 return &mips_gp_regs[REG_SP];
752 static void mips_abi_epilogue(void *self, ir_node *block, ir_node **mem, pmap *reg_map)
754 mips_abi_env_t *env = self;
755 ir_graph *irg = env->irg;
756 dbg_info *dbg = NULL; // TODO where can I get this from?
758 ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
759 ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
761 int initial_frame_size = env->debug ? 24 : 4;
762 int fp_save_offset = env->debug ? 16 : 0;
765 //sp = be_new_IncSP(&mips_gp_regs[REG_SP], irg, block, sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_against);
768 sp = new_rd_mips_move(dbg, irg, block, fp, mode_Iu);
769 mips_set_irn_reg(NULL, sp, &mips_gp_regs[REG_SP]);
770 //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_SP]);
773 load = new_rd_mips_load_r(dbg, irg, block, *mem, sp, mode_T);
774 attr = get_mips_attr(load);
775 attr->modes.load_store_mode = mode_Iu;
776 // sp is at the fp address already, so we have to do fp_save_offset - initial_frame_size
777 attr->tv = new_tarval_from_long(fp_save_offset - initial_frame_size, mode_Is);
779 fp = new_r_Proj(irg, block, load, mode_Iu, pn_Load_res);
780 mips_set_irn_reg(NULL, fp, &mips_gp_regs[REG_FP]);
781 //arch_set_irn_register(mips_get_arg_env(), fp, &mips_gp_regs[REG_FP]);
783 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
784 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
788 * Produces the type which sits between the stack args and the locals on the stack.
789 * it will contain the return address and space to store the old frame pointer.
790 * @return The Firm type modelling the ABI between type.
792 static ir_type *mips_abi_get_between_type(void *self) {
793 mips_abi_env_t *env = self;
795 static ir_type *debug_between_type = NULL;
796 static ir_type *opt_between_type = NULL;
797 static entity *old_fp_ent = NULL;
799 if(env->debug && debug_between_type == NULL) {
800 entity *a0_ent, *a1_ent, *a2_ent, *a3_ent;
801 entity *ret_addr_ent;
802 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
803 ir_type *old_fp_type = new_type_primitive(new_id_from_str("fp"), mode_P);
804 ir_type *old_param_type = new_type_primitive(new_id_from_str("param"), mode_Iu);
806 debug_between_type = new_type_class(new_id_from_str("mips_between_type"));
807 a0_ent = new_entity(debug_between_type, new_id_from_str("a0_ent"), old_param_type);
808 a1_ent = new_entity(debug_between_type, new_id_from_str("a1_ent"), old_param_type);
809 a2_ent = new_entity(debug_between_type, new_id_from_str("a2_ent"), old_param_type);
810 a3_ent = new_entity(debug_between_type, new_id_from_str("a3_ent"), old_param_type);
811 old_fp_ent = new_entity(debug_between_type, new_id_from_str("old_fp"), old_fp_type);
812 ret_addr_ent = new_entity(debug_between_type, new_id_from_str("ret_addr"), ret_addr_type);
814 set_entity_offset_bytes(a0_ent, 0);
815 set_entity_offset_bytes(a1_ent, 4);
816 set_entity_offset_bytes(a2_ent, 8);
817 set_entity_offset_bytes(a3_ent, 12);
818 set_entity_offset_bytes(old_fp_ent, 16);
819 set_entity_offset_bytes(ret_addr_ent, 20);
821 set_type_size_bytes(debug_between_type, 24);
822 } else if(!env->debug && opt_between_type == NULL) {
823 ir_type *old_fp_type = new_type_primitive(new_id_from_str("fp"), mode_P);
826 opt_between_type = new_type_class(new_id_from_str("mips_between_type"));
827 old_fp_ent = new_entity(opt_between_type, new_id_from_str("old_fp"), old_fp_type);
828 set_entity_offset_bytes(old_fp_ent, 0);
829 set_type_size_bytes(opt_between_type, 4);
832 return env->debug ? debug_between_type : opt_between_type;
835 static const be_abi_callbacks_t mips_abi_callbacks = {
838 mips_abi_get_between_type,
839 mips_abi_dont_save_regs,
845 * Get the ABI restrictions for procedure calls.
846 * @param self The this pointer.
847 * @param method_type The type of the method (procedure) in question.
848 * @param abi The abi object to be modified
850 static void mips_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
853 int n = get_method_n_params(method_type);
857 const arch_register_t *reg;
858 be_abi_call_flags_t call_flags;
860 memset(&call_flags, 0, sizeof(call_flags));
861 call_flags.bits.left_to_right = 0;
862 call_flags.bits.store_args_sequential = 0;
863 call_flags.bits.try_omit_fp = 1;
864 call_flags.bits.fp_free = 0;
865 call_flags.bits.call_has_imm = 1;
867 /* set stack parameter passing style */
868 be_abi_call_set_flags(abi, call_flags, &mips_abi_callbacks);
870 /* collect the mode for each type */
871 modes = alloca(n * sizeof(modes[0]));
872 for (i = 0; i < n; i++) {
873 tp = get_method_param_type(method_type, i);
874 modes[i] = get_type_mode(tp);
877 // assigns parameters to registers or stack
878 for (i = 0; i < n; i++) {
879 // first 4 params in $a0-$a3, the others on the stack
881 reg = &mips_gp_regs[REG_A0 + i];
882 be_abi_call_param_reg(abi, i, reg);
884 /* default: all parameters on stack */
885 be_abi_call_param_stack(abi, i, 4, 0, 0);
889 /* set return register */
890 /* default: return value is in R0 (and maybe R1) */
891 result_count = get_method_n_ress(method_type);
892 assert(result_count <= 2 && "More than 2 result values not supported");
893 for(i = 0; i < result_count; ++i) {
894 const arch_register_t* reg;
895 tp = get_method_res_type(method_type, i);
896 mode = get_type_mode(tp);
897 ASSERT_NO_FLOAT(mode);
899 reg = &mips_gp_regs[REG_V0 + i];
900 be_abi_call_res_reg(abi, i, reg);
904 static const void *mips_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
905 return &mips_irn_ops;
908 const arch_irn_handler_t mips_irn_handler = {
912 const arch_irn_handler_t *mips_get_irn_handler(const void *self) {
913 return &mips_irn_handler;
917 * Initializes the code generator interface.
919 static const arch_code_generator_if_t *mips_get_code_generator_if(void *self) {
920 return &mips_code_gen_if;
924 * Returns the necessary byte alignment for storing a register of given class.
926 static int mips_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
927 ir_mode *mode = arch_register_class_mode(cls);
928 return get_mode_size_bytes(mode);
932 * Returns the libFirm configuration parameter for this backend.
934 static const backend_params *mips_get_libfirm_params(void) {
935 static arch_dep_params_t ad = {
937 0, /* Muls are fast enough on Mips */
938 31, /* shift would be ok */
941 32, /* Mulhs & Mulhu available for 32 bit */
943 static backend_params p = {
944 NULL, /* no additional opcodes */
945 NULL, /* will be set later */
946 1, /* need dword lowering */
947 NULL, /* but yet no creator function */
948 NULL, /* context for create_intrinsic_fkt */
956 static void mips_register_options(lc_opt_entry_t *ent)
959 #endif /* WITH_LIBCORE */
961 const arch_isa_if_t mips_isa_if = {
964 mips_get_n_reg_class,
966 mips_get_reg_class_for_mode,
968 mips_get_irn_handler,
969 mips_get_code_generator_if,
970 mips_get_list_sched_selector,
971 mips_get_reg_class_alignment,
972 mips_get_libfirm_params,
974 mips_register_options