e14f30a5e7273ad6c31fcc55103019212a7ce06a
[libfirm] / ir / be / TEMPLATE / bearch_TEMPLATE.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    The main TEMPLATE backend driver file.
23  * @version  $Id$
24  */
25 #include "config.h"
26
27 #include "pseudo_irg.h"
28 #include "irgwalk.h"
29 #include "irprog.h"
30 #include "irprintf.h"
31 #include "ircons.h"
32 #include "irgmod.h"
33
34 #include "bitset.h"
35 #include "debug.h"
36
37 #include "be.h"
38 #include "../bearch.h"
39 #include "../benode.h"
40 #include "../belower.h"
41 #include "../besched.h"
42 #include "../beabi.h"
43 #include "../bemodule.h"
44 #include "../begnuas.h"
45 #include "../belistsched.h"
46
47 #include "bearch_TEMPLATE_t.h"
48
49 #include "TEMPLATE_new_nodes.h"
50 #include "gen_TEMPLATE_regalloc_if.h"
51 #include "TEMPLATE_transform.h"
52 #include "TEMPLATE_emitter.h"
53
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55
56 static arch_irn_class_t TEMPLATE_classify(const ir_node *irn)
57 {
58         (void) irn;
59         return 0;
60 }
61
62 static ir_entity *TEMPLATE_get_frame_entity(const ir_node *node)
63 {
64         (void) node;
65         /* TODO: return the ir_entity assigned to the frame */
66         return NULL;
67 }
68
69 static void TEMPLATE_set_frame_entity(ir_node *node, ir_entity *ent)
70 {
71         (void) node;
72         (void) ent;
73         /* TODO: set the ir_entity assigned to the frame */
74 }
75
76 /**
77  * This function is called by the generic backend to correct offsets for
78  * nodes accessing the stack.
79  */
80 static void TEMPLATE_set_frame_offset(ir_node *irn, int offset)
81 {
82         (void) irn;
83         (void) offset;
84         /* TODO: correct offset if irn accesses the stack */
85 }
86
87 static int TEMPLATE_get_sp_bias(const ir_node *irn)
88 {
89         (void) irn;
90         return 0;
91 }
92
93 /* fill register allocator interface */
94
95 static const arch_irn_ops_t TEMPLATE_irn_ops = {
96         get_TEMPLATE_in_req,
97         TEMPLATE_classify,
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  */
106 };
107
108
109
110 /**
111  * Transforms the standard firm graph into
112  * a TEMLPATE firm graph
113  */
114 static void TEMPLATE_prepare_graph(void *self)
115 {
116         TEMPLATE_code_gen_t *cg = self;
117
118         irg_walk_blkwise_graph(cg->irg, NULL, TEMPLATE_transform_node, cg);
119 }
120
121
122
123 /**
124  * Called immediatly before emit phase.
125  */
126 static void TEMPLATE_finish_irg(void *self)
127 {
128         TEMPLATE_code_gen_t *cg = self;
129         ir_graph            *irg = cg->irg;
130
131         dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
132 }
133
134
135 static void TEMPLATE_before_ra(void *self)
136 {
137         (void) self;
138         /* Some stuff you need to do after scheduling but before register allocation */
139 }
140
141 static void TEMPLATE_after_ra(void *self)
142 {
143         (void) self;
144         /* Some stuff you need to do immediatly after register allocation */
145 }
146
147
148
149 /**
150  * Emits the code, closes the output file and frees
151  * the code generator interface.
152  */
153 static void TEMPLATE_emit_and_done(void *self)
154 {
155         TEMPLATE_code_gen_t *cg = self;
156         ir_graph           *irg = cg->irg;
157
158         TEMPLATE_gen_routine(cg, irg);
159
160         /* de-allocate code generator */
161         free(cg);
162 }
163
164 static void *TEMPLATE_cg_init(be_irg_t *birg);
165
166 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
167         TEMPLATE_cg_init,
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 */
174         TEMPLATE_finish_irg,
175         TEMPLATE_emit_and_done
176 };
177
178 /**
179  * Initializes the code generator.
180  */
181 static void *TEMPLATE_cg_init(be_irg_t *birg)
182 {
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);
186
187         cg->impl     = &TEMPLATE_code_gen_if;
188         cg->irg      = be_get_birg_irg(birg);
189         cg->isa      = isa;
190         cg->birg     = birg;
191
192         return (arch_code_generator_t *)cg;
193 }
194
195
196
197 const arch_isa_if_t TEMPLATE_isa_if;
198 static TEMPLATE_isa_t TEMPLATE_isa_template = {
199         {
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 */
209         },
210 };
211
212 /**
213  * Initializes the backend ISA
214  */
215 static arch_env_t *TEMPLATE_init(FILE *outfile)
216 {
217         static int run_once = 0;
218         TEMPLATE_isa_t *isa;
219
220         if(run_once)
221                 return NULL;
222         run_once = 1;
223
224         isa = XMALLOC(TEMPLATE_isa_t);
225         memcpy(isa, &TEMPLATE_isa_template, sizeof(*isa));
226
227         be_emit_init(outfile);
228
229         TEMPLATE_register_init();
230         TEMPLATE_create_opcodes(&TEMPLATE_irn_ops);
231
232         return &isa->arch_env;
233 }
234
235
236
237 /**
238  * Closes the output file and frees the ISA structure.
239  */
240 static void TEMPLATE_done(void *self)
241 {
242         TEMPLATE_isa_t *isa = self;
243
244         /* emit now all global declarations */
245         be_gas_emit_decls(isa->arch_env.main_env, 0);
246
247         be_emit_exit();
248         free(self);
249 }
250
251
252 static unsigned TEMPLATE_get_n_reg_class(void)
253 {
254         return N_CLASSES;
255 }
256
257 static const arch_register_class_t *TEMPLATE_get_reg_class(unsigned i)
258 {
259         assert(i < N_CLASSES);
260         return &TEMPLATE_reg_classes[i];
261 }
262
263
264
265 /**
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.
270  */
271 const arch_register_class_t *TEMPLATE_get_reg_class_for_mode(const ir_mode *mode)
272 {
273         if (mode_is_float(mode))
274                 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_fp];
275         else
276                 return &TEMPLATE_reg_classes[CLASS_TEMPLATE_gp];
277 }
278
279
280
281 typedef struct {
282         be_abi_call_flags_bits_t flags;
283         const arch_env_t *arch_env;
284         ir_graph *irg;
285 } TEMPLATE_abi_env_t;
286
287 static void *TEMPLATE_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
288 {
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;
292         env->irg      = irg;
293         env->arch_env = arch_env;
294         return env;
295 }
296
297 /**
298  * Get the between type for that call.
299  * @param self The callback object.
300  * @return The between type of for that call.
301  */
302 static ir_type *TEMPLATE_get_between_type(void *self)
303 {
304         static ir_type *between_type = NULL;
305         static ir_entity *old_bp_ent = NULL;
306         (void) self;
307
308         if(!between_type) {
309                 ir_entity *ret_addr_ent;
310                 ir_type *ret_addr_type = new_type_primitive(mode_P);
311                 ir_type *old_bp_type   = new_type_primitive(mode_P);
312
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);
316
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));
320         }
321
322         return between_type;
323 }
324
325 /**
326  * Build the prolog, return the BASE POINTER register
327  */
328 static const arch_register_t *TEMPLATE_abi_prologue(void *self, ir_node **mem,
329                                                     pmap *reg_map, int *stack_bias)
330 {
331         TEMPLATE_abi_env_t *env = self;
332         (void) reg_map;
333         (void) mem;
334         (void) stack_bias;
335
336         if(env->flags.try_omit_fp)
337                 return env->arch_env->sp;
338         return env->arch_env->bp;
339 }
340
341 /* Build the epilog */
342 static void TEMPLATE_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
343                                   pmap *reg_map)
344 {
345         (void) self;
346         (void) bl;
347         (void) mem;
348         (void) reg_map;
349 }
350
351 static const be_abi_callbacks_t TEMPLATE_abi_callbacks = {
352         TEMPLATE_abi_init,
353         free,
354         TEMPLATE_get_between_type,
355         TEMPLATE_abi_prologue,
356         TEMPLATE_abi_epilogue,
357 };
358
359 /**
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
364  */
365 void TEMPLATE_get_call_abi(const void *self, ir_type *method_type,
366                            be_abi_call_t *abi)
367 {
368         ir_type  *tp;
369         ir_mode  *mode;
370         int       i, n = get_method_n_params(method_type);
371         be_abi_call_flags_t call_flags;
372         (void) self;
373
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;
380
381         /* set stack parameter passing style */
382         be_abi_call_set_flags(abi, call_flags, &TEMPLATE_abi_callbacks);
383
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); */
388
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);
393         }
394
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);
400
401                 be_abi_call_res_reg(abi, 0,
402                         mode_is_float(mode) ? &TEMPLATE_fp_regs[REG_F0] : &TEMPLATE_gp_regs[REG_R0]);
403         }
404 }
405
406 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn)
407 {
408         (void) block_env;
409
410         if(!is_TEMPLATE_irn(irn))
411                 return -1;
412
413         return 1;
414 }
415
416 /**
417  * Initializes the code generator interface.
418  */
419 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(
420                 void *self)
421 {
422         (void) self;
423         return &TEMPLATE_code_gen_if;
424 }
425
426 list_sched_selector_t TEMPLATE_sched_selector;
427
428 /**
429  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
430  */
431 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(
432                 const void *self, list_sched_selector_t *selector)
433 {
434         (void) self;
435         (void) selector;
436
437         TEMPLATE_sched_selector = trivial_selector;
438         TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
439         return &TEMPLATE_sched_selector;
440 }
441
442 static const ilp_sched_selector_t *TEMPLATE_get_ilp_sched_selector(
443                 const void *self)
444 {
445         (void) self;
446         return NULL;
447 }
448
449 /**
450  * Returns the necessary byte alignment for storing a register of given class.
451  */
452 static int TEMPLATE_get_reg_class_alignment(const arch_register_class_t *cls)
453 {
454         ir_mode *mode = arch_register_class_mode(cls);
455         return get_mode_size_bytes(mode);
456 }
457
458 /**
459  * Returns the libFirm configuration parameter for this backend.
460  */
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) */
474         };
475         return &p;
476 }
477
478 static const be_execution_unit_t ***TEMPLATE_get_allowed_execution_units(
479                 const ir_node *irn)
480 {
481         (void) irn;
482         /* TODO */
483         assert(0);
484         return NULL;
485 }
486
487 static const be_machine_t *TEMPLATE_get_machine(const void *self)
488 {
489         (void) self;
490         /* TODO */
491         assert(0);
492         return NULL;
493 }
494
495 static ir_graph **TEMPLATE_get_backend_irg_list(const void *self,
496                                                 ir_graph ***irgs)
497 {
498         (void) self;
499         (void) irgs;
500         return NULL;
501 }
502
503 static asm_constraint_flags_t TEMPLATE_parse_asm_constraint(const char **c)
504 {
505         (void) c;
506         return ASM_CONSTRAINT_FLAG_INVALID;
507 }
508
509 static int TEMPLATE_is_valid_clobber(const char *clobber)
510 {
511         (void) clobber;
512         return 0;
513 }
514
515 const arch_isa_if_t TEMPLATE_isa_if = {
516         TEMPLATE_init,
517         TEMPLATE_done,
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
534 };
535
536 void be_init_arch_TEMPLATE(void)
537 {
538         be_register_isa_if("TEMPLATE", &TEMPLATE_isa_if);
539         FIRM_DBG_REGISTER(dbg, "firm.be.TEMPLATE.cg");
540         TEMPLATE_init_transform();
541 }
542 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_TEMPLATE);