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