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