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