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