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