Made everything really kaputt
[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 };
204
205 static const arch_register_req_t *
206 firm_get_irn_reg_req(const arch_irn_ops_t *self,
207     arch_register_req_t *req, const ir_node *irn, int pos)
208 {
209   if(is_firm_be_mode(get_irn_mode(irn)))
210     memcpy(req, &firm_std_reg_req, sizeof(*req));
211   else
212     req = NULL;
213
214   return req;
215 }
216
217 struct irn_reg_assoc {
218   const ir_node *irn;
219   const arch_register_t *reg;
220 };
221
222 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
223 {
224   const struct irn_reg_assoc *x = a;
225   const struct irn_reg_assoc *y = b;
226
227   return x->irn != y->irn;
228 }
229
230 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn)
231 {
232   static set *reg_set = NULL;
233   struct irn_reg_assoc templ;
234
235   if(!reg_set)
236     reg_set = new_set(cmp_irn_reg_assoc, 1024);
237
238   templ.irn = irn;
239   templ.reg = NULL;
240
241   return set_insert(reg_set, &templ, sizeof(templ), HASH_PTR(irn));
242 }
243
244 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg)
245 {
246   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
247   assoc->reg = reg;
248 }
249
250 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self, const ir_node *irn)
251 {
252   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn);
253   return assoc->reg;
254 }
255
256 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
257 {
258     arch_irn_class_t res;
259
260     switch(get_irn_opcode(irn)) {
261         case iro_Cond:
262         case iro_Jmp:
263             res = arch_irn_class_branch;
264             break;
265         default:
266             res = arch_irn_class_normal;
267     }
268
269         return res;
270 }
271
272 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
273 {
274         arch_irn_flags_t res = arch_irn_flags_spillable;
275
276         if(get_irn_op(irn) == op_imm)
277                 res |= arch_irn_flags_rematerializable;
278
279         switch(get_irn_opcode(irn)) {
280                 case iro_Add:
281                 case iro_Sub:
282                 case iro_Shl:
283                 case iro_Shr:
284                 case iro_Shrs:
285                 case iro_And:
286                 case iro_Or:
287                 case iro_Eor:
288                 case iro_Not:
289                         res |= arch_irn_flags_rematerializable;
290                 default:
291                         res = res;
292         }
293
294         return res;
295 }
296
297 static const arch_irn_ops_t irn_ops = {
298   firm_get_irn_reg_req,
299   firm_set_irn_reg,
300   firm_get_irn_reg,
301   firm_classify,
302         firm_get_flags
303 };
304
305 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
306     const ir_node *irn)
307 {
308   return &irn_ops;
309 }
310
311 const arch_irn_handler_t firm_irn_handler = {
312   firm_get_irn_ops,
313 };
314
315 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
316 {
317         ir_node *ins[2];
318         ins[0] = push;
319         ins[1] = arg;
320         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
321 }
322
323 /**
324  * Creates an op_Imm node from an op_Const.
325  */
326 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
327   ir_node    *ins[1];
328   ir_node    *res;
329   imm_attr_t *attr;
330
331   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
332   attr = (imm_attr_t *) &res->attr;
333
334   switch (get_irn_opcode(cnst)) {
335     case iro_Const:
336       attr->tp      = imm_Const;
337       attr->data.cnst_attr = get_irn_const_attr(cnst);
338       break;
339     case iro_SymConst:
340       attr->tp             = imm_SymConst;
341       attr->data.symc_attr = get_irn_symconst_attr(cnst);
342       break;
343     case iro_Unknown:
344       break;
345     default:
346       assert(0 && "Cannot create Imm for this opcode");
347   }
348
349   return res;
350 }
351
352 static void prepare_walker(ir_node *irn, void *data)
353 {
354         opcode opc = get_irn_opcode(irn);
355
356         /* A replacement for this node has already been computed. */
357         if(get_irn_link(irn))
358                 return;
359
360         if(opc == iro_Call) {
361                 ir_node *bl   = get_nodes_block(irn);
362                 ir_graph *irg = get_irn_irg(bl);
363
364                 ir_node *store   = get_Call_mem(irn);
365                 ir_node *ptr     = get_Call_ptr(irn);
366                 type *ct         = get_Call_type(irn);
367                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
368
369                 if(np > 0) {
370                         ir_node *ins[1];
371                         char buf[128];
372                         ir_node *nc;
373                         int i, n = get_Call_n_params(irn);
374                         type *nt;
375       unsigned cc = get_method_calling_convention(get_Call_type(irn));
376
377       if (cc & cc_last_on_top) {
378                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
379
380                           for (i = 1; i < n; ++i)
381                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
382       }
383       else {
384         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
385
386         for (i = n - 2; i >= 0; --i)
387           store = new_Push(irg, bl, store, get_Call_param(irn, i));
388       }
389
390                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
391
392                         n = get_method_n_ress(ct);
393                         nt = new_type_method(new_id_from_str(buf), 0, n);
394                         for(i = 0; i < n; ++i)
395                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
396
397                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
398                         exchange(irn, nc);
399                         set_irn_link(nc, nc);
400                 }
401         }
402 }
403
404 static void localize_const_walker(ir_node *irn, void *data)
405 {
406         if(!is_Block(irn)) {
407                 int i, n;
408                 ir_node *bl = get_nodes_block(irn);
409
410                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
411                         ir_node *op    = get_irn_n(irn, i);
412                         opcode opc     = get_irn_opcode(op);
413
414                         if(opc == iro_Const
415                         || opc == iro_Unknown
416                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
417                                 ir_graph *irg   = get_irn_irg(bl);
418                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
419
420                                 ir_node *imm = new_Imm(irg, imm_bl, op);
421                                 set_irn_n(irn, i, imm);
422                         }
423                 }
424         }
425 }
426
427 static const arch_irn_handler_t *firm_get_irn_handler(const void *self)
428 {
429         return &firm_irn_handler;
430 }
431
432 typedef struct _firm_code_gen_t {
433         const arch_code_generator_if_t *impl;
434         ir_graph *irg;
435 } firm_code_gen_t;
436
437
438 static void clear_link(ir_node *irn, void *data)
439 {
440         set_irn_link(irn, NULL);
441 }
442
443 static void firm_prepare_graph(void *self)
444 {
445         firm_code_gen_t *cg = self;
446
447         irg_walk_graph(cg->irg, clear_link, localize_const_walker, NULL);
448         irg_walk_graph(cg->irg, NULL, prepare_walker, NULL);
449 }
450
451 static void firm_before_sched(void *self)
452 {
453 }
454
455 static void imm_scheduler(ir_node *irn, void *env) {
456         if(is_Imm(irn)) {
457                 const ir_edge_t *e;
458                 ir_node *user, *user_block, *before, *tgt_block;
459
460                 if (1 != get_irn_n_edges(irn)) {
461                         printf("Out edges: %d\n", get_irn_n_edges(irn));
462                         assert(1 == get_irn_n_edges(irn));
463                 }
464
465                 e = get_irn_out_edge_first(irn);
466                 user = e->src;
467                 user_block = get_nodes_block(user);
468                 if (is_Phi(user)) {
469                         before = get_Block_cfgpred_block(user_block, e->pos);
470                         tgt_block = before;
471                 } else {
472                         before = user;
473                         tgt_block = user_block;
474                 }
475
476                 sched_remove(irn);
477                 set_nodes_block(irn, tgt_block);
478                 sched_add_before(before, irn);
479         }
480 }
481
482 static void firm_before_ra(void *self)
483 {
484         firm_code_gen_t *cg = self;
485         irg_walk_graph(cg->irg, imm_scheduler, NULL, NULL);
486 }
487
488 static void firm_codegen_done(void *self)
489 {
490         free(self);
491 }
492
493 static const arch_code_generator_if_t firm_code_gen = {
494         firm_prepare_graph,
495         firm_before_sched,
496         firm_before_ra,
497         firm_codegen_done
498 };
499
500 static arch_code_generator_t *firm_make_code_generator(void *self, ir_graph *irg)
501 {
502         firm_code_gen_t *cg = malloc(sizeof(*cg));
503         cg->impl = &firm_code_gen;
504         cg->irg  = irg;
505         return (arch_code_generator_t *) cg;
506 }
507
508
509 static const list_sched_selector_t *firm_get_list_sched_selector(const void *self) {
510         return trivial_selector;
511 }
512
513 #ifdef WITH_LIBCORE
514 static void firm_register_options(lc_opt_entry_t *ent)
515 {
516 }
517 #endif
518
519 const arch_isa_if_t firm_isa = {
520 #ifdef WITH_LIBCORE
521         firm_register_options,
522 #endif
523         firm_init,
524         firm_done,
525         firm_get_n_reg_class,
526         firm_get_reg_class,
527         firm_get_irn_handler,
528         firm_make_code_generator,
529         firm_get_list_sched_selector
530 };