BugFix: create ProjM after Store
[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 _WIN32
12 #include <malloc.h>
13 #else
14 #include <alloca.h>
15 #endif
16
17 #include "pseudo_irg.h"
18 #include "irgwalk.h"
19 #include "irprog.h"
20 #include "irprintf.h"
21 #include "iredges_t.h"
22 #include "ircons.h"
23 #include "irgmod.h"
24 #include "irgopt.h"
25
26 #include "bitset.h"
27 #include "debug.h"
28
29 #include "../beabi.h"                 /* the general register allocator interface */
30 #include "../benode_t.h"
31 #include "../belower.h"
32 #include "../besched_t.h"
33 #include "../be.h"
34 #include "bearch_ia32_t.h"
35
36 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
37 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
38 #include "ia32_gen_decls.h"           /* interface declaration emitter */
39 #include "ia32_transform.h"
40 #include "ia32_emitter.h"
41 #include "ia32_map_regs.h"
42 #include "ia32_optimize.h"
43 #include "ia32_x87.h"
44
45 #define DEBUG_MODULE "firm.be.ia32.isa"
46
47 /* TODO: ugly */
48 static set *cur_reg_set = NULL;
49
50 #undef is_Start
51 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
52
53 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
54         return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_GP_NOREG]);
55 }
56
57 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
58         return be_abi_get_callee_save_irn(cg->birg->abi,
59                 USE_SSE2(cg) ? &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG]);
60 }
61
62 /**************************************************
63  *                         _ _              _  __
64  *                        | | |            (_)/ _|
65  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
66  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
67  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
68  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
69  *            __/ |
70  *           |___/
71  **************************************************/
72
73 static ir_node *my_skip_proj(const ir_node *n) {
74         while (is_Proj(n))
75                 n = get_Proj_pred(n);
76         return (ir_node *)n;
77 }
78
79
80 /**
81  * Return register requirements for an ia32 node.
82  * If the node returns a tuple (mode_T) then the proj's
83  * will be asked for this information.
84  */
85 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) {
86         const ia32_irn_ops_t      *ops = self;
87         const ia32_register_req_t *irn_req;
88         long                       node_pos = pos == -1 ? 0 : pos;
89         ir_mode                   *mode     = is_Block(irn) ? NULL : get_irn_mode(irn);
90         FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
91
92         if (is_Block(irn) || mode == mode_M || mode == mode_X) {
93                 DBG((mod, LEVEL_1, "ignoring Block, mode_M, mode_X node %+F\n", irn));
94                 return NULL;
95         }
96
97         if (mode == mode_T && pos < 0) {
98                 DBG((mod, LEVEL_1, "ignoring request OUT requirements for node %+F\n", irn));
99                 return NULL;
100         }
101
102         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
103
104         if (is_Proj(irn)) {
105                 if (pos == -1) {
106                         node_pos = ia32_translate_proj_pos(irn);
107                 }
108                 else {
109                         node_pos = pos;
110                 }
111
112                 irn = my_skip_proj(irn);
113
114                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
115         }
116
117         if (is_ia32_irn(irn)) {
118                 if (pos >= 0) {
119                         irn_req = get_ia32_in_req(irn, pos);
120                 }
121                 else {
122                         irn_req = get_ia32_out_req(irn, node_pos);
123                 }
124
125                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
126
127                 memcpy(req, &(irn_req->req), sizeof(*req));
128
129                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
130                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
131                         req->other_same = get_irn_n(irn, irn_req->same_pos);
132                 }
133
134                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
135                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
136                         req->other_different = get_irn_n(irn, irn_req->different_pos);
137                 }
138         }
139         else {
140                 /* treat Phi like Const with default requirements */
141                 if (is_Phi(irn)) {
142                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
143                         if (mode_is_float(mode)) {
144                                 if (USE_SSE2(ops->cg))
145                                         memcpy(req, &(ia32_default_req_ia32_xmm.req), sizeof(*req));
146                                 else
147                                         memcpy(req, &(ia32_default_req_ia32_vfp.req), sizeof(*req));
148                         }
149                         else if (mode_is_int(mode) || mode_is_reference(mode))
150                                 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
151                         else if (mode == mode_T || mode == mode_M) {
152                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
153                                 return NULL;
154                         }
155                         else
156                                 assert(0 && "unsupported Phi-Mode");
157                 }
158                 else {
159                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
160                         req = NULL;
161                 }
162         }
163
164         return req;
165 }
166
167 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
168         int                   pos = 0;
169         const ia32_irn_ops_t *ops = self;
170
171         if (get_irn_mode(irn) == mode_X) {
172                 return;
173         }
174
175         DBG((ops->cg->mod, LEVEL_1, "ia32 assigned register %s to node %+F\n", reg->name, irn));
176
177         if (is_Proj(irn)) {
178                 pos = ia32_translate_proj_pos(irn);
179                 irn = my_skip_proj(irn);
180         }
181
182         if (is_ia32_irn(irn)) {
183                 const arch_register_t **slots;
184
185                 slots      = get_ia32_slots(irn);
186                 slots[pos] = reg;
187         }
188         else {
189                 ia32_set_firm_reg(irn, reg, cur_reg_set);
190         }
191 }
192
193 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
194         int pos = 0;
195         const arch_register_t *reg = NULL;
196
197         if (is_Proj(irn)) {
198
199                 if (get_irn_mode(irn) == mode_X) {
200                         return NULL;
201                 }
202
203                 pos = ia32_translate_proj_pos(irn);
204                 irn = my_skip_proj(irn);
205         }
206
207         if (is_ia32_irn(irn)) {
208                 const arch_register_t **slots;
209                 slots = get_ia32_slots(irn);
210                 reg   = slots[pos];
211         }
212         else {
213                 reg = ia32_get_firm_reg(irn, cur_reg_set);
214         }
215
216         return reg;
217 }
218
219 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
220         irn = my_skip_proj(irn);
221         if (is_cfop(irn))
222                 return arch_irn_class_branch;
223         else if (is_ia32_irn(irn))
224                 return arch_irn_class_normal;
225         else
226                 return 0;
227 }
228
229 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
230         irn = my_skip_proj(irn);
231         if (is_ia32_irn(irn))
232                 return get_ia32_flags(irn);
233         else {
234                 return 0;
235         }
236 }
237
238 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
239         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
240 }
241
242 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
243         char buf[64];
244         const ia32_irn_ops_t *ops = self;
245
246         if (is_ia32_use_frame(irn) && bias != 0) {
247                 ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
248
249                 DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
250                 snprintf(buf, sizeof(buf), "%d", bias);
251                 add_ia32_am_offs(irn, buf);
252                 am_flav |= ia32_O;
253                 set_ia32_am_flavour(irn, am_flav);
254         }
255 }
256
257 typedef struct {
258         be_abi_call_flags_bits_t flags;
259         const arch_isa_t *isa;
260         ir_graph *irg;
261 } ia32_abi_env_t;
262
263 static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
264 {
265         ia32_abi_env_t *env    = xmalloc(sizeof(env[0]));
266         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
267         env->flags = fl.bits;
268         env->irg   = irg;
269         env->isa   = aenv->isa;
270         return env;
271 }
272
273 static void ia32_abi_dont_save_regs(void *self, pset *s)
274 {
275         ia32_abi_env_t *env = self;
276         if(env->flags.try_omit_fp)
277                 pset_insert_ptr(s, env->isa->bp);
278 }
279
280 static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
281 {
282         ia32_abi_env_t *env              = self;
283         const arch_register_t *frame_reg = env->isa->sp;
284
285         if(!env->flags.try_omit_fp) {
286                 int reg_size         = get_mode_size_bytes(env->isa->bp->reg_class->mode);
287                 ir_node *bl          = get_irg_start_block(env->irg);
288                 ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
289                 ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
290                 ir_node *curr_no_reg = be_abi_reg_map_get(reg_map, &ia32_gp_regs[REG_GP_NOREG]);
291                 ir_node *store_bp;
292
293                 curr_sp  = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, reg_size, be_stack_dir_expand);
294                 store_bp = new_rd_ia32_Store(NULL, env->irg, bl, curr_sp, curr_no_reg, curr_bp, *mem, mode_T);
295                 set_ia32_am_support(store_bp, ia32_am_Dest);
296                 set_ia32_am_flavour(store_bp, ia32_B);
297                 set_ia32_op_type(store_bp, ia32_AddrModeD);
298                 *mem     = new_r_Proj(env->irg, bl, store_bp, mode_M, 0);
299                 curr_bp  = be_new_Copy(env->isa->bp->reg_class, env->irg, bl, curr_sp);
300                 be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), env->isa->bp);
301                 be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
302
303                 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
304                 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
305         }
306
307         return frame_reg;
308 }
309
310 static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
311 {
312         ia32_abi_env_t *env = self;
313         ir_node *curr_sp     = be_abi_reg_map_get(reg_map, env->isa->sp);
314         ir_node *curr_bp     = be_abi_reg_map_get(reg_map, env->isa->bp);
315         ir_node *curr_no_reg = be_abi_reg_map_get(reg_map, &ia32_gp_regs[REG_GP_NOREG]);
316
317         if(env->flags.try_omit_fp) {
318                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_shrink);
319         }
320
321         else {
322                 ir_node *load_bp;
323                 ir_mode *mode_bp = env->isa->bp->reg_class->mode;
324
325                 curr_sp = be_new_SetSP(env->isa->sp, env->irg, bl, curr_sp, curr_bp, *mem);
326                 load_bp = new_rd_ia32_Load(NULL, env->irg, bl, curr_sp, curr_no_reg, *mem, mode_T);
327                 set_ia32_am_support(load_bp, ia32_am_Source);
328                 set_ia32_am_flavour(load_bp, ia32_B);
329                 set_ia32_op_type(load_bp, ia32_AddrModeS);
330                 set_ia32_ls_mode(load_bp, mode_bp);
331                 curr_bp = new_r_Proj(env->irg, bl, load_bp, mode_bp, 0);
332                 *mem    = new_r_Proj(env->irg, bl, load_bp, mode_M, 1);
333         }
334
335         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
336         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
337 }
338
339 /**
340  * Produces the type which sits between the stack args and the locals on the stack.
341  * it will contain the return address and space to store the old base pointer.
342  * @return The Firm type modeling the ABI between type.
343  */
344 static ir_type *ia32_abi_get_between_type(void *self)
345 {
346         static ir_type *omit_fp_between_type = NULL;
347         static ir_type *between_type         = NULL;
348
349         ia32_abi_env_t *env = self;
350
351         if(!between_type) {
352                 entity *old_bp_ent;
353                 entity *ret_addr_ent;
354                 entity *omit_fp_ret_addr_ent;
355
356                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
357                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
358
359                 between_type           = new_type_class(new_id_from_str("ia32_between_type"));
360                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
361                 ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
362
363                 set_entity_offset_bytes(old_bp_ent, 0);
364                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
365                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
366
367                 omit_fp_between_type   = new_type_class(new_id_from_str("ia32_between_type_omit_fp"));
368                 omit_fp_ret_addr_ent   = new_entity(omit_fp_between_type, new_id_from_str("ret_addr"), ret_addr_type);
369
370                 set_entity_offset_bytes(omit_fp_ret_addr_ent, 0);
371                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
372         }
373
374         return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
375 }
376
377 static const be_abi_callbacks_t ia32_abi_callbacks = {
378         ia32_abi_init,
379         free,
380         ia32_abi_get_between_type,
381         ia32_abi_dont_save_regs,
382         ia32_abi_prologue,
383         ia32_abi_epilogue,
384 };
385
386 /* fill register allocator interface */
387
388 static const arch_irn_ops_if_t ia32_irn_ops_if = {
389         ia32_get_irn_reg_req,
390         ia32_set_irn_reg,
391         ia32_get_irn_reg,
392         ia32_classify,
393         ia32_get_flags,
394         ia32_get_frame_entity,
395         ia32_set_stack_bias
396 };
397
398 ia32_irn_ops_t ia32_irn_ops = {
399         &ia32_irn_ops_if,
400         NULL
401 };
402
403
404
405 /**************************************************
406  *                _                         _  __
407  *               | |                       (_)/ _|
408  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
409  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
410  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
411  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
412  *                        __/ |
413  *                       |___/
414  **************************************************/
415
416 /**
417  * Transforms the standard firm graph into
418  * an ia32 firm graph
419  */
420 static void ia32_prepare_graph(void *self) {
421         ia32_code_gen_t *cg = self;
422         firm_dbg_module_t *old_mod = cg->mod;
423
424         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.transform");
425         irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_node, cg);
426         be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
427
428         cg->mod = old_mod;
429
430         if (cg->opt.doam) {
431                 edges_deactivate(cg->irg);
432                 //dead_node_elimination(cg->irg);
433                 edges_activate(cg->irg);
434
435                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
436                 be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
437         }
438 }
439
440
441 /**
442  * Insert copies for all ia32 nodes where the should_be_same requirement
443  * is not fulfilled.
444  * Transform Sub into Neg -- Add if IN2 == OUT
445  */
446 static void ia32_finish_irg_walker(ir_node *irn, void *env) {
447         ia32_code_gen_t            *cg = env;
448         const ia32_register_req_t **reqs;
449         const arch_register_t      *out_reg, *in_reg;
450         int                         n_res, i;
451         ir_node                    *copy, *in_node, *block;
452         ia32_op_type_t              op_tp;
453
454         if (! is_ia32_irn(irn))
455                 return;
456
457         /* AM Dest nodes don't produce any values  */
458         op_tp = get_ia32_op_type(irn);
459         if (op_tp == ia32_AddrModeD)
460                 return;
461
462         reqs  = get_ia32_out_req_all(irn);
463         n_res = get_ia32_n_res(irn);
464         block = get_nodes_block(irn);
465
466         /* check all OUT requirements, if there is a should_be_same */
467         if (op_tp == ia32_Normal) {
468                 for (i = 0; i < n_res; i++) {
469                         if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
470                                 /* get in and out register */
471                                 out_reg = get_ia32_out_reg(irn, i);
472                                 in_node = get_irn_n(irn, reqs[i]->same_pos);
473                                 in_reg  = arch_get_irn_register(cg->arch_env, in_node);
474
475                                 /* don't copy ignore nodes */
476                                 if (arch_irn_is(cg->arch_env, in_node, ignore))
477                                         continue;
478
479                                 /* check if in and out register are equal */
480                                 if (arch_register_get_index(out_reg) != arch_register_get_index(in_reg)) {
481                                         DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
482
483                                         /* create copy from in register */
484                                         copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
485
486                                         /* destination is the out register */
487                                         arch_set_irn_register(cg->arch_env, copy, out_reg);
488
489                                         /* insert copy before the node into the schedule */
490                                         sched_add_before(irn, copy);
491
492                                         /* set copy as in */
493                                         set_irn_n(irn, reqs[i]->same_pos, copy);
494                                 }
495                         }
496                 }
497         }
498
499         /* If we have a CondJmp with immediate, we need to    */
500         /* check if it's the right operand, otherwise we have */
501         /* to change it, as CMP doesn't support immediate as  */
502         /* left operands.                                     */
503         if (is_ia32_CondJmp(irn) && (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn)) && op_tp == ia32_AddrModeS) {
504                 long pnc = get_negated_pnc(get_ia32_pncode(irn), get_ia32_res_mode(irn));
505                 set_ia32_op_type(irn, ia32_AddrModeD);
506                 set_ia32_pncode(irn, pnc);
507         }
508
509         /* check if there is a sub which need to be transformed */
510         ia32_transform_sub_to_neg_add(irn, cg);
511
512         /* transform a LEA into an Add if possible */
513         ia32_transform_lea_to_add(irn, cg);
514
515         /* check for peephole optimization */
516         ia32_peephole_optimization(irn, cg);
517 }
518
519 /**
520  * Add Copy nodes for not fulfilled should_be_equal constraints
521  */
522 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
523         irg_walk_blkwise_graph(irg, NULL, ia32_finish_irg_walker, cg);
524 }
525
526
527
528 /**
529  * Dummy functions for hooks we don't need but which must be filled.
530  */
531 static void ia32_before_sched(void *self) {
532 }
533
534 /**
535  * Called before the register allocator.
536  * Calculate a block schedule here. We need it for the x87
537  * simulator and the emitter.
538  */
539 static void ia32_before_ra(void *self) {
540         ia32_code_gen_t *cg = self;
541
542         cg->blk_sched = sched_create_block_schedule(cg->irg);
543 }
544
545
546 /**
547  * Transforms a be node into a Load.
548  */
549 static void transform_to_Load(ia32_transform_env_t *env) {
550         ir_node *irn         = env->irn;
551         entity  *ent         = be_get_frame_entity(irn);
552         ir_mode *mode        = env->mode;
553         ir_node *noreg       = ia32_new_NoReg_gp(env->cg);
554         ir_node *nomem       = new_rd_NoMem(env->irg);
555         ir_node *sched_point = NULL;
556         ir_node *ptr         = get_irn_n(irn, 0);
557         ir_node *mem         = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
558         ir_node *new_op, *proj;
559         const arch_register_t *reg;
560
561         if (sched_is_scheduled(irn)) {
562                 sched_point = sched_prev(irn);
563         }
564
565         if (mode_is_float(mode)) {
566                 new_op = new_rd_ia32_fLoad(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
567         }
568         else {
569                 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
570         }
571
572         set_ia32_am_support(new_op, ia32_am_Source);
573         set_ia32_op_type(new_op, ia32_AddrModeS);
574         set_ia32_am_flavour(new_op, ia32_B);
575         set_ia32_ls_mode(new_op, mode);
576         set_ia32_frame_ent(new_op, ent);
577         set_ia32_use_frame(new_op);
578
579         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
580
581         if (sched_point) {
582                 sched_add_after(sched_point, new_op);
583                 sched_add_after(new_op, proj);
584
585                 sched_remove(irn);
586         }
587
588         /* copy the register from the old node to the new Load */
589         reg = arch_get_irn_register(env->cg->arch_env, irn);
590         arch_set_irn_register(env->cg->arch_env, new_op, reg);
591
592         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env));
593
594         exchange(irn, proj);
595 }
596
597 /**
598  * Transforms a be node into a Store.
599  */
600 static void transform_to_Store(ia32_transform_env_t *env) {
601         ir_node *irn   = env->irn;
602         entity  *ent   = be_get_frame_entity(irn);
603         ir_mode *mode  = env->mode;
604         ir_node *noreg = ia32_new_NoReg_gp(env->cg);
605         ir_node *nomem = new_rd_NoMem(env->irg);
606         ir_node *ptr   = get_irn_n(irn, 0);
607         ir_node *val   = get_irn_n(irn, 1);
608         ir_node *new_op, *proj;
609         ir_node *sched_point = NULL;
610
611         if (sched_is_scheduled(irn)) {
612                 sched_point = sched_prev(irn);
613         }
614
615         if (mode_is_float(mode)) {
616                 new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
617         }
618         else if (get_mode_size_bits(mode) == 8) {
619                 new_op = new_rd_ia32_Store8Bit(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
620         }
621         else {
622                 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
623         }
624
625         set_ia32_am_support(new_op, ia32_am_Dest);
626         set_ia32_op_type(new_op, ia32_AddrModeD);
627         set_ia32_am_flavour(new_op, ia32_B);
628         set_ia32_ls_mode(new_op, mode);
629         set_ia32_frame_ent(new_op, ent);
630         set_ia32_use_frame(new_op);
631
632         proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode_M, 0);
633
634         if (sched_point) {
635                 sched_add_after(sched_point, new_op);
636                 sched_add_after(new_op, proj);
637
638                 sched_remove(irn);
639         }
640
641         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(env));
642
643         exchange(irn, proj);
644 }
645
646 /**
647  * Calls the transform functions for StackParam, Spill and Reload.
648  */
649 static void ia32_after_ra_walker(ir_node *node, void *env) {
650         ia32_code_gen_t *cg = env;
651         ia32_transform_env_t tenv;
652
653         if (is_Block(node))
654                 return;
655
656         tenv.block = get_nodes_block(node);
657         tenv.dbg   = get_irn_dbg_info(node);
658         tenv.irg   = current_ir_graph;
659         tenv.irn   = node;
660         tenv.mod   = cg->mod;
661         tenv.mode  = get_irn_mode(node);
662         tenv.cg    = cg;
663
664         /* be_is_StackParam(node) || */
665         if (be_is_Reload(node)) {
666                 transform_to_Load(&tenv);
667         }
668         else if (be_is_Spill(node)) {
669                 /* we always spill the whole register  */
670                 tenv.mode = mode_is_float(get_irn_mode(be_get_Spill_context(node))) ? mode_D : mode_Is;
671                 transform_to_Store(&tenv);
672         }
673 }
674
675 /**
676  * We transform StackParam, Spill and Reload here. This needs to be done before
677  * stack biasing otherwise we would miss the corrected offset for these nodes.
678  */
679 static void ia32_after_ra(void *self) {
680         ia32_code_gen_t *cg = self;
681         irg_walk_blkwise_graph(cg->irg, NULL, ia32_after_ra_walker, self);
682
683         /* if we do x87 code generation, rewrite all the virtual instructions and registers */
684         if (cg->used_x87) {
685                 x87_simulate_graph(cg->arch_env, cg->irg, cg->blk_sched);
686                 be_dump(cg->irg, "-x87", dump_ir_extblock_graph_sched);
687         }
688 }
689
690
691 /**
692  * Emits the code, closes the output file and frees
693  * the code generator interface.
694  */
695 static void ia32_codegen(void *self) {
696         ia32_code_gen_t *cg = self;
697         ir_graph        *irg = cg->irg;
698         FILE            *out = cg->out;
699
700         if (cg->emit_decls) {
701                 ia32_gen_decls(cg->out);
702                 cg->emit_decls = 0;
703         }
704
705         ia32_finish_irg(irg, cg);
706         be_dump(irg, "-finished", dump_ir_block_graph_sched);
707         ia32_gen_routine(out, irg, cg);
708
709         cur_reg_set = NULL;
710
711         pmap_destroy(cg->tv_ent);
712         pmap_destroy(cg->types);
713
714         /* de-allocate code generator */
715         del_set(cg->reg_set);
716         free(self);
717 }
718
719 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
720
721 static const arch_code_generator_if_t ia32_code_gen_if = {
722         ia32_cg_init,
723         NULL,                /* before abi introduce hook */
724         ia32_prepare_graph,
725         ia32_before_sched,   /* before scheduling hook */
726         ia32_before_ra,      /* before register allocation hook */
727         ia32_after_ra,       /* after register allocation hook */
728         ia32_codegen         /* emit && done */
729 };
730
731 /**
732  * Initializes the code generator.
733  */
734 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
735         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
736         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
737
738         cg->impl      = &ia32_code_gen_if;
739         cg->irg       = birg->irg;
740         cg->reg_set   = new_set(ia32_cmp_irn_reg_assoc, 1024);
741         cg->out       = F;
742         cg->arch_env  = birg->main_env->arch_env;
743         cg->types     = pmap_create();
744         cg->tv_ent    = pmap_create();
745         cg->birg      = birg;
746         cg->blk_sched = NULL;
747         cg->fp_kind   = isa->fp_kind;
748         cg->used_x87  = 0;
749
750         FIRM_DBG_REGISTER(cg->mod, "firm.be.ia32.cg");
751
752         /* set optimizations */
753         cg->opt.incdec    = 0;
754         cg->opt.doam      = 1;
755         cg->opt.placecnst = 1;
756         cg->opt.immops    = 1;
757         cg->opt.extbb     = 1;
758
759 #ifndef NDEBUG
760         if (isa->name_obst_size) {
761                 //printf("freed %d bytes from name obst\n", isa->name_obst_size);
762                 isa->name_obst_size = 0;
763                 obstack_free(isa->name_obst, NULL);
764                 obstack_init(isa->name_obst);
765         }
766 #endif /* NDEBUG */
767
768         isa->num_codegens++;
769
770         if (isa->num_codegens > 1)
771                 cg->emit_decls = 0;
772         else
773                 cg->emit_decls = 1;
774
775         cur_reg_set = cg->reg_set;
776
777         ia32_irn_ops.cg = cg;
778
779         return (arch_code_generator_t *)cg;
780 }
781
782
783
784 /*****************************************************************
785  *  ____             _                  _   _____  _____
786  * |  _ \           | |                | | |_   _|/ ____|  /\
787  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
788  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
789  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
790  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
791  *
792  *****************************************************************/
793
794 static ia32_isa_t ia32_isa_template = {
795         &ia32_isa_if,            /* isa interface implementation */
796         &ia32_gp_regs[REG_ESP],  /* stack pointer register */
797         &ia32_gp_regs[REG_EBP],  /* base pointer register */
798         -1,                      /* stack direction */
799         0,                       /* number of code generator objects so far */
800         NULL,                    /* 16bit register names */
801         NULL,                    /* 8bit register names */
802         fp_sse2,                 /* use SSE2 unit for fp operations */
803 #ifndef NDEBUG
804         NULL,                    /* name obstack */
805         0                        /* name obst size */
806 #endif
807 };
808
809 /**
810  * Initializes the backend ISA.
811  */
812 static void *ia32_init(void) {
813         static int inited = 0;
814         ia32_isa_t *isa;
815
816         if(inited)
817                 return NULL;
818
819         isa = xcalloc(1, sizeof(*isa));
820         memcpy(isa, &ia32_isa_template, sizeof(*isa));
821
822         ia32_register_init(isa);
823         ia32_create_opcodes();
824         ia32_register_copy_attr_func();
825
826         isa->regs_16bit = pmap_create();
827         isa->regs_8bit  = pmap_create();
828 //      isa->fp_kind    = fp_x87;
829
830         ia32_build_16bit_reg_map(isa->regs_16bit);
831         ia32_build_8bit_reg_map(isa->regs_8bit);
832
833 #ifndef NDEBUG
834         isa->name_obst = xcalloc(1, sizeof(*(isa->name_obst)));
835         obstack_init(isa->name_obst);
836         isa->name_obst_size = 0;
837 #endif /* NDEBUG */
838
839         inited = 1;
840
841         return isa;
842 }
843
844
845
846 /**
847  * Closes the output file and frees the ISA structure.
848  */
849 static void ia32_done(void *self) {
850         ia32_isa_t *isa = self;
851
852         pmap_destroy(isa->regs_16bit);
853         pmap_destroy(isa->regs_8bit);
854
855 #ifndef NDEBUG
856         //printf("name obst size = %d bytes\n", isa->name_obst_size);
857         obstack_free(isa->name_obst, NULL);
858 #endif /* NDEBUG */
859
860         free(self);
861 }
862
863
864 /**
865  * Return the number of register classes for this architecture.
866  * We report always these:
867  *  - the general purpose registers
868  *  - the floating point register set (depending on the unit used for FP)
869  *  - MMX/SE registers (currently not supported)
870  */
871 static int ia32_get_n_reg_class(const void *self) {
872         return 2;
873 }
874
875 /**
876  * Return the register class for index i.
877  */
878 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
879         const ia32_isa_t *isa = self;
880         assert(i >= 0 && i < 2 && "Invalid ia32 register class requested.");
881         if (i == 0)
882                 return &ia32_reg_classes[CLASS_ia32_gp];
883         return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
884 }
885
886 /**
887  * Get the register class which shall be used to store a value of a given mode.
888  * @param self The this pointer.
889  * @param mode The mode in question.
890  * @return A register class which can hold values of the given mode.
891  */
892 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
893         const ia32_isa_t *isa = self;
894         if (mode_is_float(mode)) {
895                 return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
896         }
897         else
898                 return &ia32_reg_classes[CLASS_ia32_gp];
899 }
900
901 /**
902  * Get the ABI restrictions for procedure calls.
903  * @param self        The this pointer.
904  * @param method_type The type of the method (procedure) in question.
905  * @param abi         The abi object to be modified
906  */
907 static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
908         const ia32_isa_t *isa = self;
909         ir_type  *tp;
910         ir_mode  *mode;
911         unsigned  cc        = get_method_calling_convention(method_type);
912         int       n         = get_method_n_params(method_type);
913         int       biggest_n = -1;
914         int       stack_idx = 0;
915         int       i, ignore_1, ignore_2;
916         ir_mode **modes;
917         const arch_register_t *reg;
918         be_abi_call_flags_t call_flags;
919
920         /* set abi flags for calls */
921         call_flags.bits.left_to_right         = 0;
922         call_flags.bits.store_args_sequential = 0;
923         call_flags.bits.try_omit_fp           = 1;
924         call_flags.bits.fp_free               = 0;
925         call_flags.bits.call_has_imm          = 1;
926
927         /* set stack parameter passing style */
928         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
929
930         /* collect the mode for each type */
931         modes = alloca(n * sizeof(modes[0]));
932
933         for (i = 0; i < n; i++) {
934                 tp       = get_method_param_type(method_type, i);
935                 modes[i] = get_type_mode(tp);
936         }
937
938         /* set register parameters  */
939         if (cc & cc_reg_param) {
940                 /* determine the number of parameters passed via registers */
941                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore_1, &ignore_2);
942
943                 /* loop over all parameters and set the register requirements */
944                 for (i = 0; i <= biggest_n; i++) {
945                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
946                         assert(reg && "kaputt");
947                         be_abi_call_param_reg(abi, i, reg);
948                 }
949
950                 stack_idx = i;
951         }
952
953
954         /* set stack parameters */
955         for (i = stack_idx; i < n; i++) {
956                 be_abi_call_param_stack(abi, i, 1, 0, 0);
957         }
958
959
960         /* set return registers */
961         n = get_method_n_ress(method_type);
962
963         assert(n <= 2 && "more than two results not supported");
964
965         /* In case of 64bit returns, we will have two 32bit values */
966         if (n == 2) {
967                 tp   = get_method_res_type(method_type, 0);
968                 mode = get_type_mode(tp);
969
970                 assert(!mode_is_float(mode) && "two FP results not supported");
971
972                 tp   = get_method_res_type(method_type, 1);
973                 mode = get_type_mode(tp);
974
975                 assert(!mode_is_float(mode) && "two FP results not supported");
976
977                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
978                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
979         }
980         else if (n == 1) {
981                 const arch_register_t *reg;
982
983                 tp   = get_method_res_type(method_type, 0);
984                 assert(is_atomic_type(tp));
985                 mode = get_type_mode(tp);
986
987                 reg = mode_is_float(mode) ?
988                         (USE_SSE2(isa) ? &ia32_xmm_regs[REG_XMM0] : &ia32_vfp_regs[REG_VF0]) :
989                         &ia32_gp_regs[REG_EAX];
990
991                 be_abi_call_res_reg(abi, 0, reg);
992         }
993 }
994
995
996 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
997         return &ia32_irn_ops;
998 }
999
1000 const arch_irn_handler_t ia32_irn_handler = {
1001         ia32_get_irn_ops
1002 };
1003
1004 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
1005         return &ia32_irn_handler;
1006 }
1007
1008 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1009         return is_ia32_irn(irn);
1010 }
1011
1012 /**
1013  * Initializes the code generator interface.
1014  */
1015 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
1016         return &ia32_code_gen_if;
1017 }
1018
1019 list_sched_selector_t ia32_sched_selector;
1020
1021 /**
1022  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
1023  */
1024 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
1025         memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1026         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
1027         return &ia32_sched_selector;
1028 }
1029
1030 #ifdef WITH_LIBCORE
1031 static void ia32_register_options(lc_opt_entry_t *ent)
1032 {
1033 }
1034 #endif /* WITH_LIBCORE */
1035
1036 const arch_isa_if_t ia32_isa_if = {
1037 #ifdef WITH_LIBCORE
1038         ia32_register_options,
1039 #endif
1040         ia32_init,
1041         ia32_done,
1042         ia32_get_n_reg_class,
1043         ia32_get_reg_class,
1044         ia32_get_reg_class_for_mode,
1045         ia32_get_call_abi,
1046         ia32_get_irn_handler,
1047         ia32_get_code_generator_if,
1048         ia32_get_list_sched_selector
1049 };