1 /* The main ppc backend driver file. */
8 #include "pseudo_irg.h"
19 #include "../bearch.h" /* the general register allocator interface */
20 #include "../benode_t.h"
21 #include "../belower.h"
22 #include "../besched_t.h"
28 #include "bearch_ppc32_t.h"
30 #include "ppc32_new_nodes.h" /* ppc nodes interface */
31 #include "gen_ppc32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
32 #include "ppc32_gen_decls.h" /* interface declaration emitter */
33 #include "ppc32_transform.h"
34 #include "ppc32_transform_conv.h"
35 #include "ppc32_emitter.h"
36 #include "ppc32_map_regs.h"
38 #define DEBUG_MODULE "firm.be.ppc.isa"
41 pset *symbol_pset = NULL;
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 ppc 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 *ppc32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
69 const ppc32_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 block, mode_X or 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", 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 = ppc32_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_ppc32_irn(irn)) {
104 irn_req = get_ppc32_in_req(irn, pos);
107 irn_req = get_ppc32_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, &(ppc32_default_req_ppc32_floating_point.req), sizeof(*req));
133 else if (mode_is_int(mode) || mode_is_reference(mode)) {
134 memcpy(req, &(ppc32_default_req_ppc32_general_purpose.req), sizeof(*req));
136 else if (mode == mode_T || mode == mode_M) {
137 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
141 assert(0 && "unsupported Phi-Mode");
145 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
153 static void ppc32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
158 if (get_irn_mode(irn) == mode_X) {
162 pos = ppc32_translate_proj_pos(irn);
163 irn = my_skip_proj(irn);
166 if (is_ppc32_irn(irn)) {
167 const arch_register_t **slots;
169 slots = get_ppc32_slots(irn);
173 /* here we set the registers for the Phi nodes */
174 ppc32_set_firm_reg(irn, reg, cur_reg_set);
178 static const arch_register_t *ppc32_get_irn_reg(const void *self, const ir_node *irn) {
180 const arch_register_t *reg = NULL;
184 if (get_irn_mode(irn) == mode_X) {
188 pos = ppc32_translate_proj_pos(irn);
189 irn = my_skip_proj(irn);
192 if (is_ppc32_irn(irn)) {
193 const arch_register_t **slots;
194 slots = get_ppc32_slots(irn);
198 reg = ppc32_get_firm_reg(irn, cur_reg_set);
204 static arch_irn_class_t ppc32_classify(const void *self, const ir_node *irn) {
205 irn = my_skip_proj(irn);
208 return arch_irn_class_branch;
210 else if (is_ppc32_irn(irn)) {
211 return arch_irn_class_normal;
217 static arch_irn_flags_t ppc32_get_flags(const void *self, const ir_node *irn) {
218 irn = my_skip_proj(irn);
220 if (is_ppc32_irn(irn)) {
221 return get_ppc32_flags(irn);
223 else if (is_Unknown(irn)) {
224 return arch_irn_flags_ignore;
230 static entity *ppc32_get_frame_entity(const void *self, const ir_node *irn) {
231 if(!is_ppc32_irn(irn)) return NULL;
232 if(get_ppc32_type(irn)!=ppc32_ac_FrameEntity) return NULL;
233 return get_ppc32_frame_entity(irn);
237 * This function is called by the generic backend to correct offsets for
238 * nodes accessing the stack.
240 static void ppc32_set_stack_bias(const void *self, ir_node *irn, int bias) {
241 set_ppc32_offset(irn, bias);
246 const be_abi_call_t *call;
251 * Initialize the callback object.
252 * @param call The call object.
253 * @param aenv The architecture environment.
254 * @param irg The graph with the method.
255 * @return Some pointer. This pointer is passed to all other callback functions as self object.
257 static void *ppc32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
259 ppc32_abi_env *env = xmalloc(sizeof(ppc32_abi_env));
266 * Destroy the callback object.
267 * @param self The callback object.
269 static void ppc32_abi_done(void *self)
275 * Get the between type for that call.
276 * @param self The callback object.
277 * @return The between type of for that call.
279 static ir_type *ppc32_abi_get_between_type(void *self)
281 static ir_type *between_type = NULL;
282 static entity *old_bp_ent = NULL;
285 entity *ret_addr_ent;
286 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
287 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
289 between_type = new_type_class(new_id_from_str("ppc32_between_type"));
290 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
291 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
293 set_entity_offset_bytes(old_bp_ent, 0);
294 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
295 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
302 * Put all registers which are saved by the prologue/epilogue in a set.
303 * @param self The callback object.
306 static void ppc32_abi_regs_saved_by_me(void *self, pset *regs)
311 * Generate the prologue.
312 * @param self The callback object.
313 * @param mem A pointer to the mem node. Update this if you define new memory.
314 * @param reg_map A mapping mapping all callee_save/ignore/parameter registers to their defining nodes.
315 * @return The register which shall be used as a stack frame base.
317 * All nodes which define registers in @p reg_map must keep @p reg_map current.
319 static const arch_register_t *ppc32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
321 ppc32_abi_env *env = (ppc32_abi_env *) self;
322 be_abi_call_flags_t flags = be_abi_call_get_flags(env->call);
323 isleaf = flags.bits.irg_is_leaf;
325 if(flags.bits.try_omit_fp)
326 return &ppc32_general_purpose_regs[REG_R1];
328 return &ppc32_general_purpose_regs[REG_R31];
332 * Generate the epilogue.
333 * @param self The callback object.
334 * @param mem Memory one can attach to.
335 * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
337 * All nodes which define registers in @p reg_map must keep @p reg_map current.
338 * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
340 static void ppc32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
344 static const be_abi_callbacks_t ppc32_abi_callbacks = {
347 ppc32_abi_get_between_type,
348 ppc32_abi_regs_saved_by_me,
353 /* fill register allocator interface */
355 static const arch_irn_ops_if_t ppc32_irn_ops_if = {
356 ppc32_get_irn_reg_req,
361 ppc32_get_frame_entity,
365 ppc32_irn_ops_t ppc32_irn_ops = {
372 /**************************************************
375 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
376 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
377 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
378 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
381 **************************************************/
383 static void ppc32_before_abi(void *self) {
384 ppc32_code_gen_t *cg = self;
385 ir_type *frame_type = get_irg_frame_type(cg->irg);
387 frame_alloc_area(frame_type, 24, 4, 1);
389 ppc32_init_conv_walk();
390 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_conv_walk, cg);
393 if(cg->area_size < 32) cg->area_size = 32;
394 cg->area = frame_alloc_area(get_irg_frame_type(cg->irg), cg->area_size+24, 16, 1);
398 static void ppc32_search_start_successor(ir_node *block, void *env) {
399 ppc32_code_gen_t *cg = env;
400 int n = get_Block_n_cfgpreds(block);
401 ir_node *startblock = get_irg_start_block(cg->irg);
402 if(block == startblock) return;
404 for (n--; n >= 0; n--) {
405 ir_node *predblock = get_irn_n(get_Block_cfgpred(block, n), -1);
406 if(predblock == startblock)
408 cg->start_succ_block = block;
415 * Transforms the standard firm graph into
418 static void ppc32_prepare_graph(void *self) {
419 ppc32_code_gen_t *cg = self;
421 irg_block_walk_graph(cg->irg, NULL, ppc32_search_start_successor, cg);
422 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_pretransform_walk, cg);
423 be_dump(cg->irg, "-pretransformed", dump_ir_block_graph);
425 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_node, cg);
426 be_dump(cg->irg, "-transformed", dump_ir_block_graph);
427 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_const, cg);
433 * Called immediatly before emit phase.
435 static void ppc32_finish_irg(ir_graph *irg, ppc32_code_gen_t *cg) {
436 /* TODO: - fix offsets for nodes accessing stack
443 * These are some hooks which must be filled but are probably not needed.
445 static void ppc32_before_sched(void *self) {
446 /* Some stuff you need to do after scheduling but before register allocation */
450 * Called before the register allocator.
451 * Calculate a block schedule here. We need it for the x87
452 * simulator and the emitter.
454 static void ppc32_before_ra(void *self) {
455 ppc32_code_gen_t *cg = self;
456 cg->blk_sched = sched_create_block_schedule(cg->irg);
459 static void ppc32_transform_spill(ir_node *node, void *env)
461 ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
463 if(be_is_Spill(node))
465 ir_node *store, *proj;
466 dbg_info *dbg = get_irn_dbg_info(node);
467 ir_node *block = get_nodes_block(node);
469 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, 1);
471 if (regclass == &ppc32_reg_classes[CLASS_ppc32_general_purpose])
473 store = new_rd_ppc32_Stw(dbg, current_ir_graph, block,
474 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph), mode_T);
476 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_floating_point])
478 store = new_rd_ppc32_Stfd(dbg, current_ir_graph, block,
479 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph), mode_T);
481 else assert(0 && "Spill for register class not supported yet!");
483 set_ppc32_frame_entity(store, be_get_frame_entity(node));
485 proj = new_rd_Proj(dbg, current_ir_graph, block, store, mode_M, pn_Store_M);
487 if (sched_is_scheduled(node)) {
488 sched_add_after(sched_prev(node), store);
489 sched_add_after(store, proj);
494 exchange(node, proj);
497 if(be_is_Reload(node))
499 ir_node *load, *proj;
500 const arch_register_t *reg;
501 dbg_info *dbg = get_irn_dbg_info(node);
502 ir_node *block = get_nodes_block(node);
503 ir_mode *mode = get_irn_mode(node);
505 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, -1);
507 if (regclass == &ppc32_reg_classes[CLASS_ppc32_general_purpose])
509 load = new_rd_ppc32_Lwz(dbg, current_ir_graph, block,
510 get_irn_n(node, 0), get_irn_n(node, 1), mode_T);
512 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_floating_point])
514 load = new_rd_ppc32_Lfd(dbg, current_ir_graph, block,
515 get_irn_n(node, 0), get_irn_n(node, 1), mode_T);
517 else assert(0 && "Reload for register class not supported yet!");
519 set_ppc32_frame_entity(load, be_get_frame_entity(node));
521 proj = new_rd_Proj(dbg, current_ir_graph, block, load, mode, pn_Load_res);
523 if (sched_is_scheduled(node)) {
524 sched_add_after(sched_prev(node), load);
525 sched_add_after(load, proj);
530 /* copy the register from the old node to the new Load */
531 reg = arch_get_irn_register(cgenv->arch_env, node);
532 arch_set_irn_register(cgenv->arch_env, load, reg);
534 exchange(node, proj);
539 * Some stuff to do immediately after register allocation
541 static void ppc32_after_ra(void *self) {
542 ppc32_code_gen_t *cg = self;
543 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_spill, cg);
547 * Emits the code, closes the output file and frees
548 * the code generator interface.
550 static void ppc32_emit_and_done(void *self) {
551 ppc32_code_gen_t *cg = self;
552 ir_graph *irg = cg->irg;
555 if (cg->emit_decls) {
556 ppc32_gen_decls(cg->out);
560 ppc32_finish_irg(irg, cg);
561 dump_ir_block_graph_sched(irg, "-ppc-finished");
562 ppc32_gen_routine(out, irg, cg);
566 /* de-allocate code generator */
567 del_set(cg->reg_set);
572 del_pset(symbol_pset);
577 int is_direct_entity(entity *ent);
580 * Collects all SymConsts which need to be accessed "indirectly"
582 * @param node the firm node
583 * @param env the debug module
585 void ppc32_collect_symconsts_walk(ir_node *node, void *env) {
586 if(get_irn_op(node) == op_SymConst)
588 entity *ent = get_SymConst_entity(node);
589 if(!is_direct_entity(ent))
590 pset_insert_ptr(symbol_pset, ent);
594 static void *ppc32_cg_init(FILE *F, const be_irg_t *birg);
596 static const arch_code_generator_if_t ppc32_code_gen_if = {
600 ppc32_before_sched, /* before scheduling hook */
601 ppc32_before_ra, /* before register allocation hook */
607 * Initializes the code generator.
609 static void *ppc32_cg_init(FILE *F, const be_irg_t *birg) {
610 ppc32_isa_t *isa = (ppc32_isa_t *)birg->main_env->arch_env->isa;
611 ppc32_code_gen_t *cg = xmalloc(sizeof(*cg));
613 cg->impl = &ppc32_code_gen_if;
615 cg->reg_set = new_set(ppc32_cmp_irn_reg_assoc, 1024);
617 cg->arch_env = birg->main_env->arch_env;
621 cg->start_succ_block = NULL;
622 cg->blk_sched = NULL;
623 FIRM_DBG_REGISTER(cg->mod, "firm.be.ppc.cg");
627 if (isa->num_codegens > 1)
633 symbol_pset = pset_new_ptr(8);
634 for(i=0; i<get_irp_n_irgs(); i++)
636 cg->irg = get_irp_irg(i);
637 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_collect_symconsts_walk, cg);
642 cur_reg_set = cg->reg_set;
644 ppc32_irn_ops.cg = cg;
646 return (arch_code_generator_t *)cg;
651 /*****************************************************************
652 * ____ _ _ _____ _____
653 * | _ \ | | | | |_ _|/ ____| /\
654 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
655 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
656 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
657 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
659 *****************************************************************/
661 static ppc32_isa_t ppc32_isa_template = {
663 &ppc32_general_purpose_regs[REG_R1], // stack pointer
664 &ppc32_general_purpose_regs[REG_R31], // base pointer
665 -1, // stack is decreasing
666 0 // num codegens... ??
670 * Initializes the backend ISA and opens the output file.
672 static void *ppc32_init(void) {
673 static int inited = 0;
679 isa = xcalloc(1, sizeof(*isa));
680 memcpy(isa, &ppc32_isa_template, sizeof(*isa));
682 ppc32_register_init(isa);
683 ppc32_create_opcodes();
693 * Closes the output file and frees the ISA structure.
695 static void ppc32_done(void *self) {
701 static int ppc32_get_n_reg_class(const void *self) {
705 static const arch_register_class_t *ppc32_get_reg_class(const void *self, int i) {
706 assert(i >= 0 && i < N_CLASSES && "Invalid ppc register class requested.");
707 return &ppc32_reg_classes[i];
713 * Get the register class which shall be used to store a value of a given mode.
714 * @param self The this pointer.
715 * @param mode The mode in question.
716 * @return A register class which can hold values of the given mode.
718 const arch_register_class_t *ppc32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
719 if (mode_is_float(mode))
720 return &ppc32_reg_classes[CLASS_ppc32_floating_point];
722 return &ppc32_reg_classes[CLASS_ppc32_general_purpose];
727 * Get the ABI restrictions for procedure calls.
728 * @param self The this pointer.
729 * @param method_type The type of the method (procedure) in question.
730 * @param abi The abi object to be modified
732 static void ppc32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
735 int i, n = get_method_n_params(method_type);
736 int stackoffs = 0, lastoffs = 0, stackparamsize;
741 const arch_register_t *reg;
742 be_abi_call_flags_t call_flags = { { 0, 0, 1, 0, 0, 0, 1 } };
744 if(get_type_visibility(method_type)!=visibility_external_allocated)
745 call_flags.bits.call_has_imm = 1;
747 /* set stack parameter passing style */
748 be_abi_call_set_flags(abi, call_flags, &ppc32_abi_callbacks);
750 for (i = 0; i < n; i++) {
751 tp = get_method_param_type(method_type, i);
752 if(is_atomic_type(tp))
754 mode = get_type_mode(tp);
756 if(mode_is_float(mode))
758 if(fpregi <= REG_F13)
760 if(get_mode_size_bits(mode) == 32) gpregi++, stackparamsize=4;
761 else gpregi += 2, stackparamsize=8; // mode == irm_D
762 reg = &ppc32_floating_point_regs[fpregi++];
766 if(get_mode_size_bits(mode) == 32) stackparamsize=4;
767 else stackparamsize=8; // mode == irm_D
773 if(gpregi <= REG_R10)
774 reg = &ppc32_general_purpose_regs[gpregi++];
781 be_abi_call_param_reg(abi, i, reg);
784 be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
785 lastoffs = stackoffs+stackparamsize;
787 stackoffs += stackparamsize;
791 be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
792 stackoffs += (get_type_size_bytes(tp)+3) & -4;
793 lastoffs = stackoffs;
797 /* explain where result can be found if any */
798 if (get_method_n_ress(method_type) > 0) {
799 tp = get_method_res_type(method_type, 0);
800 mode = get_type_mode(tp);
802 be_abi_call_res_reg(abi, 0,
803 mode_is_float(mode) ? &ppc32_floating_point_regs[REG_F1] : &ppc32_general_purpose_regs[REG_R3]);
807 static const void *ppc32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
808 return &ppc32_irn_ops;
811 const arch_irn_handler_t ppc32_irn_handler = {
815 const arch_irn_handler_t *ppc32_get_irn_handler(const void *self) {
816 return &ppc32_irn_handler;
819 int ppc32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
820 return is_ppc32_irn(irn);
824 * Initializes the code generator interface.
826 static const arch_code_generator_if_t *ppc32_get_code_generator_if(void *self) {
827 return &ppc32_code_gen_if;
830 list_sched_selector_t ppc32_sched_selector;
833 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
835 static const list_sched_selector_t *ppc32_get_list_sched_selector(const void *self) {
836 memcpy(&ppc32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
837 ppc32_sched_selector.to_appear_in_schedule = ppc32_to_appear_in_schedule;
838 return &ppc32_sched_selector;
842 static void ppc32_register_options(lc_opt_entry_t *ent)
845 #endif /* WITH_LIBCORE */
847 const arch_isa_if_t ppc32_isa_if = {
849 ppc32_register_options,
853 ppc32_get_n_reg_class,
855 ppc32_get_reg_class_for_mode,
857 ppc32_get_irn_handler,
858 ppc32_get_code_generator_if,
859 ppc32_get_list_sched_selector,