amd64: Added Load and FrameAddr transformation. And fixed some corruption bugs w...
[libfirm] / ir / be / amd64 / bearch_amd64.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    The main amd64 backend driver file.
23  * @version  $Id: bearch_amd64.c 26909 2010-01-05 15:56:54Z matze $
24  */
25 #include "config.h"
26
27 #include "pseudo_irg.h"
28 #include "irgwalk.h"
29 #include "irprog.h"
30 #include "irprintf.h"
31 #include "ircons.h"
32 #include "irgmod.h"
33 #include "irdump.h"
34
35 #include "bitset.h"
36 #include "debug.h"
37
38 #include "be.h"
39 #include "../bearch.h"
40 #include "../benode.h"
41 #include "../belower.h"
42 #include "../besched.h"
43 #include "../beabi.h"
44 #include "../bemodule.h"
45 #include "../begnuas.h"
46 #include "../belistsched.h"
47 #include "../beflags.h"
48
49 #include "bearch_amd64_t.h"
50
51 #include "amd64_new_nodes.h"
52 #include "gen_amd64_regalloc_if.h"
53 #include "amd64_transform.h"
54 #include "amd64_emitter.h"
55
56 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
57
58 static arch_irn_class_t amd64_classify(const ir_node *irn)
59 {
60         (void) irn;
61         return 0;
62 }
63
64 static ir_entity *amd64_get_frame_entity(const ir_node *node)
65 {
66         if (is_amd64_FrameAddr(node)) {
67                 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
68                 return attr->entity;
69         }
70
71         (void) node;
72         /* TODO: return the ir_entity assigned to the frame */
73         return NULL;
74 }
75
76 static void amd64_set_frame_entity(ir_node *node, ir_entity *ent)
77 {
78         (void) node;
79         (void) ent;
80         /* TODO: set the ir_entity assigned to the frame */
81 }
82
83 /**
84  * This function is called by the generic backend to correct offsets for
85  * nodes accessing the stack.
86  */
87 static void amd64_set_frame_offset(ir_node *irn, int offset)
88 {
89         if (is_amd64_FrameAddr(irn)) {
90                 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
91                 attr->fp_offset += offset;
92         }
93 }
94
95 static int amd64_get_sp_bias(const ir_node *irn)
96 {
97         (void) irn;
98         return 0;
99 }
100
101 /* fill register allocator interface */
102
103 static const arch_irn_ops_t amd64_irn_ops = {
104         get_amd64_in_req,
105         amd64_classify,
106         amd64_get_frame_entity,
107         amd64_set_frame_entity,
108         amd64_set_frame_offset,
109         amd64_get_sp_bias,
110         NULL,    /* get_inverse             */
111         NULL,    /* get_op_estimated_cost   */
112         NULL,    /* possible_memory_operand */
113         NULL,    /* perform_memory_operand  */
114 };
115
116
117
118 /**
119  * Transforms the standard firm graph into
120  * a amd64 firm graph
121  */
122 static void amd64_prepare_graph(void *self)
123 {
124         amd64_code_gen_t *cg = self;
125
126         amd64_transform_graph (cg);
127
128         if (cg->dump)
129                 dump_ir_graph(cg->irg, "transformed");
130 }
131
132
133
134 /**
135  * Called immediatly before emit phase.
136  */
137 static void amd64_finish_irg(void *self)
138 {
139         amd64_code_gen_t *cg = self;
140         ir_graph         *irg = cg->irg;
141
142         dump_ir_graph(irg, "amd64-finished");
143 }
144
145 static void amd64_before_ra(void *self)
146 {
147         amd64_code_gen_t *cg = self;
148
149         be_sched_fix_flags(cg->birg, &amd64_reg_classes[CLASS_amd64_flags], 0);
150 }
151
152 static void amd64_after_ra(void *self)
153 {
154         (void) self;
155         /* Some stuff you need to do immediatly after register allocation */
156 }
157
158
159
160 /**
161  * Emits the code, closes the output file and frees
162  * the code generator interface.
163  */
164 static void amd64_emit_and_done(void *self)
165 {
166         amd64_code_gen_t *cg  = self;
167         ir_graph         *irg = cg->irg;
168
169         amd64_gen_routine(cg, irg);
170
171         /* de-allocate code generator */
172         free(cg);
173 }
174
175 static void *amd64_cg_init(be_irg_t *birg);
176
177 static const arch_code_generator_if_t amd64_code_gen_if = {
178         amd64_cg_init,
179         NULL,                    /* get_pic_base hook */
180         NULL,                    /* before abi introduce hook */
181         amd64_prepare_graph,
182         NULL,                    /* spill hook */
183         amd64_before_ra,      /* before register allocation hook */
184         amd64_after_ra,       /* after register allocation hook */
185         amd64_finish_irg,
186         amd64_emit_and_done
187 };
188
189 /**
190  * Initializes the code generator.
191  */
192 static void *amd64_cg_init(be_irg_t *birg)
193 {
194         const arch_env_t    *arch_env = be_get_birg_arch_env(birg);
195         amd64_isa_t      *isa      = (amd64_isa_t *) arch_env;
196         amd64_code_gen_t *cg       = XMALLOC(amd64_code_gen_t);
197
198         cg->impl     = &amd64_code_gen_if;
199         cg->irg      = be_get_birg_irg(birg);
200         cg->isa      = isa;
201         cg->birg     = birg;
202         cg->dump     = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
203
204         return (arch_code_generator_t *)cg;
205 }
206
207
208 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
209
210 /**
211  * Used to create per-graph unique pseudo nodes.
212  */
213 static inline ir_node *create_const(amd64_code_gen_t *cg, ir_node **place,
214                                     create_const_node_func func,
215                                     const arch_register_t* reg)
216 {
217         ir_node *block, *res;
218
219         if (*place != NULL)
220                 return *place;
221
222         block = get_irg_start_block(cg->irg);
223         res = func(NULL, block);
224         arch_set_irn_register(res, reg);
225         *place = res;
226
227         return res;
228 }
229
230 const arch_isa_if_t amd64_isa_if;
231 static amd64_isa_t amd64_isa_template = {
232         {
233                 &amd64_isa_if,             /* isa interface implementation */
234                 &amd64_gp_regs[REG_RSP],  /* stack pointer register */
235                 &amd64_gp_regs[REG_RBP],  /* base pointer register */
236                 &amd64_reg_classes[CLASS_amd64_gp],  /* link pointer register class */
237                 -1,                          /* stack direction */
238                 2,                           /* power of two stack alignment for calls, 2^2 == 4 */
239                 NULL,                        /* main environment */
240                 7,                           /* costs for a spill instruction */
241                 5,                           /* costs for a reload instruction */
242         },
243 };
244
245 /**
246  * Initializes the backend ISA
247  */
248 static arch_env_t *amd64_init(FILE *outfile)
249 {
250         static int run_once = 0;
251         amd64_isa_t *isa;
252
253         if(run_once)
254                 return NULL;
255         run_once = 1;
256
257         isa = XMALLOC(amd64_isa_t);
258         memcpy(isa, &amd64_isa_template, sizeof(*isa));
259
260         be_emit_init(outfile);
261
262         amd64_register_init();
263         amd64_create_opcodes(&amd64_irn_ops);
264
265         return &isa->arch_env;
266 }
267
268
269
270 /**
271  * Closes the output file and frees the ISA structure.
272  */
273 static void amd64_done(void *self)
274 {
275         amd64_isa_t *isa = self;
276
277         /* emit now all global declarations */
278         be_gas_emit_decls(isa->arch_env.main_env);
279
280         be_emit_exit();
281         free(self);
282 }
283
284
285 static unsigned amd64_get_n_reg_class(void)
286 {
287         return N_CLASSES;
288 }
289
290 static const arch_register_class_t *amd64_get_reg_class(unsigned i)
291 {
292         assert(i < N_CLASSES);
293         return &amd64_reg_classes[i];
294 }
295
296
297
298 /**
299  * Get the register class which shall be used to store a value of a given mode.
300  * @param self The this pointer.
301  * @param mode The mode in question.
302  * @return A register class which can hold values of the given mode.
303  */
304 static const arch_register_class_t *amd64_get_reg_class_for_mode(const ir_mode *mode)
305 {
306         assert(!mode_is_float(mode));
307         return &amd64_reg_classes[CLASS_amd64_gp];
308 }
309
310
311
312 typedef struct {
313         be_abi_call_flags_bits_t flags;
314         const arch_env_t *arch_env;
315         ir_graph *irg;
316 } amd64_abi_env_t;
317
318 static void *amd64_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
319 {
320         amd64_abi_env_t *env = XMALLOC(amd64_abi_env_t);
321         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
322         env->flags    = fl.bits;
323         env->irg      = irg;
324         env->arch_env = arch_env;
325         return env;
326 }
327
328 /**
329  * Get the between type for that call.
330  * @param self The callback object.
331  * @return The between type of for that call.
332  */
333 static ir_type *amd64_get_between_type(void *self)
334 {
335         static ir_type *between_type = NULL;
336         static ir_entity *old_bp_ent = NULL;
337         (void) self;
338
339         if(!between_type) {
340                 ir_entity *ret_addr_ent;
341                 ir_type *ret_addr_type = new_type_primitive(mode_P);
342                 ir_type *old_bp_type   = new_type_primitive(mode_P);
343
344                 between_type           = new_type_class(new_id_from_str("amd64_between_type"));
345                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
346                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
347
348                 set_entity_offset(old_bp_ent, 0);
349                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
350                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
351         }
352
353         return between_type;
354 }
355
356 /**
357  * Build the prolog, return the BASE POINTER register
358  */
359 static const arch_register_t *amd64_abi_prologue(void *self, ir_node **mem,
360                                                     pmap *reg_map, int *stack_bias)
361 {
362         amd64_abi_env_t  *env  = self;
363         const arch_env_t *aenv = env->arch_env;
364         (void) mem;
365         (void) stack_bias;
366         (void) aenv;
367         (void) reg_map;
368
369         if (!env->flags.try_omit_fp) {
370                 /* FIXME: maybe later here should be some code to generate
371                  * the usual abi prologue */
372                 return env->arch_env->bp;
373         }
374
375         return env->arch_env->sp;
376 }
377
378 /* Build the epilog */
379 static void amd64_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
380                                pmap *reg_map)
381 {
382         amd64_abi_env_t  *env  = self;
383         const arch_env_t *aenv = env->arch_env;
384         ir_node          *curr_sp  = be_abi_reg_map_get(reg_map, aenv->sp);
385         ir_node          *curr_bp  = be_abi_reg_map_get(reg_map, aenv->bp);
386         (void) bl;
387         (void) mem;
388
389         if (env->flags.try_omit_fp) {
390                 curr_sp = be_new_IncSP(aenv->sp, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
391         }
392
393         be_abi_reg_map_set(reg_map, aenv->sp, curr_sp);
394         be_abi_reg_map_set(reg_map, aenv->bp, curr_bp);
395 }
396
397 static const be_abi_callbacks_t amd64_abi_callbacks = {
398         amd64_abi_init,
399         free,
400         amd64_get_between_type,
401         amd64_abi_prologue,
402         amd64_abi_epilogue,
403 };
404
405 /**
406  * Get the ABI restrictions for procedure calls.
407  * @param self        The this pointer.
408  * @param method_type The type of the method (procedure) in question.
409  * @param abi         The abi object to be modified
410  */
411 static void amd64_get_call_abi(const void *self, ir_type *method_type,
412                            be_abi_call_t *abi)
413 {
414         ir_type  *tp;
415         ir_mode  *mode;
416         int       i, n = get_method_n_params(method_type);
417         be_abi_call_flags_t call_flags;
418         int no_reg = 0;
419
420         (void) self;
421
422         /* set abi flags for calls */
423         call_flags.bits.left_to_right         = 0;
424         call_flags.bits.store_args_sequential = 1;
425         call_flags.bits.try_omit_fp           = 1;
426         call_flags.bits.fp_free               = 0;
427         call_flags.bits.call_has_imm          = 1;
428
429         /* set stack parameter passing style */
430         be_abi_call_set_flags(abi, call_flags, &amd64_abi_callbacks);
431
432         for (i = 0; i < n; i++) {
433                 tp   = get_method_param_type(method_type, i);
434                 mode = get_type_mode(tp);
435                 //d// printf ("MODE %p %p XX %d\n", mode, mode_Iu, i);
436
437                 if (!no_reg && (i == 0 || i == 1) && mode == mode_Iu) {
438                         //d// printf("TEST%d\n", i);
439                         be_abi_call_param_reg(abi, i,
440                                               i == 0 ? &amd64_gp_regs[REG_RDI]
441                                                      : &amd64_gp_regs[REG_RSI],
442                                               ABI_CONTEXT_BOTH);
443                 /* default: all parameters on stack */
444                 } else {
445                         no_reg = 1;
446                         be_abi_call_param_stack(abi, i, mode, 4, 0, 0, ABI_CONTEXT_BOTH);
447                 }
448         }
449
450         /* TODO: set correct return register */
451         /* default: return value is in R0 resp. F0 */
452         if (get_method_n_ress(method_type) > 0) {
453                 tp   = get_method_res_type(method_type, 0);
454                 mode = get_type_mode(tp);
455
456                 /* FIXME: No floating point yet */
457                 /* be_abi_call_res_reg(abi, 0,
458                         mode_is_float(mode) ? &amd64_fp_regs[REG_F0] : &amd64_gp_regs[REG_R0], ABI_CONTEXT_BOTH) */;
459
460                 be_abi_call_res_reg(abi, 0,
461                         &amd64_gp_regs[REG_RAX], ABI_CONTEXT_BOTH);
462         }
463 }
464
465 static int amd64_to_appear_in_schedule(void *block_env, const ir_node *irn)
466 {
467         (void) block_env;
468
469         if(!is_amd64_irn(irn))
470                 return -1;
471
472         return 1;
473 }
474
475 /**
476  * Initializes the code generator interface.
477  */
478 static const arch_code_generator_if_t *amd64_get_code_generator_if(
479                 void *self)
480 {
481         (void) self;
482         return &amd64_code_gen_if;
483 }
484
485 list_sched_selector_t amd64_sched_selector;
486
487 /**
488  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
489  */
490 static const list_sched_selector_t *amd64_get_list_sched_selector(
491                 const void *self, list_sched_selector_t *selector)
492 {
493         (void) self;
494         (void) selector;
495
496         amd64_sched_selector = trivial_selector;
497         amd64_sched_selector.to_appear_in_schedule = amd64_to_appear_in_schedule;
498         return &amd64_sched_selector;
499 }
500
501 static const ilp_sched_selector_t *amd64_get_ilp_sched_selector(
502                 const void *self)
503 {
504         (void) self;
505         return NULL;
506 }
507
508 /**
509  * Returns the necessary byte alignment for storing a register of given class.
510  */
511 static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
512 {
513         ir_mode *mode = arch_register_class_mode(cls);
514         return get_mode_size_bytes(mode);
515 }
516
517 /**
518  * Returns the libFirm configuration parameter for this backend.
519  */
520 static const backend_params *amd64_get_backend_params(void) {
521         static backend_params p = {
522                 0,     /* no dword lowering */
523                 0,     /* no inline assembly */
524                 NULL,  /* will be set later */
525                 NULL,  /* no creator function */
526                 NULL,  /* context for create_intrinsic_fkt */
527                 NULL,  /* parameter for if conversion */
528                 NULL,  /* float arithmetic mode */
529                 0,     /* no trampoline support: size 0 */
530                 0,     /* no trampoline support: align 0 */
531                 NULL,  /* no trampoline support: no trampoline builder */
532                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
533         };
534         return &p;
535 }
536
537 static const be_execution_unit_t ***amd64_get_allowed_execution_units(
538                 const ir_node *irn)
539 {
540         (void) irn;
541         /* TODO */
542         assert(0);
543         return NULL;
544 }
545
546 static const be_machine_t *amd64_get_machine(const void *self)
547 {
548         (void) self;
549         /* TODO */
550         assert(0);
551         return NULL;
552 }
553
554 static ir_graph **amd64_get_backend_irg_list(const void *self,
555                                                 ir_graph ***irgs)
556 {
557         (void) self;
558         (void) irgs;
559         return NULL;
560 }
561
562 static asm_constraint_flags_t amd64_parse_asm_constraint(const char **c)
563 {
564         (void) c;
565         return ASM_CONSTRAINT_FLAG_INVALID;
566 }
567
568 static int amd64_is_valid_clobber(const char *clobber)
569 {
570         (void) clobber;
571         return 0;
572 }
573
574 const arch_isa_if_t amd64_isa_if = {
575         amd64_init,
576         amd64_done,
577         NULL,                /* handle intrinsics */
578         amd64_get_n_reg_class,
579         amd64_get_reg_class,
580         amd64_get_reg_class_for_mode,
581         amd64_get_call_abi,
582         amd64_get_code_generator_if,
583         amd64_get_list_sched_selector,
584         amd64_get_ilp_sched_selector,
585         amd64_get_reg_class_alignment,
586     amd64_get_backend_params,
587         amd64_get_allowed_execution_units,
588         amd64_get_machine,
589         amd64_get_backend_irg_list,
590         NULL,                    /* mark remat */
591         amd64_parse_asm_constraint,
592         amd64_is_valid_clobber
593 };
594
595 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_amd64);
596 void be_init_arch_amd64(void)
597 {
598         be_register_isa_if("amd64", &amd64_isa_if);
599         FIRM_DBG_REGISTER(dbg, "firm.be.amd64.cg");
600         amd64_init_transform();
601 }