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)
173 sparc_code_gen_t *cg = self;
174 /* fixup flags register */
175 be_sched_fix_flags(cg->birg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
178 static void sparc_after_ra(void *self)
181 /* Some stuff you need to do immediatly after register allocation */
187 * Emits the code, closes the output file and frees
188 * the code generator interface.
190 static void sparc_emit_and_done(void *self)
192 sparc_code_gen_t *cg = self;
193 ir_graph *irg = cg->irg;
195 sparc_gen_routine(cg, irg);
197 /* de-allocate code generator */
201 static void *sparc_cg_init(be_irg_t *birg);
203 static const arch_code_generator_if_t sparc_code_gen_if = {
205 NULL, /* get_pic_base hook */
206 NULL, /* before abi introduce hook */
208 NULL, /* spill hook */
209 sparc_before_ra, /* before register allocation hook */
210 sparc_after_ra, /* after register allocation hook */
216 * Initializes the code generator.
218 static void *sparc_cg_init(be_irg_t *birg)
220 static ir_type *int_tp = NULL;
221 sparc_isa_t *isa = (sparc_isa_t *)birg->main_env->arch_env;
222 sparc_code_gen_t *cg;
225 /* create an integer type with machine size */
226 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
229 cg = XMALLOC(sparc_code_gen_t);
230 cg->impl = &sparc_code_gen_if;
232 //cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
235 //cg->int_tp = int_tp;
236 //cg->have_fp_insn = 0;
237 //cg->unknown_gp = NULL;
238 //cg->unknown_fpa = NULL;
239 cg->dump = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
241 FIRM_DBG_REGISTER(cg->mod, "firm.be.sparc.cg");
243 /* enter the current code generator */
246 return (arch_code_generator_t *)cg;
251 const arch_isa_if_t sparc_isa_if;
252 static sparc_isa_t sparc_isa_template = {
254 &sparc_isa_if, /* isa interface implementation */
255 &sparc_gp_regs[REG_SP], /* stack pointer register */
256 &sparc_gp_regs[REG_FP], /* base pointer register */
257 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
258 -1, /* stack direction */
259 1, /* power of two stack alignment for calls, 2^2 == 4 */
260 NULL, /* main environment */
261 7, /* costs for a spill instruction */
262 5, /* costs for a reload instruction */
264 NULL /* current code generator */
268 * Initializes the backend ISA
270 static arch_env_t *sparc_init(FILE *outfile)
272 static int run_once = 0;
279 isa = XMALLOC(sparc_isa_t);
280 memcpy(isa, &sparc_isa_template, sizeof(*isa));
282 be_emit_init(outfile);
284 sparc_register_init();
285 sparc_create_opcodes(&sparc_irn_ops);
287 return &isa->arch_env;
293 * Closes the output file and frees the ISA structure.
295 static void sparc_done(void *self)
297 sparc_isa_t *isa = self;
299 /* emit now all global declarations */
300 be_gas_emit_decls(isa->arch_env.main_env, 0);
307 static unsigned sparc_get_n_reg_class(void)
312 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
314 assert(i < N_CLASSES);
315 return &sparc_reg_classes[i];
321 * Get the register class which shall be used to store a value of a given mode.
322 * @param self The this pointer.
323 * @param mode The mode in question.
324 * @return A register class which can hold values of the given mode.
326 const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
328 if (mode_is_float(mode))
329 return &sparc_reg_classes[CLASS_sparc_fp];
331 return &sparc_reg_classes[CLASS_sparc_gp];
337 be_abi_call_flags_bits_t flags;
338 const arch_env_t *arch_env;
342 static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
344 sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
345 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
346 env->flags = fl.bits;
348 env->arch_env = arch_env;
353 * Get the between type for that call.
354 * @param self The callback object.
355 * @return The between type of for that call.
357 static ir_type *sparc_get_between_type(void *self)
359 static ir_type *between_type = NULL;
362 if (between_type == NULL) {
363 between_type = new_type_class(new_id_from_str("sparc_between_type"));
364 set_type_size_bytes(between_type, 0);
371 * Build the prolog, return the BASE POINTER register
373 static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
374 pmap *reg_map, int *stack_bias)
376 sparc_abi_env_t *env = self;
381 if(env->flags.try_omit_fp)
382 return env->arch_env->sp;
384 //panic("framepointer not implemented yet");
385 return env->arch_env->bp;
388 /* Build the epilog */
389 static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
398 static const be_abi_callbacks_t sparc_abi_callbacks = {
401 sparc_get_between_type,
407 * Get the ABI restrictions for procedure calls.
408 * @param self The this pointer.
409 * @param method_type The type of the method (procedure) in question.
410 * @param abi The abi object to be modified
412 void sparc_get_call_abi(const void *self, ir_type *method_type,
417 int i, n = get_method_n_params(method_type);
418 be_abi_call_flags_t call_flags;
421 /* set abi flags for calls */
422 call_flags.bits.left_to_right = 0;
423 call_flags.bits.store_args_sequential = 1;
424 call_flags.bits.try_omit_fp = 1;
425 call_flags.bits.fp_free = 0;
426 call_flags.bits.call_has_imm = 1;
428 /* set stack parameter passing style */
429 be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);
431 for (i = 0; i < n; i++) {
432 /* TODO: implement register parameter: */
433 /* reg = get reg for param i; */
434 /* be_abi_call_param_reg(abi, i, reg); */
435 /* default: all parameters on stack */
436 tp = get_method_param_type(method_type, i);
437 mode = get_type_mode(tp);
438 be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
441 /* TODO: set correct return register */
442 /* default: return value is in O0 resp. F0 */
443 if (get_method_n_ress(method_type) > 0) {
444 tp = get_method_res_type(method_type, 0);
445 mode = get_type_mode(tp);
447 be_abi_call_res_reg(abi, 0,
448 mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0]);
452 int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
456 if(!is_sparc_irn(irn))
463 * Initializes the code generator interface.
465 static const arch_code_generator_if_t *sparc_get_code_generator_if(
469 return &sparc_code_gen_if;
472 list_sched_selector_t sparc_sched_selector;
475 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
477 static const list_sched_selector_t *sparc_get_list_sched_selector(
478 const void *self, list_sched_selector_t *selector)
483 sparc_sched_selector = trivial_selector;
484 sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
485 return &sparc_sched_selector;
488 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
496 * Returns the necessary byte alignment for storing a register of given class.
498 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
500 ir_mode *mode = arch_register_class_mode(cls);
501 return get_mode_size_bytes(mode);
505 * Returns the libFirm configuration parameter for this backend.
507 static const backend_params *sparc_get_backend_params(void) {
508 static backend_params p = {
509 0, /* no dword lowering */
510 0, /* no inline assembly */
511 NULL, /* will be set later */
512 NULL, /* no creator function */
513 NULL, /* context for create_intrinsic_fkt */
514 NULL, /* parameter for if conversion */
515 NULL, /* float arithmetic mode */
516 0, /* no trampoline support: size 0 */
517 0, /* no trampoline support: align 0 */
518 NULL, /* no trampoline support: no trampoline builder */
519 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
524 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
533 static const be_machine_t *sparc_get_machine(const void *self)
541 static ir_graph **sparc_get_backend_irg_list(const void *self,
549 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
552 return ASM_CONSTRAINT_FLAG_INVALID;
555 static int sparc_is_valid_clobber(const char *clobber)
561 const arch_isa_if_t sparc_isa_if = {
564 NULL, /* handle intrinsics */
565 sparc_get_n_reg_class,
567 sparc_get_reg_class_for_mode,
569 sparc_get_code_generator_if,
570 sparc_get_list_sched_selector,
571 sparc_get_ilp_sched_selector,
572 sparc_get_reg_class_alignment,
573 sparc_get_backend_params,
574 sparc_get_allowed_execution_units,
576 sparc_get_backend_irg_list,
577 NULL, /* mark remat */
578 sparc_parse_asm_constraint,
579 sparc_is_valid_clobber
582 void be_init_arch_sparc(void)
584 be_register_isa_if("sparc", &sparc_isa_if);
585 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
586 sparc_init_transform();
587 sparc_init_emitter();
589 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);