2 * Copyright (C) 1995-2010 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 sparc backend driver file.
29 #include "lc_opts_enum.h"
31 #include "pseudo_irg.h"
38 #include "iroptimize.h"
48 #include "../bearch.h"
49 #include "../benode.h"
50 #include "../belower.h"
51 #include "../besched.h"
54 #include "../bemachine.h"
55 #include "../beilpsched.h"
56 #include "../bemodule.h"
58 #include "../bespillslots.h"
59 #include "../begnuas.h"
60 #include "../belistsched.h"
61 #include "../beflags.h"
63 #include "bearch_sparc_t.h"
64 #include "bearch_sparc.h"
66 #include "sparc_new_nodes.h"
67 #include "gen_sparc_regalloc_if.h"
68 #include "sparc_transform.h"
69 #include "sparc_emitter.h"
70 #include "sparc_map_regs.h"
72 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74 static arch_irn_class_t sparc_classify(const ir_node *irn)
80 static ir_entity *sparc_get_frame_entity(const ir_node *irn)
82 const sparc_attr_t *attr = get_sparc_attr_const(irn);
84 if (is_sparc_FrameAddr(irn)) {
85 const sparc_symconst_attr_t *attr = get_irn_generic_attr_const(irn);
89 if (attr->is_load_store) {
90 const sparc_load_store_attr_t *load_store_attr = get_sparc_load_store_attr_const(irn);
91 if (load_store_attr->is_frame_entity) {
92 return load_store_attr->entity;
99 static void sparc_set_frame_entity(ir_node *node, ir_entity *ent)
103 panic("sparc_set_frame_entity() called. This should not happen.");
104 /* TODO: set the ir_entity assigned to the frame */
108 * This function is called by the generic backend to correct offsets for
109 * nodes accessing the stack.
111 static void sparc_set_frame_offset(ir_node *irn, int offset)
113 if (is_sparc_FrameAddr(irn)) {
114 sparc_symconst_attr_t *attr = get_irn_generic_attr(irn);
115 attr->fp_offset += offset;
117 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(irn);
118 assert(attr->base.is_load_store);
119 attr->offset += offset;
123 static int sparc_get_sp_bias(const ir_node *irn)
126 return SPARC_MIN_STACKSIZE;
129 /* fill register allocator interface */
131 static const arch_irn_ops_t sparc_irn_ops = {
134 sparc_get_frame_entity,
135 sparc_set_frame_entity,
136 sparc_set_frame_offset,
138 NULL, /* get_inverse */
139 NULL, /* get_op_estimated_cost */
140 NULL, /* possible_memory_operand */
141 NULL, /* perform_memory_operand */
147 * Transforms the standard firm graph into
150 static void sparc_prepare_graph(void *self)
152 sparc_code_gen_t *cg = self;
154 /* transform FIRM into SPARC asm nodes */
155 sparc_transform_graph(cg);
158 dump_ir_graph(cg->irg, "transformed");
163 static ir_node *sparc_flags_remat(ir_node *node, ir_node *after)
168 if (is_Block(after)) {
171 block = get_nodes_block(after);
173 copy = exact_copy(node);
174 set_nodes_block(copy, block);
175 sched_add_after(after, copy);
179 static void sparc_before_ra(void *self)
181 sparc_code_gen_t *cg = self;
182 /* fixup flags register */
183 be_sched_fix_flags(cg->irg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
187 * transform reload node => load
189 static void transform_Reload(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 = get_irn_n(node, be_pos_Reload_mem);
196 ir_mode *mode = get_irn_mode(node);
197 ir_entity *entity = be_get_frame_entity(node);
198 const arch_register_t *reg;
202 ir_node *sched_point = sched_prev(node);
204 load = new_bd_sparc_Load(dbgi, block, ptr, mem, mode, entity, false, 0, true);
205 sched_add_after(sched_point, load);
208 proj = new_rd_Proj(dbgi, load, mode, pn_sparc_Load_res);
210 reg = arch_get_irn_register(node);
211 arch_set_irn_register(proj, reg);
213 exchange(node, proj);
217 * transform spill node => store
219 static void transform_Spill(ir_node *node)
221 ir_graph *irg = get_irn_irg(node);
222 ir_node *block = get_nodes_block(node);
223 dbg_info *dbgi = get_irn_dbg_info(node);
224 ir_node *ptr = get_irg_frame(irg);
225 ir_node *mem = new_NoMem();
226 ir_node *val = get_irn_n(node, be_pos_Spill_val);
227 ir_mode *mode = get_irn_mode(val);
228 ir_entity *entity = be_get_frame_entity(node);
229 ir_node *sched_point;
232 sched_point = sched_prev(node);
233 store = new_bd_sparc_Store(dbgi, block, ptr, val, mem, mode, entity, false, 0, true);
235 sched_add_after(sched_point, store);
237 exchange(node, store);
241 * walker to transform be_Spill and be_Reload nodes
243 static void sparc_after_ra_walker(ir_node *block, void *data)
245 ir_node *node, *prev;
248 for (node = sched_last(block); !sched_is_begin(node); node = prev) {
249 prev = sched_prev(node);
251 if (be_is_Reload(node)) {
252 transform_Reload(node);
253 } else if (be_is_Spill(node)) {
254 transform_Spill(node);
260 static void sparc_after_ra(void *self)
262 sparc_code_gen_t *cg = self;
263 be_coalesce_spillslots(cg->irg);
265 irg_block_walk_graph(cg->irg, NULL, sparc_after_ra_walker, NULL);
271 * Emits the code, closes the output file and frees
272 * the code generator interface.
274 static void sparc_emit_and_done(void *self)
276 sparc_code_gen_t *cg = self;
277 ir_graph *irg = cg->irg;
279 sparc_gen_routine(cg, irg);
281 /* de-allocate code generator */
285 static void *sparc_cg_init(ir_graph *irg);
287 static const arch_code_generator_if_t sparc_code_gen_if = {
289 NULL, /* get_pic_base hook */
290 NULL, /* before abi introduce hook */
292 NULL, /* spill hook */
293 sparc_before_ra, /* before register allocation hook */
294 sparc_after_ra, /* after register allocation hook */
300 * Initializes the code generator.
302 static void *sparc_cg_init(ir_graph *irg)
304 static ir_type *int_tp = NULL;
305 sparc_isa_t *isa = (sparc_isa_t *) be_get_irg_arch_env(irg);
306 sparc_code_gen_t *cg;
309 /* create an integer type with machine size */
310 int_tp = new_type_primitive(mode_Is);
313 cg = XMALLOC(sparc_code_gen_t);
314 cg->impl = &sparc_code_gen_if;
316 //cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
318 //cg->int_tp = int_tp;
319 //cg->have_fp_insn = 0;
320 //cg->unknown_gp = NULL;
321 //cg->unknown_fpa = NULL;
322 cg->dump = (be_get_irg_options(irg)->dump_flags & DUMP_BE) ? 1 : 0;
324 /* enter the current code generator */
327 return (arch_code_generator_t *)cg;
332 const arch_isa_if_t sparc_isa_if;
333 static sparc_isa_t sparc_isa_template = {
335 &sparc_isa_if, /* isa interface implementation */
336 &sparc_gp_regs[REG_SP], /* stack pointer register */
337 &sparc_gp_regs[REG_FP], /* base pointer register */
338 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
339 -1, /* stack direction */
340 1, /* power of two stack alignment for calls, 2^2 == 4 */
341 NULL, /* main environment */
342 7, /* costs for a spill instruction */
343 5, /* costs for a reload instruction */
344 false, /* no custom abi handling */
346 NULL /* current code generator */
350 static void sparc_handle_intrinsics(void)
352 ir_type *tp, *int_tp, *uint_tp;
356 runtime_rt rt_iMod, rt_uMod;
358 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
360 int_tp = new_type_primitive(mode_Is);
361 uint_tp = new_type_primitive(mode_Iu);
364 /* SPARC has no signed mod instruction ... */
366 i_instr_record *map_Mod = &records[n_records++].i_instr;
368 tp = new_type_method(2, 1);
369 set_method_param_type(tp, 0, int_tp);
370 set_method_param_type(tp, 1, int_tp);
371 set_method_res_type(tp, 0, int_tp);
373 rt_iMod.ent = new_entity(get_glob_type(), ID(".rem"), tp);
374 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
375 rt_iMod.mode = mode_T;
376 rt_iMod.res_mode = mode_Is;
377 rt_iMod.mem_proj_nr = pn_Mod_M;
378 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
379 rt_iMod.exc_proj_nr = pn_Mod_X_except;
380 rt_iMod.exc_mem_proj_nr = pn_Mod_M;
381 rt_iMod.res_proj_nr = pn_Mod_res;
383 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
385 map_Mod->kind = INTRINSIC_INSTR;
386 map_Mod->op = op_Mod;
387 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
388 map_Mod->ctx = &rt_iMod;
390 /* ... nor an unsigned mod. */
392 i_instr_record *map_Mod = &records[n_records++].i_instr;
394 tp = new_type_method(2, 1);
395 set_method_param_type(tp, 0, uint_tp);
396 set_method_param_type(tp, 1, uint_tp);
397 set_method_res_type(tp, 0, uint_tp);
399 rt_uMod.ent = new_entity(get_glob_type(), ID(".urem"), tp);
400 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
401 rt_uMod.mode = mode_T;
402 rt_uMod.res_mode = mode_Iu;
403 rt_uMod.mem_proj_nr = pn_Mod_M;
404 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
405 rt_uMod.exc_proj_nr = pn_Mod_X_except;
406 rt_uMod.exc_mem_proj_nr = pn_Mod_M;
407 rt_uMod.res_proj_nr = pn_Mod_res;
409 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
411 map_Mod->kind = INTRINSIC_INSTR;
412 map_Mod->op = op_Mod;
413 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
414 map_Mod->ctx = &rt_uMod;
418 lower_intrinsics(records, n_records, /*part_block_used=*/0);
423 * Initializes the backend ISA
425 static arch_env_t *sparc_init(FILE *outfile)
427 static int run_once = 0;
434 isa = XMALLOC(sparc_isa_t);
435 memcpy(isa, &sparc_isa_template, sizeof(*isa));
437 be_emit_init(outfile);
439 sparc_register_init();
440 sparc_create_opcodes(&sparc_irn_ops);
441 sparc_handle_intrinsics();
443 return &isa->arch_env;
449 * Closes the output file and frees the ISA structure.
451 static void sparc_done(void *self)
453 sparc_isa_t *isa = self;
455 /* emit now all global declarations */
456 be_gas_emit_decls(isa->arch_env.main_env);
463 static unsigned sparc_get_n_reg_class(void)
468 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
470 assert(i < N_CLASSES);
471 return &sparc_reg_classes[i];
477 * Get the register class which shall be used to store a value of a given mode.
478 * @param self The this pointer.
479 * @param mode The mode in question.
480 * @return A register class which can hold values of the given mode.
482 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
484 if (mode_is_float(mode))
485 return &sparc_reg_classes[CLASS_sparc_fp];
487 return &sparc_reg_classes[CLASS_sparc_gp];
493 be_abi_call_flags_bits_t flags;
494 const arch_env_t *arch_env;
498 static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
500 sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
501 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
502 env->flags = fl.bits;
504 env->arch_env = arch_env;
509 * Get the between type for that call.
510 * @param self The callback object.
511 * @return The between type of for that call.
513 static ir_type *sparc_get_between_type(void *self)
515 static ir_type *between_type = NULL;
518 if (between_type == NULL) {
519 between_type = new_type_class(new_id_from_str("sparc_between_type"));
520 set_type_size_bytes(between_type, 0);
528 * Build the prolog, return the BASE POINTER register
530 static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
531 pmap *reg_map, int *stack_bias)
533 sparc_abi_env_t *env = self;
534 ir_node *block = get_irg_start_block(env->irg);
535 const arch_register_t *fp = &sparc_gp_regs[REG_FP];
536 const arch_register_t *sp = &sparc_gp_regs[REG_SP];
539 ir_node *sp_proj = be_abi_reg_map_get(reg_map, sp);
542 //ir_type *frame_type = get_irg_frame_type(env->irg);
543 //frame_alloc_area(frame_type, reserved_stack_size, 1, 1);
545 // alloc min required stack space
546 // TODO: the min stacksize depends on wether this is a leaf procedure or not
547 ir_node *save = new_bd_sparc_Save(NULL, block, sp_proj, *mem, SPARC_MIN_STACKSIZE);
553 *stack_bias -= SPARC_MIN_STACKSIZE;
554 sp_proj = new_r_Proj(save, sp->reg_class->mode, pn_sparc_Save_stack);
555 *mem = new_r_Proj(save, mode_M, pn_sparc_Save_mem);
557 arch_set_irn_register(sp_proj, sp);
558 be_abi_reg_map_set(reg_map, sp, sp_proj);
560 // we always have a framepointer
564 /* Build the epilog */
565 static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
574 static const be_abi_callbacks_t sparc_abi_callbacks = {
577 sparc_get_between_type,
583 * Get the ABI restrictions for procedure calls.
584 * @param self The this pointer.
585 * @param method_type The type of the method (procedure) in question.
586 * @param abi The abi object to be modified
588 static void sparc_get_call_abi(const void *self, ir_type *method_type,
593 int i, n = get_method_n_params(method_type);
594 be_abi_call_flags_t call_flags;
597 /* set abi flags for calls */
598 call_flags.bits.left_to_right = 0;
599 call_flags.bits.store_args_sequential = 1;
601 call_flags.bits.try_omit_fp = 0;
602 call_flags.bits.fp_free = 0;
603 call_flags.bits.call_has_imm = 1;
605 /* set stack parameter passing style */
606 be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);
608 for (i = 0; i < n; i++) {
609 /* reg = get reg for param i; */
610 /* be_abi_call_param_reg(abi, i, reg); */
612 /* pass outgoing params 0-5 via registers, remaining via stack */
613 /* on sparc we need to set the ABI context since register names of parameters change to i0-i5 if we are the callee */
615 be_abi_call_param_reg(abi, i, sparc_get_RegParamOut_reg(i), ABI_CONTEXT_CALLER);
616 be_abi_call_param_reg(abi, i, sparc_get_RegParamIn_reg(i), ABI_CONTEXT_CALLEE);
618 tp = get_method_param_type(method_type, i);
619 mode = get_type_mode(tp);
620 be_abi_call_param_stack(abi, i, mode, 4, 0, 0, ABI_CONTEXT_BOTH); /*< stack args have no special context >*/
624 /* set return value register: return value is in i0 resp. f0 */
625 if (get_method_n_ress(method_type) > 0) {
626 tp = get_method_res_type(method_type, 0);
627 mode = get_type_mode(tp);
629 be_abi_call_res_reg(abi, 0,
630 mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_I0], ABI_CONTEXT_CALLEE); /*< return has no special context >*/
632 be_abi_call_res_reg(abi, 0,
633 mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0], ABI_CONTEXT_CALLER); /*< return has no special context >*/
637 static int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
641 if (!is_sparc_irn(irn))
648 * Initializes the code generator interface.
650 static const arch_code_generator_if_t *sparc_get_code_generator_if(
654 return &sparc_code_gen_if;
657 list_sched_selector_t sparc_sched_selector;
660 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
662 static const list_sched_selector_t *sparc_get_list_sched_selector(
663 const void *self, list_sched_selector_t *selector)
668 sparc_sched_selector = trivial_selector;
669 sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
670 return &sparc_sched_selector;
673 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
681 * Returns the necessary byte alignment for storing a register of given class.
683 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
685 ir_mode *mode = arch_register_class_mode(cls);
686 return get_mode_size_bytes(mode);
690 * Returns the libFirm configuration parameter for this backend.
692 static const backend_params *sparc_get_backend_params(void)
694 static backend_params p = {
695 0, /* no dword lowering */
696 0, /* no inline assembly */
697 NULL, /* will be set later */
698 NULL, /* no creator function */
699 NULL, /* context for create_intrinsic_fkt */
700 NULL, /* parameter for if conversion */
701 NULL, /* float arithmetic mode */
702 0, /* no trampoline support: size 0 */
703 0, /* no trampoline support: align 0 */
704 NULL, /* no trampoline support: no trampoline builder */
705 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
710 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
715 panic("sparc_get_allowed_execution_units not implemented yet");
718 static const be_machine_t *sparc_get_machine(const void *self)
722 panic("sparc_get_machine not implemented yet");
725 static ir_graph **sparc_get_backend_irg_list(const void *self,
733 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
736 return ASM_CONSTRAINT_FLAG_INVALID;
739 static int sparc_is_valid_clobber(const char *clobber)
745 const arch_isa_if_t sparc_isa_if = {
748 NULL, /* handle intrinsics */
749 sparc_get_n_reg_class,
751 sparc_get_reg_class_for_mode,
753 sparc_get_code_generator_if,
754 sparc_get_list_sched_selector,
755 sparc_get_ilp_sched_selector,
756 sparc_get_reg_class_alignment,
757 sparc_get_backend_params,
758 sparc_get_allowed_execution_units,
760 sparc_get_backend_irg_list,
761 NULL, /* mark remat */
762 sparc_parse_asm_constraint,
763 sparc_is_valid_clobber
766 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);
767 void be_init_arch_sparc(void)
769 be_register_isa_if("sparc", &sparc_isa_if);
770 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
771 sparc_init_transform();
772 sparc_init_emitter();