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
27 #include "sparc_cconv.h"
34 #include "gen_sparc_regalloc_if.h"
35 #include "bitfiddle.h"
37 static const unsigned ignore_regs[] = {
39 /* used in case an address offset does not fit into an immediate: */
41 /* reserved for SPARC ABI: */
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],
64 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
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],
76 static arch_register_req_t float_result_reqs_double[8];
77 static arch_register_req_t float_result_reqs_quad[8];
79 static const unsigned caller_saves[] = {
122 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
124 static const unsigned returns_twice_saved[] = {
129 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
132 * Maps an input register representing the i'th register input
133 * to the i'th register output.
135 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
137 unsigned idx = reg->global_index;
138 assert(REG_I0 <= idx && idx <= REG_I7);
139 idx += REG_O0 - REG_I0;
140 assert(REG_O0 <= idx && idx <= REG_O7);
141 return &sparc_registers[idx];
144 static void check_omit_fp(ir_node *node, void *env)
146 bool *can_omit_fp = (bool*) env;
148 /* omit-fp is not possible if:
149 * - we have allocations on the stack
150 * - we have calls (with the exception of tail-calls once we support them)
152 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
153 || (is_Free(node) && get_Free_where(node) == stack_alloc)
155 *can_omit_fp = false;
159 static unsigned determine_n_float_regs(ir_mode *mode)
161 unsigned bits = get_mode_size_bits(mode);
170 panic("Unexpected floatingpoint mode %+F", mode);
174 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
177 unsigned stack_offset = 0;
178 unsigned n_param_regs_used = 0;
179 int n_param_regs = ARRAY_SIZE(param_regs);
180 unsigned n_float_result_regs = ARRAY_SIZE(float_result_regs);
181 bool omit_fp = false;
182 mtp_additional_properties mtp
183 = get_method_additional_properties(function_type);
184 reg_or_stackslot_t *params;
185 reg_or_stackslot_t *results;
190 unsigned float_regnum;
191 unsigned n_reg_results = 0;
192 calling_convention_t *cconv;
193 unsigned *caller_saves;
196 omit_fp = be_options.omit_fp;
197 /* our current vaarg handling needs the standard space to store the
199 if (get_method_variadicity(function_type) == variadicity_variadic)
201 if (omit_fp == true) {
202 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
206 caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
207 if (mtp & mtp_property_returns_twice) {
208 rbitset_copy(caller_saves, default_returns_twice_saves,
211 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
214 /* determine how parameters are passed */
215 n_params = get_method_n_params(function_type);
217 params = XMALLOCNZ(reg_or_stackslot_t, n_params);
219 for (i = 0; i < n_params; ++i) {
220 ir_type *param_type = get_method_param_type(function_type,i);
223 reg_or_stackslot_t *param;
225 if (is_compound_type(param_type))
226 panic("compound arguments not supported yet");
228 mode = get_type_mode(param_type);
229 bits = get_mode_size_bits(mode);
233 (get_method_calling_convention(function_type) & cc_compound_ret)) {
234 assert(mode_is_reference(mode) && bits == 32);
235 /* special case, we have reserved space for this on the between
237 param->type = param_type;
238 param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
242 if (regnum < n_param_regs) {
243 const arch_register_t *reg = param_regs[regnum];
244 if (irg == NULL || omit_fp)
245 reg = map_i_to_o_reg(reg);
247 param->req0 = reg->single_req;
248 param->reg_offset = regnum;
251 param->type = param_type;
252 param->offset = stack_offset;
253 /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
254 stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
258 /* we might need a 2nd 32bit component (for 64bit or double values) */
261 panic("only 32 and 64bit modes supported");
263 if (regnum < n_param_regs) {
264 const arch_register_t *reg = param_regs[regnum];
265 if (irg == NULL || omit_fp)
266 reg = map_i_to_o_reg(reg);
268 param->req1 = reg->single_req;
271 ir_mode *regmode = param_regs[0]->reg_class->mode;
272 ir_type *type = get_type_for_mode(regmode);
274 param->offset = stack_offset;
275 assert(get_mode_size_bits(regmode) == 32);
276 stack_offset += SPARC_REGISTER_SIZE;
280 n_param_regs_used = regnum;
282 /* determine how results are passed */
283 n_results = get_method_n_ress(function_type);
286 results = XMALLOCNZ(reg_or_stackslot_t, n_results);
287 for (i = 0; i < n_results; ++i) {
288 ir_type *result_type = get_method_res_type(function_type, i);
289 ir_mode *result_mode = get_type_mode(result_type);
290 reg_or_stackslot_t *result = &results[i];
292 if (mode_is_float(result_mode)) {
293 unsigned n_regs = determine_n_float_regs(result_mode);
294 unsigned next_reg = round_up2(float_regnum, n_regs);
296 if (next_reg >= n_float_result_regs) {
297 panic("Too many float results");
299 const arch_register_t *reg = float_result_regs[next_reg];
300 rbitset_clear(caller_saves, reg->global_index);
301 result->reg_offset = i;
303 result->req0 = reg->single_req;
304 } else if (n_regs == 2) {
305 result->req0 = &float_result_reqs_double[next_reg];
306 rbitset_clear(caller_saves, reg->global_index+1);
307 } else if (n_regs == 4) {
308 result->req0 = &float_result_reqs_quad[next_reg];
309 rbitset_clear(caller_saves, reg->global_index+1);
310 rbitset_clear(caller_saves, reg->global_index+2);
311 rbitset_clear(caller_saves, reg->global_index+3);
313 panic("invalid number of registers in result");
315 float_regnum = next_reg + n_regs;
320 if (get_mode_size_bits(result_mode) > 32) {
321 panic("Results with more than 32bits not supported yet");
324 if (regnum >= n_param_regs) {
325 panic("Too many results");
327 const arch_register_t *reg = param_regs[regnum++];
328 if (irg == NULL || omit_fp)
329 reg = map_i_to_o_reg(reg);
330 result->req0 = reg->single_req;
331 result->reg_offset = i;
332 rbitset_clear(caller_saves, reg->global_index);
338 cconv = XMALLOCZ(calling_convention_t);
339 cconv->parameters = params;
340 cconv->param_stack_size = stack_offset;
341 cconv->n_param_regs = n_param_regs_used;
342 cconv->results = results;
343 cconv->omit_fp = omit_fp;
344 cconv->caller_saves = caller_saves;
345 cconv->n_reg_results = n_reg_results;
347 /* setup ignore register array */
349 be_irg_t *birg = be_birg_from_irg(irg);
350 size_t n_ignores = ARRAY_SIZE(ignore_regs);
351 struct obstack *obst = &birg->obst;
354 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
355 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
356 for (r = 0; r < n_ignores; ++r) {
357 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
364 void sparc_free_calling_convention(calling_convention_t *cconv)
366 free(cconv->parameters);
367 free(cconv->results);
368 free(cconv->caller_saves);
372 void sparc_cconv_init(void)
375 for (i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
376 rbitset_set(default_caller_saves, caller_saves[i]);
379 rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
380 for (i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
381 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
383 for (i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
384 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
387 for (i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
388 arch_register_req_t *req = &float_result_reqs_double[i];
389 *req = *float_result_regs[i]->single_req;
390 req->type |= arch_register_req_type_aligned;
393 for (i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
394 arch_register_req_t *req = &float_result_reqs_quad[i];
395 *req = *float_result_regs[i]->single_req;
396 req->type |= arch_register_req_type_aligned;