adapted to interface changes
[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         if (! is_pseudo_ir_graph(cg->irg)) {
258                 irg_walk_blkwise_graph(cg->irg, TEMPLATE_place_consts, TEMPLATE_transform_node, cg);
259         }
260 }
261
262
263
264 /**
265  * Fix offsets and stacksize
266  */
267 static void TEMPLATE_finish_irg(ir_graph *irg, TEMPLATE_code_gen_t *cg) {
268         /* TODO */
269 }
270
271
272 static void TEMPLATE_before_sched(void *self) {
273 }
274
275 static void TEMPLATE_before_ra(void *self) {
276 }
277
278
279 /**
280  * Creates a Store for a Spill
281  */
282 static ir_node *TEMPLATE_lower_spill(void *self, ir_node *spill) {
283         TEMPLATE_code_gen_t *cg = self;
284         dbg_info        *dbg   = get_irn_dbg_info(spill);
285         ir_node         *block = get_nodes_block(spill);
286         ir_node         *ptr   = get_irg_frame(cg->irg);
287         ir_node         *val   = be_get_Spill_context(spill);
288         ir_node         *mem   = new_rd_NoMem(cg->irg);
289         ir_mode         *mode  = get_irn_mode(spill);
290         ir_node         *res;
291         entity          *ent   = be_get_spill_entity(spill);
292         unsigned         offs  = get_entity_offset_bytes(ent);
293
294         DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
295
296         /* TODO: create Store */
297
298         return res;
299 }
300
301 /**
302  * Create a Load for a Spill
303  */
304 static ir_node *TEMPLATE_lower_reload(void *self, ir_node *reload) {
305         TEMPLATE_code_gen_t *cg = self;
306         dbg_info        *dbg   = get_irn_dbg_info(reload);
307         ir_node         *block = get_nodes_block(reload);
308         ir_node         *ptr   = get_irg_frame(cg->irg);
309         ir_mode         *mode  = get_irn_mode(reload);
310         ir_node         *pred  = get_irn_n(reload, 0);
311         tarval          *tv;
312         ir_node         *res;
313
314         /* TODO: create Load */
315
316         return res;
317 }
318
319 /**
320  * Returns the Stackregister
321  */
322 static const arch_register_t *TEMPLATE_get_stack_register(void *self) {
323         /* TODO */
324 }
325
326 /**
327  * Emits the code, closes the output file and frees
328  * the code generator interface.
329  */
330 static void TEMPLATE_codegen(void *self) {
331         TEMPLATE_code_gen_t *cg = self;
332         ir_graph           *irg = cg->irg;
333         FILE               *out = cg->out;
334
335         if (cg->emit_decls) {
336                 TEMPLATE_gen_decls(cg->out);
337                 cg->emit_decls = 0;
338         }
339
340         TEMPLATE_finish_irg(irg, cg);
341         dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
342         TEMPLATE_gen_routine(out, irg, cg);
343
344         cur_reg_set = NULL;
345
346         /* de-allocate code generator */
347         del_set(cg->reg_set);
348         free(self);
349 }
350
351 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
352
353 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
354         TEMPLATE_cg_init,
355         TEMPLATE_prepare_graph,
356         TEMPLATE_before_sched,   /* before scheduling hook */
357         TEMPLATE_before_ra,      /* before register allocation hook */
358         TEMPLATE_lower_spill,
359         TEMPLATE_lower_reload,
360         TEMPLATE_get_stack_register,
361         TEMPLATE_codegen         /* emit && done */
362 };
363
364 /**
365  * Initializes the code generator.
366  */
367 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
368         TEMPLATE_isa_t      *isa = (TEMPLATE_isa_t *)arch_env->isa;
369         TEMPLATE_code_gen_t *cg  = xmalloc(sizeof(*cg));
370
371         cg->impl       = &TEMPLATE_code_gen_if;
372         cg->irg        = irg;
373         cg->reg_set    = new_set(TEMPLATE_cmp_irn_reg_assoc, 1024);
374         cg->mod        = firm_dbg_register("firm.be.TEMPLATE.cg");
375         cg->out        = F;
376         cg->arch_env   = arch_env;
377
378         isa->num_codegens++;
379
380         if (isa->num_codegens > 1)
381                 cg->emit_decls = 0;
382         else
383                 cg->emit_decls = 1;
384
385         cur_reg_set = cg->reg_set;
386
387         TEMPLATE_irn_ops.cg = cg;
388
389         return (arch_code_generator_t *)cg;
390 }
391
392
393
394 /*****************************************************************
395  *  ____             _                  _   _____  _____
396  * |  _ \           | |                | | |_   _|/ ____|  /\
397  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
398  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
399  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
400  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
401  *
402  *****************************************************************/
403
404 /**
405  * Initializes the backend ISA and opens the output file.
406  */
407 static void *TEMPLATE_init(void) {
408         static int inited = 0;
409         TEMPLATE_isa_t *isa   = xmalloc(sizeof(*isa));
410
411         isa->impl = &TEMPLATE_isa_if;
412
413         if(inited)
414                 return NULL;
415
416         inited            = 1;
417         isa->num_codegens = 0;
418
419         TEMPLATE_register_init(isa);
420         TEMPLATE_create_opcodes();
421
422         return isa;
423 }
424
425
426
427 /**
428  * Closes the output file and frees the ISA structure.
429  */
430 static void TEMPLATE_done(void *self) {
431         free(self);
432 }
433
434
435
436 static int TEMPLATE_get_n_reg_class(const void *self) {
437         return N_CLASSES;
438 }
439
440 static const arch_register_class_t *TEMPLATE_get_reg_class(const void *self, int i) {
441         assert(i >= 0 && i < N_CLASSES && "Invalid TEMPLATE register class requested.");
442         return &TEMPLATE_reg_classes[i];
443 }
444
445 static const void *TEMPLATE_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
446         return &TEMPLATE_irn_ops;
447 }
448
449 const arch_irn_handler_t TEMPLATE_irn_handler = {
450         TEMPLATE_get_irn_ops
451 };
452
453 const arch_irn_handler_t *TEMPLATE_get_irn_handler(const void *self) {
454         return &TEMPLATE_irn_handler;
455 }
456
457 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn) {
458         return is_TEMPLATE_irn(irn);
459 }
460
461 /**
462  * Initializes the code generator interface.
463  */
464 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(void *self) {
465         return &TEMPLATE_code_gen_if;
466 }
467
468 list_sched_selector_t TEMPLATE_sched_selector;
469
470 /**
471  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
472  */
473 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(const void *self) {
474         memcpy(&TEMPLATE_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
475         TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
476         return &TEMPLATE_sched_selector;
477 }
478
479 #ifdef WITH_LIBCORE
480 static void TEMPLATE_register_options(lc_opt_entry_t *ent)
481 {
482 }
483 #endif /* WITH_LIBCORE */
484
485 const arch_isa_if_t TEMPLATE_isa_if = {
486 #ifdef WITH_LIBCORE
487         TEMPLATE_register_options,
488 #endif
489         TEMPLATE_init,
490         TEMPLATE_done,
491         TEMPLATE_get_n_reg_class,
492         TEMPLATE_get_reg_class,
493         TEMPLATE_get_irn_handler,
494         TEMPLATE_get_code_generator_if,
495         TEMPLATE_get_list_sched_selector,
496 };