502f590535a676c4f5eae233d7c293f10f5eca23
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /**
2  * This is the main ia32 firm backend driver.
3  *
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #ifdef _WIN32
12 #include <malloc.h>
13 #else
14 #include <alloca.h>
15 #endif
16
17 #include "pseudo_irg.h"
18 #include "irgwalk.h"
19 #include "irprog.h"
20 #include "irprintf.h"
21 #include "iredges_t.h"
22 #include "ircons.h"
23 #include "irgmod.h"
24 #include "irgopt.h"
25
26 #include "bitset.h"
27 #include "debug.h"
28
29 #include "../beabi.h"                 /* the general register allocator interface */
30 #include "../benode_t.h"
31 #include "../belower.h"
32 #include "../besched_t.h"
33 #include "../be.h"
34 #include "bearch_ia32_t.h"
35
36 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
37 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
38 #include "ia32_gen_decls.h"           /* interface declaration emitter */
39 #include "ia32_transform.h"
40 #include "ia32_emitter.h"
41 #include "ia32_map_regs.h"
42 #include "ia32_optimize.h"
43
44 #define DEBUG_MODULE "firm.be.ia32.isa"
45
46 /* TODO: ugly */
47 static set *cur_reg_set = NULL;
48
49 #undef is_Start
50 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
51
52 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
53         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_XXX]);
54 }
55
56 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
57         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_fp_regs[REG_XXXX]);
58 }
59
60 /**************************************************
61  *                         _ _              _  __
62  *                        | | |            (_)/ _|
63  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
64  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
65  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
66  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
67  *            __/ |
68  *           |___/
69  **************************************************/
70
71 static ir_node *my_skip_proj(const ir_node *n) {
72         while (is_Proj(n))
73                 n = get_Proj_pred(n);
74         return (ir_node *)n;
75 }
76
77 /**
78  * Return register requirements for an ia32 node.
79  * If the node returns a tuple (mode_T) then the proj's
80  * will be asked for this information.
81  */
82 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) {
83         const ia32_register_req_t *irn_req;
84         long                       node_pos = pos == -1 ? 0 : pos;
85         ir_mode                   *mode     = get_irn_mode(irn);
86         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
87
88         if (mode == mode_T || mode == mode_M) {
89                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
90                 return NULL;
91         }
92
93         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
94
95
96         if (is_Proj(irn)) {
97                 if (pos == -1) {
98                         node_pos = ia32_translate_proj_pos(irn);
99                 }
100                 else {
101                         node_pos = pos;
102                 }
103
104                 irn = my_skip_proj(irn);
105
106                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
107         }
108
109         if (is_ia32_irn(irn)) {
110                 if (pos >= 0) {
111                         irn_req = get_ia32_in_req(irn, pos);
112                 }
113                 else {
114                         irn_req = get_ia32_out_req(irn, node_pos);
115                 }
116
117                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
118
119                 memcpy(req, &(irn_req->req), sizeof(*req));
120
121                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
122                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
123                         req->other_same = get_irn_n(irn, irn_req->same_pos);
124                 }
125
126                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
127                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
128                         req->other_different = get_irn_n(irn, irn_req->different_pos);
129                 }
130         }
131         else {
132                 /* treat Phi like Const with default requirements */
133                 if (is_Phi(irn)) {
134                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
135                         if (mode_is_float(mode))
136                                 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
137                         else if (mode_is_int(mode) || mode_is_reference(mode))
138                                 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
139                         else if (mode == mode_T || mode == mode_M) {
140                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
141                                 return NULL;
142                         }
143                         else
144                                 assert(0 && "unsupported Phi-Mode");
145                 }
146                 else {
147                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
148                         req = NULL;
149                 }
150         }
151
152         return req;
153 }
154
155 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
156         int                   pos = 0;
157         const ia32_irn_ops_t *ops = self;
158
159         DBG((ops->cg->mod, LEVEL_1, "ia32 assigned register %s to node %+F\n", reg->name, irn));
160
161         if (is_Proj(irn)) {
162                 pos = ia32_translate_proj_pos(irn);
163                 irn = my_skip_proj(irn);
164         }
165
166         if (is_ia32_irn(irn)) {
167                 const arch_register_t **slots;
168
169                 slots      = get_ia32_slots(irn);
170                 slots[pos] = reg;
171         }
172         else {
173                 ia32_set_firm_reg(irn, reg, cur_reg_set);
174         }
175 }
176
177 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
178         int pos = 0;
179         const arch_register_t *reg = NULL;
180
181         if (is_Proj(irn)) {
182                 pos = ia32_translate_proj_pos(irn);
183                 irn = my_skip_proj(irn);
184         }
185
186         if (is_ia32_irn(irn)) {
187                 const arch_register_t **slots;
188                 slots = get_ia32_slots(irn);
189                 reg   = slots[pos];
190         }
191         else {
192                 reg = ia32_get_firm_reg(irn, cur_reg_set);
193         }
194
195         return reg;
196 }
197
198 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
199         irn = my_skip_proj(irn);
200         if (is_cfop(irn))
201                 return arch_irn_class_branch;
202         else if (is_ia32_irn(irn))
203                 return arch_irn_class_normal;
204         else
205                 return 0;
206 }
207
208 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
209         irn = my_skip_proj(irn);
210         if (is_ia32_irn(irn))
211                 return get_ia32_flags(irn);
212         else {
213                 return 0;
214         }
215 }
216
217 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
218         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
219 }
220
221 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
222         char buf[64];
223         const ia32_irn_ops_t *ops = self;
224
225         if (is_ia32_use_frame(irn) && bias != 0) {
226                 ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
227
228                 DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
229                 snprintf(buf, sizeof(buf), "%d", bias);
230                 add_ia32_am_offs(irn, buf);
231                 am_flav |= ia32_O;
232                 set_ia32_am_flavour(irn, am_flav);
233         }
234 }
235
236 /* fill register allocator interface */
237
238 static const arch_irn_ops_if_t ia32_irn_ops_if = {
239         ia32_get_irn_reg_req,
240         ia32_set_irn_reg,
241         ia32_get_irn_reg,
242         ia32_classify,
243         ia32_get_flags,
244         ia32_get_frame_entity,
245         ia32_set_stack_bias
246 };
247
248 ia32_irn_ops_t ia32_irn_ops = {
249         &ia32_irn_ops_if,
250         NULL
251 };
252
253
254
255 /**************************************************
256  *                _                         _  __
257  *               | |                       (_)/ _|
258  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
259  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
260  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
261  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
262  *                        __/ |
263  *                       |___/
264  **************************************************/
265
266 /**
267  * Transforms the standard firm graph into
268  * an ia32 firm graph
269  */
270 static void ia32_prepare_graph(void *self) {
271         ia32_code_gen_t *cg = self;
272         firm_dbg_module_t *old_mod = cg->mod;
273
274         cg->mod = firm_dbg_register("firm.be.ia32.transform");
275         irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_node, cg);
276         dump_ir_block_graph_sched(cg->irg, "-transformed");
277
278         edges_deactivate(cg->irg);
279         dead_node_elimination(cg->irg);
280         edges_activate(cg->irg);
281
282         cg->mod = old_mod;
283
284         if (cg->opt.doam) {
285                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
286                 dump_ir_block_graph_sched(cg->irg, "-am");
287         }
288 }
289
290
291 /**
292  * Insert copies for all ia32 nodes where the should_be_same requirement
293  * is not fulfilled.
294  * Transform Sub into Neg -- Add if IN2 == OUT
295  */
296 static void ia32_finish_irg_walker(ir_node *irn, void *env) {
297         ia32_code_gen_t            *cg = env;
298         const ia32_register_req_t **reqs;
299         const arch_register_t      *out_reg, *in_reg;
300         int                         n_res, i;
301         ir_node                    *copy, *in_node, *block;
302
303         if (! is_ia32_irn(irn))
304                 return;
305
306         /* nodes with destination address mode don't produce values */
307         if (get_ia32_op_type(irn) == ia32_AddrModeD)
308                 return;
309
310         reqs  = get_ia32_out_req_all(irn);
311         n_res = get_ia32_n_res(irn);
312         block = get_nodes_block(irn);
313
314         /* check all OUT requirements, if there is a should_be_same */
315         for (i = 0; i < n_res; i++) {
316                 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
317                         /* get in and out register */
318                         out_reg = get_ia32_out_reg(irn, i);
319                         in_node = get_irn_n(irn, reqs[i]->same_pos);
320                         in_reg  = arch_get_irn_register(cg->arch_env, in_node);
321
322                         /* check if in and out register are equal */
323                         if (arch_register_get_index(out_reg) != arch_register_get_index(in_reg)) {
324                                 DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
325
326                                 /* create copy from in register */
327                                 copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
328
329                                 /* destination is the out register */
330                                 arch_set_irn_register(cg->arch_env, copy, out_reg);
331
332                                 /* insert copy before the node into the schedule */
333                                 sched_add_before(irn, copy);
334
335                                 /* set copy as in */
336                                 set_irn_n(irn, reqs[i]->same_pos, copy);
337                         }
338                 }
339         }
340
341         /* check if there is a sub which need to be transformed */
342         ia32_transform_sub_to_neg_add(irn, cg);
343 }
344
345 /**
346  * Add Copy nodes for not fulfilled should_be_equal constraints
347  */
348 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
349         irg_walk_blkwise_graph(irg, NULL, ia32_finish_irg_walker, cg);
350 }
351
352
353
354 /**
355  * Dummy functions for hooks we don't need but which must be filled.
356  */
357 static void ia32_before_sched(void *self) {
358 }
359
360 static void ia32_before_ra(void *self) {
361 }
362
363
364
365 /**
366  * Transforms a be node into a Load.
367  */
368 static void transform_to_Load(ia32_transform_env_t *env) {
369         ir_node *irn         = env->irn;
370         entity  *ent         = be_get_frame_entity(irn);
371         ir_mode *mode        = env->mode;
372         ir_node *noreg       = ia32_new_NoReg_gp(env->cg);
373         ir_node *nomem       = new_rd_NoMem(env->irg);
374         ir_node *sched_point = NULL;
375         ir_node *ptr         = get_irn_n(irn, 0);
376         ir_node *mem         = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
377         ir_node *new_op, *proj;
378         const arch_register_t *reg;
379
380         if (sched_is_scheduled(irn)) {
381                 sched_point = sched_prev(irn);
382         }
383
384         if (mode_is_float(mode)) {
385                 new_op = new_rd_ia32_fLoad(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
386         }
387         else {
388                 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
389         }
390
391         set_ia32_am_support(new_op, ia32_am_Source);
392         set_ia32_op_type(new_op, ia32_AddrModeS);
393         set_ia32_am_flavour(new_op, ia32_B);
394         set_ia32_ls_mode(new_op, mode);
395         set_ia32_frame_ent(new_op, ent);
396         set_ia32_use_frame(new_op);
397
398         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
399
400         if (sched_point) {
401                 sched_add_after(sched_point, new_op);
402                 sched_add_after(new_op, proj);
403
404                 sched_remove(irn);
405         }
406
407         /* copy the register from the old node to the new Load */
408         reg = arch_get_irn_register(env->cg->arch_env, irn);
409         arch_set_irn_register(env->cg->arch_env, new_op, reg);
410
411         exchange(irn, proj);
412
413 }
414
415 /**
416  * Transforms a be node into a Store.
417  */
418 static void transform_to_Store(ia32_transform_env_t *env) {
419         ir_node *irn   = env->irn;
420         entity  *ent   = be_get_frame_entity(irn);
421         ir_mode *mode  = env->mode;
422         ir_node *noreg = ia32_new_NoReg_gp(env->cg);
423         ir_node *nomem = new_rd_NoMem(env->irg);
424         ir_node *ptr   = get_irn_n(irn, 0);
425         ir_node *val   = get_irn_n(irn, 1);
426         ir_node *new_op, *proj;
427         ir_node *sched_point = NULL;
428
429         if (sched_is_scheduled(irn)) {
430                 sched_point = sched_prev(irn);
431         }
432
433         if (mode_is_float(mode)) {
434                 new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
435         }
436         else {
437                 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
438         }
439
440         set_ia32_am_support(new_op, ia32_am_Dest);
441         set_ia32_op_type(new_op, ia32_AddrModeD);
442         set_ia32_am_flavour(new_op, ia32_B);
443         set_ia32_ls_mode(new_op, get_irn_mode(val));
444         set_ia32_frame_ent(new_op, ent);
445         set_ia32_use_frame(new_op);
446
447         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, 0);
448
449         if (sched_point) {
450                 sched_add_after(sched_point, new_op);
451                 sched_add_after(new_op, proj);
452
453                 sched_remove(irn);
454         }
455
456         exchange(irn, proj);
457
458 }
459
460 /**
461  * Calls the transform functions for StackParam, Spill and Reload.
462  */
463 static void ia32_after_ra_walker(ir_node *node, void *env) {
464         ia32_code_gen_t *cg = env;
465         ia32_transform_env_t tenv;
466
467         if (is_Block(node))
468                 return;
469
470         tenv.block = get_nodes_block(node);
471         tenv.dbg   = get_irn_dbg_info(node);
472         tenv.irg   = current_ir_graph;
473         tenv.irn   = node;
474         tenv.mod   = cg->mod;
475         tenv.mode  = get_irn_mode(node);
476         tenv.cg    = cg;
477
478         /* be_is_StackParam(node) || */
479         if (be_is_Reload(node)) {
480                 transform_to_Load(&tenv);
481         }
482         else if (be_is_Spill(node)) {
483                 transform_to_Store(&tenv);
484         }
485 }
486
487 /**
488  * We transform StackParam, Spill and Reload here. This needs to be done before
489  * stack biasing otherwise we would miss the corrected offset for these nodes.
490  */
491 static void ia32_after_ra(void *self) {
492         ia32_code_gen_t *cg = self;
493         irg_walk_blkwise_graph(cg->irg, NULL, ia32_after_ra_walker, self);
494 }
495
496
497 /**
498  * Emits the code, closes the output file and frees
499  * the code generator interface.
500  */
501 static void ia32_codegen(void *self) {
502         ia32_code_gen_t *cg = self;
503         ir_graph        *irg = cg->irg;
504         FILE            *out = cg->out;
505
506         if (cg->emit_decls) {
507                 ia32_gen_decls(cg->out);
508                 cg->emit_decls = 0;
509         }
510
511         ia32_finish_irg(irg, cg);
512         dump_ir_block_graph_sched(irg, "-finished");
513         ia32_gen_routine(out, irg, cg);
514
515         cur_reg_set = NULL;
516
517         pmap_destroy(cg->tv_ent);
518         pmap_destroy(cg->types);
519
520         /* de-allocate code generator */
521         del_set(cg->reg_set);
522         free(self);
523 }
524
525 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
526
527 static const arch_code_generator_if_t ia32_code_gen_if = {
528         ia32_cg_init,
529         NULL,                /* before abi introduce hook */
530         ia32_prepare_graph,
531         ia32_before_sched,   /* before scheduling hook */
532         ia32_before_ra,      /* before register allocation hook */
533         ia32_after_ra,       /* after register allocation hook */
534         ia32_codegen         /* emit && done */
535 };
536
537 /**
538  * Initializes the code generator.
539  */
540 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
541         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
542         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
543
544         cg->impl     = &ia32_code_gen_if;
545         cg->irg      = birg->irg;
546         cg->reg_set  = new_set(ia32_cmp_irn_reg_assoc, 1024);
547         cg->mod      = firm_dbg_register("firm.be.ia32.cg");
548         cg->out      = F;
549         cg->arch_env = birg->main_env->arch_env;
550         cg->types    = pmap_create();
551         cg->tv_ent   = pmap_create();
552         cg->birg     = birg;
553
554         /* set optimizations */
555         cg->opt.incdec    = 0;
556         cg->opt.doam      = 1;
557         cg->opt.placecnst = 1;
558         cg->opt.immops    = 1;
559
560 #ifndef NDEBUG
561         if (isa->name_obst_size) {
562                 //printf("freed %d bytes from name obst\n", isa->name_obst_size);
563                 isa->name_obst_size = 0;
564                 obstack_free(isa->name_obst, NULL);
565                 obstack_init(isa->name_obst);
566         }
567 #endif /* NDEBUG */
568
569         isa->num_codegens++;
570
571         if (isa->num_codegens > 1)
572                 cg->emit_decls = 0;
573         else
574                 cg->emit_decls = 1;
575
576         cur_reg_set = cg->reg_set;
577
578         ia32_irn_ops.cg = cg;
579
580         return (arch_code_generator_t *)cg;
581 }
582
583
584
585 /*****************************************************************
586  *  ____             _                  _   _____  _____
587  * |  _ \           | |                | | |_   _|/ ____|  /\
588  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
589  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
590  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
591  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
592  *
593  *****************************************************************/
594
595 static ia32_isa_t ia32_isa_template = {
596         &ia32_isa_if,            /* isa interface implementation */
597         &ia32_gp_regs[REG_ESP],  /* stack pointer register */
598         &ia32_gp_regs[REG_EBP],  /* base pointer register */
599         -1,                      /* stack direction */
600         0,                       /* number of code generator objects so far */
601         NULL                     /* name obstack */
602 };
603
604 /**
605  * Initializes the backend ISA.
606  */
607 static void *ia32_init(void) {
608         static int inited = 0;
609         ia32_isa_t *isa;
610
611         if(inited)
612                 return NULL;
613
614         isa = xcalloc(1, sizeof(*isa));
615         memcpy(isa, &ia32_isa_template, sizeof(*isa));
616
617         ia32_register_init(isa);
618         ia32_create_opcodes();
619
620 #ifndef NDEBUG
621         isa->name_obst = xcalloc(1, sizeof(*(isa->name_obst)));
622         obstack_init(isa->name_obst);
623         isa->name_obst_size = 0;
624 #endif /* NDEBUG */
625
626         inited = 1;
627
628         return isa;
629 }
630
631
632
633 /**
634  * Closes the output file and frees the ISA structure.
635  */
636 static void ia32_done(void *self) {
637         ia32_isa_t *isa = self;
638
639 #ifndef NDEBUG
640         //printf("name obst size = %d bytes\n", isa->name_obst_size);
641         obstack_free(isa->name_obst, NULL);
642 #endif /* NDEBUG */
643
644         free(self);
645 }
646
647
648
649 static int ia32_get_n_reg_class(const void *self) {
650         return N_CLASSES;
651 }
652
653 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
654         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
655         return &ia32_reg_classes[i];
656 }
657
658 /**
659  * Get the register class which shall be used to store a value of a given mode.
660  * @param self The this pointer.
661  * @param mode The mode in question.
662  * @return A register class which can hold values of the given mode.
663  */
664 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
665         if (mode_is_float(mode))
666                 return &ia32_reg_classes[CLASS_ia32_fp];
667         else
668                 return &ia32_reg_classes[CLASS_ia32_gp];
669 }
670
671 /**
672  * Produces the type which sits between the stack args and the locals on the stack.
673  * it will contain the return address and space to store the old base pointer.
674  * @return The Firm type modeling the ABI between type.
675  */
676 static ir_type *get_between_type(void)
677 {
678         static ir_type *between_type = NULL;
679         static entity *old_bp_ent    = NULL;
680
681         if(!between_type) {
682                 entity *ret_addr_ent;
683                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
684                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
685
686                 between_type           = new_type_class(new_id_from_str("ia32_between_type"));
687                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
688                 ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
689
690                 set_entity_offset_bytes(old_bp_ent, 0);
691                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
692                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
693         }
694
695         return between_type;
696 }
697
698 /**
699  * Get the ABI restrictions for procedure calls.
700  * @param self        The this pointer.
701  * @param method_type The type of the method (procedure) in question.
702  * @param abi         The abi object to be modified
703  */
704 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
705         ir_type  *between_type;
706         ir_type  *tp;
707         ir_mode  *mode;
708         unsigned  cc        = get_method_calling_convention(method_type);
709         int       n         = get_method_n_params(method_type);
710         int       biggest_n = -1;
711         int       stack_idx = 0;
712         int       i, ignore;
713         ir_mode **modes;
714         const arch_register_t *reg;
715         be_abi_call_flags_t call_flags;
716
717         /* set abi flags for calls */
718         call_flags.bits.left_to_right         = 0;
719         call_flags.bits.store_args_sequential = 0;
720         call_flags.bits.try_omit_fp           = 1;
721         call_flags.bits.fp_free               = 0;
722         call_flags.bits.call_has_imm          = 1;
723
724         /* get the between type and the frame pointer save entity */
725         between_type = get_between_type();
726
727         /* set stack parameter passing style */
728         be_abi_call_set_flags(abi, call_flags, between_type);
729
730         /* collect the mode for each type */
731         modes = alloca(n * sizeof(modes[0]));
732
733         for (i = 0; i < n; i++) {
734                 tp       = get_method_param_type(method_type, i);
735                 modes[i] = get_type_mode(tp);
736         }
737
738         /* set register parameters  */
739         if (cc & cc_reg_param) {
740                 /* determine the number of parameters passed via registers */
741                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
742
743                 /* loop over all parameters and set the register requirements */
744                 for (i = 0; i <= biggest_n; i++) {
745                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
746                         assert(reg && "kaputt");
747                         be_abi_call_param_reg(abi, i, reg);
748                 }
749
750                 stack_idx = i;
751         }
752
753
754         /* set stack parameters */
755         for (i = stack_idx; i < n; i++) {
756                 be_abi_call_param_stack(abi, i);
757         }
758
759
760         /* set return registers */
761         n = get_method_n_ress(method_type);
762
763         assert(n <= 2 && "more than two results not supported");
764
765         /* In case of 64bit returns, we will have two 32bit values */
766         if (n == 2) {
767                 tp   = get_method_res_type(method_type, 0);
768                 mode = get_type_mode(tp);
769
770                 assert(!mode_is_float(mode) && "two FP results not supported");
771
772                 tp   = get_method_res_type(method_type, 1);
773                 mode = get_type_mode(tp);
774
775                 assert(!mode_is_float(mode) && "two FP results not supported");
776
777                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
778                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
779         }
780         else if (n == 1) {
781                 tp   = get_method_res_type(method_type, 0);
782                 assert(is_atomic_type(tp));
783                 mode = get_type_mode(tp);
784
785                 be_abi_call_res_reg(abi, 0, mode_is_float(mode) ? &ia32_fp_regs[REG_XMM0] : &ia32_gp_regs[REG_EAX]);
786         }
787 }
788
789
790 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
791         return &ia32_irn_ops;
792 }
793
794 const arch_irn_handler_t ia32_irn_handler = {
795         ia32_get_irn_ops
796 };
797
798 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
799         return &ia32_irn_handler;
800 }
801
802 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
803         return is_ia32_irn(irn);
804 }
805
806 /**
807  * Initializes the code generator interface.
808  */
809 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
810         return &ia32_code_gen_if;
811 }
812
813 list_sched_selector_t ia32_sched_selector;
814
815 /**
816  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
817  */
818 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
819         memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
820         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
821         return &ia32_sched_selector;
822 }
823
824 #ifdef WITH_LIBCORE
825 static void ia32_register_options(lc_opt_entry_t *ent)
826 {
827 }
828 #endif /* WITH_LIBCORE */
829
830 const arch_isa_if_t ia32_isa_if = {
831 #ifdef WITH_LIBCORE
832         ia32_register_options,
833 #endif
834         ia32_init,
835         ia32_done,
836         ia32_get_n_reg_class,
837         ia32_get_reg_class,
838         ia32_get_reg_class_for_mode,
839         ia32_get_call_abi,
840         ia32_get_irn_handler,
841         ia32_get_code_generator_if,
842         ia32_get_list_sched_selector
843 };