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