2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief The main mips backend driver file.
23 * @author Matthias Braun, Mehdi
28 #include "pseudo_irg.h"
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"
53 #include "../bemachine.h"
54 #include "../bemodule.h"
55 #include "../bespillslots.h"
56 #include "../beemitter.h"
57 #include "../begnuas.h"
59 #include "bearch_mips_t.h"
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"
69 #define DEBUG_MODULE "firm.be.mips.isa"
71 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
72 static set *cur_reg_set = NULL;
74 /**************************************************
77 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
78 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
79 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
80 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
83 **************************************************/
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.
91 arch_register_req_t *mips_get_irn_reg_req(const ir_node *node, int pos)
93 long node_pos = pos == -1 ? 0 : pos;
94 ir_mode *mode = get_irn_mode(node);
96 if (is_Block(node) || mode == mode_X || mode == mode_M) {
97 return arch_no_register_req;
100 if (mode == mode_T && pos < 0) {
101 return arch_no_register_req;
105 /* in case of a proj, we need to get the correct OUT slot */
106 /* of the node corresponding to the proj number */
108 node_pos = mips_translate_proj_pos(node);
114 node = skip_Proj_const(node);
117 /* get requirements for our own nodes */
118 if (is_mips_irn(node)) {
119 const arch_register_req_t *req;
121 req = get_mips_in_req(node, pos);
123 req = get_mips_out_req(node, node_pos);
129 /* unknown should be translated by now */
130 assert(!is_Unknown(node));
132 return arch_no_register_req;
135 static void mips_set_irn_reg(ir_node *irn, const arch_register_t *reg)
141 if (get_irn_mode(irn) == mode_X) {
145 pos = mips_translate_proj_pos(irn);
146 irn = skip_Proj(irn);
149 if (is_mips_irn(irn)) {
150 const arch_register_t **slots;
152 slots = get_mips_slots(irn);
155 /* here we set the registers for the Phi nodes */
156 mips_set_firm_reg(irn, reg, cur_reg_set);
160 static const arch_register_t *mips_get_irn_reg(const ir_node *irn)
163 const arch_register_t *reg = NULL;
167 if (get_irn_mode(irn) == mode_X) {
171 pos = mips_translate_proj_pos(irn);
172 irn = skip_Proj_const(irn);
175 if (is_mips_irn(irn)) {
176 const arch_register_t **slots;
177 slots = get_mips_slots(irn);
181 reg = mips_get_firm_reg(irn, cur_reg_set);
187 static arch_irn_class_t mips_classify(const ir_node *irn)
189 irn = skip_Proj_const(irn);
192 return arch_irn_class_branch;
198 static arch_irn_flags_t mips_get_flags(const ir_node *irn)
200 irn = skip_Proj_const(irn);
202 if (!is_mips_irn(irn))
205 return get_mips_flags(irn);
208 int mips_is_Load(const ir_node *node)
210 return is_mips_lw(node) || is_mips_lh(node) || is_mips_lhu(node) ||
211 is_mips_lb(node) || is_mips_lbu(node);
214 int mips_is_Store(const ir_node *node)
216 return is_mips_sw(node) || is_mips_sh(node) || is_mips_sb(node);
219 static ir_entity *mips_get_frame_entity(const ir_node *node)
221 const mips_load_store_attr_t *attr;
223 if(!is_mips_irn(node))
225 if(!mips_is_Load(node) && !mips_is_Store(node))
228 attr = get_mips_load_store_attr_const(node);
229 return attr->stack_entity;
232 static void mips_set_frame_entity(ir_node *node, ir_entity *entity)
234 mips_load_store_attr_t *attr;
236 if(!is_mips_irn(node)) {
237 panic("trying to set frame entity on non load/store node %+F", node);
239 if(!mips_is_Load(node) && !mips_is_Store(node)) {
240 panic("trying to set frame entity on non load/store node %+F", node);
243 attr = get_irn_generic_attr(node);
244 attr->stack_entity = entity;
248 * This function is called by the generic backend to correct offsets for
249 * nodes accessing the stack.
251 static void mips_set_frame_offset(ir_node *node, int offset)
253 mips_load_store_attr_t *attr;
255 if(!is_mips_irn(node)) {
256 panic("trying to set frame offset on non load/store node %+F", node);
258 if(!mips_is_Load(node) && !mips_is_Store(node)) {
259 panic("trying to set frame offset on non load/store node %+F", node);
262 attr = get_irn_generic_attr(node);
263 attr->offset += offset;
265 if(attr->offset < -32768 || attr->offset > 32767) {
266 panic("Out of stack space! (mips supports only 16bit offsets)");
270 static int mips_get_sp_bias(const ir_node *irn)
276 /* fill register allocator interface */
278 static const arch_irn_ops_t mips_irn_ops = {
279 mips_get_irn_reg_req,
284 mips_get_frame_entity,
285 mips_set_frame_entity,
286 mips_set_frame_offset,
288 NULL, /* get_inverse */
289 NULL, /* get_op_estimated_cost */
290 NULL, /* possible_memory_operand */
291 NULL, /* perform_memory_operand */
294 /**************************************************
297 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
298 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
299 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
300 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
303 **************************************************/
306 * Transforms the standard firm graph into
309 static void mips_prepare_graph(void *self) {
310 mips_code_gen_t *cg = self;
312 /* do local optimizations */
313 optimize_graph_df(cg->irg);
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)
319 edges_deactivate(cg->irg);
320 edges_activate(cg->irg);
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");
327 /* do local optimizations (mainly CSE) */
328 optimize_graph_df(cg->irg);
330 /* do code placement, to optimize the position of constants */
333 be_dump(cg->irg, "-place", dump_ir_block_graph_sched);
337 * Called immediately before emit phase.
339 static void mips_finish_irg(void *self) {
340 mips_code_gen_t *cg = self;
341 ir_graph *irg = cg->irg;
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);
347 dump_ir_block_graph_sched(irg, "-mips-finished");
351 static void mips_before_ra(void *self)
356 static void mips_after_ra(void* self)
358 mips_code_gen_t *cg = self;
359 be_coalesce_spillslots(cg->birg);
360 irg_walk_blkwise_graph(cg->irg, NULL, mips_after_ra_walker, self);
364 * Emits the code, closes the output file and frees
365 * the code generator interface.
367 static void mips_emit_and_done(void *self)
369 mips_code_gen_t *cg = self;
370 ir_graph *irg = cg->irg;
373 mips_gen_routine(cg, irg);
377 /* de-allocate code generator */
378 del_set(cg->reg_set);
382 static void *mips_cg_init(be_irg_t *birg);
384 static const arch_code_generator_if_t mips_code_gen_if = {
386 NULL, /* get_pic_base */
387 NULL, /* before abi introduce */
390 mips_before_ra, /* before register allocation hook */
397 * Initializes the code generator.
399 static void *mips_cg_init(be_irg_t *birg)
401 const arch_env_t *arch_env = be_get_birg_arch_env(birg);
402 mips_isa_t *isa = (mips_isa_t *) arch_env;
403 mips_code_gen_t *cg = XMALLOCZ(mips_code_gen_t);
405 cg->impl = &mips_code_gen_if;
406 cg->irg = be_get_birg_irg(birg);
407 cg->reg_set = new_set(mips_cmp_irn_reg_assoc, 1024);
411 cur_reg_set = cg->reg_set;
415 return (arch_code_generator_t *)cg;
419 /*****************************************************************
420 * ____ _ _ _____ _____
421 * | _ \ | | | | |_ _|/ ____| /\
422 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
423 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
424 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
425 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
427 *****************************************************************/
429 static mips_isa_t mips_isa_template = {
432 &mips_gp_regs[REG_SP],
433 &mips_gp_regs[REG_FP],
434 -1, /* stack direction */
435 2, /* power of two stack alignment for calls, 2^2 == 4 */
436 NULL, /* main environment */
438 5, /* reload costs */
444 * Initializes the backend ISA and opens the output file.
446 static arch_env_t *mips_init(FILE *file_handle) {
447 static int inited = 0;
454 isa = XMALLOC(mips_isa_t);
455 memcpy(isa, &mips_isa_template, sizeof(isa[0]));
457 be_emit_init(file_handle);
459 mips_register_init();
460 mips_create_opcodes(&mips_irn_ops);
461 // mips_init_opcode_transforms();
463 /* we mark referenced global entities, so we can only emit those which
464 * are actually referenced. (Note: you mustn't use the type visited flag
465 * elsewhere in the backend)
467 inc_master_type_visited();
469 return &isa->arch_env;
473 * Closes the output file and frees the ISA structure.
475 static void mips_done(void *self)
477 mips_isa_t *isa = self;
479 be_gas_emit_decls(isa->arch_env.main_env, 1);
485 static unsigned mips_get_n_reg_class(const void *self)
491 static const arch_register_class_t *mips_get_reg_class(const void *self,
495 assert(i < N_CLASSES);
496 return &mips_reg_classes[i];
502 * Get the register class which shall be used to store a value of a given mode.
503 * @param self The this pointer.
504 * @param mode The mode in question.
505 * @return A register class which can hold values of the given mode.
507 const arch_register_class_t *mips_get_reg_class_for_mode(const void *self,
512 ASSERT_NO_FLOAT(mode);
513 return &mips_reg_classes[CLASS_mips_gp];
517 be_abi_call_flags_bits_t flags;
518 const arch_env_t *arch_env;
520 // do special handling to support debuggers
524 static void *mips_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
526 mips_abi_env_t *env = XMALLOC(mips_abi_env_t);
527 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
528 env->flags = fl.bits;
530 env->arch_env = arch_env;
535 static void mips_abi_dont_save_regs(void *self, pset *s)
537 mips_abi_env_t *env = self;
539 if(env->flags.try_omit_fp)
540 pset_insert_ptr(s, env->arch_env->bp);
543 static const arch_register_t *mips_abi_prologue(void *self, ir_node** mem, pmap *reg_map, int *stack_bias)
545 mips_abi_env_t *env = self;
546 ir_graph *irg = env->irg;
547 ir_node *block = get_irg_start_block(env->irg);
548 ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
549 ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
550 int initialstackframesize;
556 * The calling conventions wants a stack frame of at least 24bytes size with
557 * a0-a3 saved in offset 0-12
558 * fp saved in offset 16
559 * ra saved in offset 20
562 ir_node *sync, *reg, *store;
563 initialstackframesize = 24;
565 // - setup first part of stackframe
566 sp = new_rd_mips_addu(NULL, irg, block, sp,
567 mips_create_Immediate(initialstackframesize));
568 mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
569 set_mips_flags(sp, arch_irn_flags_ignore);
571 /* TODO: where to get an edge with a0-a3
573 for(i = 0; i < 4; ++i) {
574 ir_node *reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_A0 + i]);
575 ir_node *store = new_rd_mips_store_r(dbg, irg, block, *mem, sp, reg, mode_T);
576 attr = get_mips_attr(store);
577 attr->load_store_mode = mode_Iu;
578 attr->tv = new_tarval_from_long(i * 4, mode_Is);
580 mm[i] = new_r_Proj(irg, block, store, mode_M, pn_Store_M);
584 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
585 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 16);
589 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_RA]);
590 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 20);
594 /* Note: ideally we would route these mem edges directly towards the
595 * epilogue, but this is currently not supported so we sync all mems
597 sync = new_r_Sync(irg, block, 2, mm+4);
600 ir_node *reg, *store;
601 initialstackframesize = 4;
603 // save old framepointer
604 sp = new_rd_mips_addu(NULL, irg, block, sp,
605 mips_create_Immediate(-initialstackframesize));
606 mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
607 set_mips_flags(sp, arch_irn_flags_ignore);
609 reg = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
610 store = new_rd_mips_sw(NULL, irg, block, sp, reg, *mem, NULL, 0);
615 // setup framepointer
616 fp = new_rd_mips_addu(NULL, irg, block, sp,
617 mips_create_Immediate(-initialstackframesize));
618 mips_set_irn_reg(fp, &mips_gp_regs[REG_FP]);
619 set_mips_flags(fp, arch_irn_flags_ignore);
621 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
622 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
624 return &mips_gp_regs[REG_SP];
627 static void mips_abi_epilogue(void *self, ir_node *block, ir_node **mem, pmap *reg_map)
629 mips_abi_env_t *env = self;
631 ir_graph *irg = env->irg;
632 ir_node *sp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_SP]);
633 ir_node *fp = be_abi_reg_map_get(reg_map, &mips_gp_regs[REG_FP]);
635 int initial_frame_size = env->debug ? 24 : 4;
636 int fp_save_offset = env->debug ? 16 : 0;
639 sp = new_rd_mips_or(NULL, irg, block, fp, mips_create_zero());
640 mips_set_irn_reg(sp, &mips_gp_regs[REG_SP]);
641 set_mips_flags(sp, arch_irn_flags_ignore);
644 load = new_rd_mips_lw(NULL, irg, block, sp, *mem, NULL,
645 fp_save_offset - initial_frame_size);
646 set_mips_flags(load, arch_irn_flags_ignore);
648 fp = new_r_Proj(irg, block, load, mode_Iu, pn_mips_lw_res);
649 *mem = new_r_Proj(irg, block, load, mode_Iu, pn_mips_lw_M);
650 arch_set_irn_register(fp, &mips_gp_regs[REG_FP]);
652 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_FP], fp);
653 be_abi_reg_map_set(reg_map, &mips_gp_regs[REG_SP], sp);
657 * Produces the type which sits between the stack args and the locals on the stack.
658 * it will contain the return address and space to store the old frame pointer.
659 * @return The Firm type modelling the ABI between type.
661 static ir_type *mips_abi_get_between_type(void *self) {
662 mips_abi_env_t *env = self;
664 static ir_type *debug_between_type = NULL;
665 static ir_type *opt_between_type = NULL;
666 static ir_entity *old_fp_ent = NULL;
668 if(env->debug && debug_between_type == NULL) {
669 ir_entity *a0_ent, *a1_ent, *a2_ent, *a3_ent;
670 ir_entity *ret_addr_ent;
671 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
672 ir_type *old_fp_type = new_type_primitive(new_id_from_str("fp"), mode_P);
673 ir_type *old_param_type = new_type_primitive(new_id_from_str("param"), mode_Iu);
675 debug_between_type = new_type_class(new_id_from_str("mips_between_type"));
676 a0_ent = new_entity(debug_between_type, new_id_from_str("a0_ent"), old_param_type);
677 a1_ent = new_entity(debug_between_type, new_id_from_str("a1_ent"), old_param_type);
678 a2_ent = new_entity(debug_between_type, new_id_from_str("a2_ent"), old_param_type);
679 a3_ent = new_entity(debug_between_type, new_id_from_str("a3_ent"), old_param_type);
680 old_fp_ent = new_entity(debug_between_type, new_id_from_str("old_fp"), old_fp_type);
681 ret_addr_ent = new_entity(debug_between_type, new_id_from_str("ret_addr"), ret_addr_type);
683 set_entity_offset(a0_ent, 0);
684 set_entity_offset(a1_ent, 4);
685 set_entity_offset(a2_ent, 8);
686 set_entity_offset(a3_ent, 12);
687 set_entity_offset(old_fp_ent, 16);
688 set_entity_offset(ret_addr_ent, 20);
690 set_type_size_bytes(debug_between_type, 24);
691 } else if(!env->debug && opt_between_type == NULL) {
692 ir_type *old_fp_type = new_type_primitive(new_id_from_str("fp"), mode_P);
693 ir_entity *old_fp_ent;
695 opt_between_type = new_type_class(new_id_from_str("mips_between_type"));
696 old_fp_ent = new_entity(opt_between_type, new_id_from_str("old_fp"), old_fp_type);
697 set_entity_offset(old_fp_ent, 0);
698 set_type_size_bytes(opt_between_type, 4);
701 return env->debug ? debug_between_type : opt_between_type;
704 static const be_abi_callbacks_t mips_abi_callbacks = {
707 mips_abi_get_between_type,
708 mips_abi_dont_save_regs,
714 * Get the ABI restrictions for procedure calls.
715 * @param self The this pointer.
716 * @param method_type The type of the method (procedure) in question.
717 * @param abi The abi object to be modified
719 static void mips_get_call_abi(const void *self, ir_type *method_type,
724 int n = get_method_n_params(method_type);
728 const arch_register_t *reg;
729 be_abi_call_flags_t call_flags;
732 memset(&call_flags, 0, sizeof(call_flags));
733 call_flags.bits.left_to_right = 0;
734 call_flags.bits.store_args_sequential = 0;
735 call_flags.bits.try_omit_fp = 1;
736 call_flags.bits.fp_free = 0;
737 call_flags.bits.call_has_imm = 1;
739 /* set stack parameter passing style */
740 be_abi_call_set_flags(abi, call_flags, &mips_abi_callbacks);
742 /* collect the mode for each type */
743 modes = alloca(n * sizeof(modes[0]));
744 for (i = 0; i < n; i++) {
745 tp = get_method_param_type(method_type, i);
746 modes[i] = get_type_mode(tp);
749 // assigns parameters to registers or stack
750 for (i = 0; i < n; i++) {
751 // first 4 params in $a0-$a3, the others on the stack
753 reg = &mips_gp_regs[REG_A0 + i];
754 be_abi_call_param_reg(abi, i, reg);
756 /* default: all parameters on stack */
757 be_abi_call_param_stack(abi, i, modes[i], 4, 0, 0);
761 /* set return register */
762 /* default: return value is in R0 (and maybe R1) */
763 result_count = get_method_n_ress(method_type);
764 assert(result_count <= 2 && "More than 2 result values not supported");
765 for(i = 0; i < result_count; ++i) {
766 const arch_register_t* reg;
767 tp = get_method_res_type(method_type, i);
768 mode = get_type_mode(tp);
769 ASSERT_NO_FLOAT(mode);
771 reg = &mips_gp_regs[REG_V0 + i];
772 be_abi_call_res_reg(abi, i, reg);
777 * Initializes the code generator interface.
779 static const arch_code_generator_if_t *mips_get_code_generator_if(void *self)
782 return &mips_code_gen_if;
786 * Returns the necessary byte alignment for storing a register of given class.
788 static int mips_get_reg_class_alignment(const void *self,
789 const arch_register_class_t *cls)
791 ir_mode *mode = arch_register_class_mode(cls);
793 return get_mode_size_bytes(mode);
796 static const be_execution_unit_t ***mips_get_allowed_execution_units(
797 const void *self, const ir_node *irn)
802 panic("Unimplemented mips_get_allowed_execution_units()");
805 static const be_machine_t *mips_get_machine(const void *self)
809 panic("Unimplemented mips_get_machine()");
813 * Return irp irgs in the desired order.
815 static ir_graph **mips_get_irg_list(const void *self, ir_graph ***irg_list)
823 * Returns the libFirm configuration parameter for this backend.
825 static const backend_params *mips_get_libfirm_params(void) {
826 static backend_params p = {
827 1, /* need dword lowering */
828 0, /* don't support inline assembler yet */
829 0, /* no immediate floating point mode. */
830 NULL, /* no additional opcodes */
831 NULL, /* will be set later */
832 NULL, /* but yet no creator function */
833 NULL, /* context for create_intrinsic_fkt */
834 NULL, /* no if conversion settings */
835 NULL /* no immediate fp mode */
841 static asm_constraint_flags_t mips_parse_asm_constraint(const void *self,
846 return ASM_CONSTRAINT_FLAG_INVALID;
849 static int mips_is_valid_clobber(const void *self, const char *clobber)
856 const arch_isa_if_t mips_isa_if = {
859 mips_get_n_reg_class,
861 mips_get_reg_class_for_mode,
863 mips_get_code_generator_if,
864 mips_get_list_sched_selector,
865 mips_get_ilp_sched_selector,
866 mips_get_reg_class_alignment,
867 mips_get_libfirm_params,
868 mips_get_allowed_execution_units,
871 NULL, /* mark remat */
872 mips_parse_asm_constraint,
873 mips_is_valid_clobber
876 void be_init_arch_mips(void)
878 be_register_isa_if("mips", &mips_isa_if);
881 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_mips);