calculate inverse operation of Add, Sub, Not, Minus, Xor
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /**
2  * This is the main ia32 firm backend driver.
3  * @author Christian Wuerdig
4  * $Id$
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #ifdef HAVE_MALLOC_H
12 #include <malloc.h>
13 #endif
14
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18
19 #ifdef WITH_LIBCORE
20 #include <libcore/lc_opts.h>
21 #include <libcore/lc_opts_enum.h>
22 #endif /* WITH_LIBCORE */
23
24 #include "pseudo_irg.h"
25 #include "irgwalk.h"
26 #include "irprog.h"
27 #include "irprintf.h"
28 #include "iredges_t.h"
29 #include "ircons.h"
30 #include "irgmod.h"
31 #include "irgopt.h"
32
33 #include "bitset.h"
34 #include "debug.h"
35
36 #include "../beabi.h"                 /* the general register allocator interface */
37 #include "../benode_t.h"
38 #include "../belower.h"
39 #include "../besched_t.h"
40 #include "../be.h"
41 #include "bearch_ia32_t.h"
42
43 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
44 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
45 #include "ia32_gen_decls.h"           /* interface declaration emitter */
46 #include "ia32_transform.h"
47 #include "ia32_emitter.h"
48 #include "ia32_map_regs.h"
49 #include "ia32_optimize.h"
50 #include "ia32_x87.h"
51 #include "ia32_dbg_stat.h"
52
53 #define DEBUG_MODULE "firm.be.ia32.isa"
54
55 /* TODO: ugly */
56 static set *cur_reg_set = NULL;
57
58 #undef is_Start
59 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
60
61 /* Creates the unique per irg GP NoReg node. */
62 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
63         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_GP_NOREG]);
64 }
65
66 /* Creates the unique per irg FP NoReg node. */
67 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
68         return be_abi_get_callee_save_irn(cg->birg->abi,
69                 USE_SSE2(cg) ? &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG]);
70 }
71
72 /**************************************************
73  *                         _ _              _  __
74  *                        | | |            (_)/ _|
75  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
76  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
77  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
78  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
79  *            __/ |
80  *           |___/
81  **************************************************/
82
83 static ir_node *my_skip_proj(const ir_node *n) {
84         while (is_Proj(n))
85                 n = get_Proj_pred(n);
86         return (ir_node *)n;
87 }
88
89
90 /**
91  * Return register requirements for an ia32 node.
92  * If the node returns a tuple (mode_T) then the proj's
93  * will be asked for this information.
94  */
95 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
96         const ia32_irn_ops_t      *ops = self;
97         const ia32_register_req_t *irn_req;
98         long                       node_pos = pos == -1 ? 0 : pos;
99         ir_mode                   *mode     = is_Block(irn) ? NULL : get_irn_mode(irn);
100         FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
101
102         if (is_Block(irn) || mode == mode_M || mode == mode_X) {
103                 DBG((mod, LEVEL_1, "ignoring Block, mode_M, mode_X node %+F\n", irn));
104                 return NULL;
105         }
106
107         if (mode == mode_T && pos < 0) {
108                 DBG((mod, LEVEL_1, "ignoring request OUT requirements for node %+F\n", irn));
109                 return NULL;
110         }
111
112         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
113
114         if (is_Proj(irn)) {
115                 if (pos == -1) {
116                         node_pos = ia32_translate_proj_pos(irn);
117                 }
118                 else {
119                         node_pos = pos;
120                 }
121
122                 irn = my_skip_proj(irn);
123
124                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
125         }
126
127         if (is_ia32_irn(irn)) {
128                 if (pos >= 0) {
129                         irn_req = get_ia32_in_req(irn, pos);
130                 }
131                 else {
132                         irn_req = get_ia32_out_req(irn, node_pos);
133                 }
134
135                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
136
137                 memcpy(req, &(irn_req->req), sizeof(*req));
138
139                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
140                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
141                         req->other_same = get_irn_n(irn, irn_req->same_pos);
142                 }
143
144                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
145                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
146                         req->other_different = get_irn_n(irn, irn_req->different_pos);
147                 }
148         }
149         else {
150                 /* treat Unknowns like Const with default requirements */
151                 if (is_Unknown(irn)) {
152                         DB((mod, LEVEL_1, "returning UKNWN reqs for %+F\n", irn));
153                         if (mode_is_float(mode)) {
154                                 if (USE_SSE2(ops->cg))
155                                         memcpy(req, &(ia32_default_req_ia32_xmm_xmm_UKNWN), sizeof(*req));
156                                 else
157                                         memcpy(req, &(ia32_default_req_ia32_vfp_vfp_UKNWN), sizeof(*req));
158                         }
159                         else if (mode_is_int(mode) || mode_is_reference(mode))
160                                 memcpy(req, &(ia32_default_req_ia32_gp_gp_UKNWN), sizeof(*req));
161                         else if (mode == mode_T || mode == mode_M) {
162                                 DBG((mod, LEVEL_1, "ignoring Unknown node %+F\n", irn));
163                                 return NULL;
164                         }
165                         else
166                                 assert(0 && "unsupported Unknown-Mode");
167                 }
168                 else {
169                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
170                         req = NULL;
171                 }
172         }
173
174         return req;
175 }
176
177 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
178         int                   pos = 0;
179         const ia32_irn_ops_t *ops = self;
180
181         if (get_irn_mode(irn) == mode_X) {
182                 return;
183         }
184
185         DBG((ops->cg->mod, LEVEL_1, "ia32 assigned register %s to node %+F\n", reg->name, irn));
186
187         if (is_Proj(irn)) {
188                 pos = ia32_translate_proj_pos(irn);
189                 irn = my_skip_proj(irn);
190         }
191
192         if (is_ia32_irn(irn)) {
193                 const arch_register_t **slots;
194
195                 slots      = get_ia32_slots(irn);
196                 slots[pos] = reg;
197         }
198         else {
199                 ia32_set_firm_reg(irn, reg, cur_reg_set);
200         }
201 }
202
203 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
204         int pos = 0;
205         const arch_register_t *reg = NULL;
206
207         if (is_Proj(irn)) {
208
209                 if (get_irn_mode(irn) == mode_X) {
210                         return NULL;
211                 }
212
213                 pos = ia32_translate_proj_pos(irn);
214                 irn = my_skip_proj(irn);
215         }
216
217         if (is_ia32_irn(irn)) {
218                 const arch_register_t **slots;
219                 slots = get_ia32_slots(irn);
220                 reg   = slots[pos];
221         }
222         else {
223                 reg = ia32_get_firm_reg(irn, cur_reg_set);
224         }
225
226         return reg;
227 }
228
229 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
230         irn = my_skip_proj(irn);
231         if (is_cfop(irn))
232                 return arch_irn_class_branch;
233         else if (is_ia32_Cnst(irn))
234                 return arch_irn_class_const;
235         else if (is_ia32_irn(irn))
236                 return arch_irn_class_normal;
237         else
238                 return 0;
239 }
240
241 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
242         irn = my_skip_proj(irn);
243         if (is_ia32_irn(irn))
244                 return get_ia32_flags(irn);
245         else {
246                 if (is_Unknown(irn))
247                         return arch_irn_flags_ignore;
248                 return 0;
249         }
250 }
251
252 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
253         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
254 }
255
256 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
257         char buf[64];
258         const ia32_irn_ops_t *ops = self;
259
260         if (get_ia32_frame_ent(irn)) {
261                 ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
262
263                 DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
264                 snprintf(buf, sizeof(buf), "%d", bias);
265
266                 if (get_ia32_op_type(irn) == ia32_Normal) {
267                         set_ia32_cnst(irn, buf);
268                 }
269                 else {
270                         add_ia32_am_offs(irn, buf);
271                         am_flav |= ia32_O;
272                         set_ia32_am_flavour(irn, am_flav);
273                 }
274         }
275 }
276
277 typedef struct {
278         be_abi_call_flags_bits_t flags;
279         const arch_isa_t *isa;
280         const arch_env_t *aenv;
281         ir_graph *irg;
282 } ia32_abi_env_t;
283
284 static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
285 {
286         ia32_abi_env_t *env    = xmalloc(sizeof(env[0]));
287         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
288         env->flags = fl.bits;
289         env->irg   = irg;
290         env->aenv  = aenv;
291         env->isa   = aenv->isa;
292         return env;
293 }
294
295 /**
296  * Put all registers which are saved by the prologue/epilogue in a set.
297  *
298  * @param self  The callback object.
299  * @param s     The result set.
300  */
301 static void ia32_abi_dont_save_regs(void *self, pset *s)
302 {
303         ia32_abi_env_t *env = self;
304         if(env->flags.try_omit_fp)
305                 pset_insert_ptr(s, env->isa->bp);
306 }
307
308 /**
309  * Generate the routine prologue.
310  *
311  * @param self    The callback object.
312  * @param mem     A pointer to the mem node. Update this if you define new memory.
313  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
314  *
315  * @return        The register which shall be used as a stack frame base.
316  *
317  * All nodes which define registers in @p reg_map must keep @p reg_map current.
318  */
319 static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
320 {
321         ia32_abi_env_t *env              = self;
322
323         if (!env->flags.try_omit_fp) {
324                 int reg_size         = get_mode_size_bytes(env->isa->bp->reg_class->mode);
325                 ir_node *bl          = get_irg_start_block(env->irg);
326                 ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
327                 ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
328                 ir_node *push;
329
330                 /* push ebp */
331                 push    = new_rd_ia32_Push(NULL, env->irg, bl, curr_sp, curr_bp, *mem);
332                 curr_sp = new_r_Proj(env->irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
333                 *mem    = new_r_Proj(env->irg, bl, push, mode_M, pn_ia32_Push_M);
334
335                 /* the push must have SP out register */
336                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
337                 set_ia32_flags(push, arch_irn_flags_ignore);
338
339                 /* move esp to ebp */
340                 curr_bp  = be_new_Copy(env->isa->bp->reg_class, env->irg, bl, curr_sp);
341                 be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), env->isa->bp);
342                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
343                 be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
344
345                 /* beware: the copy must be done before any other sp use */
346                 curr_sp = be_new_CopyKeep_single(env->isa->sp->reg_class, env->irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
347                 be_set_constr_single_reg(curr_sp, BE_OUT_POS(0), env->isa->sp);
348                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
349                 be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
350
351                 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
352                 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
353
354                 return env->isa->bp;
355         }
356
357         return env->isa->sp;
358 }
359
360 /**
361  * Generate the routine epilogue.
362  * @param self    The callback object.
363  * @param bl      The block for the epilog
364  * @param mem     A pointer to the mem node. Update this if you define new memory.
365  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
366  * @return        The register which shall be used as a stack frame base.
367  *
368  * All nodes which define registers in @p reg_map must keep @p reg_map current.
369  */
370 static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
371 {
372         ia32_abi_env_t *env  = self;
373         ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
374         ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
375
376         if (env->flags.try_omit_fp) {
377                 /* simply remove the stack frame here */
378                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_shrink);
379         }
380         else {
381                 const ia32_isa_t *isa = (ia32_isa_t *)env->isa;
382                 ir_mode *mode_bp = env->isa->bp->reg_class->mode;
383                 int reg_size     = get_mode_size_bytes(env->isa->bp->reg_class->mode);
384
385                 /* gcc always emits a leave at the end of a routine */
386                 if (1 || ARCH_AMD(isa->opt_arch)) {
387                         ir_node *leave;
388
389                         /* leave */
390                         leave = new_rd_ia32_Leave(NULL, env->irg, bl, curr_sp, *mem);
391                         set_ia32_flags(leave, arch_irn_flags_ignore);
392                         curr_bp = new_r_Proj(current_ir_graph, bl, leave, mode_bp, pn_ia32_Leave_frame);
393                         curr_sp = new_r_Proj(current_ir_graph, bl, leave, get_irn_mode(curr_sp), pn_ia32_Leave_stack);
394                         *mem    = new_r_Proj(current_ir_graph, bl, leave, mode_M, pn_ia32_Leave_M);
395                 }
396                 else {
397                         ir_node *pop;
398
399                         /* copy ebp to esp */
400                         curr_sp = be_new_SetSP(env->isa->sp, env->irg, bl, curr_sp, curr_bp, *mem);
401
402                         /* pop ebp */
403                         pop = new_rd_ia32_Pop(NULL, env->irg, bl, curr_sp, *mem);
404                         set_ia32_flags(pop, arch_irn_flags_ignore);
405                         curr_bp = new_r_Proj(current_ir_graph, bl, pop, mode_bp, pn_ia32_Pop_res);
406                         curr_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(curr_sp), pn_ia32_Pop_stack);
407                         *mem    = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
408                 }
409                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
410                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
411         }
412
413         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
414         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
415 }
416
417 /**
418  * Produces the type which sits between the stack args and the locals on the stack.
419  * it will contain the return address and space to store the old base pointer.
420  * @return The Firm type modeling the ABI between type.
421  */
422 static ir_type *ia32_abi_get_between_type(void *self)
423 {
424         static ir_type *omit_fp_between_type = NULL;
425         static ir_type *between_type         = NULL;
426
427         ia32_abi_env_t *env = self;
428
429         if(!between_type) {
430                 entity *old_bp_ent;
431                 entity *ret_addr_ent;
432                 entity *omit_fp_ret_addr_ent;
433
434                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
435                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
436
437                 between_type           = new_type_class(new_id_from_str("ia32_between_type"));
438                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
439                 ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
440
441                 set_entity_offset_bytes(old_bp_ent, 0);
442                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
443                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
444
445                 omit_fp_between_type   = new_type_class(new_id_from_str("ia32_between_type_omit_fp"));
446                 omit_fp_ret_addr_ent   = new_entity(omit_fp_between_type, new_id_from_str("ret_addr"), ret_addr_type);
447
448                 set_entity_offset_bytes(omit_fp_ret_addr_ent, 0);
449                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
450         }
451
452         return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
453 }
454
455 /**
456  * Returns the inverse operation if @p irn, recalculating the argument at position @p i.
457  *
458  * @param irn       The original operation
459  * @param i         Index of the argument we want the inverse operation to yield
460  * @param inverse   struct to be filled with the resulting inverse op
461  * @param obstack   The obstack to use for allocation of the returned nodes array
462  * @return          The inverse operation or NULL if operation invertible
463  */
464 static arch_inverse_t *ia32_get_inverse(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
465         ir_graph *irg;
466         ir_mode  *mode;
467         ir_node  *block, *noreg, *nomem;
468         int      pnc;
469
470         /* we cannot invert non-ia32 irns */
471         if (! is_ia32_irn(irn))
472                 return NULL;
473
474         /* operand must always be a real operand (not base, index or mem) */
475         if (i != 2 && i != 3)
476                 return NULL;
477
478         /* we don't invert address mode operations */
479         if (get_ia32_op_type(irn) != ia32_Normal)
480                 return NULL;
481
482         irg   = get_irn_irg(irn);
483         block = get_nodes_block(irn);
484         mode  = get_ia32_res_mode(irn);
485         noreg = get_irn_n(irn, 0);
486         nomem = new_r_NoMem(irg);
487
488         /* initialize structure */
489         inverse->nodes = obstack_alloc(obst, sizeof(inverse->nodes[0]));
490         inverse->costs = 0;
491         inverse->n     = 2;
492
493         switch (get_irn_opcode(irn)) {
494                 case iro_ia32_Add:
495                         if (get_ia32_immop_type(irn) == ia32_ImmConst) {
496                                 /* we have an add with a const here */
497                                 /* invers == add with negated const */
498                                 inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
499                                 pnc               = pn_ia32_Add_res;
500                                 inverse->costs   += 1;
501                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
502                                 set_ia32_Immop_tarval(inverse->nodes[0], tarval_neg(get_ia32_Immop_tarval(irn)));
503                                 set_ia32_commutative(inverse->nodes[0]);
504                         }
505                         else if (get_ia32_immop_type(irn) == ia32_ImmSymConst) {
506                                 /* we have an add with a symconst here */
507                                 /* invers == sub with const */
508                                 inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
509                                 pnc               = pn_ia32_Sub_res;
510                                 inverse->costs   += 5;
511                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
512                         }
513                         else {
514                                 /* normal add: inverse == sub */
515                                 inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, (ir_node *)irn, get_irn_n(irn, i ^ 1), nomem);
516                                 pnc               = pn_ia32_Sub_res;
517                                 inverse->costs   += 5;
518                         }
519                         break;
520                 case iro_ia32_Sub:
521                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
522                                 /* we have a sub with a const/symconst here */
523                                 /* invers == add with this const */
524                                 inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
525                                 pnc               = pn_ia32_Add_res;
526                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
527                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
528                         }
529                         else {
530                                 /* normal sub */
531                                 if (i == 2) {
532                                         inverse->nodes[0] = new_rd_ia32_Add(NULL, irg, block, noreg, noreg, (ir_node *)irn, get_irn_n(irn, 3), nomem);
533                                 }
534                                 else {
535                                         inverse->nodes[0] = new_rd_ia32_Sub(NULL, irg, block, noreg, noreg, get_irn_n(irn, 2), (ir_node *)irn, nomem);
536                                 }
537                                 pnc             = pn_ia32_Sub_res;
538                                 inverse->costs += 1;
539                         }
540                         break;
541                 case iro_ia32_Eor:
542                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
543                                 /* xor with const: inverse = xor */
544                                 inverse->nodes[0] = new_rd_ia32_Eor(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
545                                 pnc               = pn_ia32_Eor_res;
546                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
547                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
548                         }
549                         else {
550                                 /* normal xor */
551                                 inverse->nodes[0] = new_rd_ia32_Eor(NULL, irg, block, noreg, noreg, (ir_node *)irn, get_irn_n(irn, i), nomem);
552                                 pnc               = pn_ia32_Eor_res;
553                                 inverse->costs   += 1;
554                         }
555                         break;
556                 case iro_ia32_Not:
557                         inverse->nodes[0] = new_rd_ia32_Not(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), nomem);
558                         pnc = pn_ia32_Not_res;
559                         inverse->costs   += 1;
560                         break;
561                 case iro_ia32_Minus:
562                         inverse->nodes[0] = new_rd_ia32_Minus(NULL, irg, block, noreg, noreg, get_irn_n(irn, i), nomem);
563                         pnc = pn_ia32_Minus_res;
564                         inverse->costs   += 1;
565                         break;
566                 default:
567                         /* inverse operation not supported */
568                         return NULL;
569         }
570
571         inverse->nodes[1] = new_r_Proj(irg, block, inverse->nodes[0], mode, pnc);
572
573         return inverse;
574 }
575
576 static const be_abi_callbacks_t ia32_abi_callbacks = {
577         ia32_abi_init,
578         free,
579         ia32_abi_get_between_type,
580         ia32_abi_dont_save_regs,
581         ia32_abi_prologue,
582         ia32_abi_epilogue,
583 };
584
585 /* fill register allocator interface */
586
587 static const arch_irn_ops_if_t ia32_irn_ops_if = {
588         ia32_get_irn_reg_req,
589         ia32_set_irn_reg,
590         ia32_get_irn_reg,
591         ia32_classify,
592         ia32_get_flags,
593         ia32_get_frame_entity,
594         ia32_set_stack_bias,
595         ia32_get_inverse
596 };
597
598 ia32_irn_ops_t ia32_irn_ops = {
599         &ia32_irn_ops_if,
600         NULL
601 };
602
603
604
605 /**************************************************
606  *                _                         _  __
607  *               | |                       (_)/ _|
608  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
609  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
610  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
611  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
612  *                        __/ |
613  *                       |___/
614  **************************************************/
615
616 /**
617  * Transforms the standard firm graph into
618  * an ia32 firm graph
619  */
620 static void ia32_prepare_graph(void *self) {
621         ia32_code_gen_t *cg = self;
622         dom_front_info_t *dom;
623         DEBUG_ONLY(firm_dbg_module_t *old_mod = cg->mod;)
624
625         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.transform");
626
627         /* 1st: transform constants and psi condition trees */
628         irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_psi_cond_tree, cg);
629
630         /* 2nd: transform all remaining nodes */
631         ia32_register_transformers();
632         dom = be_compute_dominance_frontiers(cg->irg);
633         irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
634         be_free_dominance_frontiers(dom);
635         be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
636
637         /* 3rd: optimize address mode */
638         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.am");
639         ia32_optimize_addressmode(cg);
640         be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
641         DEBUG_ONLY(cg->mod = old_mod;)
642 }
643
644 static INLINE int need_constraint_copy(ir_node *irn) {
645         return \
646                 ! is_ia32_Lea(irn)          && \
647                 ! is_ia32_Conv_I2I(irn)     && \
648                 ! is_ia32_Conv_I2I8Bit(irn) && \
649                 ! is_ia32_CmpCMov(irn)      && \
650                 ! is_ia32_CmpSet(irn);
651 }
652
653 /**
654  * Insert copies for all ia32 nodes where the should_be_same requirement
655  * is not fulfilled.
656  * Transform Sub into Neg -- Add if IN2 == OUT
657  */
658 static void ia32_finish_node(ir_node *irn, void *env) {
659         ia32_code_gen_t            *cg = env;
660         const ia32_register_req_t **reqs;
661         const arch_register_t      *out_reg, *in_reg, *in2_reg;
662         int                         n_res, i;
663         ir_node                    *copy, *in_node, *block, *in2_node;
664         ia32_op_type_t              op_tp;
665
666         if (is_ia32_irn(irn)) {
667                 /* AM Dest nodes don't produce any values  */
668                 op_tp = get_ia32_op_type(irn);
669                 if (op_tp == ia32_AddrModeD)
670                         goto end;
671
672                 reqs  = get_ia32_out_req_all(irn);
673                 n_res = get_ia32_n_res(irn);
674                 block = get_nodes_block(irn);
675
676                 /* check all OUT requirements, if there is a should_be_same */
677                 if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) && need_constraint_copy(irn))
678                 {
679                         for (i = 0; i < n_res; i++) {
680                                 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
681                                         /* get in and out register */
682                                         out_reg  = get_ia32_out_reg(irn, i);
683                                         in_node  = get_irn_n(irn, reqs[i]->same_pos);
684                                         in_reg   = arch_get_irn_register(cg->arch_env, in_node);
685
686                                         /* don't copy ignore nodes */
687                                         if (arch_irn_is(cg->arch_env, in_node, ignore) && is_Proj(in_node))
688                                                 continue;
689
690                                         /* check if in and out register are equal */
691                                         if (! REGS_ARE_EQUAL(out_reg, in_reg)) {
692                                                 /* in case of a commutative op: just exchange the in's */
693                                                 /* beware: the current op could be everything, so test for ia32 */
694                                                 /*         commutativity first before getting the second in     */
695                                                 if (is_ia32_commutative(irn)) {
696                                                         in2_node = get_irn_n(irn, reqs[i]->same_pos ^ 1);
697                                                         in2_reg  = arch_get_irn_register(cg->arch_env, in2_node);
698
699                                                         if (REGS_ARE_EQUAL(out_reg, in2_reg)) {
700                                                                 set_irn_n(irn, reqs[i]->same_pos, in2_node);
701                                                                 set_irn_n(irn, reqs[i]->same_pos ^ 1, in_node);
702                                                         }
703                                                         else
704                                                                 goto insert_copy;
705                                                 }
706                                                 else {
707 insert_copy:
708                                                         DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
709                                                         /* create copy from in register */
710                                                         copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
711
712                                                         DBG_OPT_2ADDRCPY(copy);
713
714                                                         /* destination is the out register */
715                                                         arch_set_irn_register(cg->arch_env, copy, out_reg);
716
717                                                         /* insert copy before the node into the schedule */
718                                                         sched_add_before(irn, copy);
719
720                                                         /* set copy as in */
721                                                         set_irn_n(irn, reqs[i]->same_pos, copy);
722                                                 }
723                                         }
724                                 }
725                         }
726                 }
727
728                 /* If we have a CondJmp/CmpSet/xCmpSet with immediate, we need to    */
729                 /* check if it's the right operand, otherwise we have */
730                 /* to change it, as CMP doesn't support immediate as  */
731                 /* left operands.                                     */
732                 if ((is_ia32_CondJmp(irn) || is_ia32_CmpSet(irn) || is_ia32_xCmpSet(irn)) &&
733                         (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))                   &&
734                         op_tp == ia32_AddrModeS)
735                 {
736                         set_ia32_op_type(irn, ia32_AddrModeD);
737                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
738                 }
739
740                 /* check if there is a sub which need to be transformed */
741                 ia32_transform_sub_to_neg_add(irn, cg);
742
743                 /* transform a LEA into an Add if possible */
744                 ia32_transform_lea_to_add(irn, cg);
745         }
746 end:
747
748         /* check for peephole optimization */
749         ia32_peephole_optimization(irn, cg);
750 }
751
752 static void ia32_finish_irg_walker(ir_node *block, void *env) {
753         ir_node *irn, *next;
754
755         for (irn = sched_first(block); !sched_is_end(irn); irn = next) {
756                 next = sched_next(irn);
757                 ia32_finish_node(irn, env);
758         }
759 }
760
761 /**
762  * Add Copy nodes for not fulfilled should_be_equal constraints
763  */
764 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
765         irg_block_walk_graph(irg, NULL, ia32_finish_irg_walker, cg);
766 }
767
768
769
770 /**
771  * Dummy functions for hooks we don't need but which must be filled.
772  */
773 static void ia32_before_sched(void *self) {
774 }
775
776 /**
777  * Called before the register allocator.
778  * Calculate a block schedule here. We need it for the x87
779  * simulator and the emitter.
780  */
781 static void ia32_before_ra(void *self) {
782         ia32_code_gen_t *cg = self;
783
784         cg->blk_sched = sched_create_block_schedule(cg->irg);
785 }
786
787
788 /**
789  * Transforms a be node into a Load.
790  */
791 static void transform_to_Load(ia32_transform_env_t *env) {
792         ir_node *irn         = env->irn;
793         entity  *ent         = be_get_frame_entity(irn);
794         ir_mode *mode        = env->mode;
795         ir_node *noreg       = ia32_new_NoReg_gp(env->cg);
796         ir_node *nomem       = new_rd_NoMem(env->irg);
797         ir_node *sched_point = NULL;
798         ir_node *ptr         = get_irn_n(irn, 0);
799         ir_node *mem         = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
800         ir_node *new_op, *proj;
801         const arch_register_t *reg;
802
803         if (sched_is_scheduled(irn)) {
804                 sched_point = sched_prev(irn);
805         }
806
807         if (mode_is_float(mode)) {
808                 if (USE_SSE2(env->cg))
809                         new_op = new_rd_ia32_xLoad(env->dbg, env->irg, env->block, ptr, noreg, mem);
810                 else
811                         new_op = new_rd_ia32_vfld(env->dbg, env->irg, env->block, ptr, noreg, mem);
812         }
813         else {
814                 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem);
815         }
816
817         set_ia32_am_support(new_op, ia32_am_Source);
818         set_ia32_op_type(new_op, ia32_AddrModeS);
819         set_ia32_am_flavour(new_op, ia32_B);
820         set_ia32_ls_mode(new_op, mode);
821         set_ia32_frame_ent(new_op, ent);
822         set_ia32_use_frame(new_op);
823
824         DBG_OPT_RELOAD2LD(irn, new_op);
825
826         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
827
828         if (sched_point) {
829                 sched_add_after(sched_point, new_op);
830                 sched_add_after(new_op, proj);
831
832                 sched_remove(irn);
833         }
834
835         /* copy the register from the old node to the new Load */
836         reg = arch_get_irn_register(env->cg->arch_env, irn);
837         arch_set_irn_register(env->cg->arch_env, new_op, reg);
838
839         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
840
841         exchange(irn, proj);
842 }
843
844 /**
845  * Transforms a be node into a Store.
846  */
847 static void transform_to_Store(ia32_transform_env_t *env) {
848         ir_node *irn   = env->irn;
849         entity  *ent   = be_get_frame_entity(irn);
850         ir_mode *mode  = env->mode;
851         ir_node *noreg = ia32_new_NoReg_gp(env->cg);
852         ir_node *nomem = new_rd_NoMem(env->irg);
853         ir_node *ptr   = get_irn_n(irn, 0);
854         ir_node *val   = get_irn_n(irn, 1);
855         ir_node *new_op, *proj;
856         ir_node *sched_point = NULL;
857
858         if (sched_is_scheduled(irn)) {
859                 sched_point = sched_prev(irn);
860         }
861
862         if (mode_is_float(mode)) {
863                 if (USE_SSE2(env->cg))
864                         new_op = new_rd_ia32_xStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
865                 else
866                         new_op = new_rd_ia32_vfst(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
867         }
868         else if (get_mode_size_bits(mode) == 8) {
869                 new_op = new_rd_ia32_Store8Bit(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
870         }
871         else {
872                 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
873         }
874
875         set_ia32_am_support(new_op, ia32_am_Dest);
876         set_ia32_op_type(new_op, ia32_AddrModeD);
877         set_ia32_am_flavour(new_op, ia32_B);
878         set_ia32_ls_mode(new_op, mode);
879         set_ia32_frame_ent(new_op, ent);
880         set_ia32_use_frame(new_op);
881
882         DBG_OPT_SPILL2ST(irn, new_op);
883
884         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode_M, pn_ia32_Store_M);
885
886         if (sched_point) {
887                 sched_add_after(sched_point, new_op);
888                 sched_add_after(new_op, proj);
889
890                 sched_remove(irn);
891         }
892
893         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
894
895         exchange(irn, proj);
896 }
897
898 /**
899  * Fix the mode of Spill/Reload
900  */
901 static ir_mode *fix_spill_mode(ia32_code_gen_t *cg, ir_mode *mode)
902 {
903         if (mode_is_float(mode)) {
904                 if (USE_SSE2(cg))
905                         mode = mode_D;
906                 else
907                         mode = mode_E;
908         }
909         else
910                 mode = mode_Is;
911         return mode;
912 }
913
914 /**
915  * Block-Walker: Calls the transform functions Spill and Reload.
916  */
917 static void ia32_after_ra_walker(ir_node *block, void *env) {
918         ir_node *node, *prev;
919         ia32_code_gen_t *cg = env;
920         ia32_transform_env_t tenv;
921
922         tenv.block = block;
923         tenv.irg   = current_ir_graph;
924         tenv.cg    = cg;
925         DEBUG_ONLY(tenv.mod = cg->mod;)
926
927         /* beware: the schedule is changed here */
928         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
929                 prev = sched_prev(node);
930                 if (be_is_Reload(node)) {
931                         /* we always reload the whole register  */
932                         tenv.dbg  = get_irn_dbg_info(node);
933                         tenv.irn  = node;
934                         tenv.mode = fix_spill_mode(cg, get_irn_mode(node));
935                         transform_to_Load(&tenv);
936                 }
937                 else if (be_is_Spill(node)) {
938                         /* we always spill the whole register  */
939                         tenv.dbg  = get_irn_dbg_info(node);
940                         tenv.irn  = node;
941                         tenv.mode = fix_spill_mode(cg, get_irn_mode(be_get_Spill_context(node)));
942                         transform_to_Store(&tenv);
943                 }
944         }
945 }
946
947 /**
948  * We transform Spill and Reload here. This needs to be done before
949  * stack biasing otherwise we would miss the corrected offset for these nodes.
950  *
951  * If x87 instruction should be emitted, run the x87 simulator and patch
952  * the virtual instructions. This must obviously be done after register allocation.
953  */
954 static void ia32_after_ra(void *self) {
955         ia32_code_gen_t *cg = self;
956         irg_block_walk_graph(cg->irg, NULL, ia32_after_ra_walker, self);
957
958         /* if we do x87 code generation, rewrite all the virtual instructions and registers */
959         if (cg->used_fp == fp_x87) {
960                 x87_simulate_graph(cg->arch_env, cg->irg, cg->blk_sched);
961         }
962 }
963
964
965 /**
966  * Emits the code, closes the output file and frees
967  * the code generator interface.
968  */
969 static void ia32_codegen(void *self) {
970         ia32_code_gen_t *cg = self;
971         ir_graph        *irg = cg->irg;
972
973         ia32_finish_irg(irg, cg);
974         be_dump(irg, "-finished", dump_ir_block_graph_sched);
975         ia32_gen_routine(cg->isa->out, irg, cg);
976
977         cur_reg_set = NULL;
978
979         /* remove it from the isa */
980         cg->isa->cg = NULL;
981
982         /* de-allocate code generator */
983         del_set(cg->reg_set);
984         free(self);
985
986 }
987
988 static void *ia32_cg_init(const be_irg_t *birg);
989
990 static const arch_code_generator_if_t ia32_code_gen_if = {
991         ia32_cg_init,
992         NULL,                /* before abi introduce hook */
993         ia32_prepare_graph,
994         ia32_before_sched,   /* before scheduling hook */
995         ia32_before_ra,      /* before register allocation hook */
996         ia32_after_ra,       /* after register allocation hook */
997         ia32_codegen         /* emit && done */
998 };
999
1000 /**
1001  * Initializes a IA32 code generator.
1002  */
1003 static void *ia32_cg_init(const be_irg_t *birg) {
1004         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
1005         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
1006
1007         cg->impl      = &ia32_code_gen_if;
1008         cg->irg       = birg->irg;
1009         cg->reg_set   = new_set(ia32_cmp_irn_reg_assoc, 1024);
1010         cg->arch_env  = birg->main_env->arch_env;
1011         cg->isa       = isa;
1012         cg->birg      = birg;
1013         cg->blk_sched = NULL;
1014         cg->fp_to_gp  = NULL;
1015         cg->gp_to_fp  = NULL;
1016         cg->fp_kind   = isa->fp_kind;
1017         cg->used_fp   = fp_none;
1018
1019         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.cg");
1020
1021         /* copy optimizations from isa for easier access */
1022         cg->opt = isa->opt;
1023
1024         /* enter it */
1025         isa->cg = cg;
1026
1027 #ifndef NDEBUG
1028         if (isa->name_obst_size) {
1029                 //printf("freed %d bytes from name obst\n", isa->name_obst_size);
1030                 isa->name_obst_size = 0;
1031                 obstack_free(isa->name_obst, NULL);
1032                 obstack_init(isa->name_obst);
1033         }
1034 #endif /* NDEBUG */
1035
1036         cur_reg_set = cg->reg_set;
1037
1038         ia32_irn_ops.cg = cg;
1039
1040         return (arch_code_generator_t *)cg;
1041 }
1042
1043
1044
1045 /*****************************************************************
1046  *  ____             _                  _   _____  _____
1047  * |  _ \           | |                | | |_   _|/ ____|  /\
1048  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
1049  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
1050  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
1051  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
1052  *
1053  *****************************************************************/
1054
1055 /**
1056  * The template that generates a new ISA object.
1057  * Note that this template can be changed by command line
1058  * arguments.
1059  */
1060 static ia32_isa_t ia32_isa_template = {
1061         {
1062                 &ia32_isa_if,            /* isa interface implementation */
1063                 &ia32_gp_regs[REG_ESP],  /* stack pointer register */
1064                 &ia32_gp_regs[REG_EBP],  /* base pointer register */
1065                 -1,                      /* stack direction */
1066         },
1067         NULL,                    /* 16bit register names */
1068         NULL,                    /* 8bit register names */
1069         NULL,                    /* types */
1070         NULL,                    /* tv_ents */
1071         (0                 |
1072         IA32_OPT_INCDEC    |     /* optimize add 1, sub 1 into inc/dec               default: on  */
1073         IA32_OPT_DOAM      |     /* optimize address mode                            default: on  */
1074         IA32_OPT_LEA       |     /* optimize for LEAs                                default: on  */
1075         IA32_OPT_PLACECNST |     /* place constants immediately before instructions, default: on  */
1076         IA32_OPT_IMMOPS    |     /* operations can use immediates,                   default: on  */
1077         IA32_OPT_EXTBB),         /* use extended basic block scheduling,             default: on  */
1078         arch_pentium_4,          /* instruction architecture */
1079         arch_pentium_4,          /* optimize for architecture */
1080         fp_sse2,                 /* use sse2 unit */
1081         NULL,                    /* current code generator */
1082 #ifndef NDEBUG
1083         NULL,                    /* name obstack */
1084         0                        /* name obst size */
1085 #endif
1086 };
1087
1088 /**
1089  * Initializes the backend ISA.
1090  */
1091 static void *ia32_init(FILE *file_handle) {
1092         static int inited = 0;
1093         ia32_isa_t *isa;
1094
1095         if (inited)
1096                 return NULL;
1097
1098         isa = xmalloc(sizeof(*isa));
1099         memcpy(isa, &ia32_isa_template, sizeof(*isa));
1100
1101         ia32_register_init(isa);
1102         ia32_create_opcodes();
1103
1104         if ((ARCH_INTEL(isa->arch) && isa->arch < arch_pentium_4) ||
1105             (ARCH_AMD(isa->arch) && isa->arch < arch_athlon))
1106                 /* no SSE2 for these cpu's */
1107                 isa->fp_kind = fp_x87;
1108
1109         if (ARCH_INTEL(isa->opt_arch) && isa->opt_arch >= arch_pentium_4) {
1110                 /* Pentium 4 don't like inc and dec instructions */
1111                 isa->opt &= ~IA32_OPT_INCDEC;
1112         }
1113
1114         isa->regs_16bit = pmap_create();
1115         isa->regs_8bit  = pmap_create();
1116         isa->types      = pmap_create();
1117         isa->tv_ent     = pmap_create();
1118         isa->out        = file_handle;
1119
1120         ia32_build_16bit_reg_map(isa->regs_16bit);
1121         ia32_build_8bit_reg_map(isa->regs_8bit);
1122
1123         /* patch register names of x87 registers */
1124         if (USE_x87(isa)) {
1125           ia32_st_regs[0].name = "st";
1126           ia32_st_regs[1].name = "st(1)";
1127           ia32_st_regs[2].name = "st(2)";
1128           ia32_st_regs[3].name = "st(3)";
1129           ia32_st_regs[4].name = "st(4)";
1130           ia32_st_regs[5].name = "st(5)";
1131           ia32_st_regs[6].name = "st(6)";
1132           ia32_st_regs[7].name = "st(7)";
1133         }
1134
1135 #ifndef NDEBUG
1136         isa->name_obst = xmalloc(sizeof(*isa->name_obst));
1137         obstack_init(isa->name_obst);
1138         isa->name_obst_size = 0;
1139 #endif /* NDEBUG */
1140
1141         fprintf(isa->out, "\t.intel_syntax\n");
1142
1143         inited = 1;
1144
1145         return isa;
1146 }
1147
1148
1149
1150 /**
1151  * Closes the output file and frees the ISA structure.
1152  */
1153 static void ia32_done(void *self) {
1154         ia32_isa_t *isa = self;
1155
1156         /* emit now all global declarations */
1157         ia32_gen_decls(isa->out);
1158
1159         pmap_destroy(isa->regs_16bit);
1160         pmap_destroy(isa->regs_8bit);
1161         pmap_destroy(isa->tv_ent);
1162         pmap_destroy(isa->types);
1163
1164 #ifndef NDEBUG
1165         //printf("name obst size = %d bytes\n", isa->name_obst_size);
1166         obstack_free(isa->name_obst, NULL);
1167 #endif /* NDEBUG */
1168
1169         free(self);
1170 }
1171
1172
1173 /**
1174  * Return the number of register classes for this architecture.
1175  * We report always these:
1176  *  - the general purpose registers
1177  *  - the floating point register set (depending on the unit used for FP)
1178  *  - MMX/SSE registers (currently not supported)
1179  */
1180 static int ia32_get_n_reg_class(const void *self) {
1181         return 2;
1182 }
1183
1184 /**
1185  * Return the register class for index i.
1186  */
1187 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
1188         const ia32_isa_t *isa = self;
1189         assert(i >= 0 && i < 2 && "Invalid ia32 register class requested.");
1190         if (i == 0)
1191                 return &ia32_reg_classes[CLASS_ia32_gp];
1192         return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1193 }
1194
1195 /**
1196  * Get the register class which shall be used to store a value of a given mode.
1197  * @param self The this pointer.
1198  * @param mode The mode in question.
1199  * @return A register class which can hold values of the given mode.
1200  */
1201 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
1202         const ia32_isa_t *isa = self;
1203         if (mode_is_float(mode)) {
1204                 return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1205         }
1206         else
1207                 return &ia32_reg_classes[CLASS_ia32_gp];
1208 }
1209
1210 /**
1211  * Get the ABI restrictions for procedure calls.
1212  * @param self        The this pointer.
1213  * @param method_type The type of the method (procedure) in question.
1214  * @param abi         The abi object to be modified
1215  */
1216 static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
1217         const ia32_isa_t *isa = self;
1218         ir_type  *tp;
1219         ir_mode  *mode;
1220         unsigned  cc        = get_method_calling_convention(method_type);
1221         int       n         = get_method_n_params(method_type);
1222         int       biggest_n = -1;
1223         int       stack_idx = 0;
1224         int       i, ignore_1, ignore_2;
1225         ir_mode **modes;
1226         const arch_register_t *reg;
1227         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1228
1229         unsigned use_push = !IS_P6_ARCH(isa->opt_arch);
1230
1231         /* set abi flags for calls */
1232         call_flags.bits.left_to_right         = 0;  /* always last arg first on stack */
1233         call_flags.bits.store_args_sequential = use_push;
1234         /* call_flags.bits.try_omit_fp                 not changed: can handle both settings */
1235         call_flags.bits.fp_free               = 0;  /* the frame pointer is fixed in IA32 */
1236         call_flags.bits.call_has_imm          = 1;  /* IA32 calls can have immediate address */
1237
1238         /* set stack parameter passing style */
1239         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1240
1241         /* collect the mode for each type */
1242         modes = alloca(n * sizeof(modes[0]));
1243
1244         for (i = 0; i < n; i++) {
1245                 tp       = get_method_param_type(method_type, i);
1246                 modes[i] = get_type_mode(tp);
1247         }
1248
1249         /* set register parameters  */
1250         if (cc & cc_reg_param) {
1251                 /* determine the number of parameters passed via registers */
1252                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore_1, &ignore_2);
1253
1254                 /* loop over all parameters and set the register requirements */
1255                 for (i = 0; i <= biggest_n; i++) {
1256                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
1257                         assert(reg && "kaputt");
1258                         be_abi_call_param_reg(abi, i, reg);
1259                 }
1260
1261                 stack_idx = i;
1262         }
1263
1264
1265         /* set stack parameters */
1266         for (i = stack_idx; i < n; i++) {
1267                 be_abi_call_param_stack(abi, i, 1, 0, 0);
1268         }
1269
1270
1271         /* set return registers */
1272         n = get_method_n_ress(method_type);
1273
1274         assert(n <= 2 && "more than two results not supported");
1275
1276         /* In case of 64bit returns, we will have two 32bit values */
1277         if (n == 2) {
1278                 tp   = get_method_res_type(method_type, 0);
1279                 mode = get_type_mode(tp);
1280
1281                 assert(!mode_is_float(mode) && "two FP results not supported");
1282
1283                 tp   = get_method_res_type(method_type, 1);
1284                 mode = get_type_mode(tp);
1285
1286                 assert(!mode_is_float(mode) && "two FP results not supported");
1287
1288                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
1289                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
1290         }
1291         else if (n == 1) {
1292                 const arch_register_t *reg;
1293
1294                 tp   = get_method_res_type(method_type, 0);
1295                 assert(is_atomic_type(tp));
1296                 mode = get_type_mode(tp);
1297
1298                 reg = mode_is_float(mode) ?
1299                         (USE_SSE2(isa) ? &ia32_xmm_regs[REG_XMM0] : &ia32_vfp_regs[REG_VF0]) :
1300                         &ia32_gp_regs[REG_EAX];
1301
1302                 be_abi_call_res_reg(abi, 0, reg);
1303         }
1304 }
1305
1306
1307 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1308         return &ia32_irn_ops;
1309 }
1310
1311 const arch_irn_handler_t ia32_irn_handler = {
1312         ia32_get_irn_ops
1313 };
1314
1315 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
1316         return &ia32_irn_handler;
1317 }
1318
1319 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1320         return is_ia32_irn(irn);
1321 }
1322
1323 /**
1324  * Initializes the code generator interface.
1325  */
1326 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
1327         return &ia32_code_gen_if;
1328 }
1329
1330 list_sched_selector_t ia32_sched_selector;
1331
1332 /**
1333  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
1334  */
1335 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
1336 //      memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1337         memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
1338         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
1339         return &ia32_sched_selector;
1340 }
1341
1342 /**
1343  * Returns the necessary byte alignment for storing a register of given class.
1344  */
1345 static int ia32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1346         ir_mode *mode = arch_register_class_mode(cls);
1347         int bytes     = get_mode_size_bytes(mode);
1348
1349         if (mode_is_float(mode) && bytes > 8)
1350                 return 16;
1351         return bytes;
1352 }
1353
1354 #ifdef WITH_LIBCORE
1355
1356 /* instruction set architectures. */
1357 static const lc_opt_enum_int_items_t arch_items[] = {
1358         { "386",        arch_i386, },
1359         { "486",        arch_i486, },
1360         { "pentium",    arch_pentium, },
1361         { "586",        arch_pentium, },
1362         { "pentiumpro", arch_pentium_pro, },
1363         { "686",        arch_pentium_pro, },
1364         { "pentiummmx", arch_pentium_mmx, },
1365         { "pentium2",   arch_pentium_2, },
1366         { "p2",         arch_pentium_2, },
1367         { "pentium3",   arch_pentium_3, },
1368         { "p3",         arch_pentium_3, },
1369         { "pentium4",   arch_pentium_4, },
1370         { "p4",         arch_pentium_4, },
1371         { "pentiumm",   arch_pentium_m, },
1372         { "pm",         arch_pentium_m, },
1373         { "core",       arch_core, },
1374         { "k6",         arch_k6, },
1375         { "athlon",     arch_athlon, },
1376         { "athlon64",   arch_athlon_64, },
1377         { "opteron",    arch_opteron, },
1378         { NULL,         0 }
1379 };
1380
1381 static lc_opt_enum_int_var_t arch_var = {
1382         &ia32_isa_template.arch, arch_items
1383 };
1384
1385 static lc_opt_enum_int_var_t opt_arch_var = {
1386         &ia32_isa_template.opt_arch, arch_items
1387 };
1388
1389 static const lc_opt_enum_int_items_t fp_unit_items[] = {
1390         { "x87" ,    fp_x87 },
1391         { "sse2",    fp_sse2 },
1392         { NULL,      0 }
1393 };
1394
1395 static lc_opt_enum_int_var_t fp_unit_var = {
1396         &ia32_isa_template.fp_kind, fp_unit_items
1397 };
1398
1399 static const lc_opt_enum_int_items_t gas_items[] = {
1400         { "linux",   ASM_LINUX_GAS },
1401         { "mingw",   ASM_MINGW_GAS },
1402         { NULL,      0 }
1403 };
1404
1405 static lc_opt_enum_int_var_t gas_var = {
1406         (int *)&asm_flavour, gas_items
1407 };
1408
1409 static const lc_opt_table_entry_t ia32_options[] = {
1410         LC_OPT_ENT_ENUM_INT("arch",      "select the instruction architecture", &arch_var),
1411         LC_OPT_ENT_ENUM_INT("opt",       "optimize for instruction architecture", &opt_arch_var),
1412         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &fp_unit_var),
1413         LC_OPT_ENT_NEGBIT("noaddrmode",  "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
1414         LC_OPT_ENT_NEGBIT("nolea",       "do not optimize for LEAs", &ia32_isa_template.opt, IA32_OPT_LEA),
1415         LC_OPT_ENT_NEGBIT("noplacecnst", "do not place constants", &ia32_isa_template.opt, IA32_OPT_PLACECNST),
1416         LC_OPT_ENT_NEGBIT("noimmop",     "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
1417         LC_OPT_ENT_NEGBIT("noextbb",     "do not use extended basic block scheduling", &ia32_isa_template.opt, IA32_OPT_EXTBB),
1418         LC_OPT_ENT_ENUM_INT("gasmode",   "set the GAS compatibility mode", &gas_var),
1419         { NULL }
1420 };
1421
1422 /**
1423  * Register command line options for the ia32 backend.
1424  *
1425  * Options so far:
1426  *
1427  * ia32-arch=arch    create instruction for arch
1428  * ia32-opt=arch     optimize for run on arch
1429  * ia32-fpunit=unit  select floating point unit (x87 or SSE2)
1430  * ia32-incdec       optimize for inc/dec
1431  * ia32-noaddrmode   do not use address mode
1432  * ia32-nolea        do not optimize for LEAs
1433  * ia32-noplacecnst  do not place constants,
1434  * ia32-noimmop      no operations with immediates
1435  * ia32-noextbb      do not use extended basic block scheduling
1436  * ia32-gasmode      set the GAS compatibility mode
1437  */
1438 static void ia32_register_options(lc_opt_entry_t *ent)
1439 {
1440         lc_opt_entry_t *be_grp_ia32 = lc_opt_get_grp(ent, "ia32");
1441         lc_opt_add_table(be_grp_ia32, ia32_options);
1442 }
1443 #endif /* WITH_LIBCORE */
1444
1445 const arch_isa_if_t ia32_isa_if = {
1446         ia32_init,
1447         ia32_done,
1448         ia32_get_n_reg_class,
1449         ia32_get_reg_class,
1450         ia32_get_reg_class_for_mode,
1451         ia32_get_call_abi,
1452         ia32_get_irn_handler,
1453         ia32_get_code_generator_if,
1454         ia32_get_list_sched_selector,
1455         ia32_get_reg_class_alignment,
1456 #ifdef WITH_LIBCORE
1457         ia32_register_options
1458 #endif
1459 };