initaler checkin SPARC backend
[libfirm] / ir / be / sparc / bearch_sparc.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 sparc backend driver file.
23  * @version  $Id: bearch_TEMPLATE.c 26673 2009-10-01 16:43:13Z matze $
24  */
25
26 #include "config.h"
27
28 #include "lc_opts.h"
29 #include "lc_opts_enum.h"
30
31 #include "pseudo_irg.h"
32 #include "irgwalk.h"
33 #include "irprog.h"
34 #include "irprintf.h"
35 #include "ircons.h"
36 #include "irgmod.h"
37 #include "irgopt.h"
38 #include "iroptimize.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 "../beabi.h"
53 #include "../bemachine.h"
54 #include "../beilpsched.h"
55 #include "../bemodule.h"
56 #include "../beirg.h"
57 #include "../bespillslots.h"
58 #include "../begnuas.h"
59 #include "../belistsched.h"
60 #include "../beflags.h"
61
62 #include "bearch_sparc_t.h"
63
64 #include "sparc_new_nodes.h"
65 #include "gen_sparc_regalloc_if.h"
66 #include "sparc_transform.h"
67 #include "sparc_emitter.h"
68
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
70
71 static arch_irn_class_t sparc_classify(const ir_node *irn)
72 {
73         (void) irn;
74         return 0;
75 }
76
77 static ir_entity *sparc_get_frame_entity(const ir_node *node)
78 {
79         (void) node;
80         /* TODO: return the ir_entity assigned to the frame */
81         return NULL;
82 }
83
84 static void sparc_set_frame_entity(ir_node *node, ir_entity *ent)
85 {
86         (void) node;
87         (void) ent;
88         /* TODO: set the ir_entity assigned to the frame */
89 }
90
91 /**
92  * This function is called by the generic backend to correct offsets for
93  * nodes accessing the stack.
94  */
95 static void sparc_set_frame_offset(ir_node *irn, int offset)
96 {
97         (void) irn;
98         (void) offset;
99         /* TODO: correct offset if irn accesses the stack */
100 }
101
102 static int sparc_get_sp_bias(const ir_node *irn)
103 {
104         (void) irn;
105         return 0;
106 }
107
108 /* fill register allocator interface */
109
110 static const arch_irn_ops_t sparc_irn_ops = {
111         get_sparc_in_req,
112         sparc_classify,
113         sparc_get_frame_entity,
114         sparc_set_frame_entity,
115         sparc_set_frame_offset,
116         sparc_get_sp_bias,
117         NULL,    /* get_inverse             */
118         NULL,    /* get_op_estimated_cost   */
119         NULL,    /* possible_memory_operand */
120         NULL,    /* perform_memory_operand  */
121 };
122
123
124
125 /**
126  * Transforms the standard firm graph into
127  * a SPARC firm graph
128  */
129 static void sparc_prepare_graph(void *self)
130 {
131         sparc_code_gen_t *cg = self;
132
133         /* transform FIRM into SPARC asm nodes */
134         sparc_transform_graph(cg);
135
136         if (cg->dump)
137                 be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
138 }
139
140
141
142 /**
143  * Called immediatly before emit phase.
144  */
145 static void sparc_finish_irg(void *self)
146 {
147         sparc_code_gen_t *cg = self;
148         ir_graph            *irg = cg->irg;
149
150         dump_ir_block_graph_sched(irg, "-sparc-finished");
151 }
152
153
154 static ir_node *sparc_flags_remat(ir_node *node, ir_node *after)
155 {
156         ir_node *block;
157         ir_node *copy;
158
159         if (is_Block(after)) {
160                 block = after;
161         } else {
162                 block = get_nodes_block(after);
163         }
164         copy = exact_copy(node);
165         set_nodes_block(copy, block);
166         sched_add_after(after, copy);
167         return copy;
168 }
169
170 static void sparc_before_ra(void *self)
171 {
172         (void) self;
173         sparc_code_gen_t *cg = self;
174         /* fixup flags register */
175         be_sched_fix_flags(cg->birg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
176 }
177
178 static void sparc_after_ra(void *self)
179 {
180         (void) self;
181         /* Some stuff you need to do immediatly after register allocation */
182 }
183
184
185
186 /**
187  * Emits the code, closes the output file and frees
188  * the code generator interface.
189  */
190 static void sparc_emit_and_done(void *self)
191 {
192         sparc_code_gen_t *cg = self;
193         ir_graph           *irg = cg->irg;
194
195         sparc_gen_routine(cg, irg);
196
197         /* de-allocate code generator */
198         free(cg);
199 }
200
201 static void *sparc_cg_init(be_irg_t *birg);
202
203 static const arch_code_generator_if_t sparc_code_gen_if = {
204         sparc_cg_init,
205         NULL,                    /* get_pic_base hook */
206         NULL,                    /* before abi introduce hook */
207         sparc_prepare_graph,
208         NULL,                    /* spill hook */
209         sparc_before_ra,      /* before register allocation hook */
210         sparc_after_ra,       /* after register allocation hook */
211         sparc_finish_irg,
212         sparc_emit_and_done
213 };
214
215 /**
216  * Initializes the code generator.
217  */
218 static void *sparc_cg_init(be_irg_t *birg)
219 {
220         static ir_type *int_tp = NULL;
221         sparc_isa_t      *isa = (sparc_isa_t *)birg->main_env->arch_env;
222         sparc_code_gen_t *cg;
223
224         if (! int_tp) {
225                 /* create an integer type with machine size */
226                 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
227         }
228
229         cg                               = XMALLOC(sparc_code_gen_t);
230         cg->impl                                = &sparc_code_gen_if;
231         cg->irg                         = birg->irg;
232         //cg->reg_set                           = new_set(arm_cmp_irn_reg_assoc, 1024);
233         cg->isa                         = isa;
234         cg->birg                                = birg;
235         //cg->int_tp                            = int_tp;
236         //cg->have_fp_insn      = 0;
237         //cg->unknown_gp                = NULL;
238         //cg->unknown_fpa               = NULL;
239         cg->dump                                = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
240
241         FIRM_DBG_REGISTER(cg->mod, "firm.be.sparc.cg");
242
243         /* enter the current code generator */
244         isa->cg = cg;
245
246         return (arch_code_generator_t *)cg;
247 }
248
249
250
251 const arch_isa_if_t sparc_isa_if;
252 static sparc_isa_t sparc_isa_template = {
253         {
254                 &sparc_isa_if,             /* isa interface implementation */
255                 &sparc_gp_regs[REG_SP],  /* stack pointer register */
256                 &sparc_gp_regs[REG_FP],  /* base pointer register */
257                 &sparc_reg_classes[CLASS_sparc_gp],  /* link pointer register class */
258                 -1,                          /* stack direction */
259                 1,                           /* power of two stack alignment for calls, 2^2 == 4 */
260                 NULL,                        /* main environment */
261                 7,                           /* costs for a spill instruction */
262                 5,                           /* costs for a reload instruction */
263         },
264         NULL                                            /* current code generator */
265 };
266
267 /**
268  * Initializes the backend ISA
269  */
270 static arch_env_t *sparc_init(FILE *outfile)
271 {
272         static int run_once = 0;
273         sparc_isa_t *isa;
274
275         if(run_once)
276                 return NULL;
277         run_once = 1;
278
279         isa = XMALLOC(sparc_isa_t);
280         memcpy(isa, &sparc_isa_template, sizeof(*isa));
281
282         be_emit_init(outfile);
283
284         sparc_register_init();
285         sparc_create_opcodes(&sparc_irn_ops);
286
287         return &isa->arch_env;
288 }
289
290
291
292 /**
293  * Closes the output file and frees the ISA structure.
294  */
295 static void sparc_done(void *self)
296 {
297         sparc_isa_t *isa = self;
298
299         /* emit now all global declarations */
300         be_gas_emit_decls(isa->arch_env.main_env, 0);
301
302         be_emit_exit();
303         free(self);
304 }
305
306
307 static unsigned sparc_get_n_reg_class(void)
308 {
309         return N_CLASSES;
310 }
311
312 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
313 {
314         assert(i < N_CLASSES);
315         return &sparc_reg_classes[i];
316 }
317
318
319
320 /**
321  * Get the register class which shall be used to store a value of a given mode.
322  * @param self The this pointer.
323  * @param mode The mode in question.
324  * @return A register class which can hold values of the given mode.
325  */
326 const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
327 {
328         if (mode_is_float(mode))
329                 return &sparc_reg_classes[CLASS_sparc_fp];
330         else
331                 return &sparc_reg_classes[CLASS_sparc_gp];
332 }
333
334
335
336 typedef struct {
337         be_abi_call_flags_bits_t flags;
338         const arch_env_t *arch_env;
339         ir_graph *irg;
340 } sparc_abi_env_t;
341
342 static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
343 {
344         sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
345         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
346         env->flags    = fl.bits;
347         env->irg      = irg;
348         env->arch_env = arch_env;
349         return env;
350 }
351
352 /**
353  * Get the between type for that call.
354  * @param self The callback object.
355  * @return The between type of for that call.
356  */
357 static ir_type *sparc_get_between_type(void *self)
358 {
359         static ir_type *between_type = NULL;
360         (void) self;
361
362         if (between_type == NULL) {
363                 between_type = new_type_class(new_id_from_str("sparc_between_type"));
364                 set_type_size_bytes(between_type, 0);
365         }
366
367         return between_type;
368 }
369
370 /**
371  * Build the prolog, return the BASE POINTER register
372  */
373 static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
374                                                     pmap *reg_map, int *stack_bias)
375 {
376         sparc_abi_env_t *env = self;
377         (void) reg_map;
378         (void) mem;
379         (void) stack_bias;
380
381         if(env->flags.try_omit_fp)
382                 return env->arch_env->sp;
383
384         //panic("framepointer not implemented yet");
385         return env->arch_env->bp;
386 }
387
388 /* Build the epilog */
389 static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
390                                   pmap *reg_map)
391 {
392         (void) self;
393         (void) bl;
394         (void) mem;
395         (void) reg_map;
396 }
397
398 static const be_abi_callbacks_t sparc_abi_callbacks = {
399         sparc_abi_init,
400         free,
401         sparc_get_between_type,
402         sparc_abi_prologue,
403         sparc_abi_epilogue,
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 void sparc_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         (void) self;
420
421         /* set abi flags for calls */
422         call_flags.bits.left_to_right         = 0;
423         call_flags.bits.store_args_sequential = 1;
424         call_flags.bits.try_omit_fp           = 1;
425         call_flags.bits.fp_free               = 0;
426         call_flags.bits.call_has_imm          = 1;
427
428         /* set stack parameter passing style */
429         be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);
430
431         for (i = 0; i < n; i++) {
432                 /* TODO: implement register parameter: */
433                 /* reg = get reg for param i;          */
434                 /* be_abi_call_param_reg(abi, i, reg); */
435                 /* default: all parameters on stack */
436                 tp   = get_method_param_type(method_type, i);
437                 mode = get_type_mode(tp);
438                 be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
439         }
440
441         /* TODO: set correct return register */
442         /* default: return value is in O0 resp. F0 */
443         if (get_method_n_ress(method_type) > 0) {
444                 tp   = get_method_res_type(method_type, 0);
445                 mode = get_type_mode(tp);
446
447                 be_abi_call_res_reg(abi, 0,
448                         mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0]);
449         }
450 }
451
452 int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
453 {
454         (void) block_env;
455
456         if(!is_sparc_irn(irn))
457                 return -1;
458
459         return 1;
460 }
461
462 /**
463  * Initializes the code generator interface.
464  */
465 static const arch_code_generator_if_t *sparc_get_code_generator_if(
466                 void *self)
467 {
468         (void) self;
469         return &sparc_code_gen_if;
470 }
471
472 list_sched_selector_t sparc_sched_selector;
473
474 /**
475  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
476  */
477 static const list_sched_selector_t *sparc_get_list_sched_selector(
478                 const void *self, list_sched_selector_t *selector)
479 {
480         (void) self;
481         (void) selector;
482
483         sparc_sched_selector = trivial_selector;
484         sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
485         return &sparc_sched_selector;
486 }
487
488 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
489                 const void *self)
490 {
491         (void) self;
492         return NULL;
493 }
494
495 /**
496  * Returns the necessary byte alignment for storing a register of given class.
497  */
498 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
499 {
500         ir_mode *mode = arch_register_class_mode(cls);
501         return get_mode_size_bytes(mode);
502 }
503
504 /**
505  * Returns the libFirm configuration parameter for this backend.
506  */
507 static const backend_params *sparc_get_backend_params(void) {
508         static backend_params p = {
509                 0,     /* no dword lowering */
510                 0,     /* no inline assembly */
511                 NULL,  /* will be set later */
512                 NULL,  /* no creator function */
513                 NULL,  /* context for create_intrinsic_fkt */
514                 NULL,  /* parameter for if conversion */
515                 NULL,  /* float arithmetic mode */
516                 0,     /* no trampoline support: size 0 */
517                 0,     /* no trampoline support: align 0 */
518                 NULL,  /* no trampoline support: no trampoline builder */
519                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
520         };
521         return &p;
522 }
523
524 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
525                 const ir_node *irn)
526 {
527         (void) irn;
528         /* TODO */
529         assert(0);
530         return NULL;
531 }
532
533 static const be_machine_t *sparc_get_machine(const void *self)
534 {
535         (void) self;
536         /* TODO */
537         assert(0);
538         return NULL;
539 }
540
541 static ir_graph **sparc_get_backend_irg_list(const void *self,
542                                                 ir_graph ***irgs)
543 {
544         (void) self;
545         (void) irgs;
546         return NULL;
547 }
548
549 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
550 {
551         (void) c;
552         return ASM_CONSTRAINT_FLAG_INVALID;
553 }
554
555 static int sparc_is_valid_clobber(const char *clobber)
556 {
557         (void) clobber;
558         return 0;
559 }
560
561 const arch_isa_if_t sparc_isa_if = {
562         sparc_init,
563         sparc_done,
564         NULL,                /* handle intrinsics */
565         sparc_get_n_reg_class,
566         sparc_get_reg_class,
567         sparc_get_reg_class_for_mode,
568         sparc_get_call_abi,
569         sparc_get_code_generator_if,
570         sparc_get_list_sched_selector,
571         sparc_get_ilp_sched_selector,
572         sparc_get_reg_class_alignment,
573     sparc_get_backend_params,
574         sparc_get_allowed_execution_units,
575         sparc_get_machine,
576         sparc_get_backend_irg_list,
577         NULL,                    /* mark remat */
578         sparc_parse_asm_constraint,
579         sparc_is_valid_clobber
580 };
581
582 void be_init_arch_sparc(void)
583 {
584         be_register_isa_if("sparc", &sparc_isa_if);
585         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
586         sparc_init_transform();
587         sparc_init_emitter();
588 }
589 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);