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