2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief calling convention helpers
23 * @author Matthias Braun
28 #include "sparc_cconv.h"
35 #include "gen_sparc_regalloc_if.h"
36 #include "bitfiddle.h"
38 static const unsigned ignore_regs[] = {
40 /* used in case an address offset does not fit into an immediate: */
42 /* reserved for SPARC ABI: */
57 static const arch_register_t* const param_regs[] = {
58 &sparc_registers[REG_I0],
59 &sparc_registers[REG_I1],
60 &sparc_registers[REG_I2],
61 &sparc_registers[REG_I3],
62 &sparc_registers[REG_I4],
63 &sparc_registers[REG_I5],
65 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
67 static const arch_register_t* const float_result_regs[] = {
68 &sparc_registers[REG_F0],
69 &sparc_registers[REG_F1],
70 &sparc_registers[REG_F2],
71 &sparc_registers[REG_F3],
72 &sparc_registers[REG_F4],
73 &sparc_registers[REG_F5],
74 &sparc_registers[REG_F6],
75 &sparc_registers[REG_F7],
77 static arch_register_req_t float_result_reqs_double[8];
78 static arch_register_req_t float_result_reqs_quad[8];
80 static const unsigned caller_saves[] = {
123 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
125 static const unsigned returns_twice_saved[] = {
130 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
133 * Maps an input register representing the i'th register input
134 * to the i'th register output.
136 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
138 unsigned idx = reg->global_index;
139 assert(REG_I0 <= idx && idx <= REG_I7);
140 idx += REG_O0 - REG_I0;
141 assert(REG_O0 <= idx && idx <= REG_O7);
142 return &sparc_registers[idx];
145 static void check_omit_fp(ir_node *node, void *env)
147 bool *can_omit_fp = (bool*) env;
149 /* omit-fp is not possible if:
150 * - we have allocations on the stack
151 * - we have calls (with the exception of tail-calls once we support them)
153 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
154 || (is_Free(node) && get_Free_where(node) == stack_alloc)
156 *can_omit_fp = false;
160 static unsigned determine_n_float_regs(ir_mode *mode)
162 unsigned bits = get_mode_size_bits(mode);
171 panic("sparc: Unexpected floatingpoint mode %+F", mode);
175 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
178 unsigned stack_offset = 0;
179 unsigned n_param_regs_used = 0;
180 int n_param_regs = ARRAY_SIZE(param_regs);
181 unsigned n_float_result_regs = ARRAY_SIZE(float_result_regs);
182 bool omit_fp = false;
183 mtp_additional_properties mtp
184 = get_method_additional_properties(function_type);
185 reg_or_stackslot_t *params;
186 reg_or_stackslot_t *results;
191 unsigned float_regnum;
192 unsigned n_reg_results = 0;
193 calling_convention_t *cconv;
194 unsigned *caller_saves;
197 const be_options_t *options = be_get_irg_options(irg);
198 omit_fp = options->omit_fp;
199 /* our current vaarg handling needs the standard space to store the
201 if (get_method_variadicity(function_type) == variadicity_variadic)
203 if (omit_fp == true) {
204 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
208 caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
209 if (mtp & mtp_property_returns_twice) {
210 rbitset_copy(caller_saves, default_returns_twice_saves,
213 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
216 /* determine how parameters are passed */
217 n_params = get_method_n_params(function_type);
219 params = XMALLOCNZ(reg_or_stackslot_t, n_params);
221 for (i = 0; i < n_params; ++i) {
222 ir_type *param_type = get_method_param_type(function_type,i);
225 reg_or_stackslot_t *param;
227 if (is_compound_type(param_type))
228 panic("sparc: compound arguments not supported yet");
230 mode = get_type_mode(param_type);
231 bits = get_mode_size_bits(mode);
235 (get_method_calling_convention(function_type) & cc_compound_ret)) {
236 assert(mode_is_reference(mode) && bits == 32);
237 /* special case, we have reserved space for this on the between
239 param->type = param_type;
240 param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
244 if (regnum < n_param_regs) {
245 const arch_register_t *reg = param_regs[regnum];
246 if (irg == NULL || omit_fp)
247 reg = map_i_to_o_reg(reg);
249 param->req0 = reg->single_req;
250 param->reg_offset = regnum;
253 param->type = param_type;
254 param->offset = stack_offset;
255 /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
256 stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
260 /* we might need a 2nd 32bit component (for 64bit or double values) */
263 panic("only 32 and 64bit modes supported in sparc backend");
265 if (regnum < n_param_regs) {
266 const arch_register_t *reg = param_regs[regnum];
267 if (irg == NULL || omit_fp)
268 reg = map_i_to_o_reg(reg);
270 param->req1 = reg->single_req;
273 ir_mode *regmode = param_regs[0]->reg_class->mode;
274 ir_type *type = get_type_for_mode(regmode);
276 param->offset = stack_offset;
277 assert(get_mode_size_bits(regmode) == 32);
278 stack_offset += SPARC_REGISTER_SIZE;
282 n_param_regs_used = regnum;
284 /* determine how results are passed */
285 n_results = get_method_n_ress(function_type);
288 results = XMALLOCNZ(reg_or_stackslot_t, n_results);
289 for (i = 0; i < n_results; ++i) {
290 ir_type *result_type = get_method_res_type(function_type, i);
291 ir_mode *result_mode = get_type_mode(result_type);
292 reg_or_stackslot_t *result = &results[i];
294 if (mode_is_float(result_mode)) {
295 unsigned n_regs = determine_n_float_regs(result_mode);
296 unsigned next_reg = round_up2(float_regnum, n_regs);
298 if (next_reg >= n_float_result_regs) {
299 panic("Too many float results for sparc backend");
301 const arch_register_t *reg = float_result_regs[next_reg];
302 rbitset_clear(caller_saves, reg->global_index);
303 result->reg_offset = i;
305 result->req0 = reg->single_req;
306 } else if (n_regs == 2) {
307 result->req0 = &float_result_reqs_double[next_reg];
308 rbitset_clear(caller_saves, reg->global_index+1);
309 } else if (n_regs == 4) {
310 result->req0 = &float_result_reqs_quad[next_reg];
311 rbitset_clear(caller_saves, reg->global_index+1);
312 rbitset_clear(caller_saves, reg->global_index+2);
313 rbitset_clear(caller_saves, reg->global_index+3);
315 panic("invalid number of registers in sparc float result");
317 float_regnum = next_reg + n_regs;
322 if (get_mode_size_bits(result_mode) > 32) {
323 panic("Results with more than 32bits not supported by sparc backend yet");
326 if (regnum >= n_param_regs) {
327 panic("Too many results for sparc backend");
329 const arch_register_t *reg = param_regs[regnum++];
330 if (irg == NULL || omit_fp)
331 reg = map_i_to_o_reg(reg);
332 result->req0 = reg->single_req;
333 result->reg_offset = i;
334 rbitset_clear(caller_saves, reg->global_index);
340 cconv = XMALLOCZ(calling_convention_t);
341 cconv->parameters = params;
342 cconv->param_stack_size = stack_offset;
343 cconv->n_param_regs = n_param_regs_used;
344 cconv->results = results;
345 cconv->omit_fp = omit_fp;
346 cconv->caller_saves = caller_saves;
347 cconv->n_reg_results = n_reg_results;
349 /* setup ignore register array */
351 be_irg_t *birg = be_birg_from_irg(irg);
352 size_t n_ignores = ARRAY_SIZE(ignore_regs);
353 struct obstack *obst = &birg->obst;
356 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
357 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
358 for (r = 0; r < n_ignores; ++r) {
359 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
366 void sparc_free_calling_convention(calling_convention_t *cconv)
368 free(cconv->parameters);
369 free(cconv->results);
370 free(cconv->caller_saves);
374 void sparc_cconv_init(void)
377 for (i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
378 rbitset_set(default_caller_saves, caller_saves[i]);
381 rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
382 for (i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
383 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
385 for (i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
386 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
389 for (i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
390 arch_register_req_t *req = &float_result_reqs_double[i];
391 *req = *float_result_regs[i]->single_req;
392 req->type |= arch_register_req_type_aligned;
395 for (i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
396 arch_register_req_t *req = &float_result_reqs_quad[i];
397 *req = *float_result_regs[i]->single_req;
398 req->type |= arch_register_req_type_aligned;