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 sparc backend driver file.
23 * @version $Id: bearch_TEMPLATE.c 26673 2009-10-01 16:43:13Z matze $
29 #include "lc_opts_enum.h"
31 #include "pseudo_irg.h"
38 #include "iroptimize.h"
47 #include "../bearch.h"
48 #include "../benode.h"
49 #include "../belower.h"
50 #include "../besched.h"
53 #include "../bemachine.h"
54 #include "../beilpsched.h"
55 #include "../bemodule.h"
57 #include "../bespillslots.h"
58 #include "../begnuas.h"
59 #include "../belistsched.h"
60 #include "../beflags.h"
62 #include "bearch_sparc_t.h"
64 #include "sparc_new_nodes.h"
65 #include "gen_sparc_regalloc_if.h"
66 #include "sparc_transform.h"
67 #include "sparc_emitter.h"
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
71 static arch_irn_class_t sparc_classify(const ir_node *irn)
77 static ir_entity *sparc_get_frame_entity(const ir_node *node)
80 /* TODO: return the ir_entity assigned to the frame */
84 static void sparc_set_frame_entity(ir_node *node, ir_entity *ent)
88 /* TODO: set the ir_entity assigned to the frame */
92 * This function is called by the generic backend to correct offsets for
93 * nodes accessing the stack.
95 static void sparc_set_frame_offset(ir_node *irn, int offset)
99 /* TODO: correct offset if irn accesses the stack */
102 static int sparc_get_sp_bias(const ir_node *irn)
108 /* fill register allocator interface */
110 static const arch_irn_ops_t sparc_irn_ops = {
113 sparc_get_frame_entity,
114 sparc_set_frame_entity,
115 sparc_set_frame_offset,
117 NULL, /* get_inverse */
118 NULL, /* get_op_estimated_cost */
119 NULL, /* possible_memory_operand */
120 NULL, /* perform_memory_operand */
126 * Transforms the standard firm graph into
129 static void sparc_prepare_graph(void *self)
131 sparc_code_gen_t *cg = self;
133 /* transform FIRM into SPARC asm nodes */
134 sparc_transform_graph(cg);
137 be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
143 * Called immediatly before emit phase.
145 static void sparc_finish_irg(void *self)
147 sparc_code_gen_t *cg = self;
148 ir_graph *irg = cg->irg;
150 dump_ir_block_graph_sched(irg, "-sparc-finished");
154 static ir_node *sparc_flags_remat(ir_node *node, ir_node *after)
159 if (is_Block(after)) {
162 block = get_nodes_block(after);
164 copy = exact_copy(node);
165 set_nodes_block(copy, block);
166 sched_add_after(after, copy);
170 static void sparc_before_ra(void *self)
172 sparc_code_gen_t *cg = self;
173 /* fixup flags register */
174 be_sched_fix_flags(cg->birg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
177 static void sparc_after_ra(void *self)
180 /* Some stuff you need to do immediatly after register allocation */
186 * Emits the code, closes the output file and frees
187 * the code generator interface.
189 static void sparc_emit_and_done(void *self)
191 sparc_code_gen_t *cg = self;
192 ir_graph *irg = cg->irg;
194 sparc_gen_routine(cg, irg);
196 /* de-allocate code generator */
200 static void *sparc_cg_init(be_irg_t *birg);
202 static const arch_code_generator_if_t sparc_code_gen_if = {
204 NULL, /* get_pic_base hook */
205 NULL, /* before abi introduce hook */
207 NULL, /* spill hook */
208 sparc_before_ra, /* before register allocation hook */
209 sparc_after_ra, /* after register allocation hook */
215 * Initializes the code generator.
217 static void *sparc_cg_init(be_irg_t *birg)
219 static ir_type *int_tp = NULL;
220 sparc_isa_t *isa = (sparc_isa_t *)birg->main_env->arch_env;
221 sparc_code_gen_t *cg;
224 /* create an integer type with machine size */
225 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
228 cg = XMALLOC(sparc_code_gen_t);
229 cg->impl = &sparc_code_gen_if;
231 //cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
234 //cg->int_tp = int_tp;
235 //cg->have_fp_insn = 0;
236 //cg->unknown_gp = NULL;
237 //cg->unknown_fpa = NULL;
238 cg->dump = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
240 FIRM_DBG_REGISTER(cg->mod, "firm.be.sparc.cg");
242 /* enter the current code generator */
245 return (arch_code_generator_t *)cg;
250 const arch_isa_if_t sparc_isa_if;
251 static sparc_isa_t sparc_isa_template = {
253 &sparc_isa_if, /* isa interface implementation */
254 &sparc_gp_regs[REG_SP], /* stack pointer register */
255 &sparc_gp_regs[REG_FP], /* base pointer register */
256 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
257 -1, /* stack direction */
258 1, /* power of two stack alignment for calls, 2^2 == 4 */
259 NULL, /* main environment */
260 7, /* costs for a spill instruction */
261 5, /* costs for a reload instruction */
263 NULL /* current code generator */
267 * Initializes the backend ISA
269 static arch_env_t *sparc_init(FILE *outfile)
271 static int run_once = 0;
278 isa = XMALLOC(sparc_isa_t);
279 memcpy(isa, &sparc_isa_template, sizeof(*isa));
281 be_emit_init(outfile);
283 sparc_register_init();
284 sparc_create_opcodes(&sparc_irn_ops);
286 return &isa->arch_env;
292 * Closes the output file and frees the ISA structure.
294 static void sparc_done(void *self)
296 sparc_isa_t *isa = self;
298 /* emit now all global declarations */
299 be_gas_emit_decls(isa->arch_env.main_env, 0);
306 static unsigned sparc_get_n_reg_class(void)
311 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
313 assert(i < N_CLASSES);
314 return &sparc_reg_classes[i];
320 * Get the register class which shall be used to store a value of a given mode.
321 * @param self The this pointer.
322 * @param mode The mode in question.
323 * @return A register class which can hold values of the given mode.
325 const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
327 if (mode_is_float(mode))
328 return &sparc_reg_classes[CLASS_sparc_fp];
330 return &sparc_reg_classes[CLASS_sparc_gp];
336 be_abi_call_flags_bits_t flags;
337 const arch_env_t *arch_env;
341 static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
343 sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
344 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
345 env->flags = fl.bits;
347 env->arch_env = arch_env;
352 * Get the between type for that call.
353 * @param self The callback object.
354 * @return The between type of for that call.
356 static ir_type *sparc_get_between_type(void *self)
358 static ir_type *between_type = NULL;
361 if (between_type == NULL) {
362 between_type = new_type_class(new_id_from_str("sparc_between_type"));
363 set_type_size_bytes(between_type, 0);
370 * Build the prolog, return the BASE POINTER register
372 static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
373 pmap *reg_map, int *stack_bias)
375 sparc_abi_env_t *env = self;
380 if(env->flags.try_omit_fp)
381 return env->arch_env->sp;
383 //panic("framepointer not implemented yet");
384 return env->arch_env->bp;
387 /* Build the epilog */
388 static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
397 static const be_abi_callbacks_t sparc_abi_callbacks = {
400 sparc_get_between_type,
406 * Get the ABI restrictions for procedure calls.
407 * @param self The this pointer.
408 * @param method_type The type of the method (procedure) in question.
409 * @param abi The abi object to be modified
411 void sparc_get_call_abi(const void *self, ir_type *method_type,
416 int i, n = get_method_n_params(method_type);
417 be_abi_call_flags_t call_flags;
420 /* set abi flags for calls */
421 call_flags.bits.left_to_right = 0;
422 call_flags.bits.store_args_sequential = 1;
423 call_flags.bits.try_omit_fp = 1;
424 call_flags.bits.fp_free = 0;
425 call_flags.bits.call_has_imm = 1;
427 /* set stack parameter passing style */
428 be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);
430 for (i = 0; i < n; i++) {
431 /* TODO: implement register parameter: */
432 /* reg = get reg for param i; */
433 /* be_abi_call_param_reg(abi, i, reg); */
434 /* default: all parameters on stack */
435 tp = get_method_param_type(method_type, i);
436 mode = get_type_mode(tp);
437 be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
440 /* TODO: set correct return register */
441 /* default: return value is in O0 resp. F0 */
442 if (get_method_n_ress(method_type) > 0) {
443 tp = get_method_res_type(method_type, 0);
444 mode = get_type_mode(tp);
446 be_abi_call_res_reg(abi, 0,
447 mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0]);
451 int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
455 if(!is_sparc_irn(irn))
462 * Initializes the code generator interface.
464 static const arch_code_generator_if_t *sparc_get_code_generator_if(
468 return &sparc_code_gen_if;
471 list_sched_selector_t sparc_sched_selector;
474 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
476 static const list_sched_selector_t *sparc_get_list_sched_selector(
477 const void *self, list_sched_selector_t *selector)
482 sparc_sched_selector = trivial_selector;
483 sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
484 return &sparc_sched_selector;
487 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
495 * Returns the necessary byte alignment for storing a register of given class.
497 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
499 ir_mode *mode = arch_register_class_mode(cls);
500 return get_mode_size_bytes(mode);
504 * Returns the libFirm configuration parameter for this backend.
506 static const backend_params *sparc_get_backend_params(void) {
507 static backend_params p = {
508 0, /* no dword lowering */
509 0, /* no inline assembly */
510 NULL, /* will be set later */
511 NULL, /* no creator function */
512 NULL, /* context for create_intrinsic_fkt */
513 NULL, /* parameter for if conversion */
514 NULL, /* float arithmetic mode */
515 0, /* no trampoline support: size 0 */
516 0, /* no trampoline support: align 0 */
517 NULL, /* no trampoline support: no trampoline builder */
518 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
523 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
532 static const be_machine_t *sparc_get_machine(const void *self)
540 static ir_graph **sparc_get_backend_irg_list(const void *self,
548 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
551 return ASM_CONSTRAINT_FLAG_INVALID;
554 static int sparc_is_valid_clobber(const char *clobber)
560 const arch_isa_if_t sparc_isa_if = {
563 NULL, /* handle intrinsics */
564 sparc_get_n_reg_class,
566 sparc_get_reg_class_for_mode,
568 sparc_get_code_generator_if,
569 sparc_get_list_sched_selector,
570 sparc_get_ilp_sched_selector,
571 sparc_get_reg_class_alignment,
572 sparc_get_backend_params,
573 sparc_get_allowed_execution_units,
575 sparc_get_backend_irg_list,
576 NULL, /* mark remat */
577 sparc_parse_asm_constraint,
578 sparc_is_valid_clobber
581 void be_init_arch_sparc(void)
583 be_register_isa_if("sparc", &sparc_isa_if);
584 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
585 sparc_init_transform();
586 sparc_init_emitter();
588 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);