795bbb3253be334d99fd26437afa1b3d6fcd5a74
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /**
2  * This is the main ia32 firm backend driver.
3  *
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 static void ia32_abi_dont_save_regs(void *self, pset *s)
296 {
297         ia32_abi_env_t *env = self;
298         if(env->flags.try_omit_fp)
299                 pset_insert_ptr(s, env->isa->bp);
300 }
301
302 /**
303  * Generate the routine prologue.
304  * @param self    The callback object.
305  * @param mem     A pointer to the mem node. Update this if you define new memory.
306  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
307  * @return        The register which shall be used as a stack frame base.
308  *
309  * All nodes which define registers in @p reg_map must keep @p reg_map current.
310  */
311 static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
312 {
313         ia32_abi_env_t *env              = self;
314
315         if (!env->flags.try_omit_fp) {
316                 int reg_size         = get_mode_size_bytes(env->isa->bp->reg_class->mode);
317                 ir_node *bl          = get_irg_start_block(env->irg);
318                 ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
319                 ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
320                 ir_node *push;
321
322                 /* push ebp */
323                 push    = new_rd_ia32_Push(NULL, env->irg, bl, curr_sp, curr_bp, *mem);
324                 curr_sp = new_r_Proj(env->irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
325                 *mem    = new_r_Proj(env->irg, bl, push, mode_M, pn_ia32_Push_M);
326
327                 /* the push must have SP out register */
328                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
329                 set_ia32_flags(push, arch_irn_flags_ignore);
330
331                 /* move esp to ebp */
332                 curr_bp  = be_new_Copy(env->isa->bp->reg_class, env->irg, bl, curr_sp);
333                 be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), env->isa->bp);
334                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
335                 be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
336
337                 /* beware: the copy must be done before any other sp use */
338                 curr_sp = be_new_CopyKeep_single(env->isa->sp->reg_class, env->irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
339                 be_set_constr_single_reg(curr_sp, BE_OUT_POS(0), env->isa->sp);
340                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
341                 be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
342
343                 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
344                 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
345
346                 return env->isa->bp;
347         }
348
349         return env->isa->sp;
350 }
351
352 /**
353  * Generate the routine epilogue.
354  * @param self    The callback object.
355  * @param mem     A pointer to the mem node. Update this if you define new memory.
356  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
357  * @return        The register which shall be used as a stack frame base.
358  *
359  * All nodes which define registers in @p reg_map must keep @p reg_map current.
360  */
361 static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
362 {
363         ia32_abi_env_t *env  = self;
364         ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
365         ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
366
367         if (env->flags.try_omit_fp) {
368                 /* simply remove the stack frame here */
369                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_shrink);
370         }
371         else {
372                 const ia32_isa_t *isa = (ia32_isa_t *)env->isa;
373                 ir_mode *mode_bp = env->isa->bp->reg_class->mode;
374                 int reg_size     = get_mode_size_bytes(env->isa->bp->reg_class->mode);
375
376                 /* gcc always emits a leave at the end of a routine */
377                 if (1 || ARCH_AMD(isa->opt_arch)) {
378                         ir_node *leave;
379
380                         /* leave */
381                         leave = new_rd_ia32_Leave(NULL, env->irg, bl, curr_sp, *mem);
382                         set_ia32_flags(leave, arch_irn_flags_ignore);
383                         curr_bp = new_r_Proj(current_ir_graph, bl, leave, mode_bp, pn_ia32_Leave_frame);
384                         curr_sp = new_r_Proj(current_ir_graph, bl, leave, get_irn_mode(curr_sp), pn_ia32_Leave_stack);
385                         *mem    = new_r_Proj(current_ir_graph, bl, leave, mode_M, pn_ia32_Leave_M);
386                 }
387                 else {
388                         ir_node *pop;
389
390                         /* copy ebp to esp */
391                         curr_sp = be_new_SetSP(env->isa->sp, env->irg, bl, curr_sp, curr_bp, *mem);
392
393                         /* pop ebp */
394                         pop = new_rd_ia32_Pop(NULL, env->irg, bl, curr_sp, *mem);
395                         set_ia32_flags(pop, arch_irn_flags_ignore);
396                         curr_bp = new_r_Proj(current_ir_graph, bl, pop, mode_bp, pn_ia32_Pop_res);
397                         curr_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(curr_sp), pn_ia32_Pop_stack);
398                         *mem    = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
399                 }
400                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
401                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
402         }
403
404         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
405         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
406 }
407
408 /**
409  * Produces the type which sits between the stack args and the locals on the stack.
410  * it will contain the return address and space to store the old base pointer.
411  * @return The Firm type modeling the ABI between type.
412  */
413 static ir_type *ia32_abi_get_between_type(void *self)
414 {
415         static ir_type *omit_fp_between_type = NULL;
416         static ir_type *between_type         = NULL;
417
418         ia32_abi_env_t *env = self;
419
420         if(!between_type) {
421                 entity *old_bp_ent;
422                 entity *ret_addr_ent;
423                 entity *omit_fp_ret_addr_ent;
424
425                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
426                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
427
428                 between_type           = new_type_class(new_id_from_str("ia32_between_type"));
429                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
430                 ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
431
432                 set_entity_offset_bytes(old_bp_ent, 0);
433                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
434                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
435
436                 omit_fp_between_type   = new_type_class(new_id_from_str("ia32_between_type_omit_fp"));
437                 omit_fp_ret_addr_ent   = new_entity(omit_fp_between_type, new_id_from_str("ret_addr"), ret_addr_type);
438
439                 set_entity_offset_bytes(omit_fp_ret_addr_ent, 0);
440                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
441         }
442
443         return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
444 }
445
446 static const be_abi_callbacks_t ia32_abi_callbacks = {
447         ia32_abi_init,
448         free,
449         ia32_abi_get_between_type,
450         ia32_abi_dont_save_regs,
451         ia32_abi_prologue,
452         ia32_abi_epilogue,
453 };
454
455 /* fill register allocator interface */
456
457 static const arch_irn_ops_if_t ia32_irn_ops_if = {
458         ia32_get_irn_reg_req,
459         ia32_set_irn_reg,
460         ia32_get_irn_reg,
461         ia32_classify,
462         ia32_get_flags,
463         ia32_get_frame_entity,
464         ia32_set_stack_bias
465 };
466
467 ia32_irn_ops_t ia32_irn_ops = {
468         &ia32_irn_ops_if,
469         NULL
470 };
471
472
473
474 /**************************************************
475  *                _                         _  __
476  *               | |                       (_)/ _|
477  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
478  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
479  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
480  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
481  *                        __/ |
482  *                       |___/
483  **************************************************/
484
485 /**
486  * Transforms the standard firm graph into
487  * an ia32 firm graph
488  */
489 static void ia32_prepare_graph(void *self) {
490         ia32_code_gen_t *cg = self;
491         DEBUG_ONLY(firm_dbg_module_t *old_mod = cg->mod;)
492
493         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.transform");
494         ia32_register_transformers();
495         irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_node, cg);
496         be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
497
498         if (cg->opt & IA32_OPT_DOAM) {
499                 edges_deactivate(cg->irg);
500                 //dead_node_elimination(cg->irg);
501                 edges_activate(cg->irg);
502
503                 FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.am");
504
505                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
506                 be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
507         }
508
509         DEBUG_ONLY(cg->mod = old_mod;)
510 }
511
512
513 /**
514  * Insert copies for all ia32 nodes where the should_be_same requirement
515  * is not fulfilled.
516  * Transform Sub into Neg -- Add if IN2 == OUT
517  */
518 static void ia32_finish_node(ir_node *irn, void *env) {
519         ia32_code_gen_t            *cg = env;
520         const ia32_register_req_t **reqs;
521         const arch_register_t      *out_reg, *in_reg, *in2_reg;
522         int                         n_res, i;
523         ir_node                    *copy, *in_node, *block, *in2_node;
524         ia32_op_type_t              op_tp;
525
526         if (is_ia32_irn(irn)) {
527                 /* AM Dest nodes don't produce any values  */
528                 op_tp = get_ia32_op_type(irn);
529                 if (op_tp == ia32_AddrModeD)
530                         goto end;
531
532                 reqs  = get_ia32_out_req_all(irn);
533                 n_res = get_ia32_n_res(irn);
534                 block = get_nodes_block(irn);
535
536                 /* check all OUT requirements, if there is a should_be_same */
537                 if ((op_tp == ia32_Normal || op_tp == ia32_AddrModeS) &&
538                         ! is_ia32_Lea(irn) && ! is_ia32_Conv_I2I(irn) && ! is_ia32_Conv_I2I8Bit(irn))
539                 {
540                         for (i = 0; i < n_res; i++) {
541                                 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
542                                         /* get in and out register */
543                                         out_reg  = get_ia32_out_reg(irn, i);
544                                         in_node  = get_irn_n(irn, reqs[i]->same_pos);
545                                         in_reg   = arch_get_irn_register(cg->arch_env, in_node);
546
547                                         /* don't copy ignore nodes */
548                                         if (arch_irn_is(cg->arch_env, in_node, ignore) && is_Proj(in_node))
549                                                 continue;
550
551                                         /* check if in and out register are equal */
552                                         if (! REGS_ARE_EQUAL(out_reg, in_reg)) {
553                                                 /* in case of a commutative op: just exchange the in's */
554                                                 /* beware: the current op could be everything, so test for ia32 */
555                                                 /*         commutativity first before getting the second in     */
556                                                 if (is_ia32_commutative(irn)) {
557                                                         in2_node = get_irn_n(irn, reqs[i]->same_pos ^ 1);
558                                                         in2_reg  = arch_get_irn_register(cg->arch_env, in2_node);
559
560                                                         if (REGS_ARE_EQUAL(out_reg, in2_reg)) {
561                                                                 set_irn_n(irn, reqs[i]->same_pos, in2_node);
562                                                                 set_irn_n(irn, reqs[i]->same_pos ^ 1, in_node);
563                                                         }
564                                                         else
565                                                                 goto insert_copy;
566                                                 }
567                                                 else {
568 insert_copy:
569                                                         DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
570                                                         /* create copy from in register */
571                                                         copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
572
573                                                         DBG_OPT_2ADDRCPY(copy);
574
575                                                         /* destination is the out register */
576                                                         arch_set_irn_register(cg->arch_env, copy, out_reg);
577
578                                                         /* insert copy before the node into the schedule */
579                                                         sched_add_before(irn, copy);
580
581                                                         /* set copy as in */
582                                                         set_irn_n(irn, reqs[i]->same_pos, copy);
583                                                 }
584                                         }
585                                 }
586                         }
587                 }
588
589                 /* If we have a CondJmp with immediate, we need to    */
590                 /* check if it's the right operand, otherwise we have */
591                 /* to change it, as CMP doesn't support immediate as  */
592                 /* left operands.                                     */
593                 if (is_ia32_CondJmp(irn) && (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn)) && op_tp == ia32_AddrModeS) {
594                         set_ia32_op_type(irn, ia32_AddrModeD);
595                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
596                 }
597
598                 /* check if there is a sub which need to be transformed */
599                 ia32_transform_sub_to_neg_add(irn, cg);
600
601                 /* transform a LEA into an Add if possible */
602                 ia32_transform_lea_to_add(irn, cg);
603         }
604 end:
605
606         /* check for peephole optimization */
607         ia32_peephole_optimization(irn, cg);
608 }
609
610 static void ia32_finish_irg_walker(ir_node *block, void *env) {
611         ir_node *irn, *next;
612
613         for (irn = sched_first(block); !sched_is_end(irn); irn = next) {
614                 next = sched_next(irn);
615                 ia32_finish_node(irn, env);
616         }
617 }
618
619 /**
620  * Add Copy nodes for not fulfilled should_be_equal constraints
621  */
622 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
623         irg_block_walk_graph(irg, NULL, ia32_finish_irg_walker, cg);
624 }
625
626
627
628 /**
629  * Dummy functions for hooks we don't need but which must be filled.
630  */
631 static void ia32_before_sched(void *self) {
632 }
633
634 /**
635  * Called before the register allocator.
636  * Calculate a block schedule here. We need it for the x87
637  * simulator and the emitter.
638  */
639 static void ia32_before_ra(void *self) {
640         ia32_code_gen_t *cg = self;
641
642         cg->blk_sched = sched_create_block_schedule(cg->irg);
643 }
644
645
646 /**
647  * Transforms a be node into a Load.
648  */
649 static void transform_to_Load(ia32_transform_env_t *env) {
650         ir_node *irn         = env->irn;
651         entity  *ent         = be_get_frame_entity(irn);
652         ir_mode *mode        = env->mode;
653         ir_node *noreg       = ia32_new_NoReg_gp(env->cg);
654         ir_node *nomem       = new_rd_NoMem(env->irg);
655         ir_node *sched_point = NULL;
656         ir_node *ptr         = get_irn_n(irn, 0);
657         ir_node *mem         = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
658         ir_node *new_op, *proj;
659         const arch_register_t *reg;
660
661         if (sched_is_scheduled(irn)) {
662                 sched_point = sched_prev(irn);
663         }
664
665         if (mode_is_float(mode)) {
666                 if (USE_SSE2(env->cg))
667                         new_op = new_rd_ia32_xLoad(env->dbg, env->irg, env->block, ptr, noreg, mem);
668                 else
669                         new_op = new_rd_ia32_vfld(env->dbg, env->irg, env->block, ptr, noreg, mem);
670         }
671         else {
672                 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem);
673         }
674
675         set_ia32_am_support(new_op, ia32_am_Source);
676         set_ia32_op_type(new_op, ia32_AddrModeS);
677         set_ia32_am_flavour(new_op, ia32_B);
678         set_ia32_ls_mode(new_op, mode);
679         set_ia32_frame_ent(new_op, ent);
680         set_ia32_use_frame(new_op);
681
682         DBG_OPT_RELOAD2LD(irn, new_op);
683
684         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
685
686         if (sched_point) {
687                 sched_add_after(sched_point, new_op);
688                 sched_add_after(new_op, proj);
689
690                 sched_remove(irn);
691         }
692
693         /* copy the register from the old node to the new Load */
694         reg = arch_get_irn_register(env->cg->arch_env, irn);
695         arch_set_irn_register(env->cg->arch_env, new_op, reg);
696
697         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
698
699         exchange(irn, proj);
700 }
701
702 /**
703  * Transforms a be node into a Store.
704  */
705 static void transform_to_Store(ia32_transform_env_t *env) {
706         ir_node *irn   = env->irn;
707         entity  *ent   = be_get_frame_entity(irn);
708         ir_mode *mode  = env->mode;
709         ir_node *noreg = ia32_new_NoReg_gp(env->cg);
710         ir_node *nomem = new_rd_NoMem(env->irg);
711         ir_node *ptr   = get_irn_n(irn, 0);
712         ir_node *val   = get_irn_n(irn, 1);
713         ir_node *new_op, *proj;
714         ir_node *sched_point = NULL;
715
716         if (sched_is_scheduled(irn)) {
717                 sched_point = sched_prev(irn);
718         }
719
720         if (mode_is_float(mode)) {
721                 if (USE_SSE2(env->cg))
722                         new_op = new_rd_ia32_xStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
723                 else
724                         new_op = new_rd_ia32_vfst(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
725         }
726         else if (get_mode_size_bits(mode) == 8) {
727                 new_op = new_rd_ia32_Store8Bit(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
728         }
729         else {
730                 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem);
731         }
732
733         set_ia32_am_support(new_op, ia32_am_Dest);
734         set_ia32_op_type(new_op, ia32_AddrModeD);
735         set_ia32_am_flavour(new_op, ia32_B);
736         set_ia32_ls_mode(new_op, mode);
737         set_ia32_frame_ent(new_op, ent);
738         set_ia32_use_frame(new_op);
739
740         DBG_OPT_SPILL2ST(irn, new_op);
741
742         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode_M, pn_ia32_Store_M);
743
744         if (sched_point) {
745                 sched_add_after(sched_point, new_op);
746                 sched_add_after(new_op, proj);
747
748                 sched_remove(irn);
749         }
750
751         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env->cg, new_op));
752
753         exchange(irn, proj);
754 }
755
756 /**
757  * Fix the mode of Spill/Reload
758  */
759 static ir_mode *fix_spill_mode(ia32_code_gen_t *cg, ir_mode *mode)
760 {
761         if (mode_is_float(mode)) {
762                 if (USE_SSE2(cg))
763                         mode = mode_D;
764                 else
765                         mode = mode_E;
766         }
767         else
768                 mode = mode_Is;
769         return mode;
770 }
771
772 /**
773  * Block-Walker: Calls the transform functions Spill and Reload.
774  */
775 static void ia32_after_ra_walker(ir_node *block, void *env) {
776         ir_node *node, *prev;
777         ia32_code_gen_t *cg = env;
778         ia32_transform_env_t tenv;
779
780         tenv.block = block;
781         tenv.irg   = current_ir_graph;
782         tenv.cg    = cg;
783         DEBUG_ONLY(tenv.mod = cg->mod;)
784
785         /* beware: the schedule is changed here */
786         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
787                 prev = sched_prev(node);
788                 if (be_is_Reload(node)) {
789                         /* we always reload the whole register  */
790                         tenv.dbg  = get_irn_dbg_info(node);
791                         tenv.irn  = node;
792                         tenv.mode = fix_spill_mode(cg, get_irn_mode(node));
793                         transform_to_Load(&tenv);
794                 }
795                 else if (be_is_Spill(node)) {
796                         /* we always spill the whole register  */
797                         tenv.dbg  = get_irn_dbg_info(node);
798                         tenv.irn  = node;
799                         tenv.mode = fix_spill_mode(cg, get_irn_mode(be_get_Spill_context(node)));
800                         transform_to_Store(&tenv);
801                 }
802         }
803 }
804
805 /**
806  * We transform Spill and Reload here. This needs to be done before
807  * stack biasing otherwise we would miss the corrected offset for these nodes.
808  *
809  * If x87 instruction should be emitted, run the x87 simulator and patch
810  * the virtual instructions. This must obviously be done after register allocation.
811  */
812 static void ia32_after_ra(void *self) {
813         ia32_code_gen_t *cg = self;
814         irg_block_walk_graph(cg->irg, NULL, ia32_after_ra_walker, self);
815
816         /* if we do x87 code generation, rewrite all the virtual instructions and registers */
817         if (cg->used_fp == fp_x87) {
818                 x87_simulate_graph(cg->arch_env, cg->irg, cg->blk_sched);
819         }
820 }
821
822
823 /**
824  * Emits the code, closes the output file and frees
825  * the code generator interface.
826  */
827 static void ia32_codegen(void *self) {
828         ia32_code_gen_t *cg = self;
829         ir_graph        *irg = cg->irg;
830
831         ia32_finish_irg(irg, cg);
832         be_dump(irg, "-finished", dump_ir_block_graph_sched);
833         ia32_gen_routine(cg->isa->out, irg, cg);
834
835         cur_reg_set = NULL;
836
837         /* remove it from the isa */
838         cg->isa->cg = NULL;
839
840         /* de-allocate code generator */
841         del_set(cg->reg_set);
842         free(self);
843
844 }
845
846 static void *ia32_cg_init(const be_irg_t *birg);
847
848 static const arch_code_generator_if_t ia32_code_gen_if = {
849         ia32_cg_init,
850         NULL,                /* before abi introduce hook */
851         ia32_prepare_graph,
852         ia32_before_sched,   /* before scheduling hook */
853         ia32_before_ra,      /* before register allocation hook */
854         ia32_after_ra,       /* after register allocation hook */
855         ia32_codegen         /* emit && done */
856 };
857
858 /**
859  * Initializes a IA32 code generator.
860  */
861 static void *ia32_cg_init(const be_irg_t *birg) {
862         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
863         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
864
865         cg->impl      = &ia32_code_gen_if;
866         cg->irg       = birg->irg;
867         cg->reg_set   = new_set(ia32_cmp_irn_reg_assoc, 1024);
868         cg->arch_env  = birg->main_env->arch_env;
869         cg->isa       = isa;
870         cg->birg      = birg;
871         cg->blk_sched = NULL;
872         cg->fp_to_gp  = NULL;
873         cg->gp_to_fp  = NULL;
874         cg->fp_kind   = isa->fp_kind;
875         cg->used_fp   = fp_none;
876
877         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.cg");
878
879         /* copy optimizations from isa for easier access */
880         cg->opt = isa->opt;
881
882         /* enter it */
883         isa->cg = cg;
884
885 #ifndef NDEBUG
886         if (isa->name_obst_size) {
887                 //printf("freed %d bytes from name obst\n", isa->name_obst_size);
888                 isa->name_obst_size = 0;
889                 obstack_free(isa->name_obst, NULL);
890                 obstack_init(isa->name_obst);
891         }
892 #endif /* NDEBUG */
893
894         cur_reg_set = cg->reg_set;
895
896         ia32_irn_ops.cg = cg;
897
898         return (arch_code_generator_t *)cg;
899 }
900
901
902
903 /*****************************************************************
904  *  ____             _                  _   _____  _____
905  * |  _ \           | |                | | |_   _|/ ____|  /\
906  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
907  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
908  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
909  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
910  *
911  *****************************************************************/
912
913 /**
914  * The template that generates a new ISA object.
915  * Note that this template can be changed by command line
916  * arguments.
917  */
918 static ia32_isa_t ia32_isa_template = {
919   {
920           &ia32_isa_if,            /* isa interface implementation */
921           &ia32_gp_regs[REG_ESP],  /* stack pointer register */
922           &ia32_gp_regs[REG_EBP],  /* base pointer register */
923           -1,                      /* stack direction */
924   },
925         NULL,                    /* 16bit register names */
926         NULL,                    /* 8bit register names */
927         NULL,                    /* types */
928         NULL,                    /* tv_ents */
929         (0 |
930         IA32_OPT_INCDEC    |     /* optimize add 1, sub 1 into inc/dec               default: on  */
931         IA32_OPT_DOAM      |     /* optimize address mode                            default: on  */
932         IA32_OPT_PLACECNST |     /* place constants immediately before instructions, default: on  */
933         IA32_OPT_IMMOPS    |     /* operations can use immediates,                   default: on  */
934         IA32_OPT_EXTBB),         /* use extended basic block scheduling,             default: on  */
935         arch_pentium_4,          /* instruction architecture */
936         arch_pentium_4,          /* optimize for architecture */
937         fp_sse2,                 /* use sse2 unit */
938         NULL,                    /* current code generator */
939 #ifndef NDEBUG
940         NULL,                    /* name obstack */
941         0                        /* name obst size */
942 #endif
943 };
944
945 /**
946  * Initializes the backend ISA.
947  */
948 static void *ia32_init(FILE *file_handle) {
949         static int inited = 0;
950         ia32_isa_t *isa;
951
952         if (inited)
953                 return NULL;
954
955         isa = xmalloc(sizeof(*isa));
956         memcpy(isa, &ia32_isa_template, sizeof(*isa));
957
958         ia32_register_init(isa);
959         ia32_create_opcodes();
960         ia32_register_copy_attr_func();
961
962         if ((ARCH_INTEL(isa->arch) && isa->arch < arch_pentium_4) ||
963             (ARCH_AMD(isa->arch) && isa->arch < arch_athlon))
964                 /* no SSE2 for these cpu's */
965                 isa->fp_kind = fp_x87;
966
967         if (ARCH_INTEL(isa->opt_arch) && isa->opt_arch >= arch_pentium_4) {
968                 /* Pentium 4 don't like inc and dec instructions */
969                 isa->opt &= ~IA32_OPT_INCDEC;
970         }
971
972         isa->regs_16bit = pmap_create();
973         isa->regs_8bit  = pmap_create();
974         isa->types      = pmap_create();
975         isa->tv_ent     = pmap_create();
976         isa->out        = file_handle;
977
978         ia32_build_16bit_reg_map(isa->regs_16bit);
979         ia32_build_8bit_reg_map(isa->regs_8bit);
980
981         /* patch register names of x87 registers */
982         if (USE_x87(isa)) {
983           ia32_st_regs[0].name = "st";
984           ia32_st_regs[1].name = "st(1)";
985           ia32_st_regs[2].name = "st(2)";
986           ia32_st_regs[3].name = "st(3)";
987           ia32_st_regs[4].name = "st(4)";
988           ia32_st_regs[5].name = "st(5)";
989           ia32_st_regs[6].name = "st(6)";
990           ia32_st_regs[7].name = "st(7)";
991         }
992
993 #ifndef NDEBUG
994         isa->name_obst = xmalloc(sizeof(*isa->name_obst));
995         obstack_init(isa->name_obst);
996         isa->name_obst_size = 0;
997 #endif /* NDEBUG */
998
999         fprintf(isa->out, "\t.intel_syntax\n");
1000
1001         inited = 1;
1002
1003         return isa;
1004 }
1005
1006
1007
1008 /**
1009  * Closes the output file and frees the ISA structure.
1010  */
1011 static void ia32_done(void *self) {
1012         ia32_isa_t *isa = self;
1013
1014         /* emit now all global declarations */
1015         ia32_gen_decls(isa->out);
1016
1017         pmap_destroy(isa->regs_16bit);
1018         pmap_destroy(isa->regs_8bit);
1019         pmap_destroy(isa->tv_ent);
1020         pmap_destroy(isa->types);
1021
1022 #ifndef NDEBUG
1023         //printf("name obst size = %d bytes\n", isa->name_obst_size);
1024         obstack_free(isa->name_obst, NULL);
1025 #endif /* NDEBUG */
1026
1027         free(self);
1028 }
1029
1030
1031 /**
1032  * Return the number of register classes for this architecture.
1033  * We report always these:
1034  *  - the general purpose registers
1035  *  - the floating point register set (depending on the unit used for FP)
1036  *  - MMX/SSE registers (currently not supported)
1037  */
1038 static int ia32_get_n_reg_class(const void *self) {
1039         return 2;
1040 }
1041
1042 /**
1043  * Return the register class for index i.
1044  */
1045 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
1046         const ia32_isa_t *isa = self;
1047         assert(i >= 0 && i < 2 && "Invalid ia32 register class requested.");
1048         if (i == 0)
1049                 return &ia32_reg_classes[CLASS_ia32_gp];
1050         return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1051 }
1052
1053 /**
1054  * Get the register class which shall be used to store a value of a given mode.
1055  * @param self The this pointer.
1056  * @param mode The mode in question.
1057  * @return A register class which can hold values of the given mode.
1058  */
1059 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
1060         const ia32_isa_t *isa = self;
1061         if (mode_is_float(mode)) {
1062                 return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1063         }
1064         else
1065                 return &ia32_reg_classes[CLASS_ia32_gp];
1066 }
1067
1068 /**
1069  * Get the ABI restrictions for procedure calls.
1070  * @param self        The this pointer.
1071  * @param method_type The type of the method (procedure) in question.
1072  * @param abi         The abi object to be modified
1073  */
1074 static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
1075         const ia32_isa_t *isa = self;
1076         ir_type  *tp;
1077         ir_mode  *mode;
1078         unsigned  cc        = get_method_calling_convention(method_type);
1079         int       n         = get_method_n_params(method_type);
1080         int       biggest_n = -1;
1081         int       stack_idx = 0;
1082         int       i, ignore_1, ignore_2;
1083         ir_mode **modes;
1084         const arch_register_t *reg;
1085         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1086
1087         unsigned use_push = !IS_P6_ARCH(isa->opt_arch);
1088
1089         /* set abi flags for calls */
1090         call_flags.bits.left_to_right         = 0;  /* always last arg first on stack */
1091         call_flags.bits.store_args_sequential = use_push;
1092         /* call_flags.bits.try_omit_fp                 not changed: can handle both settings */
1093         call_flags.bits.fp_free               = 0;  /* the frame pointer is fixed in IA32 */
1094         call_flags.bits.call_has_imm          = 1;  /* IA32 calls can have immediate address */
1095
1096         /* set stack parameter passing style */
1097         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1098
1099         /* collect the mode for each type */
1100         modes = alloca(n * sizeof(modes[0]));
1101
1102         for (i = 0; i < n; i++) {
1103                 tp       = get_method_param_type(method_type, i);
1104                 modes[i] = get_type_mode(tp);
1105         }
1106
1107         /* set register parameters  */
1108         if (cc & cc_reg_param) {
1109                 /* determine the number of parameters passed via registers */
1110                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore_1, &ignore_2);
1111
1112                 /* loop over all parameters and set the register requirements */
1113                 for (i = 0; i <= biggest_n; i++) {
1114                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
1115                         assert(reg && "kaputt");
1116                         be_abi_call_param_reg(abi, i, reg);
1117                 }
1118
1119                 stack_idx = i;
1120         }
1121
1122
1123         /* set stack parameters */
1124         for (i = stack_idx; i < n; i++) {
1125                 be_abi_call_param_stack(abi, i, 1, 0, 0);
1126         }
1127
1128
1129         /* set return registers */
1130         n = get_method_n_ress(method_type);
1131
1132         assert(n <= 2 && "more than two results not supported");
1133
1134         /* In case of 64bit returns, we will have two 32bit values */
1135         if (n == 2) {
1136                 tp   = get_method_res_type(method_type, 0);
1137                 mode = get_type_mode(tp);
1138
1139                 assert(!mode_is_float(mode) && "two FP results not supported");
1140
1141                 tp   = get_method_res_type(method_type, 1);
1142                 mode = get_type_mode(tp);
1143
1144                 assert(!mode_is_float(mode) && "two FP results not supported");
1145
1146                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
1147                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
1148         }
1149         else if (n == 1) {
1150                 const arch_register_t *reg;
1151
1152                 tp   = get_method_res_type(method_type, 0);
1153                 assert(is_atomic_type(tp));
1154                 mode = get_type_mode(tp);
1155
1156                 reg = mode_is_float(mode) ?
1157                         (USE_SSE2(isa) ? &ia32_xmm_regs[REG_XMM0] : &ia32_vfp_regs[REG_VF0]) :
1158                         &ia32_gp_regs[REG_EAX];
1159
1160                 be_abi_call_res_reg(abi, 0, reg);
1161         }
1162 }
1163
1164
1165 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1166         return &ia32_irn_ops;
1167 }
1168
1169 const arch_irn_handler_t ia32_irn_handler = {
1170         ia32_get_irn_ops
1171 };
1172
1173 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
1174         return &ia32_irn_handler;
1175 }
1176
1177 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1178         return is_ia32_irn(irn);
1179 }
1180
1181 /**
1182  * Initializes the code generator interface.
1183  */
1184 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
1185         return &ia32_code_gen_if;
1186 }
1187
1188 list_sched_selector_t ia32_sched_selector;
1189
1190 /**
1191  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
1192  */
1193 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
1194 //      memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1195         memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
1196         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
1197         return &ia32_sched_selector;
1198 }
1199
1200 /**
1201  * Returns the necessary byte alignment for storing a register of given class.
1202  */
1203 static int ia32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1204         ir_mode *mode = arch_register_class_mode(cls);
1205         int bytes     = get_mode_size_bytes(mode);
1206
1207         if (mode_is_float(mode) && bytes > 8)
1208                 return 16;
1209         return bytes;
1210 }
1211
1212 #ifdef WITH_LIBCORE
1213
1214 /* instruction set architectures. */
1215 static const lc_opt_enum_int_items_t arch_items[] = {
1216         { "386",        arch_i386, },
1217         { "486",        arch_i486, },
1218         { "pentium",    arch_pentium, },
1219         { "586",        arch_pentium, },
1220         { "pentiumpro", arch_pentium_pro, },
1221         { "686",        arch_pentium_pro, },
1222         { "pentiummmx", arch_pentium_mmx, },
1223         { "pentium2",   arch_pentium_2, },
1224         { "p2",         arch_pentium_2, },
1225         { "pentium3",   arch_pentium_3, },
1226         { "p3",         arch_pentium_3, },
1227         { "pentium4",   arch_pentium_4, },
1228         { "p4",         arch_pentium_4, },
1229         { "pentiumm",   arch_pentium_m, },
1230         { "pm",         arch_pentium_m, },
1231         { "core",       arch_core, },
1232         { "k6",         arch_k6, },
1233         { "athlon",     arch_athlon, },
1234         { "athlon64",   arch_athlon_64, },
1235         { "opteron",    arch_opteron, },
1236         { NULL,         0 }
1237 };
1238
1239 static lc_opt_enum_int_var_t arch_var = {
1240         &ia32_isa_template.arch, arch_items
1241 };
1242
1243 static lc_opt_enum_int_var_t opt_arch_var = {
1244         &ia32_isa_template.opt_arch, arch_items
1245 };
1246
1247 static const lc_opt_enum_int_items_t fp_unit_items[] = {
1248         { "x87" ,    fp_x87 },
1249         { "sse2",    fp_sse2 },
1250         { NULL,      0 }
1251 };
1252
1253 static lc_opt_enum_int_var_t fp_unit_var = {
1254         &ia32_isa_template.fp_kind, fp_unit_items
1255 };
1256
1257 static const lc_opt_enum_int_items_t gas_items[] = {
1258         { "linux",   ASM_LINUX_GAS },
1259         { "mingw",   ASM_MINGW_GAS },
1260         { NULL,      0 }
1261 };
1262
1263 static lc_opt_enum_int_var_t gas_var = {
1264         &asm_flavour, gas_items
1265 };
1266
1267 static const lc_opt_table_entry_t ia32_options[] = {
1268         LC_OPT_ENT_ENUM_INT("arch",      "select the instruction architecture", &arch_var),
1269         LC_OPT_ENT_ENUM_INT("opt",       "optimize for instruction architecture", &opt_arch_var),
1270         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &fp_unit_var),
1271         LC_OPT_ENT_NEGBIT("noaddrmode",  "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
1272         LC_OPT_ENT_NEGBIT("noplacecnst", "do not place constants", &ia32_isa_template.opt, IA32_OPT_PLACECNST),
1273         LC_OPT_ENT_NEGBIT("noimmop",     "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
1274         LC_OPT_ENT_NEGBIT("noextbb",     "do not use extended basic block scheduling", &ia32_isa_template.opt, IA32_OPT_EXTBB),
1275         LC_OPT_ENT_ENUM_INT("gasmode",   "set the GAS compatibility mode", &gas_var),
1276         { NULL }
1277 };
1278
1279 /**
1280  * Register command line options for the ia32 backend.
1281  *
1282  * Options so far:
1283  *
1284  * ia32-arch=arch    create instruction for arch
1285  * ia32-opt=arch     optimize for run on arch
1286  * ia32-fpunit=unit  select floating point unit (x87 or SSE2)
1287  * ia32-incdec       optimize for inc/dec
1288  * ia32-noaddrmode   do not use address mode
1289  * ia32-noplacecnst  do not place constants,
1290  * ia32-noimmop      no operations with immediates
1291  * ia32-noextbb      do not use extended basic block scheduling
1292  * ia32-gasmode      set the GAS compatibility mode
1293  */
1294 static void ia32_register_options(lc_opt_entry_t *ent)
1295 {
1296         lc_opt_entry_t *be_grp_ia32 = lc_opt_get_grp(ent, "ia32");
1297         lc_opt_add_table(be_grp_ia32, ia32_options);
1298 }
1299 #endif /* WITH_LIBCORE */
1300
1301 const arch_isa_if_t ia32_isa_if = {
1302         ia32_init,
1303         ia32_done,
1304         ia32_get_n_reg_class,
1305         ia32_get_reg_class,
1306         ia32_get_reg_class_for_mode,
1307         ia32_get_call_abi,
1308         ia32_get_irn_handler,
1309         ia32_get_code_generator_if,
1310         ia32_get_list_sched_selector,
1311         ia32_get_reg_class_alignment,
1312 #ifdef WITH_LIBCORE
1313         ia32_register_options
1314 #endif
1315 };