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