Minor changes
[libfirm] / ir / be / firm / bearch_firm.c
1
2 /**
3  * ISA implementation for Firm IR nodes.
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #ifdef WITH_LIBCORE
10 #include <libcore/lc_opts.h>
11 #endif
12
13 #include "bitset.h"
14 #include "obst.h"
15
16 #include "irmode_t.h"
17 #include "irnode_t.h"
18 #include "iredges_t.h"
19 #include "irgmod.h"
20 #include "ircons_t.h"
21 #include "irgwalk.h"
22 #include "type.h"
23
24 #include "../bearch.h"
25 #include "../besched.h"
26 #include "../beutil.h"
27
28 #define N_REGS 3
29
30 typedef struct {
31   enum  { imm_Const, imm_SymConst } tp;
32   union {
33     const_attr    cnst_attr;
34     symconst_attr symc_attr;
35   } data;
36 } imm_attr_t;
37
38 static arch_register_t datab_regs[N_REGS];
39
40 static arch_register_class_t reg_classes[] = {
41   { "datab", N_REGS, datab_regs },
42 };
43
44 static ir_op *op_push;
45 static ir_op *op_imm;
46
47 const arch_isa_if_t firm_isa;
48
49 #define N_CLASSES \
50   (sizeof(reg_classes) / sizeof(reg_classes[0]))
51
52 #define CLS_DATAB 0
53
54 tarval *get_Imm_tv(ir_node *n) {
55   imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
56   return attr->tp == imm_Const ? attr->data.cnst_attr.tv : NULL;
57 }
58
59 int is_Imm(const ir_node *irn) {
60   return get_irn_op(irn) == op_imm;
61 }
62
63 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
64   ir_mode    *mode;
65   int        bad = 0;
66   char       buf[1024];
67   tarval     *tv;
68   imm_attr_t *attr;
69
70   switch (reason) {
71     case dump_node_opcode_txt:
72       tv = get_Imm_tv(n);
73
74       if (tv) {
75         tarval_snprintf(buf, sizeof(buf), tv);
76         fprintf(F, "%s", buf);
77       }
78       else {
79         fprintf(F, "immSymC");
80       }
81       break;
82
83     case dump_node_mode_txt:
84       mode = get_irn_mode(n);
85
86       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
87         fprintf(F, "[%s]", get_mode_name(mode));
88       }
89       break;
90
91     case dump_node_nodeattr_txt:
92       attr = (imm_attr_t *)get_irn_generic_attr(n);
93
94       if (is_Imm(n) && attr->tp == imm_SymConst) {
95         const char *name    = NULL;
96         symconst_attr *sc_attr = &attr->data.symc_attr;
97
98         switch (sc_attr->num) {
99           case symconst_addr_name:
100             name = get_id_str(sc_attr->sym.ident_p);
101             break;
102
103           case symconst_addr_ent:
104             name = get_entity_ld_name(sc_attr->sym.entity_p);
105             break;
106
107           default:
108             assert(!"Unsupported SymConst");
109         }
110
111         fprintf(F, "&%s ", name);
112       }
113
114       break;
115
116     case dump_node_info_txt:
117       break;
118   }
119
120   return bad;
121 }
122
123 static void *firm_init(void)
124 {
125   static struct obstack obst;
126   static int inited = 0;
127         arch_isa_t *isa = malloc(sizeof(*isa));
128   int k;
129
130         isa->impl = &firm_isa;
131
132   if(inited)
133     return NULL;
134
135   inited = 1;
136   obstack_init(&obst);
137
138   for(k = 0; k < N_CLASSES; ++k) {
139     const arch_register_class_t *cls = &reg_classes[k];
140     int i;
141
142     for(i = 0; i < cls->n_regs; ++i) {
143       int n;
144       char buf[8];
145       char *name;
146       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
147
148       n = snprintf(buf, sizeof(buf), "r%d", i);
149       name = obstack_copy0(&obst, buf, n);
150
151       reg->name = name;
152       reg->reg_class = cls;
153       reg->index = i;
154       reg->type = 0;
155     }
156   }
157
158         /*
159          * Create some opcodes and types to let firm look a little
160          * bit more like real machines.
161          */
162         if(!op_push) {
163                 int push_opc = get_next_ir_opcode();
164
165                 op_push = new_ir_op(push_opc, "Push",
166                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
167         }
168
169         if(!op_imm) {
170                 int imm_opc = get_next_ir_opcode();
171                 ir_op_ops ops;
172
173                 memset(&ops, 0, sizeof(ops));
174                 ops.dump_node = dump_node_Imm;
175
176                 op_imm = new_ir_op(imm_opc, "Imm",
177                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
178         }
179
180         return isa;
181 }
182
183 static void firm_done(void *self)
184 {
185         free(self);
186 }
187
188 static int firm_get_n_reg_class(const void *self)
189 {
190   return N_CLASSES;
191 }
192
193 static const arch_register_class_t *firm_get_reg_class(const void *self, int i)
194 {
195   assert(i >= 0 && i < N_CLASSES);
196   return &reg_classes[i];
197 }
198
199 static const arch_register_req_t firm_std_reg_req = {
200   arch_register_req_type_normal,
201   &reg_classes[CLS_DATAB],
202   NULL,
203   NULL
204 };
205
206 static const arch_register_req_t *
207 firm_get_irn_reg_req(const void *self,
208     arch_register_req_t *req, const ir_node *irn, int pos)
209 {
210   if(is_firm_be_mode(get_irn_mode(irn)))
211     memcpy(req, &firm_std_reg_req, sizeof(*req));
212   else
213     req = NULL;
214
215   return req;
216 }
217
218 struct irn_reg_assoc {
219   const ir_node *irn;
220   const arch_register_t *reg;
221 };
222
223 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
224 {
225   const struct irn_reg_assoc *x = a;
226   const struct irn_reg_assoc *y = b;
227
228   return x->irn != y->irn;
229 }
230
231 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
232 {
233   static set *reg_set = NULL;
234   struct irn_reg_assoc templ;
235
236   if(!reg_set)
237     reg_set = new_set(cmp_irn_reg_assoc, 1024);
238
239   templ.irn = irn;
240   templ.reg = NULL;
241
242   return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
243 }
244
245 static void firm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
246 {
247   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
248   assoc->reg = reg;
249 }
250
251 static const arch_register_t *firm_get_irn_reg(const void *self, const ir_node *irn)
252 {
253   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
254   return assoc->reg;
255 }
256
257 static arch_irn_class_t firm_classify(const void *self, const ir_node *irn)
258 {
259     arch_irn_class_t res;
260
261     switch(get_irn_opcode(irn)) {
262         case iro_Cond:
263         case iro_Jmp:
264             res = arch_irn_class_branch;
265             break;
266                 case iro_Call:
267                         res = arch_irn_class_call;
268                         break;
269         default:
270             res = arch_irn_class_normal;
271     }
272
273         return res;
274 }
275
276 static arch_irn_flags_t firm_get_flags(const void *self, const ir_node *irn)
277 {
278         arch_irn_flags_t res = 0;
279
280         if(get_irn_op(irn) == op_imm)
281                 res |= arch_irn_flags_rematerializable;
282
283         switch(get_irn_opcode(irn)) {
284                 case iro_Add:
285                 case iro_Sub:
286                 case iro_Shl:
287                 case iro_Shr:
288                 case iro_Shrs:
289                 case iro_And:
290                 case iro_Or:
291                 case iro_Eor:
292                 case iro_Not:
293                         res |= arch_irn_flags_rematerializable;
294                 default:
295                         res = res;
296         }
297
298         return res;
299 }
300
301 static const arch_irn_ops_if_t firm_irn_ops_if = {
302         firm_get_irn_reg_req,
303         firm_set_irn_reg,
304         firm_get_irn_reg,
305         firm_classify,
306         firm_get_flags
307 };
308
309 static const arch_irn_ops_t firm_irn_ops = {
310         &firm_irn_ops_if
311 };
312
313 static const void *firm_get_irn_ops(const arch_irn_handler_t *self,
314         const ir_node *irn)
315 {
316         return &firm_irn_ops;
317 }
318
319 const arch_irn_handler_t firm_irn_handler = {
320         firm_get_irn_ops,
321 };
322
323 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
324 {
325         ir_node *ins[2];
326         ins[0] = push;
327         ins[1] = arg;
328         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
329 }
330
331 /**
332  * Creates an op_Imm node from an op_Const.
333  */
334 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
335   ir_node    *ins[1];
336   ir_node    *res;
337   imm_attr_t *attr;
338
339   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
340   attr = (imm_attr_t *) &res->attr;
341
342   switch (get_irn_opcode(cnst)) {
343     case iro_Const:
344       attr->tp      = imm_Const;
345       attr->data.cnst_attr = get_irn_const_attr(cnst);
346       break;
347     case iro_SymConst:
348       attr->tp             = imm_SymConst;
349       attr->data.symc_attr = get_irn_symconst_attr(cnst);
350       break;
351     case iro_Unknown:
352       break;
353     default:
354       assert(0 && "Cannot create Imm for this opcode");
355   }
356
357   return res;
358 }
359
360 static void prepare_walker(ir_node *irn, void *data)
361 {
362         opcode opc = get_irn_opcode(irn);
363
364         /* A replacement for this node has already been computed. */
365         if(get_irn_link(irn))
366                 return;
367
368         if(opc == iro_Call) {
369                 ir_node *bl   = get_nodes_block(irn);
370                 ir_graph *irg = get_irn_irg(bl);
371
372                 ir_node *store   = get_Call_mem(irn);
373                 ir_node *ptr     = get_Call_ptr(irn);
374                 ir_type *ct      = get_Call_type(irn);
375                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
376
377                 if(np > 0) {
378                         ir_node *ins[1];
379                         char buf[128];
380                         ir_node *nc;
381                         int i, n = get_Call_n_params(irn);
382                         ir_type *nt;
383       unsigned cc = get_method_calling_convention(get_Call_type(irn));
384
385       if (cc & cc_last_on_top) {
386                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
387
388                           for (i = 1; i < n; ++i)
389                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
390       }
391       else {
392         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
393
394         for (i = n - 2; i >= 0; --i)
395           store = new_Push(irg, bl, store, get_Call_param(irn, i));
396       }
397
398                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
399
400                         n = get_method_n_ress(ct);
401                         nt = new_type_method(new_id_from_str(buf), 0, n);
402                         for(i = 0; i < n; ++i)
403                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
404
405                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
406                         exchange(irn, nc);
407                         set_irn_link(nc, nc);
408                 }
409         }
410 }
411
412 static void localize_const_walker(ir_node *irn, void *data)
413 {
414         if(!is_Block(irn)) {
415                 int i, n;
416                 ir_node *bl = get_nodes_block(irn);
417
418                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
419                         ir_node *op    = get_irn_n(irn, i);
420                         opcode opc     = get_irn_opcode(op);
421
422                         if(opc == iro_Const
423                         || opc == iro_Unknown
424                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
425                                 ir_graph *irg   = get_irn_irg(bl);
426                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
427
428                                 ir_node *imm = new_Imm(irg, imm_bl, op);
429                                 set_irn_n(irn, i, imm);
430                         }
431                 }
432         }
433 }
434
435 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
436 {
437         return &firm_irn_handler;
438 }
439
440 typedef struct _firm_code_gen_t {
441         const arch_code_generator_if_t *impl;
442         ir_graph *irg;
443 } firm_code_gen_t;
444
445
446 static void clear_link(ir_node *irn, void *data)
447 {
448         set_irn_link(irn, NULL);
449 }
450
451 static void firm_prepare_graph(void *self)
452 {
453         firm_code_gen_t *cg = self;
454
455         irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
456         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
457 }
458
459 static void firm_before_sched(void *self)
460 {
461 }
462
463 static void imm_scheduler(ir_node *irn, void *env) {
464         if(is_Imm(irn)) {
465                 const ir_edge_t *e;
466                 ir_node *user, *user_block, *before, *tgt_block;
467
468                 if (1 != get_irn_n_edges(irn)) {
469                         printf("Out edges: %d\n", get_irn_n_edges(irn));
470                         assert(1 == get_irn_n_edges(irn));
471                 }
472
473                 e = get_irn_out_edge_first(irn);
474                 user = e->src;
475                 user_block = get_nodes_block(user);
476                 if (is_Phi(user)) {
477                         before = get_Block_cfgpred_block(user_block, e->pos);
478                         tgt_block = before;
479                 } else {
480                         before = user;
481                         tgt_block = user_block;
482                 }
483
484                 sched_remove(irn);
485                 set_nodes_block(irn, tgt_block);
486                 sched_add_before(before, irn);
487         }
488 }
489
490 static void firm_before_ra(void *self)
491 {
492         firm_code_gen_t *cg = self;
493         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
494 }
495
496 static void firm_codegen_done(void *self)
497 {
498         free(self);
499 }
500
501 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env);
502
503 static const arch_code_generator_if_t firm_code_gen_if = {
504         firm_cg_init,
505         firm_prepare_graph,
506         firm_before_sched,
507         firm_before_ra,
508         NULL,  /* lower spill */
509         NULL,  /* lower reload */
510         firm_codegen_done
511 };
512
513 static void *firm_cg_init(FILE *file_handle, ir_graph *irg, const arch_env_t *env)
514 {
515         firm_code_gen_t *cg = xmalloc(sizeof(*cg));
516         cg->impl = &firm_code_gen_if;
517         cg->irg  = irg;
518         return cg;
519 }
520
521
522 static const arch_code_generator_if_t *firm_get_code_generator_if(void *self)
523 {
524         return &firm_code_gen_if;
525 }
526
527 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
528         return trivial_selector;
529 }
530
531 #ifdef WITH_LIBCORE
532 static void firm_register_options(lc_opt_entry_t *ent)
533 {
534 }
535 #endif
536
537 const arch_isa_if_t firm_isa = {
538 #ifdef WITH_LIBCORE
539         firm_register_options,
540 #endif
541         firm_init,
542         firm_done,
543         firm_get_n_reg_class,
544         firm_get_reg_class,
545         firm_get_irn_handler,
546         firm_get_code_generator_if,
547         firm_get_list_sched_selector
548 };