added AM emitter
[libfirm] / ir / be / TEMPLATE / bearch_TEMPLATE.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "pseudo_irg.h"
6 #include "irgwalk.h"
7 #include "irprog.h"
8 #include "irprintf.h"
9 #include "ircons.h"
10 #include "irgmod.h"
11
12 #include "bitset.h"
13 #include "debug.h"
14
15 #include "../bearch.h"                /* the general register allocator interface */
16 #include "../benode_t.h"
17 #include "../belower.h"
18 #include "../besched_t.h"
19 #include "bearch_TEMPLATE_t.h"
20
21 #include "TEMPLATE_new_nodes.h"           /* TEMPLATE nodes interface */
22 #include "gen_TEMPLATE_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
23 #include "TEMPLATE_gen_decls.h"           /* interface declaration emitter */
24 #include "TEMPLATE_transform.h"
25 #include "TEMPLATE_emitter.h"
26 #include "TEMPLATE_map_regs.h"
27
28 #define DEBUG_MODULE "firm.be.TEMPLATE.isa"
29
30 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
31 static set *cur_reg_set = NULL;
32
33 /**************************************************
34  *                         _ _              _  __
35  *                        | | |            (_)/ _|
36  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
37  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
38  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
39  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
40  *            __/ |
41  *           |___/
42  **************************************************/
43
44 static ir_node *my_skip_proj(const ir_node *n) {
45         while (is_Proj(n))
46                 n = get_Proj_pred(n);
47         return (ir_node *)n;
48 }
49
50 /**
51  * Return register requirements for a TEMPLATE node.
52  * If the node returns a tuple (mode_T) then the proj's
53  * will be asked for this information.
54  */
55 static const arch_register_req_t *TEMPLATE_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
56         const TEMPLATE_register_req_t *irn_req;
57         long               node_pos = pos == -1 ? 0 : pos;
58         ir_mode           *mode     = get_irn_mode(irn);
59         firm_dbg_module_t *mod      = firm_dbg_register(DEBUG_MODULE);
60
61         if (mode == mode_T || mode == mode_M) {
62                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
63                 return NULL;
64         }
65
66         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
67
68         if (is_Proj(irn)) {
69                 /* in case of a proj, we need to get the correct OUT slot */
70                 /* of the node corresponding to the proj number */
71                 if (pos == -1) {
72                         node_pos = TEMPLATE_translate_proj_pos(irn);
73                 }
74                 else {
75                         node_pos = pos;
76                 }
77
78                 irn = my_skip_proj(irn);
79
80                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
81         }
82
83         /* get requirements for our own nodes */
84         if (is_TEMPLATE_irn(irn)) {
85                 if (pos >= 0) {
86                         irn_req = get_TEMPLATE_in_req(irn, pos);
87                 }
88                 else {
89                         irn_req = get_TEMPLATE_out_req(irn, node_pos);
90                 }
91
92                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
93
94                 memcpy(req, &(irn_req->req), sizeof(*req));
95
96                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
97                         arch_register_req_is(&(irn_req->req), should_be_different)) {
98                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
99                         req->other = get_irn_n(irn, irn_req->pos);
100                 }
101         }
102         /* get requirements for FIRM nodes */
103         else {
104                 /* treat Phi like Const with default requirements */
105                 if (is_Phi(irn)) {
106                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
107
108                         if (mode_is_float(mode)) {
109                                 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_floating_point.req), sizeof(*req));
110                         }
111                         else if (mode_is_int(mode) || mode_is_reference(mode)) {
112                                 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_general_purpose.req), sizeof(*req));
113                         }
114                         else if (mode == mode_T || mode == mode_M) {
115                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
116                                 return NULL;
117                         }
118                         else {
119                                 assert(0 && "unsupported Phi-Mode");
120                         }
121                 }
122                 else if ((get_irn_op(irn) == op_Return) && pos > 0) {
123                         /* pos == 0 is Memory -> no requirements */
124                         DB((mod, LEVEL_1, "giving return (%+F) requirements\n", irn));
125
126                         if (pos == 1) {
127                                 /* pos == 1 is Stackpointer */
128                                 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_general_purpose_r6.req), sizeof(*req));
129                         }
130                         else {
131                                 if (mode_is_float(get_irn_mode(get_Return_res(irn, pos)))) {
132                                         /* fp result */
133                                         memcpy(req, &(TEMPLATE_default_req_TEMPLATE_floating_point_f0.req), sizeof(*req));
134                                 }
135                                 else {
136                                         /* integer result, 64bit results are returned as two 32bit values */
137                                         if (pos == 2) {
138                                                 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_general_purpose_r0.req), sizeof(*req));
139                                         }
140                                         else {
141                                                 memcpy(req, &(TEMPLATE_default_req_TEMPLATE_general_purpose_r1.req), sizeof(*req));
142                                         }
143                                 }
144                         }
145                 }
146                 else {
147                         DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
148                         req = NULL;
149                 }
150         }
151
152         return req;
153 }
154
155 static void TEMPLATE_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
156         int pos = 0;
157
158         else if (is_Proj(irn)) {
159                 pos = TEMPLATE_translate_proj_pos(irn);
160                 irn = my_skip_proj(irn);
161         }
162
163         if (is_TEMPLATE_irn(irn)) {
164                 const arch_register_t **slots;
165
166                 slots      = get_TEMPLATE_slots(irn);
167                 slots[pos] = reg;
168         }
169         else {
170                 /* here we set the registers for the Phi nodes */
171                 TEMPLATE_set_firm_reg(irn, reg, cur_reg_set);
172         }
173 }
174
175 static const arch_register_t *TEMPLATE_get_irn_reg(const void *self, const ir_node *irn) {
176         int pos = 0;
177         const arch_register_t *reg = NULL;
178
179         else if (is_Proj(irn)) {
180                 pos = TEMPLATE_translate_proj_pos(irn);
181                 irn = my_skip_proj(irn);
182         }
183
184         if (is_TEMPLATE_irn(irn)) {
185                 const arch_register_t **slots;
186                 slots = get_TEMPLATE_slots(irn);
187                 reg   = slots[pos];
188         }
189         else {
190                 reg = TEMPLATE_get_firm_reg(irn, cur_reg_set);
191         }
192
193         return reg;
194 }
195
196 static arch_irn_class_t TEMPLATE_classify(const void *self, const ir_node *irn) {
197         irn = my_skip_proj(irn);
198
199         if (is_cfop(irn)) {
200                 return arch_irn_class_branch;
201         }
202         else if (is_TEMPLATE_irn(irn)) {
203                 return arch_irn_class_normal;
204         }
205
206         return 0;
207 }
208
209 static arch_irn_flags_t TEMPLATE_get_flags(const void *self, const ir_node *irn) {
210         irn = my_skip_proj(irn);
211
212         if (is_TEMPLATE_irn(irn)) {
213                 return get_TEMPLATE_flags(irn);
214         }
215         else if (is_Unknown(irn)) {
216                 return arch_irn_flags_ignore;
217         }
218
219         return 0;
220 }
221
222 /* fill register allocator interface */
223
224 static const arch_irn_ops_if_t TEMPLATE_irn_ops_if = {
225         TEMPLATE_get_irn_reg_req,
226         TEMPLATE_set_irn_reg,
227         TEMPLATE_get_irn_reg,
228         TEMPLATE_classify,
229         TEMPLATE_get_flags
230 };
231
232 TEMPLATE_irn_ops_t TEMPLATE_irn_ops = {
233         &TEMPLATE_irn_ops_if,
234         NULL
235 };
236
237
238
239 /**************************************************
240  *                _                         _  __
241  *               | |                       (_)/ _|
242  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
243  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
244  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
245  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
246  *                        __/ |
247  *                       |___/
248  **************************************************/
249
250 /**
251  * Transforms the standard firm graph into
252  * a TEMLPATE firm graph
253  */
254 static void TEMPLATE_prepare_graph(void *self) {
255         TEMPLATE_code_gen_t *cg = self;
256
257         irg_walk_blkwise_graph(cg->irg, TEMPLATE_place_consts, TEMPLATE_transform_node, cg);
258 }
259
260
261
262 /**
263  * Fix offsets and stacksize
264  */
265 static void TEMPLATE_finish_irg(ir_graph *irg, TEMPLATE_code_gen_t *cg) {
266         /* TODO */
267 }
268
269
270 static void TEMPLATE_before_sched(void *self) {
271 }
272
273 static void TEMPLATE_before_ra(void *self) {
274 }
275
276
277 /**
278  * Creates a Store for a Spill
279  */
280 static ir_node *TEMPLATE_lower_spill(void *self, ir_node *spill) {
281         TEMPLATE_code_gen_t *cg = self;
282         dbg_info        *dbg   = get_irn_dbg_info(spill);
283         ir_node         *block = get_nodes_block(spill);
284         ir_node         *ptr   = get_irg_frame(cg->irg);
285         ir_node         *val   = be_get_Spill_context(spill);
286         ir_node         *mem   = new_rd_NoMem(cg->irg);
287         ir_mode         *mode  = get_irn_mode(spill);
288         ir_node         *res;
289         entity          *ent   = be_get_spill_entity(spill);
290         unsigned         offs  = get_entity_offset_bytes(ent);
291
292         DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
293
294         /* TODO: create Store */
295
296         return res;
297 }
298
299 /**
300  * Create a Load for a Spill
301  */
302 static ir_node *TEMPLATE_lower_reload(void *self, ir_node *reload) {
303         TEMPLATE_code_gen_t *cg = self;
304         dbg_info        *dbg   = get_irn_dbg_info(reload);
305         ir_node         *block = get_nodes_block(reload);
306         ir_node         *ptr   = get_irg_frame(cg->irg);
307         ir_mode         *mode  = get_irn_mode(reload);
308         ir_node         *pred  = get_irn_n(reload, 0);
309         tarval          *tv;
310         ir_node         *res;
311
312         /* TODO: create Load */
313
314         return res;
315 }
316
317 /**
318  * Returns the Stackregister
319  */
320 static const arch_register_t *TEMPLATE_get_stack_register(void *self) {
321         /* TODO */
322 }
323
324 /**
325  * Emits the code, closes the output file and frees
326  * the code generator interface.
327  */
328 static void TEMPLATE_codegen(void *self) {
329         TEMPLATE_code_gen_t *cg = self;
330         ir_graph           *irg = cg->irg;
331         FILE               *out = cg->out;
332
333         if (cg->emit_decls) {
334                 TEMPLATE_gen_decls(cg->out);
335                 cg->emit_decls = 0;
336         }
337
338         TEMPLATE_finish_irg(irg, cg);
339         dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
340         TEMPLATE_gen_routine(out, irg, cg);
341
342         cur_reg_set = NULL;
343
344         /* de-allocate code generator */
345         del_set(cg->reg_set);
346         free(self);
347 }
348
349 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
350
351 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
352         TEMPLATE_cg_init,
353         TEMPLATE_prepare_graph,
354         TEMPLATE_before_sched,   /* before scheduling hook */
355         TEMPLATE_before_ra,      /* before register allocation hook */
356         TEMPLATE_lower_spill,
357         TEMPLATE_lower_reload,
358         TEMPLATE_get_stack_register,
359         TEMPLATE_codegen         /* emit && done */
360 };
361
362 /**
363  * Initializes the code generator.
364  */
365 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
366         TEMPLATE_isa_t      *isa = (TEMPLATE_isa_t *)arch_env->isa;
367         TEMPLATE_code_gen_t *cg  = xmalloc(sizeof(*cg));
368
369         cg->impl       = &TEMPLATE_code_gen_if;
370         cg->irg        = irg;
371         cg->reg_set    = new_set(TEMPLATE_cmp_irn_reg_assoc, 1024);
372         cg->mod        = firm_dbg_register("firm.be.TEMPLATE.cg");
373         cg->out        = F;
374         cg->arch_env   = arch_env;
375
376         isa->num_codegens++;
377
378         if (isa->num_codegens > 1)
379                 cg->emit_decls = 0;
380         else
381                 cg->emit_decls = 1;
382
383         cur_reg_set = cg->reg_set;
384
385         TEMPLATE_irn_ops.cg = cg;
386
387         return (arch_code_generator_t *)cg;
388 }
389
390
391
392 /*****************************************************************
393  *  ____             _                  _   _____  _____
394  * |  _ \           | |                | | |_   _|/ ____|  /\
395  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
396  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
397  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
398  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
399  *
400  *****************************************************************/
401
402 /**
403  * Initializes the backend ISA and opens the output file.
404  */
405 static void *TEMPLATE_init(void) {
406         static int inited = 0;
407         TEMPLATE_isa_t *isa   = xmalloc(sizeof(*isa));
408
409         isa->impl = &TEMPLATE_isa_if;
410
411         if(inited)
412                 return NULL;
413
414         inited            = 1;
415         isa->num_codegens = 0;
416
417         TEMPLATE_register_init(isa);
418         TEMPLATE_create_opcodes();
419
420         return isa;
421 }
422
423
424
425 /**
426  * Closes the output file and frees the ISA structure.
427  */
428 static void TEMPLATE_done(void *self) {
429         free(self);
430 }
431
432
433
434 static int TEMPLATE_get_n_reg_class(const void *self) {
435         return N_CLASSES;
436 }
437
438 static const arch_register_class_t *TEMPLATE_get_reg_class(const void *self, int i) {
439         assert(i >= 0 && i < N_CLASSES && "Invalid TEMPLATE register class requested.");
440         return &TEMPLATE_reg_classes[i];
441 }
442
443 static const void *TEMPLATE_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
444         return &TEMPLATE_irn_ops;
445 }
446
447 const arch_irn_handler_t TEMPLATE_irn_handler = {
448         TEMPLATE_get_irn_ops
449 };
450
451 const arch_irn_handler_t *TEMPLATE_get_irn_handler(const void *self) {
452         return &TEMPLATE_irn_handler;
453 }
454
455 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn) {
456         return is_TEMPLATE_irn(irn);
457 }
458
459 /**
460  * Initializes the code generator interface.
461  */
462 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(void *self) {
463         return &TEMPLATE_code_gen_if;
464 }
465
466 list_sched_selector_t TEMPLATE_sched_selector;
467
468 /**
469  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
470  */
471 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(const void *self) {
472         memcpy(&TEMPLATE_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
473         TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
474         return &TEMPLATE_sched_selector;
475 }
476
477 #ifdef WITH_LIBCORE
478 static void TEMPLATE_register_options(lc_opt_entry_t *ent)
479 {
480 }
481 #endif /* WITH_LIBCORE */
482
483 const arch_isa_if_t TEMPLATE_isa_if = {
484 #ifdef WITH_LIBCORE
485         TEMPLATE_register_options,
486 #endif
487         TEMPLATE_init,
488         TEMPLATE_done,
489         TEMPLATE_get_n_reg_class,
490         TEMPLATE_get_reg_class,
491         TEMPLATE_get_irn_handler,
492         TEMPLATE_get_code_generator_if,
493         TEMPLATE_get_list_sched_selector,
494 };