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.
28 #include "lc_opts_enum.h"
36 #include "iroptimize.h"
46 #include "../bearch.h"
47 #include "../benode.h"
48 #include "../belower.h"
49 #include "../besched.h"
51 #include "../bemachine.h"
52 #include "../beilpsched.h"
53 #include "../bemodule.h"
55 #include "../bespillslots.h"
56 #include "../begnuas.h"
57 #include "../belistsched.h"
58 #include "../beflags.h"
60 #include "bearch_sparc_t.h"
62 #include "sparc_new_nodes.h"
63 #include "gen_sparc_regalloc_if.h"
64 #include "sparc_transform.h"
65 #include "sparc_emitter.h"
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69 static arch_irn_class_t sparc_classify(const ir_node *irn)
75 static ir_entity *sparc_get_frame_entity(const ir_node *irn)
77 if (is_sparc_FrameAddr(irn)) {
78 const sparc_symconst_attr_t *attr = get_irn_generic_attr_const(irn);
82 if (sparc_has_load_store_attr(irn)) {
83 const sparc_load_store_attr_t *load_store_attr = get_sparc_load_store_attr_const(irn);
84 if (load_store_attr->is_frame_entity) {
85 return load_store_attr->entity;
93 * This function is called by the generic backend to correct offsets for
94 * nodes accessing the stack.
96 static void sparc_set_frame_offset(ir_node *irn, int offset)
98 if (is_sparc_FrameAddr(irn)) {
99 sparc_symconst_attr_t *attr = get_irn_generic_attr(irn);
100 attr->fp_offset += offset;
102 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(irn);
103 assert(sparc_has_load_store_attr(irn));
104 attr->offset += offset;
108 static int sparc_get_sp_bias(const ir_node *node)
110 if (is_sparc_Save(node)) {
111 const sparc_save_attr_t *attr = get_sparc_save_attr_const(node);
112 /* Note we do not retport the change of the SPARC_MIN_STACKSIZE
113 * size, since we have additional magic in the emitter which
114 * calculates that! */
115 assert(attr->initial_stacksize >= SPARC_MIN_STACKSIZE);
116 return attr->initial_stacksize - SPARC_MIN_STACKSIZE;
121 /* fill register allocator interface */
123 static const arch_irn_ops_t sparc_irn_ops = {
126 sparc_get_frame_entity,
127 sparc_set_frame_offset,
129 NULL, /* get_inverse */
130 NULL, /* get_op_estimated_cost */
131 NULL, /* possible_memory_operand */
132 NULL, /* perform_memory_operand */
138 * Transforms the standard firm graph into
141 static void sparc_prepare_graph(void *self)
143 sparc_code_gen_t *cg = self;
145 /* transform FIRM into SPARC asm nodes */
146 sparc_transform_graph(cg);
149 dump_ir_graph(cg->irg, "transformed");
152 static bool sparc_modifies_flags(const ir_node *node)
154 return arch_irn_get_flags(node) & sparc_arch_irn_flag_modifies_flags;
157 static bool sparc_modifies_fp_flags(const ir_node *node)
159 return arch_irn_get_flags(node) & sparc_arch_irn_flag_modifies_fp_flags;
162 static void sparc_before_ra(void *self)
164 sparc_code_gen_t *cg = self;
165 /* fixup flags register */
166 be_sched_fix_flags(cg->irg, &sparc_reg_classes[CLASS_sparc_flags_class],
167 NULL, sparc_modifies_flags);
168 be_sched_fix_flags(cg->irg, &sparc_reg_classes[CLASS_sparc_fpflags_class],
169 NULL, sparc_modifies_fp_flags);
173 * transform reload node => load
175 static void transform_Reload(ir_node *node)
177 ir_node *block = get_nodes_block(node);
178 dbg_info *dbgi = get_irn_dbg_info(node);
179 ir_node *ptr = get_irn_n(node, be_pos_Spill_frame);
180 ir_node *mem = get_irn_n(node, be_pos_Reload_mem);
181 ir_mode *mode = get_irn_mode(node);
182 ir_entity *entity = be_get_frame_entity(node);
183 const arch_register_t *reg;
187 ir_node *sched_point = sched_prev(node);
189 load = new_bd_sparc_Ld(dbgi, block, ptr, mem, mode, entity, 0, true);
190 sched_add_after(sched_point, load);
193 proj = new_rd_Proj(dbgi, load, mode, pn_sparc_Ld_res);
195 reg = arch_get_irn_register(node);
196 arch_set_irn_register(proj, reg);
198 exchange(node, proj);
202 * transform spill node => store
204 static void transform_Spill(ir_node *node)
206 ir_node *block = get_nodes_block(node);
207 dbg_info *dbgi = get_irn_dbg_info(node);
208 ir_node *ptr = get_irn_n(node, be_pos_Spill_frame);
209 ir_node *mem = new_NoMem();
210 ir_node *val = get_irn_n(node, be_pos_Spill_val);
211 ir_mode *mode = get_irn_mode(val);
212 ir_entity *entity = be_get_frame_entity(node);
213 ir_node *sched_point;
216 sched_point = sched_prev(node);
217 store = new_bd_sparc_St(dbgi, block, ptr, val, mem, mode, entity, 0, true);
219 sched_add_after(sched_point, store);
221 exchange(node, store);
225 * walker to transform be_Spill and be_Reload nodes
227 static void sparc_after_ra_walker(ir_node *block, void *data)
229 ir_node *node, *prev;
232 for (node = sched_last(block); !sched_is_begin(node); node = prev) {
233 prev = sched_prev(node);
235 if (be_is_Reload(node)) {
236 transform_Reload(node);
237 } else if (be_is_Spill(node)) {
238 transform_Spill(node);
243 static void sparc_collect_frame_entity_nodes(ir_node *node, void *data)
245 be_fec_env_t *env = data;
249 const sparc_load_store_attr_t *attr;
251 if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
252 mode = get_irn_mode(node);
253 align = get_mode_size_bytes(mode);
254 be_node_needs_frame_entity(env, node, mode, align);
258 if (!is_sparc_Ld(node) && !is_sparc_Ldf(node))
261 attr = get_sparc_load_store_attr_const(node);
262 entity = attr->entity;
263 mode = attr->load_store_mode;
266 if (!attr->is_frame_entity)
268 if (arch_irn_get_flags(node) & sparc_arch_irn_flag_needs_64bit_spillslot)
270 align = get_mode_size_bytes(mode);
271 be_node_needs_frame_entity(env, node, mode, align);
274 static void sparc_set_frame_entity(ir_node *node, ir_entity *entity)
276 if (is_be_node(node)) {
277 be_node_set_frame_entity(node, entity);
279 /* we only say be_node_needs_frame_entity on nodes with load_store
280 * attributes, so this should be fine */
281 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(node);
282 attr->entity = entity;
287 static void sparc_after_ra(void *self)
289 sparc_code_gen_t *cg = self;
290 ir_graph *irg = cg->irg;
291 be_fec_env_t *fec_env = be_new_frame_entity_coalescer(irg);
293 irg_walk_graph(irg, NULL, sparc_collect_frame_entity_nodes, fec_env);
294 be_assign_entities(fec_env, sparc_set_frame_entity);
295 be_free_frame_entity_coalescer(fec_env);
297 irg_block_walk_graph(cg->irg, NULL, sparc_after_ra_walker, NULL);
303 * Emits the code, closes the output file and frees
304 * the code generator interface.
306 static void sparc_emit_and_done(void *self)
308 sparc_code_gen_t *cg = self;
309 ir_graph *irg = cg->irg;
311 sparc_gen_routine(cg, irg);
313 /* de-allocate code generator */
317 static void *sparc_cg_init(ir_graph *irg);
319 static const arch_code_generator_if_t sparc_code_gen_if = {
321 NULL, /* get_pic_base hook */
322 NULL, /* before abi introduce hook */
324 NULL, /* spill hook */
325 sparc_before_ra, /* before register allocation hook */
326 sparc_after_ra, /* after register allocation hook */
332 * Initializes the code generator.
334 static void *sparc_cg_init(ir_graph *irg)
336 sparc_isa_t *isa = (sparc_isa_t *) be_get_irg_arch_env(irg);
337 sparc_code_gen_t *cg = XMALLOCZ(sparc_code_gen_t);
339 cg->impl = &sparc_code_gen_if;
342 cg->dump = (be_get_irg_options(irg)->dump_flags & DUMP_BE) != 0;
343 cg->constants = pmap_create();
345 /* enter the current code generator */
348 return (arch_code_generator_t*) cg;
351 const arch_isa_if_t sparc_isa_if;
352 static sparc_isa_t sparc_isa_template = {
354 &sparc_isa_if, /* isa interface implementation */
355 &sparc_gp_regs[REG_SP], /* stack pointer register */
356 &sparc_gp_regs[REG_FRAME_POINTER], /* base pointer register */
357 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
358 -1, /* stack direction */
359 3, /* power of two stack alignment
361 NULL, /* main environment */
362 7, /* costs for a spill instruction */
363 5, /* costs for a reload instruction */
364 true, /* custom abi handling */
366 NULL /* current code generator */
370 static void sparc_handle_intrinsics(void)
372 ir_type *tp, *int_tp, *uint_tp;
376 runtime_rt rt_iMod, rt_uMod;
378 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
380 int_tp = new_type_primitive(mode_Is);
381 uint_tp = new_type_primitive(mode_Iu);
384 /* SPARC has no signed mod instruction ... */
386 i_instr_record *map_Mod = &records[n_records++].i_instr;
388 tp = new_type_method(2, 1);
389 set_method_param_type(tp, 0, int_tp);
390 set_method_param_type(tp, 1, int_tp);
391 set_method_res_type(tp, 0, int_tp);
393 rt_iMod.ent = new_entity(get_glob_type(), ID(".rem"), tp);
394 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
395 rt_iMod.mode = mode_T;
396 rt_iMod.res_mode = mode_Is;
397 rt_iMod.mem_proj_nr = pn_Mod_M;
398 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
399 rt_iMod.exc_proj_nr = pn_Mod_X_except;
400 rt_iMod.exc_mem_proj_nr = pn_Mod_M;
401 rt_iMod.res_proj_nr = pn_Mod_res;
403 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
405 map_Mod->kind = INTRINSIC_INSTR;
406 map_Mod->op = op_Mod;
407 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
408 map_Mod->ctx = &rt_iMod;
410 /* ... nor an unsigned mod. */
412 i_instr_record *map_Mod = &records[n_records++].i_instr;
414 tp = new_type_method(2, 1);
415 set_method_param_type(tp, 0, uint_tp);
416 set_method_param_type(tp, 1, uint_tp);
417 set_method_res_type(tp, 0, uint_tp);
419 rt_uMod.ent = new_entity(get_glob_type(), ID(".urem"), tp);
420 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
421 rt_uMod.mode = mode_T;
422 rt_uMod.res_mode = mode_Iu;
423 rt_uMod.mem_proj_nr = pn_Mod_M;
424 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
425 rt_uMod.exc_proj_nr = pn_Mod_X_except;
426 rt_uMod.exc_mem_proj_nr = pn_Mod_M;
427 rt_uMod.res_proj_nr = pn_Mod_res;
429 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
431 map_Mod->kind = INTRINSIC_INSTR;
432 map_Mod->op = op_Mod;
433 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
434 map_Mod->ctx = &rt_uMod;
438 lower_intrinsics(records, n_records, /*part_block_used=*/0);
443 * Initializes the backend ISA
445 static arch_env_t *sparc_init(FILE *outfile)
447 static int run_once = 0;
454 isa = XMALLOC(sparc_isa_t);
455 memcpy(isa, &sparc_isa_template, sizeof(*isa));
457 be_emit_init(outfile);
459 sparc_register_init();
460 sparc_create_opcodes(&sparc_irn_ops);
461 sparc_handle_intrinsics();
469 * Closes the output file and frees the ISA structure.
471 static void sparc_done(void *self)
473 sparc_isa_t *isa = self;
475 /* emit now all global declarations */
476 be_gas_emit_decls(isa->base.main_env);
483 static unsigned sparc_get_n_reg_class(void)
488 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
490 assert(i < N_CLASSES);
491 return &sparc_reg_classes[i];
497 * Get the register class which shall be used to store a value of a given mode.
498 * @param self The this pointer.
499 * @param mode The mode in question.
500 * @return A register class which can hold values of the given mode.
502 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
504 if (mode_is_float(mode))
505 return &sparc_reg_classes[CLASS_sparc_fp];
507 return &sparc_reg_classes[CLASS_sparc_gp];
510 static int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
514 if (!is_sparc_irn(irn))
521 * Initializes the code generator interface.
523 static const arch_code_generator_if_t *sparc_get_code_generator_if(
527 return &sparc_code_gen_if;
530 list_sched_selector_t sparc_sched_selector;
533 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
535 static const list_sched_selector_t *sparc_get_list_sched_selector(
536 const void *self, list_sched_selector_t *selector)
541 sparc_sched_selector = trivial_selector;
542 sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
543 return &sparc_sched_selector;
546 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
554 * Returns the necessary byte alignment for storing a register of given class.
556 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
558 ir_mode *mode = arch_register_class_mode(cls);
559 return get_mode_size_bytes(mode);
562 static void sparc_lower_for_target(void)
564 /* TODO, doubleword lowering and others */
568 * Returns the libFirm configuration parameter for this backend.
570 static const backend_params *sparc_get_backend_params(void)
572 static backend_params p = {
573 0, /* no inline assembly */
574 sparc_lower_for_target, /* lowering callback */
575 NULL, /* will be set later */
576 NULL, /* parameter for if conversion */
577 NULL, /* float arithmetic mode */
578 0, /* no trampoline support: size 0 */
579 0, /* no trampoline support: align 0 */
580 NULL, /* no trampoline support: no trampoline builder */
581 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
586 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
591 panic("sparc_get_allowed_execution_units not implemented yet");
594 static const be_machine_t *sparc_get_machine(const void *self)
598 panic("sparc_get_machine not implemented yet");
601 static ir_graph **sparc_get_backend_irg_list(const void *self,
609 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
612 return ASM_CONSTRAINT_FLAG_INVALID;
615 static int sparc_is_valid_clobber(const char *clobber)
621 const arch_isa_if_t sparc_isa_if = {
624 NULL, /* handle intrinsics */
625 sparc_get_n_reg_class,
627 sparc_get_reg_class_for_mode,
629 sparc_get_code_generator_if,
630 sparc_get_list_sched_selector,
631 sparc_get_ilp_sched_selector,
632 sparc_get_reg_class_alignment,
633 sparc_get_backend_params,
634 sparc_get_allowed_execution_units,
636 sparc_get_backend_irg_list,
637 NULL, /* mark remat */
638 sparc_parse_asm_constraint,
639 sparc_is_valid_clobber
642 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);
643 void be_init_arch_sparc(void)
645 be_register_isa_if("sparc", &sparc_isa_if);
646 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
647 sparc_init_transform();
648 sparc_init_emitter();