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 ppc backend driver file.
23 * @author Moritz Kroll, Jens Mueller
28 #include "pseudo_irg.h"
40 #include "../bearch_t.h" /* the general register allocator interface */
41 #include "../benode_t.h"
42 #include "../belower.h"
43 #include "../besched_t.h"
46 #include "../bemachine.h"
47 #include "../bemodule.h"
48 #include "../bespillslots.h"
49 #include "../beblocksched.h"
50 #include "../beirg_t.h"
51 #include "../begnuas.h"
55 #include "bearch_ppc32_t.h"
57 #include "ppc32_new_nodes.h" /* ppc nodes interface */
58 #include "gen_ppc32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
59 #include "ppc32_transform.h"
60 #include "ppc32_transform_conv.h"
61 #include "ppc32_emitter.h"
62 #include "ppc32_map_regs.h"
64 #define DEBUG_MODULE "firm.be.ppc.isa"
68 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
69 static set *cur_reg_set = NULL;
71 /**************************************************
74 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
75 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
76 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
77 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
80 **************************************************/
83 * Return register requirements for a ppc node.
84 * If the node returns a tuple (mode_T) then the proj's
85 * will be asked for this information.
87 static const arch_register_req_t *ppc32_get_irn_reg_req(const ir_node *irn,
90 long node_pos = pos == -1 ? 0 : pos;
91 ir_mode *mode = get_irn_mode(irn);
92 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
94 if (is_Block(irn) || mode == mode_X || mode == mode_M) {
95 DBG((mod, LEVEL_1, "ignoring block, mode_X or mode_M node %+F\n", irn));
96 return arch_no_register_req;
99 if (mode == mode_T && pos < 0) {
100 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F", irn));
101 return arch_no_register_req;
104 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
107 /* in case of a proj, we need to get the correct OUT slot */
108 /* of the node corresponding to the proj number */
110 node_pos = ppc32_translate_proj_pos(irn);
115 irn = skip_Proj_const(irn);
117 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
120 /* get requirements for our own nodes */
121 if (is_ppc32_irn(irn)) {
122 const arch_register_req_t *req;
124 req = get_ppc32_in_req(irn, pos);
126 req = get_ppc32_out_req(irn, node_pos);
129 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
133 /* unknowns should be transformed by now */
134 assert(!is_Unknown(irn));
136 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
137 return arch_no_register_req;
140 static arch_irn_class_t ppc32_classify(const ir_node *irn)
142 irn = skip_Proj_const(irn);
145 return arch_irn_class_branch;
151 static ir_entity *ppc32_get_frame_entity(const ir_node *irn)
153 if(!is_ppc32_irn(irn)) return NULL;
154 if(get_ppc32_type(irn)!=ppc32_ac_FrameEntity) return NULL;
155 return get_ppc32_frame_entity(irn);
158 static void ppc32_set_frame_entity(ir_node *irn, ir_entity *ent)
160 if (! is_ppc32_irn(irn) || get_ppc32_type(irn) != ppc32_ac_FrameEntity)
162 set_ppc32_frame_entity(irn, ent);
166 * This function is called by the generic backend to correct offsets for
167 * nodes accessing the stack.
169 static void ppc32_set_stack_bias(ir_node *irn, int bias)
171 set_ppc32_offset(irn, bias);
174 static int ppc32_get_sp_bias(const ir_node *irn)
182 const be_abi_call_t *call;
187 * Initialize the callback object.
188 * @param call The call object.
189 * @param aenv The architecture environment.
190 * @param irg The graph with the method.
191 * @return Some pointer. This pointer is passed to all other callback functions as self object.
193 static void *ppc32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
195 ppc32_abi_env *env = XMALLOC(ppc32_abi_env);
204 * Destroy the callback object.
205 * @param self The callback object.
207 static void ppc32_abi_done(void *self)
213 * Get the between type for that call.
214 * @param self The callback object.
215 * @return The between type of for that call.
217 static ir_type *ppc32_abi_get_between_type(void *self)
219 static ir_type *between_type = NULL;
220 static ir_entity *old_bp_ent = NULL;
224 ir_entity *ret_addr_ent;
225 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
226 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
228 between_type = new_type_class(new_id_from_str("ppc32_between_type"));
229 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
230 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
232 set_entity_offset(old_bp_ent, 0);
233 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
234 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
241 * Generate the prologue.
242 * @param self The callback object.
243 * @param mem A pointer to the mem node. Update this if you define new memory.
244 * @param reg_map A mapping mapping all callee_save/ignore/parameter registers to their defining nodes.
245 * @param stack_bias Points to the current stack bias, can be modified if needed.
247 * @return The register which shall be used as a stack frame base.
249 * All nodes which define registers in @p reg_map must keep @p reg_map current.
251 static const arch_register_t *ppc32_abi_prologue(void *self, ir_node **mem, pmap *reg_map, int *stack_bias)
253 ppc32_abi_env *env = (ppc32_abi_env *) self;
254 be_abi_call_flags_t flags = be_abi_call_get_flags(env->call);
258 isleaf = flags.bits.irg_is_leaf;
260 if (flags.bits.try_omit_fp)
261 return &ppc32_gp_regs[REG_R1];
263 return &ppc32_gp_regs[REG_R31];
267 * Generate the epilogue.
268 * @param self The callback object.
269 * @param mem Memory one can attach to.
270 * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
272 * All nodes which define registers in @p reg_map must keep @p reg_map current.
273 * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
275 static void ppc32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
283 static const be_abi_callbacks_t ppc32_abi_callbacks = {
286 ppc32_abi_get_between_type,
291 /* fill register allocator interface */
293 static const arch_irn_ops_t ppc32_irn_ops = {
294 ppc32_get_irn_reg_req,
296 ppc32_get_frame_entity,
297 ppc32_set_frame_entity,
298 ppc32_set_stack_bias,
300 NULL, /* get_inverse */
301 NULL, /* get_op_estimated_cost */
302 NULL, /* possible_memory_operand */
303 NULL, /* perform_memory_operand */
306 /**************************************************
309 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
310 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
311 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
312 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
315 **************************************************/
317 static void ppc32_before_abi(void *self) {
318 ppc32_code_gen_t *cg = self;
319 ir_type *frame_type = get_irg_frame_type(cg->irg);
321 frame_alloc_area(frame_type, 24, 4, 1);
323 ppc32_init_conv_walk();
324 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_conv_walk, cg);
327 if(cg->area_size < 32) cg->area_size = 32;
328 cg->area = frame_alloc_area(get_irg_frame_type(cg->irg), cg->area_size+24, 16, 1);
332 static void ppc32_search_start_successor(ir_node *block, void *env) {
333 ppc32_code_gen_t *cg = env;
334 int n = get_Block_n_cfgpreds(block);
335 ir_node *startblock = get_irg_start_block(cg->irg);
336 if(block == startblock) return;
338 for (n--; n >= 0; n--) {
339 ir_node *predblock = get_irn_n(get_Block_cfgpred(block, n), -1);
340 if(predblock == startblock)
342 cg->start_succ_block = block;
349 * Transforms the standard firm graph into
352 static void ppc32_prepare_graph(void *self) {
353 ppc32_code_gen_t *cg = self;
355 irg_block_walk_graph(cg->irg, NULL, ppc32_search_start_successor, cg);
356 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_pretransform_walk, cg);
357 be_dump(cg->irg, "-pretransformed", dump_ir_block_graph);
359 ppc32_register_transformers();
360 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_node, cg);
361 be_dump(cg->irg, "-transformed", dump_ir_block_graph);
362 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_const, cg);
368 * Called immediatly before emit phase.
370 static void ppc32_finish_irg(void *self) {
372 /* TODO: - fix offsets for nodes accessing stack
379 * Called before the register allocator.
380 * Calculate a block schedule here. We need it for the x87
381 * simulator and the emitter.
383 static void ppc32_before_ra(void *self) {
384 ppc32_code_gen_t *cg = self;
385 cg->blk_sched = be_create_block_schedule(cg->irg, cg->birg->exec_freq);
388 static void ppc32_transform_spill(ir_node *node, void *env)
392 if(be_is_Spill(node))
394 ir_node *store, *proj;
395 dbg_info *dbg = get_irn_dbg_info(node);
396 ir_node *block = get_nodes_block(node);
398 const arch_register_class_t *regclass = arch_get_irn_reg_class(node, 1);
400 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
402 store = new_rd_ppc32_Stw(dbg, current_ir_graph, block,
403 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
405 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
407 store = new_rd_ppc32_Stfd(dbg, current_ir_graph, block,
408 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
410 else panic("Spill for register class not supported yet!");
412 set_ppc32_frame_entity(store, be_get_frame_entity(node));
414 proj = new_rd_Proj(dbg, current_ir_graph, block, store, mode_M, pn_Store_M);
416 if (sched_is_scheduled(node)) {
417 sched_add_after(sched_prev(node), store);
418 sched_add_after(store, proj);
423 exchange(node, proj);
426 if(be_is_Reload(node))
428 ir_node *load, *proj;
429 const arch_register_t *reg;
430 dbg_info *dbg = get_irn_dbg_info(node);
431 ir_node *block = get_nodes_block(node);
432 ir_mode *mode = get_irn_mode(node);
434 const arch_register_class_t *regclass = arch_get_irn_reg_class_out(node);
436 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
438 load = new_rd_ppc32_Lwz(dbg, current_ir_graph, block, get_irn_n(node, 0), get_irn_n(node, 1));
440 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
442 load = new_rd_ppc32_Lfd(dbg, current_ir_graph, block, get_irn_n(node, 0), get_irn_n(node, 1));
444 else panic("Reload for register class not supported yet!");
446 set_ppc32_frame_entity(load, be_get_frame_entity(node));
448 proj = new_rd_Proj(dbg, current_ir_graph, block, load, mode, pn_Load_res);
450 if (sched_is_scheduled(node)) {
451 sched_add_after(sched_prev(node), load);
452 sched_add_after(load, proj);
457 /* copy the register from the old node to the new Load */
458 reg = arch_get_irn_register(node);
459 arch_set_irn_register(load, reg);
461 exchange(node, proj);
466 * Some stuff to do immediately after register allocation
468 static void ppc32_after_ra(void *self) {
469 ppc32_code_gen_t *cg = self;
470 be_coalesce_spillslots(cg->birg);
471 irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_spill, NULL);
475 * Emits the code, closes the output file and frees
476 * the code generator interface.
478 static void ppc32_emit_and_done(void *self) {
479 ppc32_code_gen_t *cg = self;
480 ir_graph *irg = cg->irg;
482 dump_ir_block_graph_sched(irg, "-ppc-finished");
483 ppc32_gen_routine(cg, irg);
487 /* de-allocate code generator */
488 del_set(cg->reg_set);
492 int is_direct_entity(ir_entity *ent);
494 static void *ppc32_cg_init(be_irg_t *birg);
496 static const arch_code_generator_if_t ppc32_code_gen_if = {
498 NULL, /* get_pic_base */
502 ppc32_before_ra, /* before register allocation hook */
509 * Initializes the code generator.
511 static void *ppc32_cg_init(be_irg_t *birg) {
512 ppc32_isa_t *isa = (ppc32_isa_t *)birg->main_env->arch_env;
513 ppc32_code_gen_t *cg = XMALLOC(ppc32_code_gen_t);
515 cg->impl = &ppc32_code_gen_if;
517 cg->reg_set = new_set(ppc32_cmp_irn_reg_assoc, 1024);
522 cg->start_succ_block = NULL;
523 cg->blk_sched = NULL;
524 FIRM_DBG_REGISTER(cg->mod, "firm.be.ppc.cg");
526 cur_reg_set = cg->reg_set;
528 return (arch_code_generator_t *)cg;
533 /*****************************************************************
534 * ____ _ _ _____ _____
535 * | _ \ | | | | |_ _|/ ____| /\
536 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
537 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
538 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
539 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
541 *****************************************************************/
543 static ppc32_isa_t ppc32_isa_template = {
545 &ppc32_isa_if, /* isa interface */
546 &ppc32_gp_regs[REG_R1], /* stack pointer */
547 &ppc32_gp_regs[REG_R31], /* base pointer */
548 -1, /* stack is decreasing */
549 2, /* power of two stack alignment for calls, 2^2 == 4 */
550 NULL, /* main environment */
552 5, /* reload costs */
554 NULL /* symbol set */
558 * Collects all SymConsts which need to be accessed "indirectly"
560 * @param node the firm node
561 * @param env the symbol set
563 static void ppc32_collect_symconsts_walk(ir_node *node, void *env) {
564 pset *symbol_set = env;
566 if (is_SymConst(node)) {
567 ir_entity *ent = get_SymConst_entity(node);
568 set_entity_backend_marked(ent, 1);
569 if (! is_direct_entity(ent))
570 pset_insert_ptr(symbol_set, ent);
575 * Initializes the backend ISA and opens the output file.
577 static arch_env_t *ppc32_init(FILE *file_handle) {
578 static int inited = 0;
585 isa = XMALLOC(ppc32_isa_t);
586 memcpy(isa, &ppc32_isa_template, sizeof(*isa));
588 be_emit_init(file_handle);
590 ppc32_register_init();
591 ppc32_create_opcodes(&ppc32_irn_ops);
595 isa->symbol_set = pset_new_ptr(8);
596 for (i = 0; i < get_irp_n_irgs(); ++i) {
597 ir_graph *irg = get_irp_irg(i);
598 irg_walk_blkwise_graph(irg, NULL, ppc32_collect_symconsts_walk, isa->symbol_set);
601 /* we mark referenced global entities, so we can only emit those which
602 * are actually referenced. (Note: you mustn't use the type visited flag
603 * elsewhere in the backend)
605 inc_master_type_visited();
607 return &isa->arch_env;
610 static void ppc32_dump_indirect_symbols(ppc32_isa_t *isa) {
613 foreach_pset(isa->symbol_set, ent) {
614 const char *ld_name = get_entity_ld_name(ent);
615 be_emit_irprintf(".non_lazy_symbol_pointer\n%s:\n\t.indirect_symbol _%s\n\t.long 0\n\n", ld_name, ld_name);
616 be_emit_write_line();
621 * Closes the output file and frees the ISA structure.
623 static void ppc32_done(void *self) {
624 ppc32_isa_t *isa = self;
626 be_gas_emit_decls(isa->arch_env.main_env, 1);
627 be_gas_emit_switch_section(GAS_SECTION_DATA);
628 ppc32_dump_indirect_symbols(isa);
631 del_pset(isa->symbol_set);
638 static unsigned ppc32_get_n_reg_class(const void *self) {
643 static const arch_register_class_t *ppc32_get_reg_class(const void *self,
646 assert(i < N_CLASSES && "Invalid ppc register class requested.");
647 return &ppc32_reg_classes[i];
653 * Get the register class which shall be used to store a value of a given mode.
654 * @param self The this pointer.
655 * @param mode The mode in question.
656 * @return A register class which can hold values of the given mode.
658 const arch_register_class_t *ppc32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
660 if (mode_is_float(mode))
661 return &ppc32_reg_classes[CLASS_ppc32_fp];
663 return &ppc32_reg_classes[CLASS_ppc32_gp];
668 * Get the ABI restrictions for procedure calls.
669 * @param self The this pointer.
670 * @param method_type The type of the method (procedure) in question.
671 * @param abi The abi object to be modified
673 static void ppc32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
676 int i, n = get_method_n_params(method_type);
677 int stackoffs = 0, lastoffs = 0, stackparamsize;
682 const arch_register_t *reg;
683 be_abi_call_flags_t call_flags = { { 0, 0, 1, 0, 0, 0, 1 } };
686 if(get_type_visibility(method_type)!=visibility_external_allocated)
687 call_flags.bits.call_has_imm = 1;
689 /* set stack parameter passing style */
690 be_abi_call_set_flags(abi, call_flags, &ppc32_abi_callbacks);
692 for (i = 0; i < n; i++) {
693 tp = get_method_param_type(method_type, i);
694 mode = get_type_mode(tp);
695 if(is_atomic_type(tp))
697 if(mode_is_float(mode))
699 if(fpregi <= REG_F13)
701 if(get_mode_size_bits(mode) == 32) gpregi++, stackparamsize=4;
702 else gpregi += 2, stackparamsize=8; // mode == irm_D
703 reg = &ppc32_fp_regs[fpregi++];
707 if(get_mode_size_bits(mode) == 32) stackparamsize=4;
708 else stackparamsize=8; // mode == irm_D
714 if(gpregi <= REG_R10)
715 reg = &ppc32_gp_regs[gpregi++];
722 be_abi_call_param_reg(abi, i, reg);
725 be_abi_call_param_stack(abi, i, mode, 4, stackoffs - lastoffs, 0);
726 lastoffs = stackoffs+stackparamsize;
728 stackoffs += stackparamsize;
732 be_abi_call_param_stack(abi, i, mode, 4, stackoffs - lastoffs, 0);
733 stackoffs += (get_type_size_bytes(tp)+3) & -4;
734 lastoffs = stackoffs;
738 /* explain where result can be found if any */
739 if (get_method_n_ress(method_type) > 0) {
740 tp = get_method_res_type(method_type, 0);
741 mode = get_type_mode(tp);
743 be_abi_call_res_reg(abi, 0,
744 mode_is_float(mode) ? &ppc32_fp_regs[REG_F1] : &ppc32_gp_regs[REG_R3]);
748 int ppc32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
750 if(!is_ppc32_irn(irn))
757 * Initializes the code generator interface.
759 static const arch_code_generator_if_t *ppc32_get_code_generator_if(void *self) {
761 return &ppc32_code_gen_if;
764 list_sched_selector_t ppc32_sched_selector;
767 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
769 static const list_sched_selector_t *ppc32_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
772 ppc32_sched_selector = trivial_selector;
773 ppc32_sched_selector.to_appear_in_schedule = ppc32_to_appear_in_schedule;
774 return &ppc32_sched_selector;
777 static const ilp_sched_selector_t *ppc32_get_ilp_sched_selector(const void *self) {
783 * Returns the necessary byte alignment for storing a register of given class.
785 static int ppc32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
786 ir_mode *mode = arch_register_class_mode(cls);
789 return get_mode_size_bytes(mode);
792 static const be_execution_unit_t ***ppc32_get_allowed_execution_units(const void *self, const ir_node *irn) {
796 panic("Unimplemented ppc32_get_allowed_execution_units()");
800 static const be_machine_t *ppc32_get_machine(const void *self) {
803 panic("Unimplemented ppc32_get_machine()");
808 * Return irp irgs in the desired order.
810 static ir_graph **ppc32_get_irg_list(const void *self, ir_graph ***irg_list) {
817 * Returns the libFirm configuration parameter for this backend.
819 static const backend_params *ppc32_get_libfirm_params(void) {
820 static backend_params p = {
821 1, /* need dword lowering */
822 0, /* don't support inline assembler yet */
823 0, /* no immediate floating point mode. */
824 NULL, /* no additional opcodes */
825 NULL, /* will be set later */
826 NULL, /* but yet no creator function */
827 NULL, /* context for create_intrinsic_fkt */
828 NULL, /* no if conversion settings */
829 NULL /* no immediate fp mode */
835 static asm_constraint_flags_t ppc32_parse_asm_constraint(const void *self, const char **c)
837 /* no asm support yet */
840 return ASM_CONSTRAINT_FLAG_INVALID;
843 static int ppc32_is_valid_clobber(const void *self, const char *clobber)
845 /* no asm support yet */
851 const arch_isa_if_t ppc32_isa_if = {
854 NULL, /* handle intrinsics */
855 ppc32_get_n_reg_class,
857 ppc32_get_reg_class_for_mode,
859 ppc32_get_code_generator_if,
860 ppc32_get_list_sched_selector,
861 ppc32_get_ilp_sched_selector,
862 ppc32_get_reg_class_alignment,
863 ppc32_get_libfirm_params,
864 ppc32_get_allowed_execution_units,
867 NULL, /* mark remat */
868 ppc32_parse_asm_constraint,
869 ppc32_is_valid_clobber
872 void be_init_arch_ppc32(void)
874 be_register_isa_if("ppc32", &ppc32_isa_if);
877 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ppc32);