backend is responsible for defining what lowering operations need to be performed...
[libfirm] / ir / be / sparc / bearch_sparc.c
1 /*
2  * Copyright (C) 1995-2010 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 sparc backend driver file.
23  * @version  $Id$
24  */
25 #include "config.h"
26
27 #include "lc_opts.h"
28 #include "lc_opts_enum.h"
29
30 #include "irgwalk.h"
31 #include "irprog.h"
32 #include "irprintf.h"
33 #include "ircons.h"
34 #include "irgmod.h"
35 #include "irgopt.h"
36 #include "iroptimize.h"
37 #include "irtools.h"
38 #include "irdump.h"
39 #include "lowering.h"
40
41 #include "bitset.h"
42 #include "debug.h"
43 #include "array_t.h"
44 #include "error.h"
45
46 #include "../bearch.h"
47 #include "../benode.h"
48 #include "../belower.h"
49 #include "../besched.h"
50 #include "be.h"
51 #include "../bemachine.h"
52 #include "../beilpsched.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_sparc_t.h"
61
62 #include "sparc_new_nodes.h"
63 #include "gen_sparc_regalloc_if.h"
64 #include "sparc_transform.h"
65 #include "sparc_emitter.h"
66
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
68
69 static arch_irn_class_t sparc_classify(const ir_node *irn)
70 {
71         (void) irn;
72         return 0;
73 }
74
75 static ir_entity *sparc_get_frame_entity(const ir_node *irn)
76 {
77         const sparc_attr_t *attr = get_sparc_attr_const(irn);
78
79         if (is_sparc_FrameAddr(irn)) {
80                 const sparc_symconst_attr_t *attr = get_irn_generic_attr_const(irn);
81                 return attr->entity;
82         }
83
84         if (attr->is_load_store) {
85                 const sparc_load_store_attr_t *load_store_attr = get_sparc_load_store_attr_const(irn);
86                 if (load_store_attr->is_frame_entity) {
87                         return load_store_attr->entity;
88                 }
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 sparc_set_frame_offset(ir_node *irn, int offset)
99 {
100         if (is_sparc_FrameAddr(irn)) {
101                 sparc_symconst_attr_t *attr = get_irn_generic_attr(irn);
102                 attr->fp_offset += offset;
103         } else {
104                 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(irn);
105                 assert(attr->base.is_load_store);
106                 attr->offset += offset;
107         }
108 }
109
110 static int sparc_get_sp_bias(const ir_node *node)
111 {
112         if (is_sparc_Save(node)) {
113                 const sparc_save_attr_t *attr = get_sparc_save_attr_const(node);
114                 /* Note we do not retport the change of the SPARC_MIN_STACKSIZE
115                  * size, since we have additional magic in the emitter which
116                  * calculates that! */
117                 assert(attr->initial_stacksize >= SPARC_MIN_STACKSIZE);
118                 return attr->initial_stacksize - SPARC_MIN_STACKSIZE;
119         }
120         return 0;
121 }
122
123 /* fill register allocator interface */
124
125 static const arch_irn_ops_t sparc_irn_ops = {
126         get_sparc_in_req,
127         sparc_classify,
128         sparc_get_frame_entity,
129         sparc_set_frame_offset,
130         sparc_get_sp_bias,
131         NULL,    /* get_inverse             */
132         NULL,    /* get_op_estimated_cost   */
133         NULL,    /* possible_memory_operand */
134         NULL,    /* perform_memory_operand  */
135 };
136
137
138
139 /**
140  * Transforms the standard firm graph into
141  * a SPARC firm graph
142  */
143 static void sparc_prepare_graph(void *self)
144 {
145         sparc_code_gen_t *cg = self;
146
147         /* transform FIRM into SPARC asm nodes */
148         sparc_transform_graph(cg);
149
150         if (cg->dump)
151                 dump_ir_graph(cg->irg, "transformed");
152 }
153
154 static bool sparc_modifies_flags(const ir_node *node)
155 {
156         return arch_irn_get_flags(node) & sparc_arch_irn_flag_modifies_flags;
157 }
158
159 static bool sparc_modifies_fp_flags(const ir_node *node)
160 {
161         return arch_irn_get_flags(node) & sparc_arch_irn_flag_modifies_fp_flags;
162 }
163
164 static void sparc_before_ra(void *self)
165 {
166         sparc_code_gen_t *cg = self;
167         /* fixup flags register */
168         be_sched_fix_flags(cg->irg, &sparc_reg_classes[CLASS_sparc_flags_class],
169                            NULL, sparc_modifies_flags);
170         be_sched_fix_flags(cg->irg, &sparc_reg_classes[CLASS_sparc_fpflags_class],
171                            NULL, sparc_modifies_fp_flags);
172 }
173
174 /**
175  * transform reload node => load
176  */
177 static void transform_Reload(ir_node *node)
178 {
179         ir_node   *block  = get_nodes_block(node);
180         dbg_info  *dbgi   = get_irn_dbg_info(node);
181         ir_node   *ptr    = get_irn_n(node, be_pos_Spill_frame);
182         ir_node   *mem    = get_irn_n(node, be_pos_Reload_mem);
183         ir_mode   *mode   = get_irn_mode(node);
184         ir_entity *entity = be_get_frame_entity(node);
185         const arch_register_t *reg;
186         ir_node   *proj;
187         ir_node   *load;
188
189         ir_node  *sched_point = sched_prev(node);
190
191         load = new_bd_sparc_Ld(dbgi, block, ptr, mem, mode, entity, false, 0, true);
192         sched_add_after(sched_point, load);
193         sched_remove(node);
194
195         proj = new_rd_Proj(dbgi, load, mode, pn_sparc_Ld_res);
196
197         reg = arch_get_irn_register(node);
198         arch_set_irn_register(proj, reg);
199
200         exchange(node, proj);
201 }
202
203 /**
204  * transform spill node => store
205  */
206 static void transform_Spill(ir_node *node)
207 {
208         ir_node   *block  = get_nodes_block(node);
209         dbg_info  *dbgi   = get_irn_dbg_info(node);
210         ir_node   *ptr    = get_irn_n(node, be_pos_Spill_frame);
211         ir_node   *mem    = new_NoMem();
212         ir_node   *val    = get_irn_n(node, be_pos_Spill_val);
213         ir_mode   *mode   = get_irn_mode(val);
214         ir_entity *entity = be_get_frame_entity(node);
215         ir_node   *sched_point;
216         ir_node   *store;
217
218         sched_point = sched_prev(node);
219         store = new_bd_sparc_St(dbgi, block, ptr, val, mem, mode, entity, false, 0, true);
220         sched_remove(node);
221         sched_add_after(sched_point, store);
222
223         exchange(node, store);
224 }
225
226 /**
227  * walker to transform be_Spill and be_Reload nodes
228  */
229 static void sparc_after_ra_walker(ir_node *block, void *data)
230 {
231         ir_node *node, *prev;
232         (void) data;
233
234         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
235                 prev = sched_prev(node);
236
237                 if (be_is_Reload(node)) {
238                         transform_Reload(node);
239                 } else if (be_is_Spill(node)) {
240                         transform_Spill(node);
241                 }
242         }
243 }
244
245 static void sparc_collect_frame_entity_nodes(ir_node *node, void *data)
246 {
247         be_fec_env_t  *env = data;
248         const ir_mode *mode;
249         int            align;
250         ir_entity     *entity;
251         const sparc_load_store_attr_t *attr;
252
253         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
254                 mode  = get_irn_mode(node);
255                 align = get_mode_size_bytes(mode);
256                 be_node_needs_frame_entity(env, node, mode, align);
257                 return;
258         }
259
260         if (!is_sparc_Ld(node) && !is_sparc_Ldf(node))
261                 return;
262
263         attr   = get_sparc_load_store_attr_const(node);
264         entity = attr->entity;
265         mode   = attr->load_store_mode;
266         if (entity != NULL)
267                 return;
268         if (!attr->is_frame_entity)
269                 return;
270         if (arch_irn_get_flags(node) & sparc_arch_irn_flag_needs_64bit_spillslot)
271                 mode = mode_Lu;
272         align  = get_mode_size_bytes(mode);
273         be_node_needs_frame_entity(env, node, mode, align);
274 }
275
276 static void sparc_set_frame_entity(ir_node *node, ir_entity *entity)
277 {
278         if (is_be_node(node)) {
279                 be_node_set_frame_entity(node, entity);
280         } else {
281                 /* we only say be_node_needs_frame_entity on nodes with load_store
282                  * attributes, so this should be fine */
283                 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(node);
284                 attr->entity = entity;
285         }
286 }
287
288
289 static void sparc_after_ra(void *self)
290 {
291         sparc_code_gen_t *cg      = self;
292         ir_graph         *irg     = cg->irg;
293         be_fec_env_t     *fec_env = be_new_frame_entity_coalescer(irg);
294
295         irg_walk_graph(irg, NULL, sparc_collect_frame_entity_nodes, fec_env);
296         be_assign_entities(fec_env, sparc_set_frame_entity);
297         be_free_frame_entity_coalescer(fec_env);
298
299         irg_block_walk_graph(cg->irg, NULL, sparc_after_ra_walker, NULL);
300 }
301
302
303
304 /**
305  * Emits the code, closes the output file and frees
306  * the code generator interface.
307  */
308 static void sparc_emit_and_done(void *self)
309 {
310         sparc_code_gen_t *cg = self;
311         ir_graph           *irg = cg->irg;
312
313         sparc_gen_routine(cg, irg);
314
315         /* de-allocate code generator */
316         free(cg);
317 }
318
319 static void *sparc_cg_init(ir_graph *irg);
320
321 static const arch_code_generator_if_t sparc_code_gen_if = {
322         sparc_cg_init,
323         NULL,                 /* get_pic_base hook */
324         NULL,                 /* before abi introduce hook */
325         sparc_prepare_graph,
326         NULL,                 /* spill hook */
327         sparc_before_ra,      /* before register allocation hook */
328         sparc_after_ra,       /* after register allocation hook */
329         NULL,
330         sparc_emit_and_done
331 };
332
333 /**
334  * Initializes the code generator.
335  */
336 static void *sparc_cg_init(ir_graph *irg)
337 {
338         sparc_isa_t      *isa = (sparc_isa_t *) be_get_irg_arch_env(irg);
339         sparc_code_gen_t *cg  = XMALLOCZ(sparc_code_gen_t);
340
341         cg->impl      = &sparc_code_gen_if;
342         cg->irg       = irg;
343         cg->isa       = isa;
344         cg->dump      = (be_get_irg_options(irg)->dump_flags & DUMP_BE) != 0;
345         cg->constants = pmap_create();
346
347         /* enter the current code generator */
348         isa->cg = cg;
349
350         return (arch_code_generator_t*) cg;
351 }
352
353 const arch_isa_if_t sparc_isa_if;
354 static sparc_isa_t sparc_isa_template = {
355         {
356                 &sparc_isa_if,                      /* isa interface implementation */
357                 &sparc_gp_regs[REG_SP],             /* stack pointer register */
358                 &sparc_gp_regs[REG_FRAME_POINTER],  /* base pointer register */
359                 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
360                 -1,                                 /* stack direction */
361                 3,                                  /* power of two stack alignment
362                                                        for calls */
363                 NULL,                               /* main environment */
364                 7,                                  /* costs for a spill instruction */
365                 5,                                  /* costs for a reload instruction */
366                 true,                               /* custom abi handling */
367         },
368         NULL                                            /* current code generator */
369 };
370
371
372 static void sparc_handle_intrinsics(void)
373 {
374         ir_type *tp, *int_tp, *uint_tp;
375         i_record records[8];
376         int n_records = 0;
377
378         runtime_rt rt_iMod, rt_uMod;
379
380 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
381
382         int_tp  = new_type_primitive(mode_Is);
383         uint_tp = new_type_primitive(mode_Iu);
384
385
386         /* SPARC has no signed mod instruction ... */
387         {
388                 i_instr_record *map_Mod = &records[n_records++].i_instr;
389
390                 tp = new_type_method(2, 1);
391                 set_method_param_type(tp, 0, int_tp);
392                 set_method_param_type(tp, 1, int_tp);
393                 set_method_res_type(tp, 0, int_tp);
394
395                 rt_iMod.ent             = new_entity(get_glob_type(), ID(".rem"), tp);
396                 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
397                 rt_iMod.mode            = mode_T;
398                 rt_iMod.res_mode        = mode_Is;
399                 rt_iMod.mem_proj_nr     = pn_Mod_M;
400                 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
401                 rt_iMod.exc_proj_nr     = pn_Mod_X_except;
402                 rt_iMod.exc_mem_proj_nr = pn_Mod_M;
403                 rt_iMod.res_proj_nr     = pn_Mod_res;
404
405                 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
406
407                 map_Mod->kind     = INTRINSIC_INSTR;
408                 map_Mod->op       = op_Mod;
409                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
410                 map_Mod->ctx      = &rt_iMod;
411         }
412         /* ... nor an unsigned mod. */
413         {
414                 i_instr_record *map_Mod = &records[n_records++].i_instr;
415
416                 tp = new_type_method(2, 1);
417                 set_method_param_type(tp, 0, uint_tp);
418                 set_method_param_type(tp, 1, uint_tp);
419                 set_method_res_type(tp, 0, uint_tp);
420
421                 rt_uMod.ent             = new_entity(get_glob_type(), ID(".urem"), tp);
422                 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
423                 rt_uMod.mode            = mode_T;
424                 rt_uMod.res_mode        = mode_Iu;
425                 rt_uMod.mem_proj_nr     = pn_Mod_M;
426                 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
427                 rt_uMod.exc_proj_nr     = pn_Mod_X_except;
428                 rt_uMod.exc_mem_proj_nr = pn_Mod_M;
429                 rt_uMod.res_proj_nr     = pn_Mod_res;
430
431                 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
432
433                 map_Mod->kind     = INTRINSIC_INSTR;
434                 map_Mod->op       = op_Mod;
435                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
436                 map_Mod->ctx      = &rt_uMod;
437         }
438
439         if (n_records > 0)
440                 lower_intrinsics(records, n_records, /*part_block_used=*/0);
441 }
442
443
444 /**
445  * Initializes the backend ISA
446  */
447 static arch_env_t *sparc_init(FILE *outfile)
448 {
449         static int run_once = 0;
450         sparc_isa_t *isa;
451
452         if (run_once)
453                 return NULL;
454         run_once = 1;
455
456         isa = XMALLOC(sparc_isa_t);
457         memcpy(isa, &sparc_isa_template, sizeof(*isa));
458
459         be_emit_init(outfile);
460
461         sparc_register_init();
462         sparc_create_opcodes(&sparc_irn_ops);
463         sparc_handle_intrinsics();
464
465         return &isa->base;
466 }
467
468
469
470 /**
471  * Closes the output file and frees the ISA structure.
472  */
473 static void sparc_done(void *self)
474 {
475         sparc_isa_t *isa = self;
476
477         /* emit now all global declarations */
478         be_gas_emit_decls(isa->base.main_env);
479
480         be_emit_exit();
481         free(self);
482 }
483
484
485 static unsigned sparc_get_n_reg_class(void)
486 {
487         return N_CLASSES;
488 }
489
490 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
491 {
492         assert(i < N_CLASSES);
493         return &sparc_reg_classes[i];
494 }
495
496
497
498 /**
499  * Get the register class which shall be used to store a value of a given mode.
500  * @param self The this pointer.
501  * @param mode The mode in question.
502  * @return A register class which can hold values of the given mode.
503  */
504 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
505 {
506         if (mode_is_float(mode))
507                 return &sparc_reg_classes[CLASS_sparc_fp];
508         else
509                 return &sparc_reg_classes[CLASS_sparc_gp];
510 }
511
512 static int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
513 {
514         (void) block_env;
515
516         if (!is_sparc_irn(irn))
517                 return -1;
518
519         return 1;
520 }
521
522 /**
523  * Initializes the code generator interface.
524  */
525 static const arch_code_generator_if_t *sparc_get_code_generator_if(
526                 void *self)
527 {
528         (void) self;
529         return &sparc_code_gen_if;
530 }
531
532 list_sched_selector_t sparc_sched_selector;
533
534 /**
535  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
536  */
537 static const list_sched_selector_t *sparc_get_list_sched_selector(
538                 const void *self, list_sched_selector_t *selector)
539 {
540         (void) self;
541         (void) selector;
542
543         sparc_sched_selector = trivial_selector;
544         sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
545         return &sparc_sched_selector;
546 }
547
548 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
549                 const void *self)
550 {
551         (void) self;
552         return NULL;
553 }
554
555 /**
556  * Returns the necessary byte alignment for storing a register of given class.
557  */
558 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
559 {
560         ir_mode *mode = arch_register_class_mode(cls);
561         return get_mode_size_bytes(mode);
562 }
563
564 static void sparc_lower_for_target(void)
565 {
566         /* TODO, doubleword lowering and others */
567 }
568
569 /**
570  * Returns the libFirm configuration parameter for this backend.
571  */
572 static const backend_params *sparc_get_backend_params(void)
573 {
574         static backend_params p = {
575                 0,     /* no inline assembly */
576                 sparc_lower_for_target, /* lowering callback */
577                 NULL,  /* will be set later */
578                 NULL,  /* parameter for if conversion */
579                 NULL,  /* float arithmetic mode */
580                 0,     /* no trampoline support: size 0 */
581                 0,     /* no trampoline support: align 0 */
582                 NULL,  /* no trampoline support: no trampoline builder */
583                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
584         };
585         return &p;
586 }
587
588 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
589                 const ir_node *irn)
590 {
591         (void) irn;
592         /* TODO */
593         panic("sparc_get_allowed_execution_units not implemented yet");
594 }
595
596 static const be_machine_t *sparc_get_machine(const void *self)
597 {
598         (void) self;
599         /* TODO */
600         panic("sparc_get_machine not implemented yet");
601 }
602
603 static ir_graph **sparc_get_backend_irg_list(const void *self,
604                                              ir_graph ***irgs)
605 {
606         (void) self;
607         (void) irgs;
608         return NULL;
609 }
610
611 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
612 {
613         (void) c;
614         return ASM_CONSTRAINT_FLAG_INVALID;
615 }
616
617 static int sparc_is_valid_clobber(const char *clobber)
618 {
619         (void) clobber;
620         return 0;
621 }
622
623 const arch_isa_if_t sparc_isa_if = {
624         sparc_init,
625         sparc_done,
626         NULL,                /* handle intrinsics */
627         sparc_get_n_reg_class,
628         sparc_get_reg_class,
629         sparc_get_reg_class_for_mode,
630         NULL,
631         sparc_get_code_generator_if,
632         sparc_get_list_sched_selector,
633         sparc_get_ilp_sched_selector,
634         sparc_get_reg_class_alignment,
635         sparc_get_backend_params,
636         sparc_get_allowed_execution_units,
637         sparc_get_machine,
638         sparc_get_backend_irg_list,
639         NULL,                    /* mark remat */
640         sparc_parse_asm_constraint,
641         sparc_is_valid_clobber
642 };
643
644 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);
645 void be_init_arch_sparc(void)
646 {
647         be_register_isa_if("sparc", &sparc_isa_if);
648         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
649         sparc_init_transform();
650         sparc_init_emitter();
651 }