Added path constraints for phi classes, some ifdef switches, removed old stuff
[libfirm] / ir / be / 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 8
25
26 static arch_register_t datab_regs[N_REGS];
27
28 static arch_register_class_t reg_classes[] = {
29   { "datab", N_REGS, datab_regs },
30 };
31
32 static ir_op *op_push;
33 static ir_op *op_imm;
34
35 typedef struct {
36         enum { imm_Const, imm_SymConst } tp;
37         union {
38                 tarval *tv;
39                 entity *ent;
40         } data;
41 } imm_attr_t;
42
43 #define N_CLASSES \
44   (sizeof(reg_classes) / sizeof(reg_classes[0]))
45
46 #define CLS_DATAB 0
47
48 static void firm_init(void)
49 {
50   static struct obstack obst;
51   static int inited = 0;
52   int k;
53
54   if(inited)
55     return;
56
57   inited = 1;
58   obstack_init(&obst);
59
60   for(k = 0; k < N_CLASSES; ++k) {
61     const arch_register_class_t *cls = &reg_classes[k];
62     int i;
63
64     for(i = 0; i < cls->n_regs; ++i) {
65       int n;
66       char buf[8];
67       char *name;
68       arch_register_t *reg = (arch_register_t *) &cls->regs[i];
69
70       n = snprintf(buf, sizeof(buf), "r%d", i);
71       name = obstack_copy0(&obst, buf, n);
72
73       reg->name = name;
74       reg->reg_class = cls;
75       reg->index = i;
76       reg->type = 0;
77     }
78   }
79
80         /*
81          * Create some opcodes and types to let firm look a little
82          * bit more like real machines.
83          */
84         if(!op_push) {
85                 rflct_sig_t *sig;
86                 int push_opc = get_next_ir_opcode();
87
88                 op_push = new_ir_op(push_opc, "Push",
89                                 op_pin_state_pinned, 0, oparity_binary, 0, 0);
90
91                 sig = rflct_signature_allocate(1, 3);
92                 rflct_signature_set_arg(sig, 0, 0, "Store", RFLCT_MC(Mem), 0, 0);
93                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
94                 rflct_signature_set_arg(sig, 1, 1, "Store", RFLCT_MC(Mem), 0, 0);
95                 rflct_signature_set_arg(sig, 1, 2, "Arg", RFLCT_MC(Datab), 0, 0);
96
97                 rflct_new_opcode(push_opc, "Push", false);
98                 rflct_opcode_add_signature(push_opc, sig);
99         }
100
101         if(!op_imm) {
102                 rflct_sig_t *sig;
103                 int imm_opc = get_next_ir_opcode();
104
105                 op_imm = new_ir_op(imm_opc, "Imm",
106                                 op_pin_state_pinned, 0, oparity_zero, 0, sizeof(imm_attr_t));
107
108                 sig = rflct_signature_allocate(1, 1);
109                 rflct_signature_set_arg(sig, 0, 0, "Imm", RFLCT_MC(Data), 0, 0);
110                 rflct_signature_set_arg(sig, 1, 0, "Block", RFLCT_MC(BB), 0, 0);
111                 rflct_new_opcode(imm_opc, "Imm", false);
112                 rflct_opcode_add_signature(imm_opc, sig);
113         }
114 }
115
116 static int firm_get_n_reg_class(void)
117 {
118   return N_CLASSES;
119 }
120
121 static const arch_register_class_t *firm_get_reg_class(int i)
122 {
123   assert(i >= 0 && i < N_CLASSES);
124   return &reg_classes[i];
125 }
126
127 static const arch_register_req_t firm_std_reg_req = {
128   arch_register_req_type_normal,
129   &reg_classes[CLS_DATAB],
130   { NULL }
131 };
132
133 static const rflct_arg_t *get_arg(const ir_node *irn, int pos)
134 {
135   int sig = rflct_get_signature(irn);
136   const rflct_arg_t *args =
137     rflct_get_args(get_irn_opcode(irn), sig, arch_pos_is_in(pos));
138   return &args[arch_pos_get_index(pos)];
139 }
140
141 static const arch_register_req_t *
142 firm_get_irn_reg_req(const arch_irn_ops_t *self,
143     arch_register_req_t *req, const ir_node *irn, int pos)
144 {
145   if(is_firm_be_mode(get_irn_mode(irn)))
146     memcpy(req, &firm_std_reg_req, sizeof(*req));
147   else
148     req = NULL;
149
150   return req;
151 }
152
153 static int firm_get_n_operands(const arch_irn_ops_t *self, const ir_node *irn, int in_out)
154 {
155   int sig;
156
157         while(is_Proj(irn))
158                 irn = get_Proj_pred(irn);
159
160         sig = rflct_get_signature(irn);
161   return rflct_get_args_count(get_irn_opcode(irn), sig, in_out >= 0);
162 }
163
164 struct irn_reg_assoc {
165   const ir_node *irn;
166   int pos;
167   const arch_register_t *reg;
168 };
169
170 static int cmp_irn_reg_assoc(const void *a, const void *b, size_t len)
171 {
172   const struct irn_reg_assoc *x = a;
173   const struct irn_reg_assoc *y = b;
174
175   return !(x->irn == y->irn && x->pos == y->pos);
176 }
177
178 static struct irn_reg_assoc *get_irn_reg_assoc(const ir_node *irn, int pos)
179 {
180   static set *reg_set = NULL;
181   struct irn_reg_assoc templ;
182   unsigned int hash;
183
184   if(!reg_set)
185     reg_set = new_set(cmp_irn_reg_assoc, 1024);
186
187   templ.irn = irn;
188   templ.pos = pos;
189   templ.reg = NULL;
190   hash = HASH_PTR(irn) + 7 * pos;
191
192   return set_insert(reg_set, &templ, sizeof(templ), hash);
193 }
194
195 static void firm_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn,
196     int pos, const arch_register_t *reg)
197 {
198   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
199   assoc->reg = reg;
200 }
201
202 static const arch_register_t *firm_get_irn_reg(const arch_irn_ops_t *self,
203     const ir_node *irn, int pos)
204 {
205   struct irn_reg_assoc *assoc = get_irn_reg_assoc(irn, pos);
206   return assoc->reg;
207 }
208
209 static arch_irn_class_t firm_classify(const arch_irn_ops_t *self, const ir_node *irn)
210 {
211     arch_irn_class_t res;
212
213     switch(get_irn_opcode(irn)) {
214         case iro_Cond:
215         case iro_Jmp:
216             res = arch_irn_class_branch;
217             break;
218         default:
219             res = arch_irn_class_normal;
220     }
221
222         return res;
223 }
224
225 static arch_irn_flags_t firm_get_flags(const arch_irn_ops_t *self, const ir_node *irn)
226 {
227         arch_irn_flags_t res = arch_irn_flags_spillable;
228
229         if(get_irn_op(irn) == op_imm)
230                 res |= arch_irn_flags_rematerializable;
231
232         switch(get_irn_opcode(irn)) {
233                 case iro_Add:
234                 case iro_Sub:
235                 case iro_Shl:
236                 case iro_Shr:
237                 case iro_Shrs:
238                 case iro_And:
239                 case iro_Or:
240                 case iro_Eor:
241                 case iro_Not:
242                         res |= arch_irn_flags_rematerializable;
243                 default:
244                         res = res;
245         }
246
247         return res;
248 }
249
250 static const arch_irn_ops_t irn_ops = {
251   firm_get_irn_reg_req,
252   firm_get_n_operands,
253   firm_set_irn_reg,
254   firm_get_irn_reg,
255   firm_classify,
256         firm_get_flags
257 };
258
259 static const arch_irn_ops_t *firm_get_irn_ops(const arch_irn_handler_t *self,
260     const ir_node *irn)
261 {
262   return &irn_ops;
263 }
264
265 const arch_irn_handler_t firm_irn_handler = {
266   firm_get_irn_ops,
267 };
268
269 static ir_node *new_Push(ir_graph *irg, ir_node *bl, ir_node *push, ir_node *arg)
270 {
271         ir_node *ins[2];
272         ins[0] = push;
273         ins[1] = arg;
274         return new_ir_node(NULL, irg, bl, op_push, mode_M, 2, ins);
275 }
276
277 static ir_node *new_Imm(ir_graph *irg, ir_node *bl, ir_node *cnst)
278 {
279         ir_node *ins[1];
280         ir_node *res;
281         imm_attr_t *attr;
282
283         res = new_ir_node(NULL, irg, bl, op_imm, get_irn_mode(cnst), 0, ins);
284         attr = (imm_attr_t *) &res->attr;
285
286         if(get_irn_opcode(cnst) == iro_SymConst) {
287                 attr->tp = imm_SymConst;
288                 attr->data.ent = get_SymConst_entity(cnst);
289         }
290
291         else {
292                 attr->tp = imm_Const;
293                 attr->data.tv = get_Const_tarval(cnst);
294         }
295
296         return res;
297 }
298
299 static void prepare_walker(ir_node *irn, void *data)
300 {
301         opcode opc = get_irn_opcode(irn);
302
303         /* A replacement for this node has already been computed. */
304         if(get_irn_link(irn))
305                 return;
306
307         if(opc == iro_Call) {
308                 ir_node *bl   = get_nodes_block(irn);
309                 ir_graph *irg = get_irn_irg(bl);
310
311                 ir_node *store   = get_Call_mem(irn);
312                 ir_node *ptr     = get_Call_ptr(irn);
313                 type *ct         = get_Call_type(irn);
314                 int np           = get_Call_n_params(irn) > 0 ? 1 : 0;
315
316                 if(np > 0) {
317                         ir_node *ins[1];
318                         char buf[128];
319                         ir_node *nc;
320                         ir_node *push;
321                         int i, n;
322                         type *nt;
323
324                         store = new_Push(irg, bl, store, get_Call_param(irn, 0));
325
326                         for(i = 1, n = get_Call_n_params(irn); i < n; ++i) {
327                                 store = new_Push(irg, bl, store, get_Call_param(irn, i));
328                         }
329
330                         snprintf(buf, sizeof(buf), "push_%s", get_type_name(ct));
331
332                         n = get_method_n_ress(ct);
333                         nt = new_type_method(new_id_from_str(buf), 0, n);
334                         for(i = 0; i < n; ++i)
335                                 set_method_res_type(nt, i, get_method_res_type(ct, i));
336
337                         nc = new_r_Call(irg, bl, store, ptr, 0, ins, nt);
338                         exchange(irn, nc);
339                         set_irn_link(nc, nc);
340                 }
341         }
342 }
343
344 static void localize_const_walker(ir_node *irn, void *data)
345 {
346         if(!is_Block(irn)) {
347                 int i, n;
348                 ir_node *bl = get_nodes_block(irn);
349
350                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
351                         ir_node *op    = get_irn_n(irn, i);
352                         opcode opc     = get_irn_opcode(op);
353
354                         if(opc == iro_Const
355                         || (opc == iro_SymConst && get_SymConst_kind(op) == symconst_addr_ent)) {
356                                 ir_graph *irg   = get_irn_irg(bl);
357                                 ir_node *imm_bl = is_Phi(irn) ? get_Block_cfgpred_block(bl, i) : bl;
358
359                                 ir_node *imm = new_Imm(irg, imm_bl, op);
360                                 set_irn_n(irn, i, imm);
361                         }
362                 }
363         }
364 }
365
366 static void clear_link(ir_node *irn, void *data)
367 {
368         set_irn_link(irn, NULL);
369 }
370
371 static void firm_prepare_graph(ir_graph *irg)
372 {
373         irg_walk_graph(irg, clear_link, localize_const_walker, NULL);
374         irg_walk_graph(irg, NULL, prepare_walker, NULL);
375 }
376
377 const arch_isa_if_t firm_isa = {
378   firm_init,
379   firm_get_n_reg_class,
380   firm_get_reg_class,
381         firm_prepare_graph
382 };