removed unnecessary function
[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  * Emits the code, closes the output file and frees
319  * the code generator interface.
320  */
321 static void TEMPLATE_codegen(void *self) {
322         TEMPLATE_code_gen_t *cg = self;
323         ir_graph           *irg = cg->irg;
324         FILE               *out = cg->out;
325
326         if (cg->emit_decls) {
327                 TEMPLATE_gen_decls(cg->out);
328                 cg->emit_decls = 0;
329         }
330
331         TEMPLATE_finish_irg(irg, cg);
332         dump_ir_block_graph_sched(irg, "-TEMPLATE-finished");
333         TEMPLATE_gen_routine(out, irg, cg);
334
335         cur_reg_set = NULL;
336
337         /* de-allocate code generator */
338         del_set(cg->reg_set);
339         free(self);
340 }
341
342 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
343
344 static const arch_code_generator_if_t TEMPLATE_code_gen_if = {
345         TEMPLATE_cg_init,
346         TEMPLATE_prepare_graph,
347         TEMPLATE_before_sched,   /* before scheduling hook */
348         TEMPLATE_before_ra,      /* before register allocation hook */
349         TEMPLATE_lower_spill,
350         TEMPLATE_lower_reload,
351         TEMPLATE_codegen         /* emit && done */
352 };
353
354 /**
355  * Initializes the code generator.
356  */
357 static void *TEMPLATE_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
358         TEMPLATE_isa_t      *isa = (TEMPLATE_isa_t *)arch_env->isa;
359         TEMPLATE_code_gen_t *cg  = xmalloc(sizeof(*cg));
360
361         cg->impl       = &TEMPLATE_code_gen_if;
362         cg->irg        = irg;
363         cg->reg_set    = new_set(TEMPLATE_cmp_irn_reg_assoc, 1024);
364         cg->mod        = firm_dbg_register("firm.be.TEMPLATE.cg");
365         cg->out        = F;
366         cg->arch_env   = arch_env;
367
368         isa->num_codegens++;
369
370         if (isa->num_codegens > 1)
371                 cg->emit_decls = 0;
372         else
373                 cg->emit_decls = 1;
374
375         cur_reg_set = cg->reg_set;
376
377         TEMPLATE_irn_ops.cg = cg;
378
379         return (arch_code_generator_t *)cg;
380 }
381
382
383
384 /*****************************************************************
385  *  ____             _                  _   _____  _____
386  * |  _ \           | |                | | |_   _|/ ____|  /\
387  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
388  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
389  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
390  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
391  *
392  *****************************************************************/
393
394 /**
395  * Initializes the backend ISA and opens the output file.
396  */
397 static void *TEMPLATE_init(void) {
398         static int inited = 0;
399         TEMPLATE_isa_t *isa   = xmalloc(sizeof(*isa));
400
401         isa->impl = &TEMPLATE_isa_if;
402
403         if(inited)
404                 return NULL;
405
406         inited            = 1;
407         isa->num_codegens = 0;
408
409         TEMPLATE_register_init(isa);
410         TEMPLATE_create_opcodes();
411
412         return isa;
413 }
414
415
416
417 /**
418  * Closes the output file and frees the ISA structure.
419  */
420 static void TEMPLATE_done(void *self) {
421         free(self);
422 }
423
424
425
426 static int TEMPLATE_get_n_reg_class(const void *self) {
427         return N_CLASSES;
428 }
429
430 static const arch_register_class_t *TEMPLATE_get_reg_class(const void *self, int i) {
431         assert(i >= 0 && i < N_CLASSES && "Invalid TEMPLATE register class requested.");
432         return &TEMPLATE_reg_classes[i];
433 }
434
435 static const void *TEMPLATE_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
436         return &TEMPLATE_irn_ops;
437 }
438
439 const arch_irn_handler_t TEMPLATE_irn_handler = {
440         TEMPLATE_get_irn_ops
441 };
442
443 const arch_irn_handler_t *TEMPLATE_get_irn_handler(const void *self) {
444         return &TEMPLATE_irn_handler;
445 }
446
447 int TEMPLATE_to_appear_in_schedule(void *block_env, const ir_node *irn) {
448         return is_TEMPLATE_irn(irn);
449 }
450
451 /**
452  * Initializes the code generator interface.
453  */
454 static const arch_code_generator_if_t *TEMPLATE_get_code_generator_if(void *self) {
455         return &TEMPLATE_code_gen_if;
456 }
457
458 list_sched_selector_t TEMPLATE_sched_selector;
459
460 /**
461  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
462  */
463 static const list_sched_selector_t *TEMPLATE_get_list_sched_selector(const void *self) {
464         memcpy(&TEMPLATE_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
465         TEMPLATE_sched_selector.to_appear_in_schedule = TEMPLATE_to_appear_in_schedule;
466         return &TEMPLATE_sched_selector;
467 }
468
469 #ifdef WITH_LIBCORE
470 static void TEMPLATE_register_options(lc_opt_entry_t *ent)
471 {
472 }
473 #endif /* WITH_LIBCORE */
474
475 const arch_isa_if_t TEMPLATE_isa_if = {
476 #ifdef WITH_LIBCORE
477         TEMPLATE_register_options,
478 #endif
479         TEMPLATE_init,
480         TEMPLATE_done,
481         TEMPLATE_get_n_reg_class,
482         TEMPLATE_get_reg_class,
483         TEMPLATE_get_irn_handler,
484         TEMPLATE_get_code_generator_if,
485         TEMPLATE_get_list_sched_selector,
486 };