Removed C99 features and fixed 2 wrong assertions.
[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         sparc_code_gen_t *cg = self;
173         /* fixup flags register */
174         be_sched_fix_flags(cg->birg, &sparc_reg_classes[CLASS_sparc_flags], &sparc_flags_remat);
175 }
176
177 static void sparc_after_ra(void *self)
178 {
179         (void) self;
180         /* Some stuff you need to do immediatly after register allocation */
181 }
182
183
184
185 /**
186  * Emits the code, closes the output file and frees
187  * the code generator interface.
188  */
189 static void sparc_emit_and_done(void *self)
190 {
191         sparc_code_gen_t *cg = self;
192         ir_graph           *irg = cg->irg;
193
194         sparc_gen_routine(cg, irg);
195
196         /* de-allocate code generator */
197         free(cg);
198 }
199
200 static void *sparc_cg_init(be_irg_t *birg);
201
202 static const arch_code_generator_if_t sparc_code_gen_if = {
203         sparc_cg_init,
204         NULL,                    /* get_pic_base hook */
205         NULL,                    /* before abi introduce hook */
206         sparc_prepare_graph,
207         NULL,                    /* spill hook */
208         sparc_before_ra,      /* before register allocation hook */
209         sparc_after_ra,       /* after register allocation hook */
210         sparc_finish_irg,
211         sparc_emit_and_done
212 };
213
214 /**
215  * Initializes the code generator.
216  */
217 static void *sparc_cg_init(be_irg_t *birg)
218 {
219         static ir_type *int_tp = NULL;
220         sparc_isa_t      *isa = (sparc_isa_t *)birg->main_env->arch_env;
221         sparc_code_gen_t *cg;
222
223         if (! int_tp) {
224                 /* create an integer type with machine size */
225                 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
226         }
227
228         cg                               = XMALLOC(sparc_code_gen_t);
229         cg->impl                                = &sparc_code_gen_if;
230         cg->irg                         = birg->irg;
231         //cg->reg_set                           = new_set(arm_cmp_irn_reg_assoc, 1024);
232         cg->isa                         = isa;
233         cg->birg                                = birg;
234         //cg->int_tp                            = int_tp;
235         //cg->have_fp_insn      = 0;
236         //cg->unknown_gp                = NULL;
237         //cg->unknown_fpa               = NULL;
238         cg->dump                                = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
239
240         FIRM_DBG_REGISTER(cg->mod, "firm.be.sparc.cg");
241
242         /* enter the current code generator */
243         isa->cg = cg;
244
245         return (arch_code_generator_t *)cg;
246 }
247
248
249
250 const arch_isa_if_t sparc_isa_if;
251 static sparc_isa_t sparc_isa_template = {
252         {
253                 &sparc_isa_if,             /* isa interface implementation */
254                 &sparc_gp_regs[REG_SP],  /* stack pointer register */
255                 &sparc_gp_regs[REG_FP],  /* base pointer register */
256                 &sparc_reg_classes[CLASS_sparc_gp],  /* link pointer register class */
257                 -1,                          /* stack direction */
258                 1,                           /* power of two stack alignment for calls, 2^2 == 4 */
259                 NULL,                        /* main environment */
260                 7,                           /* costs for a spill instruction */
261                 5,                           /* costs for a reload instruction */
262         },
263         NULL                                            /* current code generator */
264 };
265
266 /**
267  * Initializes the backend ISA
268  */
269 static arch_env_t *sparc_init(FILE *outfile)
270 {
271         static int run_once = 0;
272         sparc_isa_t *isa;
273
274         if(run_once)
275                 return NULL;
276         run_once = 1;
277
278         isa = XMALLOC(sparc_isa_t);
279         memcpy(isa, &sparc_isa_template, sizeof(*isa));
280
281         be_emit_init(outfile);
282
283         sparc_register_init();
284         sparc_create_opcodes(&sparc_irn_ops);
285
286         return &isa->arch_env;
287 }
288
289
290
291 /**
292  * Closes the output file and frees the ISA structure.
293  */
294 static void sparc_done(void *self)
295 {
296         sparc_isa_t *isa = self;
297
298         /* emit now all global declarations */
299         be_gas_emit_decls(isa->arch_env.main_env, 0);
300
301         be_emit_exit();
302         free(self);
303 }
304
305
306 static unsigned sparc_get_n_reg_class(void)
307 {
308         return N_CLASSES;
309 }
310
311 static const arch_register_class_t *sparc_get_reg_class(unsigned i)
312 {
313         assert(i < N_CLASSES);
314         return &sparc_reg_classes[i];
315 }
316
317
318
319 /**
320  * Get the register class which shall be used to store a value of a given mode.
321  * @param self The this pointer.
322  * @param mode The mode in question.
323  * @return A register class which can hold values of the given mode.
324  */
325 const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
326 {
327         if (mode_is_float(mode))
328                 return &sparc_reg_classes[CLASS_sparc_fp];
329         else
330                 return &sparc_reg_classes[CLASS_sparc_gp];
331 }
332
333
334
335 typedef struct {
336         be_abi_call_flags_bits_t flags;
337         const arch_env_t *arch_env;
338         ir_graph *irg;
339 } sparc_abi_env_t;
340
341 static void *sparc_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
342 {
343         sparc_abi_env_t *env = XMALLOC(sparc_abi_env_t);
344         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
345         env->flags    = fl.bits;
346         env->irg      = irg;
347         env->arch_env = arch_env;
348         return env;
349 }
350
351 /**
352  * Get the between type for that call.
353  * @param self The callback object.
354  * @return The between type of for that call.
355  */
356 static ir_type *sparc_get_between_type(void *self)
357 {
358         static ir_type *between_type = NULL;
359         (void) self;
360
361         if (between_type == NULL) {
362                 between_type = new_type_class(new_id_from_str("sparc_between_type"));
363                 set_type_size_bytes(between_type, 0);
364         }
365
366         return between_type;
367 }
368
369 /**
370  * Build the prolog, return the BASE POINTER register
371  */
372 static const arch_register_t *sparc_abi_prologue(void *self, ir_node **mem,
373                                                     pmap *reg_map, int *stack_bias)
374 {
375         sparc_abi_env_t *env = self;
376         (void) reg_map;
377         (void) mem;
378         (void) stack_bias;
379
380         if(env->flags.try_omit_fp)
381                 return env->arch_env->sp;
382
383         //panic("framepointer not implemented yet");
384         return env->arch_env->bp;
385 }
386
387 /* Build the epilog */
388 static void sparc_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
389                                   pmap *reg_map)
390 {
391         (void) self;
392         (void) bl;
393         (void) mem;
394         (void) reg_map;
395 }
396
397 static const be_abi_callbacks_t sparc_abi_callbacks = {
398         sparc_abi_init,
399         free,
400         sparc_get_between_type,
401         sparc_abi_prologue,
402         sparc_abi_epilogue,
403 };
404
405 /**
406  * Get the ABI restrictions for procedure calls.
407  * @param self        The this pointer.
408  * @param method_type The type of the method (procedure) in question.
409  * @param abi         The abi object to be modified
410  */
411 void sparc_get_call_abi(const void *self, ir_type *method_type,
412                            be_abi_call_t *abi)
413 {
414         ir_type  *tp;
415         ir_mode  *mode;
416         int       i, n = get_method_n_params(method_type);
417         be_abi_call_flags_t call_flags;
418         (void) self;
419
420         /* set abi flags for calls */
421         call_flags.bits.left_to_right         = 0;
422         call_flags.bits.store_args_sequential = 1;
423         call_flags.bits.try_omit_fp           = 1;
424         call_flags.bits.fp_free               = 0;
425         call_flags.bits.call_has_imm          = 1;
426
427         /* set stack parameter passing style */
428         be_abi_call_set_flags(abi, call_flags, &sparc_abi_callbacks);
429
430         for (i = 0; i < n; i++) {
431                 /* TODO: implement register parameter: */
432                 /* reg = get reg for param i;          */
433                 /* be_abi_call_param_reg(abi, i, reg); */
434                 /* default: all parameters on stack */
435                 tp   = get_method_param_type(method_type, i);
436                 mode = get_type_mode(tp);
437                 be_abi_call_param_stack(abi, i, mode, 4, 0, 0);
438         }
439
440         /* TODO: set correct return register */
441         /* default: return value is in O0 resp. F0 */
442         if (get_method_n_ress(method_type) > 0) {
443                 tp   = get_method_res_type(method_type, 0);
444                 mode = get_type_mode(tp);
445
446                 be_abi_call_res_reg(abi, 0,
447                         mode_is_float(mode) ? &sparc_fp_regs[REG_F0] : &sparc_gp_regs[REG_O0]);
448         }
449 }
450
451 int sparc_to_appear_in_schedule(void *block_env, const ir_node *irn)
452 {
453         (void) block_env;
454
455         if(!is_sparc_irn(irn))
456                 return -1;
457
458         return 1;
459 }
460
461 /**
462  * Initializes the code generator interface.
463  */
464 static const arch_code_generator_if_t *sparc_get_code_generator_if(
465                 void *self)
466 {
467         (void) self;
468         return &sparc_code_gen_if;
469 }
470
471 list_sched_selector_t sparc_sched_selector;
472
473 /**
474  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
475  */
476 static const list_sched_selector_t *sparc_get_list_sched_selector(
477                 const void *self, list_sched_selector_t *selector)
478 {
479         (void) self;
480         (void) selector;
481
482         sparc_sched_selector = trivial_selector;
483         sparc_sched_selector.to_appear_in_schedule = sparc_to_appear_in_schedule;
484         return &sparc_sched_selector;
485 }
486
487 static const ilp_sched_selector_t *sparc_get_ilp_sched_selector(
488                 const void *self)
489 {
490         (void) self;
491         return NULL;
492 }
493
494 /**
495  * Returns the necessary byte alignment for storing a register of given class.
496  */
497 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
498 {
499         ir_mode *mode = arch_register_class_mode(cls);
500         return get_mode_size_bytes(mode);
501 }
502
503 /**
504  * Returns the libFirm configuration parameter for this backend.
505  */
506 static const backend_params *sparc_get_backend_params(void) {
507         static backend_params p = {
508                 0,     /* no dword lowering */
509                 0,     /* no inline assembly */
510                 NULL,  /* will be set later */
511                 NULL,  /* no creator function */
512                 NULL,  /* context for create_intrinsic_fkt */
513                 NULL,  /* parameter for if conversion */
514                 NULL,  /* float arithmetic mode */
515                 0,     /* no trampoline support: size 0 */
516                 0,     /* no trampoline support: align 0 */
517                 NULL,  /* no trampoline support: no trampoline builder */
518                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
519         };
520         return &p;
521 }
522
523 static const be_execution_unit_t ***sparc_get_allowed_execution_units(
524                 const ir_node *irn)
525 {
526         (void) irn;
527         /* TODO */
528         assert(0);
529         return NULL;
530 }
531
532 static const be_machine_t *sparc_get_machine(const void *self)
533 {
534         (void) self;
535         /* TODO */
536         assert(0);
537         return NULL;
538 }
539
540 static ir_graph **sparc_get_backend_irg_list(const void *self,
541                                                 ir_graph ***irgs)
542 {
543         (void) self;
544         (void) irgs;
545         return NULL;
546 }
547
548 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
549 {
550         (void) c;
551         return ASM_CONSTRAINT_FLAG_INVALID;
552 }
553
554 static int sparc_is_valid_clobber(const char *clobber)
555 {
556         (void) clobber;
557         return 0;
558 }
559
560 const arch_isa_if_t sparc_isa_if = {
561         sparc_init,
562         sparc_done,
563         NULL,                /* handle intrinsics */
564         sparc_get_n_reg_class,
565         sparc_get_reg_class,
566         sparc_get_reg_class_for_mode,
567         sparc_get_call_abi,
568         sparc_get_code_generator_if,
569         sparc_get_list_sched_selector,
570         sparc_get_ilp_sched_selector,
571         sparc_get_reg_class_alignment,
572     sparc_get_backend_params,
573         sparc_get_allowed_execution_units,
574         sparc_get_machine,
575         sparc_get_backend_irg_list,
576         NULL,                    /* mark remat */
577         sparc_parse_asm_constraint,
578         sparc_is_valid_clobber
579 };
580
581 void be_init_arch_sparc(void)
582 {
583         be_register_isa_if("sparc", &sparc_isa_if);
584         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
585         sparc_init_transform();
586         sparc_init_emitter();
587 }
588 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc);