fix a bunch of warnings reported by cparser
[libfirm] / ir / be / sparc / sparc_cconv.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   calling convention helpers
23  * @author  Matthias Braun
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "sparc_cconv.h"
29 #include "irmode.h"
30 #include "irgwalk.h"
31 #include "typerep.h"
32 #include "xmalloc.h"
33 #include "util.h"
34 #include "error.h"
35 #include "gen_sparc_regalloc_if.h"
36
37 static const unsigned ignore_regs[] = {
38         REG_G0,
39         /* reserved for sparc ABI: */
40         REG_G5,
41         REG_G6,
42         REG_G7,
43
44         REG_SP,
45         REG_O7,
46         REG_FRAME_POINTER,
47         REG_I7,
48
49         REG_FPFLAGS,
50         REG_FLAGS,
51         REG_Y,
52 };
53
54 /**
55  * Maps an input register representing the i'th register input
56  * to the i'th register output.
57  */
58 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
59 {
60         unsigned idx = reg->global_index;
61         assert(REG_I0 <= idx && idx <= REG_I7);
62         idx += REG_O0 - REG_I0;
63         assert(REG_O0 <= idx && idx <= REG_O7);
64         return &sparc_registers[idx];
65 }
66
67 static void check_omit_fp(ir_node *node, void *env)
68 {
69         bool *can_omit_fp = (bool*) env;
70
71         /* omit-fp is not possible if:
72          *  - we have allocations on the stack
73          *  - we have calls (with the exception of tail-calls once we support them)
74          */
75         if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
76                         || (is_Free(node) && get_Free_where(node) == stack_alloc)
77                         || is_Call(node)) {
78                 *can_omit_fp = false;
79         }
80 }
81
82 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
83                                                       ir_graph *irg)
84 {
85         int                   stack_offset        = 0;
86         int                   n_param_regs        = ARRAY_SIZE(param_regs);
87         int                   n_float_result_regs = ARRAY_SIZE(float_result_regs);
88         bool                  omit_fp             = false;
89         reg_or_stackslot_t   *params;
90         reg_or_stackslot_t   *results;
91         int                   n_params;
92         int                   n_results;
93         int                   i;
94         int                   regnum;
95         int                   float_regnum;
96         calling_convention_t *cconv;
97
98         if (irg != NULL) {
99                 const be_options_t *options = be_get_irg_options(irg);
100                 omit_fp = options->omit_fp;
101                 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
102         }
103
104         /* determine how parameters are passed */
105         n_params = get_method_n_params(function_type);
106         regnum   = 0;
107         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
108
109         for (i = 0; i < n_params; ++i) {
110                 ir_type            *param_type = get_method_param_type(function_type,i);
111                 ir_mode            *mode       = get_type_mode(param_type);
112                 int                 bits       = get_mode_size_bits(mode);
113                 reg_or_stackslot_t *param      = &params[i];
114
115                 if (regnum < n_param_regs) {
116                         const arch_register_t *reg = param_regs[regnum++];
117                         if (irg == NULL || omit_fp)
118                                 reg = map_i_to_o_reg(reg);
119                         param->reg0 = reg;
120                 } else {
121                         param->type   = param_type;
122                         param->offset = stack_offset;
123                         /* increase offset 4 bytes so everything is aligned */
124                         stack_offset += bits > 32 ? bits/8 : 4;
125                         continue;
126                 }
127
128                 /* we might need a 2nd 32bit component (for 64bit or double values) */
129                 if (bits > 32) {
130                         if (bits > 64)
131                                 panic("only 32 and 64bit modes supported in sparc backend");
132
133                         if (regnum < n_param_regs) {
134                                 const arch_register_t *reg = param_regs[regnum++];
135                                 if (irg == NULL || omit_fp)
136                                         reg = map_i_to_o_reg(reg);
137                                 param->reg1 = reg;
138                         } else {
139                                 ir_mode *regmode = param_regs[0]->reg_class->mode;
140                                 ir_type *type    = get_type_for_mode(regmode);
141                                 param->type      = type;
142                                 param->offset    = stack_offset;
143                                 assert(get_mode_size_bits(regmode) == 32);
144                                 stack_offset += 4;
145                         }
146                 }
147         }
148
149         /* determine how results are passed */
150         n_results    = get_method_n_ress(function_type);
151         regnum       = 0;
152         float_regnum = 0;
153         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
154         for (i = 0; i < n_results; ++i) {
155                 ir_type            *result_type = get_method_res_type(function_type, i);
156                 ir_mode            *result_mode = get_type_mode(result_type);
157                 reg_or_stackslot_t *result      = &results[i];
158
159                 if (mode_is_float(result_mode)) {
160                         if (float_regnum >= n_float_result_regs) {
161                                 panic("Too many float results for sparc backend");
162                         } else {
163                                 const arch_register_t *reg = float_result_regs[float_regnum++];
164                                 result->reg0 = reg;
165                         }
166                 } else {
167                         if (get_mode_size_bits(result_mode) > 32) {
168                                 panic("Results with more than 32bits not supported by sparc backend yet");
169                         }
170
171                         if (regnum >= n_param_regs) {
172                                 panic("Too many results for sparc backend");
173                         } else {
174                                 const arch_register_t *reg = param_regs[regnum++];
175                                 if (irg == NULL || omit_fp)
176                                         reg = map_i_to_o_reg(reg);
177                                 result->reg0 = reg;
178                         }
179                 }
180         }
181
182         cconv                   = XMALLOCZ(calling_convention_t);
183         cconv->parameters       = params;
184         cconv->param_stack_size = stack_offset;
185         cconv->results          = results;
186         cconv->omit_fp          = omit_fp;
187
188         /* setup ignore register array */
189         if (irg != NULL) {
190                 be_irg_t       *birg      = be_birg_from_irg(irg);
191                 size_t          n_ignores = ARRAY_SIZE(ignore_regs);
192                 struct obstack *obst      = &birg->obst;
193                 size_t          r;
194
195                 assert(birg->allocatable_regs == NULL);
196                 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
197                 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
198                 for (r = 0; r < n_ignores; ++r) {
199                         rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
200                 }
201         }
202
203         return cconv;
204 }
205
206 void sparc_free_calling_convention(calling_convention_t *cconv)
207 {
208         free(cconv->parameters);
209         free(cconv->results);
210         free(cconv);
211 }