becopyheur4: Clean up co_mst_irn_init().
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       This is the main ia32 firm backend driver.
9  * @author      Christian Wuerdig
10  */
11 #include "config.h"
12
13 #include "lc_opts.h"
14 #include "lc_opts_enum.h"
15 #include "irarch.h"
16 #include "irgwalk.h"
17 #include "irprog.h"
18 #include "irprintf.h"
19 #include "iredges_t.h"
20 #include "ircons.h"
21 #include "irflag.h"
22 #include "irgmod.h"
23 #include "irgopt.h"
24 #include "irgopt.h"
25 #include "irdump.h"
26 #include "pdeq.h"
27 #include "pset.h"
28 #include "debug.h"
29 #include "error.h"
30 #include "xmalloc.h"
31 #include "irtools.h"
32 #include "iroptimize.h"
33 #include "instrument.h"
34 #include "iropt_t.h"
35 #include "lower_dw.h"
36 #include "lower_calls.h"
37 #include "lower_mode_b.h"
38 #include "lower_softfloat.h"
39 #include "firmstat_t.h"
40
41 #include "beabi.h"
42 #include "benode.h"
43 #include "belower.h"
44 #include "besched.h"
45 #include "be.h"
46 #include "be_t.h"
47 #include "beirgmod.h"
48 #include "beblocksched.h"
49 #include "bespillutil.h"
50 #include "bespillslots.h"
51 #include "bemodule.h"
52 #include "begnuas.h"
53 #include "bestate.h"
54 #include "beflags.h"
55 #include "betranshlp.h"
56 #include "belistsched.h"
57 #include "beabihelper.h"
58 #include "bestack.h"
59
60 #include "bearch_ia32_t.h"
61
62 #include "ia32_new_nodes.h"
63 #include "gen_ia32_regalloc_if.h"
64 #include "ia32_common_transform.h"
65 #include "ia32_transform.h"
66 #include "ia32_emitter.h"
67 #include "ia32_optimize.h"
68 #include "ia32_x87.h"
69 #include "ia32_dbg_stat.h"
70 #include "ia32_finish.h"
71 #include "ia32_fpu.h"
72 #include "ia32_architecture.h"
73
74 #ifdef FIRM_GRGEN_BE
75 #include "ia32_pbqp_transform.h"
76
77 transformer_t be_transformer = TRANSFORMER_DEFAULT;
78 #endif
79
80 ir_mode *ia32_mode_fpcw;
81 ir_mode *ia32_mode_E;
82 ir_type *ia32_type_E;
83
84 /** The current omit-fp state */
85 static ir_type *omit_fp_between_type   = NULL;
86 static ir_type *between_type           = NULL;
87 static ir_entity *old_bp_ent           = NULL;
88 static ir_entity *ret_addr_ent         = NULL;
89 static ir_entity *omit_fp_ret_addr_ent = NULL;
90 static int        precise_x87_spills;
91
92 /**
93  * The environment for the intrinsic mapping.
94  */
95 static ia32_intrinsic_env_t intrinsic_env = {
96         NULL,    /* entity for __divdi3 library call */
97         NULL,    /* entity for __moddi3 library call */
98         NULL,    /* entity for __udivdi3 library call */
99         NULL,    /* entity for __umoddi3 library call */
100 };
101
102
103 typedef ir_node *(*create_const_node_func) (dbg_info *dbgi, ir_node *block);
104
105 /**
106  * Used to create per-graph unique pseudo nodes.
107  */
108 static inline ir_node *create_const(ir_graph *irg, ir_node **place,
109                                     create_const_node_func func,
110                                     const arch_register_t* reg)
111 {
112         ir_node *block, *res;
113
114         if (*place != NULL)
115                 return *place;
116
117         block = get_irg_start_block(irg);
118         res = func(NULL, block);
119         arch_set_irn_register(res, reg);
120         *place = res;
121
122         return res;
123 }
124
125 /* Creates the unique per irg GP NoReg node. */
126 ir_node *ia32_new_NoReg_gp(ir_graph *irg)
127 {
128         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
129         return create_const(irg, &irg_data->noreg_gp, new_bd_ia32_NoReg_GP,
130                             &ia32_registers[REG_GP_NOREG]);
131 }
132
133 ir_node *ia32_new_NoReg_fp(ir_graph *irg)
134 {
135         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
136         return create_const(irg, &irg_data->noreg_fp, new_bd_ia32_NoReg_FP,
137                             &ia32_registers[REG_FP_NOREG]);
138 }
139
140 ir_node *ia32_new_NoReg_xmm(ir_graph *irg)
141 {
142         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
143         return create_const(irg, &irg_data->noreg_xmm, new_bd_ia32_NoReg_XMM,
144                             &ia32_registers[REG_XMM_NOREG]);
145 }
146
147 ir_node *ia32_new_Fpu_truncate(ir_graph *irg)
148 {
149         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
150         return create_const(irg, &irg_data->fpu_trunc_mode, new_bd_ia32_ChangeCW,
151                         &ia32_registers[REG_FPCW]);
152 }
153
154
155 /**
156  * Returns the admissible noreg register node for input register pos of node irn.
157  */
158 static ir_node *ia32_get_admissible_noreg(ir_node *irn, int pos)
159 {
160         ir_graph                  *irg = get_irn_irg(irn);
161         const arch_register_req_t *req = arch_get_irn_register_req_in(irn, pos);
162
163         assert(req != NULL && "Missing register requirements");
164         if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
165                 return ia32_new_NoReg_gp(irg);
166
167         if (ia32_cg_config.use_sse2) {
168                 return ia32_new_NoReg_xmm(irg);
169         } else {
170                 return ia32_new_NoReg_fp(irg);
171         }
172 }
173
174 static ir_entity *ia32_get_frame_entity(const ir_node *irn)
175 {
176         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
177 }
178
179 static void ia32_set_frame_entity(ir_node *node, ir_entity *entity)
180 {
181         if (is_be_node(node))
182                 be_node_set_frame_entity(node, entity);
183         else
184                 set_ia32_frame_ent(node, entity);
185 }
186
187 static void ia32_set_frame_offset(ir_node *irn, int bias)
188 {
189         if (get_ia32_frame_ent(irn) == NULL)
190                 return;
191
192         if (is_ia32_Pop(irn) || is_ia32_PopMem(irn)) {
193                 ir_graph          *irg     = get_irn_irg(irn);
194                 be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
195                 if (layout->sp_relative) {
196                         /* Pop nodes modify the stack pointer before calculating the
197                          * destination address, so fix this here
198                          */
199                         bias -= 4;
200                 }
201         }
202         add_ia32_am_offs_int(irn, bias);
203 }
204
205 static int ia32_get_sp_bias(const ir_node *node)
206 {
207         if (is_ia32_Call(node))
208                 return -(int)get_ia32_call_attr_const(node)->pop;
209
210         if (is_ia32_Push(node))
211                 return 4;
212
213         if (is_ia32_Pop(node) || is_ia32_PopMem(node))
214                 return -4;
215
216         if (is_ia32_Leave(node) || is_ia32_CopyEbpEsp(node)) {
217                 return SP_BIAS_RESET;
218         }
219
220         return 0;
221 }
222
223 /**
224  * Build the between type and entities if not already build.
225  */
226 static void ia32_build_between_type(void)
227 {
228 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
229         if (between_type == NULL) {
230                 ir_type *old_bp_type   = new_type_primitive(mode_Iu);
231                 ir_type *ret_addr_type = new_type_primitive(mode_Iu);
232
233                 between_type           = new_type_struct(IDENT("ia32_between_type"));
234                 old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
235                 ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
236
237                 set_entity_offset(old_bp_ent, 0);
238                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
239                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
240                 set_type_state(between_type, layout_fixed);
241
242                 omit_fp_between_type = new_type_struct(IDENT("ia32_between_type_omit_fp"));
243                 omit_fp_ret_addr_ent = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
244
245                 set_entity_offset(omit_fp_ret_addr_ent, 0);
246                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
247                 set_type_state(omit_fp_between_type, layout_fixed);
248         }
249 #undef IDENT
250 }
251
252 /**
253  * Produces the type which sits between the stack args and the locals on the stack.
254  * it will contain the return address and space to store the old base pointer.
255  * @return The Firm type modeling the ABI between type.
256  */
257 static ir_type *ia32_abi_get_between_type(ir_graph *irg)
258 {
259         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
260         ia32_build_between_type();
261         return layout->sp_relative ? omit_fp_between_type : between_type;
262 }
263
264 /**
265  * Return the stack entity that contains the return address.
266  */
267 ir_entity *ia32_get_return_address_entity(ir_graph *irg)
268 {
269         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
270         ia32_build_between_type();
271         return layout->sp_relative ? omit_fp_ret_addr_ent : ret_addr_ent;
272 }
273
274 /**
275  * Return the stack entity that contains the frame address.
276  */
277 ir_entity *ia32_get_frame_address_entity(ir_graph *irg)
278 {
279         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
280         ia32_build_between_type();
281         return layout->sp_relative ? NULL : old_bp_ent;
282 }
283
284 /**
285  * Get the estimated cycle count for @p irn.
286  *
287  * @param irn  The node.
288  *
289  * @return     The estimated cycle count for this operation
290  */
291 static int ia32_get_op_estimated_cost(ir_node const *const irn)
292 {
293         if (is_ia32_CopyB_i(irn)) {
294                 unsigned const size = get_ia32_copyb_size(irn);
295                 return 20 + size * 4 / 3;
296         }
297
298         unsigned cost = get_ia32_latency(irn);
299
300         /* in case of address mode operations add additional cycles */
301         if (get_ia32_op_type(irn) != ia32_Normal) {
302                 if (is_ia32_use_frame(irn) || (
303                       is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_base)) &&
304                       is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_index))
305                     )) {
306                         /* Stack access, assume it is cached. */
307                         cost += 5;
308                 } else {
309                         /* Access probably elsewhere. */
310                         cost += 20;
311                 }
312         }
313
314         return cost;
315 }
316
317 static ir_mode *get_spill_mode_mode(const ir_mode *mode)
318 {
319         if (mode_is_float(mode))
320                 return precise_x87_spills ? ia32_mode_E : mode_D;
321
322         return mode_Iu;
323 }
324
325 /**
326  * Get the mode that should be used for spilling value node
327  */
328 static ir_mode *get_spill_mode(const ir_node *node)
329 {
330         ir_mode *mode = get_irn_mode(node);
331         return get_spill_mode_mode(mode);
332 }
333
334 /**
335  * Check if irn can load its operand at position i from memory (source addressmode).
336  * @param irn    The irn to be checked
337  * @param i      The operands position
338  * @return Non-Zero if operand can be loaded
339  */
340 static int ia32_possible_memory_operand(const ir_node *irn, unsigned int i)
341 {
342         ir_node       *op        = get_irn_n(irn, i);
343         const ir_mode *mode      = get_irn_mode(op);
344
345         if (!is_ia32_irn(irn)                              ||  /* must be an ia32 irn */
346             get_ia32_op_type(irn) != ia32_Normal           ||  /* must not already be a addressmode irn */
347             is_ia32_use_frame(irn))                            /* must not already use frame */
348                 return 0;
349         if (mode_is_float(mode)) {
350                 ir_mode *spillmode = get_spill_mode_mode(mode);
351                 if (spillmode != mode_D && spillmode != mode_F)
352                         return 0;
353         }
354
355         switch (get_ia32_am_support(irn)) {
356                 case ia32_am_none:
357                         return 0;
358
359                 case ia32_am_unary:
360                         if (i != n_ia32_unary_op)
361                                 return 0;
362                         break;
363
364                 case ia32_am_binary:
365                         switch (i) {
366                                 case n_ia32_binary_left: {
367                                         if (!is_ia32_commutative(irn))
368                                                 return 0;
369
370                                         /* we can't swap left/right for limited registers
371                                          * (As this (currently) breaks constraint handling copies) */
372                                         arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, n_ia32_binary_left);
373                                         if (arch_register_req_is(req, limited))
374                                                 return 0;
375                                         break;
376                                 }
377
378                                 case n_ia32_binary_right:
379                                         break;
380
381                                 default:
382                                         return 0;
383                         }
384                         break;
385
386                 default:
387                         panic("Unknown AM type");
388         }
389
390         /* HACK: must not already use "real" memory.
391          * This can happen for Call and Div */
392         if (!is_NoMem(get_irn_n(irn, n_ia32_mem)))
393                 return 0;
394
395         return 1;
396 }
397
398 static void ia32_perform_memory_operand(ir_node *irn, ir_node *spill,
399                                         unsigned int i)
400 {
401         assert(ia32_possible_memory_operand(irn, i) && "Cannot perform memory operand change");
402
403         set_ia32_op_type(irn, ia32_AddrModeS);
404
405         ir_mode *op_mode      = get_irn_mode(get_irn_n(irn, i));
406         ir_mode *load_mode    = get_spill_mode_mode(op_mode);
407         ir_mode *dest_op_mode = get_ia32_ls_mode(irn);
408         if (get_mode_size_bits(load_mode) <= get_mode_size_bits(dest_op_mode)) {
409                 set_ia32_ls_mode(irn, load_mode);
410         }
411         set_ia32_use_frame(irn);
412         set_ia32_need_stackent(irn);
413
414         if (i == n_ia32_binary_left                    &&
415             get_ia32_am_support(irn) == ia32_am_binary &&
416             /* immediates are only allowed on the right side */
417             !is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_right))) {
418                 ia32_swap_left_right(irn);
419                 i = n_ia32_binary_right;
420         }
421
422         assert(is_NoMem(get_irn_n(irn, n_ia32_mem)));
423
424         set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
425         set_irn_n(irn, n_ia32_mem,  spill);
426         set_irn_n(irn, i,           ia32_get_admissible_noreg(irn, i));
427         set_ia32_is_reload(irn);
428 }
429
430 static const be_abi_callbacks_t ia32_abi_callbacks = {
431         ia32_abi_get_between_type,
432 };
433
434 /* register allocator interface */
435 static const arch_irn_ops_t ia32_irn_ops = {
436         ia32_get_frame_entity,
437         ia32_set_frame_offset,
438         ia32_get_sp_bias,
439         ia32_get_op_estimated_cost,
440         ia32_possible_memory_operand,
441         ia32_perform_memory_operand,
442 };
443
444 static int gprof = 0;
445
446 static void ia32_before_abi(ir_graph *irg)
447 {
448         if (gprof) {
449                 static ir_entity *mcount = NULL;
450                 if (mcount == NULL) {
451                         ir_type *tp = new_type_method(0, 0);
452                         ident   *id = new_id_from_str("mcount");
453                         mcount = new_entity(get_glob_type(), id, tp);
454                         /* FIXME: enter the right ld_ident here */
455                         set_entity_ld_ident(mcount, get_entity_ident(mcount));
456                         set_entity_visibility(mcount, ir_visibility_external);
457                 }
458                 instrument_initcall(irg, mcount);
459         }
460 }
461
462 /**
463  * Transforms the standard firm graph into
464  * an ia32 firm graph
465  */
466 static void ia32_prepare_graph(ir_graph *irg)
467 {
468         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
469
470 #ifdef FIRM_GRGEN_BE
471         switch (be_transformer) {
472         case TRANSFORMER_DEFAULT:
473                 /* transform remaining nodes into assembler instructions */
474                 ia32_transform_graph(irg);
475                 break;
476
477         case TRANSFORMER_PBQP:
478         case TRANSFORMER_RAND:
479                 /* transform nodes into assembler instructions by PBQP magic */
480                 ia32_transform_graph_by_pbqp(irg);
481                 break;
482
483         default:
484                 panic("invalid transformer");
485         }
486 #else
487         ia32_transform_graph(irg);
488 #endif
489
490         /* do local optimizations (mainly CSE) */
491         optimize_graph_df(irg);
492         /* backend code expects that outedges are always enabled */
493         assure_edges(irg);
494
495         if (irg_data->dump)
496                 dump_ir_graph(irg, "transformed");
497
498         /* optimize address mode */
499         ia32_optimize_graph(irg);
500
501         /* do code placement, to optimize the position of constants */
502         place_code(irg);
503         /* backend code expects that outedges are always enabled */
504         assure_edges(irg);
505
506         if (irg_data->dump)
507                 dump_ir_graph(irg, "place");
508 }
509
510 ir_node *ia32_turn_back_am(ir_node *node)
511 {
512         dbg_info *dbgi  = get_irn_dbg_info(node);
513         ir_graph *irg   = get_irn_irg(node);
514         ir_node  *block = get_nodes_block(node);
515         ir_node  *base  = get_irn_n(node, n_ia32_base);
516         ir_node  *idx   = get_irn_n(node, n_ia32_index);
517         ir_node  *mem   = get_irn_n(node, n_ia32_mem);
518         ir_node  *noreg;
519
520         ir_node  *load     = new_bd_ia32_Load(dbgi, block, base, idx, mem);
521         ir_node  *load_res = new_rd_Proj(dbgi, load, mode_Iu, pn_ia32_Load_res);
522
523         ia32_copy_am_attrs(load, node);
524         if (is_ia32_is_reload(node))
525                 set_ia32_is_reload(load);
526         set_irn_n(node, n_ia32_mem, get_irg_no_mem(irg));
527
528         switch (get_ia32_am_support(node)) {
529                 case ia32_am_unary:
530                         set_irn_n(node, n_ia32_unary_op, load_res);
531                         break;
532
533                 case ia32_am_binary:
534                         if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
535                                 set_irn_n(node, n_ia32_binary_left, load_res);
536                         } else {
537                                 set_irn_n(node, n_ia32_binary_right, load_res);
538                         }
539                         break;
540
541                 default:
542                         panic("Unknown AM type");
543         }
544         noreg = ia32_new_NoReg_gp(irg);
545         set_irn_n(node, n_ia32_base,  noreg);
546         set_irn_n(node, n_ia32_index, noreg);
547         set_ia32_am_offs_int(node, 0);
548         set_ia32_am_sc(node, NULL);
549         set_ia32_am_scale(node, 0);
550         clear_ia32_am_sc_sign(node);
551
552         /* rewire mem-proj */
553         if (get_irn_mode(node) == mode_T) {
554                 foreach_out_edge(node, edge) {
555                         ir_node *out = get_edge_src_irn(edge);
556                         if (get_irn_mode(out) == mode_M) {
557                                 set_Proj_pred(out, load);
558                                 set_Proj_proj(out, pn_ia32_Load_M);
559                                 break;
560                         }
561                 }
562         }
563
564         set_ia32_op_type(node, ia32_Normal);
565         if (sched_is_scheduled(node))
566                 sched_add_before(node, load);
567
568         return load_res;
569 }
570
571 static ir_node *flags_remat(ir_node *node, ir_node *after)
572 {
573         /* we should turn back source address mode when rematerializing nodes */
574         ia32_op_type_t type;
575         ir_node        *block;
576         ir_node        *copy;
577
578         if (is_Block(after)) {
579                 block = after;
580         } else {
581                 block = get_nodes_block(after);
582         }
583
584         type = get_ia32_op_type(node);
585         switch (type) {
586                 case ia32_AddrModeS:
587                         ia32_turn_back_am(node);
588                         break;
589
590                 case ia32_AddrModeD:
591                         /* TODO implement this later... */
592                         panic("found DestAM with flag user %+F this should not happen", node);
593
594                 default: assert(type == ia32_Normal); break;
595         }
596
597         copy = exact_copy(node);
598         set_nodes_block(copy, block);
599         sched_add_after(after, copy);
600
601         return copy;
602 }
603
604 /**
605  * Called before the register allocator.
606  */
607 static void ia32_before_ra(ir_graph *irg)
608 {
609         /* setup fpu rounding modes */
610         ia32_setup_fpu_mode(irg);
611
612         /* fixup flags */
613         be_sched_fix_flags(irg, &ia32_reg_classes[CLASS_ia32_flags],
614                            &flags_remat, NULL);
615
616         be_add_missing_keeps(irg);
617 }
618
619
620 /**
621  * Transforms a be_Reload into a ia32 Load.
622  */
623 static void transform_to_Load(ir_node *node)
624 {
625         ir_graph *irg        = get_irn_irg(node);
626         dbg_info *dbgi       = get_irn_dbg_info(node);
627         ir_node *block       = get_nodes_block(node);
628         ir_entity *ent       = be_get_frame_entity(node);
629         ir_mode *mode        = get_irn_mode(node);
630         ir_mode *spillmode   = get_spill_mode(node);
631         ir_node *noreg       = ia32_new_NoReg_gp(irg);
632         ir_node *ptr         = get_irg_frame(irg);
633         ir_node *mem         = get_irn_n(node, n_be_Reload_mem);
634         ir_node *new_op, *proj;
635         const arch_register_t *reg;
636
637         if (mode_is_float(spillmode)) {
638                 if (ia32_cg_config.use_sse2)
639                         new_op = new_bd_ia32_xLoad(dbgi, block, ptr, noreg, mem, spillmode);
640                 else
641                         new_op = new_bd_ia32_fld(dbgi, block, ptr, noreg, mem, spillmode);
642         }
643         else if (get_mode_size_bits(spillmode) == 128) {
644                 /* Reload 128 bit SSE registers */
645                 new_op = new_bd_ia32_xxLoad(dbgi, block, ptr, noreg, mem);
646         }
647         else
648                 new_op = new_bd_ia32_Load(dbgi, block, ptr, noreg, mem);
649
650         set_ia32_op_type(new_op, ia32_AddrModeS);
651         set_ia32_ls_mode(new_op, spillmode);
652         set_ia32_frame_ent(new_op, ent);
653         set_ia32_use_frame(new_op);
654         set_ia32_is_reload(new_op);
655
656         DBG_OPT_RELOAD2LD(node, new_op);
657
658         proj = new_rd_Proj(dbgi, new_op, mode, pn_ia32_Load_res);
659
660         sched_replace(node, new_op);
661
662         /* copy the register from the old node to the new Load */
663         reg = arch_get_irn_register(node);
664         arch_set_irn_register(proj, reg);
665
666         SET_IA32_ORIG_NODE(new_op, node);
667
668         exchange(node, proj);
669 }
670
671 /**
672  * Transforms a be_Spill node into a ia32 Store.
673  */
674 static void transform_to_Store(ir_node *node)
675 {
676         ir_graph *irg  = get_irn_irg(node);
677         dbg_info *dbgi = get_irn_dbg_info(node);
678         ir_node *block = get_nodes_block(node);
679         ir_entity *ent = be_get_frame_entity(node);
680         const ir_node *spillval = get_irn_n(node, n_be_Spill_val);
681         ir_mode *mode  = get_spill_mode(spillval);
682         ir_node *noreg = ia32_new_NoReg_gp(irg);
683         ir_node *nomem = get_irg_no_mem(irg);
684         ir_node *ptr   = get_irg_frame(irg);
685         ir_node *val   = get_irn_n(node, n_be_Spill_val);
686         ir_node *res;
687         ir_node *store;
688
689         if (mode_is_float(mode)) {
690                 if (ia32_cg_config.use_sse2) {
691                         store = new_bd_ia32_xStore(dbgi, block, ptr, noreg, nomem, val);
692                         res   = new_r_Proj(store, mode_M, pn_ia32_xStore_M);
693                 } else {
694                         store = new_bd_ia32_fst(dbgi, block, ptr, noreg, nomem, val, mode);
695                         res   = new_r_Proj(store, mode_M, pn_ia32_fst_M);
696                 }
697         } else if (get_mode_size_bits(mode) == 128) {
698                 /* Spill 128 bit SSE registers */
699                 store = new_bd_ia32_xxStore(dbgi, block, ptr, noreg, nomem, val);
700                 res   = new_r_Proj(store, mode_M, pn_ia32_xxStore_M);
701         } else {
702                 store = get_mode_size_bits(mode) == 8
703                         ? new_bd_ia32_Store_8bit(dbgi, block, ptr, noreg, nomem, val)
704                         : new_bd_ia32_Store     (dbgi, block, ptr, noreg, nomem, val);
705                 res   = new_r_Proj(store, mode_M, pn_ia32_Store_M);
706         }
707
708         set_ia32_op_type(store, ia32_AddrModeD);
709         set_ia32_ls_mode(store, mode);
710         set_ia32_frame_ent(store, ent);
711         set_ia32_use_frame(store);
712         set_ia32_is_spill(store);
713         SET_IA32_ORIG_NODE(store, node);
714         DBG_OPT_SPILL2ST(node, store);
715
716         sched_replace(node, store);
717         exchange(node, res);
718 }
719
720 static ir_node *create_push(ir_node *node, ir_node *schedpoint, ir_node *sp, ir_node *mem, ir_entity *ent)
721 {
722         dbg_info *dbgi  = get_irn_dbg_info(node);
723         ir_node  *block = get_nodes_block(node);
724         ir_graph *irg   = get_irn_irg(node);
725         ir_node  *noreg = ia32_new_NoReg_gp(irg);
726         ir_node  *frame = get_irg_frame(irg);
727
728         ir_node *push = new_bd_ia32_Push(dbgi, block, frame, noreg, mem, noreg, sp);
729
730         set_ia32_frame_ent(push, ent);
731         set_ia32_use_frame(push);
732         set_ia32_op_type(push, ia32_AddrModeS);
733         set_ia32_ls_mode(push, mode_Is);
734         set_ia32_is_spill(push);
735
736         sched_add_before(schedpoint, push);
737         return push;
738 }
739
740 static ir_node *create_pop(ir_node *node, ir_node *schedpoint, ir_node *sp, ir_entity *ent)
741 {
742         dbg_info *dbgi  = get_irn_dbg_info(node);
743         ir_node  *block = get_nodes_block(node);
744         ir_graph *irg   = get_irn_irg(node);
745         ir_node  *noreg = ia32_new_NoReg_gp(irg);
746         ir_node  *frame = get_irg_frame(irg);
747
748         ir_node *pop = new_bd_ia32_PopMem(dbgi, block, frame, noreg,
749                                           get_irg_no_mem(irg), sp);
750
751         set_ia32_frame_ent(pop, ent);
752         set_ia32_use_frame(pop);
753         set_ia32_op_type(pop, ia32_AddrModeD);
754         set_ia32_ls_mode(pop, mode_Is);
755         set_ia32_is_reload(pop);
756
757         sched_add_before(schedpoint, pop);
758
759         return pop;
760 }
761
762 static ir_node* create_spproj(ir_node *node, ir_node *pred, int pos)
763 {
764         dbg_info *dbgi   = get_irn_dbg_info(node);
765         ir_mode  *spmode = mode_Iu;
766         const arch_register_t *spreg = &ia32_registers[REG_ESP];
767         ir_node *sp;
768
769         sp = new_rd_Proj(dbgi, pred, spmode, pos);
770         arch_set_irn_register(sp, spreg);
771
772         return sp;
773 }
774
775 /**
776  * Transform MemPerm, currently we do this the ugly way and produce
777  * push/pop into/from memory cascades. This is possible without using
778  * any registers.
779  */
780 static void transform_MemPerm(ir_node *node)
781 {
782         ir_node  *block = get_nodes_block(node);
783         ir_graph *irg   = get_irn_irg(node);
784         ir_node  *sp    = be_get_initial_reg_value(irg, &ia32_registers[REG_ESP]);
785         int       arity = be_get_MemPerm_entity_arity(node);
786         ir_node **pops  = ALLOCAN(ir_node*, arity);
787         ir_node  *in[1];
788         ir_node  *keep;
789         int       i;
790
791         /* create Pushs */
792         for (i = 0; i < arity; ++i) {
793                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
794                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
795                 ir_type *enttype = get_entity_type(inent);
796                 unsigned entsize = get_type_size_bytes(enttype);
797                 unsigned entsize2 = get_type_size_bytes(get_entity_type(outent));
798                 ir_node *mem = get_irn_n(node, i + 1);
799                 ir_node *push;
800
801                 /* work around cases where entities have different sizes */
802                 if (entsize2 < entsize)
803                         entsize = entsize2;
804                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
805
806                 push = create_push(node, node, sp, mem, inent);
807                 sp = create_spproj(node, push, pn_ia32_Push_stack);
808                 if (entsize == 8) {
809                         /* add another push after the first one */
810                         push = create_push(node, node, sp, mem, inent);
811                         add_ia32_am_offs_int(push, 4);
812                         sp = create_spproj(node, push, pn_ia32_Push_stack);
813                 }
814
815                 set_irn_n(node, i, new_r_Bad(irg, mode_X));
816         }
817
818         /* create pops */
819         for (i = arity - 1; i >= 0; --i) {
820                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
821                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
822                 ir_type *enttype = get_entity_type(outent);
823                 unsigned entsize = get_type_size_bytes(enttype);
824                 unsigned entsize2 = get_type_size_bytes(get_entity_type(inent));
825                 ir_node *pop;
826
827                 /* work around cases where entities have different sizes */
828                 if (entsize2 < entsize)
829                         entsize = entsize2;
830                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
831
832                 pop = create_pop(node, node, sp, outent);
833                 sp = create_spproj(node, pop, pn_ia32_Pop_stack);
834                 if (entsize == 8) {
835                         add_ia32_am_offs_int(pop, 4);
836
837                         /* add another pop after the first one */
838                         pop = create_pop(node, node, sp, outent);
839                         sp = create_spproj(node, pop, pn_ia32_Pop_stack);
840                 }
841
842                 pops[i] = pop;
843         }
844
845         in[0] = sp;
846         keep  = be_new_Keep(block, 1, in);
847         sched_replace(node, keep);
848
849         /* exchange memprojs */
850         foreach_out_edge_safe(node, edge) {
851                 ir_node *proj = get_edge_src_irn(edge);
852                 int p = get_Proj_proj(proj);
853
854                 assert(p < arity);
855
856                 set_Proj_pred(proj, pops[p]);
857                 set_Proj_proj(proj, pn_ia32_Pop_M);
858         }
859
860         /* remove memperm */
861         kill_node(node);
862 }
863
864 /**
865  * Block-Walker: Calls the transform functions Spill and Reload.
866  */
867 static void ia32_after_ra_walker(ir_node *block, void *env)
868 {
869         (void) env;
870
871         /* beware: the schedule is changed here */
872         sched_foreach_reverse_safe(block, node) {
873                 if (be_is_Reload(node)) {
874                         transform_to_Load(node);
875                 } else if (be_is_Spill(node)) {
876                         transform_to_Store(node);
877                 } else if (be_is_MemPerm(node)) {
878                         transform_MemPerm(node);
879                 }
880         }
881 }
882
883 /**
884  * Collects nodes that need frame entities assigned.
885  */
886 static void ia32_collect_frame_entity_nodes(ir_node *node, void *data)
887 {
888         be_fec_env_t  *env = (be_fec_env_t*)data;
889         const ir_mode *mode;
890         int            align;
891
892         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
893                 mode  = get_spill_mode_mode(get_irn_mode(node));
894                 align = get_mode_size_bytes(mode);
895         } else if (is_ia32_irn(node)         &&
896                         get_ia32_frame_ent(node) == NULL &&
897                         is_ia32_use_frame(node)) {
898                 if (is_ia32_need_stackent(node))
899                         goto need_stackent;
900
901                 switch (get_ia32_irn_opcode(node)) {
902 need_stackent:
903                         case iro_ia32_Load: {
904                                 const ia32_attr_t *attr = get_ia32_attr_const(node);
905
906                                 if (attr->data.need_32bit_stackent) {
907                                         mode = mode_Is;
908                                 } else if (attr->data.need_64bit_stackent) {
909                                         mode = mode_Ls;
910                                 } else {
911                                         mode = get_ia32_ls_mode(node);
912                                         if (is_ia32_is_reload(node))
913                                                 mode = get_spill_mode_mode(mode);
914                                 }
915                                 align = get_mode_size_bytes(mode);
916                                 break;
917                         }
918
919                         case iro_ia32_fild:
920                         case iro_ia32_fld:
921                         case iro_ia32_xLoad: {
922                                 mode  = get_ia32_ls_mode(node);
923                                 align = 4;
924                                 break;
925                         }
926
927                         case iro_ia32_FldCW: {
928                                 /* although 2 byte would be enough 4 byte performs best */
929                                 mode  = mode_Iu;
930                                 align = 4;
931                                 break;
932                         }
933
934                         default:
935 #ifndef NDEBUG
936                                 panic("unexpected frame user while collection frame entity nodes");
937
938                         case iro_ia32_FnstCW:
939                         case iro_ia32_Store:
940                         case iro_ia32_fst:
941                         case iro_ia32_fist:
942                         case iro_ia32_fisttp:
943                         case iro_ia32_xStore:
944                         case iro_ia32_xStoreSimple:
945 #endif
946                                 return;
947                 }
948         } else {
949                 return;
950         }
951         be_node_needs_frame_entity(env, node, mode, align);
952 }
953
954 static int determine_ebp_input(ir_node *ret)
955 {
956         const arch_register_t *bp = &ia32_registers[REG_EBP];
957         int   arity               = get_irn_arity(ret);
958         int   i;
959
960         for (i = 0; i < arity; ++i) {
961                 ir_node *input = get_irn_n(ret, i);
962                 if (arch_get_irn_register(input) == bp)
963                         return i;
964         }
965         panic("no ebp input found at %+F", ret);
966 }
967
968 static void introduce_epilog(ir_node *ret)
969 {
970         const arch_register_t *sp         = &ia32_registers[REG_ESP];
971         const arch_register_t *bp         = &ia32_registers[REG_EBP];
972         ir_graph              *irg        = get_irn_irg(ret);
973         ir_type               *frame_type = get_irg_frame_type(irg);
974         unsigned               frame_size = get_type_size_bytes(frame_type);
975         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
976         ir_node               *block      = get_nodes_block(ret);
977         ir_node               *first_sp   = get_irn_n(ret, n_be_Return_sp);
978         ir_node               *curr_sp    = first_sp;
979         ir_mode               *mode_gp    = ia32_reg_classes[CLASS_ia32_gp].mode;
980
981         if (!layout->sp_relative) {
982                 int      n_ebp   = determine_ebp_input(ret);
983                 ir_node *curr_bp = get_irn_n(ret, n_ebp);
984                 if (ia32_cg_config.use_leave) {
985                         ir_node *leave = new_bd_ia32_Leave(NULL, block, curr_bp);
986                         curr_bp        = new_r_Proj(leave, mode_gp, pn_ia32_Leave_frame);
987                         curr_sp        = new_r_Proj(leave, mode_gp, pn_ia32_Leave_stack);
988                         arch_set_irn_register(curr_bp, bp);
989                         arch_set_irn_register(curr_sp, sp);
990                         sched_add_before(ret, leave);
991                 } else {
992                         ir_node *pop;
993                         ir_node *curr_mem = get_irn_n(ret, n_be_Return_mem);
994                         /* copy ebp to esp */
995                         curr_sp = new_bd_ia32_CopyEbpEsp(NULL, block, curr_bp);
996                         arch_set_irn_register(curr_sp, sp);
997                         sched_add_before(ret, curr_sp);
998
999                         /* pop ebp */
1000                         pop      = new_bd_ia32_PopEbp(NULL, block, curr_mem, curr_sp);
1001                         curr_bp  = new_r_Proj(pop, mode_gp, pn_ia32_PopEbp_res);
1002                         curr_sp  = new_r_Proj(pop, mode_gp, pn_ia32_PopEbp_stack);
1003                         curr_mem = new_r_Proj(pop, mode_M, pn_ia32_Pop_M);
1004                         arch_set_irn_register(curr_bp, bp);
1005                         arch_set_irn_register(curr_sp, sp);
1006                         sched_add_before(ret, pop);
1007
1008                         set_irn_n(ret, n_be_Return_mem, curr_mem);
1009                 }
1010                 set_irn_n(ret, n_ebp, curr_bp);
1011         } else {
1012                 ir_node *incsp = be_new_IncSP(sp, block, curr_sp, -(int)frame_size, 0);
1013                 sched_add_before(ret, incsp);
1014                 curr_sp = incsp;
1015         }
1016         set_irn_n(ret, n_be_Return_sp, curr_sp);
1017
1018         /* keep verifier happy... */
1019         if (get_irn_n_edges(first_sp) == 0 && is_Proj(first_sp)) {
1020                 kill_node(first_sp);
1021         }
1022 }
1023
1024 /**
1025  * put the Prolog code at the beginning, epilog code before each return
1026  */
1027 static void introduce_prolog_epilog(ir_graph *irg)
1028 {
1029         const arch_register_t *sp         = &ia32_registers[REG_ESP];
1030         const arch_register_t *bp         = &ia32_registers[REG_EBP];
1031         ir_node               *start      = get_irg_start(irg);
1032         ir_node               *block      = get_nodes_block(start);
1033         ir_type               *frame_type = get_irg_frame_type(irg);
1034         unsigned               frame_size = get_type_size_bytes(frame_type);
1035         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
1036         ir_node               *initial_sp = be_get_initial_reg_value(irg, sp);
1037         ir_node               *curr_sp    = initial_sp;
1038         ir_mode               *mode_gp    = mode_Iu;
1039
1040         if (!layout->sp_relative) {
1041                 /* push ebp */
1042                 ir_node *mem        = get_irg_initial_mem(irg);
1043                 ir_node *noreg      = ia32_new_NoReg_gp(irg);
1044                 ir_node *initial_bp = be_get_initial_reg_value(irg, bp);
1045                 ir_node *push       = new_bd_ia32_Push(NULL, block, noreg, noreg, mem, initial_bp, initial_sp);
1046                 ir_node *incsp;
1047
1048                 curr_sp = new_r_Proj(push, mode_gp, pn_ia32_Push_stack);
1049                 arch_set_irn_register(curr_sp, sp);
1050                 sched_add_after(start, push);
1051
1052                 /* move esp to ebp */
1053                 ir_node *const curr_bp = be_new_Copy(block, curr_sp);
1054                 sched_add_after(push, curr_bp);
1055                 be_set_constr_single_reg_out(curr_bp, 0, bp, arch_register_req_type_ignore);
1056                 curr_sp = be_new_CopyKeep_single(block, curr_sp, curr_bp);
1057                 sched_add_after(curr_bp, curr_sp);
1058                 be_set_constr_single_reg_out(curr_sp, 0, sp, arch_register_req_type_produces_sp);
1059                 edges_reroute_except(initial_bp, curr_bp, push);
1060
1061                 incsp = be_new_IncSP(sp, block, curr_sp, frame_size, 0);
1062                 edges_reroute_except(initial_sp, incsp, push);
1063                 sched_add_after(curr_sp, incsp);
1064
1065                 /* make sure the initial IncSP is really used by someone */
1066                 if (get_irn_n_edges(incsp) <= 1) {
1067                         ir_node *in[] = { incsp };
1068                         ir_node *keep = be_new_Keep(block, 1, in);
1069                         sched_add_after(incsp, keep);
1070                 }
1071
1072                 layout->initial_bias = -4;
1073         } else {
1074                 ir_node *const incsp = be_new_IncSP(sp, block, initial_sp, frame_size, 0);
1075                 edges_reroute_except(initial_sp, incsp, incsp);
1076                 sched_add_after(start, incsp);
1077         }
1078
1079         /* introduce epilog for every return node */
1080         {
1081                 ir_node *end_block = get_irg_end_block(irg);
1082                 int      arity     = get_irn_arity(end_block);
1083                 int      i;
1084
1085                 for (i = 0; i < arity; ++i) {
1086                         ir_node *ret = get_irn_n(end_block, i);
1087                         assert(be_is_Return(ret));
1088                         introduce_epilog(ret);
1089                 }
1090         }
1091 }
1092
1093 /**
1094  * Last touchups for the graph before emit: x87 simulation to replace the
1095  * virtual with real x87 instructions, creating a block schedule and peephole
1096  * optimisations.
1097  */
1098 static void ia32_finish_graph(ir_graph *irg)
1099 {
1100         ia32_irg_data_t   *irg_data     = ia32_get_irg_data(irg);
1101         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
1102         bool               at_begin     = stack_layout->sp_relative ? true : false;
1103         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
1104
1105         /* create and coalesce frame entities */
1106         irg_walk_graph(irg, NULL, ia32_collect_frame_entity_nodes, fec_env);
1107         be_assign_entities(fec_env, ia32_set_frame_entity, at_begin);
1108         be_free_frame_entity_coalescer(fec_env);
1109
1110         irg_block_walk_graph(irg, NULL, ia32_after_ra_walker, NULL);
1111
1112         introduce_prolog_epilog(irg);
1113
1114         /* fix stack entity offsets */
1115         be_abi_fix_stack_nodes(irg);
1116         be_abi_fix_stack_bias(irg);
1117
1118         /* fix 2-address code constraints */
1119         ia32_finish_irg(irg);
1120
1121         /* we might have to rewrite x87 virtual registers */
1122         if (irg_data->do_x87_sim) {
1123                 ia32_x87_simulate_graph(irg);
1124         }
1125
1126         /* do peephole optimisations */
1127         ia32_peephole_optimization(irg);
1128
1129         be_remove_dead_nodes_from_schedule(irg);
1130
1131         /* create block schedule, this also removes empty blocks which might
1132          * produce critical edges */
1133         irg_data->blk_sched = be_create_block_schedule(irg);
1134 }
1135
1136 /**
1137  * Emits the code, closes the output file and frees
1138  * the code generator interface.
1139  */
1140 static void ia32_emit(ir_graph *irg)
1141 {
1142         if (ia32_cg_config.emit_machcode) {
1143                 ia32_gen_binary_routine(irg);
1144         } else {
1145                 ia32_gen_routine(irg);
1146         }
1147 }
1148
1149 /**
1150  * Returns the node representing the PIC base.
1151  */
1152 static ir_node *ia32_get_pic_base(ir_graph *irg)
1153 {
1154         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
1155         ir_node         *block;
1156         ir_node         *get_eip = irg_data->get_eip;
1157         if (get_eip != NULL)
1158                 return get_eip;
1159
1160         block             = get_irg_start_block(irg);
1161         get_eip           = new_bd_ia32_GetEIP(NULL, block);
1162         irg_data->get_eip = get_eip;
1163
1164         return get_eip;
1165 }
1166
1167 /**
1168  * Initializes a IA32 code generator.
1169  */
1170 static void ia32_init_graph(ir_graph *irg)
1171 {
1172         struct obstack  *obst     = be_get_be_obst(irg);
1173         ia32_irg_data_t *irg_data = OALLOCZ(obst, ia32_irg_data_t);
1174
1175         irg_data->dump = (be_options.dump_flags & DUMP_BE) ? 1 : 0;
1176
1177         if (gprof) {
1178                 /* Linux gprof implementation needs base pointer */
1179                 be_options.omit_fp = 0;
1180         }
1181
1182         be_birg_from_irg(irg)->isa_link = irg_data;
1183 }
1184
1185 static const tarval_mode_info mo_integer = {
1186         TVO_HEX,
1187         "0x",
1188         NULL,
1189 };
1190
1191 /*
1192  * set the tarval output mode of all integer modes to decimal
1193  */
1194 static void set_tarval_output_modes(void)
1195 {
1196         size_t i;
1197
1198         for (i = ir_get_n_modes(); i > 0;) {
1199                 ir_mode *mode = ir_get_mode(--i);
1200
1201                 if (mode_is_int(mode))
1202                         set_tarval_mode_output_option(mode, &mo_integer);
1203         }
1204 }
1205
1206 extern const arch_isa_if_t ia32_isa_if;
1207
1208 static void init_asm_constraints(void)
1209 {
1210         be_init_default_asm_constraint_flags();
1211
1212         asm_constraint_flags['a'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1213         asm_constraint_flags['b'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1214         asm_constraint_flags['c'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1215         asm_constraint_flags['d'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1216         asm_constraint_flags['D'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1217         asm_constraint_flags['S'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1218         asm_constraint_flags['Q'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1219         asm_constraint_flags['q'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1220         asm_constraint_flags['A'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1221         asm_constraint_flags['l'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1222         asm_constraint_flags['R'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1223         asm_constraint_flags['r'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1224         asm_constraint_flags['p'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1225         asm_constraint_flags['f'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1226         asm_constraint_flags['t'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1227         asm_constraint_flags['u'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1228         asm_constraint_flags['Y'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1229         asm_constraint_flags['X'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1230         asm_constraint_flags['n'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
1231         asm_constraint_flags['g'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
1232
1233         /* no support for autodecrement/autoincrement */
1234         asm_constraint_flags['<'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1235         asm_constraint_flags['>'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1236         /* no float consts */
1237         asm_constraint_flags['E'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1238         asm_constraint_flags['F'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1239         /* makes no sense on x86 */
1240         asm_constraint_flags['s'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1241         /* no support for sse consts yet */
1242         asm_constraint_flags['C'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1243         /* no support for x87 consts yet */
1244         asm_constraint_flags['G'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1245         /* no support for mmx registers yet */
1246         asm_constraint_flags['y'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1247         /* not available in 32bit mode */
1248         asm_constraint_flags['Z'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1249         asm_constraint_flags['e'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1250
1251         /* no code yet to determine register class needed... */
1252         asm_constraint_flags['X'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1253 }
1254
1255 /**
1256  * Check if Mux(sel, mux_true, mux_false) would represent a Max or Min operation
1257  */
1258 static bool mux_is_float_min_max(ir_node *sel, ir_node *mux_true,
1259                                  ir_node *mux_false)
1260 {
1261         ir_node    *cmp_l;
1262         ir_node    *cmp_r;
1263         ir_relation relation;
1264
1265         if (!is_Cmp(sel))
1266                 return false;
1267
1268         cmp_l = get_Cmp_left(sel);
1269         cmp_r = get_Cmp_right(sel);
1270         if (!mode_is_float(get_irn_mode(cmp_l)))
1271                 return false;
1272
1273         /* check for min/max. They're defined as (C-Semantik):
1274          *  min(a, b) = a < b ? a : b
1275          *  or min(a, b) = a <= b ? a : b
1276          *  max(a, b) = a > b ? a : b
1277          *  or max(a, b) = a >= b ? a : b
1278          * (Note we only handle float min/max here)
1279          */
1280         relation = get_Cmp_relation(sel);
1281         switch (relation) {
1282         case ir_relation_greater_equal:
1283         case ir_relation_greater:
1284                 /* this is a max */
1285                 if (cmp_l == mux_true && cmp_r == mux_false)
1286                         return true;
1287                 break;
1288         case ir_relation_less_equal:
1289         case ir_relation_less:
1290                 /* this is a min */
1291                 if (cmp_l == mux_true && cmp_r == mux_false)
1292                         return true;
1293                 break;
1294         case ir_relation_unordered_greater_equal:
1295         case ir_relation_unordered_greater:
1296                 /* this is a min */
1297                 if (cmp_l == mux_false && cmp_r == mux_true)
1298                         return true;
1299                 break;
1300         case ir_relation_unordered_less_equal:
1301         case ir_relation_unordered_less:
1302                 /* this is a max */
1303                 if (cmp_l == mux_false && cmp_r == mux_true)
1304                         return true;
1305                 break;
1306
1307         default:
1308                 break;
1309         }
1310
1311         return false;
1312 }
1313
1314 static bool mux_is_set(ir_node *sel, ir_node *mux_true, ir_node *mux_false)
1315 {
1316         ir_mode *mode = get_irn_mode(mux_true);
1317         (void) sel;
1318
1319         if (!mode_is_int(mode) && !mode_is_reference(mode)
1320                         && mode != mode_b)
1321                 return false;
1322
1323         if (is_Const(mux_true) && is_Const(mux_false)) {
1324                 /* we can create a set plus up two 3 instructions for any combination
1325                  * of constants */
1326                 return true;
1327         }
1328
1329         return false;
1330 }
1331
1332 static bool mux_is_float_const_const(ir_node *sel, ir_node *mux_true,
1333                                      ir_node *mux_false)
1334 {
1335         (void) sel;
1336
1337         if (!mode_is_float(get_irn_mode(mux_true)))
1338                 return false;
1339
1340         return is_Const(mux_true) && is_Const(mux_false);
1341 }
1342
1343 static bool mux_is_doz(ir_node *sel, ir_node *mux_true, ir_node *mux_false)
1344 {
1345         ir_node    *cmp_left;
1346         ir_node    *cmp_right;
1347         ir_node    *sub_left;
1348         ir_node    *sub_right;
1349         ir_mode    *mode;
1350         ir_relation relation;
1351
1352         if (!is_Cmp(sel))
1353                 return false;
1354
1355         mode = get_irn_mode(mux_true);
1356         if (mode_is_signed(mode) || mode_is_float(mode))
1357                 return false;
1358
1359         relation  = get_Cmp_relation(sel);
1360         cmp_left  = get_Cmp_left(sel);
1361         cmp_right = get_Cmp_right(sel);
1362
1363         /* "move" zero constant to false input */
1364         if (is_Const(mux_true) && is_Const_null(mux_true)) {
1365                 ir_node *tmp = mux_false;
1366                 mux_false = mux_true;
1367                 mux_true  = tmp;
1368                 relation = get_negated_relation(relation);
1369         }
1370         if (!is_Const(mux_false) || !is_Const_null(mux_false))
1371                 return false;
1372         if (!is_Sub(mux_true))
1373                 return false;
1374         sub_left  = get_Sub_left(mux_true);
1375         sub_right = get_Sub_right(mux_true);
1376
1377         /* Mux(a >=u b, 0, a-b) */
1378         if ((relation & ir_relation_greater)
1379                         && sub_left == cmp_left && sub_right == cmp_right)
1380                 return true;
1381         /* Mux(a <=u b, 0, b-a) */
1382         if ((relation & ir_relation_less)
1383                         && sub_left == cmp_right && sub_right == cmp_left)
1384                 return true;
1385
1386         return false;
1387 }
1388
1389 static int ia32_is_mux_allowed(ir_node *sel, ir_node *mux_false,
1390                                ir_node *mux_true)
1391 {
1392         ir_mode *mode;
1393
1394         /* middleend can handle some things */
1395         if (ir_is_optimizable_mux(sel, mux_false, mux_true))
1396                 return true;
1397         /* we can handle Set for all modes and compares */
1398         if (mux_is_set(sel, mux_true, mux_false))
1399                 return true;
1400         /* SSE has own min/max operations */
1401         if (ia32_cg_config.use_sse2
1402                         && mux_is_float_min_max(sel, mux_true, mux_false))
1403                 return true;
1404         /* we can handle Mux(?, Const[f], Const[f]) */
1405         if (mux_is_float_const_const(sel, mux_true, mux_false)) {
1406 #ifdef FIRM_GRGEN_BE
1407                 /* well, some code selectors can't handle it */
1408                 if (be_transformer != TRANSFORMER_PBQP
1409                                 || be_transformer != TRANSFORMER_RAND)
1410                         return true;
1411 #else
1412                 return true;
1413 #endif
1414         }
1415
1416         /* no support for 64bit inputs to cmov */
1417         mode = get_irn_mode(mux_true);
1418         if (get_mode_size_bits(mode) > 32)
1419                 return false;
1420         /* we can handle Abs for all modes and compares (except 64bit) */
1421         if (ir_mux_is_abs(sel, mux_false, mux_true) != 0)
1422                 return true;
1423         /* we can't handle MuxF yet */
1424         if (mode_is_float(mode))
1425                 return false;
1426
1427         if (mux_is_doz(sel, mux_true, mux_false))
1428                 return true;
1429
1430         /* Check Cmp before the node */
1431         if (is_Cmp(sel)) {
1432                 ir_mode *cmp_mode = get_irn_mode(get_Cmp_left(sel));
1433
1434                 /* we can't handle 64bit compares */
1435                 if (get_mode_size_bits(cmp_mode) > 32)
1436                         return false;
1437
1438                 /* we can't handle float compares */
1439                 if (mode_is_float(cmp_mode))
1440                         return false;
1441         }
1442
1443         /* did we disable cmov generation? */
1444         if (!ia32_cg_config.use_cmov)
1445                 return false;
1446
1447         /* we can use a cmov */
1448         return true;
1449 }
1450
1451 /**
1452  * Create the trampoline code.
1453  */
1454 static ir_node *ia32_create_trampoline_fkt(ir_node *block, ir_node *mem, ir_node *trampoline, ir_node *env, ir_node *callee)
1455 {
1456         ir_graph *const irg  = get_irn_irg(block);
1457         ir_node  *      p    = trampoline;
1458         ir_mode  *const mode = get_irn_mode(p);
1459         ir_node  *const one  = new_r_Const(irg, get_mode_one(mode_Iu));
1460         ir_node  *const four = new_r_Const_long(irg, mode_Iu, 4);
1461         ir_node  *      st;
1462
1463         /* mov  ecx,<env> */
1464         st  = new_r_Store(block, mem, p, new_r_Const_long(irg, mode_Bu, 0xb9), cons_none);
1465         mem = new_r_Proj(st, mode_M, pn_Store_M);
1466         p   = new_r_Add(block, p, one, mode);
1467         st  = new_r_Store(block, mem, p, env, cons_none);
1468         mem = new_r_Proj(st, mode_M, pn_Store_M);
1469         p   = new_r_Add(block, p, four, mode);
1470         /* jmp  <callee> */
1471         st  = new_r_Store(block, mem, p, new_r_Const_long(irg, mode_Bu, 0xe9), cons_none);
1472         mem = new_r_Proj(st, mode_M, pn_Store_M);
1473         p   = new_r_Add(block, p, one, mode);
1474         st  = new_r_Store(block, mem, p, callee, cons_none);
1475         mem = new_r_Proj(st, mode_M, pn_Store_M);
1476
1477         return mem;
1478 }
1479
1480 static const ir_settings_arch_dep_t ia32_arch_dep = {
1481         1,                   /* also use subs */
1482         4,                   /* maximum shifts */
1483         63,                  /* maximum shift amount */
1484         ia32_evaluate_insn,  /* evaluate the instruction sequence */
1485
1486         1,  /* allow Mulhs */
1487         1,  /* allow Mulus */
1488         32, /* Mulh allowed up to 32 bit */
1489 };
1490 static backend_params ia32_backend_params = {
1491         1,     /* support Rotl nodes */
1492         0,     /* little endian */
1493         1,     /* modulo shift efficient */
1494         0,     /* non-modulo shift not efficient */
1495         &ia32_arch_dep, /* will be set later */
1496         ia32_is_mux_allowed,
1497         32,    /* machine_size */
1498         NULL,  /* float arithmetic mode, will be set below */
1499         NULL,  /* long long type */
1500         NULL,  /* unsigned long long type */
1501         NULL,  /* long double type */
1502         12,    /* size of trampoline code */
1503         4,     /* alignment of trampoline code */
1504         ia32_create_trampoline_fkt,
1505         4      /* alignment of stack parameter */
1506 };
1507
1508 /**
1509  * Initializes the backend ISA.
1510  */
1511 static void ia32_init(void)
1512 {
1513         ir_mode *mode_long_long;
1514         ir_mode *mode_unsigned_long_long;
1515         ir_type *type_long_long;
1516         ir_type *type_unsigned_long_long;
1517
1518         ia32_setup_cg_config();
1519
1520         init_asm_constraints();
1521
1522         ia32_mode_fpcw = new_int_mode("Fpcw", irma_twos_complement, 16, 0, 0);
1523
1524         /* note mantissa is 64bit but with explicitely encoded 1 so the really
1525          * usable part as counted by firm is only 63 bits */
1526         ia32_mode_E = new_float_mode("E", irma_x86_extended_float, 15, 63);
1527         ia32_type_E = new_type_primitive(ia32_mode_E);
1528         set_type_size_bytes(ia32_type_E, 12);
1529         set_type_alignment_bytes(ia32_type_E, 4);
1530
1531         mode_long_long = new_int_mode("long long", irma_twos_complement, 64, 1, 64);
1532         type_long_long = new_type_primitive(mode_long_long);
1533         mode_unsigned_long_long
1534                 = new_int_mode("unsigned long long", irma_twos_complement, 64, 0, 64);
1535         type_unsigned_long_long = new_type_primitive(mode_unsigned_long_long);
1536
1537         ia32_backend_params.type_long_long          = type_long_long;
1538         ia32_backend_params.type_unsigned_long_long = type_unsigned_long_long;
1539
1540         if (ia32_cg_config.use_sse2 || ia32_cg_config.use_softfloat) {
1541                 ia32_backend_params.mode_float_arithmetic = NULL;
1542                 ia32_backend_params.type_long_double = NULL;
1543         } else {
1544                 ia32_backend_params.mode_float_arithmetic = ia32_mode_E;
1545                 ia32_backend_params.type_long_double      = ia32_type_E;
1546         }
1547
1548         ia32_register_init();
1549         obstack_init(&opcodes_obst);
1550         ia32_create_opcodes(&ia32_irn_ops);
1551 }
1552
1553 static void ia32_finish(void)
1554 {
1555         if (between_type != NULL) {
1556                 free_type(between_type);
1557                 between_type = NULL;
1558         }
1559         ia32_free_opcodes();
1560         obstack_free(&opcodes_obst, NULL);
1561 }
1562
1563 /**
1564  * The template that generates a new ISA object.
1565  * Note that this template can be changed by command line
1566  * arguments.
1567  */
1568 static ia32_isa_t ia32_isa_template = {
1569         {
1570                 &ia32_isa_if,             /* isa interface implementation */
1571                 N_IA32_REGISTERS,
1572                 ia32_registers,
1573                 N_IA32_CLASSES,
1574                 ia32_reg_classes,
1575                 &ia32_registers[REG_ESP], /* stack pointer register */
1576                 &ia32_registers[REG_EBP], /* base pointer register */
1577                 2,                        /* power of two stack alignment, 2^2 == 4 */
1578                 7,                        /* costs for a spill instruction */
1579                 5,                        /* costs for a reload instruction */
1580                 false,                    /* no custom abi handling */
1581         },
1582         NULL,                       /* tv_ents */
1583         IA32_FPU_ARCH_X87,          /* FPU architecture */
1584 };
1585
1586 static arch_env_t *ia32_begin_codegeneration(void)
1587 {
1588         ia32_isa_t *isa = XMALLOC(ia32_isa_t);
1589
1590         set_tarval_output_modes();
1591
1592         *isa        = ia32_isa_template;
1593         isa->tv_ent = pmap_create();
1594
1595         return &isa->base;
1596 }
1597
1598 /**
1599  * Closes the output file and frees the ISA structure.
1600  */
1601 static void ia32_end_codegeneration(void *self)
1602 {
1603         ia32_isa_t *isa = (ia32_isa_t*)self;
1604         pmap_destroy(isa->tv_ent);
1605         free(self);
1606 }
1607
1608 /**
1609  * Returns the register for parameter nr.
1610  */
1611 static const arch_register_t *ia32_get_RegParam_reg(unsigned cc, unsigned nr,
1612                                                     const ir_mode *mode)
1613 {
1614         static const arch_register_t *gpreg_param_reg_fastcall[] = {
1615                 &ia32_registers[REG_ECX],
1616                 &ia32_registers[REG_EDX],
1617                 NULL
1618         };
1619         static const unsigned MAXNUM_GPREG_ARGS = 3;
1620
1621         static const arch_register_t *gpreg_param_reg_regparam[] = {
1622                 &ia32_registers[REG_EAX],
1623                 &ia32_registers[REG_EDX],
1624                 &ia32_registers[REG_ECX]
1625         };
1626
1627         static const arch_register_t *gpreg_param_reg_this[] = {
1628                 &ia32_registers[REG_ECX],
1629                 NULL,
1630                 NULL
1631         };
1632
1633         static const arch_register_t *fpreg_sse_param_reg_std[] = {
1634                 &ia32_registers[REG_XMM0],
1635                 &ia32_registers[REG_XMM1],
1636                 &ia32_registers[REG_XMM2],
1637                 &ia32_registers[REG_XMM3],
1638                 &ia32_registers[REG_XMM4],
1639                 &ia32_registers[REG_XMM5],
1640                 &ia32_registers[REG_XMM6],
1641                 &ia32_registers[REG_XMM7]
1642         };
1643
1644         static const arch_register_t *fpreg_sse_param_reg_this[] = {
1645                 NULL,  /* in case of a "this" pointer, the first parameter must not be a float */
1646         };
1647         static const unsigned MAXNUM_SSE_ARGS = 8;
1648
1649         if ((cc & cc_this_call) && nr == 0)
1650                 return gpreg_param_reg_this[0];
1651
1652         if (! (cc & cc_reg_param))
1653                 return NULL;
1654
1655         if (mode_is_float(mode)) {
1656                 if (!ia32_cg_config.use_sse2 || (cc & cc_fpreg_param) == 0)
1657                         return NULL;
1658                 if (nr >= MAXNUM_SSE_ARGS)
1659                         return NULL;
1660
1661                 if (cc & cc_this_call) {
1662                         return fpreg_sse_param_reg_this[nr];
1663                 }
1664                 return fpreg_sse_param_reg_std[nr];
1665         } else if (mode_is_int(mode) || mode_is_reference(mode)) {
1666                 unsigned num_regparam;
1667
1668                 if (get_mode_size_bits(mode) > 32)
1669                         return NULL;
1670
1671                 if (nr >= MAXNUM_GPREG_ARGS)
1672                         return NULL;
1673
1674                 if (cc & cc_this_call) {
1675                         return gpreg_param_reg_this[nr];
1676                 }
1677                 num_regparam = cc & ~cc_bits;
1678                 if (num_regparam == 0) {
1679                         /* default fastcall */
1680                         return gpreg_param_reg_fastcall[nr];
1681                 }
1682                 if (nr < num_regparam)
1683                         return gpreg_param_reg_regparam[nr];
1684                 return NULL;
1685         }
1686
1687         panic("unknown argument mode");
1688 }
1689
1690 /**
1691  * Get the ABI restrictions for procedure calls.
1692  */
1693 static void ia32_get_call_abi(ir_type *method_type, be_abi_call_t *abi)
1694 {
1695         unsigned  cc;
1696         int       n, i, regnum;
1697         int                 pop_amount = 0;
1698         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1699
1700         /* set abi flags for calls */
1701         /* call_flags.try_omit_fp                 not changed: can handle both settings */
1702         call_flags.call_has_imm = false;  /* No call immediate, we handle this by ourselves */
1703
1704         /* set parameter passing style */
1705         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1706
1707         cc = get_method_calling_convention(method_type);
1708         if (get_method_variadicity(method_type) == variadicity_variadic) {
1709                 /* pass all parameters of a variadic function on the stack */
1710                 cc = cc_cdecl_set | (cc & cc_this_call);
1711         } else {
1712                 if (get_method_additional_properties(method_type) & mtp_property_private &&
1713                     ia32_cg_config.optimize_cc) {
1714                         /* set the fast calling conventions (allowing up to 3) */
1715                         cc = SET_FASTCALL(cc) | 3;
1716                 }
1717         }
1718
1719         /* we have to pop the shadow parameter ourself for compound calls */
1720         if ( (get_method_calling_convention(method_type) & cc_compound_ret)
1721                         && !(cc & cc_reg_param)) {
1722                 pop_amount += get_mode_size_bytes(mode_P_data);
1723         }
1724
1725         n = get_method_n_params(method_type);
1726         for (i = regnum = 0; i < n; i++) {
1727                 const arch_register_t *reg  = NULL;
1728                 ir_type               *tp   = get_method_param_type(method_type, i);
1729                 ir_mode               *mode = get_type_mode(tp);
1730
1731                 if (mode != NULL) {
1732                         reg  = ia32_get_RegParam_reg(cc, regnum, mode);
1733                 }
1734                 if (reg != NULL) {
1735                         be_abi_call_param_reg(abi, i, reg, ABI_CONTEXT_BOTH);
1736                         ++regnum;
1737                 } else {
1738                         /* Micro optimisation: if the mode is shorter than 4 bytes, load 4 bytes.
1739                          * movl has a shorter opcode than mov[sz][bw]l */
1740                         ir_mode *load_mode = mode;
1741
1742                         if (mode != NULL) {
1743                                 unsigned size = get_mode_size_bytes(mode);
1744
1745                                 if (cc & cc_callee_clear_stk) {
1746                                         pop_amount += (size + 3U) & ~3U;
1747                                 }
1748
1749                                 if (size < 4) load_mode = mode_Iu;
1750                         }
1751
1752                         be_abi_call_param_stack(abi, i, load_mode, 4, 0, 0, ABI_CONTEXT_BOTH);
1753                 }
1754         }
1755
1756         be_abi_call_set_pop(abi, pop_amount);
1757
1758         /* set return registers */
1759         n = get_method_n_ress(method_type);
1760
1761         assert(n <= 2 && "more than two results not supported");
1762
1763         /* In case of 64bit returns, we will have two 32bit values */
1764         if (n == 2) {
1765                 ir_type *tp   = get_method_res_type(method_type, 0);
1766                 ir_mode *mode = get_type_mode(tp);
1767
1768                 assert(!mode_is_float(mode) && "two FP results not supported");
1769
1770                 tp   = get_method_res_type(method_type, 1);
1771                 mode = get_type_mode(tp);
1772
1773                 assert(!mode_is_float(mode) && "mixed INT, FP results not supported");
1774
1775                 be_abi_call_res_reg(abi, 0, &ia32_registers[REG_EAX], ABI_CONTEXT_BOTH);
1776                 be_abi_call_res_reg(abi, 1, &ia32_registers[REG_EDX], ABI_CONTEXT_BOTH);
1777         }
1778         else if (n == 1) {
1779                 ir_type *tp   = get_method_res_type(method_type, 0);
1780                 ir_mode *mode = get_type_mode(tp);
1781                 const arch_register_t *reg;
1782                 assert(is_atomic_type(tp));
1783
1784                 reg = mode_is_float(mode) ? &ia32_registers[REG_ST0] : &ia32_registers[REG_EAX];
1785
1786                 be_abi_call_res_reg(abi, 0, reg, ABI_CONTEXT_BOTH);
1787         }
1788 }
1789
1790 static void ia32_mark_remat(ir_node *node)
1791 {
1792         if (is_ia32_irn(node)) {
1793                 set_ia32_is_remat(node);
1794         }
1795 }
1796
1797 static asm_constraint_flags_t ia32_parse_asm_constraint(const char **c)
1798 {
1799         (void) c;
1800
1801         /* we already added all our simple flags to the flags modifier list in
1802          * init, so this flag we don't know. */
1803         return ASM_CONSTRAINT_FLAG_INVALID;
1804 }
1805
1806 static int ia32_is_valid_clobber(const char *clobber)
1807 {
1808         return ia32_get_clobber_register(clobber) != NULL;
1809 }
1810
1811 static void ia32_lower_for_target(void)
1812 {
1813         ir_mode *mode_gp = ia32_reg_classes[CLASS_ia32_gp].mode;
1814         size_t i, n_irgs = get_irp_n_irgs();
1815
1816         /* perform doubleword lowering */
1817         lwrdw_param_t lower_dw_params = {
1818                 1,  /* little endian */
1819                 64, /* doubleword size */
1820                 ia32_create_intrinsic_fkt,
1821                 &intrinsic_env,
1822         };
1823
1824         /* lower compound param handling
1825          * Note: we lower compound arguments ourself, since on ia32 we don't
1826          * have hidden parameters but know where to find the structs on the stack.
1827          * (This also forces us to always allocate space for the compound arguments
1828          *  on the callframe and we can't just use an arbitrary position on the
1829          *  stackframe)
1830          */
1831         lower_calls_with_compounds(LF_RETURN_HIDDEN | LF_DONT_LOWER_ARGUMENTS);
1832
1833         /* replace floating point operations by function calls */
1834         if (ia32_cg_config.use_softfloat) {
1835                 lower_floating_point();
1836         }
1837
1838         for (i = 0; i < n_irgs; ++i) {
1839                 ir_graph *irg = get_irp_irg(i);
1840                 /* break up switches with wide ranges */
1841                 lower_switch(irg, 4, 256, mode_gp);
1842         }
1843
1844         ir_prepare_dw_lowering(&lower_dw_params);
1845         ir_lower_dw_ops();
1846
1847         for (i = 0; i < n_irgs; ++i) {
1848                 ir_graph *irg = get_irp_irg(i);
1849                 /* lower for mode_b stuff */
1850                 ir_lower_mode_b(irg, mode_Iu);
1851         }
1852
1853         for (i = 0; i < n_irgs; ++i) {
1854                 ir_graph *irg = get_irp_irg(i);
1855                 /* Turn all small CopyBs into loads/stores, keep medium-sized CopyBs,
1856                  * so we can generate rep movs later, and turn all big CopyBs into
1857                  * memcpy calls. */
1858                 lower_CopyB(irg, 64, 8193, true);
1859         }
1860 }
1861
1862 /**
1863  * Returns the libFirm configuration parameter for this backend.
1864  */
1865 static const backend_params *ia32_get_libfirm_params(void)
1866 {
1867         return &ia32_backend_params;
1868 }
1869
1870 /**
1871  * Check if the given register is callee or caller save.
1872  */
1873 static int ia32_register_saved_by(const arch_register_t *reg, int callee)
1874 {
1875         switch (reg->global_index) {
1876         case REG_EBX:
1877         case REG_EBP:
1878         case REG_ESI:
1879         case REG_EDI:
1880         case REG_FPCW:
1881                 return callee;
1882
1883         case REG_EAX:
1884         case REG_ECX:
1885         case REG_EDX:
1886         case REG_ST0:
1887         case REG_ST1:
1888         case REG_ST2:
1889         case REG_ST3:
1890         case REG_ST4:
1891         case REG_ST5:
1892         case REG_ST6:
1893         case REG_ST7:
1894         case REG_XMM0:
1895         case REG_XMM1:
1896         case REG_XMM2:
1897         case REG_XMM3:
1898         case REG_XMM4:
1899         case REG_XMM5:
1900         case REG_XMM6:
1901         case REG_XMM7:
1902                 return !callee;
1903
1904         default:
1905                 return 0;
1906         }
1907 }
1908
1909 static const lc_opt_enum_int_items_t gas_items[] = {
1910         { "elf",   OBJECT_FILE_FORMAT_ELF    },
1911         { "mingw", OBJECT_FILE_FORMAT_COFF   },
1912         { "macho", OBJECT_FILE_FORMAT_MACH_O },
1913         { NULL,    0 }
1914 };
1915
1916 static lc_opt_enum_int_var_t gas_var = {
1917         (int*) &be_gas_object_file_format, gas_items
1918 };
1919
1920 #ifdef FIRM_GRGEN_BE
1921 static const lc_opt_enum_int_items_t transformer_items[] = {
1922         { "default", TRANSFORMER_DEFAULT },
1923         { "pbqp",    TRANSFORMER_PBQP    },
1924         { "random",  TRANSFORMER_RAND    },
1925         { NULL,      0                   }
1926 };
1927
1928 static lc_opt_enum_int_var_t transformer_var = {
1929         (int*)&be_transformer, transformer_items
1930 };
1931 #endif
1932
1933 static const lc_opt_table_entry_t ia32_options[] = {
1934         LC_OPT_ENT_ENUM_INT("gasmode", "set the GAS compatibility mode", &gas_var),
1935 #ifdef FIRM_GRGEN_BE
1936         LC_OPT_ENT_ENUM_INT("transformer", "the transformer used for code selection", &transformer_var),
1937 #endif
1938         LC_OPT_ENT_INT ("stackalign", "set power of two stack alignment for calls",
1939                         &ia32_isa_template.base.stack_alignment),
1940         LC_OPT_ENT_BOOL("gprof",      "create gprof profiling code",                                    &gprof),
1941         LC_OPT_ENT_BOOL("precise_float_spill", "Spill floatingpoint values precisely (the whole 80 bits)", &precise_x87_spills),
1942         LC_OPT_LAST
1943 };
1944
1945 const arch_isa_if_t ia32_isa_if = {
1946         ia32_init,
1947         ia32_finish,
1948         ia32_get_libfirm_params,
1949         ia32_lower_for_target,
1950         ia32_parse_asm_constraint,
1951         ia32_is_valid_clobber,
1952
1953         ia32_begin_codegeneration,
1954         ia32_end_codegeneration,
1955         ia32_init_graph,
1956         ia32_get_call_abi,
1957         ia32_mark_remat,
1958         ia32_get_pic_base,   /* return node used as base in pic code addresses */
1959         be_new_spill,
1960         be_new_reload,
1961         ia32_register_saved_by,
1962
1963         ia32_handle_intrinsics,
1964         ia32_before_abi,     /* before abi introduce hook */
1965         ia32_prepare_graph,
1966         ia32_before_ra,      /* before register allocation hook */
1967         ia32_finish_graph,   /* called before codegen */
1968         ia32_emit,           /* emit && done */
1969 };
1970
1971 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ia32)
1972 void be_init_arch_ia32(void)
1973 {
1974         lc_opt_entry_t *be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
1975         lc_opt_entry_t *ia32_grp = lc_opt_get_grp(be_grp, "ia32");
1976
1977         lc_opt_add_table(ia32_grp, ia32_options);
1978         be_register_isa_if("ia32", &ia32_isa_if);
1979
1980         ia32_init_emitter();
1981         ia32_init_finish();
1982         ia32_init_optimize();
1983         ia32_init_transform();
1984         ia32_init_x87();
1985         ia32_init_architecture();
1986 }