c94fa45bebfaa453ef3de88fd89b2d6aa5c7f664
[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  */
25 #include "config.h"
26
27 #include "sparc_cconv.h"
28 #include "irmode.h"
29 #include "irgwalk.h"
30 #include "typerep.h"
31 #include "xmalloc.h"
32 #include "util.h"
33 #include "error.h"
34 #include "gen_sparc_regalloc_if.h"
35 #include "bitfiddle.h"
36
37 static const unsigned ignore_regs[] = {
38         REG_G0,
39         /* used in case an address offset does not fit into an immediate: */
40         REG_G4,
41         /* reserved for SPARC ABI: */
42         REG_G5,
43         REG_G6,
44         REG_G7,
45
46         REG_SP,
47         REG_O7,
48         REG_FRAME_POINTER,
49         REG_I7,
50
51         REG_FPFLAGS,
52         REG_FLAGS,
53         REG_Y,
54 };
55
56 static const arch_register_t* const param_regs[] = {
57         &sparc_registers[REG_I0],
58         &sparc_registers[REG_I1],
59         &sparc_registers[REG_I2],
60         &sparc_registers[REG_I3],
61         &sparc_registers[REG_I4],
62         &sparc_registers[REG_I5],
63 };
64 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
65
66 static const arch_register_t* const float_result_regs[] = {
67         &sparc_registers[REG_F0],
68         &sparc_registers[REG_F1],
69         &sparc_registers[REG_F2],
70         &sparc_registers[REG_F3],
71         &sparc_registers[REG_F4],
72         &sparc_registers[REG_F5],
73         &sparc_registers[REG_F6],
74         &sparc_registers[REG_F7],
75 };
76 static arch_register_req_t float_result_reqs_double[8];
77 static arch_register_req_t float_result_reqs_quad[8];
78
79 static const unsigned caller_saves[] = {
80         REG_G1,
81         REG_G2,
82         REG_G3,
83         REG_O0,
84         REG_O1,
85         REG_O2,
86         REG_O3,
87         REG_O4,
88         REG_O5,
89         REG_F0,
90         REG_F1,
91         REG_F2,
92         REG_F3,
93         REG_F4,
94         REG_F5,
95         REG_F6,
96         REG_F7,
97         REG_F8,
98         REG_F9,
99         REG_F10,
100         REG_F11,
101         REG_F12,
102         REG_F13,
103         REG_F14,
104         REG_F15,
105         REG_F16,
106         REG_F17,
107         REG_F18,
108         REG_F19,
109         REG_F20,
110         REG_F21,
111         REG_F22,
112         REG_F23,
113         REG_F24,
114         REG_F25,
115         REG_F26,
116         REG_F27,
117         REG_F28,
118         REG_F29,
119         REG_F30,
120         REG_F31,
121         REG_FLAGS,
122         REG_FPFLAGS,
123         REG_Y,
124 };
125 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
126
127 static const unsigned returns_twice_saved[] = {
128         REG_SP,
129         REG_FRAME_POINTER,
130         REG_I7
131 };
132 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
133
134 /**
135  * Maps an input register representing the i'th register input
136  * to the i'th register output.
137  */
138 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
139 {
140         unsigned idx = reg->global_index;
141         assert(REG_I0 <= idx && idx <= REG_I7);
142         idx += REG_O0 - REG_I0;
143         assert(REG_O0 <= idx && idx <= REG_O7);
144         return &sparc_registers[idx];
145 }
146
147 static void check_omit_fp(ir_node *node, void *env)
148 {
149         bool *can_omit_fp = (bool*) env;
150
151         /* omit-fp is not possible if:
152          *  - we have allocations on the stack
153          *  - we have calls (with the exception of tail-calls once we support them)
154          */
155         if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
156                         || (is_Free(node) && get_Free_where(node) == stack_alloc)
157                         || is_Call(node)) {
158                 *can_omit_fp = false;
159         }
160 }
161
162 static unsigned determine_n_float_regs(ir_mode *mode)
163 {
164         unsigned bits = get_mode_size_bits(mode);
165         switch (bits) {
166         case 32:
167                 return 1;
168         case 64:
169                 return 2;
170         case 128:
171                 return 4;
172         default:
173                 panic("Unexpected floatingpoint mode %+F", mode);
174         }
175 }
176
177 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
178                                                       ir_graph *irg)
179 {
180         unsigned              stack_offset        = 0;
181         unsigned              n_param_regs_used   = 0;
182         int                   n_param_regs        = ARRAY_SIZE(param_regs);
183         unsigned              n_float_result_regs = ARRAY_SIZE(float_result_regs);
184         bool                  omit_fp             = false;
185         mtp_additional_properties mtp
186                 = get_method_additional_properties(function_type);
187         reg_or_stackslot_t   *params;
188         reg_or_stackslot_t   *results;
189         int                   n_params;
190         int                   n_results;
191         int                   i;
192         int                   regnum;
193         unsigned              float_regnum;
194         unsigned              n_reg_results = 0;
195         calling_convention_t *cconv;
196         unsigned             *caller_saves;
197
198         if (irg != NULL) {
199                 omit_fp = be_options.omit_fp;
200                 /* our current vaarg handling needs the standard space to store the
201                  * args 0-5 in it */
202                 if (get_method_variadicity(function_type) == variadicity_variadic)
203                         omit_fp = false;
204                 if (omit_fp == true) {
205                         irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
206                 }
207         }
208
209         caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
210         if (mtp & mtp_property_returns_twice) {
211                 rbitset_copy(caller_saves, default_returns_twice_saves,
212                              N_SPARC_REGISTERS);
213         } else {
214                 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
215         }
216
217         /* determine how parameters are passed */
218         n_params = get_method_n_params(function_type);
219         regnum   = 0;
220         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
221
222         for (i = 0; i < n_params; ++i) {
223                 ir_type            *param_type = get_method_param_type(function_type,i);
224                 ir_mode            *mode;
225                 int                 bits;
226                 reg_or_stackslot_t *param;
227
228                 if (is_compound_type(param_type))
229                         panic("compound arguments not supported yet");
230
231                 mode  = get_type_mode(param_type);
232                 bits  = get_mode_size_bits(mode);
233                 param = &params[i];
234
235                 if (i == 0 &&
236                     (get_method_calling_convention(function_type) & cc_compound_ret)) {
237                         assert(mode_is_reference(mode) && bits == 32);
238                         /* special case, we have reserved space for this on the between
239                          * type */
240                         param->type   = param_type;
241                         param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
242                         continue;
243                 }
244
245                 if (regnum < n_param_regs) {
246                         const arch_register_t *reg = param_regs[regnum];
247                         if (irg == NULL || omit_fp)
248                                 reg = map_i_to_o_reg(reg);
249                         param->reg0       = reg;
250                         param->req0       = reg->single_req;
251                         param->reg_offset = regnum;
252                         ++regnum;
253                 } else {
254                         param->type   = param_type;
255                         param->offset = stack_offset;
256                         /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
257                         stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
258                         continue;
259                 }
260
261                 /* we might need a 2nd 32bit component (for 64bit or double values) */
262                 if (bits > 32) {
263                         if (bits > 64)
264                                 panic("only 32 and 64bit modes supported");
265
266                         if (regnum < n_param_regs) {
267                                 const arch_register_t *reg = param_regs[regnum];
268                                 if (irg == NULL || omit_fp)
269                                         reg = map_i_to_o_reg(reg);
270                                 param->reg1       = reg;
271                                 param->req1       = reg->single_req;
272                                 ++regnum;
273                         } else {
274                                 ir_mode *regmode = param_regs[0]->reg_class->mode;
275                                 ir_type *type    = get_type_for_mode(regmode);
276                                 param->type      = type;
277                                 param->offset    = stack_offset;
278                                 assert(get_mode_size_bits(regmode) == 32);
279                                 stack_offset += SPARC_REGISTER_SIZE;
280                         }
281                 }
282         }
283         n_param_regs_used = regnum;
284
285         /* determine how results are passed */
286         n_results    = get_method_n_ress(function_type);
287         regnum       = 0;
288         float_regnum = 0;
289         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
290         for (i = 0; i < n_results; ++i) {
291                 ir_type            *result_type = get_method_res_type(function_type, i);
292                 ir_mode            *result_mode = get_type_mode(result_type);
293                 reg_or_stackslot_t *result      = &results[i];
294
295                 if (mode_is_float(result_mode)) {
296                         unsigned n_regs   = determine_n_float_regs(result_mode);
297                         unsigned next_reg = round_up2(float_regnum, n_regs);
298
299                         if (next_reg >= n_float_result_regs) {
300                                 panic("Too many float results");
301                         } else {
302                                 const arch_register_t *reg = float_result_regs[next_reg];
303                                 rbitset_clear(caller_saves, reg->global_index);
304                                 result->reg_offset = i;
305                                 if (n_regs == 1) {
306                                         result->req0 = reg->single_req;
307                                 } else if (n_regs == 2) {
308                                         result->req0 = &float_result_reqs_double[next_reg];
309                                         rbitset_clear(caller_saves, reg->global_index+1);
310                                 } else if (n_regs == 4) {
311                                         result->req0 = &float_result_reqs_quad[next_reg];
312                                         rbitset_clear(caller_saves, reg->global_index+1);
313                                         rbitset_clear(caller_saves, reg->global_index+2);
314                                         rbitset_clear(caller_saves, reg->global_index+3);
315                                 } else {
316                                         panic("invalid number of registers in result");
317                                 }
318                                 float_regnum = next_reg + n_regs;
319
320                                 ++n_reg_results;
321                         }
322                 } else {
323                         if (get_mode_size_bits(result_mode) > 32) {
324                                 panic("Results with more than 32bits not supported yet");
325                         }
326
327                         if (regnum >= n_param_regs) {
328                                 panic("Too many results");
329                         } else {
330                                 const arch_register_t *reg = param_regs[regnum++];
331                                 if (irg == NULL || omit_fp)
332                                         reg = map_i_to_o_reg(reg);
333                                 result->req0       = reg->single_req;
334                                 result->reg_offset = i;
335                                 rbitset_clear(caller_saves, reg->global_index);
336                                 ++n_reg_results;
337                         }
338                 }
339         }
340
341         cconv                   = XMALLOCZ(calling_convention_t);
342         cconv->parameters       = params;
343         cconv->param_stack_size = stack_offset;
344         cconv->n_param_regs     = n_param_regs_used;
345         cconv->results          = results;
346         cconv->omit_fp          = omit_fp;
347         cconv->caller_saves     = caller_saves;
348         cconv->n_reg_results    = n_reg_results;
349
350         /* setup ignore register array */
351         if (irg != NULL) {
352                 be_irg_t       *birg      = be_birg_from_irg(irg);
353                 size_t          n_ignores = ARRAY_SIZE(ignore_regs);
354                 struct obstack *obst      = &birg->obst;
355                 size_t          r;
356
357                 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
358                 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
359                 for (r = 0; r < n_ignores; ++r) {
360                         rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
361                 }
362         }
363
364         return cconv;
365 }
366
367 void sparc_free_calling_convention(calling_convention_t *cconv)
368 {
369         free(cconv->parameters);
370         free(cconv->results);
371         free(cconv->caller_saves);
372         free(cconv);
373 }
374
375 void sparc_cconv_init(void)
376 {
377         size_t i;
378         for (i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
379                 rbitset_set(default_caller_saves, caller_saves[i]);
380         }
381
382         rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
383         for (i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
384                 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
385         }
386         for (i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
387                 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
388         }
389
390         for (i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
391                 arch_register_req_t *req = &float_result_reqs_double[i];
392                 *req = *float_result_regs[i]->single_req;
393                 req->type |= arch_register_req_type_aligned;
394                 req->width = 2;
395         }
396         for (i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
397                 arch_register_req_t *req = &float_result_reqs_quad[i];
398                 *req = *float_result_regs[i]->single_req;
399                 req->type |= arch_register_req_type_aligned;
400                 req->width = 4;
401         }
402 }