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 $
38 #include "../bearch.h"
39 #include "../benode.h"
40 #include "../belower.h"
41 #include "../besched.h"
43 #include "../bemodule.h"
44 #include "../begnuas.h"
45 #include "../belistsched.h"
46 #include "../beflags.h"
47 #include "../bespillslots.h"
49 #include "bearch_amd64_t.h"
51 #include "amd64_new_nodes.h"
52 #include "gen_amd64_regalloc_if.h"
53 #include "amd64_transform.h"
54 #include "amd64_emitter.h"
56 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
58 static arch_irn_class_t amd64_classify(const ir_node *irn)
64 static ir_entity *amd64_get_frame_entity(const ir_node *node)
66 if (is_amd64_FrameAddr(node)) {
67 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
70 } else if (is_amd64_Store(node)) {
71 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
74 } else if (is_amd64_Load(node)) {
75 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
80 /* TODO: return the ir_entity assigned to the frame */
85 * This function is called by the generic backend to correct offsets for
86 * nodes accessing the stack.
88 static void amd64_set_frame_offset(ir_node *irn, int offset)
90 if (is_amd64_FrameAddr(irn)) {
91 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
92 attr->fp_offset += offset;
94 } else if (is_amd64_Store(irn)) {
95 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
96 attr->fp_offset += offset;
98 } else if (is_amd64_Load(irn)) {
99 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
100 attr->fp_offset += offset;
105 static int amd64_get_sp_bias(const ir_node *irn)
111 /* fill register allocator interface */
113 static const arch_irn_ops_t amd64_irn_ops = {
116 amd64_get_frame_entity,
117 amd64_set_frame_offset,
119 NULL, /* get_inverse */
120 NULL, /* get_op_estimated_cost */
121 NULL, /* possible_memory_operand */
122 NULL, /* perform_memory_operand */
128 * Transforms the standard firm graph into
131 static void amd64_prepare_graph(void *self)
133 amd64_code_gen_t *cg = self;
135 amd64_transform_graph (cg);
138 dump_ir_graph(cg->irg, "transformed");
143 * Called immediatly before emit phase.
145 static void amd64_finish_irg(void *self)
147 amd64_code_gen_t *cg = self;
148 ir_graph *irg = cg->irg;
150 dump_ir_graph(irg, "amd64-finished");
153 static void amd64_before_ra(void *self)
155 amd64_code_gen_t *cg = self;
157 be_sched_fix_flags(cg->irg, &amd64_reg_classes[CLASS_amd64_flags],
162 static void transform_Reload(ir_node *node)
164 ir_graph *irg = get_irn_irg(node);
165 ir_node *block = get_nodes_block(node);
166 dbg_info *dbgi = get_irn_dbg_info(node);
167 ir_node *ptr = get_irg_frame(irg);
168 ir_node *mem = get_irn_n(node, be_pos_Reload_mem);
169 ir_mode *mode = get_irn_mode(node);
170 ir_entity *entity = be_get_frame_entity(node);
171 const arch_register_t *reg;
175 ir_node *sched_point = sched_prev(node);
177 load = new_bd_amd64_Load(dbgi, block, ptr, mem, entity);
178 sched_add_after(sched_point, load);
181 proj = new_rd_Proj(dbgi, load, mode, pn_amd64_Load_res);
183 reg = arch_get_irn_register(node);
184 arch_set_irn_register(proj, reg);
186 exchange(node, proj);
189 static void transform_Spill(ir_node *node)
191 ir_graph *irg = get_irn_irg(node);
192 ir_node *block = get_nodes_block(node);
193 dbg_info *dbgi = get_irn_dbg_info(node);
194 ir_node *ptr = get_irg_frame(irg);
195 ir_node *mem = new_NoMem();
196 ir_node *val = get_irn_n(node, be_pos_Spill_val);
197 //ir_mode *mode = get_irn_mode(val);
198 ir_entity *entity = be_get_frame_entity(node);
199 ir_node *sched_point;
202 sched_point = sched_prev(node);
203 store = new_bd_amd64_Store(dbgi, block, ptr, val, mem, entity);
206 sched_add_after(sched_point, store);
208 exchange(node, store);
211 static void amd64_after_ra_walker(ir_node *block, void *data)
213 ir_node *node, *prev;
216 for (node = sched_last(block); !sched_is_begin(node); node = prev) {
217 prev = sched_prev(node);
219 if (be_is_Reload(node)) {
220 transform_Reload(node);
221 } else if (be_is_Spill(node)) {
222 transform_Spill(node);
227 static void amd64_after_ra(void *self)
229 amd64_code_gen_t *cg = self;
230 be_coalesce_spillslots(cg->irg);
232 irg_block_walk_graph(cg->irg, NULL, amd64_after_ra_walker, NULL);
237 * Emits the code, closes the output file and frees
238 * the code generator interface.
240 static void amd64_emit_and_done(void *self)
242 amd64_code_gen_t *cg = self;
243 ir_graph *irg = cg->irg;
245 amd64_gen_routine(cg, irg);
247 /* de-allocate code generator */
251 static void *amd64_cg_init(ir_graph *irg);
253 static const arch_code_generator_if_t amd64_code_gen_if = {
255 NULL, /* get_pic_base hook */
256 NULL, /* before abi introduce hook */
258 NULL, /* spill hook */
259 amd64_before_ra, /* before register allocation hook */
260 amd64_after_ra, /* after register allocation hook */
266 * Initializes the code generator.
268 static void *amd64_cg_init(ir_graph *irg)
270 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
271 amd64_isa_t *isa = (amd64_isa_t *) arch_env;
272 amd64_code_gen_t *cg = XMALLOC(amd64_code_gen_t);
274 cg->impl = &amd64_code_gen_if;
277 cg->dump = (be_get_irg_options(irg)->dump_flags & DUMP_BE) ? 1 : 0;
279 return (arch_code_generator_t *)cg;
283 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
286 * Used to create per-graph unique pseudo nodes.
288 static inline ir_node *create_const(amd64_code_gen_t *cg, ir_node **place,
289 create_const_node_func func,
290 const arch_register_t* reg)
292 ir_node *block, *res;
297 block = get_irg_start_block(cg->irg);
298 res = func(NULL, block);
299 arch_set_irn_register(res, reg);
305 const arch_isa_if_t amd64_isa_if;
306 static amd64_isa_t amd64_isa_template = {
308 &amd64_isa_if, /* isa interface implementation */
309 &amd64_gp_regs[REG_RSP], /* stack pointer register */
310 &amd64_gp_regs[REG_RBP], /* base pointer register */
311 &amd64_reg_classes[CLASS_amd64_gp], /* link pointer register class */
312 -1, /* stack direction */
313 3, /* power of two stack alignment for calls, 2^2 == 4 */
314 NULL, /* main environment */
315 7, /* costs for a spill instruction */
316 5, /* costs for a reload instruction */
317 false, /* no custom abi handling */
322 * Initializes the backend ISA
324 static arch_env_t *amd64_init(FILE *outfile)
326 static int run_once = 0;
333 isa = XMALLOC(amd64_isa_t);
334 memcpy(isa, &amd64_isa_template, sizeof(*isa));
336 be_emit_init(outfile);
338 amd64_register_init();
339 amd64_create_opcodes(&amd64_irn_ops);
347 * Closes the output file and frees the ISA structure.
349 static void amd64_done(void *self)
351 amd64_isa_t *isa = self;
353 /* emit now all global declarations */
354 be_gas_emit_decls(isa->base.main_env);
361 static unsigned amd64_get_n_reg_class(void)
366 static const arch_register_class_t *amd64_get_reg_class(unsigned i)
368 assert(i < N_CLASSES);
369 return &amd64_reg_classes[i];
375 * Get the register class which shall be used to store a value of a given mode.
376 * @param self The this pointer.
377 * @param mode The mode in question.
378 * @return A register class which can hold values of the given mode.
380 static const arch_register_class_t *amd64_get_reg_class_for_mode(const ir_mode *mode)
382 assert(!mode_is_float(mode));
383 return &amd64_reg_classes[CLASS_amd64_gp];
389 be_abi_call_flags_bits_t flags;
393 static void *amd64_abi_init(const be_abi_call_t *call, ir_graph *irg)
395 amd64_abi_env_t *env = XMALLOC(amd64_abi_env_t);
396 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
397 env->flags = fl.bits;
403 * Get the between type for that call.
404 * @param self The callback object.
405 * @return The between type of for that call.
407 static ir_type *amd64_get_between_type(void *self)
409 static ir_type *between_type = NULL;
410 static ir_entity *old_bp_ent = NULL;
414 ir_entity *ret_addr_ent;
415 ir_type *ret_addr_type = new_type_primitive(mode_P);
416 ir_type *old_bp_type = new_type_primitive(mode_P);
418 between_type = new_type_class(new_id_from_str("amd64_between_type"));
419 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
420 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
422 set_entity_offset(old_bp_ent, 0);
423 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
424 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
431 * Build the prolog, return the BASE POINTER register
433 static const arch_register_t *amd64_abi_prologue(void *self, ir_node **mem,
434 pmap *reg_map, int *stack_bias)
436 amd64_abi_env_t *env = self;
437 const arch_env_t *aenv = be_get_irg_arch_env(env->irg);
443 if (!env->flags.try_omit_fp) {
444 /* FIXME: maybe later here should be some code to generate
445 * the usual abi prologue */
452 /* Build the epilog */
453 static void amd64_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
456 amd64_abi_env_t *env = self;
457 const arch_env_t *aenv = be_get_irg_arch_env(env->irg);
458 ir_node *curr_sp = be_abi_reg_map_get(reg_map, aenv->sp);
459 ir_node *curr_bp = be_abi_reg_map_get(reg_map, aenv->bp);
463 if (env->flags.try_omit_fp) {
464 curr_sp = be_new_IncSP(aenv->sp, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
467 be_abi_reg_map_set(reg_map, aenv->sp, curr_sp);
468 be_abi_reg_map_set(reg_map, aenv->bp, curr_bp);
471 static const be_abi_callbacks_t amd64_abi_callbacks = {
474 amd64_get_between_type,
479 static const arch_register_t *gpreg_param_reg_std[] = {
480 &amd64_gp_regs[REG_RDI],
481 &amd64_gp_regs[REG_RSI],
482 &amd64_gp_regs[REG_RDX],
483 &amd64_gp_regs[REG_RCX],
484 &amd64_gp_regs[REG_R8],
485 &amd64_gp_regs[REG_R9],
488 static const arch_register_t *amd64_get_RegParam_reg(int n)
490 assert(n < 6 && n >=0 && "register param > 6 requested");
491 return gpreg_param_reg_std[n];
495 * Get the ABI restrictions for procedure calls.
496 * @param self The this pointer.
497 * @param method_type The type of the method (procedure) in question.
498 * @param abi The abi object to be modified
500 static void amd64_get_call_abi(const void *self, ir_type *method_type,
505 int i, n = get_method_n_params(method_type);
506 be_abi_call_flags_t call_flags;
511 /* set abi flags for calls */
512 call_flags.bits.left_to_right = 0;
513 call_flags.bits.store_args_sequential = 0;
514 call_flags.bits.try_omit_fp = 1;
515 call_flags.bits.fp_free = 0;
516 call_flags.bits.call_has_imm = 1;
518 /* set stack parameter passing style */
519 be_abi_call_set_flags(abi, call_flags, &amd64_abi_callbacks);
521 for (i = 0; i < n; i++) {
522 tp = get_method_param_type(method_type, i);
523 mode = get_type_mode(tp);
524 //d// printf ("MODE %p %p XX %d\n", mode, mode_Iu, i);
526 if (!no_reg && i < 6 && mode_is_data (mode)) {
527 //d// printf("TEST%d\n", i);
528 be_abi_call_param_reg(abi, i, amd64_get_RegParam_reg (i),
530 /* default: all parameters on stack */
533 be_abi_call_param_stack(abi, i, mode, 8, 0, 0, ABI_CONTEXT_BOTH);
537 /* TODO: set correct return register */
538 /* default: return value is in R0 resp. F0 */
539 if (get_method_n_ress(method_type) > 0) {
540 tp = get_method_res_type(method_type, 0);
541 mode = get_type_mode(tp);
543 /* FIXME: No floating point yet */
544 /* be_abi_call_res_reg(abi, 0,
545 mode_is_float(mode) ? &amd64_fp_regs[REG_F0] : &amd64_gp_regs[REG_R0], ABI_CONTEXT_BOTH) */;
547 be_abi_call_res_reg(abi, 0,
548 &amd64_gp_regs[REG_RAX], ABI_CONTEXT_BOTH);
552 static int amd64_to_appear_in_schedule(void *block_env, const ir_node *irn)
556 if(!is_amd64_irn(irn))
563 * Initializes the code generator interface.
565 static const arch_code_generator_if_t *amd64_get_code_generator_if(
569 return &amd64_code_gen_if;
572 list_sched_selector_t amd64_sched_selector;
575 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
577 static const list_sched_selector_t *amd64_get_list_sched_selector(
578 const void *self, list_sched_selector_t *selector)
583 amd64_sched_selector = trivial_selector;
584 amd64_sched_selector.to_appear_in_schedule = amd64_to_appear_in_schedule;
585 return &amd64_sched_selector;
588 static const ilp_sched_selector_t *amd64_get_ilp_sched_selector(
596 * Returns the necessary byte alignment for storing a register of given class.
598 static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
600 ir_mode *mode = arch_register_class_mode(cls);
601 return get_mode_size_bytes(mode);
604 static void amd64_lower_for_target(void)
609 * Returns the libFirm configuration parameter for this backend.
611 static const backend_params *amd64_get_backend_params(void) {
612 static backend_params p = {
613 0, /* no inline assembly */
614 1, /* support Rotl nodes */
615 amd64_lower_for_target, /* lowering callback */
616 NULL, /* will be set later */
617 NULL, /* parameter for if conversion */
618 NULL, /* float arithmetic mode */
619 0, /* no trampoline support: size 0 */
620 0, /* no trampoline support: align 0 */
621 NULL, /* no trampoline support: no trampoline builder */
622 8 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
627 static const be_execution_unit_t ***amd64_get_allowed_execution_units(
636 static const be_machine_t *amd64_get_machine(const void *self)
644 static ir_graph **amd64_get_backend_irg_list(const void *self,
652 static asm_constraint_flags_t amd64_parse_asm_constraint(const char **c)
655 return ASM_CONSTRAINT_FLAG_INVALID;
658 static int amd64_is_valid_clobber(const char *clobber)
664 const arch_isa_if_t amd64_isa_if = {
667 NULL, /* handle intrinsics */
668 amd64_get_n_reg_class,
670 amd64_get_reg_class_for_mode,
672 amd64_get_code_generator_if,
673 amd64_get_list_sched_selector,
674 amd64_get_ilp_sched_selector,
675 amd64_get_reg_class_alignment,
676 amd64_get_backend_params,
677 amd64_get_allowed_execution_units,
679 amd64_get_backend_irg_list,
680 NULL, /* mark remat */
681 amd64_parse_asm_constraint,
682 amd64_is_valid_clobber
685 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_amd64);
686 void be_init_arch_amd64(void)
688 be_register_isa_if("amd64", &amd64_isa_if);
689 FIRM_DBG_REGISTER(dbg, "firm.be.amd64.cg");
690 amd64_init_transform();