rename be_pos_XXX to n_be_XXX to be consistent with the architecture specific constants
[libfirm] / ir / be / arm / bearch_arm.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 arm backend driver file.
23  * @author  Matthias Braun, Oliver Richter, Tobias Gneist
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "lc_opts.h"
29 #include "lc_opts_enum.h"
30
31 #include "irgwalk.h"
32 #include "irprog.h"
33 #include "irprintf.h"
34 #include "ircons.h"
35 #include "irgmod.h"
36 #include "irgopt.h"
37 #include "iroptimize.h"
38 #include "irdump.h"
39 #include "lowering.h"
40 #include "error.h"
41
42 #include "bitset.h"
43 #include "debug.h"
44 #include "array_t.h"
45 #include "irtools.h"
46
47 #include "../bearch.h"
48 #include "../benode.h"
49 #include "../belower.h"
50 #include "../besched.h"
51 #include "be.h"
52 #include "../bemachine.h"
53 #include "../bemodule.h"
54 #include "../beirg.h"
55 #include "../bespillslots.h"
56 #include "../begnuas.h"
57 #include "../belistsched.h"
58 #include "../beflags.h"
59
60 #include "bearch_arm_t.h"
61
62 #include "arm_new_nodes.h"
63 #include "gen_arm_regalloc_if.h"
64 #include "arm_transform.h"
65 #include "arm_optimize.h"
66 #include "arm_emitter.h"
67 #include "arm_map_regs.h"
68
69 static arch_irn_class_t arm_classify(const ir_node *irn)
70 {
71         (void) irn;
72         /* TODO: we should mark reload/spill instructions and classify them here */
73         return arch_irn_class_none;
74 }
75
76 static ir_entity *arm_get_frame_entity(const ir_node *irn)
77 {
78         const arm_attr_t *attr = get_arm_attr_const(irn);
79
80         if (is_arm_FrameAddr(irn)) {
81                 const arm_SymConst_attr_t *attr = get_arm_SymConst_attr_const(irn);
82                 return attr->entity;
83         }
84         if (attr->is_load_store) {
85                 const arm_load_store_attr_t *load_store_attr
86                         = get_arm_load_store_attr_const(irn);
87                 if (load_store_attr->is_frame_entity) {
88                         return load_store_attr->entity;
89                 }
90         }
91         return NULL;
92 }
93
94 /**
95  * This function is called by the generic backend to correct offsets for
96  * nodes accessing the stack.
97  */
98 static void arm_set_stack_bias(ir_node *irn, int bias)
99 {
100         if (is_arm_FrameAddr(irn)) {
101                 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(irn);
102                 attr->fp_offset += bias;
103         } else {
104                 arm_load_store_attr_t *attr = get_arm_load_store_attr(irn);
105                 assert(attr->base.is_load_store);
106                 attr->offset += bias;
107         }
108 }
109
110 static int arm_get_sp_bias(const ir_node *irn)
111 {
112         /* We don't have any nodes changing the stack pointer.
113            We probably want to support post-/pre increment/decrement later */
114         (void) irn;
115         return 0;
116 }
117
118 /* fill register allocator interface */
119
120 static const arch_irn_ops_t arm_irn_ops = {
121         arm_classify,
122         arm_get_frame_entity,
123         arm_set_stack_bias,
124         arm_get_sp_bias,
125         NULL,    /* get_inverse             */
126         NULL,    /* get_op_estimated_cost   */
127         NULL,    /* possible_memory_operand */
128         NULL,    /* perform_memory_operand  */
129 };
130
131 /**
132  * Transforms the standard Firm graph into
133  * a ARM firm graph.
134  */
135 static void arm_prepare_graph(ir_graph *irg)
136 {
137         /* transform nodes into assembler instructions */
138         arm_transform_graph(irg);
139
140         /* do local optimizations (mainly CSE) */
141         local_optimize_graph(irg);
142
143         /* do code placement, to optimize the position of constants */
144         place_code(irg);
145 }
146
147 /**
148  * Called immediately before emit phase.
149  */
150 static void arm_finish_irg(ir_graph *irg)
151 {
152         /* do peephole optimizations and fix stack offsets */
153         arm_peephole_optimization(irg);
154 }
155
156 static void arm_before_ra(ir_graph *irg)
157 {
158         be_sched_fix_flags(irg, &arm_reg_classes[CLASS_arm_flags], NULL, NULL);
159 }
160
161 static void transform_Reload(ir_node *node)
162 {
163         ir_node   *block  = get_nodes_block(node);
164         dbg_info  *dbgi   = get_irn_dbg_info(node);
165         ir_node   *ptr    = get_irn_n(node, n_be_Reload_frame);
166         ir_node   *mem    = get_irn_n(node, n_be_Reload_mem);
167         ir_mode   *mode   = get_irn_mode(node);
168         ir_entity *entity = be_get_frame_entity(node);
169         const arch_register_t *reg;
170         ir_node   *proj;
171         ir_node   *load;
172
173         ir_node  *sched_point = sched_prev(node);
174
175         load = new_bd_arm_Ldr(dbgi, block, ptr, mem, mode, entity, false, 0, true);
176         sched_add_after(sched_point, load);
177         sched_remove(node);
178
179         proj = new_rd_Proj(dbgi, load, mode, pn_arm_Ldr_res);
180
181         reg = arch_get_irn_register(node);
182         arch_set_irn_register(proj, reg);
183
184         exchange(node, proj);
185 }
186
187 static void transform_Spill(ir_node *node)
188 {
189         ir_node   *block  = get_nodes_block(node);
190         dbg_info  *dbgi   = get_irn_dbg_info(node);
191         ir_node   *ptr    = get_irn_n(node, n_be_Spill_frame);
192         ir_graph  *irg    = get_irn_irg(node);
193         ir_node   *mem    = new_r_NoMem(irg);
194         ir_node   *val    = get_irn_n(node, n_be_Spill_val);
195         ir_mode   *mode   = get_irn_mode(val);
196         ir_entity *entity = be_get_frame_entity(node);
197         ir_node   *sched_point;
198         ir_node   *store;
199
200         sched_point = sched_prev(node);
201         store = new_bd_arm_Str(dbgi, block, ptr, val, mem, mode, entity, false, 0,
202                                true);
203
204         sched_remove(node);
205         sched_add_after(sched_point, store);
206
207         exchange(node, store);
208 }
209
210 static void arm_after_ra_walker(ir_node *block, void *data)
211 {
212         ir_node *node, *prev;
213         (void) data;
214
215         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
216                 prev = sched_prev(node);
217
218                 if (be_is_Reload(node)) {
219                         transform_Reload(node);
220                 } else if (be_is_Spill(node)) {
221                         transform_Spill(node);
222                 }
223         }
224 }
225
226 static void arm_collect_frame_entity_nodes(ir_node *node, void *data)
227 {
228         be_fec_env_t  *env = (be_fec_env_t*)data;
229         const ir_mode *mode;
230         int            align;
231         ir_entity     *entity;
232         const arm_load_store_attr_t *attr;
233
234         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
235                 mode  = get_irn_mode(node);
236                 align = get_mode_size_bytes(mode);
237                 be_node_needs_frame_entity(env, node, mode, align);
238                 return;
239         }
240
241         switch (get_arm_irn_opcode(node)) {
242         case iro_arm_Ldf:
243         case iro_arm_Ldr:
244                 break;
245         default:
246                 return;
247         }
248
249         attr   = get_arm_load_store_attr_const(node);
250         entity = attr->entity;
251         mode   = attr->load_store_mode;
252         align  = get_mode_size_bytes(mode);
253         if (entity != NULL)
254                 return;
255         if (!attr->is_frame_entity)
256                 return;
257         be_node_needs_frame_entity(env, node, mode, align);
258 }
259
260 static void arm_set_frame_entity(ir_node *node, ir_entity *entity)
261 {
262         if (is_be_node(node)) {
263                 be_node_set_frame_entity(node, entity);
264         } else {
265                 arm_load_store_attr_t *attr = get_arm_load_store_attr(node);
266                 attr->entity = entity;
267         }
268 }
269
270 static void arm_after_ra(ir_graph *irg)
271 {
272         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
273         bool               at_begin     = stack_layout->sp_relative ? true : false;
274         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
275
276         irg_walk_graph(irg, NULL, arm_collect_frame_entity_nodes, fec_env);
277         be_assign_entities(fec_env, arm_set_frame_entity, at_begin);
278         be_free_frame_entity_coalescer(fec_env);
279
280         irg_block_walk_graph(irg, NULL, arm_after_ra_walker, NULL);
281 }
282
283 /**
284  * Initializes the code generator.
285  */
286 static void arm_init_graph(ir_graph *irg)
287 {
288         (void) irg;
289 }
290
291
292 /**
293  * Maps all intrinsic calls that the backend support
294  * and map all instructions the backend did not support
295  * to runtime calls.
296  */
297 static void arm_handle_intrinsics(void)
298 {
299         ir_type *tp, *int_tp, *uint_tp;
300         i_record records[8];
301         int n_records = 0;
302
303         runtime_rt rt_iDiv, rt_uDiv, rt_iMod, rt_uMod;
304
305 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
306
307         int_tp  = get_type_for_mode(mode_Is);
308         uint_tp = get_type_for_mode(mode_Iu);
309
310         /* ARM has neither a signed div instruction ... */
311         {
312                 i_instr_record *map_Div = &records[n_records++].i_instr;
313
314                 tp = new_type_method(2, 1);
315                 set_method_param_type(tp, 0, int_tp);
316                 set_method_param_type(tp, 1, int_tp);
317                 set_method_res_type(tp, 0, int_tp);
318
319                 rt_iDiv.ent             = new_entity(get_glob_type(), ID("__divsi3"), tp);
320                 set_entity_ld_ident(rt_iDiv.ent, ID("__divsi3"));
321                 rt_iDiv.mode            = mode_T;
322                 rt_iDiv.res_mode        = mode_Is;
323                 rt_iDiv.mem_proj_nr     = pn_Div_M;
324                 rt_iDiv.regular_proj_nr = pn_Div_X_regular;
325                 rt_iDiv.exc_proj_nr     = pn_Div_X_except;
326                 rt_iDiv.exc_mem_proj_nr = pn_Div_M;
327                 rt_iDiv.res_proj_nr     = pn_Div_res;
328
329                 add_entity_linkage(rt_iDiv.ent, IR_LINKAGE_CONSTANT);
330                 set_entity_visibility(rt_iDiv.ent, ir_visibility_external);
331
332                 map_Div->kind     = INTRINSIC_INSTR;
333                 map_Div->op       = op_Div;
334                 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
335                 map_Div->ctx      = &rt_iDiv;
336         }
337         /* ... nor an unsigned div instruction ... */
338         {
339                 i_instr_record *map_Div = &records[n_records++].i_instr;
340
341                 tp = new_type_method(2, 1);
342                 set_method_param_type(tp, 0, uint_tp);
343                 set_method_param_type(tp, 1, uint_tp);
344                 set_method_res_type(tp, 0, uint_tp);
345
346                 rt_uDiv.ent             = new_entity(get_glob_type(), ID("__udivsi3"), tp);
347                 set_entity_ld_ident(rt_uDiv.ent, ID("__udivsi3"));
348                 rt_uDiv.mode            = mode_T;
349                 rt_uDiv.res_mode        = mode_Iu;
350                 rt_uDiv.mem_proj_nr     = pn_Div_M;
351                 rt_uDiv.regular_proj_nr = pn_Div_X_regular;
352                 rt_uDiv.exc_proj_nr     = pn_Div_X_except;
353                 rt_uDiv.exc_mem_proj_nr = pn_Div_M;
354                 rt_uDiv.res_proj_nr     = pn_Div_res;
355
356                 set_entity_visibility(rt_uDiv.ent, ir_visibility_external);
357
358                 map_Div->kind     = INTRINSIC_INSTR;
359                 map_Div->op       = op_Div;
360                 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
361                 map_Div->ctx      = &rt_uDiv;
362         }
363         /* ... nor a signed mod instruction ... */
364         {
365                 i_instr_record *map_Mod = &records[n_records++].i_instr;
366
367                 tp = new_type_method(2, 1);
368                 set_method_param_type(tp, 0, int_tp);
369                 set_method_param_type(tp, 1, int_tp);
370                 set_method_res_type(tp, 0, int_tp);
371
372                 rt_iMod.ent             = new_entity(get_glob_type(), ID("__modsi3"), tp);
373                 set_entity_ld_ident(rt_iMod.ent, ID("__modsi3"));
374                 rt_iMod.mode            = mode_T;
375                 rt_iMod.res_mode        = mode_Is;
376                 rt_iMod.mem_proj_nr     = pn_Mod_M;
377                 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
378                 rt_iMod.exc_proj_nr     = pn_Mod_X_except;
379                 rt_iMod.exc_mem_proj_nr = pn_Mod_M;
380                 rt_iMod.res_proj_nr     = pn_Mod_res;
381
382                 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
383
384                 map_Mod->kind     = INTRINSIC_INSTR;
385                 map_Mod->op       = op_Mod;
386                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
387                 map_Mod->ctx      = &rt_iMod;
388         }
389         /* ... nor an unsigned mod. */
390         {
391                 i_instr_record *map_Mod = &records[n_records++].i_instr;
392
393                 tp = new_type_method(2, 1);
394                 set_method_param_type(tp, 0, uint_tp);
395                 set_method_param_type(tp, 1, uint_tp);
396                 set_method_res_type(tp, 0, uint_tp);
397
398                 rt_uMod.ent             = new_entity(get_glob_type(), ID("__umodsi3"), tp);
399                 set_entity_ld_ident(rt_uMod.ent, ID("__umodsi3"));
400                 rt_uMod.mode            = mode_T;
401                 rt_uMod.res_mode        = mode_Iu;
402                 rt_uMod.mem_proj_nr     = pn_Mod_M;
403                 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
404                 rt_uMod.exc_proj_nr     = pn_Mod_X_except;
405                 rt_uMod.exc_mem_proj_nr = pn_Mod_M;
406                 rt_uMod.res_proj_nr     = pn_Mod_res;
407
408                 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
409
410                 map_Mod->kind     = INTRINSIC_INSTR;
411                 map_Mod->op       = op_Mod;
412                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
413                 map_Mod->ctx      = &rt_uMod;
414         }
415
416         if (n_records > 0)
417                 lower_intrinsics(records, n_records, /*part_block_used=*/0);
418 }
419
420 extern const arch_isa_if_t arm_isa_if;
421 static arm_isa_t arm_isa_template = {
422         {
423                 &arm_isa_if,           /* isa interface */
424                 N_ARM_REGISTERS,
425                 arm_registers,
426                 N_ARM_CLASSES,
427                 arm_reg_classes,
428                 &arm_registers[REG_SP],  /* stack pointer */
429                 &arm_registers[REG_R11], /* base pointer */
430                 &arm_reg_classes[CLASS_arm_gp],  /* static link pointer class */
431                 -1,                    /* stack direction */
432                 2,                     /* power of two stack alignment for calls, 2^2 == 4 */
433                 NULL,                  /* main environment */
434                 7,                     /* spill costs */
435                 5,                     /* reload costs */
436                 true,                  /* we do have custom abi handling */
437         },
438         ARM_FPU_ARCH_FPE,      /* FPU architecture */
439 };
440
441 /**
442  * Initializes the backend ISA and opens the output file.
443  */
444 static arch_env_t *arm_init(FILE *file_handle)
445 {
446         arm_isa_t *isa = XMALLOC(arm_isa_t);
447         memcpy(isa, &arm_isa_template, sizeof(*isa));
448
449         arm_register_init();
450
451         be_emit_init(file_handle);
452
453         arm_create_opcodes(&arm_irn_ops);
454         arm_handle_intrinsics();
455
456         be_gas_emit_types = false;
457
458         return &isa->base;
459 }
460
461
462
463 /**
464  * Closes the output file and frees the ISA structure.
465  */
466 static void arm_done(void *self)
467 {
468         arm_isa_t *isa = (arm_isa_t*)self;
469
470         be_gas_emit_decls(isa->base.main_env);
471
472         be_emit_exit();
473         free(self);
474 }
475
476 /**
477  * Get the register class which shall be used to store a value of a given mode.
478  * @param self The this pointer.
479  * @param mode The mode in question.
480  * @return A register class which can hold values of the given mode.
481  */
482 static const arch_register_class_t *arm_get_reg_class_for_mode(const ir_mode *mode)
483 {
484         if (mode_is_float(mode))
485                 return &arm_reg_classes[CLASS_arm_fpa];
486         else
487                 return &arm_reg_classes[CLASS_arm_gp];
488 }
489
490 /**
491  * Returns the necessary byte alignment for storing a register of given class.
492  */
493 static int arm_get_reg_class_alignment(const arch_register_class_t *cls)
494 {
495         (void) cls;
496         /* ARM is a 32 bit CPU, no need for other alignment */
497         return 4;
498 }
499
500 /**
501  * Return irp irgs in the desired order.
502  */
503 static ir_graph **arm_get_irg_list(const void *self, ir_graph ***irg_list)
504 {
505         (void) self;
506         (void) irg_list;
507         return NULL;
508 }
509
510 /**
511  * Allows or disallows the creation of Psi nodes for the given Phi nodes.
512  * @return 1 if allowed, 0 otherwise
513  */
514 static int arm_is_mux_allowed(ir_node *sel, ir_node *mux_false,
515                               ir_node *mux_true)
516 {
517         (void) sel;
518         (void) mux_false;
519         (void) mux_true;
520         return false;
521 }
522
523 static asm_constraint_flags_t arm_parse_asm_constraint(const char **c)
524 {
525         /* asm not supported */
526         (void) c;
527         return ASM_CONSTRAINT_FLAG_INVALID;
528 }
529
530 static int arm_is_valid_clobber(const char *clobber)
531 {
532         (void) clobber;
533         return 0;
534 }
535
536 static void arm_lower_for_target(void)
537 {
538         size_t i, n_irgs = get_irp_n_irgs();
539
540         lower_params_t params = {
541                 4,                                     /* def_ptr_alignment */
542                 LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
543                 ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
544                 NULL,                                  /* find pointer type */
545                 NULL,                                  /* ret_compound_in_regs */
546         };
547
548         /* lower compound param handling */
549         lower_calls_with_compounds(&params);
550
551         for (i = 0; i < n_irgs; ++i) {
552                 ir_graph *irg = get_irp_irg(i);
553                 lower_switch(irg, 256, true);
554         }
555 }
556
557 /**
558  * Returns the libFirm configuration parameter for this backend.
559  */
560 static const backend_params *arm_get_libfirm_params(void)
561 {
562         static ir_settings_arch_dep_t ad = {
563                 1,    /* allow subs */
564                 1,    /* Muls are fast enough on ARM but ... */
565                 31,   /* ... one shift would be possible better */
566                 NULL, /* no evaluator function */
567                 0,    /* SMUL is needed, only in Arch M */
568                 0,    /* UMUL is needed, only in Arch M */
569                 32,   /* SMUL & UMUL available for 32 bit */
570         };
571         static backend_params p = {
572                 0,     /* don't support inline assembler yet */
573                 1,     /* support Rotl nodes */
574                 1,     /* big endian */
575                 &ad,   /* will be set later */
576                 arm_is_mux_allowed, /* allow_ifconv function */
577                 NULL,  /* float arithmetic mode (TODO) */
578                 0,     /* no trampoline support: size 0 */
579                 0,     /* no trampoline support: align 0 */
580                 NULL,  /* no trampoline support: no trampoline builder */
581                 4      /* alignment of stack parameter */
582         };
583
584         return &p;
585 }
586
587 /* fpu set architectures. */
588 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
589         { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
590         { "fpe",       ARM_FPU_ARCH_FPE },
591         { "fpa",       ARM_FPU_ARCH_FPA },
592         { "vfp1xd",    ARM_FPU_ARCH_VFP_V1xD },
593         { "vfp1",      ARM_FPU_ARCH_VFP_V1 },
594         { "vfp2",      ARM_FPU_ARCH_VFP_V2 },
595         { NULL,        0 }
596 };
597
598 static lc_opt_enum_int_var_t arch_fpu_var = {
599         &arm_isa_template.fpu_arch, arm_fpu_items
600 };
601
602 static const lc_opt_table_entry_t arm_options[] = {
603         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &arch_fpu_var),
604         LC_OPT_LAST
605 };
606
607 const arch_isa_if_t arm_isa_if = {
608         arm_init,
609         arm_lower_for_target,
610         arm_done,
611         NULL,  /* handle_intrinsics */
612         arm_get_reg_class_for_mode,
613         NULL,
614         arm_get_reg_class_alignment,
615         arm_get_libfirm_params,
616         arm_get_irg_list,
617         NULL,               /* mark remat */
618         arm_parse_asm_constraint,
619         arm_is_valid_clobber,
620
621         arm_init_graph,
622         NULL,  /* get_pic_base */
623         NULL,  /* before_abi */
624         arm_prepare_graph,
625         arm_before_ra,
626         arm_after_ra,
627         arm_finish_irg,
628         arm_gen_routine,
629 };
630
631 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm);
632 void be_init_arch_arm(void)
633 {
634         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
635         lc_opt_entry_t *arm_grp = lc_opt_get_grp(be_grp, "arm");
636
637         lc_opt_add_table(arm_grp, arm_options);
638
639         be_register_isa_if("arm", &arm_isa_if);
640
641         arm_init_transform();
642         arm_init_emitter();
643 }