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