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