removed broken include
[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 #include "bitset.h"
10 #include "obst.h"
11
12 #include "irmode_t.h"
13 #include "irnode_t.h"
14 #include "irgmod.h"
15 #include "ircons_t.h"
16 #include "irgwalk.h"
17 #include "type.h"
18
19 #include "../bearch.h"
20 #include "../beutil.h"
21
22 #include "irreflect.h"
23
24 #define N_REGS 3
25
26 typedef struct {
27   enum  { imm_Const, imm_SymConst } tp;
28   union {
29     tarval  *tv;
30     ir_node *symconst;
31   } data;
32 } imm_attr_t;
33
34 static arch_register_t datab_regs[N_REGS];
35
36 static arch_register_class_t reg_classes[] = {
37   { "datab", N_REGS, datab_regs },
38 };
39
40 static ir_op *op_push;
41 static ir_op *op_imm;
42
43 #define N_CLASSES \
44   (sizeof(reg_classes) / sizeof(reg_classes[0]))
45
46 #define CLS_DATAB 0
47
48 tarval *get_Imm_tv(ir_node *n) {
49   imm_attr_t *attr = (imm_attr_t *)get_irn_generic_attr(n);
50   return attr->data.tv;
51 }
52
53 int is_Imm(const ir_node *irn) {
54   return get_irn_op(irn) == op_imm;
55 }
56
57 static int dump_node_Imm(ir_node *n, FILE *F, dump_reason_t reason) {
58   ir_mode    *mode;
59   int        bad = 0;
60   char       buf[1024];
61   tarval     *tv;
62   imm_attr_t *attr;
63
64   switch (reason) {
65     case dump_node_opcode_txt:
66       tv = get_Imm_tv(n);
67
68       if (tv) {
69         tarval_snprintf(buf, sizeof(buf), tv);
70         fprintf(F, "%s", buf);
71       }
72       else {
73         fprintf(F, "immSymC");
74       }
75       break;
76
77     case dump_node_mode_txt:
78       mode = get_irn_mode(n);
79
80       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD && mode != mode_T) {
81         fprintf(F, "[%s]", get_mode_name(mode));
82       }
83       break;
84
85     case dump_node_nodeattr_txt:
86       attr = (imm_attr_t *)get_irn_generic_attr(n);
87
88       if (is_Imm(n) && attr->tp == imm_SymConst) {
89         const char *name    = NULL;
90         ir_node    *old_sym = attr->data.symconst;
91
92         switch (get_SymConst_kind(old_sym)) {
93           case symconst_addr_name:
94             name = get_id_str(get_SymConst_name(old_sym));
95             break;
96
97           case symconst_addr_ent:
98             name = get_entity_ld_name(get_SymConst_entity(old_sym));
99             break;
100
101           default:
102             assert(!"Unsupported SymConst");
103         }
104
105         fprintf(F, "&%s ", name);
106       }
107
108       break;
109
110     case dump_node_info_txt:
111       break;
112   }
113
114   return bad;
115 }
116
117 static void firm_init(void)
118 {
119   static struct obstack obst;
120   static int inited = 0;
121   int k;
122
123   if(inited)
124     return;
125
126   inited = 1;
127   obstack_init(&obst);
128
129   for(k = 0; k < N_CLASSES; ++k) {
130     const arch_register_class_t *cls = &reg_classes[k];
131     int i;
132
133     for(i = 0; i < cls->n_regs; ++i) {
134       int n;
135       char buf[8];
136       char *name;
137       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
138
139       n = snprintf(buf, sizeof(buf), "r%d", i);
140       name = obstack_copy0(&obst, buf, n);
141
142       reg->name = name;
143       reg->reg_class = cls;
144       reg->index = i;
145       reg->type = 0;
146     }
147   }
148
149         /*
150          * Create some opcodes and types to let firm look a little
151          * bit more like real machines.
152          */
153         if(!op_push) {
154                 rflct_sig_t *sig;
155                 int push_opc = get_next_ir_opcode();
156
157                 op_push = new_ir_op(push_opc, "Push",
158                                 op_pin_state_pinned, 0, oparity_binary, 0, 0, NULL);
159
160                 sig = rflct_signature_allocate(1, 3);
161                 rflct_signature_set_arg(sig, 0, 0, "Store", RFLCT_MC(Mem), 0, 0);
162                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
163                 rflct_signature_set_arg(sig, 1, 1, "Store", RFLCT_MC(Mem), 0, 0);
164                 rflct_signature_set_arg(sig, 1, 2, "Arg", RFLCT_MC(Datab), 0, 0);
165
166                 rflct_new_opcode(push_opc, "Push", 0);
167                 rflct_opcode_add_signature(push_opc, sig);
168         }
169
170         if(!op_imm) {
171                 rflct_sig_t *sig;
172                 int imm_opc = get_next_ir_opcode();
173                 ir_op_ops ops;
174
175                 memset(&ops, 0, sizeof(ops));
176                 ops.dump_node = dump_node_Imm;
177
178                 op_imm = new_ir_op(imm_opc, "Imm",
179                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t), &ops);
180
181                 sig = rflct_signature_allocate(1, 1);
182                 rflct_signature_set_arg(sig, 0, 0, "Imm", RFLCT_MC(Data), 0, 0);
183                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
184                 rflct_new_opcode(imm_opc, "Imm", 0);
185                 rflct_opcode_add_signature(imm_opc, sig);
186         }
187 }
188
189 static int firm_get_n_reg_class(void)
190 {
191   return N_CLASSES;
192 }
193
194 static const arch_register_class_t *firm_get_reg_class(int i)
195 {
196   assert(i >= 0 && i < N_CLASSES);
197   return &reg_classes[i];
198 }
199
200 static const arch_register_req_t firm_std_reg_req = {
201   arch_register_req_type_normal,
202   &reg_classes[CLS_DATAB],
203   { NULL }
204 };
205
206 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
207 {
208   int sig = rflct_get_signature(irn);
209   const rflct_arg_t *args =
210     rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
211   return &args[arch_pos_get_index(pos)];
212 }
213
214 static const arch_register_req_t *
215 firm_get_irn_reg_req(const arch_irn_ops_t *self,
216     arch_register_req_t *req, const ir_node *irn, int pos)
217 {
218   if(is_firm_be_mode(get_irn_mode(irn)))
219     memcpy(req, &firm_std_reg_req, sizeof(*req));
220   else
221     req = NULL;
222
223   return req;
224 }
225
226 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
227 {
228   int sig;
229
230         while(is_Proj(irn))
231                 irn = get_Proj_pred(irn);
232
233         sig = rflct_get_signature(irn);
234   return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
235 }
236
237 struct irn_reg_assoc {
238   const ir_node *irn;
239   int pos;
240   const arch_register_t *reg;
241 };
242
243 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
244 {
245   const struct irn_reg_assoc *x = a;
246   const struct irn_reg_assoc *y = b;
247
248   return !(x->irn == y->irn && x->pos == y->pos);
249 }
250
251 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
252 {
253   static set *reg_set = NULL;
254   struct irn_reg_assoc templ;
255   unsigned int hash;
256
257   if(!reg_set)
258     reg_set = new_set(cmp_irn_reg_assoc, 1024);
259
260   templ.irn = irn;
261   templ.pos = pos;
262   templ.reg = NULL;
263   hash = HASH_PTR(irn) + 7 * pos;
264
265   return set_insert(reg_set, &templ, sizeof(templ), hash);
266 }
267
268 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
269     int pos, const arch_register_t *reg)
270 {
271   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
272   assoc->reg = reg;
273 }
274
275 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
276     const ir_node *irn, int pos)
277 {
278   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
279   return assoc->reg;
280 }
281
282 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
283 {
284     arch_irn_class_t res;
285
286     switch(get_irn_opcode(irn)) {
287         case iro_Cond:
288         case iro_Jmp:
289             res = arch_irn_class_branch;
290             break;
291         default:
292             res = arch_irn_class_normal;
293     }
294
295         return res;
296 }
297
298 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
299 {
300         arch_irn_flags_t res = arch_irn_flags_spillable;
301
302         if(get_irn_op(irn) == op_imm)
303                 res |= arch_irn_flags_rematerializable;
304
305         switch(get_irn_opcode(irn)) {
306                 case iro_Add:
307                 case iro_Sub:
308                 case iro_Shl:
309                 case iro_Shr:
310                 case iro_Shrs:
311                 case iro_And:
312                 case iro_Or:
313                 case iro_Eor:
314                 case iro_Not:
315                         res |= arch_irn_flags_rematerializable;
316                 default:
317                         res = res;
318         }
319
320         return res;
321 }
322
323 static const arch_irn_ops_t irn_ops = {
324   firm_get_irn_reg_req,
325   firm_get_n_operands,
326   firm_set_irn_reg,
327   firm_get_irn_reg,
328   firm_classify,
329         firm_get_flags
330 };
331
332 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
333     const ir_node *irn)
334 {
335   return &irn_ops;
336 }
337
338 const arch_irn_handler_t firm_irn_handler = {
339   firm_get_irn_ops,
340 };
341
342 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
343 {
344         ir_node *ins[2];
345         ins[0] = push;
346         ins[1] = arg;
347         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
348 }
349
350 /**
351  * Creates an op_Imm node from an op_Const.
352  */
353 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst) {
354   ir_node    *ins[1];
355   ir_node    *res;
356   imm_attr_t *attr;
357
358   res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
359   attr = (imm_attr_t *) &res->attr;
360
361   switch (get_irn_opcode(cnst)) {
362     case iro_Const:
363       attr->tp      = imm_Const;
364       attr->data.tv = get_Const_tarval(cnst);
365       break;
366     case iro_SymConst:
367       attr->tp            = imm_SymConst;
368       attr->data.symconst = cnst;
369       break;
370     case iro_Unknown:
371       break;
372     default:
373       assert(0 && "Cannot create Imm for this opcode");
374   }
375
376   return res;
377 }
378
379 static void prepare_walker(ir_node *irn, void *data)
380 {
381         opcode opc = get_irn_opcode(irn);
382
383         /* A replacement for this node has already been computed. */
384         if(get_irn_link(irn))
385                 return;
386
387         if(opc == iro_Call) {
388                 ir_node *bl   = get_nodes_block(irn);
389                 ir_graph *irg = get_irn_irg(bl);
390
391                 ir_node *store   = get_Call_mem(irn);
392                 ir_node *ptr     = get_Call_ptr(irn);
393                 type *ct         = get_Call_type(irn);
394                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
395
396                 if(np > 0) {
397                         ir_node *ins[1];
398                         char buf[128];
399                         ir_node *nc;
400                         ir_node *push;
401                         int i, n = get_Call_n_params(irn);
402                         type *nt;
403       unsigned cc = get_method_calling_convention(get_Call_type(irn));
404
405       if (cc & cc_last_on_top) {
406                           store = new_Push(irg, bl, store, get_Call_param(irn, 0));
407
408                           for (i = 1; i < n; ++i)
409                                   store = new_Push(irg, bl, store, get_Call_param(irn, i));
410       }
411       else {
412         store = new_Push(irg, bl, store, get_Call_param(irn, n - 1));
413
414         for (i = n - 2; i >= 0; --i)
415           store = new_Push(irg, bl, store, get_Call_param(irn, i));
416       }
417
418                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
419
420                         n = get_method_n_ress(ct);
421                         nt = new_type_method(new_id_from_str(buf), 0, n);
422                         for(i = 0; i < n; ++i)
423                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
424
425                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
426                         exchange(irn, nc);
427                         set_irn_link(nc, nc);
428                 }
429         }
430 }
431
432 static void localize_const_walker(ir_node *irn, void *data)
433 {
434         if(!is_Block(irn)) {
435                 int i, n;
436                 ir_node *bl = get_nodes_block(irn);
437
438                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
439                         ir_node *op    = get_irn_n(irn, i);
440                         opcode opc     = get_irn_opcode(op);
441
442                         if(opc == iro_Const
443                         || opc == iro_Unknown
444                         || (opc == iro_SymConst /*&& get_SymConst_kind(op) == symconst_addr_ent*/)) {
445                                 ir_graph *irg   = get_irn_irg(bl);
446                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
447
448                                 ir_node *imm = new_Imm(irg, imm_bl, op);
449                                 set_irn_n(irn, i, imm);
450                         }
451                 }
452         }
453 }
454
455 static void clear_link(ir_node *irn, void *data)
456 {
457         set_irn_link(irn, NULL);
458 }
459
460 static void firm_prepare_graph(ir_graph *irg)
461 {
462         irg_walk_graph(irg, clear_link, localize_const_walker, NULL);
463         irg_walk_graph(irg, NULL, prepare_walker, NULL);
464 }
465
466 const arch_isa_if_t firm_isa = {
467         firm_init,
468         firm_get_n_reg_class,
469         firm_get_reg_class,
470         firm_prepare_graph,
471         &firm_irn_handler,
472         NULL
473 };