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