fixed indentation, deleted useless comment
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       This is the main ia32 firm backend driver.
23  * @author      Christian Wuerdig
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "lc_opts.h"
31 #include "lc_opts_enum.h"
32
33 #include <math.h>
34
35 #include "pseudo_irg.h"
36 #include "irarch.h"
37 #include "irgwalk.h"
38 #include "irprog.h"
39 #include "irprintf.h"
40 #include "iredges_t.h"
41 #include "ircons.h"
42 #include "irgmod.h"
43 #include "irgopt.h"
44 #include "irbitset.h"
45 #include "irgopt.h"
46 #include "irdump_grgen.h"
47 #include "pdeq.h"
48 #include "pset.h"
49 #include "debug.h"
50 #include "error.h"
51 #include "xmalloc.h"
52 #include "irtools.h"
53 #include "iroptimize.h"
54
55 #include "../beabi.h"
56 #include "../beirg_t.h"
57 #include "../benode_t.h"
58 #include "../belower.h"
59 #include "../besched_t.h"
60 #include "be.h"
61 #include "../be_t.h"
62 #include "../beirgmod.h"
63 #include "../be_dbgout.h"
64 #include "../beblocksched.h"
65 #include "../bemachine.h"
66 #include "../beilpsched.h"
67 #include "../bespillslots.h"
68 #include "../bemodule.h"
69 #include "../begnuas.h"
70 #include "../bestate.h"
71 #include "../beflags.h"
72
73 #include "bearch_ia32_t.h"
74
75 #include "ia32_new_nodes.h"
76 #include "gen_ia32_regalloc_if.h"
77 #include "gen_ia32_machine.h"
78 #include "ia32_transform.h"
79 #include "ia32_emitter.h"
80 #include "ia32_map_regs.h"
81 #include "ia32_optimize.h"
82 #include "ia32_x87.h"
83 #include "ia32_dbg_stat.h"
84 #include "ia32_finish.h"
85 #include "ia32_util.h"
86 #include "ia32_fpu.h"
87 #include "ia32_architecture.h"
88
89 #ifdef FIRM_GRGEN_BE
90 #include "ia32_pbqp_transform.h"
91 #endif
92
93 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
94
95 /* TODO: ugly */
96 static set *cur_reg_set = NULL;
97
98 ir_mode         *mode_fpcw       = NULL;
99 ia32_code_gen_t *ia32_current_cg = NULL;
100
101 /**
102  * The environment for the intrinsic mapping.
103  */
104 static ia32_intrinsic_env_t intrinsic_env = {
105         NULL,    /* the isa */
106         NULL,    /* the irg, these entities belong to */
107         NULL,    /* entity for first div operand (move into FPU) */
108         NULL,    /* entity for second div operand (move into FPU) */
109         NULL,    /* entity for converts ll -> d */
110         NULL,    /* entity for converts d -> ll */
111         NULL,    /* entity for __divdi3 library call */
112         NULL,    /* entity for __moddi3 library call */
113         NULL,    /* entity for __udivdi3 library call */
114         NULL,    /* entity for __umoddi3 library call */
115         NULL,    /* bias value for conversion from float to unsigned 64 */
116 };
117
118
119 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_graph *irg, ir_node *block);
120
121 static INLINE ir_node *create_const(ia32_code_gen_t *cg, ir_node **place,
122                                     create_const_node_func func,
123                                     const arch_register_t* reg)
124 {
125         ir_node *block, *res;
126
127         if(*place != NULL)
128                 return *place;
129
130         block = get_irg_start_block(cg->irg);
131         res = func(NULL, cg->irg, block);
132         arch_set_irn_register(cg->arch_env, res, reg);
133         *place = res;
134
135         add_irn_dep(get_irg_end(cg->irg), res);
136         /* add_irn_dep(get_irg_start(cg->irg), res); */
137
138         return res;
139 }
140
141 /* Creates the unique per irg GP NoReg node. */
142 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
143         return create_const(cg, &cg->noreg_gp, new_rd_ia32_NoReg_GP,
144                             &ia32_gp_regs[REG_GP_NOREG]);
145 }
146
147 ir_node *ia32_new_NoReg_vfp(ia32_code_gen_t *cg) {
148         return create_const(cg, &cg->noreg_vfp, new_rd_ia32_NoReg_VFP,
149                             &ia32_vfp_regs[REG_VFP_NOREG]);
150 }
151
152 ir_node *ia32_new_NoReg_xmm(ia32_code_gen_t *cg) {
153         return create_const(cg, &cg->noreg_xmm, new_rd_ia32_NoReg_XMM,
154                             &ia32_xmm_regs[REG_XMM_NOREG]);
155 }
156
157 ir_node *ia32_new_Unknown_gp(ia32_code_gen_t *cg) {
158         return create_const(cg, &cg->unknown_gp, new_rd_ia32_Unknown_GP,
159                             &ia32_gp_regs[REG_GP_UKNWN]);
160 }
161
162 ir_node *ia32_new_Unknown_vfp(ia32_code_gen_t *cg) {
163         return create_const(cg, &cg->unknown_vfp, new_rd_ia32_Unknown_VFP,
164                             &ia32_vfp_regs[REG_VFP_UKNWN]);
165 }
166
167 ir_node *ia32_new_Unknown_xmm(ia32_code_gen_t *cg) {
168         return create_const(cg, &cg->unknown_xmm, new_rd_ia32_Unknown_XMM,
169                             &ia32_xmm_regs[REG_XMM_UKNWN]);
170 }
171
172 ir_node *ia32_new_Fpu_truncate(ia32_code_gen_t *cg) {
173         return create_const(cg, &cg->fpu_trunc_mode, new_rd_ia32_ChangeCW,
174                         &ia32_fp_cw_regs[REG_FPCW]);
175 }
176
177
178 /**
179  * Returns gp_noreg or fp_noreg, depending in input requirements.
180  */
181 ir_node *ia32_get_admissible_noreg(ia32_code_gen_t *cg, ir_node *irn, int pos) {
182         const arch_register_req_t *req;
183
184         req = arch_get_register_req(cg->arch_env, irn, pos);
185         assert(req != NULL && "Missing register requirements");
186         if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
187                 return ia32_new_NoReg_gp(cg);
188
189         if (ia32_cg_config.use_sse2) {
190                 return ia32_new_NoReg_xmm(cg);
191         } else {
192                 return ia32_new_NoReg_vfp(cg);
193         }
194 }
195
196 /**************************************************
197  *                         _ _              _  __
198  *                        | | |            (_)/ _|
199  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
200  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
201  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
202  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
203  *            __/ |
204  *           |___/
205  **************************************************/
206
207 /**
208  * Return register requirements for an ia32 node.
209  * If the node returns a tuple (mode_T) then the proj's
210  * will be asked for this information.
211  */
212 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self,
213                                                        const ir_node *node,
214                                                                                                            int pos)
215 {
216         long node_pos = pos == -1 ? 0 : pos;
217         ir_mode *mode     = is_Block(node) ? NULL : get_irn_mode(node);
218         (void) self;
219
220         if (is_Block(node) || mode == mode_X) {
221                 return arch_no_register_req;
222         }
223
224         if (mode == mode_T && pos < 0) {
225                 return arch_no_register_req;
226         }
227
228         if (is_Proj(node)) {
229                 if(mode == mode_M)
230                         return arch_no_register_req;
231
232                 if(pos >= 0) {
233                         return arch_no_register_req;
234                 }
235
236                 node_pos = (pos == -1) ? get_Proj_proj(node) : pos;
237                 node     = skip_Proj_const(node);
238         }
239
240         if (is_ia32_irn(node)) {
241                 const arch_register_req_t *req;
242                 if(pos >= 0)
243                         req = get_ia32_in_req(node, pos);
244                 else
245                         req = get_ia32_out_req(node, node_pos);
246
247                 assert(req != NULL);
248
249                 return req;
250         }
251
252         /* unknowns should be transformed already */
253         assert(!is_Unknown(node));
254
255         return arch_no_register_req;
256 }
257
258 static void ia32_set_irn_reg(const void *self, ir_node *irn,
259                              const arch_register_t *reg)
260 {
261         int                   pos = 0;
262         (void) self;
263
264         if (get_irn_mode(irn) == mode_X) {
265                 return;
266         }
267
268         if (is_Proj(irn)) {
269                 pos = get_Proj_proj(irn);
270                 irn = skip_Proj(irn);
271         }
272
273         if (is_ia32_irn(irn)) {
274                 const arch_register_t **slots;
275
276                 slots      = get_ia32_slots(irn);
277                 slots[pos] = reg;
278         } else {
279                 ia32_set_firm_reg(irn, reg, cur_reg_set);
280         }
281 }
282
283 static const arch_register_t *ia32_get_irn_reg(const void *self,
284                                                const ir_node *irn)
285 {
286         int pos = 0;
287         const arch_register_t *reg = NULL;
288         (void) self;
289
290         if (is_Proj(irn)) {
291
292                 if (get_irn_mode(irn) == mode_X) {
293                         return NULL;
294                 }
295
296                 pos = get_Proj_proj(irn);
297                 irn = skip_Proj_const(irn);
298         }
299
300         if (is_ia32_irn(irn)) {
301                 const arch_register_t **slots;
302                 slots = get_ia32_slots(irn);
303                 assert(pos < get_ia32_n_res(irn));
304                 reg   = slots[pos];
305         } else {
306                 reg = ia32_get_firm_reg(irn, cur_reg_set);
307         }
308
309         return reg;
310 }
311
312 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
313         arch_irn_class_t classification = arch_irn_class_normal;
314         (void) self;
315
316         irn = skip_Proj_const(irn);
317
318         if (is_cfop(irn))
319                 classification |= arch_irn_class_branch;
320
321         if (! is_ia32_irn(irn))
322                 return classification & ~arch_irn_class_normal;
323
324         if (is_ia32_Ld(irn))
325                 classification |= arch_irn_class_load;
326
327         if (is_ia32_St(irn))
328                 classification |= arch_irn_class_store;
329
330         if (is_ia32_need_stackent(irn))
331                 classification |= arch_irn_class_reload;
332
333         return classification;
334 }
335
336 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
337         arch_irn_flags_t flags = arch_irn_flags_none;
338         (void) self;
339
340         if (is_Unknown(irn))
341                 return arch_irn_flags_ignore;
342
343         if(is_Proj(irn) && mode_is_datab(get_irn_mode(irn))) {
344                 ir_node *pred = get_Proj_pred(irn);
345
346                 if(is_ia32_irn(pred)) {
347                         flags = get_ia32_out_flags(pred, get_Proj_proj(irn));
348                 }
349
350                 irn = pred;
351         }
352
353         if (is_ia32_irn(irn)) {
354                 flags |= get_ia32_flags(irn);
355         }
356
357         return flags;
358 }
359
360 /**
361  * The IA32 ABI callback object.
362  */
363 typedef struct {
364         be_abi_call_flags_bits_t flags;  /**< The call flags. */
365         const arch_isa_t *isa;           /**< The ISA handle. */
366         const arch_env_t *aenv;          /**< The architecture environment. */
367         ir_graph *irg;                   /**< The associated graph. */
368 } ia32_abi_env_t;
369
370 static ir_entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
371         (void) self;
372         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
373 }
374
375 static void ia32_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent) {
376         (void) self;
377         set_ia32_frame_ent(irn, ent);
378 }
379
380 static void ia32_set_frame_offset(const void *self, ir_node *irn, int bias) {
381         const ia32_irn_ops_t *ops = self;
382
383         if (get_ia32_frame_ent(irn)) {
384                 if (is_ia32_Pop(irn)) {
385                         int omit_fp = be_abi_omit_fp(ops->cg->birg->abi);
386                         if (omit_fp) {
387                                 /* Pop nodes modify the stack pointer before calculating the destination
388                                  * address, so fix this here
389                                  */
390                                 bias -= 4;
391                         }
392                 }
393
394                 add_ia32_am_offs_int(irn, bias);
395         }
396 }
397
398 static int ia32_get_sp_bias(const void *self, const ir_node *node)
399 {
400         (void) self;
401
402         if (is_ia32_Push(node))
403                 return 4;
404
405         if (is_ia32_Pop(node))
406                 return -4;
407
408         return 0;
409 }
410
411 /**
412  * Put all registers which are saved by the prologue/epilogue in a set.
413  *
414  * @param self  The callback object.
415  * @param s     The result set.
416  */
417 static void ia32_abi_dont_save_regs(void *self, pset *s)
418 {
419         ia32_abi_env_t *env = self;
420         if(env->flags.try_omit_fp)
421                 pset_insert_ptr(s, env->isa->bp);
422 }
423
424 /**
425  * Generate the routine prologue.
426  *
427  * @param self    The callback object.
428  * @param mem     A pointer to the mem node. Update this if you define new memory.
429  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
430  *
431  * @return        The register which shall be used as a stack frame base.
432  *
433  * All nodes which define registers in @p reg_map must keep @p reg_map current.
434  */
435 static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
436 {
437         ia32_abi_env_t *env = self;
438         const ia32_isa_t *isa     = (ia32_isa_t *)env->isa;
439         ia32_code_gen_t *cg = isa->cg;
440
441         if (! env->flags.try_omit_fp) {
442                 ir_node *bl      = get_irg_start_block(env->irg);
443                 ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
444                 ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
445                 ir_node *noreg = ia32_new_NoReg_gp(cg);
446                 ir_node *push;
447
448                 /* ALL nodes representing bp must be set to ignore. */
449                 be_node_set_flags(get_Proj_pred(curr_bp), BE_OUT_POS(get_Proj_proj(curr_bp)), arch_irn_flags_ignore);
450
451                 /* push ebp */
452                 push    = new_rd_ia32_Push(NULL, env->irg, bl, noreg, noreg, *mem, curr_sp, curr_bp);
453                 curr_sp = new_r_Proj(env->irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
454                 *mem    = new_r_Proj(env->irg, bl, push, mode_M, pn_ia32_Push_M);
455
456                 /* the push must have SP out register */
457                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
458                 set_ia32_flags(push, arch_irn_flags_ignore);
459
460                 /* move esp to ebp */
461                 curr_bp  = be_new_Copy(env->isa->bp->reg_class, env->irg, bl, curr_sp);
462                 be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), env->isa->bp);
463                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
464                 be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
465
466                 /* beware: the copy must be done before any other sp use */
467                 curr_sp = be_new_CopyKeep_single(env->isa->sp->reg_class, env->irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
468                 be_set_constr_single_reg(curr_sp, BE_OUT_POS(0), env->isa->sp);
469                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
470                 be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
471
472                 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
473                 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
474
475                 return env->isa->bp;
476         }
477
478         return env->isa->sp;
479 }
480
481 /**
482  * Generate the routine epilogue.
483  * @param self    The callback object.
484  * @param bl      The block for the epilog
485  * @param mem     A pointer to the mem node. Update this if you define new memory.
486  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
487  * @return        The register which shall be used as a stack frame base.
488  *
489  * All nodes which define registers in @p reg_map must keep @p reg_map current.
490  */
491 static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
492 {
493         ia32_abi_env_t *env     = self;
494         ir_node        *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
495         ir_node        *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
496
497         if (env->flags.try_omit_fp) {
498                 /* simply remove the stack frame here */
499                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK);
500                 add_irn_dep(curr_sp, *mem);
501         } else {
502                 const ia32_isa_t *isa     = (ia32_isa_t *)env->isa;
503                 ia32_code_gen_t *cg = isa->cg;
504                 ir_mode         *mode_bp = env->isa->bp->reg_class->mode;
505                 ir_graph        *irg     = current_ir_graph;
506
507                 if (ia32_cg_config.use_leave) {
508                         ir_node *leave;
509
510                         /* leave */
511                         leave   = new_rd_ia32_Leave(NULL, irg, bl, curr_sp, curr_bp);
512                         set_ia32_flags(leave, arch_irn_flags_ignore);
513                         curr_bp = new_r_Proj(irg, bl, leave, mode_bp, pn_ia32_Leave_frame);
514                         curr_sp = new_r_Proj(irg, bl, leave, get_irn_mode(curr_sp), pn_ia32_Leave_stack);
515                 } else {
516                         ir_node *noreg = ia32_new_NoReg_gp(cg);
517                         ir_node *pop;
518
519                         /* the old SP is not needed anymore (kill the proj) */
520                         assert(is_Proj(curr_sp));
521                         be_kill_node(curr_sp);
522
523                         /* copy ebp to esp */
524                         curr_sp = be_new_Copy(&ia32_reg_classes[CLASS_ia32_gp], irg, bl, curr_bp);
525                         arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
526                         be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
527
528                         /* pop ebp */
529                         pop     = new_rd_ia32_Pop(NULL, env->irg, bl, noreg, noreg, *mem, curr_sp);
530                         set_ia32_flags(pop, arch_irn_flags_ignore);
531                         curr_bp = new_r_Proj(irg, bl, pop, mode_bp, pn_ia32_Pop_res);
532                         curr_sp = new_r_Proj(irg, bl, pop, get_irn_mode(curr_sp), pn_ia32_Pop_stack);
533
534                         *mem = new_r_Proj(irg, bl, pop, mode_M, pn_ia32_Pop_M);
535                 }
536                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
537                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
538         }
539
540         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
541         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
542 }
543
544 /**
545  * Initialize the callback object.
546  * @param call The call object.
547  * @param aenv The architecture environment.
548  * @param irg  The graph with the method.
549  * @return     Some pointer. This pointer is passed to all other callback functions as self object.
550  */
551 static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
552 {
553         ia32_abi_env_t *env    = xmalloc(sizeof(env[0]));
554         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
555         env->flags = fl.bits;
556         env->irg   = irg;
557         env->aenv  = aenv;
558         env->isa   = aenv->isa;
559         return env;
560 }
561
562 /**
563  * Destroy the callback object.
564  * @param self The callback object.
565  */
566 static void ia32_abi_done(void *self) {
567         free(self);
568 }
569
570 /**
571  * Produces the type which sits between the stack args and the locals on the stack.
572  * it will contain the return address and space to store the old base pointer.
573  * @return The Firm type modeling the ABI between type.
574  */
575 static ir_type *ia32_abi_get_between_type(void *self)
576 {
577 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
578         static ir_type *omit_fp_between_type = NULL;
579         static ir_type *between_type         = NULL;
580
581         ia32_abi_env_t *env = self;
582
583         if (! between_type) {
584                 ir_entity *old_bp_ent;
585                 ir_entity *ret_addr_ent;
586                 ir_entity *omit_fp_ret_addr_ent;
587
588                 ir_type *old_bp_type   = new_type_primitive(IDENT("bp"), mode_Iu);
589                 ir_type *ret_addr_type = new_type_primitive(IDENT("return_addr"), mode_Iu);
590
591                 between_type           = new_type_struct(IDENT("ia32_between_type"));
592                 old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
593                 ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
594
595                 set_entity_offset(old_bp_ent, 0);
596                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
597                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
598                 set_type_state(between_type, layout_fixed);
599
600                 omit_fp_between_type = new_type_struct(IDENT("ia32_between_type_omit_fp"));
601                 omit_fp_ret_addr_ent = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
602
603                 set_entity_offset(omit_fp_ret_addr_ent, 0);
604                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
605                 set_type_state(omit_fp_between_type, layout_fixed);
606         }
607
608         return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
609 #undef IDENT
610 }
611
612 /**
613  * Get the estimated cycle count for @p irn.
614  *
615  * @param self The this pointer.
616  * @param irn  The node.
617  *
618  * @return     The estimated cycle count for this operation
619  */
620 static int ia32_get_op_estimated_cost(const void *self, const ir_node *irn)
621 {
622         int            cost;
623         ia32_op_type_t op_tp;
624         (void) self;
625
626         if (is_Proj(irn))
627                 return 0;
628         if (!is_ia32_irn(irn))
629                 return 0;
630
631         assert(is_ia32_irn(irn));
632
633         cost  = get_ia32_latency(irn);
634         op_tp = get_ia32_op_type(irn);
635
636         if (is_ia32_CopyB(irn)) {
637                 cost = 250;
638         }
639         else if (is_ia32_CopyB_i(irn)) {
640                 int size = get_ia32_copyb_size(irn);
641                 cost     = 20 + (int)ceil((4/3) * size);
642         }
643         /* in case of address mode operations add additional cycles */
644         else if (op_tp == ia32_AddrModeD || op_tp == ia32_AddrModeS) {
645                 /*
646                         In case of stack access and access to fixed addresses add 5 cycles
647                         (we assume they are in cache), other memory operations cost 20
648                         cycles.
649                 */
650                 if(is_ia32_use_frame(irn) ||
651                                 (is_ia32_NoReg_GP(get_irn_n(irn, 0)) &&
652                          is_ia32_NoReg_GP(get_irn_n(irn, 1)))) {
653                         cost += 5;
654                 } else {
655                         cost += 20;
656                 }
657         }
658
659         return cost;
660 }
661
662 /**
663  * Returns the inverse operation if @p irn, recalculating the argument at position @p i.
664  *
665  * @param irn       The original operation
666  * @param i         Index of the argument we want the inverse operation to yield
667  * @param inverse   struct to be filled with the resulting inverse op
668  * @param obstack   The obstack to use for allocation of the returned nodes array
669  * @return          The inverse operation or NULL if operation invertible
670  */
671 static arch_inverse_t *ia32_get_inverse(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
672         ir_graph *irg;
673         ir_mode  *mode;
674         ir_mode  *irn_mode;
675         ir_node  *block, *noreg, *nomem;
676         dbg_info *dbg;
677         (void) self;
678
679         /* we cannot invert non-ia32 irns */
680         if (! is_ia32_irn(irn))
681                 return NULL;
682
683         /* operand must always be a real operand (not base, index or mem) */
684         if (i != n_ia32_binary_left && i != n_ia32_binary_right)
685                 return NULL;
686
687         /* we don't invert address mode operations */
688         if (get_ia32_op_type(irn) != ia32_Normal)
689                 return NULL;
690
691         /* TODO: adjust for new immediates... */
692         ir_fprintf(stderr, "TODO: fix get_inverse for new immediates (%+F)\n",
693                    irn);
694         return NULL;
695
696         irg      = get_irn_irg(irn);
697         block    = get_nodes_block(irn);
698         mode     = get_irn_mode(irn);
699         irn_mode = get_irn_mode(irn);
700         noreg    = get_irn_n(irn, 0);
701         nomem    = new_r_NoMem(irg);
702         dbg      = get_irn_dbg_info(irn);
703
704         /* initialize structure */
705         inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
706         inverse->costs = 0;
707         inverse->n     = 1;
708
709         switch (get_ia32_irn_opcode(irn)) {
710                 case iro_ia32_Add:
711 #if 0
712                         if (get_ia32_immop_type(irn) == ia32_ImmConst) {
713                                 /* we have an add with a const here */
714                                 /* invers == add with negated const */
715                                 inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
716                                 inverse->costs   += 1;
717                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
718                                 set_ia32_Immop_tarval(inverse->nodes[0], tarval_neg(get_ia32_Immop_tarval(irn)));
719                                 set_ia32_commutative(inverse->nodes[0]);
720                         }
721                         else if (get_ia32_immop_type(irn) == ia32_ImmSymConst) {
722                                 /* we have an add with a symconst here */
723                                 /* invers == sub with const */
724                                 inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
725                                 inverse->costs   += 2;
726                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
727                         }
728                         else {
729                                 /* normal add: inverse == sub */
730                                 inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, i ^ 1));
731                                 inverse->costs   += 2;
732                         }
733 #endif
734                         break;
735                 case iro_ia32_Sub:
736 #if 0
737                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
738                                 /* we have a sub with a const/symconst here */
739                                 /* invers == add with this const */
740                                 inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
741                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
742                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
743                         }
744                         else {
745                                 /* normal sub */
746                                 if (i == n_ia32_binary_left) {
747                                         inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, nomem, (ir_node*) irn, get_irn_n(irn, 3));
748                                 }
749                                 else {
750                                         inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, n_ia32_binary_left), (ir_node*) irn);
751                                 }
752                                 inverse->costs += 1;
753                         }
754 #endif
755                         break;
756                 case iro_ia32_Xor:
757 #if 0
758                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
759                                 /* xor with const: inverse = xor */
760                                 inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, nomem, get_irn_n(irn, i), noreg);
761                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
762                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
763                         }
764                         else {
765                                 /* normal xor */
766                                 inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, nomem, (ir_node *) irn, get_irn_n(irn, i));
767                                 inverse->costs   += 1;
768                         }
769 #endif
770                         break;
771                 case iro_ia32_Not: {
772                         inverse->nodes[0] = new_rd_ia32_Not(dbg, irg, block, (ir_node*) irn);
773                         inverse->costs   += 1;
774                         break;
775                 }
776                 case iro_ia32_Neg: {
777                         inverse->nodes[0] = new_rd_ia32_Neg(dbg, irg, block, (ir_node*) irn);
778                         inverse->costs   += 1;
779                         break;
780                 }
781                 default:
782                         /* inverse operation not supported */
783                         return NULL;
784         }
785
786         return inverse;
787 }
788
789 static ir_mode *get_spill_mode_mode(const ir_mode *mode)
790 {
791         if(mode_is_float(mode))
792                 return mode_D;
793
794         return mode_Iu;
795 }
796
797 /**
798  * Get the mode that should be used for spilling value node
799  */
800 static ir_mode *get_spill_mode(const ir_node *node)
801 {
802         ir_mode *mode = get_irn_mode(node);
803         return get_spill_mode_mode(mode);
804 }
805
806 /**
807  * Checks whether an addressmode reload for a node with mode mode is compatible
808  * with a spillslot of mode spill_mode
809  */
810 static int ia32_is_spillmode_compatible(const ir_mode *mode, const ir_mode *spillmode)
811 {
812         if(mode_is_float(mode)) {
813                 return mode == spillmode;
814         } else {
815                 return 1;
816         }
817 }
818
819 /**
820  * Check if irn can load it's operand at position i from memory (source addressmode).
821  * @param self   Pointer to irn ops itself
822  * @param irn    The irn to be checked
823  * @param i      The operands position
824  * @return Non-Zero if operand can be loaded
825  */
826 static int ia32_possible_memory_operand(const void *self, const ir_node *irn, unsigned int i) {
827         ir_node *op = get_irn_n(irn, i);
828         const ir_mode *mode = get_irn_mode(op);
829         const ir_mode *spillmode = get_spill_mode(op);
830         (void) self;
831
832         if (! is_ia32_irn(irn)                                  ||  /* must be an ia32 irn */
833                 get_ia32_am_arity(irn) != ia32_am_binary              ||  /* must be a binary operation TODO is this necessary? */
834                 get_ia32_op_type(irn) != ia32_Normal                  ||  /* must not already be a addressmode irn */
835                 ! (get_ia32_am_support(irn) & ia32_am_Source)         ||  /* must be capable of source addressmode */
836                 ! ia32_is_spillmode_compatible(mode, spillmode)       ||
837                 (i != n_ia32_binary_left && i != n_ia32_binary_right) || /* a "real" operand position must be requested */
838                 is_ia32_use_frame(irn))                                  /* must not already use frame */
839                 return 0;
840
841         if (i == n_ia32_binary_left) {
842                 const arch_register_req_t *req;
843                 if(!is_ia32_commutative(irn))
844                         return 0;
845                 /* we can't swap left/right for limited registers
846                  * (As this (currently) breaks constraint handling copies)
847                  */
848                 req = get_ia32_in_req(irn, n_ia32_binary_left);
849                 if(req->type & arch_register_req_type_limited) {
850                         return 0;
851                 }
852         }
853
854         return 1;
855 }
856
857 static void ia32_perform_memory_operand(const void *self, ir_node *irn,
858                                         ir_node *spill, unsigned int i)
859 {
860         const ia32_irn_ops_t *ops = self;
861         ia32_code_gen_t      *cg  = ops->cg;
862
863         assert(ia32_possible_memory_operand(self, irn, i) && "Cannot perform memory operand change");
864
865         if (i == n_ia32_binary_left) {
866                 ia32_swap_left_right(irn);
867         }
868
869         set_ia32_op_type(irn, ia32_AddrModeS);
870         set_ia32_ls_mode(irn, get_irn_mode(get_irn_n(irn, i)));
871         set_ia32_use_frame(irn);
872         set_ia32_need_stackent(irn);
873
874         set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
875         set_irn_n(irn, n_ia32_binary_right, ia32_get_admissible_noreg(cg, irn, n_ia32_binary_right));
876         set_irn_n(irn, n_ia32_mem, spill);
877
878         /* immediates are only allowed on the right side */
879         if (i == n_ia32_binary_left && is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_left))) {
880                 ia32_swap_left_right(irn);
881         }
882 }
883
884 static const be_abi_callbacks_t ia32_abi_callbacks = {
885         ia32_abi_init,
886         ia32_abi_done,
887         ia32_abi_get_between_type,
888         ia32_abi_dont_save_regs,
889         ia32_abi_prologue,
890         ia32_abi_epilogue
891 };
892
893 /* fill register allocator interface */
894
895 static const arch_irn_ops_if_t ia32_irn_ops_if = {
896         ia32_get_irn_reg_req,
897         ia32_set_irn_reg,
898         ia32_get_irn_reg,
899         ia32_classify,
900         ia32_get_flags,
901         ia32_get_frame_entity,
902         ia32_set_frame_entity,
903         ia32_set_frame_offset,
904         ia32_get_sp_bias,
905         ia32_get_inverse,
906         ia32_get_op_estimated_cost,
907         ia32_possible_memory_operand,
908         ia32_perform_memory_operand,
909 };
910
911 static ia32_irn_ops_t ia32_irn_ops = {
912         &ia32_irn_ops_if,
913         NULL
914 };
915
916
917
918 /**************************************************
919  *                _                         _  __
920  *               | |                       (_)/ _|
921  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
922  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
923  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
924  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
925  *                        __/ |
926  *                       |___/
927  **************************************************/
928
929 static void ia32_before_abi(void *self) {
930         lower_mode_b_config_t lower_mode_b_config = {
931                 mode_Iu,  /* lowered mode */
932                 mode_Bu,  /* prefered mode for set */
933                 0,        /* don't lower direct compares */
934         };
935         ia32_code_gen_t *cg = self;
936
937         ir_lower_mode_b(cg->irg, &lower_mode_b_config);
938         if (cg->dump)
939                 be_dump(cg->irg, "-lower_modeb", dump_ir_block_graph_sched);
940 }
941
942 /**
943  * Transforms the standard firm graph into
944  * an ia32 firm graph
945  */
946 static void ia32_prepare_graph(void *self) {
947         ia32_code_gen_t *cg = self;
948
949         /* do local optimisations */
950         optimize_graph_df(cg->irg);
951
952         /* TODO: we often have dead code reachable through out-edges here. So for
953          * now we rebuild edges (as we need correct user count for code selection)
954          */
955 #if 1
956         edges_deactivate(cg->irg);
957         edges_activate(cg->irg);
958 #endif
959
960         if (cg->dump)
961                 be_dump(cg->irg, "-pre_transform", dump_ir_block_graph_sched);
962
963 #ifdef FIRM_GRGEN_BE
964         /* transform nodes into assembler instructions by PBQP magic */
965         ia32_transform_graph_by_pbqp(cg);
966 #endif
967
968         if (cg->dump)
969                 be_dump(cg->irg, "-after_pbqp_transform", dump_ir_block_graph_sched);
970
971         /* transform remaining nodes into assembler instructions */
972         ia32_transform_graph(cg);
973
974         /* do local optimisations (mainly CSE) */
975         optimize_graph_df(cg->irg);
976
977         if (cg->dump)
978                 be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
979
980         /* optimize address mode */
981         ia32_optimize_graph(cg);
982
983         if (cg->dump)
984                 be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
985
986         /* do code placement, to optimize the position of constants */
987         place_code(cg->irg);
988
989         if (cg->dump)
990                 be_dump(cg->irg, "-place", dump_ir_block_graph_sched);
991 }
992
993 /**
994  * Dummy functions for hooks we don't need but which must be filled.
995  */
996 static void ia32_before_sched(void *self) {
997         (void) self;
998 }
999
1000 static void turn_back_am(ir_node *node)
1001 {
1002         ir_graph *irg   = current_ir_graph;
1003         dbg_info *dbgi  = get_irn_dbg_info(node);
1004         ir_node  *block = get_nodes_block(node);
1005         ir_node  *base  = get_irn_n(node, n_ia32_base);
1006         ir_node  *index = get_irn_n(node, n_ia32_index);
1007         ir_node  *mem   = get_irn_n(node, n_ia32_mem);
1008         ir_node  *noreg = ia32_new_NoReg_gp(ia32_current_cg);
1009         ir_node  *load;
1010         ir_node  *load_res;
1011         ir_node  *mem_proj;
1012         const ir_edge_t *edge;
1013
1014         load     = new_rd_ia32_Load(dbgi, irg, block, base, index, mem);
1015         load_res = new_rd_Proj(dbgi, irg, block, load, mode_Iu, pn_ia32_Load_res);
1016
1017         ia32_copy_am_attrs(load, node);
1018         set_irn_n(node, n_ia32_mem, new_NoMem());
1019
1020         switch (get_ia32_am_arity(node)) {
1021                 case ia32_am_unary:
1022                         set_irn_n(node, n_ia32_unary_op, load_res);
1023                         break;
1024
1025                 case ia32_am_binary:
1026                         if (is_ia32_Immediate(get_irn_n(node, n_ia32_Cmp_right))) {
1027                                 assert(is_ia32_Cmp(node)  || is_ia32_Cmp8Bit(node) ||
1028                                        is_ia32_Test(node) || is_ia32_Test8Bit(node));
1029                                 set_irn_n(node, n_ia32_binary_left, load_res);
1030                         } else {
1031                                 set_irn_n(node, n_ia32_binary_right, load_res);
1032                         }
1033                         break;
1034
1035                 case ia32_am_ternary:
1036                         set_irn_n(node, n_ia32_binary_right, load_res);
1037                         break;
1038
1039                 default: break;
1040         }
1041         set_irn_n(node, n_ia32_base, noreg);
1042         set_irn_n(node, n_ia32_index, noreg);
1043         set_ia32_am_offs_int(node, 0);
1044         set_ia32_am_sc(node, NULL);
1045         set_ia32_am_scale(node, 0);
1046         clear_ia32_am_sc_sign(node);
1047
1048         /* rewire mem-proj */
1049         if (get_irn_mode(node) == mode_T) {
1050                 mem_proj = NULL;
1051                 foreach_out_edge(node, edge) {
1052                         ir_node *out = get_edge_src_irn(edge);
1053                         if(get_Proj_proj(out) == pn_ia32_mem) {
1054                                 mem_proj = out;
1055                                 break;
1056                         }
1057                 }
1058
1059                 if(mem_proj != NULL) {
1060                         set_Proj_pred(mem_proj, load);
1061                         set_Proj_proj(mem_proj, pn_ia32_Load_M);
1062                 }
1063         }
1064
1065         set_ia32_op_type(node, ia32_Normal);
1066         if (sched_is_scheduled(node))
1067                 sched_add_before(node, load);
1068 }
1069
1070 static ir_node *flags_remat(ir_node *node, ir_node *after)
1071 {
1072         /* we should turn back source address mode when rematerializing nodes */
1073         ia32_op_type_t  type = get_ia32_op_type(node);
1074         ir_node        *block;
1075         ir_node        *copy;
1076
1077         if (is_Block(after)) {
1078                 block = after;
1079         } else {
1080                 block = get_nodes_block(after);
1081         }
1082
1083         switch (type) {
1084                 case ia32_AddrModeS: turn_back_am(node); break;
1085
1086                 case ia32_AddrModeD:
1087                         /* TODO implement this later... */
1088                         panic("found DestAM with flag user %+F this should not happen", node);
1089                         break;
1090
1091                 default: assert(type == ia32_Normal); break;
1092         }
1093
1094         copy = exact_copy(node);
1095         set_nodes_block(copy, block);
1096         sched_add_after(after, copy);
1097
1098         return copy;
1099 }
1100
1101 /**
1102  * Called before the register allocator.
1103  * Calculate a block schedule here. We need it for the x87
1104  * simulator and the emitter.
1105  */
1106 static void ia32_before_ra(void *self) {
1107         ia32_code_gen_t *cg = self;
1108
1109         /* setup fpu rounding modes */
1110         ia32_setup_fpu_mode(cg);
1111
1112         /* fixup flags */
1113         be_sched_fix_flags(cg->birg, &ia32_reg_classes[CLASS_ia32_flags],
1114                            &flags_remat);
1115
1116         ia32_add_missing_keeps(cg);
1117 }
1118
1119
1120 /**
1121  * Transforms a be_Reload into a ia32 Load.
1122  */
1123 static void transform_to_Load(ia32_code_gen_t *cg, ir_node *node) {
1124         ir_graph *irg        = get_irn_irg(node);
1125         dbg_info *dbg        = get_irn_dbg_info(node);
1126         ir_node *block       = get_nodes_block(node);
1127         ir_entity *ent       = be_get_frame_entity(node);
1128         ir_mode *mode        = get_irn_mode(node);
1129         ir_mode *spillmode   = get_spill_mode(node);
1130         ir_node *noreg       = ia32_new_NoReg_gp(cg);
1131         ir_node *sched_point = NULL;
1132         ir_node *ptr         = get_irg_frame(irg);
1133         ir_node *mem         = get_irn_n(node, be_pos_Reload_mem);
1134         ir_node *new_op, *proj;
1135         const arch_register_t *reg;
1136
1137         if (sched_is_scheduled(node)) {
1138                 sched_point = sched_prev(node);
1139         }
1140
1141         if (mode_is_float(spillmode)) {
1142                 if (ia32_cg_config.use_sse2)
1143                         new_op = new_rd_ia32_xLoad(dbg, irg, block, ptr, noreg, mem, spillmode);
1144                 else
1145                         new_op = new_rd_ia32_vfld(dbg, irg, block, ptr, noreg, mem, spillmode);
1146         }
1147         else if (get_mode_size_bits(spillmode) == 128) {
1148                 // Reload 128 bit sse registers
1149                 new_op = new_rd_ia32_xxLoad(dbg, irg, block, ptr, noreg, mem);
1150         }
1151         else
1152                 new_op = new_rd_ia32_Load(dbg, irg, block, ptr, noreg, mem);
1153
1154         set_ia32_op_type(new_op, ia32_AddrModeS);
1155         set_ia32_ls_mode(new_op, spillmode);
1156         set_ia32_frame_ent(new_op, ent);
1157         set_ia32_use_frame(new_op);
1158
1159         DBG_OPT_RELOAD2LD(node, new_op);
1160
1161         proj = new_rd_Proj(dbg, irg, block, new_op, mode, pn_ia32_Load_res);
1162
1163         if (sched_point) {
1164                 sched_add_after(sched_point, new_op);
1165                 sched_remove(node);
1166         }
1167
1168         /* copy the register from the old node to the new Load */
1169         reg = arch_get_irn_register(cg->arch_env, node);
1170         arch_set_irn_register(cg->arch_env, new_op, reg);
1171
1172         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(cg, node));
1173
1174         exchange(node, proj);
1175 }
1176
1177 /**
1178  * Transforms a be_Spill node into a ia32 Store.
1179  */
1180 static void transform_to_Store(ia32_code_gen_t *cg, ir_node *node) {
1181         ir_graph *irg  = get_irn_irg(node);
1182         dbg_info *dbg  = get_irn_dbg_info(node);
1183         ir_node *block = get_nodes_block(node);
1184         ir_entity *ent = be_get_frame_entity(node);
1185         const ir_node *spillval = get_irn_n(node, be_pos_Spill_val);
1186         ir_mode *mode  = get_spill_mode(spillval);
1187         ir_node *noreg = ia32_new_NoReg_gp(cg);
1188         ir_node *nomem = new_rd_NoMem(irg);
1189         ir_node *ptr   = get_irg_frame(irg);
1190         ir_node *val   = get_irn_n(node, be_pos_Spill_val);
1191         ir_node *store;
1192         ir_node *sched_point = NULL;
1193
1194         if (sched_is_scheduled(node)) {
1195                 sched_point = sched_prev(node);
1196         }
1197
1198         /* No need to spill unknown values... */
1199         if(is_ia32_Unknown_GP(val) ||
1200                 is_ia32_Unknown_VFP(val) ||
1201                 is_ia32_Unknown_XMM(val)) {
1202                 store = nomem;
1203                 if(sched_point)
1204                         sched_remove(node);
1205
1206                 exchange(node, store);
1207                 return;
1208         }
1209
1210         if (mode_is_float(mode)) {
1211                 if (ia32_cg_config.use_sse2)
1212                         store = new_rd_ia32_xStore(dbg, irg, block, ptr, noreg, nomem, val);
1213                 else
1214                         store = new_rd_ia32_vfst(dbg, irg, block, ptr, noreg, nomem, val, mode);
1215         } else if (get_mode_size_bits(mode) == 128) {
1216                 // Spill 128 bit SSE registers
1217                 store = new_rd_ia32_xxStore(dbg, irg, block, ptr, noreg, nomem, val);
1218         } else if (get_mode_size_bits(mode) == 8) {
1219                 store = new_rd_ia32_Store8Bit(dbg, irg, block, ptr, noreg, nomem, val);
1220         } else {
1221                 store = new_rd_ia32_Store(dbg, irg, block, ptr, noreg, nomem, val);
1222         }
1223
1224         set_ia32_op_type(store, ia32_AddrModeD);
1225         set_ia32_ls_mode(store, mode);
1226         set_ia32_frame_ent(store, ent);
1227         set_ia32_use_frame(store);
1228         SET_IA32_ORIG_NODE(store, ia32_get_old_node_name(cg, node));
1229         DBG_OPT_SPILL2ST(node, store);
1230
1231         if (sched_point) {
1232                 sched_add_after(sched_point, store);
1233                 sched_remove(node);
1234         }
1235
1236         exchange(node, store);
1237 }
1238
1239 static ir_node *create_push(ia32_code_gen_t *cg, ir_node *node, ir_node *schedpoint, ir_node *sp, ir_node *mem, ir_entity *ent) {
1240         ir_graph *irg = get_irn_irg(node);
1241         dbg_info *dbg = get_irn_dbg_info(node);
1242         ir_node *block = get_nodes_block(node);
1243         ir_node *noreg = ia32_new_NoReg_gp(cg);
1244         ir_node *frame = get_irg_frame(irg);
1245
1246         ir_node *push = new_rd_ia32_Push(dbg, irg, block, frame, noreg, mem, sp, noreg);
1247
1248         set_ia32_frame_ent(push, ent);
1249         set_ia32_use_frame(push);
1250         set_ia32_op_type(push, ia32_AddrModeS);
1251         set_ia32_ls_mode(push, mode_Is);
1252
1253         sched_add_before(schedpoint, push);
1254         return push;
1255 }
1256
1257 static ir_node *create_pop(ia32_code_gen_t *cg, ir_node *node, ir_node *schedpoint, ir_node *sp, ir_entity *ent) {
1258         ir_graph *irg = get_irn_irg(node);
1259         dbg_info *dbg = get_irn_dbg_info(node);
1260         ir_node *block = get_nodes_block(node);
1261         ir_node *noreg = ia32_new_NoReg_gp(cg);
1262         ir_node *frame = get_irg_frame(irg);
1263
1264         ir_node *pop = new_rd_ia32_Pop(dbg, irg, block, frame, noreg, new_NoMem(), sp);
1265
1266         set_ia32_frame_ent(pop, ent);
1267         set_ia32_use_frame(pop);
1268         set_ia32_op_type(pop, ia32_AddrModeD);
1269         set_ia32_ls_mode(pop, mode_Is);
1270
1271         sched_add_before(schedpoint, pop);
1272
1273         return pop;
1274 }
1275
1276 static ir_node* create_spproj(ia32_code_gen_t *cg, ir_node *node, ir_node *pred, int pos) {
1277         ir_graph *irg = get_irn_irg(node);
1278         dbg_info *dbg = get_irn_dbg_info(node);
1279         ir_node *block = get_nodes_block(node);
1280         ir_mode *spmode = mode_Iu;
1281         const arch_register_t *spreg = &ia32_gp_regs[REG_ESP];
1282         ir_node *sp;
1283
1284         sp = new_rd_Proj(dbg, irg, block, pred, spmode, pos);
1285         arch_set_irn_register(cg->arch_env, sp, spreg);
1286
1287         return sp;
1288 }
1289
1290 /**
1291  * Transform memperm, currently we do this the ugly way and produce
1292  * push/pop into/from memory cascades. This is possible without using
1293  * any registers.
1294  */
1295 static void transform_MemPerm(ia32_code_gen_t *cg, ir_node *node) {
1296         ir_graph *irg = get_irn_irg(node);
1297         ir_node *block = get_nodes_block(node);
1298         ir_node *in[1];
1299         ir_node *keep;
1300         int i, arity;
1301         ir_node *sp = be_abi_get_ignore_irn(cg->birg->abi, &ia32_gp_regs[REG_ESP]);
1302         const ir_edge_t *edge;
1303         const ir_edge_t *next;
1304         ir_node **pops;
1305
1306         arity = be_get_MemPerm_entity_arity(node);
1307         pops = alloca(arity * sizeof(pops[0]));
1308
1309         /* create Pushs */
1310         for(i = 0; i < arity; ++i) {
1311                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
1312                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
1313                 ir_type *enttype = get_entity_type(inent);
1314                 unsigned entsize = get_type_size_bytes(enttype);
1315                 unsigned entsize2 = get_type_size_bytes(get_entity_type(outent));
1316                 ir_node *mem = get_irn_n(node, i + 1);
1317                 ir_node *push;
1318
1319                 /* work around cases where entities have different sizes */
1320                 if(entsize2 < entsize)
1321                         entsize = entsize2;
1322                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
1323
1324                 push = create_push(cg, node, node, sp, mem, inent);
1325                 sp = create_spproj(cg, node, push, pn_ia32_Push_stack);
1326                 if(entsize == 8) {
1327                         /* add another push after the first one */
1328                         push = create_push(cg, node, node, sp, mem, inent);
1329                         add_ia32_am_offs_int(push, 4);
1330                         sp = create_spproj(cg, node, push, pn_ia32_Push_stack);
1331                 }
1332
1333                 set_irn_n(node, i, new_Bad());
1334         }
1335
1336         /* create pops */
1337         for(i = arity - 1; i >= 0; --i) {
1338                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
1339                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
1340                 ir_type *enttype = get_entity_type(outent);
1341                 unsigned entsize = get_type_size_bytes(enttype);
1342                 unsigned entsize2 = get_type_size_bytes(get_entity_type(inent));
1343                 ir_node *pop;
1344
1345                 /* work around cases where entities have different sizes */
1346                 if(entsize2 < entsize)
1347                         entsize = entsize2;
1348                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
1349
1350                 pop = create_pop(cg, node, node, sp, outent);
1351                 sp = create_spproj(cg, node, pop, pn_ia32_Pop_stack);
1352                 if(entsize == 8) {
1353                         add_ia32_am_offs_int(pop, 4);
1354
1355                         /* add another pop after the first one */
1356                         pop = create_pop(cg, node, node, sp, outent);
1357                         sp = create_spproj(cg, node, pop, pn_ia32_Pop_stack);
1358                 }
1359
1360                 pops[i] = pop;
1361         }
1362
1363         in[0] = sp;
1364         keep  = be_new_Keep(&ia32_reg_classes[CLASS_ia32_gp], irg, block, 1, in);
1365         sched_add_before(node, keep);
1366
1367         /* exchange memprojs */
1368         foreach_out_edge_safe(node, edge, next) {
1369                 ir_node *proj = get_edge_src_irn(edge);
1370                 int p = get_Proj_proj(proj);
1371
1372                 assert(p < arity);
1373
1374                 set_Proj_pred(proj, pops[p]);
1375                 set_Proj_proj(proj, pn_ia32_Pop_M);
1376         }
1377
1378         /* remove memperm */
1379         arity = get_irn_arity(node);
1380         for(i = 0; i < arity; ++i) {
1381                 set_irn_n(node, i, new_Bad());
1382         }
1383         sched_remove(node);
1384 }
1385
1386 /**
1387  * Block-Walker: Calls the transform functions Spill and Reload.
1388  */
1389 static void ia32_after_ra_walker(ir_node *block, void *env) {
1390         ir_node *node, *prev;
1391         ia32_code_gen_t *cg = env;
1392
1393         /* beware: the schedule is changed here */
1394         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
1395                 prev = sched_prev(node);
1396
1397                 if (be_is_Reload(node)) {
1398                         transform_to_Load(cg, node);
1399                 } else if (be_is_Spill(node)) {
1400                         transform_to_Store(cg, node);
1401                 } else if(be_is_MemPerm(node)) {
1402                         transform_MemPerm(cg, node);
1403                 }
1404         }
1405 }
1406
1407 /**
1408  * Collects nodes that need frame entities assigned.
1409  */
1410 static void ia32_collect_frame_entity_nodes(ir_node *node, void *data)
1411 {
1412         be_fec_env_t *env = data;
1413
1414         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
1415                 const ir_mode *mode = get_spill_mode_mode(get_irn_mode(node));
1416                 int align = get_mode_size_bytes(mode);
1417                 be_node_needs_frame_entity(env, node, mode, align);
1418         } else if(is_ia32_irn(node) && get_ia32_frame_ent(node) == NULL
1419                   && is_ia32_use_frame(node)) {
1420                 if (is_ia32_need_stackent(node) || is_ia32_Load(node)) {
1421                         const ir_mode     *mode  = get_ia32_ls_mode(node);
1422                         const ia32_attr_t *attr  = get_ia32_attr_const(node);
1423                         int                align = get_mode_size_bytes(mode);
1424
1425                         if(attr->data.need_64bit_stackent) {
1426                                 mode = mode_Ls;
1427                         }
1428                         if(attr->data.need_32bit_stackent) {
1429                                 mode = mode_Is;
1430                         }
1431                         be_node_needs_frame_entity(env, node, mode, align);
1432                 } else if (is_ia32_vfild(node) || is_ia32_xLoad(node)
1433                            || is_ia32_vfld(node)) {
1434                         const ir_mode *mode  = get_ia32_ls_mode(node);
1435                         int            align = 4;
1436                         be_node_needs_frame_entity(env, node, mode, align);
1437                 } else if(is_ia32_FldCW(node)) {
1438                         /* although 2 byte would be enough 4 byte performs best */
1439                         const ir_mode *mode  = mode_Iu;
1440                         int            align = 4;
1441                         be_node_needs_frame_entity(env, node, mode, align);
1442                 } else {
1443 #ifndef NDEBUG
1444                         assert(is_ia32_St(node) ||
1445                                    is_ia32_xStoreSimple(node) ||
1446                                    is_ia32_vfst(node) ||
1447                                    is_ia32_vfist(node) ||
1448                                is_ia32_FnstCW(node));
1449 #endif
1450                 }
1451         }
1452 }
1453
1454 /**
1455  * We transform Spill and Reload here. This needs to be done before
1456  * stack biasing otherwise we would miss the corrected offset for these nodes.
1457  */
1458 static void ia32_after_ra(void *self) {
1459         ia32_code_gen_t *cg = self;
1460         ir_graph *irg = cg->irg;
1461         be_fec_env_t *fec_env = be_new_frame_entity_coalescer(cg->birg);
1462
1463         /* create and coalesce frame entities */
1464         irg_walk_graph(irg, NULL, ia32_collect_frame_entity_nodes, fec_env);
1465         be_assign_entities(fec_env);
1466         be_free_frame_entity_coalescer(fec_env);
1467
1468         irg_block_walk_graph(irg, NULL, ia32_after_ra_walker, cg);
1469 }
1470
1471 /**
1472  * Last touchups for the graph before emit: x87 simulation to replace the
1473  * virtual with real x87 instructions, creating a block schedule and peephole
1474  * optimisations.
1475  */
1476 static void ia32_finish(void *self) {
1477         ia32_code_gen_t *cg = self;
1478         ir_graph        *irg = cg->irg;
1479
1480         ia32_finish_irg(irg, cg);
1481
1482         /* we might have to rewrite x87 virtual registers */
1483         if (cg->do_x87_sim) {
1484                 x87_simulate_graph(cg->arch_env, cg->birg);
1485         }
1486
1487         /* do peephole optimisations */
1488         ia32_peephole_optimization(cg);
1489
1490         /* create block schedule, this also removes empty blocks which might
1491          * produce critical edges */
1492         cg->blk_sched = be_create_block_schedule(irg, cg->birg->exec_freq);
1493 }
1494
1495 /**
1496  * Emits the code, closes the output file and frees
1497  * the code generator interface.
1498  */
1499 static void ia32_codegen(void *self) {
1500         ia32_code_gen_t *cg = self;
1501         ir_graph        *irg = cg->irg;
1502
1503         ia32_gen_routine(cg, irg);
1504
1505         cur_reg_set = NULL;
1506
1507         /* remove it from the isa */
1508         cg->isa->cg = NULL;
1509
1510         assert(ia32_current_cg == cg);
1511         ia32_current_cg = NULL;
1512
1513         /* de-allocate code generator */
1514         del_set(cg->reg_set);
1515         free(cg);
1516 }
1517
1518 static ir_node *ia32_get_pic_base(void *self) {
1519         ir_node         *block;
1520         ia32_code_gen_t *cg      = self;
1521         ir_node         *get_eip = cg->get_eip;
1522         if(get_eip != NULL)
1523                 return get_eip;
1524
1525
1526         block       = get_irg_start_block(cg->irg);
1527         get_eip     = new_rd_ia32_GetEIP(NULL, cg->irg, block);
1528         cg->get_eip = get_eip;
1529
1530         add_irn_dep(get_eip, get_irg_frame(cg->irg));
1531
1532         return get_eip;
1533 }
1534
1535 static void *ia32_cg_init(be_irg_t *birg);
1536
1537 static const arch_code_generator_if_t ia32_code_gen_if = {
1538         ia32_cg_init,
1539         ia32_get_pic_base,
1540         ia32_before_abi,     /* before abi introduce hook */
1541         ia32_prepare_graph,
1542         NULL,                /* spill */
1543         ia32_before_sched,   /* before scheduling hook */
1544         ia32_before_ra,      /* before register allocation hook */
1545         ia32_after_ra,       /* after register allocation hook */
1546         ia32_finish,         /* called before codegen */
1547         ia32_codegen         /* emit && done */
1548 };
1549
1550 /**
1551  * Initializes a IA32 code generator.
1552  */
1553 static void *ia32_cg_init(be_irg_t *birg) {
1554         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
1555         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
1556
1557         cg->impl      = &ia32_code_gen_if;
1558         cg->irg       = birg->irg;
1559         cg->reg_set   = new_set(ia32_cmp_irn_reg_assoc, 1024);
1560         cg->arch_env  = birg->main_env->arch_env;
1561         cg->isa       = isa;
1562         cg->birg      = birg;
1563         cg->blk_sched = NULL;
1564         cg->dump      = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
1565
1566         /* enter it */
1567         isa->cg = cg;
1568
1569 #ifndef NDEBUG
1570         if (isa->name_obst) {
1571                 obstack_free(isa->name_obst, NULL);
1572                 obstack_init(isa->name_obst);
1573         }
1574 #endif /* NDEBUG */
1575
1576         cur_reg_set = cg->reg_set;
1577
1578         ia32_irn_ops.cg = cg;
1579
1580         assert(ia32_current_cg == NULL);
1581         ia32_current_cg = cg;
1582
1583         return (arch_code_generator_t *)cg;
1584 }
1585
1586
1587
1588 /*****************************************************************
1589  *  ____             _                  _   _____  _____
1590  * |  _ \           | |                | | |_   _|/ ____|  /\
1591  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
1592  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
1593  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
1594  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
1595  *
1596  *****************************************************************/
1597
1598 /**
1599  * Set output modes for GCC
1600  */
1601 static const tarval_mode_info mo_integer = {
1602         TVO_HEX,
1603         "0x",
1604         NULL,
1605 };
1606
1607 /*
1608  * set the tarval output mode of all integer modes to decimal
1609  */
1610 static void set_tarval_output_modes(void)
1611 {
1612         int i;
1613
1614         for (i = get_irp_n_modes() - 1; i >= 0; --i) {
1615                 ir_mode *mode = get_irp_mode(i);
1616
1617                 if (mode_is_int(mode))
1618                         set_tarval_mode_output_option(mode, &mo_integer);
1619         }
1620 }
1621
1622 const arch_isa_if_t ia32_isa_if;
1623
1624 /**
1625  * The template that generates a new ISA object.
1626  * Note that this template can be changed by command line
1627  * arguments.
1628  */
1629 static ia32_isa_t ia32_isa_template = {
1630         {
1631                 &ia32_isa_if,            /* isa interface implementation */
1632                 &ia32_gp_regs[REG_ESP],  /* stack pointer register */
1633                 &ia32_gp_regs[REG_EBP],  /* base pointer register */
1634                 -1,                      /* stack direction */
1635                 NULL,                    /* main environment */
1636                 7,                       /* costs for a spill instruction */
1637                 5,                       /* costs for a reload instruction */
1638         },
1639         NULL,                    /* 16bit register names */
1640         NULL,                    /* 8bit register names */
1641         NULL,                    /* 8bit register names high */
1642         NULL,                    /* types */
1643         NULL,                    /* tv_ents */
1644         NULL,                    /* current code generator */
1645         NULL,                    /* abstract machine */
1646 #ifndef NDEBUG
1647         NULL,                    /* name obstack */
1648 #endif
1649 };
1650
1651 /**
1652  * Initializes the backend ISA.
1653  */
1654 static void *ia32_init(FILE *file_handle) {
1655         static int inited = 0;
1656         ia32_isa_t *isa;
1657
1658         if (inited)
1659                 return NULL;
1660         inited = 1;
1661
1662         set_tarval_output_modes();
1663
1664         isa = xmalloc(sizeof(*isa));
1665         memcpy(isa, &ia32_isa_template, sizeof(*isa));
1666
1667         if(mode_fpcw == NULL) {
1668                 mode_fpcw = new_ir_mode("Fpcw", irms_int_number, 16, 0, irma_none, 0);
1669         }
1670
1671         ia32_register_init();
1672         ia32_create_opcodes();
1673
1674         be_emit_init(file_handle);
1675         isa->regs_16bit     = pmap_create();
1676         isa->regs_8bit      = pmap_create();
1677         isa->regs_8bit_high = pmap_create();
1678         isa->types          = pmap_create();
1679         isa->tv_ent         = pmap_create();
1680         isa->cpu            = ia32_init_machine_description();
1681
1682         ia32_build_16bit_reg_map(isa->regs_16bit);
1683         ia32_build_8bit_reg_map(isa->regs_8bit);
1684         ia32_build_8bit_reg_map_high(isa->regs_8bit_high);
1685
1686 #ifndef NDEBUG
1687         isa->name_obst = xmalloc(sizeof(*isa->name_obst));
1688         obstack_init(isa->name_obst);
1689 #endif /* NDEBUG */
1690
1691         /* enter the ISA object into the intrinsic environment */
1692         intrinsic_env.isa = isa;
1693         ia32_handle_intrinsics();
1694
1695         /* needed for the debug support */
1696         be_gas_emit_switch_section(GAS_SECTION_TEXT);
1697         be_emit_cstring(".Ltext0:\n");
1698         be_emit_write_line();
1699
1700         /* we mark referenced global entities, so we can only emit those which
1701          * are actually referenced. (Note: you mustn't use the type visited flag
1702          * elsewhere in the backend)
1703          */
1704         inc_master_type_visited();
1705
1706         return isa;
1707 }
1708
1709
1710
1711 /**
1712  * Closes the output file and frees the ISA structure.
1713  */
1714 static void ia32_done(void *self) {
1715         ia32_isa_t *isa = self;
1716
1717         /* emit now all global declarations */
1718         be_gas_emit_decls(isa->arch_isa.main_env, 1);
1719
1720         pmap_destroy(isa->regs_16bit);
1721         pmap_destroy(isa->regs_8bit);
1722         pmap_destroy(isa->regs_8bit_high);
1723         pmap_destroy(isa->tv_ent);
1724         pmap_destroy(isa->types);
1725
1726 #ifndef NDEBUG
1727         obstack_free(isa->name_obst, NULL);
1728 #endif /* NDEBUG */
1729
1730         be_emit_exit();
1731
1732         free(self);
1733 }
1734
1735
1736 /**
1737  * Return the number of register classes for this architecture.
1738  * We report always these:
1739  *  - the general purpose registers
1740  *  - the SSE floating point register set
1741  *  - the virtual floating point registers
1742  *  - the SSE vector register set
1743  */
1744 static unsigned ia32_get_n_reg_class(const void *self) {
1745         (void) self;
1746         return N_CLASSES;
1747 }
1748
1749 /**
1750  * Return the register class for index i.
1751  */
1752 static const arch_register_class_t *ia32_get_reg_class(const void *self,
1753                                                        unsigned i)
1754 {
1755         (void) self;
1756         assert(i < N_CLASSES);
1757         return &ia32_reg_classes[i];
1758 }
1759
1760 /**
1761  * Get the register class which shall be used to store a value of a given mode.
1762  * @param self The this pointer.
1763  * @param mode The mode in question.
1764  * @return A register class which can hold values of the given mode.
1765  */
1766 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self,
1767                 const ir_mode *mode)
1768 {
1769         (void) self;
1770
1771         if (mode_is_float(mode)) {
1772                 return ia32_cg_config.use_sse2 ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1773         }
1774         else
1775                 return &ia32_reg_classes[CLASS_ia32_gp];
1776 }
1777
1778 /**
1779  * Get the ABI restrictions for procedure calls.
1780  * @param self        The this pointer.
1781  * @param method_type The type of the method (procedure) in question.
1782  * @param abi         The abi object to be modified
1783  */
1784 static void ia32_get_call_abi(const void *self, ir_type *method_type,
1785                               be_abi_call_t *abi)
1786 {
1787         ir_type  *tp;
1788         ir_mode  *mode;
1789         unsigned  cc;
1790         int       n, i, regnum;
1791         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1792         (void) self;
1793
1794         /* set abi flags for calls */
1795         call_flags.bits.left_to_right         = 0;  /* always last arg first on stack */
1796         call_flags.bits.store_args_sequential = 0;
1797         /* call_flags.bits.try_omit_fp                 not changed: can handle both settings */
1798         call_flags.bits.fp_free               = 0;  /* the frame pointer is fixed in IA32 */
1799         call_flags.bits.call_has_imm          = 1;  /* IA32 calls can have immediate address */
1800
1801         /* set parameter passing style */
1802         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1803
1804         if (get_method_variadicity(method_type) == variadicity_variadic) {
1805                 /* pass all parameters of a variadic function on the stack */
1806                 cc = cc_cdecl_set;
1807         } else {
1808                 cc = get_method_calling_convention(method_type);
1809                 if (get_method_additional_properties(method_type) & mtp_property_private
1810                                 && (ia32_cg_config.optimize_cc)) {
1811                         /* set the calling conventions to register parameter */
1812                         cc = (cc & ~cc_bits) | cc_reg_param;
1813                 }
1814         }
1815
1816         /* we have to pop the shadow parameter ourself for compound calls */
1817         if( (get_method_calling_convention(method_type) & cc_compound_ret)
1818                         && !(cc & cc_reg_param)) {
1819                 be_abi_call_set_pop(abi, get_mode_size_bytes(mode_P_data));
1820         }
1821
1822         n = get_method_n_params(method_type);
1823         for (i = regnum = 0; i < n; i++) {
1824                 ir_mode               *mode;
1825                 const arch_register_t *reg = NULL;
1826
1827                 tp   = get_method_param_type(method_type, i);
1828                 mode = get_type_mode(tp);
1829                 if (mode != NULL) {
1830                         reg  = ia32_get_RegParam_reg(cc, regnum, mode);
1831                 }
1832                 if (reg != NULL) {
1833                         be_abi_call_param_reg(abi, i, reg);
1834                         ++regnum;
1835                 } else {
1836                         /* Micro optimisation: if the mode is shorter than 4 bytes, load 4 bytes.
1837                          * movl has a shorter opcode than mov[sz][bw]l */
1838                         ir_mode *load_mode = mode;
1839                         if (mode != NULL && get_mode_size_bytes(mode) < 4) load_mode = mode_Iu;
1840                         be_abi_call_param_stack(abi, i, load_mode, 4, 0, 0);
1841                 }
1842         }
1843
1844         /* set return registers */
1845         n = get_method_n_ress(method_type);
1846
1847         assert(n <= 2 && "more than two results not supported");
1848
1849         /* In case of 64bit returns, we will have two 32bit values */
1850         if (n == 2) {
1851                 tp   = get_method_res_type(method_type, 0);
1852                 mode = get_type_mode(tp);
1853
1854                 assert(!mode_is_float(mode) && "two FP results not supported");
1855
1856                 tp   = get_method_res_type(method_type, 1);
1857                 mode = get_type_mode(tp);
1858
1859                 assert(!mode_is_float(mode) && "mixed INT, FP results not supported");
1860
1861                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
1862                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
1863         }
1864         else if (n == 1) {
1865                 const arch_register_t *reg;
1866
1867                 tp   = get_method_res_type(method_type, 0);
1868                 assert(is_atomic_type(tp));
1869                 mode = get_type_mode(tp);
1870
1871                 reg = mode_is_float(mode) ? &ia32_vfp_regs[REG_VF0] : &ia32_gp_regs[REG_EAX];
1872
1873                 be_abi_call_res_reg(abi, 0, reg);
1874         }
1875 }
1876
1877
1878 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self,
1879                                     const ir_node *irn)
1880 {
1881         (void) self;
1882         (void) irn;
1883         return &ia32_irn_ops;
1884 }
1885
1886 const arch_irn_handler_t ia32_irn_handler = {
1887         ia32_get_irn_ops
1888 };
1889
1890 const arch_irn_handler_t *ia32_get_irn_handler(const void *self)
1891 {
1892         (void) self;
1893         return &ia32_irn_handler;
1894 }
1895
1896 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn)
1897 {
1898         (void) block_env;
1899
1900         if(!is_ia32_irn(irn)) {
1901                 return -1;
1902         }
1903
1904         if(is_ia32_NoReg_GP(irn) || is_ia32_NoReg_VFP(irn) || is_ia32_NoReg_XMM(irn)
1905                 || is_ia32_Unknown_GP(irn) || is_ia32_Unknown_XMM(irn)
1906                 || is_ia32_Unknown_VFP(irn) || is_ia32_ChangeCW(irn)
1907                 || is_ia32_Immediate(irn))
1908                 return 0;
1909
1910         return 1;
1911 }
1912
1913 /**
1914  * Initializes the code generator interface.
1915  */
1916 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self)
1917 {
1918         (void) self;
1919         return &ia32_code_gen_if;
1920 }
1921
1922 /**
1923  * Returns the estimated execution time of an ia32 irn.
1924  */
1925 static sched_timestep_t ia32_sched_exectime(void *env, const ir_node *irn) {
1926         const arch_env_t *arch_env = env;
1927         return is_ia32_irn(irn) ? ia32_get_op_estimated_cost(arch_get_irn_ops(arch_env, irn), irn) : 1;
1928 }
1929
1930 list_sched_selector_t ia32_sched_selector;
1931
1932 /**
1933  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
1934  */
1935 static const list_sched_selector_t *ia32_get_list_sched_selector(
1936                 const void *self, list_sched_selector_t *selector)
1937 {
1938         (void) self;
1939         memcpy(&ia32_sched_selector, selector, sizeof(ia32_sched_selector));
1940         ia32_sched_selector.exectime              = ia32_sched_exectime;
1941         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
1942         return &ia32_sched_selector;
1943 }
1944
1945 static const ilp_sched_selector_t *ia32_get_ilp_sched_selector(const void *self)
1946 {
1947         (void) self;
1948         return NULL;
1949 }
1950
1951 /**
1952  * Returns the necessary byte alignment for storing a register of given class.
1953  */
1954 static int ia32_get_reg_class_alignment(const void *self,
1955                                         const arch_register_class_t *cls)
1956 {
1957         ir_mode *mode = arch_register_class_mode(cls);
1958         int bytes     = get_mode_size_bytes(mode);
1959         (void) self;
1960
1961         if (mode_is_float(mode) && bytes > 8)
1962                 return 16;
1963         return bytes;
1964 }
1965
1966 static const be_execution_unit_t ***ia32_get_allowed_execution_units(
1967                 const void *self, const ir_node *irn)
1968 {
1969         static const be_execution_unit_t *_allowed_units_BRANCH[] = {
1970                 &ia32_execution_units_BRANCH[IA32_EXECUNIT_TP_BRANCH_BRANCH1],
1971                 &ia32_execution_units_BRANCH[IA32_EXECUNIT_TP_BRANCH_BRANCH2],
1972                 NULL,
1973         };
1974         static const be_execution_unit_t *_allowed_units_GP[] = {
1975                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EAX],
1976                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EBX],
1977                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_ECX],
1978                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EDX],
1979                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_ESI],
1980                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EDI],
1981                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EBP],
1982                 NULL,
1983         };
1984         static const be_execution_unit_t *_allowed_units_DUMMY[] = {
1985                 &be_machine_execution_units_DUMMY[0],
1986                 NULL,
1987         };
1988         static const be_execution_unit_t **_units_callret[] = {
1989                 _allowed_units_BRANCH,
1990                 NULL
1991         };
1992         static const be_execution_unit_t **_units_other[] = {
1993                 _allowed_units_GP,
1994                 NULL
1995         };
1996         static const be_execution_unit_t **_units_dummy[] = {
1997                 _allowed_units_DUMMY,
1998                 NULL
1999         };
2000         const be_execution_unit_t ***ret;
2001         (void) self;
2002
2003         if (is_ia32_irn(irn)) {
2004                 ret = get_ia32_exec_units(irn);
2005         }
2006         else if (is_be_node(irn)) {
2007                 if (be_is_Call(irn) || be_is_Return(irn)) {
2008                         ret = _units_callret;
2009                 }
2010                 else if (be_is_Barrier(irn)) {
2011                         ret = _units_dummy;
2012                 }
2013                 else {
2014                          ret = _units_other;
2015                 }
2016         }
2017         else {
2018                 ret = _units_dummy;
2019         }
2020
2021         return ret;
2022 }
2023
2024 /**
2025  * Return the abstract ia32 machine.
2026  */
2027 static const be_machine_t *ia32_get_machine(const void *self) {
2028         const ia32_isa_t *isa = self;
2029         return isa->cpu;
2030 }
2031
2032 /**
2033  * Return irp irgs in the desired order.
2034  */
2035 static ir_graph **ia32_get_irg_list(const void *self, ir_graph ***irg_list)
2036 {
2037         (void) self;
2038         (void) irg_list;
2039         return NULL;
2040 }
2041
2042 /**
2043  * Allows or disallows the creation of Psi nodes for the given Phi nodes.
2044  * @return 1 if allowed, 0 otherwise
2045  */
2046 static int ia32_is_psi_allowed(ir_node *sel, ir_node *phi_list, int i, int j)
2047 {
2048         ir_node *phi;
2049
2050         (void)sel;
2051         (void)i;
2052         (void)j;
2053
2054         if(!ia32_cg_config.use_cmov) {
2055                 /* TODO: we could still handle abs(x)... */
2056                 return 0;
2057         }
2058
2059         /* we can't handle psis with 64bit compares yet */
2060         if(is_Proj(sel)) {
2061                 ir_node *pred = get_Proj_pred(sel);
2062                 if(is_Cmp(pred)) {
2063                         ir_node *left     = get_Cmp_left(pred);
2064                         ir_mode *cmp_mode = get_irn_mode(left);
2065                         if(!mode_is_float(cmp_mode) && get_mode_size_bits(cmp_mode) > 32)
2066                                 return 0;
2067                 }
2068         }
2069
2070         /* check the Phi nodes */
2071         for (phi = phi_list; phi; phi = get_irn_link(phi)) {
2072                 ir_mode *mode = get_irn_mode(phi);
2073
2074                 if (mode_is_float(mode) || get_mode_size_bits(mode) > 32)
2075                         return 0;
2076         }
2077
2078         return 1;
2079 }
2080
2081 /**
2082  * Returns the libFirm configuration parameter for this backend.
2083  */
2084 static const backend_params *ia32_get_libfirm_params(void) {
2085         static const ir_settings_if_conv_t ifconv = {
2086                 4,                    /* maxdepth, doesn't matter for Psi-conversion */
2087                 ia32_is_psi_allowed   /* allows or disallows Psi creation for given selector */
2088         };
2089         static const ir_settings_arch_dep_t ad = {
2090                 1,                   /* also use subs */
2091                 4,                   /* maximum shifts */
2092                 31,                  /* maximum shift amount */
2093                 ia32_evaluate_insn,  /* evaluate the instruction sequence */
2094
2095                 1,  /* allow Mulhs */
2096                 1,  /* allow Mulus */
2097                 32  /* Mulh allowed up to 32 bit */
2098         };
2099         static backend_params p = {
2100                 1,     /* need dword lowering */
2101                 1,     /* support inline assembly */
2102                 NULL,  /* no additional opcodes */
2103                 NULL,  /* will be set later */
2104                 ia32_create_intrinsic_fkt,
2105                 &intrinsic_env,  /* context for ia32_create_intrinsic_fkt */
2106                 NULL,  /* will be set below */
2107         };
2108
2109         ia32_setup_cg_config();
2110
2111         p.dep_param    = &ad;
2112         p.if_conv_info = &ifconv;
2113         return &p;
2114 }
2115
2116 static const lc_opt_enum_int_items_t gas_items[] = {
2117         { "elf",     GAS_FLAVOUR_ELF },
2118         { "mingw",   GAS_FLAVOUR_MINGW  },
2119         { "yasm",    GAS_FLAVOUR_YASM   },
2120         { "macho",   GAS_FLAVOUR_MACH_O },
2121         { NULL,      0 }
2122 };
2123
2124 static lc_opt_enum_int_var_t gas_var = {
2125         (int*) &be_gas_flavour, gas_items
2126 };
2127
2128 static const lc_opt_table_entry_t ia32_options[] = {
2129         LC_OPT_ENT_ENUM_INT("gasmode", "set the GAS compatibility mode", &gas_var),
2130         LC_OPT_LAST
2131 };
2132
2133 const arch_isa_if_t ia32_isa_if = {
2134         ia32_init,
2135         ia32_done,
2136         ia32_get_n_reg_class,
2137         ia32_get_reg_class,
2138         ia32_get_reg_class_for_mode,
2139         ia32_get_call_abi,
2140         ia32_get_irn_handler,
2141         ia32_get_code_generator_if,
2142         ia32_get_list_sched_selector,
2143         ia32_get_ilp_sched_selector,
2144         ia32_get_reg_class_alignment,
2145         ia32_get_libfirm_params,
2146         ia32_get_allowed_execution_units,
2147         ia32_get_machine,
2148         ia32_get_irg_list,
2149 };
2150
2151 void ia32_init_emitter(void);
2152 void ia32_init_finish(void);
2153 void ia32_init_optimize(void);
2154 void ia32_init_transform(void);
2155 void ia32_init_x87(void);
2156
2157 void be_init_arch_ia32(void)
2158 {
2159         lc_opt_entry_t *be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
2160         lc_opt_entry_t *ia32_grp = lc_opt_get_grp(be_grp, "ia32");
2161
2162         lc_opt_add_table(ia32_grp, ia32_options);
2163         be_register_isa_if("ia32", &ia32_isa_if);
2164
2165         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.cg");
2166
2167         ia32_init_emitter();
2168         ia32_init_finish();
2169         ia32_init_optimize();
2170         ia32_init_transform();
2171         ia32_init_x87();
2172         ia32_init_architecture();
2173 }
2174
2175 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ia32);