5e2e1a93ccb0ff14a2cefd91e811f3042cf246b0
[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         align  = get_mode_size_bytes(mode);
267         if (entity != NULL)
268                 return;
269         if (!attr->is_frame_entity)
270                 return;
271         be_node_needs_frame_entity(env, node, mode, align);
272 }
273
274 static void sparc_set_frame_entity(ir_node *node, ir_entity *entity)
275 {
276         if (is_be_node(node)) {
277                 be_node_set_frame_entity(node, entity);
278         } else {
279                 /* we only say be_node_needs_frame_entity on nodes with load_store
280                  * attributes, so this should be fine */
281                 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(node);
282                 attr->entity = entity;
283         }
284 }
285
286
287 static void sparc_after_ra(void *self)
288 {
289         sparc_code_gen_t *cg      = self;
290         ir_graph         *irg     = cg->irg;
291         be_fec_env_t     *fec_env = be_new_frame_entity_coalescer(irg);
292
293         irg_walk_graph(irg, NULL, sparc_collect_frame_entity_nodes, fec_env);
294         be_assign_entities(fec_env, sparc_set_frame_entity);
295         be_free_frame_entity_coalescer(fec_env);
296
297         irg_block_walk_graph(cg->irg, NULL, sparc_after_ra_walker, NULL);
298 }
299
300
301
302 /**
303  * Emits the code, closes the output file and frees
304  * the code generator interface.
305  */
306 static void sparc_emit_and_done(void *self)
307 {
308         sparc_code_gen_t *cg = self;
309         ir_graph           *irg = cg->irg;
310
311         sparc_gen_routine(cg, irg);
312
313         /* de-allocate code generator */
314         free(cg);
315 }
316
317 static void *sparc_cg_init(ir_graph *irg);
318
319 static const arch_code_generator_if_t sparc_code_gen_if = {
320         sparc_cg_init,
321         NULL,                 /* get_pic_base hook */
322         NULL,                 /* before abi introduce hook */
323         sparc_prepare_graph,
324         NULL,                 /* spill hook */
325         sparc_before_ra,      /* before register allocation hook */
326         sparc_after_ra,       /* after register allocation hook */
327         NULL,
328         sparc_emit_and_done
329 };
330
331 /**
332  * Initializes the code generator.
333  */
334 static void *sparc_cg_init(ir_graph *irg)
335 {
336         sparc_isa_t      *isa = (sparc_isa_t *) be_get_irg_arch_env(irg);
337         sparc_code_gen_t *cg  = XMALLOCZ(sparc_code_gen_t);
338
339         cg->impl      = &sparc_code_gen_if;
340         cg->irg       = irg;
341         cg->isa       = isa;
342         cg->dump      = (be_get_irg_options(irg)->dump_flags & DUMP_BE) != 0;
343         cg->constants = pmap_create();
344
345         /* enter the current code generator */
346         isa->cg = cg;
347
348         return (arch_code_generator_t*) cg;
349 }
350
351 const arch_isa_if_t sparc_isa_if;
352 static sparc_isa_t sparc_isa_template = {
353         {
354                 &sparc_isa_if,                      /* isa interface implementation */
355                 &sparc_gp_regs[REG_SP],             /* stack pointer register */
356                 &sparc_gp_regs[REG_FRAME_POINTER],  /* base pointer register */
357                 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
358                 -1,                                 /* stack direction */
359                 3,                                  /* power of two stack alignment
360                                                        for calls */
361                 NULL,                               /* main environment */
362                 7,                                  /* costs for a spill instruction */
363                 5,                                  /* costs for a reload instruction */
364                 true,                               /* custom abi handling */
365         },
366         NULL                                            /* current code generator */
367 };
368
369
370 static void sparc_handle_intrinsics(void)
371 {
372         ir_type *tp, *int_tp, *uint_tp;
373         i_record records[8];
374         int n_records = 0;
375
376         runtime_rt rt_iMod, rt_uMod;
377
378 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
379
380         int_tp  = new_type_primitive(mode_Is);
381         uint_tp = new_type_primitive(mode_Iu);
382
383
384         /* SPARC has no signed mod instruction ... */
385         {
386                 i_instr_record *map_Mod = &records[n_records++].i_instr;
387
388                 tp = new_type_method(2, 1);
389                 set_method_param_type(tp, 0, int_tp);
390                 set_method_param_type(tp, 1, int_tp);
391                 set_method_res_type(tp, 0, int_tp);
392
393                 rt_iMod.ent             = new_entity(get_glob_type(), ID(".rem"), tp);
394                 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
395                 rt_iMod.mode            = mode_T;
396                 rt_iMod.res_mode        = mode_Is;
397                 rt_iMod.mem_proj_nr     = pn_Mod_M;
398                 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
399                 rt_iMod.exc_proj_nr     = pn_Mod_X_except;
400                 rt_iMod.exc_mem_proj_nr = pn_Mod_M;
401                 rt_iMod.res_proj_nr     = pn_Mod_res;
402
403                 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
404
405                 map_Mod->kind     = INTRINSIC_INSTR;
406                 map_Mod->op       = op_Mod;
407                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
408                 map_Mod->ctx      = &rt_iMod;
409         }
410         /* ... nor an unsigned mod. */
411         {
412                 i_instr_record *map_Mod = &records[n_records++].i_instr;
413
414                 tp = new_type_method(2, 1);
415                 set_method_param_type(tp, 0, uint_tp);
416                 set_method_param_type(tp, 1, uint_tp);
417                 set_method_res_type(tp, 0, uint_tp);
418
419                 rt_uMod.ent             = new_entity(get_glob_type(), ID(".urem"), tp);
420                 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
421                 rt_uMod.mode            = mode_T;
422                 rt_uMod.res_mode        = mode_Iu;
423                 rt_uMod.mem_proj_nr     = pn_Mod_M;
424                 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
425                 rt_uMod.exc_proj_nr     = pn_Mod_X_except;
426                 rt_uMod.exc_mem_proj_nr = pn_Mod_M;
427                 rt_uMod.res_proj_nr     = pn_Mod_res;
428
429                 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
430
431                 map_Mod->kind     = INTRINSIC_INSTR;
432                 map_Mod->op       = op_Mod;
433                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
434                 map_Mod->ctx      = &rt_uMod;
435         }
436
437         if (n_records > 0)
438                 lower_intrinsics(records, n_records, /*part_block_used=*/0);
439 }
440
441
442 /**
443  * Initializes the backend ISA
444  */
445 static arch_env_t *sparc_init(FILE *outfile)
446 {
447         static int run_once = 0;
448         sparc_isa_t *isa;
449
450         if (run_once)
451                 return NULL;
452         run_once = 1;
453
454         isa = XMALLOC(sparc_isa_t);
455         memcpy(isa, &sparc_isa_template, sizeof(*isa));
456
457         be_emit_init(outfile);
458
459         sparc_register_init();
460         sparc_create_opcodes(&sparc_irn_ops);
461         sparc_handle_intrinsics();
462
463         return &isa->base;
464 }
465
466
467
468 /**
469  * Closes the output file and frees the ISA structure.
470  */
471 static void sparc_done(void *self)
472 {
473         sparc_isa_t *isa = self;
474
475         /* emit now all global declarations */
476         be_gas_emit_decls(isa->base.main_env);
477
478         be_emit_exit();
479         free(self);
480 }
481
482
483 static unsigned sparc_get_n_reg_class(void)
484 {
485         return N_CLASSES;
486 }
487
488 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
489 {
490         assert(i < N_CLASSES);
491         return &sparc_reg_classes[i];
492 }
493
494
495
496 /**
497  * Get the register class which shall be used to store a value of a given mode.
498  * @param self The this pointer.
499  * @param mode The mode in question.
500  * @return A register class which can hold values of the given mode.
501  */
502 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
503 {
504         if (mode_is_float(mode))
505                 return &sparc_reg_classes[CLASS_sparc_fp];
506         else
507                 return &sparc_reg_classes[CLASS_sparc_gp];
508 }
509
510 static int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
511 {
512         (void) block_env;
513
514         if (!is_sparc_irn(irn))
515                 return -1;
516
517         return 1;
518 }
519
520 /**
521  * Initializes the code generator interface.
522  */
523 static const arch_code_generator_if_t *sparc_get_code_generator_if(
524                 void *self)
525 {
526         (void) self;
527         return &sparc_code_gen_if;
528 }
529
530 list_sched_selector_t sparc_sched_selector;
531
532 /**
533  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
534  */
535 static const list_sched_selector_t *sparc_get_list_sched_selector(
536                 const void *self, list_sched_selector_t *selector)
537 {
538         (void) self;
539         (void) selector;
540
541         sparc_sched_selector = trivial_selector;
542         sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
543         return &sparc_sched_selector;
544 }
545
546 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
547                 const void *self)
548 {
549         (void) self;
550         return NULL;
551 }
552
553 /**
554  * Returns the necessary byte alignment for storing a register of given class.
555  */
556 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
557 {
558         ir_mode *mode = arch_register_class_mode(cls);
559         return get_mode_size_bytes(mode);
560 }
561
562 /**
563  * Returns the libFirm configuration parameter for this backend.
564  */
565 static const backend_params *sparc_get_backend_params(void)
566 {
567         static backend_params p = {
568                 0,     /* no dword lowering */
569                 0,     /* no inline assembly */
570                 NULL,  /* will be set later */
571                 NULL,  /* no creator function */
572                 NULL,  /* context for create_intrinsic_fkt */
573                 NULL,  /* parameter for if conversion */
574                 NULL,  /* float arithmetic mode */
575                 0,     /* no trampoline support: size 0 */
576                 0,     /* no trampoline support: align 0 */
577                 NULL,  /* no trampoline support: no trampoline builder */
578                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
579         };
580         return &p;
581 }
582
583 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
584                 const ir_node *irn)
585 {
586         (void) irn;
587         /* TODO */
588         panic("sparc_get_allowed_execution_units not implemented yet");
589 }
590
591 static const be_machine_t *sparc_get_machine(const void *self)
592 {
593         (void) self;
594         /* TODO */
595         panic("sparc_get_machine not implemented yet");
596 }
597
598 static ir_graph **sparc_get_backend_irg_list(const void *self,
599                                              ir_graph ***irgs)
600 {
601         (void) self;
602         (void) irgs;
603         return NULL;
604 }
605
606 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
607 {
608         (void) c;
609         return ASM_CONSTRAINT_FLAG_INVALID;
610 }
611
612 static int sparc_is_valid_clobber(const char *clobber)
613 {
614         (void) clobber;
615         return 0;
616 }
617
618 const arch_isa_if_t sparc_isa_if = {
619         sparc_init,
620         sparc_done,
621         NULL,                /* handle intrinsics */
622         sparc_get_n_reg_class,
623         sparc_get_reg_class,
624         sparc_get_reg_class_for_mode,
625         NULL,
626         sparc_get_code_generator_if,
627         sparc_get_list_sched_selector,
628         sparc_get_ilp_sched_selector,
629         sparc_get_reg_class_alignment,
630         sparc_get_backend_params,
631         sparc_get_allowed_execution_units,
632         sparc_get_machine,
633         sparc_get_backend_irg_list,
634         NULL,                    /* mark remat */
635         sparc_parse_asm_constraint,
636         sparc_is_valid_clobber
637 };
638
639 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);
640 void be_init_arch_sparc(void)
641 {
642         be_register_isa_if("sparc", &sparc_isa_if);
643         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
644         sparc_init_transform();
645         sparc_init_emitter();
646 }