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 TEMPLATE backend driver file.
27 #include "pseudo_irg.h"
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"
47 #include "bearch_TEMPLATE_t.h"
49 #include "TEMPLATE_new_nodes.h"
50 #include "gen_TEMPLATE_regalloc_if.h"
51 #include "TEMPLATE_transform.h"
52 #include "TEMPLATE_emitter.h"
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56 static arch_irn_class_t TEMPLATE_classify(const ir_node *irn)
62 static ir_entity *TEMPLATE_get_frame_entity(const ir_node *node)
65 /* TODO: return the ir_entity assigned to the frame */
69 static void TEMPLATE_set_frame_entity(ir_node *node, ir_entity *ent)
73 /* TODO: set the ir_entity assigned to the frame */
77 * This function is called by the generic backend to correct offsets for
78 * nodes accessing the stack.
80 static void TEMPLATE_set_frame_offset(ir_node *irn, int offset)
84 /* TODO: correct offset if irn accesses the stack */
87 static int TEMPLATE_get_sp_bias(const ir_node *irn)
93 /* fill register allocator interface */
95 static const arch_irn_ops_t TEMPLATE_irn_ops = {
98 TEMPLATE_get_frame_entity,
99 TEMPLATE_set_frame_entity,
100 TEMPLATE_set_frame_offset,
101 TEMPLATE_get_sp_bias,
102 NULL, /* get_inverse */
103 NULL, /* get_op_estimated_cost */
104 NULL, /* possible_memory_operand */
105 NULL, /* perform_memory_operand */
111 * Transforms the standard firm graph into
112 * a TEMLPATE firm graph
114 static void TEMPLATE_prepare_graph(void *self)
116 TEMPLATE_code_gen_t *cg = self;
118 irg_walk_blkwise_graph(cg->irg, NULL, TEMPLATE_transform_node, cg);
124 * Called immediatly before emit phase.
126 static void TEMPLATE_finish_irg(void *self)
128 TEMPLATE_code_gen_t *cg = self;
129 ir_graph *irg = cg->irg;
131 dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
135 static void TEMPLATE_before_ra(void *self)
138 /* Some stuff you need to do after scheduling but before register allocation */
141 static void TEMPLATE_after_ra(void *self)
144 /* Some stuff you need to do immediatly after register allocation */
150 * Emits the code, closes the output file and frees
151 * the code generator interface.
153 static void TEMPLATE_emit_and_done(void *self)
155 TEMPLATE_code_gen_t *cg = self;
156 ir_graph *irg = cg->irg;
158 TEMPLATE_gen_routine(cg, irg);
160 /* de-allocate code generator */
164 static void *TEMPLATE_cg_init(be_irg_t *birg);
166 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
168 NULL, /* get_pic_base hook */
169 NULL, /* before abi introduce hook */
170 TEMPLATE_prepare_graph,
171 NULL, /* spill hook */
172 TEMPLATE_before_ra, /* before register allocation hook */
173 TEMPLATE_after_ra, /* after register allocation hook */
175 TEMPLATE_emit_and_done
179 * Initializes the code generator.
181 static void *TEMPLATE_cg_init(be_irg_t *birg)
183 const arch_env_t *arch_env = be_get_birg_arch_env(birg);
184 TEMPLATE_isa_t *isa = (TEMPLATE_isa_t *) arch_env;
185 TEMPLATE_code_gen_t *cg = XMALLOC(TEMPLATE_code_gen_t);
187 cg->impl = &TEMPLATE_code_gen_if;
188 cg->irg = be_get_birg_irg(birg);
192 return (arch_code_generator_t *)cg;
197 const arch_isa_if_t TEMPLATE_isa_if;
198 static TEMPLATE_isa_t TEMPLATE_isa_template = {
200 &TEMPLATE_isa_if, /* isa interface implementation */
201 &TEMPLATE_gp_regs[REG_SP], /* stack pointer register */
202 &TEMPLATE_gp_regs[REG_BP], /* base pointer register */
203 &TEMPLATE_reg_classes[CLASS_TEMPLATE_gp], /* link pointer register class */
204 -1, /* stack direction */
205 2, /* power of two stack alignment for calls, 2^2 == 4 */
206 NULL, /* main environment */
207 7, /* costs for a spill instruction */
208 5, /* costs for a reload instruction */
213 * Initializes the backend ISA
215 static arch_env_t *TEMPLATE_init(FILE *outfile)
217 static int run_once = 0;
224 isa = XMALLOC(TEMPLATE_isa_t);
225 memcpy(isa, &TEMPLATE_isa_template, sizeof(*isa));
227 be_emit_init(outfile);
229 TEMPLATE_register_init();
230 TEMPLATE_create_opcodes(&TEMPLATE_irn_ops);
232 return &isa->arch_env;
238 * Closes the output file and frees the ISA structure.
240 static void TEMPLATE_done(void *self)
242 TEMPLATE_isa_t *isa = self;
244 /* emit now all global declarations */
245 be_gas_emit_decls(isa->arch_env.main_env, 0);
252 static unsigned TEMPLATE_get_n_reg_class(void)
257 static const arch_register_class_t *TEMPLATE_get_reg_class(unsigned i)
259 assert(i < N_CLASSES);
260 return &TEMPLATE_reg_classes[i];
266 * Get the register class which shall be used to store a value of a given mode.
267 * @param self The this pointer.
268 * @param mode The mode in question.
269 * @return A register class which can hold values of the given mode.
271 const arch_register_class_t *TEMPLATE_get_reg_class_for_mode(const ir_mode *mode)
273 if (mode_is_float(mode))
274 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_fp];
276 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_gp];
282 be_abi_call_flags_bits_t flags;
283 const arch_env_t *arch_env;
285 } TEMPLATE_abi_env_t;
287 static void *TEMPLATE_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
289 TEMPLATE_abi_env_t *env = XMALLOC(TEMPLATE_abi_env_t);
290 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
291 env->flags = fl.bits;
293 env->arch_env = arch_env;
298 * Get the between type for that call.
299 * @param self The callback object.
300 * @return The between type of for that call.
302 static ir_type *TEMPLATE_get_between_type(void *self)
304 static ir_type *between_type = NULL;
305 static ir_entity *old_bp_ent = NULL;
309 ir_entity *ret_addr_ent;
310 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
311 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
313 between_type = new_type_class(new_id_from_str("TEMPLATE_between_type"));
314 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
315 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
317 set_entity_offset(old_bp_ent, 0);
318 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
319 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
326 * Build the prolog, return the BASE POINTER register
328 static const arch_register_t *TEMPLATE_abi_prologue(void *self, ir_node **mem,
329 pmap *reg_map, int *stack_bias)
331 TEMPLATE_abi_env_t *env = self;
336 if(env->flags.try_omit_fp)
337 return env->arch_env->sp;
338 return env->arch_env->bp;
341 /* Build the epilog */
342 static void TEMPLATE_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
351 static const be_abi_callbacks_t TEMPLATE_abi_callbacks = {
354 TEMPLATE_get_between_type,
355 TEMPLATE_abi_prologue,
356 TEMPLATE_abi_epilogue,
360 * Get the ABI restrictions for procedure calls.
361 * @param self The this pointer.
362 * @param method_type The type of the method (procedure) in question.
363 * @param abi The abi object to be modified
365 void TEMPLATE_get_call_abi(const void *self, ir_type *method_type,
370 int i, n = get_method_n_params(method_type);
371 be_abi_call_flags_t call_flags;
374 /* set abi flags for calls */
375 call_flags.bits.left_to_right = 0;
376 call_flags.bits.store_args_sequential = 1;
377 call_flags.bits.try_omit_fp = 1;
378 call_flags.bits.fp_free = 0;
379 call_flags.bits.call_has_imm = 1;
381 /* set stack parameter passing style */
382 be_abi_call_set_flags(abi, call_flags, &TEMPLATE_abi_callbacks);
384 for (i = 0; i < n; i++) {
385 /* TODO: implement register parameter: */
386 /* reg = get reg for param i; */
387 /* be_abi_call_param_reg(abi, i, reg); */
389 /* default: all parameters on stack */
390 tp = get_method_param_type(method_type, i);
391 mode = get_type_mode(tp);
392 be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
395 /* TODO: set correct return register */
396 /* default: return value is in R0 resp. F0 */
397 if (get_method_n_ress(method_type) > 0) {
398 tp = get_method_res_type(method_type, 0);
399 mode = get_type_mode(tp);
401 be_abi_call_res_reg(abi, 0,
402 mode_is_float(mode) ? &TEMPLATE_fp_regs[REG_F0] : &TEMPLATE_gp_regs[REG_R0]);
406 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn)
410 if(!is_TEMPLATE_irn(irn))
417 * Initializes the code generator interface.
419 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(
423 return &TEMPLATE_code_gen_if;
426 list_sched_selector_t TEMPLATE_sched_selector;
429 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
431 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(
432 const void *self, list_sched_selector_t *selector)
437 TEMPLATE_sched_selector = trivial_selector;
438 TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
439 return &TEMPLATE_sched_selector;
442 static const ilp_sched_selector_t *TEMPLATE_get_ilp_sched_selector(
450 * Returns the necessary byte alignment for storing a register of given class.
452 static int TEMPLATE_get_reg_class_alignment(const arch_register_class_t *cls)
454 ir_mode *mode = arch_register_class_mode(cls);
455 return get_mode_size_bytes(mode);
459 * Returns the libFirm configuration parameter for this backend.
461 static const backend_params *TEMPLATE_get_backend_params(void) {
462 static backend_params p = {
463 0, /* no dword lowering */
464 0, /* no inline assembly */
465 NULL, /* will be set later */
466 NULL, /* no creator function */
467 NULL, /* context for create_intrinsic_fkt */
468 NULL, /* parameter for if conversion */
469 NULL, /* float arithmetic mode */
470 0, /* no trampoline support: size 0 */
471 0, /* no trampoline support: align 0 */
472 NULL, /* no trampoline support: no trampoline builder */
473 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
478 static const be_execution_unit_t ***TEMPLATE_get_allowed_execution_units(
487 static const be_machine_t *TEMPLATE_get_machine(const void *self)
495 static ir_graph **TEMPLATE_get_backend_irg_list(const void *self,
503 static asm_constraint_flags_t TEMPLATE_parse_asm_constraint(const char **c)
506 return ASM_CONSTRAINT_FLAG_INVALID;
509 static int TEMPLATE_is_valid_clobber(const char *clobber)
515 const arch_isa_if_t TEMPLATE_isa_if = {
518 NULL, /* handle intrinsics */
519 TEMPLATE_get_n_reg_class,
520 TEMPLATE_get_reg_class,
521 TEMPLATE_get_reg_class_for_mode,
522 TEMPLATE_get_call_abi,
523 TEMPLATE_get_code_generator_if,
524 TEMPLATE_get_list_sched_selector,
525 TEMPLATE_get_ilp_sched_selector,
526 TEMPLATE_get_reg_class_alignment,
527 TEMPLATE_get_backend_params,
528 TEMPLATE_get_allowed_execution_units,
529 TEMPLATE_get_machine,
530 TEMPLATE_get_backend_irg_list,
531 NULL, /* mark remat */
532 TEMPLATE_parse_asm_constraint,
533 TEMPLATE_is_valid_clobber
536 void be_init_arch_TEMPLATE(void)
538 be_register_isa_if("TEMPLATE", &TEMPLATE_isa_if);
539 FIRM_DBG_REGISTER(dbg, "firm.be.TEMPLATE.cg");
540 TEMPLATE_init_transform();
542 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_TEMPLATE);