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 amd64 backend driver file.
23 * @version $Id: bearch_amd64.c 26909 2010-01-05 15:56:54Z matze $
27 #include "pseudo_irg.h"
39 #include "../bearch.h"
40 #include "../benode.h"
41 #include "../belower.h"
42 #include "../besched.h"
44 #include "../bemodule.h"
45 #include "../begnuas.h"
46 #include "../belistsched.h"
47 #include "../beflags.h"
48 #include "../bespillslots.h"
50 #include "bearch_amd64_t.h"
52 #include "amd64_new_nodes.h"
53 #include "gen_amd64_regalloc_if.h"
54 #include "amd64_transform.h"
55 #include "amd64_emitter.h"
57 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59 static arch_irn_class_t amd64_classify(const ir_node *irn)
65 static ir_entity *amd64_get_frame_entity(const ir_node *node)
67 if (is_amd64_FrameAddr(node)) {
68 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
71 } else if (is_amd64_Store(node)) {
72 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
75 } else if (is_amd64_Load(node)) {
76 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
81 /* TODO: return the ir_entity assigned to the frame */
85 static void amd64_set_frame_entity(ir_node *node, ir_entity *ent)
89 /* TODO: set the ir_entity assigned to the frame */
93 * This function is called by the generic backend to correct offsets for
94 * nodes accessing the stack.
96 static void amd64_set_frame_offset(ir_node *irn, int offset)
98 if (is_amd64_FrameAddr(irn)) {
99 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
100 attr->fp_offset += offset;
102 } else if (is_amd64_Store(irn)) {
103 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
104 attr->fp_offset += offset;
106 } else if (is_amd64_Load(irn)) {
107 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
108 attr->fp_offset += offset;
113 static int amd64_get_sp_bias(const ir_node *irn)
119 /* fill register allocator interface */
121 static const arch_irn_ops_t amd64_irn_ops = {
124 amd64_get_frame_entity,
125 amd64_set_frame_entity,
126 amd64_set_frame_offset,
128 NULL, /* get_inverse */
129 NULL, /* get_op_estimated_cost */
130 NULL, /* possible_memory_operand */
131 NULL, /* perform_memory_operand */
137 * Transforms the standard firm graph into
140 static void amd64_prepare_graph(void *self)
142 amd64_code_gen_t *cg = self;
144 amd64_transform_graph (cg);
147 dump_ir_graph(cg->irg, "transformed");
152 * Called immediatly before emit phase.
154 static void amd64_finish_irg(void *self)
156 amd64_code_gen_t *cg = self;
157 ir_graph *irg = cg->irg;
159 dump_ir_graph(irg, "amd64-finished");
162 static void amd64_before_ra(void *self)
164 amd64_code_gen_t *cg = self;
166 be_sched_fix_flags(cg->birg, &amd64_reg_classes[CLASS_amd64_flags], 0);
170 static void transform_Reload(ir_node *node)
172 ir_graph *irg = get_irn_irg(node);
173 ir_node *block = get_nodes_block(node);
174 dbg_info *dbgi = get_irn_dbg_info(node);
175 ir_node *ptr = get_irg_frame(irg);
176 ir_node *mem = get_irn_n(node, be_pos_Reload_mem);
177 ir_mode *mode = get_irn_mode(node);
178 ir_entity *entity = be_get_frame_entity(node);
179 const arch_register_t *reg;
183 ir_node *sched_point = sched_prev(node);
185 load = new_bd_amd64_Load(dbgi, block, ptr, mem, entity);
186 sched_add_after(sched_point, load);
189 proj = new_rd_Proj(dbgi, load, mode, pn_amd64_Load_res);
191 reg = arch_get_irn_register(node);
192 arch_set_irn_register(proj, reg);
194 exchange(node, proj);
197 static void transform_Spill(ir_node *node)
199 ir_graph *irg = get_irn_irg(node);
200 ir_node *block = get_nodes_block(node);
201 dbg_info *dbgi = get_irn_dbg_info(node);
202 ir_node *ptr = get_irg_frame(irg);
203 ir_node *mem = new_NoMem();
204 ir_node *val = get_irn_n(node, be_pos_Spill_val);
205 //ir_mode *mode = get_irn_mode(val);
206 ir_entity *entity = be_get_frame_entity(node);
207 ir_node *sched_point;
210 sched_point = sched_prev(node);
211 store = new_bd_amd64_Store(dbgi, block, ptr, val, mem, entity);
214 sched_add_after(sched_point, store);
216 exchange(node, store);
219 static void amd64_after_ra_walker(ir_node *block, void *data)
221 ir_node *node, *prev;
224 for (node = sched_last(block); !sched_is_begin(node); node = prev) {
225 prev = sched_prev(node);
227 if (be_is_Reload(node)) {
228 transform_Reload(node);
229 } else if (be_is_Spill(node)) {
230 transform_Spill(node);
235 static void amd64_after_ra(void *self)
237 amd64_code_gen_t *cg = self;
238 be_coalesce_spillslots(cg->birg);
240 irg_block_walk_graph(cg->irg, NULL, amd64_after_ra_walker, NULL);
245 * Emits the code, closes the output file and frees
246 * the code generator interface.
248 static void amd64_emit_and_done(void *self)
250 amd64_code_gen_t *cg = self;
251 ir_graph *irg = cg->irg;
253 amd64_gen_routine(cg, irg);
255 /* de-allocate code generator */
259 static void *amd64_cg_init(be_irg_t *birg);
261 static const arch_code_generator_if_t amd64_code_gen_if = {
263 NULL, /* get_pic_base hook */
264 NULL, /* before abi introduce hook */
266 NULL, /* spill hook */
267 amd64_before_ra, /* before register allocation hook */
268 amd64_after_ra, /* after register allocation hook */
274 * Initializes the code generator.
276 static void *amd64_cg_init(be_irg_t *birg)
278 const arch_env_t *arch_env = be_get_irg_arch_env(birg->irg);
279 amd64_isa_t *isa = (amd64_isa_t *) arch_env;
280 amd64_code_gen_t *cg = XMALLOC(amd64_code_gen_t);
282 cg->impl = &amd64_code_gen_if;
283 cg->irg = be_get_birg_irg(birg);
286 cg->dump = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
288 return (arch_code_generator_t *)cg;
292 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
295 * Used to create per-graph unique pseudo nodes.
297 static inline ir_node *create_const(amd64_code_gen_t *cg, ir_node **place,
298 create_const_node_func func,
299 const arch_register_t* reg)
301 ir_node *block, *res;
306 block = get_irg_start_block(cg->irg);
307 res = func(NULL, block);
308 arch_set_irn_register(res, reg);
314 const arch_isa_if_t amd64_isa_if;
315 static amd64_isa_t amd64_isa_template = {
317 &amd64_isa_if, /* isa interface implementation */
318 &amd64_gp_regs[REG_RSP], /* stack pointer register */
319 &amd64_gp_regs[REG_RBP], /* base pointer register */
320 &amd64_reg_classes[CLASS_amd64_gp], /* link pointer register class */
321 -1, /* stack direction */
322 3, /* power of two stack alignment for calls, 2^2 == 4 */
323 NULL, /* main environment */
324 7, /* costs for a spill instruction */
325 5, /* costs for a reload instruction */
330 * Initializes the backend ISA
332 static arch_env_t *amd64_init(FILE *outfile)
334 static int run_once = 0;
341 isa = XMALLOC(amd64_isa_t);
342 memcpy(isa, &amd64_isa_template, sizeof(*isa));
344 be_emit_init(outfile);
346 amd64_register_init();
347 amd64_create_opcodes(&amd64_irn_ops);
349 return &isa->arch_env;
355 * Closes the output file and frees the ISA structure.
357 static void amd64_done(void *self)
359 amd64_isa_t *isa = self;
361 /* emit now all global declarations */
362 be_gas_emit_decls(isa->arch_env.main_env);
369 static unsigned amd64_get_n_reg_class(void)
374 static const arch_register_class_t *amd64_get_reg_class(unsigned i)
376 assert(i < N_CLASSES);
377 return &amd64_reg_classes[i];
383 * Get the register class which shall be used to store a value of a given mode.
384 * @param self The this pointer.
385 * @param mode The mode in question.
386 * @return A register class which can hold values of the given mode.
388 static const arch_register_class_t *amd64_get_reg_class_for_mode(const ir_mode *mode)
390 assert(!mode_is_float(mode));
391 return &amd64_reg_classes[CLASS_amd64_gp];
397 be_abi_call_flags_bits_t flags;
398 const arch_env_t *arch_env;
402 static void *amd64_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
404 amd64_abi_env_t *env = XMALLOC(amd64_abi_env_t);
405 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
406 env->flags = fl.bits;
408 env->arch_env = arch_env;
413 * Get the between type for that call.
414 * @param self The callback object.
415 * @return The between type of for that call.
417 static ir_type *amd64_get_between_type(void *self)
419 static ir_type *between_type = NULL;
420 static ir_entity *old_bp_ent = NULL;
424 ir_entity *ret_addr_ent;
425 ir_type *ret_addr_type = new_type_primitive(mode_P);
426 ir_type *old_bp_type = new_type_primitive(mode_P);
428 between_type = new_type_class(new_id_from_str("amd64_between_type"));
429 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
430 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
432 set_entity_offset(old_bp_ent, 0);
433 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
434 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
441 * Build the prolog, return the BASE POINTER register
443 static const arch_register_t *amd64_abi_prologue(void *self, ir_node **mem,
444 pmap *reg_map, int *stack_bias)
446 amd64_abi_env_t *env = self;
447 const arch_env_t *aenv = env->arch_env;
453 if (!env->flags.try_omit_fp) {
454 /* FIXME: maybe later here should be some code to generate
455 * the usual abi prologue */
456 return env->arch_env->bp;
459 return env->arch_env->sp;
462 /* Build the epilog */
463 static void amd64_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
466 amd64_abi_env_t *env = self;
467 const arch_env_t *aenv = env->arch_env;
468 ir_node *curr_sp = be_abi_reg_map_get(reg_map, aenv->sp);
469 ir_node *curr_bp = be_abi_reg_map_get(reg_map, aenv->bp);
473 if (env->flags.try_omit_fp) {
474 curr_sp = be_new_IncSP(aenv->sp, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
477 be_abi_reg_map_set(reg_map, aenv->sp, curr_sp);
478 be_abi_reg_map_set(reg_map, aenv->bp, curr_bp);
481 static const be_abi_callbacks_t amd64_abi_callbacks = {
484 amd64_get_between_type,
489 static const arch_register_t *gpreg_param_reg_std[] = {
490 &amd64_gp_regs[REG_RDI],
491 &amd64_gp_regs[REG_RSI],
492 &amd64_gp_regs[REG_RDX],
493 &amd64_gp_regs[REG_RCX],
494 &amd64_gp_regs[REG_R8],
495 &amd64_gp_regs[REG_R9],
498 static const arch_register_t *amd64_get_RegParam_reg(int n)
500 assert(n < 6 && n >=0 && "register param > 6 requested");
501 return gpreg_param_reg_std[n];
505 * Get the ABI restrictions for procedure calls.
506 * @param self The this pointer.
507 * @param method_type The type of the method (procedure) in question.
508 * @param abi The abi object to be modified
510 static void amd64_get_call_abi(const void *self, ir_type *method_type,
515 int i, n = get_method_n_params(method_type);
516 be_abi_call_flags_t call_flags;
521 /* set abi flags for calls */
522 call_flags.bits.left_to_right = 0;
523 call_flags.bits.store_args_sequential = 0;
524 call_flags.bits.try_omit_fp = 1;
525 call_flags.bits.fp_free = 0;
526 call_flags.bits.call_has_imm = 1;
528 /* set stack parameter passing style */
529 be_abi_call_set_flags(abi, call_flags, &amd64_abi_callbacks);
531 for (i = 0; i < n; i++) {
532 tp = get_method_param_type(method_type, i);
533 mode = get_type_mode(tp);
534 //d// printf ("MODE %p %p XX %d\n", mode, mode_Iu, i);
536 if (!no_reg && i < 6 && mode_is_data (mode)) {
537 //d// printf("TEST%d\n", i);
538 be_abi_call_param_reg(abi, i, amd64_get_RegParam_reg (i),
540 /* default: all parameters on stack */
543 be_abi_call_param_stack(abi, i, mode, 8, 0, 0, ABI_CONTEXT_BOTH);
547 /* TODO: set correct return register */
548 /* default: return value is in R0 resp. F0 */
549 if (get_method_n_ress(method_type) > 0) {
550 tp = get_method_res_type(method_type, 0);
551 mode = get_type_mode(tp);
553 /* FIXME: No floating point yet */
554 /* be_abi_call_res_reg(abi, 0,
555 mode_is_float(mode) ? &amd64_fp_regs[REG_F0] : &amd64_gp_regs[REG_R0], ABI_CONTEXT_BOTH) */;
557 be_abi_call_res_reg(abi, 0,
558 &amd64_gp_regs[REG_RAX], ABI_CONTEXT_BOTH);
562 static int amd64_to_appear_in_schedule(void *block_env, const ir_node *irn)
566 if(!is_amd64_irn(irn))
573 * Initializes the code generator interface.
575 static const arch_code_generator_if_t *amd64_get_code_generator_if(
579 return &amd64_code_gen_if;
582 list_sched_selector_t amd64_sched_selector;
585 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
587 static const list_sched_selector_t *amd64_get_list_sched_selector(
588 const void *self, list_sched_selector_t *selector)
593 amd64_sched_selector = trivial_selector;
594 amd64_sched_selector.to_appear_in_schedule = amd64_to_appear_in_schedule;
595 return &amd64_sched_selector;
598 static const ilp_sched_selector_t *amd64_get_ilp_sched_selector(
606 * Returns the necessary byte alignment for storing a register of given class.
608 static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
610 ir_mode *mode = arch_register_class_mode(cls);
611 return get_mode_size_bytes(mode);
615 * Returns the libFirm configuration parameter for this backend.
617 static const backend_params *amd64_get_backend_params(void) {
618 static backend_params p = {
619 0, /* no dword lowering */
620 0, /* no inline assembly */
621 NULL, /* will be set later */
622 NULL, /* no creator function */
623 NULL, /* context for create_intrinsic_fkt */
624 NULL, /* parameter for if conversion */
625 NULL, /* float arithmetic mode */
626 0, /* no trampoline support: size 0 */
627 0, /* no trampoline support: align 0 */
628 NULL, /* no trampoline support: no trampoline builder */
629 8 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
634 static const be_execution_unit_t ***amd64_get_allowed_execution_units(
643 static const be_machine_t *amd64_get_machine(const void *self)
651 static ir_graph **amd64_get_backend_irg_list(const void *self,
659 static asm_constraint_flags_t amd64_parse_asm_constraint(const char **c)
662 return ASM_CONSTRAINT_FLAG_INVALID;
665 static int amd64_is_valid_clobber(const char *clobber)
671 const arch_isa_if_t amd64_isa_if = {
674 NULL, /* handle intrinsics */
675 amd64_get_n_reg_class,
677 amd64_get_reg_class_for_mode,
679 amd64_get_code_generator_if,
680 amd64_get_list_sched_selector,
681 amd64_get_ilp_sched_selector,
682 amd64_get_reg_class_alignment,
683 amd64_get_backend_params,
684 amd64_get_allowed_execution_units,
686 amd64_get_backend_irg_list,
687 NULL, /* mark remat */
688 amd64_parse_asm_constraint,
689 amd64_is_valid_clobber
692 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_amd64);
693 void be_init_arch_amd64(void)
695 be_register_isa_if("amd64", &amd64_isa_if);
696 FIRM_DBG_REGISTER(dbg, "firm.be.amd64.cg");
697 amd64_init_transform();