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