26ee9c5533c02c9858fa25cf7a8e73433c5e77c1
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #ifdef _WIN32
6 #include <malloc.h>
7 #else
8 #include <alloca.h>
9 #endif
10
11 #include "pseudo_irg.h"
12 #include "irgwalk.h"
13 #include "irprog.h"
14 #include "irprintf.h"
15 #include "iredges_t.h"
16 #include "ircons.h"
17 #include "irgmod.h"
18
19 #include "bitset.h"
20 #include "debug.h"
21
22 #include "../beabi.h"                 /* the general register allocator interface */
23 #include "../benode_t.h"
24 #include "../belower.h"
25 #include "../besched_t.h"
26 #include "../be.h"
27 #include "bearch_ia32_t.h"
28
29 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
30 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
31 #include "ia32_gen_decls.h"           /* interface declaration emitter */
32 #include "ia32_transform.h"
33 #include "ia32_emitter.h"
34 #include "ia32_map_regs.h"
35 #include "ia32_optimize.h"
36
37 #define DEBUG_MODULE "firm.be.ia32.isa"
38
39 /* TODO: ugly */
40 static set *cur_reg_set = NULL;
41
42 #undef is_Start
43 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
44
45 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
46         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_XXX]);
47 }
48
49 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
50         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_fp_regs[REG_XXXX]);
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 void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
76         const ia32_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         const ia32_irn_ops_t      *ops      = self;
81
82         if (mode == mode_T || mode == mode_M) {
83                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
84                 return NULL;
85         }
86
87         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
88
89
90         if (is_Proj(irn)) {
91                 if (pos == -1) {
92                         node_pos = ia32_translate_proj_pos(irn);
93                 }
94                 else {
95                         node_pos = pos;
96                 }
97
98                 irn = my_skip_proj(irn);
99
100                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
101         }
102
103         if (is_ia32_irn(irn)) {
104                 if (pos >= 0) {
105                         irn_req = get_ia32_in_req(irn, pos);
106                 }
107                 else {
108                         irn_req = get_ia32_out_req(irn, node_pos);
109                 }
110
111                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
112
113                 memcpy(req, &(irn_req->req), sizeof(*req));
114
115                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
116                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
117                         req->other_same = get_irn_n(irn, irn_req->same_pos);
118                 }
119
120                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
121                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
122                         req->other_different = get_irn_n(irn, irn_req->different_pos);
123                 }
124         }
125         else {
126                 /* treat Phi like Const with default requirements */
127                 if (is_Phi(irn)) {
128                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
129                         if (mode_is_float(mode))
130                                 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
131                         else if (mode_is_int(mode) || mode_is_reference(mode))
132                                 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
133                         else if (mode == mode_T || mode == mode_M) {
134                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
135                                 return NULL;
136                         }
137                         else
138                                 assert(0 && "unsupported Phi-Mode");
139                 }
140                 else {
141                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
142                         req = NULL;
143                 }
144         }
145
146         return req;
147 }
148
149 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
150         int pos = 0;
151
152         if (is_Proj(irn)) {
153                 pos = ia32_translate_proj_pos(irn);
154                 irn = my_skip_proj(irn);
155         }
156
157         if (is_ia32_irn(irn)) {
158                 const arch_register_t **slots;
159
160                 slots      = get_ia32_slots(irn);
161                 slots[pos] = reg;
162         }
163         else {
164                 ia32_set_firm_reg(irn, reg, cur_reg_set);
165         }
166 }
167
168 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
169         int pos = 0;
170         const arch_register_t *reg = NULL;
171
172         if (is_Proj(irn)) {
173                 pos = ia32_translate_proj_pos(irn);
174                 irn = my_skip_proj(irn);
175         }
176
177         if (is_ia32_irn(irn)) {
178                 const arch_register_t **slots;
179                 slots = get_ia32_slots(irn);
180                 reg   = slots[pos];
181         }
182         else {
183                 reg = ia32_get_firm_reg(irn, cur_reg_set);
184         }
185
186         return reg;
187 }
188
189 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
190         irn = my_skip_proj(irn);
191         if (is_cfop(irn))
192                 return arch_irn_class_branch;
193         else if (is_ia32_Call(irn))
194                 return arch_irn_class_call;
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 void *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                 return 0;
207         }
208 }
209
210 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn)
211 {
212         /* TODO: Implement */
213         return NULL;
214 }
215
216 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
217         if (get_ia32_use_frame(irn)) {
218                 /* TODO: correct offset */
219         }
220 }
221
222 /* fill register allocator interface */
223
224 static const arch_irn_ops_if_t ia32_irn_ops_if = {
225         ia32_get_irn_reg_req,
226         ia32_set_irn_reg,
227         ia32_get_irn_reg,
228         ia32_classify,
229         ia32_get_flags,
230         ia32_get_frame_entity,
231         ia32_set_stack_bias
232 };
233
234 ia32_irn_ops_t ia32_irn_ops = {
235         &ia32_irn_ops_if,
236         NULL
237 };
238
239
240
241 /**************************************************
242  *                _                         _  __
243  *               | |                       (_)/ _|
244  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
245  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
246  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
247  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
248  *                        __/ |
249  *                       |___/
250  **************************************************/
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         irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
260         dump_ir_block_graph_sched(cg->irg, "-transformed");
261         edges_deactivate(cg->irg);
262         edges_activate(cg->irg);
263         irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
264         dump_ir_block_graph_sched(cg->irg, "-am");
265 }
266
267
268
269 /**
270  * Stack reservation and StackParam lowering.
271  */
272 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
273
274 }
275
276
277
278 /**
279  * Dummy functions for hooks we don't need but which must be filled.
280  */
281 static void ia32_before_sched(void *self) {
282 }
283
284 static void ia32_before_ra(void *self) {
285 }
286
287
288 /**
289  * Creates a Store for a Spill
290  */
291 static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
292         ia32_code_gen_t *cg    = self;
293         ir_graph        *irg   = cg->irg;
294         dbg_info        *dbg   = get_irn_dbg_info(spill);
295         ir_node         *block = get_nodes_block(spill);
296         ir_node         *ptr   = get_irg_frame(irg);
297         ir_node         *val   = be_get_Spill_context(spill);
298         ir_node         *mem   = new_rd_NoMem(irg);
299         ir_mode         *mode  = get_irn_mode(spill);
300         entity          *ent   = be_get_spill_entity(spill);
301         unsigned         offs  = get_entity_offset_bytes(ent);
302         ir_node         *noreg, *res;
303         char             buf[64];
304
305         DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
306
307         if (mode_is_float(mode)) {
308                 noreg = ia32_new_NoReg_fp(cg);
309                 res   = new_rd_ia32_fStore(dbg, irg, block, ptr, noreg, val, mem, mode);
310         }
311         else {
312                 noreg = ia32_new_NoReg_gp(cg);
313                 res   = new_rd_ia32_Store(dbg, irg, block, ptr, noreg, val, mem, mode);
314         }
315
316         snprintf(buf, sizeof(buf), "%d", offs);
317         add_ia32_am_offs(res, buf);
318
319         return res;
320 }
321
322 /**
323  * Create a Load for a Spill
324  */
325 static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
326         ia32_code_gen_t *cg    = self;
327         ir_graph        *irg   = cg->irg;
328         dbg_info        *dbg   = get_irn_dbg_info(reload);
329         ir_node         *block = get_nodes_block(reload);
330         ir_node         *ptr   = get_irg_frame(irg);
331         ir_mode         *mode  = get_irn_mode(reload);
332         ir_node         *pred  = get_irn_n(reload, 0);
333         char             buf[64];
334         char            *ofs;
335         ir_node         *noreg, *res;
336
337         /* Get the offset to Load from. It can either be a Spill or a Store. */
338         if (be_is_Spill(pred)) {
339                 entity   *ent  = be_get_spill_entity(pred);
340                 unsigned  offs = get_entity_offset_bytes(ent);
341                 DB((cg->mod, LEVEL_1, "lower_reload: got offset %d for %+F\n", offs, ent));
342
343                 snprintf(buf, sizeof(buf), "%d", offs);
344         }
345         else if (is_ia32_Store(pred) || is_ia32_fStore(pred)) {
346                 ofs = get_ia32_am_offs(pred);
347                 strncpy(buf, ofs, sizeof(buf));
348                 free(ofs);
349         }
350         else {
351                 assert(0 && "unsupported Reload predecessor");
352         }
353
354         /* Create the Load */
355         if (mode_is_float(mode)) {
356                 noreg = ia32_new_NoReg_fp(cg);
357                 res   = new_rd_ia32_fLoad(dbg, irg, block, ptr, noreg, pred, mode_T);
358         }
359         else {
360                 noreg = ia32_new_NoReg_gp(cg);
361                 res   = new_rd_ia32_Load(dbg, irg, block, ptr, noreg, pred, mode_T);
362         }
363
364         /* Set offset */
365         add_ia32_am_offs(res, buf);
366
367         /* Return the result Proj */
368         return new_rd_Proj(dbg, irg, block, res, mode, 0);
369 }
370
371 /**
372  * Emits the code, closes the output file and frees
373  * the code generator interface.
374  */
375 static void ia32_codegen(void *self) {
376         ia32_code_gen_t *cg = self;
377         ir_graph        *irg = cg->irg;
378         FILE            *out = cg->out;
379
380         if (cg->emit_decls) {
381                 ia32_gen_decls(cg->out);
382                 cg->emit_decls = 0;
383         }
384
385         ia32_finish_irg(irg, cg);
386         //dump_ir_block_graph_sched(irg, "-finished");
387         ia32_gen_routine(out, irg, cg);
388
389         cur_reg_set = NULL;
390
391         pmap_destroy(cg->tv_ent);
392         pmap_destroy(cg->types);
393
394         /* de-allocate code generator */
395         del_set(cg->reg_set);
396         free(self);
397 }
398
399 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
400
401 static const arch_code_generator_if_t ia32_code_gen_if = {
402         ia32_cg_init,
403         ia32_prepare_graph,
404         ia32_before_sched,   /* before scheduling hook */
405         ia32_before_ra,      /* before register allocation hook */
406         ia32_lower_spill,
407         ia32_lower_reload,
408         ia32_codegen         /* emit && done */
409 };
410
411 /**
412  * Initializes the code generator.
413  */
414 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
415         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
416         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
417
418         cg->impl     = &ia32_code_gen_if;
419         cg->irg      = birg->irg;
420         cg->reg_set  = new_set(ia32_cmp_irn_reg_assoc, 1024);
421         cg->mod      = firm_dbg_register("firm.be.ia32.cg");
422         cg->out      = F;
423         cg->arch_env = birg->main_env->arch_env;
424         cg->types    = pmap_create();
425         cg->tv_ent   = pmap_create();
426         cg->birg     = birg;
427
428         isa->num_codegens++;
429
430         if (isa->num_codegens > 1)
431                 cg->emit_decls = 0;
432         else
433                 cg->emit_decls = 1;
434
435         cur_reg_set = cg->reg_set;
436
437         ia32_irn_ops.cg = cg;
438
439         return (arch_code_generator_t *)cg;
440 }
441
442
443
444 /*****************************************************************
445  *  ____             _                  _   _____  _____
446  * |  _ \           | |                | | |_   _|/ ____|  /\
447  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
448  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
449  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
450  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
451  *
452  *****************************************************************/
453
454 static ia32_isa_t ia32_isa_template = {
455         &ia32_isa_if,
456         &ia32_gp_regs[REG_ESP],
457         &ia32_gp_regs[REG_EBP],
458         -1,
459         0
460 };
461
462 /**
463  * Initializes the backend ISA.
464  */
465 static void *ia32_init(void) {
466         static int inited = 0;
467         ia32_isa_t *isa;
468
469         if(inited)
470                 return NULL;
471
472         isa = xcalloc(1, sizeof(*isa));
473         memcpy(isa, &ia32_isa_template, sizeof(*isa));
474
475         ia32_register_init(isa);
476         ia32_create_opcodes();
477
478         inited = 1;
479
480         return isa;
481 }
482
483
484
485 /**
486  * Closes the output file and frees the ISA structure.
487  */
488 static void ia32_done(void *self) {
489         free(self);
490 }
491
492
493
494 static int ia32_get_n_reg_class(const void *self) {
495         return N_CLASSES;
496 }
497
498 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
499         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
500         return &ia32_reg_classes[i];
501 }
502
503 /**
504  * Get the register class which shall be used to store a value of a given mode.
505  * @param self The this pointer.
506  * @param mode The mode in question.
507  * @return A register class which can hold values of the given mode.
508  */
509 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
510         if (mode_is_float(mode))
511                 return &ia32_reg_classes[CLASS_ia32_fp];
512         else
513                 return &ia32_reg_classes[CLASS_ia32_gp];
514 }
515
516 /**
517  * Produces the type which sits between the stack args and the locals on the stack.
518  * it will contain the return address and space to store the old base pointer.
519  * @return The Firm type modelling the ABI between type.
520  */
521 static ir_type *get_between_type(void)
522 {
523         static ir_type *between_type = NULL;
524         static entity *old_bp_ent    = NULL;
525
526         if(!between_type) {
527                 entity *ret_addr_ent;
528                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
529                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
530
531                 between_type           = new_type_class(new_id_from_str("ia32_between_type"));
532                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
533                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
534
535                 set_entity_offset_bytes(old_bp_ent, 0);
536                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
537                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
538         }
539
540         return between_type;
541 }
542
543 /**
544  * Get the ABI restrictions for procedure calls.
545  * @param self        The this pointer.
546  * @param method_type The type of the method (procedure) in question.
547  * @param abi         The abi object to be modified
548  */
549 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
550         ir_type  *between_type;
551         ir_type  *tp;
552         ir_mode  *mode;
553         unsigned  cc        = get_method_calling_convention(method_type);
554         int       n         = get_method_n_params(method_type);
555         int       biggest_n = -1;
556         int       stack_idx = 0;
557         int       i, ignore;
558         ir_mode **modes;
559         const arch_register_t *reg;
560
561         /* get the between type and the frame pointer save entity */
562         between_type = get_between_type();
563
564         /* set stack parameter passing style */
565         be_abi_call_set_flags(abi, BE_ABI_NONE, between_type);
566
567         /* collect the mode for each type */
568         modes = alloca(n * sizeof(modes[0]));
569
570         for (i = 0; i < n; i++) {
571                 tp       = get_method_param_type(method_type, i);
572                 modes[i] = get_type_mode(tp);
573         }
574
575         /* set register parameters  */
576         if (cc & cc_reg_param) {
577                 /* determine the number of parameters passed via registers */
578                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
579
580                 /* loop over all parameters and set the register requirements */
581                 for (i = 0; i <= biggest_n; i++) {
582                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
583                         assert(reg && "kaputt");
584                         be_abi_call_param_reg(abi, i, reg);
585                 }
586
587                 stack_idx = i;
588         }
589
590
591         /* set stack parameters */
592         for (i = stack_idx; i < n; i++) {
593                 be_abi_call_param_stack(abi, i);
594         }
595
596
597         /* set return registers */
598         n = get_method_n_ress(method_type);
599
600         assert(n <= 2 && "more than two results not supported");
601
602         /* In case of 64bit returns, we will have two 32bit values */
603         if (n == 2) {
604                 tp   = get_method_res_type(method_type, 0);
605                 mode = get_type_mode(tp);
606
607                 assert(!mode_is_float(mode) && "two FP results not supported");
608
609                 tp   = get_method_res_type(method_type, 1);
610                 mode = get_type_mode(tp);
611
612                 assert(!mode_is_float(mode) && "two FP results not supported");
613
614                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
615                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
616         }
617         else if (n == 1) {
618                 tp   = get_method_res_type(method_type, 0);
619                 mode = get_type_mode(tp);
620
621                 be_abi_call_res_reg(abi, 0, &ia32_fp_regs[mode_is_float(mode) ? REG_XMM0 : REG_EAX]);
622         }
623 }
624
625
626 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
627         return &ia32_irn_ops;
628 }
629
630 const arch_irn_handler_t ia32_irn_handler = {
631         ia32_get_irn_ops
632 };
633
634 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
635         return &ia32_irn_handler;
636 }
637
638 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
639         return is_ia32_irn(irn);
640 }
641
642 /**
643  * Initializes the code generator interface.
644  */
645 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
646         return &ia32_code_gen_if;
647 }
648
649 list_sched_selector_t ia32_sched_selector;
650
651 /**
652  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
653  */
654 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
655         memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
656         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
657         return &ia32_sched_selector;
658 }
659
660 #ifdef WITH_LIBCORE
661 static void ia32_register_options(lc_opt_entry_t *ent)
662 {
663 }
664 #endif /* WITH_LIBCORE */
665
666 const arch_isa_if_t ia32_isa_if = {
667 #ifdef WITH_LIBCORE
668         ia32_register_options,
669 #endif
670         ia32_init,
671         ia32_done,
672         ia32_get_n_reg_class,
673         ia32_get_reg_class,
674         ia32_get_reg_class_for_mode,
675         ia32_get_call_abi,
676         ia32_get_irn_handler,
677         ia32_get_code_generator_if,
678         ia32_get_list_sched_selector
679 };