1aa1e7683b1954eb8e2f23d7ec4e4aac4e0c1357
[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 "../bearch.h"                /* the general register allocator interface */
23 #include "../benode_t.h"
24 #include "../belower.h"
25 #include "../besched_t.h"
26 #include "bearch_ia32_t.h"
27
28 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
29 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
30 #include "ia32_gen_decls.h"           /* interface declaration emitter */
31 #include "ia32_transform.h"
32 #include "ia32_emitter.h"
33 #include "ia32_map_regs.h"
34 #include "ia32_optimize.h"
35
36 #define DEBUG_MODULE "firm.be.ia32.isa"
37
38 /* TODO: ugly */
39 static set *cur_reg_set = NULL;
40
41 #undef is_Start
42 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
43
44 /**************************************************
45  *                         _ _              _  __
46  *                        | | |            (_)/ _|
47  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
48  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
49  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
50  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
51  *            __/ |
52  *           |___/
53  **************************************************/
54
55 static ir_node *my_skip_proj(const ir_node *n) {
56         while (is_Proj(n))
57                 n = get_Proj_pred(n);
58         return (ir_node *)n;
59 }
60
61 static int is_Call_Proj(const ir_node *n) {
62         if (is_Proj(n)                               &&
63                 is_Proj(get_Proj_pred(n))                &&
64                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
65                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
66         {
67                 return 1;
68         }
69
70         return 0;
71 }
72
73 static int is_Start_Proj(const ir_node *n) {
74         if (is_Proj(n)                               &&
75                 is_Proj(get_Proj_pred(n))                &&
76                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
77                 is_Start(get_Proj_pred(get_Proj_pred(n))))
78         {
79                 return 1;
80         }
81
82         return 0;
83 }
84
85 static int is_P_frame_base_Proj(const ir_node *n) {
86         if (is_Proj(n)                                    &&
87                 is_Start(get_Proj_pred(n)) &&
88                 get_Proj_proj(n) == pn_Start_P_frame_base)
89         {
90                 return 1;
91         }
92
93         return 0;
94 }
95
96 static int is_used_by_Keep(const ir_node *n) {
97         return be_is_Keep(get_edge_src_irn(get_irn_out_edge_first(n)));
98 }
99
100 /**
101  * Return register requirements for an ia32 node.
102  * If the node returns a tuple (mode_T) then the proj's
103  * will be asked for this information.
104  */
105 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) {
106         const ia32_register_req_t *irn_req;
107         long                       node_pos = pos == -1 ? 0 : pos;
108         ir_mode                   *mode     = get_irn_mode(irn);
109         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
110         const ia32_irn_ops_t      *ops      = self;
111
112         if (mode == mode_T || mode == mode_M) {
113                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
114                 return NULL;
115         }
116
117         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
118
119
120         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
121                 if (pos >= 0) {
122                         req = NULL;
123                 }
124                 else {
125                         irn_req = ia32_projnum_reg_req_map[get_Proj_proj(irn)];
126                         memcpy(req, &(irn_req->req), sizeof(*req));
127                 }
128
129                 return req;
130         }
131         else if (is_Start_Proj(irn)) {
132                 irn_req = ops->cg->reg_param_req[get_Proj_proj(irn)];
133                 assert(irn_req && "missing requirement for regparam");
134                 memcpy(req, &(irn_req->req), sizeof(*req));
135                 return req;
136         }
137         else if (is_Proj(irn)) {
138                 if (pos == -1) {
139                         node_pos = ia32_translate_proj_pos(irn);
140                 }
141                 else {
142                         node_pos = pos;
143                 }
144
145                 irn = my_skip_proj(irn);
146
147                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
148         }
149
150         if (is_ia32_irn(irn)) {
151                 if (pos >= 0) {
152                         irn_req = get_ia32_in_req(irn, pos);
153                 }
154                 else {
155                         irn_req = get_ia32_out_req(irn, node_pos);
156                 }
157
158                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
159
160                 memcpy(req, &(irn_req->req), sizeof(*req));
161
162                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
163                         arch_register_req_is(&(irn_req->req), should_be_different)) {
164                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
165                         req->other = get_irn_n(irn, irn_req->pos);
166                 }
167         }
168         else {
169                 /* treat Phi like Const with default requirements */
170                 if (is_Phi(irn)) {
171                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
172                         if (mode_is_float(mode))
173                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
174                         else if (mode_is_int(mode) || mode_is_reference(mode))
175                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
176                         else if (mode == mode_T || mode == mode_M) {
177                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
178                                 return NULL;
179                         }
180                         else
181                                 assert(0 && "unsupported Phi-Mode");
182                 }
183                 else if (is_Start(irn)) {
184                         DB((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
185                         switch (node_pos) {
186                                 case pn_Start_X_initial_exec:
187                                 case pn_Start_P_value_arg_base:
188                                 case pn_Start_P_globals:
189                                 case pn_Start_P_frame_base:
190                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
191                                         break;
192                                 case pn_Start_T_args:
193                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
194                         }
195                 }
196                 else if (get_irn_op(irn) == op_Return && pos > 0) {
197                         DB((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
198                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
199                 }
200                 else {
201                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
202                         req = NULL;
203                 }
204         }
205
206         return req;
207 }
208
209 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
210         int pos = 0;
211
212         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
213                 is_P_frame_base_Proj(irn)                   ||
214                 is_Start_Proj(irn))
215         {
216                 /* don't skip the proj, we want to take the else below */
217         }
218         else if (is_Proj(irn)) {
219                 pos = ia32_translate_proj_pos(irn);
220                 irn = my_skip_proj(irn);
221         }
222
223         if (is_ia32_irn(irn)) {
224                 const arch_register_t **slots;
225
226                 slots      = get_ia32_slots(irn);
227                 slots[pos] = reg;
228         }
229         else {
230                 ia32_set_firm_reg(irn, reg, cur_reg_set);
231         }
232 }
233
234 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
235         int pos = 0;
236         const arch_register_t *reg = NULL;
237
238         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
239                 is_P_frame_base_Proj(irn)                   ||
240                 is_Start_Proj(irn))
241         {
242                 /* don't skip the proj, we want to take the else below */
243         }
244         else if (is_Proj(irn)) {
245                 pos = ia32_translate_proj_pos(irn);
246                 irn = my_skip_proj(irn);
247         }
248
249         if (is_ia32_irn(irn)) {
250                 const arch_register_t **slots;
251                 slots = get_ia32_slots(irn);
252                 reg   = slots[pos];
253         }
254         else {
255                 reg = ia32_get_firm_reg(irn, cur_reg_set);
256         }
257
258         return reg;
259 }
260
261 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
262         irn = my_skip_proj(irn);
263         if (is_cfop(irn))
264                 return arch_irn_class_branch;
265         else if (is_ia32_Call(irn))
266                 return arch_irn_class_call;
267         else if (is_ia32_irn(irn))
268                 return arch_irn_class_normal;
269         else
270                 return 0;
271 }
272
273 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
274         irn = my_skip_proj(irn);
275         if (is_ia32_irn(irn))
276                 return get_ia32_flags(irn);
277         else {
278                 if (is_Start_Proj(irn))
279                         return arch_irn_flags_ignore;
280
281                 return 0;
282         }
283 }
284
285 /* fill register allocator interface */
286
287 static const arch_irn_ops_if_t ia32_irn_ops_if = {
288         ia32_get_irn_reg_req,
289         ia32_set_irn_reg,
290         ia32_get_irn_reg,
291         ia32_classify,
292         ia32_get_flags
293 };
294
295 ia32_irn_ops_t ia32_irn_ops = {
296         &ia32_irn_ops_if,
297         NULL
298 };
299
300
301
302 /**************************************************
303  *                _                         _  __
304  *               | |                       (_)/ _|
305  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
306  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
307  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
308  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
309  *                        __/ |
310  *                       |___/
311  **************************************************/
312
313 static void check_for_alloca(ir_node *irn, void *env) {
314         int *has_alloca = env;
315
316         if (get_irn_op(irn) == op_Alloc) {
317                 if (get_Alloc_where(irn) == stack_alloc) {
318                         *has_alloca = 1;
319                 }
320         }
321 }
322
323 /**
324  * Transforms the standard firm graph into
325  * an ia32 firm graph
326  */
327 static void ia32_prepare_graph(void *self) {
328         ia32_code_gen_t *cg = self;
329
330         if (! is_pseudo_ir_graph(cg->irg)) {
331                 /* If there is a alloca in the irg, we use %ebp for stack addressing */
332                 /* instead of %esp, as alloca destroys %esp.                         */
333
334                 cg->has_alloca = 0;
335
336                 /* check for alloca node */
337                 irg_walk_blkwise_graph(cg->irg, check_for_alloca, NULL, &(cg->has_alloca));
338
339                 if (cg->has_alloca) {
340                         ia32_general_purpose_regs[REG_EBP].type = arch_register_type_ignore;
341                 }
342
343                 irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
344         }
345 }
346
347
348
349 /**
350  * Stack reservation and StackParam lowering.
351  */
352 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
353         firm_dbg_module_t *mod       = cg->mod;
354         ir_node           *frame     = get_irg_frame(irg);
355         ir_node           *end_block = get_irg_end_block(irg);
356         ir_node          **returns, **in, **new_in;
357         ir_node           *stack_reserve, *sched_point;
358         ir_node           *stack_free, *new_ret, *return_block;
359         int                stack_size = 0, i, n_arg;
360         arch_register_t   *stack_reg;
361         tarval            *stack_size_tv;
362         dbg_info          *frame_dbg;
363
364         /* Determine stack register */
365         if (cg->has_alloca) {
366                 stack_reg = &ia32_general_purpose_regs[REG_EBP];
367         }
368         else {
369                 stack_reg = &ia32_general_purpose_regs[REG_ESP];
370         }
371
372         /* If frame is used, then we need to reserve some stackspace. */
373         if (get_irn_n_edges(frame) > 0) {
374                 /* The initial stack reservation. */
375                 stack_size    = get_type_size_bytes(get_irg_frame_type(irg));
376                 frame_dbg     = get_irn_dbg_info(frame);
377                 stack_reserve = new_rd_ia32_Sub_i(frame_dbg, irg, get_nodes_block(frame), new_NoMem(), mode_Is);
378                 stack_size_tv = new_tarval_from_long(stack_size, mode_Is);
379                 set_ia32_Immop_tarval(stack_reserve, stack_size_tv);
380
381                 assert(stack_size && "bOrken stack layout");
382
383                 /* reroute all edges from frame pointer to corrected frame pointer */
384                 edges_reroute(frame, stack_reserve, irg);
385                 set_irn_n(stack_reserve, 0, frame);
386
387                 /* schedule frame pointer */
388                 if (! sched_is_scheduled(frame)) {
389                         sched_add_after(get_irg_start(irg), frame);
390                 }
391
392                 /* set register */
393                 arch_set_irn_register(cg->arch_env, frame, stack_reg);
394                 arch_set_irn_register(cg->arch_env, stack_reserve, stack_reg);
395
396                 /* insert into schedule */
397                 sched_add_after(frame, stack_reserve);
398
399                 /* Free stack for each Return node */
400                 returns = get_Block_cfgpred_arr(end_block);
401                 for (i = 0; i < get_Block_n_cfgpreds(end_block); i++) {
402                         assert(get_irn_opcode(returns[i]) == iro_Return && "cfgpred of endblock is not a return");
403
404                         return_block = get_nodes_block(returns[i]);
405
406                         /* free the stack */
407                         stack_free = new_rd_ia32_Add_i(frame_dbg, irg, return_block, stack_reserve, mode_Is);
408                         set_ia32_Immop_tarval(stack_free, stack_size_tv);
409                         arch_set_irn_register(cg->arch_env, stack_free, stack_reg);
410
411                         DBG((mod, LEVEL_1, "examining %+F, %+F created, block %+F", returns[i], stack_free, return_block));
412
413                         /* get the old Return arguments */
414                         n_arg  = get_Return_n_ress(returns[i]);
415                         in     = get_Return_res_arr(returns[i]);
416                         new_in = xmalloc((n_arg + 2) * sizeof(new_in[0]));
417
418                         if (!new_in) {
419                                 printf("\nMUAAAAHAHAHAHAHAHAHAH\n");
420                                 exit(1);
421                         }
422
423                         /* copy the old to the new in's */
424                         memcpy(new_in, in, n_arg * sizeof(in[0]));
425                         new_in[n_arg++] = stack_free;
426                         new_in[n_arg++] = get_Return_mem(returns[i]);
427
428                         /* create the new return node */
429                         new_ret = new_rd_ia32_Return(get_irn_dbg_info(returns[i]), irg, return_block, n_arg, new_in);
430
431                         /* In case the return node is the only node in the block, */
432                         /* it is not scheduled, so we need this work-around.      */
433                         if (! sched_is_scheduled(returns[i])) {
434                                 sched_point = return_block;
435                         }
436                         else {
437                                 sched_point = sched_prev(returns[i]);
438                                 sched_remove(returns[i]);
439                         }
440
441                         /* exchange the old return with the new one */
442                         exchange(returns[i], new_ret);
443
444                         DB((mod, LEVEL_1, " ... replaced with %+F\n", new_ret));
445
446                         /* remove the old one from schedule and add the new nodes properly */
447                         sched_add_after(sched_point, new_ret);
448                         sched_add_after(sched_point, stack_free);
449                 }
450         }
451 }
452
453
454
455 /**
456  * Dummy functions for hooks we don't need but which must be filled.
457  */
458 static void ia32_before_sched(void *self) {
459         ia32_code_gen_t *cg = self;
460
461         lower_nodes_before_sched(cg->irg, cg->arch_env);
462 }
463
464 static void ia32_before_ra(void *self) {
465 }
466
467
468 /**
469  * Creates a Store for a Spill
470  */
471 static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
472         ia32_code_gen_t *cg    = self;
473         dbg_info        *dbg   = get_irn_dbg_info(spill);
474         ir_node         *block = get_nodes_block(spill);
475         ir_node         *ptr   = get_irg_frame(cg->irg);
476         ir_node         *val   = be_get_Spill_context(spill);
477         ir_node         *mem   = new_rd_NoMem(cg->irg);
478         ir_mode         *mode  = get_irn_mode(spill);
479         ir_node         *res;
480         entity          *ent   = be_get_spill_entity(spill);
481         unsigned         offs  = get_entity_offset_bytes(ent);
482
483         DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
484
485         res = new_rd_ia32_Store(dbg, cg->irg, block, ptr, val, mem, mode);
486         set_ia32_am_offs(res, new_tarval_from_long(offs, mode_Iu));
487
488         return res;
489 }
490
491 /**
492  * Create a Load for a Spill
493  */
494 static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
495         ia32_code_gen_t *cg    = self;
496         dbg_info        *dbg   = get_irn_dbg_info(reload);
497         ir_node         *block = get_nodes_block(reload);
498         ir_node         *ptr   = get_irg_frame(cg->irg);
499         ir_mode         *mode  = get_irn_mode(reload);
500         ir_node         *pred  = get_irn_n(reload, 0);
501         tarval          *tv;
502         ir_node         *res;
503
504         if (be_is_Spill(pred)) {
505                 entity   *ent  = be_get_spill_entity(pred);
506                 unsigned  offs = get_entity_offset_bytes(ent);
507                 DB((cg->mod, LEVEL_1, "lower_reload: got offset %d for %+F\n", offs, ent));
508                 tv = new_tarval_from_long(offs, mode_Iu);
509         }
510         else if (is_ia32_Store(pred)) {
511                 tv = get_ia32_am_offs(pred);
512         }
513         else {
514                 assert(0 && "unsupported Reload predecessor");
515         }
516
517         res = new_rd_ia32_Load(dbg, cg->irg, block, ptr, pred, mode);
518         set_ia32_am_offs(res, tv);
519
520         return res;
521 }
522
523 /**
524  * Return the stack register for this irg.
525  */
526 static const arch_register_t *ia32_get_stack_register(void *self) {
527         ia32_code_gen_t *cg = self;
528
529         if (cg->has_alloca) {
530                 return &ia32_general_purpose_regs[REG_EBP];
531         }
532
533         return &ia32_general_purpose_regs[REG_ESP];
534 }
535
536 /**
537  * Emits the code, closes the output file and frees
538  * the code generator interface.
539  */
540 static void ia32_codegen(void *self) {
541         ia32_code_gen_t *cg = self;
542         ir_graph       *irg = cg->irg;
543         FILE           *out = cg->out;
544
545         if (cg->emit_decls) {
546                 ia32_gen_decls(cg->out);
547                 cg->emit_decls = 0;
548         }
549
550         ia32_finish_irg(irg, cg);
551         dump_ir_block_graph_sched(irg, "-finished");
552         ia32_gen_routine(out, irg, cg);
553
554         cur_reg_set = NULL;
555
556         /* de-allocate code generator */
557         del_set(cg->reg_set);
558         free(self);
559 }
560
561 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
562
563 static const arch_code_generator_if_t ia32_code_gen_if = {
564         ia32_cg_init,
565         ia32_prepare_graph,
566         ia32_before_sched,   /* before scheduling hook */
567         ia32_before_ra,      /* before register allocation hook */
568         ia32_lower_spill,
569         ia32_lower_reload,
570         ia32_get_stack_register,
571         ia32_codegen         /* emit && done */
572 };
573
574 /**
575  * Initializes the code generator.
576  */
577 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
578         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
579         ia32_code_gen_t *cg  = xmalloc(sizeof(*cg));
580
581         cg->impl       = &ia32_code_gen_if;
582         cg->irg        = irg;
583         cg->reg_set    = new_set(ia32_cmp_irn_reg_assoc, 1024);
584         cg->mod        = firm_dbg_register("firm.be.ia32.cg");
585         cg->out        = F;
586         cg->arch_env   = arch_env;
587
588         isa->num_codegens++;
589
590         if (isa->num_codegens > 1)
591                 cg->emit_decls = 0;
592         else
593                 cg->emit_decls = 1;
594
595         cur_reg_set = cg->reg_set;
596
597         ia32_irn_ops.cg = cg;
598
599         return (arch_code_generator_t *)cg;
600 }
601
602
603
604 /*****************************************************************
605  *  ____             _                  _   _____  _____
606  * |  _ \           | |                | | |_   _|/ ____|  /\
607  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
608  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
609  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
610  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
611  *
612  *****************************************************************/
613
614 /**
615  * Initializes the backend ISA and opens the output file.
616  */
617 static void *ia32_init(void) {
618         static int inited = 0;
619         ia32_isa_t *isa   = xmalloc(sizeof(*isa));
620
621         isa->impl = &ia32_isa_if;
622
623         if(inited)
624                 return NULL;
625
626         inited = 1;
627
628         isa->num_codegens    = 0;
629         isa->reg_projnum_map = new_set(ia32_cmp_reg_projnum_assoc, 1024);
630
631         ia32_register_init(isa);
632         ia32_create_opcodes();
633
634         return isa;
635 }
636
637
638
639 /**
640  * Closes the output file and frees the ISA structure.
641  */
642 static void ia32_done(void *self) {
643         free(self);
644 }
645
646
647
648 static int ia32_get_n_reg_class(const void *self) {
649         return N_CLASSES;
650 }
651
652 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
653         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
654         return &ia32_reg_classes[i];
655 }
656
657 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
658         return &ia32_irn_ops;
659 }
660
661 const arch_irn_handler_t ia32_irn_handler = {
662         ia32_get_irn_ops
663 };
664
665 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
666         return &ia32_irn_handler;
667 }
668
669 long ia32_handle_call_proj(const void *self, ir_node *proj, int is_keep) {
670         ia32_isa_t *isa = (ia32_isa_t *)self;
671         long        pn  = get_Proj_proj(proj);
672
673         if (!is_keep) {
674                 /* It's not a Keep proj, which means, that it is a result proj. */
675                 /* Possible result proj numbers are 0 and 1                     */
676                 /* Set the correct register (depends on the mode) and the       */
677                 /* corresponding proj number                                    */
678                 if (mode_is_float(get_irn_mode(proj))) {
679                         assert(pn == 0 && "only one floating point result supported");
680
681                         /* Get the proj number for the floating point result */
682                         pn = ia32_get_reg_projnum(&ia32_floating_point_regs[REG_XMM0], isa->reg_projnum_map);
683                 }
684                 else {
685                         /* In case of 64bit return value, the result is */
686                         /* in EDX:EAX and we have two result projs.     */
687                         switch (pn) {
688                                 case 0:
689                                         pn = ia32_get_reg_projnum(&ia32_floating_point_regs[REG_EAX], isa->reg_projnum_map);
690                                         break;
691                                 case 1:
692                                         pn = ia32_get_reg_projnum(&ia32_floating_point_regs[REG_EDX], isa->reg_projnum_map);
693                                         break;
694                                 default:
695                                         assert(0 && "only two int results supported");
696                         }
697                 }
698
699                 /* Set the correct proj number */
700                 set_Proj_proj(proj, pn);
701         }
702         else {
703                 /* Set mode to floating point if required */
704                 if (!strcmp(ia32_reg_classes[CLASS_ia32_floating_point].name,
705                                         ia32_projnum_reg_req_map[pn]->req.cls->name)) {
706                         set_irn_mode(proj, mode_F);
707                 }
708         }
709
710         return pn;
711 }
712
713 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
714         return is_ia32_irn(irn);
715 }
716
717 /**
718  * Initializes the code generator interface.
719  */
720 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
721         return &ia32_code_gen_if;
722 }
723
724 list_sched_selector_t ia32_sched_selector;
725
726 /**
727  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
728  */
729 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
730         memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
731         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
732         return &ia32_sched_selector;
733 }
734
735 #ifdef WITH_LIBCORE
736 static void ia32_register_options(lc_opt_entry_t *ent)
737 {
738 }
739 #endif /* WITH_LIBCORE */
740
741 const arch_isa_if_t ia32_isa_if = {
742 #ifdef WITH_LIBCORE
743         ia32_register_options,
744 #endif
745         ia32_init,
746         ia32_done,
747         ia32_get_n_reg_class,
748         ia32_get_reg_class,
749         ia32_get_irn_handler,
750         ia32_get_code_generator_if,
751         ia32_get_list_sched_selector,
752         ia32_handle_call_proj
753 };