473664d8cab5b8b03672e42be4dfc606df5bdae3
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "pseudo_irg.h"
6 #include "irgwalk.h"
7 #include "irprog.h"
8 #include "irprintf.h"
9
10 #include "bitset.h"
11 #include "debug.h"
12
13 #include <obstack.h>
14
15 #ifdef obstack_chunk_alloc
16 # undef obstack_chunk_alloc
17 # define obstack_chunk_alloc malloc
18 #else
19 # define obstack_chunk_alloc malloc
20 # define obstack_chunk_free free
21 #endif
22
23 #include "../bearch.h"                /* the general register allocator interface */
24 #include "bearch_ia32_t.h"
25
26 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
27 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
28 #include "ia32_gen_decls.h"           /* interface declaration emitter */
29 #include "ia32_transform.h"
30 #include "ia32_emitter.h"
31 #include "ia32_map_regs.h"
32
33 #define DEBUG_MODULE "ir.be.isa.ia32"
34
35 /* TODO: ugly */
36 static set *cur_reg_set = NULL;
37
38
39
40 /**************************************************
41  *                         _ _              _  __
42  *                        | | |            (_)/ _|
43  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
44  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
45  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
46  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
47  *            __/ |
48  *           |___/
49  **************************************************/
50
51 static ir_node *my_skip_proj(const ir_node *n) {
52         while (is_Proj(n))
53                 n = get_Proj_pred(n);
54         return (ir_node *)n;
55 }
56
57 /**
58  * Return register requirements for an ia32 node.
59  * If the node returns a tuple (mode_T) then the proj's
60  * will be asked for this information.
61  */
62 static const arch_register_req_t *ia32_get_irn_reg_req(const arch_irn_ops_t *self, arch_register_req_t *req, const ir_node *irn, int pos) {
63         const ia32_register_req_t *irn_req;
64         long                       node_pos = pos == -1 ? 0 : pos;
65         ir_mode                   *mode     = get_irn_mode(irn);
66         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
67
68         if (mode == mode_T || mode == mode_M) {
69                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
70                 return NULL;
71         }
72
73         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
74
75         if (is_Proj(irn)) {
76                 if (pos == -1) {
77                         node_pos = translate_proj_pos(irn);
78                 }
79                 else {
80                         node_pos = pos;
81                 }
82
83                 irn = my_skip_proj(irn);
84
85                 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
86         }
87
88         if (is_ia32_irn(irn)) {
89                 if (pos >= 0) {
90                         irn_req = get_ia32_in_req(irn, pos);
91                 }
92                 else {
93                         irn_req = get_ia32_out_req(irn, node_pos);
94                 }
95
96                 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
97
98                 memcpy(req, &(irn_req->req), sizeof(*req));
99
100                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
101                         arch_register_req_is(&(irn_req->req), should_be_different)) {
102                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
103                         req->other = get_irn_n(irn, irn_req->pos);
104                 }
105
106                 return req;
107         }
108         else {
109                 /* treat Phi like Const with default requirements */
110                 if (is_Phi(irn)) {
111                         DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
112                         if (mode_is_float(mode))
113                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
114                         else if (mode_is_int(mode) || mode_is_reference(mode))
115                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
116                         else if (mode == mode_T || mode == mode_M) {
117                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
118                                 return NULL;
119                         }
120                         else
121                                 assert(0 && "unsupported Phi-Mode");
122                 }
123                 else if (get_irn_op(irn) == op_Start) {
124                         DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
125                         switch (node_pos) {
126                                 case pn_Start_X_initial_exec:
127                                 case pn_Start_P_value_arg_base:
128                                 case pn_Start_P_globals:
129                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
130                                         break;
131                                 case pn_Start_P_frame_base:
132                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
133                                         break;
134                                 case pn_Start_T_args:
135                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
136                         }
137                 }
138                 else if (get_irn_op(irn) == op_Return && pos > 0) {
139                         DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
140                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
141                 }
142                 else {
143                         DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
144                         req = NULL;
145                 }
146         }
147
148         return req;
149 }
150
151 static void ia32_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg) {
152         int pos = 0;
153
154         if (is_Proj(irn)) {
155                 pos = translate_proj_pos(irn);
156                 irn = my_skip_proj(irn);
157         }
158
159         if (is_ia32_irn(irn)) {
160                 const arch_register_t **slots;
161
162                 slots      = get_ia32_slots(irn);
163                 slots[pos] = reg;
164         }
165         else {
166                 ia32_set_firm_reg(self, irn, reg, cur_reg_set);
167         }
168 }
169
170 static const arch_register_t *ia32_get_irn_reg(const arch_irn_ops_t *self, const ir_node *irn) {
171         int pos = 0;
172         const arch_register_t *reg = NULL;
173
174         if (is_Proj(irn)) {
175                 pos = translate_proj_pos(irn);
176                 irn = my_skip_proj(irn);
177         }
178
179         if (is_ia32_irn(irn)) {
180                 const arch_register_t **slots;
181                 slots = get_ia32_slots(irn);
182                 reg   = slots[pos];
183         }
184         else {
185                 reg = ia32_get_firm_reg(self, irn, cur_reg_set);
186         }
187
188         return reg;
189 }
190
191 static arch_irn_class_t ia32_classify(const arch_irn_ops_t *self, const ir_node *irn) {
192         irn = my_skip_proj(irn);
193         if (is_cfop(irn))
194                 return arch_irn_class_branch;
195         else if (is_ia32_irn(irn))
196                 return arch_irn_class_normal;
197         else
198                 return 0;
199 }
200
201 static arch_irn_flags_t ia32_get_flags(const arch_irn_ops_t *self, const ir_node *irn) {
202         irn = my_skip_proj(irn);
203         if (is_ia32_irn(irn))
204                 return get_ia32_flags(irn);
205         else {
206                 ir_printf("don't know flags of %+F\n", irn);
207                 return 0;
208         }
209 }
210
211 /* fill register allocator interface */
212
213 static const arch_irn_ops_t ia32_irn_ops = {
214         ia32_get_irn_reg_req,
215         ia32_set_irn_reg,
216         ia32_get_irn_reg,
217         ia32_classify,
218         ia32_get_flags
219 };
220
221
222
223 /**************************************************
224  *                _                         _  __
225  *               | |                       (_)/ _|
226  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
227  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
228  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
229  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
230  *                        __/ |
231  *                       |___/
232  **************************************************/
233
234 typedef struct _ia32_isa_t {
235         const arch_isa_if_t *impl;
236         int                  num_codegens;
237 } ia32_isa_t;
238
239 /**
240  * Transforms the standard firm graph into
241  * an ia32 firm graph
242  */
243 static void ia32_prepare_graph(void *self) {
244         ia32_code_gen_t *cg  = self;
245
246         if (! is_pseudo_ir_graph(cg->irg))
247                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
248 }
249
250
251
252 /**
253  * Dummy functions for hooks we don't need but which must be filled.
254  */
255 static void ia32_before_sched(void *self) {
256 }
257
258 static void ia32_before_ra(void *self) {
259 }
260
261
262
263 /**
264  * Emits the code, closes the output file and frees
265  * the code generator interface.
266  */
267 static void ia32_codegen(void *self) {
268         ia32_code_gen_t *cg = self;
269         ir_graph       *irg = cg->irg;
270         FILE           *out = cg->out;
271
272         if (cg->emit_decls) {
273                 ia32_gen_decls(cg->out);
274                 cg->emit_decls = 0;
275         }
276
277 //      ia32_finish_irg(irg);
278         ia32_gen_routine(out, irg, cg->arch_env);
279
280         cur_reg_set = NULL;
281
282         /* de-allocate code generator */
283         del_set(cg->reg_set);
284         free(self);
285 }
286
287 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
288
289 static const arch_code_generator_if_t ia32_code_gen_if = {
290         ia32_cg_init,
291         ia32_prepare_graph,
292         ia32_before_sched,   /* before scheduling hook */
293         ia32_before_ra,      /* before register allocation hook */
294         ia32_codegen         /* emit && done */
295 };
296
297 /**
298  * Initializes the code generator.
299  */
300 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
301         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
302         ia32_code_gen_t *cg  = malloc(sizeof(*cg));
303
304         cg->impl       = &ia32_code_gen_if;
305         cg->irg        = irg;
306         cg->reg_set    = new_set(cmp_irn_reg_assoc, 1024);
307         cg->mod        = firm_dbg_register("be.transform.ia32");
308         cg->out        = F;
309         cg->arch_env   = arch_env;
310
311         isa->num_codegens++;
312
313         if (isa->num_codegens > 1)
314                 cg->emit_decls = 0;
315         else
316                 cg->emit_decls = 1;
317
318         cur_reg_set = cg->reg_set;
319
320         return (arch_code_generator_t *)cg;
321 }
322
323
324
325 /*****************************************************************
326  *  ____             _                  _   _____  _____
327  * |  _ \           | |                | | |_   _|/ ____|  /\
328  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
329  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
330  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
331  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
332  *
333  *****************************************************************/
334
335 /**
336  * Initializes the backend ISA and opens the output file.
337  */
338 static void *ia32_init(void) {
339         static int inited = 0;
340         ia32_isa_t *isa   = malloc(sizeof(*isa));
341
342         isa->impl = &ia32_isa_if;
343
344         if(inited)
345                 return NULL;
346
347         inited = 1;
348
349         isa->num_codegens = 0;
350
351         ia32_register_init();
352         ia32_create_opcodes();
353
354         return isa;
355 }
356
357
358
359 /**
360  * Closes the output file and frees the ISA structure.
361  */
362 static void ia32_done(void *self) {
363         free(self);
364 }
365
366
367
368 static int ia32_get_n_reg_class(const void *self) {
369         return N_CLASSES;
370 }
371
372 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
373         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
374         return &ia32_reg_classes[i];
375 }
376
377 static const arch_irn_ops_t *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
378         return &ia32_irn_ops;
379 }
380
381 const arch_irn_handler_t ia32_irn_handler = {
382         ia32_get_irn_ops
383 };
384
385 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
386         return &ia32_irn_handler;
387 }
388
389
390
391 /**
392  * Initializes the code generator interface.
393  */
394 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
395         return &ia32_code_gen_if;
396 }
397
398 /**
399  * Returns the default scheduler
400  */
401 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
402         return reg_pressure_selector;
403 }
404
405 #ifdef WITH_LIBCORE
406 static void ia32_register_options(lc_opt_entry_t *ent)
407 {
408 }
409 #endif /* WITH_LIBCORE */
410
411 const arch_isa_if_t ia32_isa_if = {
412 #ifdef WITH_LIBCORE
413         ia32_register_options,
414 #endif
415         ia32_init,
416         ia32_done,
417         ia32_get_n_reg_class,
418         ia32_get_reg_class,
419         ia32_get_irn_handler,
420         ia32_get_code_generator_if,
421         ia32_get_list_sched_selector
422 };