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