11 #include "pseudo_irg.h"
15 #include "iredges_t.h"
22 #include "../beabi.h" /* the general register allocator interface */
23 #include "../benode_t.h"
24 #include "../belower.h"
25 #include "../besched_t.h"
27 #include "bearch_ia32_t.h"
29 #include "ia32_new_nodes.h" /* ia32 nodes interface */
30 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
31 #include "ia32_gen_decls.h" /* interface declaration emitter */
32 #include "ia32_transform.h"
33 #include "ia32_emitter.h"
34 #include "ia32_map_regs.h"
35 #include "ia32_optimize.h"
37 #define DEBUG_MODULE "firm.be.ia32.isa"
40 static set *cur_reg_set = NULL;
43 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
45 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
46 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_XXX]);
49 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
50 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_fp_regs[REG_XXXX]);
53 /**************************************************
56 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
57 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
58 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
59 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
62 **************************************************/
64 static ir_node *my_skip_proj(const ir_node *n) {
71 * Return register requirements for an ia32 node.
72 * If the node returns a tuple (mode_T) then the proj's
73 * will be asked for this information.
75 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
76 const ia32_register_req_t *irn_req;
77 long node_pos = pos == -1 ? 0 : pos;
78 ir_mode *mode = get_irn_mode(irn);
79 firm_dbg_module_t *mod = firm_dbg_register(DEBUG_MODULE);
80 const ia32_irn_ops_t *ops = self;
82 if (mode == mode_T || mode == mode_M) {
83 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
87 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
92 node_pos = ia32_translate_proj_pos(irn);
98 irn = my_skip_proj(irn);
100 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
103 if (is_ia32_irn(irn)) {
105 irn_req = get_ia32_in_req(irn, pos);
108 irn_req = get_ia32_out_req(irn, node_pos);
111 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
113 memcpy(req, &(irn_req->req), sizeof(*req));
115 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
116 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
117 req->other_same = get_irn_n(irn, irn_req->same_pos);
120 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
121 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
122 req->other_different = get_irn_n(irn, irn_req->different_pos);
126 /* treat Phi like Const with default requirements */
128 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
129 if (mode_is_float(mode))
130 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
131 else if (mode_is_int(mode) || mode_is_reference(mode))
132 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
133 else if (mode == mode_T || mode == mode_M) {
134 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
138 assert(0 && "unsupported Phi-Mode");
141 DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
149 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
153 pos = ia32_translate_proj_pos(irn);
154 irn = my_skip_proj(irn);
157 if (is_ia32_irn(irn)) {
158 const arch_register_t **slots;
160 slots = get_ia32_slots(irn);
164 ia32_set_firm_reg(irn, reg, cur_reg_set);
168 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
170 const arch_register_t *reg = NULL;
173 pos = ia32_translate_proj_pos(irn);
174 irn = my_skip_proj(irn);
177 if (is_ia32_irn(irn)) {
178 const arch_register_t **slots;
179 slots = get_ia32_slots(irn);
183 reg = ia32_get_firm_reg(irn, cur_reg_set);
189 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
190 irn = my_skip_proj(irn);
192 return arch_irn_class_branch;
193 else if (is_ia32_Call(irn))
194 return arch_irn_class_call;
195 else if (is_ia32_irn(irn))
196 return arch_irn_class_normal;
201 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
202 irn = my_skip_proj(irn);
203 if (is_ia32_irn(irn))
204 return get_ia32_flags(irn);
210 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn)
212 /* TODO: Implement */
216 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
217 if (is_ia32_use_frame(irn)) {
218 /* TODO: correct offset */
222 /* fill register allocator interface */
224 static const arch_irn_ops_if_t ia32_irn_ops_if = {
225 ia32_get_irn_reg_req,
230 ia32_get_frame_entity,
234 ia32_irn_ops_t ia32_irn_ops = {
241 /**************************************************
244 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
245 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
246 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
247 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
250 **************************************************/
253 * Transforms the standard firm graph into
256 static void ia32_prepare_graph(void *self) {
257 ia32_code_gen_t *cg = self;
259 irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
260 dump_ir_block_graph_sched(cg->irg, "-transformed");
261 edges_deactivate(cg->irg);
262 edges_activate(cg->irg);
263 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
264 dump_ir_block_graph_sched(cg->irg, "-am");
270 * Stack reservation and StackParam lowering.
272 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
279 * Dummy functions for hooks we don't need but which must be filled.
281 static void ia32_before_sched(void *self) {
284 static void ia32_before_ra(void *self) {
290 * Emits the code, closes the output file and frees
291 * the code generator interface.
293 static void ia32_codegen(void *self) {
294 ia32_code_gen_t *cg = self;
295 ir_graph *irg = cg->irg;
298 if (cg->emit_decls) {
299 ia32_gen_decls(cg->out);
303 ia32_finish_irg(irg, cg);
304 //dump_ir_block_graph_sched(irg, "-finished");
305 ia32_gen_routine(out, irg, cg);
309 pmap_destroy(cg->tv_ent);
310 pmap_destroy(cg->types);
312 /* de-allocate code generator */
313 del_set(cg->reg_set);
317 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
319 static const arch_code_generator_if_t ia32_code_gen_if = {
322 ia32_before_sched, /* before scheduling hook */
323 ia32_before_ra, /* before register allocation hook */
324 ia32_codegen /* emit && done */
328 * Initializes the code generator.
330 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
331 ia32_isa_t *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
332 ia32_code_gen_t *cg = xcalloc(1, sizeof(*cg));
334 cg->impl = &ia32_code_gen_if;
336 cg->reg_set = new_set(ia32_cmp_irn_reg_assoc, 1024);
337 cg->mod = firm_dbg_register("firm.be.ia32.cg");
339 cg->arch_env = birg->main_env->arch_env;
340 cg->types = pmap_create();
341 cg->tv_ent = pmap_create();
346 if (isa->num_codegens > 1)
351 cur_reg_set = cg->reg_set;
353 ia32_irn_ops.cg = cg;
355 return (arch_code_generator_t *)cg;
360 /*****************************************************************
361 * ____ _ _ _____ _____
362 * | _ \ | | | | |_ _|/ ____| /\
363 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
364 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
365 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
366 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
368 *****************************************************************/
370 static ia32_isa_t ia32_isa_template = {
372 &ia32_gp_regs[REG_ESP],
373 &ia32_gp_regs[REG_EBP],
379 * Initializes the backend ISA.
381 static void *ia32_init(void) {
382 static int inited = 0;
388 isa = xcalloc(1, sizeof(*isa));
389 memcpy(isa, &ia32_isa_template, sizeof(*isa));
391 ia32_register_init(isa);
392 ia32_create_opcodes();
402 * Closes the output file and frees the ISA structure.
404 static void ia32_done(void *self) {
410 static int ia32_get_n_reg_class(const void *self) {
414 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
415 assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
416 return &ia32_reg_classes[i];
420 * Get the register class which shall be used to store a value of a given mode.
421 * @param self The this pointer.
422 * @param mode The mode in question.
423 * @return A register class which can hold values of the given mode.
425 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
426 if (mode_is_float(mode))
427 return &ia32_reg_classes[CLASS_ia32_fp];
429 return &ia32_reg_classes[CLASS_ia32_gp];
433 * Produces the type which sits between the stack args and the locals on the stack.
434 * it will contain the return address and space to store the old base pointer.
435 * @return The Firm type modelling the ABI between type.
437 static ir_type *get_between_type(void)
439 static ir_type *between_type = NULL;
440 static entity *old_bp_ent = NULL;
443 entity *ret_addr_ent;
444 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
445 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
447 between_type = new_type_class(new_id_from_str("ia32_between_type"));
448 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
449 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
451 set_entity_offset_bytes(old_bp_ent, 0);
452 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
453 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
460 * Get the ABI restrictions for procedure calls.
461 * @param self The this pointer.
462 * @param method_type The type of the method (procedure) in question.
463 * @param abi The abi object to be modified
465 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
466 ir_type *between_type;
469 unsigned cc = get_method_calling_convention(method_type);
470 int n = get_method_n_params(method_type);
475 const arch_register_t *reg;
476 be_abi_call_flags_t call_flags = { 0, 0, 1, 0, 1 };
478 /* get the between type and the frame pointer save entity */
479 between_type = get_between_type();
481 /* set stack parameter passing style */
482 be_abi_call_set_flags(abi, call_flags, between_type);
484 /* collect the mode for each type */
485 modes = alloca(n * sizeof(modes[0]));
487 for (i = 0; i < n; i++) {
488 tp = get_method_param_type(method_type, i);
489 modes[i] = get_type_mode(tp);
492 /* set register parameters */
493 if (cc & cc_reg_param) {
494 /* determine the number of parameters passed via registers */
495 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
497 /* loop over all parameters and set the register requirements */
498 for (i = 0; i <= biggest_n; i++) {
499 reg = ia32_get_RegParam_reg(n, modes, i, cc);
500 assert(reg && "kaputt");
501 be_abi_call_param_reg(abi, i, reg);
508 /* set stack parameters */
509 for (i = stack_idx; i < n; i++) {
510 be_abi_call_param_stack(abi, i);
514 /* set return registers */
515 n = get_method_n_ress(method_type);
517 assert(n <= 2 && "more than two results not supported");
519 /* In case of 64bit returns, we will have two 32bit values */
521 tp = get_method_res_type(method_type, 0);
522 mode = get_type_mode(tp);
524 assert(!mode_is_float(mode) && "two FP results not supported");
526 tp = get_method_res_type(method_type, 1);
527 mode = get_type_mode(tp);
529 assert(!mode_is_float(mode) && "two FP results not supported");
531 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
532 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
535 tp = get_method_res_type(method_type, 0);
536 mode = get_type_mode(tp);
538 be_abi_call_res_reg(abi, 0, &ia32_fp_regs[mode_is_float(mode) ? REG_XMM0 : REG_EAX]);
543 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
544 return &ia32_irn_ops;
547 const arch_irn_handler_t ia32_irn_handler = {
551 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
552 return &ia32_irn_handler;
555 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
556 return is_ia32_irn(irn);
560 * Initializes the code generator interface.
562 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
563 return &ia32_code_gen_if;
566 list_sched_selector_t ia32_sched_selector;
569 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
571 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
572 memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
573 ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
574 return &ia32_sched_selector;
578 static void ia32_register_options(lc_opt_entry_t *ent)
581 #endif /* WITH_LIBCORE */
583 const arch_isa_if_t ia32_isa_if = {
585 ia32_register_options,
589 ia32_get_n_reg_class,
591 ia32_get_reg_class_for_mode,
593 ia32_get_irn_handler,
594 ia32_get_code_generator_if,
595 ia32_get_list_sched_selector