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: */
58 static const arch_register_t* const param_regs[] = {
59 &sparc_registers[REG_I0],
60 &sparc_registers[REG_I1],
61 &sparc_registers[REG_I2],
62 &sparc_registers[REG_I3],
63 &sparc_registers[REG_I4],
64 &sparc_registers[REG_I5],
66 COMPILETIME_ASSERT(ARRAY_SIZE(param_regs) == SPARC_N_PARAM_REGS, sparcparamregs)
68 static const arch_register_t* const float_result_regs[] = {
69 &sparc_registers[REG_F0],
70 &sparc_registers[REG_F1],
71 &sparc_registers[REG_F2],
72 &sparc_registers[REG_F3],
73 &sparc_registers[REG_F4],
74 &sparc_registers[REG_F5],
75 &sparc_registers[REG_F6],
76 &sparc_registers[REG_F7],
78 static arch_register_req_t float_result_reqs_double[8];
79 static arch_register_req_t float_result_reqs_quad[8];
81 static const unsigned caller_saves[] = {
126 static unsigned default_caller_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
128 static const unsigned returns_twice_saved[] = {
133 static unsigned default_returns_twice_saves[BITSET_SIZE_ELEMS(N_SPARC_REGISTERS)];
136 * Maps an input register representing the i'th register input
137 * to the i'th register output.
139 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
141 unsigned idx = reg->global_index;
142 assert(REG_I0 <= idx && idx <= REG_I7);
143 idx += REG_O0 - REG_I0;
144 assert(REG_O0 <= idx && idx <= REG_O7);
145 return &sparc_registers[idx];
148 static void check_omit_fp(ir_node *node, void *env)
150 /* omit-fp is not possible if:
151 * - we have allocations on the stack
152 * - we have calls (with the exception of tail-calls once we support them)
154 if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
155 || (is_Free(node) && get_Free_where(node) == stack_alloc)
157 bool *can_omit_fp = (bool*) env;
158 *can_omit_fp = false;
162 static unsigned determine_n_float_regs(ir_mode *mode)
164 unsigned bits = get_mode_size_bits(mode);
173 panic("Unexpected floatingpoint mode %+F", mode);
177 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
180 bool omit_fp = false;
182 omit_fp = be_options.omit_fp;
183 /* our current vaarg handling needs the standard space to store the
185 if (get_method_variadicity(function_type) == variadicity_variadic)
187 if (omit_fp == true) {
188 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
192 mtp_additional_properties mtp
193 = get_method_additional_properties(function_type);
194 unsigned *caller_saves = rbitset_malloc(N_SPARC_REGISTERS);
195 if (mtp & mtp_property_returns_twice) {
196 rbitset_copy(caller_saves, default_returns_twice_saves,
199 rbitset_copy(caller_saves, default_caller_saves, N_SPARC_REGISTERS);
202 /* determine how parameters are passed */
203 int n_params = get_method_n_params(function_type);
205 reg_or_stackslot_t *params = XMALLOCNZ(reg_or_stackslot_t, n_params);
207 int n_param_regs = ARRAY_SIZE(param_regs);
208 unsigned stack_offset = 0;
209 for (int i = 0; i < n_params; ++i) {
210 ir_type *param_type = get_method_param_type(function_type,i);
213 reg_or_stackslot_t *param;
215 if (is_compound_type(param_type))
216 panic("compound arguments not supported yet");
218 mode = get_type_mode(param_type);
219 bits = get_mode_size_bits(mode);
223 (get_method_calling_convention(function_type) & cc_compound_ret)) {
224 assert(mode_is_reference(mode) && bits == 32);
225 /* special case, we have reserved space for this on the between
227 param->type = param_type;
228 param->offset = -SPARC_MIN_STACKSIZE+SPARC_AGGREGATE_RETURN_OFFSET;
232 if (regnum < n_param_regs) {
233 const arch_register_t *reg = param_regs[regnum];
234 if (irg == NULL || omit_fp)
235 reg = map_i_to_o_reg(reg);
237 param->req0 = reg->single_req;
238 param->reg_offset = regnum;
241 param->type = param_type;
242 param->offset = stack_offset;
243 /* increase offset by at least SPARC_REGISTER_SIZE bytes so everything is aligned */
244 stack_offset += bits > 8 * SPARC_REGISTER_SIZE ? bits / 8 : SPARC_REGISTER_SIZE;
248 /* we might need a 2nd 32bit component (for 64bit or double values) */
251 panic("only 32 and 64bit modes supported");
253 if (regnum < n_param_regs) {
254 const arch_register_t *reg = param_regs[regnum];
255 if (irg == NULL || omit_fp)
256 reg = map_i_to_o_reg(reg);
258 param->req1 = reg->single_req;
261 ir_mode *regmode = param_regs[0]->reg_class->mode;
262 ir_type *type = get_type_for_mode(regmode);
264 param->offset = stack_offset;
265 assert(get_mode_size_bits(regmode) == 32);
266 stack_offset += SPARC_REGISTER_SIZE;
270 unsigned n_param_regs_used = regnum;
272 /* determine how results are passed */
273 int n_results = get_method_n_ress(function_type);
274 unsigned float_regnum = 0;
275 unsigned n_reg_results = 0;
276 unsigned n_float_result_regs = ARRAY_SIZE(float_result_regs);
277 reg_or_stackslot_t *results = XMALLOCNZ(reg_or_stackslot_t, n_results);
279 for (int i = 0; i < n_results; ++i) {
280 ir_type *result_type = get_method_res_type(function_type, i);
281 ir_mode *result_mode = get_type_mode(result_type);
282 reg_or_stackslot_t *result = &results[i];
284 if (mode_is_float(result_mode)) {
285 unsigned n_regs = determine_n_float_regs(result_mode);
286 unsigned next_reg = round_up2(float_regnum, n_regs);
288 if (next_reg >= n_float_result_regs) {
289 panic("Too many float results");
291 const arch_register_t *reg = float_result_regs[next_reg];
292 rbitset_clear(caller_saves, reg->global_index);
293 result->reg_offset = i;
295 result->req0 = reg->single_req;
296 } else if (n_regs == 2) {
297 result->req0 = &float_result_reqs_double[next_reg];
298 rbitset_clear(caller_saves, reg->global_index+1);
299 } else if (n_regs == 4) {
300 result->req0 = &float_result_reqs_quad[next_reg];
301 rbitset_clear(caller_saves, reg->global_index+1);
302 rbitset_clear(caller_saves, reg->global_index+2);
303 rbitset_clear(caller_saves, reg->global_index+3);
305 panic("invalid number of registers in result");
307 float_regnum = next_reg + n_regs;
312 if (get_mode_size_bits(result_mode) > 32) {
313 panic("Results with more than 32bits not supported yet");
316 if (regnum >= n_param_regs) {
317 panic("Too many results");
319 const arch_register_t *reg = param_regs[regnum++];
320 if (irg == NULL || omit_fp)
321 reg = map_i_to_o_reg(reg);
322 result->req0 = reg->single_req;
323 result->reg_offset = i;
324 rbitset_clear(caller_saves, reg->global_index);
330 calling_convention_t *cconv = XMALLOCZ(calling_convention_t);
331 cconv->parameters = params;
332 cconv->param_stack_size = stack_offset;
333 cconv->n_param_regs = n_param_regs_used;
334 cconv->results = results;
335 cconv->omit_fp = omit_fp;
336 cconv->caller_saves = caller_saves;
337 cconv->n_reg_results = n_reg_results;
339 /* setup ignore register array */
341 be_irg_t *birg = be_birg_from_irg(irg);
342 size_t n_ignores = ARRAY_SIZE(ignore_regs);
343 struct obstack *obst = &birg->obst;
346 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_SPARC_REGISTERS);
347 rbitset_set_all(birg->allocatable_regs, N_SPARC_REGISTERS);
348 for (r = 0; r < n_ignores; ++r) {
349 rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
356 void sparc_free_calling_convention(calling_convention_t *cconv)
358 free(cconv->parameters);
359 free(cconv->results);
360 free(cconv->caller_saves);
364 void sparc_cconv_init(void)
366 for (size_t i = 0; i < ARRAY_SIZE(caller_saves); ++i) {
367 rbitset_set(default_caller_saves, caller_saves[i]);
370 rbitset_set_all(default_returns_twice_saves, N_SPARC_REGISTERS);
371 for (size_t i = 0; i < ARRAY_SIZE(returns_twice_saved); ++i) {
372 rbitset_clear(default_returns_twice_saves, returns_twice_saved[i]);
374 for (size_t i = 0; i < ARRAY_SIZE(ignore_regs); ++i) {
375 rbitset_clear(default_returns_twice_saves, ignore_regs[i]);
378 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_double); i += 2) {
379 arch_register_req_t *req = &float_result_reqs_double[i];
380 *req = *float_result_regs[i]->single_req;
381 req->type |= arch_register_req_type_aligned;
384 for (size_t i = 0; i < ARRAY_SIZE(float_result_reqs_quad); i += 4) {
385 arch_register_req_t *req = &float_result_reqs_quad[i];
386 *req = *float_result_regs[i]->single_req;
387 req->type |= arch_register_req_type_aligned;