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