1 /* The main TEMPLATE backend driver file. */
8 #include "pseudo_irg.h"
18 #include "../bearch.h" /* the general register allocator interface */
19 #include "../benode_t.h"
20 #include "../belower.h"
21 #include "../besched_t.h"
25 #include "bearch_TEMPLATE_t.h"
27 #include "TEMPLATE_new_nodes.h" /* TEMPLATE nodes interface */
28 #include "gen_TEMPLATE_regalloc_if.h" /* the generated interface (register type and class defenitions) */
29 #include "TEMPLATE_gen_decls.h" /* interface declaration emitter */
30 #include "TEMPLATE_transform.h"
31 #include "TEMPLATE_emitter.h"
32 #include "TEMPLATE_map_regs.h"
34 #define DEBUG_MODULE "firm.be.TEMPLATE.isa"
36 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
37 static set *cur_reg_set = NULL;
39 /**************************************************
42 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
43 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
44 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
45 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
48 **************************************************/
50 static ir_node *my_skip_proj(const ir_node *n) {
57 * Return register requirements for a TEMPLATE node.
58 * If the node returns a tuple (mode_T) then the proj's
59 * will be asked for this information.
61 static const arch_register_req_t *TEMPLATE_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
62 const TEMPLATE_register_req_t *irn_req;
63 long node_pos = pos == -1 ? 0 : pos;
64 ir_mode *mode = get_irn_mode(irn);
65 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
67 if (mode == mode_T || mode == mode_M) {
68 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
72 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
75 /* in case of a proj, we need to get the correct OUT slot */
76 /* of the node corresponding to the proj number */
78 node_pos = TEMPLATE_translate_proj_pos(irn);
84 irn = my_skip_proj(irn);
86 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
89 /* get requirements for our own nodes */
90 if (is_TEMPLATE_irn(irn)) {
92 irn_req = get_TEMPLATE_in_req(irn, pos);
95 irn_req = get_TEMPLATE_out_req(irn, node_pos);
98 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
100 memcpy(req, &(irn_req->req), sizeof(*req));
102 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
103 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
104 req->other_same = get_irn_n(irn, irn_req->same_pos);
107 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
108 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
109 req->other_different = get_irn_n(irn, irn_req->different_pos);
112 /* get requirements for FIRM nodes */
114 /* treat Phi like Const with default requirements */
116 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
118 if (mode_is_float(mode)) {
119 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_floating_point.req), sizeof(*req));
121 else if (mode_is_int(mode) || mode_is_reference(mode)) {
122 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_general_purpose.req), sizeof(*req));
124 else if (mode == mode_T || mode == mode_M) {
125 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
129 assert(0 && "unsupported Phi-Mode");
133 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
141 static void TEMPLATE_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
145 pos = TEMPLATE_translate_proj_pos(irn);
146 irn = my_skip_proj(irn);
149 if (is_TEMPLATE_irn(irn)) {
150 const arch_register_t **slots;
152 slots = get_TEMPLATE_slots(irn);
156 /* here we set the registers for the Phi nodes */
157 TEMPLATE_set_firm_reg(irn, reg, cur_reg_set);
161 static const arch_register_t *TEMPLATE_get_irn_reg(const void *self, const ir_node *irn) {
163 const arch_register_t *reg = NULL;
166 pos = TEMPLATE_translate_proj_pos(irn);
167 irn = my_skip_proj(irn);
170 if (is_TEMPLATE_irn(irn)) {
171 const arch_register_t **slots;
172 slots = get_TEMPLATE_slots(irn);
176 reg = TEMPLATE_get_firm_reg(irn, cur_reg_set);
182 static arch_irn_class_t TEMPLATE_classify(const void *self, const ir_node *irn) {
183 irn = my_skip_proj(irn);
186 return arch_irn_class_branch;
188 else if (is_TEMPLATE_irn(irn)) {
189 return arch_irn_class_normal;
195 static arch_irn_flags_t TEMPLATE_get_flags(const void *self, const ir_node *irn) {
196 irn = my_skip_proj(irn);
198 if (is_TEMPLATE_irn(irn)) {
199 return get_TEMPLATE_flags(irn);
201 else if (is_Unknown(irn)) {
202 return arch_irn_flags_ignore;
208 static entity *TEMPLATE_get_frame_entity(const void *self, const ir_node *irn) {
209 /* TODO: return the entity assigned to the frame */
213 static void TEMPLATE_set_frame_entity(const void *self, const ir_node *irn, entity *ent) {
214 /* TODO: set the entity assigned to the frame */
218 * This function is called by the generic backend to correct offsets for
219 * nodes accessing the stack.
221 static void TEMPLATE_set_stack_bias(const void *self, ir_node *irn, int bias) {
222 /* TODO: correct offset if irn accesses the stack */
225 /* fill register allocator interface */
227 static const arch_irn_ops_if_t TEMPLATE_irn_ops_if = {
228 TEMPLATE_get_irn_reg_req,
229 TEMPLATE_set_irn_reg,
230 TEMPLATE_get_irn_reg,
233 TEMPLATE_get_frame_entity,
234 TEMPLATE_set_frame_entity,
235 TEMPLATE_set_stack_bias
236 NULL, /* get_inverse */
237 NULL, /* get_op_estimated_cost */
238 NULL, /* possible_memory_operand */
239 NULL, /* perform_memory_operand */
242 TEMPLATE_irn_ops_t TEMPLATE_irn_ops = {
243 &TEMPLATE_irn_ops_if,
249 /**************************************************
252 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
253 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
254 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
255 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
258 **************************************************/
261 * Transforms the standard firm graph into
262 * a TEMLPATE firm graph
264 static void TEMPLATE_prepare_graph(void *self) {
265 TEMPLATE_code_gen_t *cg = self;
267 irg_walk_blkwise_graph(cg->irg, NULL, TEMPLATE_transform_node, cg);
273 * Called immediatly before emit phase.
275 static void TEMPLATE_finish_irg(void *self) {
276 TEMPLATE_code_gen_t *cg = self;
277 ir_graph *irg = cg->irg;
279 dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
284 * These are some hooks which must be filled but are probably not needed.
286 static void TEMPLATE_before_sched(void *self) {
287 /* Some stuff you need to do after scheduling but before register allocation */
290 static void TEMPLATE_before_ra(void *self) {
291 /* Some stuff you need to do after scheduling but before register allocation */
294 static void TEMPLATE_after_ra(void *self) {
295 /* Some stuff you need to do immediatly after register allocation */
301 * Emits the code, closes the output file and frees
302 * the code generator interface.
304 static void TEMPLATE_emit_and_done(void *self) {
305 TEMPLATE_code_gen_t *cg = self;
306 ir_graph *irg = cg->irg;
307 FILE *out = cg->isa->out;
309 if (cg->emit_decls) {
310 TEMPLATE_gen_decls(out);
314 TEMPLATE_gen_routine(out, irg, cg);
318 /* de-allocate code generator */
319 del_set(cg->reg_set);
323 static void *TEMPLATE_cg_init(const be_irg_t *birg);
325 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
327 TEMPLATE_prepare_graph,
328 TEMPLATE_before_sched, /* before scheduling hook */
329 TEMPLATE_before_ra, /* before register allocation hook */
330 TEMPLATE_after_ra, /* after register allocation hook */
332 TEMPLATE_emit_and_done
336 * Initializes the code generator.
338 static void *TEMPLATE_cg_init(const be_irg_t *birg) {
339 TEMPLATE_isa_t *isa = (TEMPLATE_isa_t *)birg->main_env->arch_env->isa;
340 TEMPLATE_code_gen_t *cg = xmalloc(sizeof(*cg));
342 cg->impl = &TEMPLATE_code_gen_if;
344 cg->reg_set = new_set(TEMPLATE_cmp_irn_reg_assoc, 1024);
345 cg->arch_env = birg->main_env->arch_env;
348 FIRM_DBG_REGISTER(cg->mod, "firm.be.TEMPLATE.cg");
352 if (isa->num_codegens > 1)
357 cur_reg_set = cg->reg_set;
359 TEMPLATE_irn_ops.cg = cg;
361 return (arch_code_generator_t *)cg;
366 /*****************************************************************
367 * ____ _ _ _____ _____
368 * | _ \ | | | | |_ _|/ ____| /\
369 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
370 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
371 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
372 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
374 *****************************************************************/
376 static TEMPLATE_isa_t TEMPLATE_isa_template = {
378 &TEMPLATE_general_purpose_regs[REG_R6],
379 &TEMPLATE_general_purpose_regs[REG_R7],
385 * Initializes the backend ISA and opens the output file.
387 static void *TEMPLATE_init(FILE *outfile) {
388 static int inited = 0;
394 isa = xcalloc(1, sizeof(*isa));
395 memcpy(isa, &TEMPLATE_isa_template, sizeof(*isa));
399 TEMPLATE_register_init(isa);
400 TEMPLATE_create_opcodes();
410 * Closes the output file and frees the ISA structure.
412 static void TEMPLATE_done(void *self) {
418 static int TEMPLATE_get_n_reg_class(const void *self) {
422 static const arch_register_class_t *TEMPLATE_get_reg_class(const void *self, int i) {
423 assert(i >= 0 && i < N_CLASSES && "Invalid TEMPLATE register class requested.");
424 return &TEMPLATE_reg_classes[i];
430 * Get the register class which shall be used to store a value of a given mode.
431 * @param self The this pointer.
432 * @param mode The mode in question.
433 * @return A register class which can hold values of the given mode.
435 const arch_register_class_t *TEMPLATE_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
436 if (mode_is_float(mode))
437 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_floating_point];
439 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_general_purpose];
445 * Produces the type which sits between the stack args and the locals on the stack.
446 * it will contain the return address and space to store the old base pointer.
447 * @return The Firm type modelling the ABI between type.
449 static ir_type *get_between_type(void) {
450 static ir_type *between_type = NULL;
451 static entity *old_bp_ent = NULL;
454 entity *ret_addr_ent;
455 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
456 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
458 between_type = new_type_class(new_id_from_str("TEMPLATE_between_type"));
459 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
460 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
462 set_entity_offset_bytes(old_bp_ent, 0);
463 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
464 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
471 * Get the ABI restrictions for procedure calls.
472 * @param self The this pointer.
473 * @param method_type The type of the method (procedure) in question.
474 * @param abi The abi object to be modified
476 void TEMPLATE_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
477 ir_type *between_type;
480 int i, n = get_method_n_params(method_type);
481 const arch_register_t *reg;
482 be_abi_call_flags_t call_flags;
484 /* set abi flags for calls */
485 call_flags.bits.left_to_right = 0;
486 call_flags.bits.store_args_sequential = 1;
487 call_flags.bits.try_omit_fp = 1;
488 call_flags.bits.fp_free = 0;
489 call_flags.bits.call_has_imm = 1;
491 /* get the between type and the frame pointer save entity */
492 between_type = get_between_type();
494 /* set stack parameter passing style */
495 be_abi_call_set_flags(abi, call_flags, between_type);
497 for (i = 0; i < n; i++) {
498 /* TODO: implement register parameter: */
499 /* reg = get reg for param i; */
500 /* be_abi_call_param_reg(abi, i, reg); */
502 /* default: all parameters on stack */
503 be_abi_call_param_stack(abi, i, 4, 0, 0);
506 /* TODO: set correct return register */
507 /* default: return value is in R0 resp. F0 */
508 if (get_method_n_ress(method_type) > 0) {
509 tp = get_method_res_type(method_type, 0);
510 mode = get_type_mode(tp);
512 be_abi_call_res_reg(abi, 0,
513 mode_is_float(mode) ? &TEMPLATE_floating_point_regs[REG_F0] : &TEMPLATE_general_purpose_regs[REG_R0]);
517 static const void *TEMPLATE_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
518 return &TEMPLATE_irn_ops;
521 const arch_irn_handler_t TEMPLATE_irn_handler = {
525 const arch_irn_handler_t *TEMPLATE_get_irn_handler(const void *self) {
526 return &TEMPLATE_irn_handler;
529 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn) {
530 return is_TEMPLATE_irn(irn);
534 * Initializes the code generator interface.
536 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(void *self) {
537 return &TEMPLATE_code_gen_if;
540 list_sched_selector_t TEMPLATE_sched_selector;
543 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
545 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(const void *self) {
546 memcpy(&TEMPLATE_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
547 TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
548 return &TEMPLATE_sched_selector;
552 * Returns the necessary byte alignment for storing a register of given class.
554 static int TEMPLATE_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
555 ir_mode *mode = arch_register_class_mode(cls);
556 return get_mode_size_bytes(mode);
560 * Returns the libFirm configuration parameter for this backend.
562 static const backend_params *TEMPLATE_get_libfirm_params(void) {
563 static arch_dep_params_t ad = {
565 0, /* Muls are fast enough on Firm */
566 31, /* shift would be ok */
571 static backend_params p = {
572 NULL, /* no additional opcodes */
573 NULL, /* will be set later */
574 0, /* no dword lowering */
575 NULL, /* no creator function */
576 NULL, /* context for create_intrinsic_fkt */
584 static void TEMPLATE_register_options(lc_opt_entry_t *ent)
587 #endif /* WITH_LIBCORE */
589 const arch_isa_if_t TEMPLATE_isa_if = {
592 TEMPLATE_get_n_reg_class,
593 TEMPLATE_get_reg_class,
594 TEMPLATE_get_reg_class_for_mode,
595 TEMPLATE_get_call_abi,
596 TEMPLATE_get_irn_handler,
597 TEMPLATE_get_code_generator_if,
598 TEMPLATE_get_list_sched_selector,
599 TEMPLATE_get_reg_class_alignment,
600 TEMPLATE_get_libfirm_params,
602 TEMPLATE_register_options